Overview
You will learn
- How to import UI5 Web Components into your application
- Learn about the usage of props
- How to use event handling
Prerequisites
- Complete the tutorial Get Started with UI5 Web Components for React.
Steps
Intro
You will now integrate the first UI5 Web Component into your App. You can find all available UI5 Web Components for React in the Storybook. There you can try out the different components and also take a look at the coding and the available props.
First you need to import the components you want to use.
You can check out all available components in the Storybook. Take your time to play around a little, change some props and take a look at the coding.
Start with importing a
Cardcomponent into yourMyApp.tsxfile.TypeScriptimport { Card } from "@ui5/webcomponents-react";Now it’s time to use the
Cardcomponent. Replace the content of yourMyAppfunction with the following code.TypeScriptreturn ( <div> <Card>This is the content area of the Card</Card> </div> );For now, the
Cardonly contains some text aschildren, but in the Storybook, you can see that aCardcan receive many props.Your webpage should now look like this:

Card01 Your
MyApp.tsxfile should now look like this :TypeScriptimport { Card } from "@ui5/webcomponents-react"; export function MyApp() { return ( <div> <Card>This is the content area of the Card</Card> </div> ); }
The heading area of the Card component is empty, this is because it didn’t receive the header prop. The header prop itself expects another component, the CardHeader.
Import the
CardHeader.TypeScriptimport { Card, CardHeader } from "@ui5/webcomponents-react";Add the
CardHeadercomponent to yourCardand give it a title by setting thetitleTextprop:TypeScript<div> <Card header={<CardHeader titleText="Card" />}> This is the content area of the Card </Card> </div>Our application already applies the SAP defined
font-familyfor all texts that don’t implementfont-familythemselves via theindex.cssfile, which is why the inner text already has the right font. However, to enable this on a single text without using CSS, you can use theTextcomponent. Let’s wrap the text content of theCardinside theTextcomponent:TypeScriptimport { Card, CardHeader, Text } from "@ui5/webcomponents-react";TypeScript<div> <Card header={<CardHeader titleText="Card" />}> <Text>This is the content area of the Card</Text> </Card> </div>
In this step, we will only apply inline-styling. You can also style your component using CSS (modules), but this and many more information regarding the styling approach of UI5 Web Components (for React) will be covered in part six of the tutorial series.
The Card currently spreads across the whole screen, this behavior is intended, so it takes up the whole space of its container.
To restrict the
widthof theCard, add thestyleprop.TypeScript<Card header={<CardHeader titleText="Card" />} style={{ width: "300px" }}> <Text>This is the content area of the Card</Text> </Card>
Card02 The content of the card is way too close to the border of the
Card, so apaddingis needed. You can define your own spacing, or use the standard SAP spacing variables. In this example we’re using one of the global CSS Variables from the theming-base-content repo, which are already included when using UI5 Web Components.The CSS var in question is
--sapContent_Space_S(1rem) and we’re going to apply it via inline-style again:TypeScript<Text style={{padding: "var(--sapContent_Space_S)"}}> This is the content area of the Card </Text>Hereby you get a standardized content-padding.
After this step
MyApp.tsxshould look like this:TypeScriptimport { Card, CardHeader, Text } from "@ui5/webcomponents-react"; export function MyApp() { return ( <div> <Card header={<CardHeader titleText="Card" />} style={{ width: "300px" }}> <Text style={{ padding: "var(--sapContent_Space_S)" }}> This is the content area of the Card </Text> </Card> </div> ); }Your application should look like this:

Card03
The
Cardheader can also be interactive, to enable this setinteractivetotrue.TypeScript<Card header={<CardHeader titleText="Card" interactive />} ... </Card>We didn’t pass a value to
interactive, because it defaults to true if the value is omitted.When you now click or focus the header, the appropriate styles are applied, so users know they can interact with it.
To make the header react to a click (or SPACE/ENTER press), add a function as value to the
onClickprop.TypeScript<Card header={ <CardHeader titleText="Card" interactive onClick={handleHeaderClick} /> } style={{ width: "300px" }} > ... </Card>Now, add the callback function right in the beginning of the component (definition function):
TypeScriptexport function MyApp() { const handleHeaderClick = () => { alert("Header clicked"); }; ...The file should now look like this:
TypeScriptimport { Card, CardHeader, Text } from "@ui5/webcomponents-react"; export function MyApp() { const handleHeaderClick = () => { alert("Header clicked"); }; return ( <div> <Card header={ <CardHeader titleText="Card" interactive onClick={handleHeaderClick} /> } style={{ width: "300px" }} > <Text style={{ padding: "var(--sapContent_Space_S)" }}> This is the content area of the Card </Text> </Card> </div> ); }Now the header opens an alert box on click.
Resources
Discussion
Share feedback on this tutorial or join the conversation in SAP Community.