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.

sx prop on custom components not working

See original GitHub issue

I’ve noticed while writing components that the sx prop didn’t work on custom components which i’d built myself, but when i went to use it with other plugin libraries (fontawesome for example) it did work as expected. So am i missing something? I can’t imagine large libraries like FA are doing something special to accept the sx prop.

An example component i’ve made (though this seems to affect everything i’ve written, so i’m curious what’s wrong).

/** @jsx jsx */
import { jsx } from 'theme-ui'

interface PropTypes {
  to: string
  variant?: string
  children: string
}

const Button = ({ to, variant, children }: PropTypes) => (
  <a
    href={to}
    className={`button`}
    sx={{
      px: 3,
      py: 2,
      border: 0,
      borderRadius: 4,
      cursor: 'pointer',
      variant: variant ? `buttons.${variant}` : `buttons.primary`,
    }}
  >
    {children}
  </a>
)

export default Button
<Button to='#' variantbutton='primary'>Button text</Button>

What i’d like to do, as an example

<Button to='#' variantbutton='primary' sx={{ display: 'flex' }}>Button text</Button>

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
jxnblkcommented, Oct 1, 2019

You’ll need to pass the className prop through to the underlying HTML element. The sx prop is converted to className with Theme UI’s jsx function

1reaction
danspratlingcommented, Oct 1, 2019

Okay i think i’ve got a solution which is clean & gives no errors.

Best solution so far

/* imports */

interface PropTypes {
  to: string,
  variant: string,
  sx: object, //we expect a sx prop but never use it below
  className: string, //we expect a className prop but never define one when the component is called
  children: string
}

const Button = (props: PropTypes) => { //props is defined and compared against the interface
  const { to, variant, className, children } = props //then we destructure later, ignore sx because we don't use it
  return (
    <a
      href={to}
      className={className} //merging properly, yay!
      sx={{...}}
    >
      {children}
    </a>
  )
}
<Button to='#' variant='button.primary' sx={{ display: 'flex' }}>Button text</Button>

No duplicated props, and everything is used some way or another.

Going to close as i’m happy with this solution (especially as sx errors on html elements anyway in ts so removing the errors in my own components is 👌)

Thanks all!

Read more comments on GitHub >

github_iconTop Results From Across the Web

sx prop not getting transformed in custom components ...
Check your component code, you might import the v4 component: import ListItemText from "@material-ui/core/ListItemText";.
Read more >
sx prop not getting transformed in custom components in ...
[Solved]-sx prop not getting transformed in custom components in Material-UI 5-Reactjs ... In v5, you can use sx directly from the components since...
Read more >
The sx prop - MUI System
The sx prop is a shortcut for defining custom styles that has access to the theme. The sx prop lets you work with...
Read more >
The `sx` Prop
The sx Prop. The sx prop lets you style elements inline, using values from your theme. To use the sx prop, add the...
Read more >
How to use the sx prop in MUI v5. Still using makeStyles?
The form with no custom styling. The page without styling outside of the MUI default components looks a little wonky. The default Typography,...
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