question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Improve themeGet helper

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
emplumscommented, Dec 18, 2018

I think this could work! Regarding leaving theme out of defaultProps, we’ll still need that in every component so that the user has access to the theme for color and space props if a ThemeProvider is not provided

1reaction
emplumscommented, Dec 28, 2018

@shawnbot these solutions are cool, but I’m not sure how necessary they are IMO. Setting up system props, supplying a default, and setting the theme in one function feels a bit too much of a mixture of concerns for me and sort of what I was trying to get away from in the refactor. The defaultProps API is pretty standard and easy for people to jump in and know exactly what’s going on.

Read more comments on GitHub >

github_iconTop Results From Across the Web

better-theme-get - npm
better -theme-get. A better existential getter function for styled-system. This is based off of @styled-system/theme-get.
Read more >
Bring compatibility with v4 · Issue #191 · styled-components ...
ThemeProvider seems to work (I updated my comment). The main idea is that themeGet can't work without a theme.
Read more >
Advanced Usage - styled-components
styled-components has full theming support by exporting a <ThemeProvider> wrapper component. This component provides a theme to all React components underneath ...
Read more >
How to Improve GGPlot Theme Background Color and Grids
Arguments of the helper functons: element_line(color, size, linetype) . Modify grid lines color size and type. element_rect(fill, color, ...
Read more >
Theme API ‒ Qlik Sense for developers - Qlik | Help
The QTheme object consists of a theme and helper methods. The id parameter is mandatory when theme.get is used in a global context...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found