Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add displayNames to React components #2132

Merged
merged 8 commits into from
Dec 5, 2020
Merged

Add displayNames to React components #2132

merged 8 commits into from
Dec 5, 2020

Conversation

dcastil
Copy link
Contributor

@dcastil dcastil commented Nov 23, 2020

What:

Add displayNames to Global, ClassNames and component returned from withEmotionCache to prevent components being labeled as Anonymous. Display names are not added in production.

Why:

Easier for devs to orient themselves in React devtools and tests, also easier to find Emotion-related components through search.

How:

Manually attaching displayName to Global and ClassNames components and the component returned from withEmotionCache.

Note to withEmotionCache:

// → Component has displayName "WithEmotionCache(MyComponent)"
const Component = withEmotionCache(function MyComponent() {})

In React devtools this component will appear as MyComponent with the modifiers ForwardRef and WithEmotionCache.

Component in React Components tab:
Screenshot 2020-11-23 at 21 00 36

Component in detail view of React Components tab:
Screenshot 2020-11-23 at 21 01 15

Checklist:

  • Documentation
  • Tests
  • Code complete
  • Changeset

@changeset-bot
Copy link

changeset-bot bot commented Nov 23, 2020

🦋 Changeset detected

Latest commit: 0b22c3e

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 4 packages
Name Type
@emotion/react Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@codesandbox-ci
Copy link

codesandbox-ci bot commented Nov 23, 2020

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 0b22c3e:

Sandbox Source
Emotion Configuration

@@ -142,3 +142,7 @@ export const ClassNames: React.AbstractComponent<
}
return ele
})

if (process.env.NODE_ENV !== 'production') {
ClassNames.displayName = 'EmotionClassNames'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be just ClassNames? It would reflect the actual name and match the inferred name if only this would be inferrable here.

@@ -136,3 +136,7 @@ export let Global: React.AbstractComponent<

return null
})

if (process.env.NODE_ENV !== 'production') {
Global.displayName = 'EmotionGlobal'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as here: https://github.com/emotion-js/emotion/pull/2132/files#r530285457

Please note that I'm open for discussion about this.

Copy link
Contributor Author

@dcastil dcastil Nov 25, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefixed it with Emotion for following reasons:

  • It shows up when I search for emotion in React devtools which might help me find emotion-related components.
  • It is more specific than Global. If someone new to a codebase sees this component in the devtools and searches for EmotionGlobal on Google, they find the correct docs immediately.
  • It doesn't seem to have any disadvantages. When I search for Global, I still find this component.

But I don't have a big opinion on this. I'm happy to change them to their exact name if you think it makes more sense.

let withEmotionCache = function withEmotionCache<Props, Ref: React.Ref<*>>(
func: (props: Props, cache: EmotionCache, ref: Ref) => React.Node
): React.AbstractComponent<Props> {
let withEmotionCache = function withEmotionCache<
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think changes here are required - withEmotionCache is purely internal and you have already covered all cases that use it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 5227c72

@Andarist Andarist merged commit 3f8bf70 into emotion-js:master Dec 5, 2020
@dcastil dcastil deleted the fix-global-component-display-name branch December 5, 2020 23:01
@github-actions github-actions bot mentioned this pull request Dec 5, 2020
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.

None yet

2 participants