-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
feat: improve focus selector perf with focus-visible and focus-within polyfill #24154
Merged
ling1726
merged 15 commits into
microsoft:master
from
ling1726:feat/focus-visible-polyfill
Aug 3, 2022
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
c8f83e0
feat: focus-visible polyfill
ling1726 f23ceb7
focus-within polyfill
ling1726 ba99928
use focusout
ling1726 221b523
changefiles
ling1726 be7ee86
update md
ling1726 4afce7a
remove unused
ling1726 26dbf9d
fix typings
ling1726 8b9742b
fix link vr tests
ling1726 ff125d3
fix link vr tests
ling1726 a74be74
PR suggestions
ling1726 62fa5f1
update changefile
ling1726 4ff8d9b
assign ref in effect
ling1726 990ac96
add doics
ling1726 515a4b3
update md
ling1726 d5aae20
PR suggestions
ling1726 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
change/@fluentui-react-checkbox-25fb0c36-d07f-4a39-bd82-831bb2b756b0.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"type": "patch", | ||
"comment": "refactor: use `useFocusWithin` hook for :focus-within styles", | ||
"packageName": "@fluentui/react-checkbox", | ||
"email": "[email protected]", | ||
"dependentChangeType": "patch" | ||
} |
7 changes: 7 additions & 0 deletions
7
change/@fluentui-react-portal-2940da05-fb94-4cac-8763-6e0c4a14ee8f.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"type": "patch", | ||
"comment": "fix: use `useFocusVisible` hook for :focus-visible styles", | ||
"packageName": "@fluentui/react-portal", | ||
"email": "[email protected]", | ||
"dependentChangeType": "patch" | ||
} |
7 changes: 7 additions & 0 deletions
7
change/@fluentui-react-provider-9ddc6123-46e0-4094-9670-659f00f241b8.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"type": "patch", | ||
"comment": "fix: use `useFocusVisible` hook for :focus-visible styles", | ||
"packageName": "@fluentui/react-provider", | ||
"email": "[email protected]", | ||
"dependentChangeType": "patch" | ||
} |
7 changes: 7 additions & 0 deletions
7
change/@fluentui-react-radio-36321438-6ccb-4827-85da-098c7e6c76cf.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"type": "patch", | ||
"comment": "refactor: use `useFocusWithin` hook for :focus-within styles", | ||
"packageName": "@fluentui/react-radio", | ||
"email": "[email protected]", | ||
"dependentChangeType": "patch" | ||
} |
7 changes: 7 additions & 0 deletions
7
change/@fluentui-react-slider-e562a2af-50fe-4b42-9eec-003613926997.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"type": "patch", | ||
"comment": "refactor: use `useFocusWithin` hook for :focus-within styles", | ||
"packageName": "@fluentui/react-slider", | ||
"email": "[email protected]", | ||
"dependentChangeType": "patch" | ||
} |
7 changes: 7 additions & 0 deletions
7
change/@fluentui-react-switch-504a98f0-bdbd-4377-959c-1dcb2275040a.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"type": "patch", | ||
"comment": "refactor: use `useFocusWithin` hook for :focus-within styles", | ||
"packageName": "@fluentui/react-switch", | ||
"email": "[email protected]", | ||
"dependentChangeType": "patch" | ||
} |
7 changes: 7 additions & 0 deletions
7
change/@fluentui-react-tabster-897f1851-6bb3-4082-8a54-ace4ca191faf.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"type": "minor", | ||
"comment": "feat: create `:focus-visible` and `:focus-within` polyfills", | ||
"packageName": "@fluentui/react-tabster", | ||
"email": "[email protected]", | ||
"dependentChangeType": "patch" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
packages/react-components/react-tabster/src/focus/constants.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
packages/react-components/react-tabster/src/focus/focusVisiblePolyfill.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import { KEYBORG_FOCUSIN, KeyborgFocusInEvent, createKeyborg, disposeKeyborg } from 'keyborg'; | ||
import { FOCUS_VISIBLE_CLASS } from './constants'; | ||
|
||
/** | ||
* Because `addEventListener` type override falls back to 2nd definition (evt name is unknown string literal) | ||
* evt is being typed as a base class of MouseEvent -> `Event`. | ||
* This type is used to override `listener` calls to make TS happy | ||
*/ | ||
type ListenerOverride = (evt: Event) => void; | ||
|
||
type FocusVisibleState = { | ||
/** | ||
* Current element with focus visible in state | ||
*/ | ||
current: HTMLElement | undefined; | ||
}; | ||
|
||
type HTMLElementWithFocusVisibleScope = { | ||
focusVisible: boolean | undefined; | ||
} & HTMLElement; | ||
|
||
export function applyFocusVisiblePolyfill(scope: HTMLElement, win: Window): () => void { | ||
if (alreadyInScope(scope)) { | ||
// Focus visible polyfill already applied at this scope | ||
return () => undefined; | ||
} | ||
|
||
const state: FocusVisibleState = { | ||
current: undefined, | ||
}; | ||
|
||
const keyborg = createKeyborg(win); | ||
|
||
// When navigation mode changes remove the focus-visible selector | ||
keyborg.subscribe(isNavigatingWithKeyboard => { | ||
if (!isNavigatingWithKeyboard && state.current) { | ||
removeFocusVisibleClass(state.current); | ||
state.current = undefined; | ||
} | ||
}); | ||
|
||
// Keyborg's focusin event is delegated so it's only registered once on the window | ||
// and contains metadata about the focus event | ||
const keyborgListener = (e: KeyborgFocusInEvent) => { | ||
if (state.current) { | ||
removeFocusVisibleClass(state.current); | ||
state.current = undefined; | ||
} | ||
|
||
if (keyborg.isNavigatingWithKeyboard() && isHTMLElement(e.target) && e.target) { | ||
// Griffel can't create chained global styles so use the parent element for now | ||
state.current = e.target; | ||
applyFocusVisibleClass(state.current); | ||
} | ||
}; | ||
|
||
// Make sure that when focus leaves the scope, the focus visible class is removed | ||
const blurListener = (e: FocusEvent) => { | ||
if (!e.relatedTarget || (isHTMLElement(e.relatedTarget) && !scope.contains(e.relatedTarget))) { | ||
if (state.current) { | ||
removeFocusVisibleClass(state.current); | ||
state.current = undefined; | ||
} | ||
} | ||
}; | ||
|
||
scope.addEventListener(KEYBORG_FOCUSIN, keyborgListener as ListenerOverride); | ||
scope.addEventListener('focusout', blurListener); | ||
(scope as HTMLElementWithFocusVisibleScope).focusVisible = true; | ||
|
||
// Return disposer | ||
return () => { | ||
scope.removeEventListener(KEYBORG_FOCUSIN, keyborgListener as ListenerOverride); | ||
scope.removeEventListener('focusout', blurListener); | ||
delete (scope as HTMLElementWithFocusVisibleScope).focusVisible; | ||
disposeKeyborg(keyborg); | ||
}; | ||
} | ||
|
||
function applyFocusVisibleClass(el: HTMLElement) { | ||
el.classList.add(FOCUS_VISIBLE_CLASS); | ||
} | ||
|
||
function removeFocusVisibleClass(el: HTMLElement) { | ||
el.classList.remove(FOCUS_VISIBLE_CLASS); | ||
} | ||
|
||
function isHTMLElement(target: EventTarget | null): target is HTMLElement { | ||
if (!target) { | ||
return false; | ||
} | ||
return Boolean(target && typeof target === 'object' && 'classList' in target && 'contains' in target); | ||
} | ||
|
||
function alreadyInScope(el: HTMLElement | null | undefined): boolean { | ||
if (!el) { | ||
return false; | ||
} | ||
|
||
if ((el as HTMLElementWithFocusVisibleScope).focusVisible) { | ||
return true; | ||
} | ||
|
||
return alreadyInScope(el?.parentElement); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 story with
createCustomFocusIndicatorStyle()
&useFocusWithin()
looks a bit dangerous for me as the dependency onuseFocusWithin()
is implicit. Do you have something on your mind to make it explicit?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.
tbh I am not happy at all that this
focus-within
requires an extra hook, but globally there is no way to know what parent of the focused element should have this class.The 'safety' mechanism currently is that
createCustomFocusIndicatorStyles
needs explicitfocus-within
selector to be used. The styles make sure that there is no any focus styles are applied unlessuseFocusWithin
is called.If anyone uses
:focus-within
directly (I think inputs do it) then they should know that there is no keyboard/mouse heuristic there.If you have any proposals, I'd be happy to hear them, I'm not 100% happy with the current situation either
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.
I don't have a good proposal, the one that I have will be a regression to the previous behavior in terms of performance...