Skip to content

Instantly share code, notes, and snippets.

@mxstbr
Last active August 24, 2021 20:26
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save mxstbr/c0961900465e2cf51d4a0f56c392e1aa to your computer and use it in GitHub Desktop.
Save mxstbr/c0961900465e2cf51d4a0f56c392e1aa to your computer and use it in GitHub Desktop.
styled-components ❤ tachyons
// There's two ways to use Tachyons together with styled-components
// Both are valid, which one you use depends if you want to always apply a tachyons class when a certain component
// is rendered or if you want to apply it for specific instances.
// 1. Use .attrs to define classNames that should always be applied to a styled component
// Whenever you use <Component /> now it'll have both the styled-components as well as the Tachyons class
// See the docs for more info: https://www.styled-components.com/docs/basics#attaching-additional-props
const Component = styled.div.attrs({
className: 'bw0-l',
})`
color: blue;
`
<Component /> // has bw0-1 class applied!
// 2. Use the Tachyons className in the JSX
// styled-components doesn't force you to not use any classes so this works like with any other component
const Component = styled.div`
color: blue;
`
// Add the Tachyons class when you render the component
<Component className="bw0-1" />
@jikkujose
Copy link

jikkujose commented Feb 22, 2018

You need to add an empty string literal at the end of the function like so:

export const Main = styled.main.attrs({
  className: "center w-50 sans-serif"
})``
//^^

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment