Repository navigation
perf(ActionList, TreeView): avoid whole-subtree style invalidation on hover, focus and attribute changes #8682
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
base: main
Are you sure you want to change the base?
Changes from all commits
780b0a3
203b237
7dda141
a94cc7b
dbe493f
3c097e2
d50a467
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 | ||
| --- | ||
|
|
||
| ActionList: Reduce style recalculation when unrelated elements on the page change hover state or attributes |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@primer/react': patch | ||
| --- | ||
|
|
||
| TreeView: Reduce style recalculation when the pointer or focus enters or leaves the tree, and when focus moves between items in Safari |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import type React from 'react' | ||
| import {useEffect} from 'react' | ||
|
|
||
| /** | ||
| * Mirrors `:focus-within` onto the tree root as `data-focus-within`. | ||
| * | ||
| * WebKit clears and re-sets `:focus-within` up the ancestor chain on every focus move, so a | ||
| * `:focus-within` rule on the root restyles every level line on each arrow key press. This | ||
| * attribute only changes when focus enters or leaves the tree. It is written to the DOM | ||
| * directly so the tree doesn't re-render. | ||
| */ | ||
| export function useFocusWithinAttribute(containerRef: React.RefObject<HTMLElement | null>) { | ||
| useEffect(() => { | ||
| const root = containerRef.current | ||
| if (!root) return | ||
|
|
||
| const sync = (event?: FocusEvent) => { | ||
| const focused = event | ||
| ? event.type === 'focusin' || root.contains(event.relatedTarget as Node | null) | ||
| : root.matches(':focus-within') | ||
| root.toggleAttribute('data-focus-within', focused) | ||
| } | ||
|
|
||
| sync() | ||
| root.addEventListener('focusin', sync) | ||
| root.addEventListener('focusout', sync) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I reproduced a case in WebKit where removing the focused item emits no I recorded a minimal DOM reproduction in WebKit 26.5 using the base/PR TreeView CSS and the PR's exact effect body (without React). On each side I focus the removable item, delete it, then focus the outside button. The base hides its lines; the PR keeps them visible with treeview-focus-regression.mp4 |
||
| return () => { | ||
| root.removeEventListener('focusin', sync) | ||
| root.removeEventListener('focusout', sync) | ||
| root.removeAttribute('data-focus-within') | ||
| } | ||
| }, [containerRef]) | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.