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 display names to components #817

Merged
merged 10 commits into from
Dec 16, 2020

Conversation

dburles
Copy link
Contributor

@dburles dburles commented Mar 31, 2020

React devtools and Emotion will now render the correct component names, for #811

React devtools and Emotion will now render the correct component names
@lachlanjc
Copy link
Member

lachlanjc commented Mar 31, 2020

This is awesome! Any reason we can’t do it with the simpler syntax though?

@dburles
Copy link
Contributor Author

dburles commented Mar 31, 2020

For that instance it is due to the emotion styled API not reading it for whatever reason

@dburles
Copy link
Contributor Author

dburles commented Mar 31, 2020

The function name approach is needed when using forwardRef https://reactjs.org/docs/forwarding-refs.html#displaying-a-custom-name-in-devtools

@jxnblk
Copy link
Member

jxnblk commented Apr 1, 2020

Rad, thanks! Would changing the Box component to not use the styled HOC affect this at all? (See #692 (comment))

@dburles
Copy link
Contributor Author

dburles commented Apr 1, 2020

No it should be okay, if it's just a regular component, it will only need to pass a named function to forwardRef. These changes will definitely conflict with #692. I think if this were merged first, it shouldn't be very difficult to reconcile.

@yuriy-sng
Copy link

yuriy-sng commented Apr 3, 2020

Hey! Thanks for this PR related to my issue.

I may not be approaching this the right way, but when I inspect the components in the Deploy preview all I see are -Box suffixes, instead of -Image, -Heading and such.

image

@dburles
Copy link
Contributor Author

dburles commented Apr 3, 2020

Hey! Thanks for this PR related to my issue.

I may not be approaching this the right way, but when I inspect the components in the Deploy preview all I see are -Box suffixes, instead of -Image, -Heading and such.

image

Yeah that is odd, I'll have a look into it

@dburles
Copy link
Contributor Author

dburles commented Apr 3, 2020

Ok after digging into it a bit, I think the best solution to ensure emotion renders the correct names is to add the label property to all components. I'll push a fix soon.

@dburles
Copy link
Contributor Author

dburles commented Apr 4, 2020

Edit: See #823

I'm kinda second guessing using the emotion label option as they're not stripped in production, though it is one solution. I've gone down a bit of a rabbit hole and the indirection through the Box component is causing problems in that emotion always resolves to Box unless we otherwise specify a label on every component.

This should be able to be greatly simplified by writing components without reference to Box such as:

/** @jsx jsx */
import { jsx } from 'theme-ui'
import React from 'react'

export const Alert = React.forwardRef(function Alert(props, ref) {
  return (
    <div
      ref={ref}
      {...props}
      sx={{
        display: 'flex',
        alignItems: 'center',
        px: 3,
        py: 2,
        fontWeight: 'bold',
        color: 'white',
        bg: 'primary',
        borderRadius: 4,
        ...(props.variant && { variant: `alerts.${props.variant}` }),
      }}
    />
  )
})

Some of these components use styled-system style props, which is another reason why they all inherit from the Box component, e.g.:

<Alert variant='secondary' mb={2}>Secondary</Alert>

IMO, there is no need for this now that we have the sx prop, it is odd to have two API's and only adds confusion. This might also simplify the TS refactor over at #692. Also, this definitely plays into the variant prop idea also: #800.

@dburles dburles mentioned this pull request Apr 4, 2020
7 tasks
@dburles
Copy link
Contributor Author

dburles commented Apr 8, 2020

I think I'll roll with the label option for now as that'll ensure that emotion renders the correct names. I'll try have that up soon.

@dburles
Copy link
Contributor Author

dburles commented Apr 13, 2020

Alright so I think this is ready! Emotion will now render the component names, it's unfortunate but they'll all be named like css-191ogd4-Box-Text for example (always with Box). So to summarise this PR, it includes two things:

  1. React devtools will now render the component names
  2. Emotion will generate classnames containing the component names

It would be even better if we could get CSS sourcemaps working too, the emotion babel plugin will require changes to support theme-ui. Something to look at in future :)

}}
/>
))
export const Alert = React.forwardRef(function Alert(props, ref) {
Copy link
Member

Choose a reason for hiding this comment

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

For my understanding: is the named function syntax still necessary with the Emotion label property added below?

Copy link
Member

Choose a reason for hiding this comment

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

Also, holy smokes! I don't think I realized the label property would work more generally in the sx prop – that's probably worth putting in a guide in the docs or mentioning somewhere 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For my understanding: is the named function syntax still necessary with the Emotion label property added below?

Just for React devtools

Copy link
Member

Choose a reason for hiding this comment

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

Does the displayName static property no longer work for React devtools?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just thought I'd double check it, it does indeed work with forwardRef! Strange, I am sure that it wasn't working at the time, anyway, I'd probably stick with this approach as it's what's recommended and is more concise.

@hasparus hasparus mentioned this pull request Jun 23, 2020
32 tasks
@lachlanjc lachlanjc linked an issue Dec 3, 2020 that may be closed by this pull request
@lachlanjc lachlanjc added the enhancement New feature or request label Dec 3, 2020
@atanasster
Copy link
Collaborator

Hi @dburles - this is a great PR.

Can you please upgrade it to resolve conflicts and we also have some new components

  • Paragraph
  • Switch

Also whats the final status on displayName?

I hope we can merge this soon, as @lachlanjc is working on a rewrite to remove emotion/styled and it seems there is some overlap here.

@vercel
Copy link

vercel bot commented Dec 11, 2020

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://vercel.com/systemui/theme-ui/20uog2mlc
✅ Preview: https://theme-ui-git-components-display-names.systemui.vercel.app

@dburles
Copy link
Contributor Author

dburles commented Dec 11, 2020

@atanasster Thanks. I've just fixed the merge conflicts. Named functions serve the same purpose as displayName, so either way it doesn't make any difference.

I think we should probably remove the label property:

  1. It isn't stripped in production, which means additional data over the wire.
  2. It's tedious to correctly handle alongside the __css prop on Box. For instance CheckBoxChecked (in Checkbox) which renders the SVG component has custom styles defined using the __css prop. The means in each lower level component (SVG in this case), for those styles to function we would have to merge label with those styles so that the styles are correctly applied. It's possible to do, but unnecessarily tedious. This is only a problem for such instances (which I think are few, but still...).

I'm not sure label is worth the additional complexity, at least with the current implementation with everything using Box.

If we can make a decision on that I am happy to remove it and at least sort out the naming in devtools for now. Emotion label can always be revisited later.

@atanasster
Copy link
Collaborator

Thanks @dburles thats great

I would say remove label. You are correct this PR should be only about the devTools and we can decide about label in some other PR if its requested by users.

Would you mind switching to static displayName, its a bit more standard and will be also easier for the rework that @lachlanjc is doing.

@dburles
Copy link
Contributor Author

dburles commented Dec 11, 2020

I can't find why displayName appears to be working now (it didn't used to), the React docs still recommend naming the functions instead of using displayName when using forwardRef. So I'm not sure which is incorrect.

@atanasster
Copy link
Collaborator

@dburles i think displaying the component name looks cleaner too, no?

@dburles
Copy link
Contributor Author

dburles commented Dec 11, 2020

I mean like, technically you shouldn't be able to use displayName on forwardRef components. I do recall it not working when writing this PR some time in the past, now for some reason it seems to work. I'm not sure what that means because the docs still recommend providing a named function to forwardRef (and not displayName). Unless I can confirm that displayName is now supported on forwardRef components I wouldn't recommend it.

@atanasster
Copy link
Collaborator

I see you are correct it was recommended by bvaughn in that thread. You make the call, seems both work so we should be good.

@dburles
Copy link
Contributor Author

dburles commented Dec 11, 2020

We could do this too, but it's not really as nice:

const Component = ({}, ref) => ...;
Component.displayName = 'Button';
const Button = React.forwardRef(Component);
export default Button;

@dburles
Copy link
Contributor Author

dburles commented Dec 11, 2020

I'll aim to have the label removed by early next week.

@atanasster
Copy link
Collaborator

Fantastic, really looking forward to have your PR merged.

For displayName - any choice you make is fine by me, but until then (monday) if @hasparus @lachlanjc have any notes about it here

Copy link
Member

@hasparus hasparus left a comment

Choose a reason for hiding this comment

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

Great PR, @dburles! Thank you!

@atanasster let's merge this after the label removal.

@atanasster
Copy link
Collaborator

@dburles - would you mind updating Paragraph and Switch, just so we dont forget to bring them up to date as well.

@dburles
Copy link
Contributor Author

dburles commented Dec 14, 2020

@atanasster Whoops, nice spot. Just updated those, should be good to go then.

@hasparus hasparus closed this Dec 14, 2020
@hasparus hasparus deleted the branch system-ui:develop December 14, 2020 08:53
@hasparus hasparus reopened this Dec 14, 2020
@hasparus hasparus changed the base branch from master to develop December 14, 2020 09:12
@atanasster
Copy link
Collaborator

@dburles - thanks a lot - its ready to merge. Let's just wait @hasparus to confirm if we are merging only bug fixes to release a 0.6.0 stable, or if we are also merging features.

Copy link
Collaborator

@atanasster atanasster left a comment

Choose a reason for hiding this comment

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

Thanks a bunch, great PR

@hasparus
Copy link
Member

Let's just wait @hasparus to confirm if we are merging only bug fixes to release a 0.6.0 stable, or if we are also merging features.

You wrote it like I'm some sort of authority.

This PR is open since March, and IMHO it's a bugfix, as I'd expect proper names in React DevTools.

@hasparus hasparus merged commit e9429e6 into system-ui:develop Dec 16, 2020
@hasparus
Copy link
Member

Sorry it took so long to merge it, @dburles 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improve Components DisplayNames properties
6 participants