Styled-system: Styled System forwarding props (e.g., "width", "font-size") to HTML elements

Created on 2 Jul 2019  路  3Comments  路  Source: styled-system/styled-system

I'm using styled components. Is this the intended behavior?

Rendered HTML:

<div font-size="14px" font-weight="bold" class="..." />

Most helpful comment

This was a blocker for us when using AMP because unrecognized HTML props means your AMP page is invalid.

If you're looking for a solution, I've written an HOC that omits props from being passed down.

export default function omitProps(Component, propsToOmit) {
  function WithoutOmittedProps({ children, ...rest }) {
    return <Component {...omit(rest, propsToOmit)}>{children}</Component>
  }

  WithoutOmittedProps.propTypes = {
    children: PropTypes.node,
  }

  WithoutOmittedProps.displayName = `WithoutOmittedProps(${Component.displayName || Component.name})`

  return WithoutOmittedProps
}

You can then use it like so:

import styled from 'styled-components'
import { space, layout, color } from 'styled-system'
import propTypes from '@styled-system/prop-types'

import omitProps from 'connectors/omit-props'

const propsToOmit = [
  ...Object.keys(propTypes.space),
  ...Object.keys(propTypes.layout),
  ...Object.keys(propTypes.color),
]

const Box = styled(omitProps('div', propsToOmit))`
  ${space}
  ${layout}
  ${color}
`

export default Box

All 3 comments

This is an issue with the styled-components library, see https://styled-system.com/guides/removing-props-from-html

Thanks!

This was a blocker for us when using AMP because unrecognized HTML props means your AMP page is invalid.

If you're looking for a solution, I've written an HOC that omits props from being passed down.

export default function omitProps(Component, propsToOmit) {
  function WithoutOmittedProps({ children, ...rest }) {
    return <Component {...omit(rest, propsToOmit)}>{children}</Component>
  }

  WithoutOmittedProps.propTypes = {
    children: PropTypes.node,
  }

  WithoutOmittedProps.displayName = `WithoutOmittedProps(${Component.displayName || Component.name})`

  return WithoutOmittedProps
}

You can then use it like so:

import styled from 'styled-components'
import { space, layout, color } from 'styled-system'
import propTypes from '@styled-system/prop-types'

import omitProps from 'connectors/omit-props'

const propsToOmit = [
  ...Object.keys(propTypes.space),
  ...Object.keys(propTypes.layout),
  ...Object.keys(propTypes.color),
]

const Box = styled(omitProps('div', propsToOmit))`
  ${space}
  ${layout}
  ${color}
`

export default Box
Was this page helpful?
0 / 5 - 0 ratings

Related issues

philholden picture philholden  路  5Comments

smolak picture smolak  路  4Comments

ghost picture ghost  路  3Comments

releaf picture releaf  路  3Comments

D1no picture D1no  路  4Comments