Base component that prevents rendering unknown props in DOM
- Filter unknown props from DOM
- Based on
react-html-attributes(50% smaller - 3kb when minified / 1kb gziped, without many deprecated attributes & elements) - Override inner element with
asprop - Great for CSS-in-JS component libraries (NOTE: some provide this feature out of box!)
$ yarn add react-styled-baseimportBasefrom'react-styled-base'as: Component— React component or DOM element (likediv,input,span, ...), defaultdiv
See createBase for more options.
import{render}from'react-dom'importstyledfrom'react-emotion'importBasefrom'react-styled-base'constLinkComp=styled(Base)` color: ${props=>props.foo==='bar' ? 'royalblue' : 'hotpink'};`render((<LinkCompas='a'href='http://example.com'foo='bar'abc='xyz'>
Click Me
</LinkComp>),document.body)// → // <a class="css-0" href="http://example.com">Click Me</a>import{createBase}from'react-styled-base'defaultComp: Component— React component or DOM element (likediv,input,span, ...), defaultdivoptions: Object— (Optional, default to{ componentProp: 'as' })options.whitelist: Array— List of props that always will be rendered (optional)options.blacklist: Array— List of props that always be be omitted (optional)options.isPropValid: function (tagName, propName) => boolean— Custom function to filter propsoptions.tagName: string— DOM element. Used whendefaultCompis not DOM element (optional)
Return: Component — wrapped in React.forwardRef.
import{render}from'react-dom'importstyledfrom'react-emotion'import{LinkasRouterLink}from'react-router-dom'import{createBase}from'react-styled-base'constLinkComp=styled(createBase('span'))` color: ${props=>props.foo==='bar' ? 'royalblue' : 'hotpink'};`constRouterLinkBase=createBase(RouterLink,{tagName: 'a',whitelist: ['to']})constCustomComp=createBase((props)=><span{...props}/>,{isPropValid: (tag,prop)=>prop!=='foo'})render((<span><LinkCompas={RouterLinkBase}to='/page-2'foo='bar'>
Page 2
</LinkComp><LinkCompas='a'href='https://google.com'target='_blank'foo='baz'>
Search
</LinkComp><CustomComptitle='notice'foo='bar'>
Notice
</CustomComp</span>),document.body)// →// <span>// <a class="css-0" href="/app/page-2">Page 2</a>// <a class="css-1" href="https://google.com" target="_blank">Search</a>// <span title="notice">Notice</span>// </span>import{isPropValid}from'react-styled-base'propName: string— prop name (likehref,value,onChange)tagName: string— DOM element (likea,input,div)
Return: boolean
import{isPropValid}from'react-styled-base'isPropValid('foo','a')// → falseisPropValid('bar','a')// → falseisPropValid('href','a')// → trueclean-element— Use component prop-types as blacklist
⚠️ Unknown Prop Warning- 💬 Separate HTML attributes from styling props
- 💄
pss— Prop Styles System - 📐
pss-components— Components - 💄
glamorous— Inspired by - 👩🎤
@emotion/is-prop-valid— Approach used insideemotionandstyled-components
MIT © John Grishin