From 51b7cbd34213c207584d4180634130e5b9b60e1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aar=C3=B3n=20Garc=C3=ADa=20Herv=C3=A1s?= Date: Tue, 29 Oct 2024 15:46:33 +0100 Subject: [PATCH] Bring back useIsFocusVisible --- packages/mui-utils/src/index.ts | 1 + .../mui-utils/src/useIsFocusVisible/index.ts | 2 + .../useIsFocusVisible.test.js | 126 +++++++++++++ .../useIsFocusVisible/useIsFocusVisible.ts | 175 ++++++++++++++++++ 4 files changed, 304 insertions(+) create mode 100644 packages/mui-utils/src/useIsFocusVisible/index.ts create mode 100644 packages/mui-utils/src/useIsFocusVisible/useIsFocusVisible.test.js create mode 100644 packages/mui-utils/src/useIsFocusVisible/useIsFocusVisible.ts diff --git a/packages/mui-utils/src/index.ts b/packages/mui-utils/src/index.ts index eb8cfe9ed67f1b..d1b651419059a1 100644 --- a/packages/mui-utils/src/index.ts +++ b/packages/mui-utils/src/index.ts @@ -27,6 +27,7 @@ export { default as unstable_useForkRef } from './useForkRef'; export { default as unstable_useLazyRef } from './useLazyRef'; export { default as unstable_useTimeout, Timeout as unstable_Timeout } from './useTimeout'; export { default as unstable_useOnMount } from './useOnMount'; +export { default as unstable_useIsFocusVisible } from './useIsFocusVisible'; export { default as unstable_isFocusVisible } from './isFocusVisible'; export { default as unstable_getScrollbarSize } from './getScrollbarSize'; export { default as usePreviousProps } from './usePreviousProps'; diff --git a/packages/mui-utils/src/useIsFocusVisible/index.ts b/packages/mui-utils/src/useIsFocusVisible/index.ts new file mode 100644 index 00000000000000..9f7a0b9ab5e7ad --- /dev/null +++ b/packages/mui-utils/src/useIsFocusVisible/index.ts @@ -0,0 +1,2 @@ +export { default } from './useIsFocusVisible'; +export * from './useIsFocusVisible'; diff --git a/packages/mui-utils/src/useIsFocusVisible/useIsFocusVisible.test.js b/packages/mui-utils/src/useIsFocusVisible/useIsFocusVisible.test.js new file mode 100644 index 00000000000000..d0c9350a1e0ad8 --- /dev/null +++ b/packages/mui-utils/src/useIsFocusVisible/useIsFocusVisible.test.js @@ -0,0 +1,126 @@ +import { expect } from 'chai'; +import * as React from 'react'; +import * as ReactDOMClient from 'react-dom/client'; +import { + act, + createRenderer, + focusVisible, + simulatePointerDevice, + programmaticFocusTriggersFocusVisible, +} from '@mui/internal-test-utils'; +import useIsFocusVisible, { teardown as teardownFocusVisible } from './useIsFocusVisible'; +import useForkRef from '../useForkRef'; + +const SimpleButton = React.forwardRef(function SimpleButton(props, ref) { + const { + isFocusVisibleRef, + onBlur: handleBlurVisible, + onFocus: handleFocusVisible, + ref: focusVisibleRef, + } = useIsFocusVisible(); + + const handleRef = useForkRef(focusVisibleRef, ref); + + const [isFocusVisible, setIsFocusVisible] = React.useState(false); + + const handleBlur = (event) => { + handleBlurVisible(event); + if (isFocusVisibleRef.current === false) { + setIsFocusVisible(false); + } + }; + + const handleFocus = (event) => { + handleFocusVisible(event); + if (isFocusVisibleRef.current === true) { + setIsFocusVisible(true); + } + }; + + return ( +