一些检查失败了
automerge / automerge (push) Has been cancelled
react-native-android-build-apk / AssembleRelease (push) Has been cancelled
Build macOS M1 / Main (push) Has been cancelled
Joplin Continuous Integration / Main (macos-15-intel) (push) Has been cancelled
Joplin Continuous Integration / Main (ubuntu-22.04) (push) Has been cancelled
Joplin Continuous Integration / Main (ubuntu-22.04-arm) (push) Has been cancelled
Joplin Continuous Integration / Main (windows-11-vs2026-arm) (push) Has been cancelled
Joplin Continuous Integration / Main (windows-2025) (push) Has been cancelled
Joplin Continuous Integration / ServerDockerImage (ubuntu-22.04) (push) Has been cancelled
Joplin Continuous Integration / ServerDockerImage (ubuntu-22.04-arm) (push) Has been cancelled
Joplin UI tests / Main (ubuntu-22.04) (push) Has been cancelled
Joplin UI tests / Main (windows-2025) (push) Has been cancelled
Joplin UI tests / Mobile (push) Has been cancelled
Close stale issues / ProcessStaleIssues (push) Has been cancelled
46 行
1.2 KiB
TypeScript
46 行
1.2 KiB
TypeScript
import { useCallback, useState, useRef, RefObject } from 'react';
|
|
|
|
const useFocusVisible = (containerRef: RefObject<HTMLElement>, onFocusEnter: ()=> void) => {
|
|
const [focusVisible, setFocusVisible] = useState(false);
|
|
|
|
const onFocusEnterRef = useRef(onFocusEnter);
|
|
onFocusEnterRef.current = onFocusEnter;
|
|
const focusVisibleRef = useRef(focusVisible);
|
|
focusVisibleRef.current = focusVisible;
|
|
|
|
const onFocusVisible = useCallback(() => {
|
|
if (!focusVisibleRef.current) {
|
|
setFocusVisible(true);
|
|
onFocusEnterRef.current();
|
|
}
|
|
}, []);
|
|
|
|
const onFocus = useCallback(() => {
|
|
if (containerRef.current.matches(':focus-visible')) {
|
|
onFocusVisible();
|
|
}
|
|
}, [containerRef, onFocusVisible]);
|
|
|
|
const onKeyUp = useCallback(() => {
|
|
if (containerRef.current.contains(document.activeElement)) {
|
|
onFocusVisible();
|
|
}
|
|
}, [containerRef, onFocusVisible]);
|
|
|
|
const onBlur = useCallback(() => setFocusVisible(false), []);
|
|
|
|
return {
|
|
focusVisible,
|
|
onFocus,
|
|
|
|
// When focus becomes visible due to a key press, but the item was already
|
|
// focused, no new focus event is emitted and the browser :focus-visible doesn't
|
|
// change. As such, we need to handle this case ourselves.
|
|
onKeyUp,
|
|
|
|
onBlur,
|
|
};
|
|
};
|
|
|
|
export default useFocusVisible;
|