Skip to content

[docs-infra] Simplify click header#42593

Merged
alexfauquette merged 1 commit intomui:nextfrom
oliviertassinari:simplify-click-solution
Jun 10, 2024
Merged

[docs-infra] Simplify click header#42593
alexfauquette merged 1 commit intomui:nextfrom
oliviertassinari:simplify-click-solution

Conversation

@oliviertassinari
Copy link
Member

@oliviertassinari oliviertassinari commented Jun 9, 2024

@oliviertassinari oliviertassinari added internal Behind-the-scenes enhancement. Formerly called “core”. scope: docs-infra Involves the docs-infra product (https://www.notion.so/mui-org/b9f676062eb94747b6768209f7751305). labels Jun 9, 2024
@mui-bot
Copy link

mui-bot commented Jun 9, 2024

Netlify deploy preview

https://deploy-preview-42593--material-ui.netlify.app/

Bundle size report

No bundle size changes (Toolpad)
No bundle size changes

Generated by 🚫 dangerJS against 44f05fa

Comment on lines -828 to +832
const elements = document.getElementsByClassName('title-link-to-anchor');
const elements = rootRef.current!.getElementsByClassName('title-link-to-anchor');
Copy link
Member Author

@oliviertassinari oliviertassinari Jun 9, 2024

Choose a reason for hiding this comment

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

The CSS is scoped to the component, so much better not to leak outside of this scope, especially since we bypass React here.

Comment on lines -833 to +837
elements[i].addEventListener('click', handleClick, false);
elements[i].addEventListener('click', handleHeaderClick);
Copy link
Member Author

@oliviertassinari oliviertassinari Jun 9, 2024

Choose a reason for hiding this comment

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

  • false is the default argument, no need
  • false is missing from the remove event handler, fix the removal logic
  • handleHeaderClick feels clearer

Comment on lines -835 to -840

return () => {
for (let i = 0; i < elements.length; i += 1) {
elements[i].removeEventListener('click', handleClick);
}
};
Copy link
Member Author

@oliviertassinari oliviertassinari Jun 9, 2024

Choose a reason for hiding this comment

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

If the component unmounts, these DOM nodes are removed. I imagine we don't need to remove event handlers. Even if the content is dynamic, removed DOM nodes would be gone, and elements[i].addEventListener('click', is smart, it deduplicates listeners if the arguments are identical (https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener), which is the case since handleClick is hoisted to the top level scope.


if (isRangeSelection) {
if (selection.type === 'Range') {
// Disable the <a> behavior to be able to select text.
Copy link
Member Author

Choose a reason for hiding this comment

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

We have this implicit rule to try to document each event.preventDefault(); call in the source because they tend to have strong implications, it's hard to not break stuff with them.

@alexfauquette alexfauquette merged commit e211fd8 into mui:next Jun 10, 2024
@oliviertassinari oliviertassinari deleted the simplify-click-solution branch June 10, 2024 13:43
mnajdova pushed a commit to mnajdova/material-ui that referenced this pull request Jun 12, 2024
joserodolfofreitas pushed a commit to joserodolfofreitas/material-ui that referenced this pull request Jul 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

internal Behind-the-scenes enhancement. Formerly called “core”. scope: docs-infra Involves the docs-infra product (https://www.notion.so/mui-org/b9f676062eb94747b6768209f7751305).

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants