Skip to content

Add display name to generated class - #153

Closed
JamieDixon wants to merge 29 commits into
styled-components:masterfrom
JamieDixon:display-name
Closed

JamieDixon wants to merge 29 commits into
styled-components:masterfrom
JamieDixon:display-name

Conversation

@JamieDixon

@JamieDixon JamieDixon commented Oct 31, 2016 •

Copy link
Copy Markdown
Contributor

I'm opening this PR more for discussion than to actually be included at this time.

I wanted to have the ability to see the displayName of the rendered component in the css class when using dev tools in the browser. Sometimes it can be tough to figure out which component generated which class name, especially when switching from custom className="my-descriptive-foo to class="hbfd5".

screen shot 2016-10-31 at 21 26 53

This current PR adds the displayName all the time however I'm sure there's a way of doing this so it only happens in Dev mode. I'll need a pointer in the right direction to do that.

Here's what I have so far. Please have a look and let me know if there's a different approach which might work better. Thanks.

  • Suffix displayName onto generated class when process.env.NODE_ENV === 'development'
  • Strip out CSS selector characters from displayName

@mxstbr

mxstbr commented Nov 1, 2016

Copy link
Copy Markdown
Member

I like it, but I really thought we were doing something like this already!

@thien-do

thien-do commented Nov 1, 2016

Copy link
Copy Markdown

is there any way to apply this for development only?

@mxstbr-bot

mxstbr-bot commented Nov 1, 2016 •

Copy link
Copy Markdown
✅ Good on 'ya.
✅ Please include a CHANGELOG entry.
You can find it at CHANGELOG.md.
3 Warnings
⚠️ There’re library changes, but not tests. That’s OK as long as you’re refactoring existing code.
⚠️ A change was made in StyledComponent.js that wasn’t made in StyledNativeComponent.js or viceversa.
⚠️ changes to StyledComponent.js might be semver major changes

Generated by 🚫 danger

@vdanchenkov

Copy link
Copy Markdown
Member

Great addition! I'm so glad that it works smoothly with my babel plugin. Although, there is minor problem.

I might miss something, but in my tests two components with equal rule set displays this way.

const Arguments = styled.div`
  color: red;
`

const Condition = styled.div`
  color: red;
`
export default () => (
  <div>
    <Arguments>A</Arguments>
    <Condition>B</Condition>
  </div>
)
<div data-reactid="19">
  <div class="jldpnK-Arguments" data-reactid="20">A</div>
  <div class="jldpnK-Arguments" data-reactid="21">B</div>
</div>

I'd expect either different classes (jldpnK-Arguments, jldpnK-Condition), or one jldpnK-Arguments-Condition. I guess latter is not possible.

@JamieDixon

Copy link
Copy Markdown
Contributor Author

Thanks @vdanchenkov. We've got some ideas to fix that problem. Looking into it now.

@JamieDixon

Copy link
Copy Markdown
Contributor Author

@vdanchenkov Your observation should be fixed in the latest commit. I've changed the way we add the displayName to the class and it's no longer combined with the hash.

Let me know what you guys think.

Turns off the default no-duplicate-imports because it marks import type
as a duplicate. This should be ok, since import/no-duplicate covers the
same cases but allows type imports.

Furthermore, needed to inline disable one import/no-duplicates since it
does mark import typeof as a duplicate.
@JamieDixon

Copy link
Copy Markdown
Contributor Author

Now, instead of appending something to the generated class hash, we're just inserting the display name as a separate class value. Here's how it looks in dev given these recent changes:

screen shot 2016-11-01 at 16 46 27

@JamieDixon

Copy link
Copy Markdown
Contributor Author

Replaced by #160

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants