-
Notifications
You must be signed in to change notification settings - Fork 650
perf(Button): fix CounterLabel remount, remove DEV hook, enable React Compiler #7552
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@primer/react': patch | ||
| --- | ||
|
|
||
| perf(Button): fix CounterLabel remount and remove conditional DEV hook |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -60,24 +60,19 @@ const ButtonBase = forwardRef(({children, as: Component = 'button', ...props}, f | |
| const ariaDescribedByIds = loading ? [loadingAnnouncementID, ariaDescribedBy] : [ariaDescribedBy] | ||
|
|
||
| if (__DEV__) { | ||
| /** | ||
| * The Linter yells because it thinks this conditionally calls an effect, | ||
| * but since this is a compile-time flag and not a runtime conditional | ||
| * this is safe, and ensures the entire effect is kept out of prod builds | ||
| * shaving precious bytes from the output, and avoiding mounting a noop effect | ||
| */ | ||
| // eslint-disable-next-line react-hooks/rules-of-hooks | ||
| React.useEffect(() => { | ||
| if ( | ||
| innerRef.current && | ||
| !(innerRef.current instanceof HTMLButtonElement) && | ||
| !((innerRef.current as unknown) instanceof HTMLAnchorElement) && | ||
| !((innerRef.current as HTMLElement).tagName === 'SUMMARY') | ||
| ) { | ||
| // eslint-disable-next-line no-console | ||
| console.warn('This component should be an instanceof a semantic button or anchor') | ||
| } | ||
| }, [innerRef]) | ||
| // Validate that the element is a semantic button/anchor. | ||
| // This runs during render (not in an effect) to avoid a conditional hook call | ||
| // that prevents React Compiler from optimizing this component. | ||
| const el = innerRef.current | ||
| if ( | ||
| el && | ||
| !(el instanceof HTMLButtonElement) && | ||
| !((el as unknown) instanceof HTMLAnchorElement) && | ||
| !((el as HTMLElement).tagName === 'SUMMARY') | ||
| ) { | ||
| // eslint-disable-next-line no-console | ||
| console.warn('This component should be an instanceof a semantic button or anchor') | ||
| } | ||
|
Comment on lines
62
to
+75
|
||
| } | ||
| return ( | ||
| <ConditionalWrapper | ||
|
|
@@ -154,11 +149,9 @@ const ButtonBase = forwardRef(({children, as: Component = 'button', ...props}, f | |
| */ | ||
| count !== undefined && !TrailingVisual | ||
| ? renderModuleVisual( | ||
| () => ( | ||
| <CounterLabel className={classes.CounterLabel} data-component="ButtonCounter"> | ||
| {count} | ||
| </CounterLabel> | ||
| ), | ||
| <CounterLabel className={classes.CounterLabel} data-component="ButtonCounter"> | ||
| {count} | ||
| </CounterLabel>, | ||
| Boolean(loading) && !LeadingVisual, | ||
| 'trailingVisual', | ||
| true, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The warning message text is grammatically incorrect ("an instanceof a"). Since this is user-facing console output in dev, it should be updated to something clearer like "should be an instance of a semantic button or anchor" (and ideally include the actual rendered tag name).