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.

Nested 'as' loses styling

See original GitHub issue

Environment

	## System:
	 - OS: macOS 10.14.1
	 - CPU: (8) x64 Intel(R) Core(TM) i7-4770HQ CPU @ 2.20GHz
	 - Memory: 816.95 MB / 16.00 GB
	 - Shell: 5.3 - /bin/zsh
	## Binaries:
	 - Node: 8.12.0 - /usr/local/bin/node
	 - Yarn: 1.2.0 - ~/.yarn/bin/yarn
	 - npm: 6.4.1 - /usr/local/bin/npm
	## npmPackages:
	 - babel-plugin-styled-components: ^1.8.0 => 1.8.0
	 - styled-components: ^4.1.2 => 4.1.2

Reproduction

I am trying to create a UI Kit in which a bunch of typography ‘atoms’ are styled-components.

I’ve decided to make a <Base> styled component which houses a group of default styles and behaviours that all other elements can use, ‘extend’ and override.

I’m having an issue which I can work around but I would like to understand why it doesn’t work.

https://codesandbox.io/s/mj3m9p3jyj

Steps to reproduce

  • I setup a base styled component which renders a span and a few styles. It has some default props that can be overridden and passed through.
  • Next I setup a <HeadingOne> element using a functional component so I can set new default props for this element. I sometimes also set propType checks at this point if required.
  • Finally, I use that <HeadingOne> element to create a <Title> but want it to be blue and a <h2>. Doing a normal extension such as adding color: blue works perfectly but when I set the as prop it loses all styles from the <HeadingOne> and the <Base> elements.

Expected Behavior

Element should inherit HeadingOne, Base and Title styling.

Actual Behavior

Element loses HeadingOne & Base styling but keeps Title styling.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:10 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
JamesBlisscommented, Nov 30, 2018

@probablyup and @mxstbr thanks so much for getting back to me

I thought it would be some sort of weird implementation / usage thing. Inline with the advice above surrounding making a shared css declaration my solution is:

https://codesandbox.io/s/qzo5vox3y6

Hopefully this can help anyone else who has this issue. If anyone has any more advice for improving it that would be great 😁!

0reactions
probablyupcommented, Nov 29, 2018

styled(StyledComponent) === extend styled(OtherComponentMaybeWithStyledComponentInside) === withComponent

as === render something else, with the same props and generated styling class name

Read more comments on GitHub >

github_iconTop Results From Across the Web

Nested child components lose their css when the parent is ...
it rolls up all the component styles so they all get applied at the top. You lose component style scoping, though. 3
Read more >
Why isn't this nested css style being applied?
Your CSS is invalid, there is no such thing as nesting in CSS. Only Less or Sass, but you have a long way...
Read more >
Nested <ul>: Can't control styling - HTML & CSS
I'm trying to override the style of a parent list in a nested list, but nothing I do works. The parent has no...
Read more >
CSS Nesting Module
A style rule can be directly nested within another style rule if its selector is nest-prefixed. To be nest-prefixed , a nesting selector...
Read more >
Breaking changes in v5, part one: styles and themes
Nested imports of more than one level are private. For example, you can no longer import red from @mui/material/colors/red . - ...
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