Workshop context
Before this workshop
- In the previous workshop, you created files for three low-level components (
Button
,Card
andSpinner
).
This workshop
- In this workshop, we’ll add props with types to the
Button
andCard
components, along with a skeleton JSX structure.
After this workshop
- The application itself will be created in the next two series, Next.js Data Fetching and AI Style Generator .
- For context, at the end of the AI Style Generator series, the application will look like this. You can see the
Button
variants in the upper part, and a orangeCard
in the lower part.
Workshop goal
By the end of this workshop:
- Both the
Button
andCard
components will be set up to take props with defined types. Card
will have these required props:textColor
: color of the text for the cardbackgroundColor
: color of the background for the card
Button
will have this optional prop:variant
: either “filled” (a button with a dark-colored background) or “outline” (a button with a light-colored background that’s similar to the page background). The defaultvariant
value should be “filled”.
- Both of the components will also take
children
as one of the props, and return an element that wraps thechildren
.- See screenshot and description above for a preview of these components in the eventual UI.
- Don’t worry about styling for now. That will come in future workshops in this series.
Hints
Hint 1
For the Card
component, both textColor
and backgroundColor
can be typed as string
.
For the Button
component, variant
can be defined as one of the two specific strings, like this:
Button.tsx
Hint 2
The top-level element for the Button
component should be button
. Using semantic HTML , the top level component for the Card
component can be article
. A card fits the “independent item of content” description in the MDN docs .
Hint 3
To add the children
prop, you can use React.ComponentProps
(as described in Matt Pocock’s article ):
Button.tsx
Then you can destructure any props that a button
element accepts. For example, children
:
Button.tsx
Hint 4
To add the children to the return value, put them inside the parent element that’s returned. for example:
Button.tsx
Hint 5
To make the variant
prop optional, add a ?
to the type declaration:
Button.tsx
Specify “filled” as the default value when the props are destructured:
Button.tsx
Resources
- Matt Pocock’s article on TypeScript for React props
- Matt Pocock on type vs interface for React props
- TypeScript docs for literal types
- TypeScript docs for optional properties for TypeScript objects
- React docs on JSX children
- MDN default values for destructuring assignment