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

useDisabled: isDisabled to be deprecated. #51398

Closed
wants to merge 3 commits into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ export function useBlockProps( props = {}, { __unstableIsHtml } = {} ) {
enableAnimation,
triggerAnimationOnChange: index,
} ),
useDisabled( { isDisabled: ! hasOverlay } ),
useDisabled( { inert: !! hasOverlay } ),
] );

const blockEditContext = useBlockEditContext();
Expand Down
2 changes: 1 addition & 1 deletion packages/block-editor/src/components/iframe/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ function Iframe( {
forceRender();
} );
}, [] );
const disabledRef = useDisabled( { isDisabled: ! readonly } );
const disabledRef = useDisabled( { inert: !! readonly } );
const bodyRef = useMergeRefs( [
contentRef,
clearerRef,
Expand Down
2 changes: 1 addition & 1 deletion packages/compose/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ const DisabledExample = () => {
_Parameters_

- _config_ `Object`: Configuration object.
- _config.isDisabled_ `boolean=`: Whether the element should be disabled.
- _config.inert_ `boolean|undefined`: Whether to disable the element or not. If this is false, the element is not disabled.

_Returns_

Expand Down
46 changes: 40 additions & 6 deletions packages/compose/src/hooks/use-disabled/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,27 @@
*/
import { debounce } from '../../utils/debounce';
import useRefEffect from '../use-ref-effect';
/**
* WordPress dependencies
*/
import deprecated from '@wordpress/deprecated';

type UseDisabledProps = {
/**
* Whether the element should be disabled. @deprecated
*
* @default false
* @deprecated
*/
isDisabled?: boolean;

/**
* Whether to disable the element or not. If this is false, the element is not disabled.
*
* @default true
*/
inert?: boolean;
};

/**
* In some circumstances, such as block previews, all focusable DOM elements
Expand All @@ -11,8 +32,8 @@ import useRefEffect from '../use-ref-effect';
*
* If you can, prefer the use of the inert HTML attribute.
*
* @param {Object} config Configuration object.
* @param {boolean=} config.isDisabled Whether the element should be disabled.
* @param {Object} config Configuration object.
* @param {boolean|undefined} config.inert Whether to disable the element or not. If this is false, the element is not disabled.
* @return {import('react').RefCallback<HTMLElement>} Element Ref.
*
* @example
Expand All @@ -31,11 +52,24 @@ import useRefEffect from '../use-ref-effect';
* ```
*/
export default function useDisabled( {
isDisabled: isDisabledProp = false,
} = {} ) {
inert: inertProp = true,
...props
}: UseDisabledProps = {} ) {
let isInert = inertProp;
const { isDisabled } = props;

if ( isDisabled !== undefined ) {
deprecated( '`isDisabled` parameter in `useDisabled`', {
since: '6.3',
alternative: '`inert` parameter',
} );

isInert = ! isDisabled;
}

return useRefEffect(
( node ) => {
if ( isDisabledProp ) {
if ( ! isInert ) {
return;
}

Expand Down Expand Up @@ -81,6 +115,6 @@ export default function useDisabled( {
updates.forEach( ( update ) => update() );
};
},
[ isDisabledProp ]
[ isInert ]
);
}
14 changes: 13 additions & 1 deletion packages/compose/src/hooks/use-disabled/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe( 'useDisabled', () => {
} );

function DisabledComponent( props ) {
const disabledRef = useDisabled();
const disabledRef = useDisabled( { inert: props.inert } );
return <Form ref={ disabledRef } { ...props } />;
}

Expand All @@ -42,6 +42,18 @@ describe( 'useDisabled', () => {
expect( p ).toHaveAttribute( 'inert', 'true' );
} );

it( 'will not disable all fields', () => {
render( <DisabledComponent inert={ false } /> );

const input = screen.getByRole( 'textbox' );
const link = screen.getByRole( 'link' );
const p = screen.getByRole( 'document' );

expect( input ).not.toHaveAttribute( 'inert' );
expect( link ).not.toHaveAttribute( 'inert' );
expect( p ).not.toHaveAttribute( 'inert' );
} );

it( 'will disable an element rendered in an update to the component', async () => {
const { rerender } = render(
<DisabledComponent showButton={ false } />
Expand Down