Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1 - Convert starter styles to rebass theme #4

Merged
merged 1 commit into from
Mar 24, 2019
Merged

Conversation

erikdstock
Copy link
Owner

⚠️ WATCH OUT

This is a Tutorial PR on "Setting up a Rebass theme with Gatsby."

START A CODE WALKTHROUGH →

Overview

There isn't much to this; rebass is an opinionated set of core components built with styled-system. Combined together they make it easy to define component libraries that abstract most layout concerns for you. Because they are backed by styled-components the patterns are well-established and the components and themes are easy to extend - you always have the escape hatch when you need it.

Requirements

colors: {
gray: {
"1": "hsl(0,0%,32%)",
"2": "hsl(0,0%,47%)",
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import styledNormalize from 'styled-normalize'

const theme = {
breakpoints: {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We start here by setting up a theme object. Themes are passed through the context via styled-components' ThemeProvider, so now we'll have access to our theme object.

next ->

textDecoration: `none`,
}}
>
<Text as="h1" fontFamily="sans" style={{ margin: 0 }}>
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rebass provides root components that hook into your theme, removing the need to use css for most layout concerns. In this case Text will render as an h1 and receive theme.fonts['sans'] as its css font-family. If I'd wanted I could have done m={0} to select the 0th element in my rebass' default theme.space array (which happens to be 0). The docs for rebass & styled-system cover this in much more detail than my PR could.

<>
<Header siteTitle={data.site.siteMetadata.title} />
<PageContent m="0 auto" maxWidth={["560px"]}>
<Box p="0px 1.0875rem 1.45rem">
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

p="0px 1.0875rem 1.45rem": I'm just replacing the padding prop from the gatsby starter with a string literal to be used as css- No reliance on a theme here.

const Header: React.FunctionComponent<{ siteTitle: string }> = ({
siteTitle,
}) => (
<Box bg="rebeccapurple" mb="1.45rem">
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Props that expect colors will look in your theme.colors first, then fallback to treating your string as a color value itself.

return (
<Theme>
{element}
</Theme>
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should go in both gatsby-browser.js and gatsby-ssr.js. I'm not totally clear how these two points differ, sadly... As you can see here i copy-pasted the comment as well. Oops.

@erikdstock erikdstock merged commit f4fde4e into master Mar 24, 2019
@erikdstock erikdstock changed the title Convert starter styles to rebass theme 1 - Convert starter styles to rebass theme Mar 24, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant