-
Notifications
You must be signed in to change notification settings - Fork 198
feat: add ignored element support for focus lock #727
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ee2acde
feat: add ignored element support for focus lock
zombieJ 2155804
Update src/Dom/focus.ts
zombieJ d73787b
test: add test for ignoreElement functionality
zombieJ ba194a5
refactor: use stable ID as key for ignoredElementMap
zombieJ a8fd11c
refactor: simplify map structure - use id mapping to element
zombieJ 6cd187e
refactor: simplify isIgnoredElement logic
zombieJ 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 hidden or 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 |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| import { useEffect } from 'react'; | ||
| import isVisible from './isVisible'; | ||
| import useId from '../hooks/useId'; | ||
|
|
||
| type DisabledElement = | ||
| | HTMLLinkElement | ||
|
|
@@ -102,11 +103,37 @@ export function triggerFocus( | |
| // ====================================================== | ||
| let lastFocusElement: HTMLElement | null = null; | ||
| let focusElements: HTMLElement[] = []; | ||
| // Map stable ID to lock element | ||
| const idToElementMap = new Map<string, HTMLElement>(); | ||
| // Map stable ID to ignored element | ||
| const ignoredElementMap = new Map<string, HTMLElement | null>(); | ||
|
|
||
| function getLastElement() { | ||
| return focusElements[focusElements.length - 1]; | ||
| } | ||
|
|
||
| function isIgnoredElement(element: Element | null): boolean { | ||
| const lastElement = getLastElement(); | ||
|
|
||
| if (element && lastElement) { | ||
| // Find the ID that maps to the last element | ||
| let lockId: string | undefined; | ||
| for (const [id, ele] of idToElementMap.entries()) { | ||
| if (ele === lastElement) { | ||
| lockId = id; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| const ignoredEle = ignoredElementMap.get(lockId); | ||
| return ( | ||
| !!ignoredEle && (ignoredEle === element || ignoredEle.contains(element)) | ||
| ); | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| function hasFocus(element: HTMLElement) { | ||
| const { activeElement } = document; | ||
| return element === activeElement || element.contains(activeElement); | ||
|
|
@@ -116,6 +143,11 @@ function syncFocus() { | |
| const lastElement = getLastElement(); | ||
| const { activeElement } = document; | ||
|
|
||
| // If current focus is on an ignored element, don't force it back | ||
| if (isIgnoredElement(activeElement as HTMLElement)) { | ||
| return; | ||
| } | ||
|
|
||
| if (lastElement && !hasFocus(lastElement)) { | ||
| const focusableList = getFocusNodeList(lastElement); | ||
|
|
||
|
|
@@ -149,9 +181,13 @@ function onWindowKeyDown(e: KeyboardEvent) { | |
| /** | ||
| * Lock focus in the element. | ||
| * It will force back to the first focusable element when focus leaves the element. | ||
| * @param id - A stable ID for this lock instance | ||
| */ | ||
| export function lockFocus(element: HTMLElement): VoidFunction { | ||
| export function lockFocus(element: HTMLElement, id: string): VoidFunction { | ||
| if (element) { | ||
| // Store the mapping between ID and element | ||
| idToElementMap.set(id, element); | ||
|
|
||
| // Refresh focus elements | ||
| focusElements = focusElements.filter(ele => ele !== element); | ||
| focusElements.push(element); | ||
|
|
@@ -166,6 +202,8 @@ export function lockFocus(element: HTMLElement): VoidFunction { | |
| return () => { | ||
| lastFocusElement = null; | ||
| focusElements = focusElements.filter(ele => ele !== element); | ||
| idToElementMap.delete(id); | ||
| ignoredElementMap.delete(id); | ||
| if (focusElements.length === 0) { | ||
| window.removeEventListener('focusin', syncFocus); | ||
| window.removeEventListener('keydown', onWindowKeyDown, true); | ||
|
|
@@ -177,17 +215,29 @@ export function lockFocus(element: HTMLElement): VoidFunction { | |
| * Lock focus within an element. | ||
| * When locked, focus will be restricted to focusable elements within the specified element. | ||
| * If multiple elements are locked, only the last locked element will be effective. | ||
| * @returns A function to mark an element as ignored, which will temporarily allow focus on that element even if it's outside the locked area. | ||
| */ | ||
| export function useLockFocus( | ||
| lock: boolean, | ||
| getElement: () => HTMLElement | null, | ||
| ) { | ||
| ): [ignoreElement: (ele: HTMLElement) => void] { | ||
| const id = useId(); | ||
|
|
||
| useEffect(() => { | ||
| if (lock) { | ||
| const element = getElement(); | ||
| if (element) { | ||
| return lockFocus(element); | ||
| return lockFocus(element, id); | ||
| } | ||
| } | ||
| }, [lock]); | ||
| }, [lock, id]); | ||
|
|
||
| const ignoreElement = (ele: HTMLElement) => { | ||
| if (ele) { | ||
| // Set the ignored element using stable ID | ||
| ignoredElementMap.set(id, ele); | ||
| } | ||
| }; | ||
|
|
||
| return [ignoreElement]; | ||
|
Comment on lines
+235
to
+242
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. 🛠️ Refactor suggestion | 🟠 Major
由于 ♻️ 建议使用 useCallback+import { useEffect, useCallback } from 'react';
-import { useEffect } from 'react';- const ignoreElement = (ele: HTMLElement) => {
- if (ele) {
- // Set the ignored element using stable ID
- ignoredElementMap.set(id, ele);
- }
- };
+ const ignoreElement = useCallback((ele: HTMLElement) => {
+ if (ele) {
+ ignoredElementMap.set(id, ele);
+ }
+ }, [id]);🤖 Prompt for AI Agents |
||
| } | ||
This file contains hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.