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

[Doc] How to access theme values #121

Closed
Kikobeats opened this issue Feb 22, 2018 · 5 comments
Closed

[Doc] How to access theme values #121

Kikobeats opened this issue Feb 22, 2018 · 5 comments

Comments

@Kikobeats
Copy link
Contributor

Hey,

I'm seeing that is possible access to theming values, such as:

import { theme } from 'styled-system'
theme('color.white') // => '#fff'

how do that, for example, for array elements?

I'm trying different approach but looks it doesn't works

import { theme } from 'styled-system'
theme('breakpoints.1') // => nope?
theme('breakpoints[1]') // => nope?
@jxnblk
Copy link
Member

jxnblk commented Feb 22, 2018

The themeGet function is intended for use as an interpolation in styled-components, etc. Without styled-components the call should look like this:

const value = themeGet('colors.blue')(props)

With styled-components it looks like this:

const Box = styled.div`
  color: ${themeGet('colors.blue')};
`

It's mostly a shortcut to avoid props => _.get(props.theme, 'colors.blue') and also allows for fallback values as a second argument:

color: ${themeGet('colors.blue', '#07c')};

@jxnblk jxnblk closed this as completed Feb 23, 2018
@xralphack
Copy link

xralphack commented Mar 27, 2018

const value = themeGet('colors.blue')(props)

what is this props?

I want to use theme value in component by myself

@nicolasletoublon
Copy link

Same thing here, I would like to access theme values outside of styled-components, and even if I call the themeGet('')(props) with this props, its doing nothing :/

@muhajirdev
Copy link

Hey guys, if you still face this problem. Make sure you use withTheme

import { themeGet } from 'styled-system'
import { withTheme } from 'styled-components'

const ChartContainer = props => {
  const { data, title, unit } = props
  const color = themeGet('colors.text')(props)
  console.log('color is ', color)
  return (
    <Card variant="primary" my={5} height={8}>
      <Heading fontSize={2} fontWeight="bold">
        {title}
      </Heading>
      <Chart data={transformData(data, title, unit)} color="#fff" />
    </Card>
  )
}

export default withTheme(ChartContainer)

It works for me :)

@jikkujose
Copy link

Surprised to find there is no easy way to get a theme variable without so much fuss!

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

No branches or pull requests

6 participants