Skip to content

Conversation

@ben-rogerson
Copy link
Owner

@ben-rogerson ben-rogerson commented Jul 11, 2020

This new feature makes it easier to grab individual theme values from your tailwind.config.js

This change means you won't need to set up a theme provider to pass values down to your components.
You just need to import theme and define the object path to the value.

Some usage examples:

Syncing tailwind media queries with your css

import tw, { css, theme } from 'twin.macro'

const xlBp = content => css`
  @media (min-width: ${theme`screens.xl`}) {
    ${content}
  }
`

const Component = () => (
  <div css={xlBp(`
      ${tw`block`}
      color: red;
  `)} />
)

Adding color from your config

// Css prop

import { css, theme } from 'twin.macro'

const Input = () => <input css={css({ color: theme`colors.purple.500` })} />

// or

import { css, theme } from 'twin.macro'

const Input = () => (
  <input
    css={css`
      color: ${theme`colors.purple.500`};
    `}
  />
)
// Styled components

import { styled, theme } from 'twin.macro'

const Input = styled.div({ color: theme`colors.purple.500` })

// or

import { styled, theme } from 'twin.macro'

const Input = styled.div`
  color: ${theme`colors.purple.500`};
`

Setting a :root css variable

import { createGlobalStyle } from 'styled-components'
import { theme } from 'twin.macro'

const GlobalStyle = createGlobalStyle({
  ':root': {
    '--bg-color': theme`colors.black`
  }
})

Adding a gradient

// Css prop

import { theme } from 'twin.macro'

const Input = () => (
  <div
    css={{
      color: `linear-gradient(${theme`colors.pink.500`}, ${theme`colors.blue.500`})`,
    }}
  />
)

// Styled components

import { styled, theme } from 'twin.macro'

const Input = styled.div([
    { color: `linear-gradient(${theme`colors.pink.500`}, ${theme`colors.blue.500`})` }
])

@ben-rogerson ben-rogerson self-assigned this Jul 11, 2020
@ben-rogerson ben-rogerson added the feature request New feature request label Jul 11, 2020
@ben-rogerson ben-rogerson force-pushed the feature/theme-import branch 2 times, most recently from b97b734 to f5a0bb1 Compare July 20, 2020 11:12
@ben-rogerson ben-rogerson force-pushed the feature/theme-import branch from 5de9af9 to debfab3 Compare July 21, 2020 23:40
@ben-rogerson ben-rogerson merged commit 8c51892 into master Jul 22, 2020
@ben-rogerson ben-rogerson deleted the feature/theme-import branch July 29, 2020 00:18
@ben-rogerson
Copy link
Owner Author

The theme import can now bring back objects from your config too #169

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

Labels

feature request New feature request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants