You can find a summary of the code updates in this pull request . Read on for explanations.
1. Create component files
I adapted Josh W. Comeau’s new-component module to create component files with CSS Modules that follow his delightful file structure .
I like this file structure because:
- it’s pretty simple to import any component into any other component without having to hunt around.
- The directories keep everything contained for individual components, and the index.ts files mean you can use the directory path for import (like
import Button from "../Button") -- but all the significant code is in a file with the component name (
Button.tsx`, for example). - You could put all the code in the index.ts file, but then you might have a bunch of index.ts files open in your IDE tabs. It’s easier to identify which file you want if the filename matches the component name.
If you’re on MacOS or Linux (apologies to Windows users), the new-component
script creates the files for you. Windows users can read the README to see what files they need to create. Here are the commands I ran on MacOS:
Terminal commands
Windows users can create files manually, using the the new-component
README for reference.
2. Generate shadows
Josh has also created a Shadow Palette Generator which creates pretty nice-looking shadows.
We need to update the background color in the Shadow Palette Generator to be the app’s background color: #eff1ef
(defined in src/app/globals.css as the background-color
for the body
). Click on the background color swatch in the generator and add the new color in the color picker like this:
(Your color picker may look a little different depending on what browser and operating system you’re using.) All the other parameters can use the default (or play around with them to find something you like better!).
Define shadows in CSS
To make the shadows available to components, add the generated tokens to src/app/globals.css:
globals.css
I like to reference where I got code in comments, both for attribution reasons and so I can look up the source in the future without having to think too hard about where it came from.
How to use shadows
Now these shadows are available for us to use in CSS rules in the future, like this:
All right! We’re in good shape to write code for the rest of this series. Next up: props and TypeScript.
Workshops in this series
- Setup: files and shadows
- Props and TypeScript
- Delegated props
- Variants: Button component
- Centering: Card component
- Animations: loading spinner