Skip to content

Improve themeGet helper #375

@shawnbot

Description

@shawnbot

I made a suggestion in #368 that I think allows us to at least remove the fallback values from our styled-system themeGet() calls. Rather than this:

import {themeGet} from 'styled-system'
import {colors} from './theme'
const Component = styled('div')`
  color: ${themeGet('colors.green.5', colors.green[5])};
`

We can export a get() function from theme.js using styled-system's own get() function to compute the fallback value:

import {get as getKey, themeGet} from 'styled-system'
export const get = key => themeGet(key, getKey(theme, key))

...which simplifies our components that use themeGet() nicely:

import {get} from './theme'
const Component = styled('div')`
  color: ${get('colors.green.5')};
`

This does allow us to get rid of defaultProps.theme in cases where we only use themeGet(), but other system props will still need theme to be set outside of a ThemeProvider to work properly. 😕

/cc @mxstbr

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions