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.

[Question] How to implement custom component aware of `sx`?

See original GitHub issue

Hi, I’d like to write a custom component based on theme-ui elements with a structure where children are aware of sx prop of the parent.

Let say:

export const FooComponent = forwardRef(
  (
    { sx: sxParent, ...rest },
    ref,
  ) => {
    console.log(sxParent, rest)

    return (
      <Box {...rest} ref={ref}>
        <Text sx={{ backgroundColor: sxParent.backgroundColor || 'black' }}>
           foo
        </Text>
      </Box>
    )
  },
)

I noticed that if in the file where I call FooComponent the jsx pragma is present then sxParent is converted to a { className: 'css-wo3z44' }, otherwise it is the original object (let say a valid SxStyleProp object).

How can I handle this? Should I need to parse the css-wo3z44 somehow?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
wereHamstercommented, Jun 17, 2020

I never use the jsx pragma, and instead passthrough sx to the underlying theme-ui component, eg.

export const Foo = ({ sx, ...props }) => (
  <Box sx={{ m: 3, ...sx }} {...props}>…</Box>
)
1reaction
hasparuscommented, Jun 17, 2020

Yes, sx prop is transformed by jsx pragma, similarly to how Emotion’s jsx pragma consumes css prop.

You can use any other prop name. If you want to reuse a name you’re already using, style should be okay, albeit a little confusing. I’d probably go with styles or pass just backgroundColor as a prop.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Adding sx prop to custom component - Stack Overflow
You can use the styled function to add the sx prop to your custom component like this: import { styled } from "@mui/material/styles"; ......
Read more >
How To Create Custom Components in React - DigitalOcean
In this tutorial, you'll learn to create custom components in React. Components are independent pieces of functionality that you can reuse ...
Read more >
Frequently Asked Questions (WEEE) - European Commission
The purpose of this Frequently Asked Questions (FAQ) document is to clarify certain aspects of ... Do components fall within the scope of...
Read more >
Custom components - MUI System
Custom components. Learn how to use MUI System with custom components. Using sx with custom components. Normally you would use the Box component...
Read more >
AWS Lambda – FAQs
Q : When should I use AWS Lambda versus Amazon EC2? ... You can invoke a Lambda function using a custom event through...
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