Page views:
I’ve recently built a number of sites using a new tech stack, where pages are written in Markdown with embedded React components & theme-able (e.g. dark/light modes) styling.
I’ve written this explainer for the React user who’s a beginner to the world of CSS-in-JS, MDX, etc.
Note: the following is extracted from my tutorial on building a hackathon website.
pages/about.mdx
with some text in it, & you now have a high-performance static page at /about
on your site. This is magical.The site is then hosted on Netlify, because it’s free, easy to set up, automatically deploys whenever you push to GitHub, & just works (most of the time).
Check out the Hack Pennsylvania recap site codebase, open source & under the MIT License: github.com/hackpenn/site
The two important folders are pages
& components
—that’s where you’ll spend the vast majority of your time editing the site. Pages are written in MDX (or JSX), components in JSX (React components).
How styling works is a bit complex, but once you wrap your head around the system, it’s incredibly fast to work with.
It starts with a theme. The theme keeps all the primary “components” of your design—all the colors & fonts, but also the amounts of spacing (margin/padding in CSS), the font sizes used, the box-shadows used, etc, all in one place. By not having colors & font sizes & friends all spread out through your codebase, you can super easily modify the look of your whole site just by changing the theme, & you’ll also have fantastic consistency in your design, because you won’t have a random colors used in just one place or the like.
colors
set is the light mode, then when the dark mode is in use, the colors dynamically change to what you’ve defined in the theme.You won’t be writing direct CSS anywhere on the site. You’ll instead see sx={{
on the components & on the pages. A quick guide to what goes inside the sx
prop:
fontSize: 3
inside sx={{ … }}
, you won’t get the CSS font-size: 3px;
. Instead, Theme UI will look up the third font size value in your theme.fontSize
, you’re generating CSS Media Queries for responsive styling. fontSize: [3, 4, 5]
means that on phones & screens larger than phones, the element will use the theme’s font size #3, then on tablets, theme value #4, then on laptops & larger, theme value #5. As you can see, for very little code, you can make a site that’s optimized for all screen sizes.my
, mr
, px
, etc. These are for margin & padding, using the theme space
scale values just like how font size works. mt
means margin-top
, mb
= margin-bottom
, then my
is margin on the Y axis, so both top & bottom (full set: my mx my mr mb ml
). The same goes for padding, its properties beginning with p
(full set: py px pt pr pb pl
).bg
sets the background-color
to one of the colors
in your theme. color
sets the text color similarly. The superpower here is that if you enable dark mode or another theme, the colors dynamically change to that theme, as defined in your theme file.I wrote a bit more about how styling is used at a component level over here about the Hack Pennsylvania site.