Skip to content
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

components: Add types to Disabled #32105

Merged
merged 1 commit into from
May 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 32 additions & 10 deletions packages/components/src/disabled/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,26 @@ const DISABLED_ELIGIBLE_NODE_NAMES = [
'TEXTAREA',
];

/**
* @typedef OwnProps
* @property {string} [className] Classname for the disabled element.
* @property {import('react').ReactNode} children Children to disable.
* @property {boolean} [isDisabled=true] Whether to disable the children.
*/

/**
* @param {OwnProps & import('react').HTMLAttributes<HTMLDivElement>} props
* @return {JSX.Element} Element wrapping the children to disable them when isDisabled is true.
*/
function Disabled( { className, children, isDisabled = true, ...props } ) {
const node = useRef();
/** @type {import('react').RefObject<HTMLDivElement>} */
const node = useRef( null );

const disable = () => {
if ( ! node.current ) {
return;
}

focus.focusable.find( node.current ).forEach( ( focusable ) => {
if (
includes( DISABLED_ELIGIBLE_NODE_NAMES, focusable.nodeName )
Expand All @@ -54,7 +70,7 @@ function Disabled( { className, children, isDisabled = true, ...props } ) {
}

if ( focusable.nodeName === 'A' ) {
focusable.setAttribute( 'tabindex', -1 );
focusable.setAttribute( 'tabindex', '-1' );
}

const tabIndex = focusable.getAttribute( 'tabindex' );
Expand All @@ -71,7 +87,7 @@ function Disabled( { className, children, isDisabled = true, ...props } ) {
// Debounce re-disable since disabling process itself will incur
// additional mutations which should be ignored.
const debouncedDisable = useCallback(
debounce( disable, { leading: true } ),
debounce( disable, undefined, { leading: true } ),
[]
);

Expand All @@ -82,15 +98,21 @@ function Disabled( { className, children, isDisabled = true, ...props } ) {

disable();

const observer = new window.MutationObserver( debouncedDisable );
observer.observe( node.current, {
childList: true,
attributes: true,
subtree: true,
} );
/** @type {MutationObserver | undefined} */
let observer;
if ( node.current ) {
observer = new window.MutationObserver( debouncedDisable );
observer.observe( node.current, {
childList: true,
attributes: true,
subtree: true,
} );
}

return () => {
observer.disconnect();
if ( observer ) {
observer.disconnect();
}
debouncedDisable.cancel();
};
}, [] );
Expand Down
2 changes: 2 additions & 0 deletions packages/components/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
},
"references": [
{ "path": "../deprecated" },
{ "path": "../dom" },
{ "path": "../element" },
{ "path": "../hooks" },
{ "path": "../icons" },
Expand All @@ -19,6 +20,7 @@
"src/animate/**/*",
"src/base-control/**/*",
"src/dashicon/**/*",
"src/disabled/**/*",
"src/draggable/**/*",
"src/flex/**/*",
"src/form-group/**/*",
Expand Down