babel-plugin-styled-components icon indicating copy to clipboard operation
babel-plugin-styled-components copied to clipboard

Component "name" in className with `.extend`

Open breadadams opened this issue 7 years ago • 7 comments

It seems that with an extended component, the component being extended's name/filename (I've tried with both config options) are used for the classname applied to the DOM element. Ie:

const Example = styled.div`
  color: red;
`
const ExampleExtended = Example.extend`
  background: green;
`
...

render() {
  return <ExampleExtended />
}

The above returns something like:

<div class="Example-cJRMOC jmwXEm"></div>

Whereas ideally I was expecting:

<div class="ExampleExtended-cJRMOC jmwXEm"></div>

Is this intended functionality?


Versions
"styled-components": "^3.3.3",
"babel-plugin-styled-components": "^1.5.1",

breadadams avatar Jun 27 '18 19:06 breadadams

Here's a real-world example (we're using a base "building block" to construct other components via .extend):

Before (using const Component = styled.div...)

image

After (using const Component = Box.extend...)

image

As you can see we lose the debugging benefit, as all component names are replaced with the component used in the .extend (even the "parent component" names are gone 🤷‍♂️ )

breadadams avatar Jun 27 '18 20:06 breadadams

Hmm this is probably a bug.

quantizor avatar Jun 28 '18 03:06 quantizor

Happy to take a look however it may be past my abilities... It seems /src/visitors/displayNameAndId.js is taking care of the naming @probablyup?

breadadams avatar Jun 28 '18 11:06 breadadams

Yeah that should be correct. You’ll also want to upgrade whatever version of s-c we’re testing with locally since there were some recent changes to displayName handling. On Thu, Jun 28, 2018 at 6:44 AM Brad Adams [email protected] wrote:

Happy to take a look however it may be past my abilities... It seems /src/visitors/displayNameAndId.js is taking care of the naming @probablyup https://github.com/probablyup?

— You are receiving this because you were mentioned.

Reply to this email directly, view it on GitHub https://github.com/styled-components/babel-plugin-styled-components/issues/145#issuecomment-401007233, or mute the thread https://github.com/notifications/unsubscribe-auth/AAiy1odazlqdYxVriqR2O8JVbNfj7Mxnks5uBMGxgaJpZM4U6Oaa .

quantizor avatar Jun 28 '18 12:06 quantizor

I've done some testing on the issue and don't appear to be getting very far, however I'll share my findings incase it can assist someone else in coming up with a solution 🙂

I updated the tests to look like so:

image

The path object (received in getName) for an extended component only includes the real name (not the name of the component from which it was extended) in 3 places, references, bindings & code:

image image

And therefore, the actual name assigned to DoubleInterpolation is TestToBeExtended:

image

Could it be that the source of this bug doesn't lie within the babel plugin but SC itself? 🤔

Edit: Gist to the object in screenshots: https://gist.github.com/breadadams/9e602b19d8ba139c0657a0f97c26f1cf

breadadams avatar Jul 01 '18 20:07 breadadams

Also @probablyup I couldn't detect which version of styled-components was being used locally, it doesn't appear to be in package.json, or my yarn.lock after init. Something I'm missing?

breadadams avatar Jul 01 '18 20:07 breadadams

@breadadams yarn list styled-components should give you the answer

As a general FYI, the "extend" API is deprecated and will be removed in v4, so I'd recommend switching to styled(StyledComponent) instead which should not have any issues with classnames. https://github.com/styled-components/styled-components/issues/1694

quantizor avatar Aug 21 '18 06:08 quantizor