Finalize the UI
In this section, we’ll explore UI containers to enable us to layout the UI components better. We’ll also discuss how to reference images hosted from an external source.
Add ample spacing between the header and the Text components
-
Update the content body in
src/frontend/index.jsxby encapsulating them inside a Box component. First addBoxto the import statement.import ForgeReconciler, { Text, Heading, Box } from '@forge/react'; -
Move the
Textcomponents inside theBox.<> <Heading size='medium'>Welcome, {data ? data[0].name : '[Loading]'}!</Heading> <Box> <Text>Life expectancy: {data ? data[0].min_life_expectancy : '[Loading]' } – {data ? data[0].max_life_expectancy : '[Loading]' } years</Text> <Text>Height: {data ? data[0].max_weight_female : '[Loading]' } inches</Text> <Text>Weight: {data ? data[0].max_height_female : '[Loading]' } lbs</Text> </Box> </> -
Let’s see it in action. Let’s run the tunnel.
forge tunnel -
Use the
paddingBlockproperty ofBoxto add space between the header and the content.<> ... <Box paddingBlock='space.200'> ... </Box> </>
Add an Image component and reference the image from an external source
-
To use the Image component, first add it to the import
import ForgeReconciler, { Text, Heading, Box, Image } from '@forge/react'; -
Add the Image component and use a chihuahua stock image from API Ninjas as source
<> <Heading size='medium'>Welcome, {data ? data[0].name : '[Loading]'}!</Heading> <Image src="https://api-ninjas.com/images/dogs/chihuahua.jpg"/> <Box paddingBlock='space.200'> <Text>Life expectancy: {data ? data[0].min_life_expectancy : '[Loading]' } – {data ? data[0].max_life_expectancy : '[Loading]' } years</Text> <Text>Height: {data ? data[0].max_weight_female : '[Loading]' } inches</Text> <Text>Weight: {data ? data[0].max_height_female : '[Loading]' } lbs</Text> </Box> </> -
If your tunnel is running, you will get a permission-related error. Don’t worry, this is EXPECTED.
Fix the error. Forge linter to the rescue!
-
Verify your
manifest.ymlif you indeed do not have the permissions defined according to the error message we got. -
If your tunnel is still running, stop the tunnel by pressing
CTRL + C. -
To fix the error, we’ll use a special linter command to automatically add the missing permissions. Go to the command line, and inside your project directory run the command
forge lint --fix -
Check your
manifest.ymlagain and ensure that the necessary external permission forapi-ninjas.comhas been added. Your manifest file’s permission section should look like thispermissions: external: fetch: backend: - address: api.api-ninjas.com images: - address: api-ninjas.com scopes: [] -
Since you changed something in your manifest file, you need to redeploy your app and upgrade the installation.
forge deploy --approve MAJOR_VERSION_RULEforge install --upgradeYour app will be upgraded and additionally exchange data with the following urls: - api-ninjas.com ? Do you want to continue? Yes? Are you sure you want to proceed? Yes -
Once installation upgrade is done, verify that your app is working as expected.
Update the dog image to make it dynamic
-
Before we update the UI, let’s run the tunnel again so we can see the UI changes in real time
forge tunnel -
Checking the API response payload, we can see these properties:
[ { "image_link": "https://api-ninjas.com/images/dogs/chihuahua.jpg", "good_with_children": 1, "good_with_other_dogs": 3, "shedding": 2, ... } ] -
In
src/frontend/index.jsx, Replace the static image with the image being returned by the API. Add analtproperty as an alternative display if the image has not been loaded yet<Image src={data ? data[0].image_link : "https://api-ninjas.com/images/dogs/loading.jpg" } alt='[Loading]'/> {/* Replace the line below */} {/* <Image src="https://api-ninjas.com/images/dogs/chihuahua.jpg"/> */} -
After saving the
index.jsxfile, you should be able to see a glorious image of a chihuahua
Finishing touches: align the image with the text content
-
Use the Inline component. Add it to the import statement
import ForgeReconciler, { Text, Heading, Box, Image, Inline } from '@forge/react'; -
Place the
ImageandBoxcomponents insideInline<> <Heading size='medium'>Welcome, {data ? data[0].name : '[Loading]'}!</Heading> <Inline> <Image src={data ? data[0].image_link : "https://api-ninjas.com/images/dogs/loading.jpg" } alt='[Loading]'/> <Box paddingBlock='space.200'> <Text>Life expectancy: {data ? data[0].min_life_expectancy : '[Loading]' } – {data ? data[0].max_life_expectancy : '[Loading]' } years</Text> <Text>Height: {data ? data[0].max_weight_female : '[Loading]' } inches</Text> <Text>Weight: {data ? data[0].max_height_female : '[Loading]' } lbs</Text> </Box> </Inline> </>
Bonus exercises
-
Change the image size to
mediumas it looks too big for the panel. Go to the Image component documentation. -
Move the image to the right side of the text. Try using different Inline properties and see how it affects the UI.
-
Change the color, size, and/or weight of your
Textcomponents. Check out the Text properties.