[docs] Improve documentation to cover shouldForwardProp
See original GitHub issueI’ve been having issues with the following code:
const Box = styled('div')({
background: ({ color }) => color
})
...
<Box color='red' />
The attribute color="red"
gets added to the <div>
in the DOM. Nothing in the documentation told me how to fix this. Some fixes on Stackoverflow or Spectrum.chat look like this:
const Box = styled(({ color, ...props }) => <div {...props} />)({
background: ({ color }) => color
})
but this is messy, and removes the Typescript typings that Box
has when doing styled('div')
.
I dug around in the source code of styled()
, and it turns out there is a filterProps
property I can use:
const Box = styled('div')({
filterProps: ['color'],
background: ({ color }) => color
})
This works great! Exactly what I need. However, a few problems:
- As I mentioned, nowhere in the documentation can I find reference to this, much less how to use it.
- Typescript doesn’t like it -
const Box = styled('div')<any, { color: string }>({
filterProps: ['color'], // error
background: ({ color }) => color
})
/*
Type 'string[]' is not assignable to type 'string | number | JSSFontface | JSSFontface[] | CreateCSSProperties<{ color: string; }> | ((props: { color: string; }) => JSSFontface | JSSFontface[]) | ... 755 more ... | ((props: { ...; }) => VectorEffectProperty)'.
Type 'string[]' is not assignable to type 'JSSFontface[]'.
Type 'string' has no properties in common with type 'JSSFontface'
*/
- It would probably make more sense adding it to the
options
object as a 2nd parameter:
const Box = styled('div')<any, { color: string }>({
background: ({ color }) => color
}, {
filterProps: ['color']
})
Implementing this change would be very simple:
- Move this code block above the
useStyles()
call, and change it to this:
if (stylesOptions.filterProps) {
filterProps = stylesOptions.filterProps;
delete stylesOptions.filterProps;
}
- In the Typescript type file on this line, make this change:
- options?: WithStylesOptions<Theme>
+ options?: WithStylesOptions<Theme> & { filterProps?: string[] }
- Throw together some quick documentation!
Since this field isn’t documented, you could argue that moving it isn’t a breaking change, since this isn’t part of the public interface. However, it’s probably better to:
- Update the Typescript, add the option to the
options
object, but also allow it to still be specified in the style block - Add a
console.warn()
deprecation message in the console whenNODE_ENV !== 'production'
- Release this as a minor version bump
- Remove the ability to specify in the style block in the next major version bump.
- The issue is present in the latest release.
- I have searched the issues of this repository and believe that this is not a duplicate.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:4
- Comments:7 (6 by maintainers)
Top Results From Across the Web
[docs] Improve documentation to cover shouldForwardProp
I've been having issues with the following code: const Box = styled('div')({ background: ({ color }) => color }) .
Read more >reactjs - Can you pass custom props to Material-UI v5 `styled ...
So MUI gave us the shouldForwardProp option to tell MUI whether it "should forward the prop" to the root node or not. The...
Read more >How to customize - Material UI - MUI
Learn how to customize Material UI components by taking advantage of different strategies for specific use cases.
Read more >@mui/core | Yarn - Package Manager
Important: This documentation covers modern versions of Yarn. For 1.x docs, see classic.yarnpkg.com. Yarn.
Read more >Releases - styled-components
now using stylis v4 (if using stylis-plugin-rtl you'll need to upgrade to the ... use transient props for styling-only props or shouldForwardProp for...
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
Should we consider this solved, now that we have https://next.material-ui.com/customization/styled/?
I have repurposed the issue to cover v5 (instead of v4). We can:
shouldForwardProp
option: https://next.material-ui.com/guides/styled-engine/$
prefix #25925