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.

Passing tagged template literals to .withComponent()

See original GitHub issue

If I’m correct, styled-component’s main feature leverages tagged template literals for its whole ecosystem, with the star function being styled + taggedTemplateLiterals, so that we can create a new element and attach new styles to it:

const Text = styled.span`
    fontSize: 1em;
`

Also, extend + taggedTemplateLiterals is possible so we can quite add styles to an existing element:

const MyNewText = extend`
    fontSize: 2em;
`

The helper css also comes bundled with taggedTemplateLiterals :

const textStyles = css`
    font-size: 1em;
    color: #252525;
    margin-bottom: .4 rem;
`

Then we got the cool withComponent kid and can create a new element inheriting styles from another component:

const Paragraph = Text.withComponent('p')

However, to my surprise, we cannot use withComponent + taggedTemplateLiterals

This would be really nice :

const Text = styled.span`
    font-size: 1em;
    color: #252525;
    margin-bottom: .4 rem;
`
Text.h1 = Text.withComponent('h1')`
    font-size: 3em;
`
Text.h2 = Text.withComponent('h2')`
    font-size: 2.7em;
`
Text.h3 = Text.withComponent('h3')`
    font-size: 2.1em;
`
Text.h4 = Text.withComponent('h4')`
    font-size: 1.8em;
`
Text.h5 = Text.withComponent('h5')`
    font-size: 1.3em;
`
Text.h6 = Text.withComponent('h6')`
    font-size: .9em;
`
export default Text;

Today, I end up doing:

const textStyles = css`
    font-size: 1em;
    color: #252525;
    margin-bottom: .4 rem;
`
Text = styled.span`
    ${textStyles}
`
Text.h1 = styled.h1`
    ${textStyles}
    font-size: 3em;
`
Text.h2 = styled.h2`
    ${textStyles}
    font-size: 2.7em;
`
Text.h3 = styled.h3`
    ${textStyles}
    font-size: 2.1em;
`
Text.h4 = styled.h4`
    ${textStyles}
    font-size: 1.8em;
`
Text.h5 = styled.h5`
    ${textStyles}
    font-size: 1.3em;
`
Text.h6 = styled.h6`
    ${textStyles}
    font-size: .9em;
`
export default Text;

Don’t know if there are other patterns though (??), but for consistency I would like to see this feature. Not sure if this is difficult to implement or feasible at all. What do you think ? 😉

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

8reactions
mxstbrcommented, Jun 9, 2017
const Paragraph = Text.withComponent('p').extend`
  color: blue;
`

Reference explanation: #851

2reactions
mxstbrcommented, Jun 9, 2017

@philpl we should add this pattern to the docs since it seems like folks are confused about this!

Read more comments on GitHub >

github_iconTop Results From Across the Web

ES6: All Hail, Template Literals — Part 2: Tagged Templates
The tag function that is passed data from the template literally for which the literal will be processed. The function automatically breaks down...
Read more >
Template literals (Template strings) - JavaScript | MDN
Template literals are literals delimited with backtick (`) characters ... In that case, the template literal is passed to your tag function, ...
Read more >
Styled Components - Emotion
By default, Emotion passes all props (except for theme ) to custom components and only props that are valid html attributes for string...
Read more >
Advanced Usage - styled-components
Tagged Template Literals are a new feature in ES6. They let you define custom string interpolation rules, which is how we're able to...
Read more >
A quick Guide to Styled Components with interactive Examples
Find out why styled components are built with tagged template literals. ... You can pass props to the Title like to any other...
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