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.

Add sx prop to Themed components

See original GitHub issue

As of v0.6, the Styled component was renamed to Themed. After renaming all imports and updating theme-ui, the sx styles on my Themed components are no longer applied unless I explicitly state @jsxImportSource theme-ui (auto runtime) or @jsx jsx (classic). The sx object is also no longer recognized by VS Code as an instance of ThemeUIStyleObject for Themed, whereas it is properly reflected for Box, Image, etc.

This seems to be a regression made somewhere between 0.5 and 0.6, as all my Styled components worked as intended up until the 0.6 release. Tested and confirmed on all 0.6 versions.

Example code:

import { Box, Flex, Image, Themed } from 'theme-ui'

export default () => (
  <Flex sx={{ width: 100vw, height: 100vh, justifyContent: 'center', alignItems: 'center' }}>
    <Box sx={{ flexGrow: 1 }}>
      <Image ... />
      <Themed.h1 sx={{ textAlign: 'center', textTransform: 'uppercase' }}>Enter Site</Themed.h1>
    </Box>
  </Flex>
)

Expected: expected

Actual: actual

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
hoobdeeblacommented, Dec 30, 2020

My mistake, I was testing going back and forth between classic/pragma and automatic/@jsxImportSource and must have gotten confused along the way. I went back and tested on a 0.3 site and confirmed that Themed.X never had a sx prop without using the pragma.

IMO the sx prop on Themed.X components should work without a pragma and without a custom Babel config, just as Box does now. Using the Babel config suggested in #1335 on a Gatsby site broke gatsby-plugin-mdx (I removed "importSource": "theme-ui" to fix this), so I have only added @jsxImportSource theme-ui to pages that necessitate use of the sx prop on non-Theme UI components. Currently that includes pages that use Themed.X - confusing, right? I avoid using the pragma most of the time by aliasing to Box, i.e. <Box as='article' sx={...}> or <Box as={GatsbyLink} sx={...}>, but doing <Box as='h1' variant='styles.h1' sx={...}> defeats the purpose of having <Themed.h1 sx={...}> in the first place, so I end up declaring the import source for files that use Themed.X. After Themed.X has a native sx prop, I won’t need to declare @jsxImportSource theme-ui anywhere.

As an aside, it would be cool if Themed.X got the variant and style props like Box as well.

1reaction
fwextensionscommented, Jun 2, 2022

I’m using JS.

Read more comments on GitHub >

github_iconTop Results From Across the Web

The `sx` Prop - Theme UI
The sx prop lets you style elements inline, using values from your theme. To use the sx prop, add the custom /** @jsxImportSource...
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 >
Theme Styling with the sx Prop - Sam Rose
Theme UI introduces a powerful styling feature, the sx prop. This prop lets you style elements inline using values from your theme. You...
Read more >
Passing sx prop to a custom component - reactjs
In the documentation they just cast the type to const. You can add them as follows using the spread operator: }, sxProps &&...
Read more >
MUI V5: The SX Prop - YouTube
In this video I go over React Material UI's new SX prop that comes built into every MUI V5 component. We go over...
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