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.

Component selectors don't work with TypeScript `CreateStyled<Theme>`

See original GitHub issue
  • emotion version: 10.0.10
  • react version: 16.8.6

Relevant code:

styled.ts:

import styled, { CreateStyled } from '@emotion/styled'
import { Theme } from './theme'

export default styled as CreateStyled<Theme>

text.tsx:

import styled from 'src/styles/styled'

// ...

export const Title2 = styled<'h3', TextProps>('h3')`
  color: ${props => props.theme.colors.charcoalGrey};
  ${typography.title2}
  ${textStyles}
`

export const SectionTitle = styled<'h4', TextProps>('h4')`
  color: ${props => props.theme.colors.charcoalGrey};
  ${typography.sectionTitle}
  ${textStyles}
`

someOtherFile.tsx:

const DeduplicateExaggeratedMargins = styled(Box)`
  ${Title2} + ${SectionTitle} {
    margin-top: ${props => props.theme.space[standardMargin]}px;
  }
`

What you did:

  1. I followed the instructions here to define a theme with our own version of styled.
  2. Now I can’t use Component Selectors!

What happened:

I received an error:

Error: Component selectors can only be used in conjunction with babel-plugin-emotion.

Reproduction:

Problem description:

Switching to test.tsx to use import styled from '@emotion/styled' rather than our version of styled with theming.

Suggested solution:

Make babel-plugin-emotion support what is documented in the “Typescript.md” file.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:11
  • Comments:16 (2 by maintainers)

github_iconTop GitHub Comments

16reactions
IvanRodriCallejacommented, Aug 20, 2019

I had the same problem with CreateStyled<Theme> and i fixed it in a simple way (i think) taking the problem into the typings that is where the problem comes.

I “overrode” the existing typings from @emotion/styled because it is the problem of our not typed theme in styled-components.

customTypings.d.ts

image

Whit this, you can use the @emotion/styled (this is the alias that you need to use because macros search this alias from babel-preset-emotion and if you use other it will no parse anything)

I am doing the same that you have to do to create a “custom theme” into typings, with this i avoid to play gith alias in webpack and declare this modules in typings (it creates problems with HMR).

When you use your styled now you have a typed theme.

anyComponent.tsx

image

There is only one case in which this solution doesn’t work, it is if you need to have more than one different theme in the same project (to be honest i don’t think anybody need this)

4reactions
OzzieOrcacommented, Sep 5, 2019

So no one else has to retype the above code, I added types/@emotion/styled.d.tscontaining:

import { CreateStyled } from '@emotion/styled/types/index';

import { MyTheme } from '../../src/myTheme';
export * from '@emotion/styled/types/index';
const customStyled: CreateStyled<MyTheme>;
export default customStyled;
Read more comments on GitHub >

github_iconTop Results From Across the Web

Theme with styled-components and Typescript
Firstly, we will need to define how our theme interface looks like. Inside src folder, create styled.d.ts file and extend the existing styled- ......
Read more >
styled-components: Advanced Usage
This component provides a theme to all React components underneath itself via the context API. In the render tree all styled-components will have...
Read more >
Can't use Component Selectors with MUI v5 Emotion Library
1 Answer 1 · if I import from "@emotion/styled/macro" then I can't access theme object with styled components. – Darab Ahmed · Hey...
Read more >
Manage design tokens with TypeScript and styled-components
But you don't neccecaraly have to have a design system to work with them. As a matter of fact, you could utilize design...
Read more >
React Theme Selector with Styled Components & Context API
Code: https://github.com/stuyy/react-context- theme -selectorBecome a Member: https://www.youtube.com/ansonthedeveloper/joinBecome a Patreon: ...
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