Improve themeGet helper
See original GitHub issueI 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:
- Created 5 years ago
- Reactions:1
- Comments:9 (9 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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
andspace
props if a ThemeProvider is not provided@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.