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

refactor(CodeSnippet): use hooks clean up render logic #4636

Merged
merged 18 commits into from
Nov 16, 2019

Conversation

vpicone
Copy link
Contributor

@vpicone vpicone commented Nov 9, 2019

No external changes. One of perhaps several components related to #4635

Changed:

  • use FC instead of Class w/ static properties
  • useLayoutEffect to combine componendDidMount and componentDidUpdate lifecycles
  • Use ref for uid rather than just assigning to class instance
  • clean up/combine return statements

Testing:

  • Component should perform identically to published version

@vpicone vpicone requested a review from a team as a code owner November 9, 2019 23:33
@ghost ghost requested review from jnm2377 and joshblack November 9, 2019 23:33
@netlify
Copy link

netlify bot commented Nov 9, 2019

Deploy preview for the-carbon-components ready!

Built with commit da0cea6

https://deploy-preview-4636--the-carbon-components.netlify.com

@netlify
Copy link

netlify bot commented Nov 9, 2019

Deploy preview for carbon-components-react ready!

Built with commit da0cea6

https://deploy-preview-4636--carbon-components-react.netlify.com

@netlify
Copy link

netlify bot commented Nov 9, 2019

Deploy preview for carbon-elements ready!

Built with commit da0cea6

https://deploy-preview-4636--carbon-elements.netlify.com

Co-Authored-By: emyarod <emyarod@users.noreply.github.com>
@joshblack
Copy link
Contributor

Would love to take a look at this, wanted to leave a comment just to say I'd love to review this before it gets merged in 👀

Copy link
Contributor

@jnm2377 jnm2377 left a comment

Choose a reason for hiding this comment

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

I'm no longer seeing the show more/less button on the long multi-text snippet.
Screen Shot 2019-11-11 at 10 22 25 AM

@joshblack
Copy link
Contributor

Random, noticed we swapped from useLayoutEffect. Curious what ended up not working with:

const [shouldShowButton, setShouldShowButton] = useState(false);
const ref = useRef(null);

useLayoutEffect(() => {
  if (ref.current) {
    const { height } = ref.current.getBoundingClientRect();
    setShouldShowButton(height > 255);
  }
}, [children]);

// ...
<pre ref={ref}>{children}</pre>
// ...

@vpicone
Copy link
Contributor Author

vpicone commented Nov 11, 2019

@joshblack on first render the ref is null, since ref updates don’t trigger a re-render, the hook doesn’t get called again. #4636 (comment)

@joshblack
Copy link
Contributor

@vpicone is that due to the fact that the ref was destructured? Didn’t notice it locally when trying it. I think when you access a ref in an effect with the current property it’ll work.

@vpicone
Copy link
Contributor Author

vpicone commented Nov 12, 2019

@joshblack 🤦‍♂ fixed, ty.

Copy link
Member

@emyarod emyarod left a comment

Choose a reason for hiding this comment

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

the refactor looks good to me, but how should we be checking that react docgen now processes the updated component? ref original issue

@vpicone
Copy link
Contributor Author

vpicone commented Nov 12, 2019

@emyarod it does!

}) {
const [expandedCode, setExpandedCode] = useState(false);
const [shouldShowMoreLessBtn, setShouldShowMoreLessBtn] = useState(false);
const { current: uid } = useRef(getUniqueId());
Copy link
Contributor

Choose a reason for hiding this comment

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

One thing that I don't think we remember is that technically we should not have ids start with a number, I think it's fine in HTML5 but in general can cause issues in certain cases (or at least has been brought up that way). Would we want to bring over our useId hook or add a prefix to this that's been passed to id?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@joshblack our uniqueId actually adds an id prefix by default, so starting with a number shouldn't be an issue. Do you think we should be specifying a more descriptive prefix? I'm a little gun shy about tweaking ID generation behavior at this point haha.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, didn't realize it was using the older file for unique ids. In general, we use https://github.com/carbon-design-system/carbon/blob/master/packages/react/src/tools/setupGetInstanceId.js for newer code

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@joshblack oh missed that. I think this is a great opportunity to switch to a hook. I really highly doubt someone's depending on the ID of the inline code component, but do you think it'd be worth keeping the id prefix?

Copy link
Contributor

Choose a reason for hiding this comment

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

It's not that an external user is leveraging it, just that folks have pointed out that ids generated with a number as the first character technically doesn't adhere to the HTML4 spec (I think this is addressed in HTML5). I think we could totally do id-{number}, we just have to be careful around collisions and the reason this came up with the newer approach is that it's easy to collide since they don't all share the same global id variable (which helps out a ton for tests). I think combining the local prefix plus the global id generation in a hook would be great and useId could help do that

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I opened #4676 since I feel like changing the id generation behavior might be a little beyond the scope. The hook we have in the hooks package forces a - that doesn't exist in the current uniqueId implementation so it'd break some snapshots probably.

Copy link
Contributor

Choose a reason for hiding this comment

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

@vpicone are we or others using automatically generated ids in tests?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@joshblack wasn’t that the impetus for that big slack thread? Someone was using a generated ID to select an element in testing.

Copy link
Contributor

Choose a reason for hiding this comment

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

@vpicone that was when someone used an id they passed into the component, not a generated one.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh!!! That makes way more sense.

Copy link
Contributor

@jnm2377 jnm2377 left a comment

Choose a reason for hiding this comment

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

LGTM 👍

@vpicone vpicone merged commit 272488b into carbon-design-system:master Nov 16, 2019
@vpicone vpicone deleted the code-snippet-refactor branch November 16, 2019 05:16
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

4 participants