-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor block preview behaviour to a useBlockPreview hook for the po…
…st content block
- Loading branch information
1 parent
04caf9b
commit b06b374
Showing
7 changed files
with
195 additions
and
66 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { includes, debounce } from 'lodash'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useCallback, useLayoutEffect, useRef } from '@wordpress/element'; | ||
import { focus } from '@wordpress/dom'; | ||
|
||
/** | ||
* Names of control nodes which qualify for disabled behavior. | ||
* | ||
* See WHATWG HTML Standard: 4.10.18.5: "Enabling and disabling form controls: the disabled attribute". | ||
* | ||
* @see https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#enabling-and-disabling-form-controls:-the-disabled-attribute | ||
* | ||
* @type {string[]} | ||
*/ | ||
const DISABLED_ELIGIBLE_NODE_NAMES = [ | ||
'BUTTON', | ||
'FIELDSET', | ||
'INPUT', | ||
'OPTGROUP', | ||
'OPTION', | ||
'SELECT', | ||
'TEXTAREA', | ||
]; | ||
|
||
export default function useDisabled() { | ||
/** @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 ) | ||
) { | ||
focusable.setAttribute( 'disabled', '' ); | ||
} | ||
|
||
if ( focusable.nodeName === 'A' ) { | ||
focusable.setAttribute( 'tabindex', '-1' ); | ||
} | ||
|
||
const tabIndex = focusable.getAttribute( 'tabindex' ); | ||
if ( tabIndex !== null && tabIndex !== '-1' ) { | ||
focusable.removeAttribute( 'tabindex' ); | ||
} | ||
|
||
if ( focusable.hasAttribute( 'contenteditable' ) ) { | ||
focusable.setAttribute( 'contenteditable', 'false' ); | ||
} | ||
} ); | ||
}; | ||
|
||
// Debounce re-disable since disabling process itself will incur | ||
// additional mutations which should be ignored. | ||
const debouncedDisable = useCallback( | ||
debounce( disable, undefined, { leading: true } ), | ||
[] | ||
); | ||
|
||
useLayoutEffect( () => { | ||
disable(); | ||
|
||
/** @type {MutationObserver | undefined} */ | ||
let observer; | ||
if ( node.current ) { | ||
observer = new window.MutationObserver( debouncedDisable ); | ||
observer.observe( node.current, { | ||
childList: true, | ||
attributes: true, | ||
subtree: true, | ||
} ); | ||
} | ||
|
||
return () => { | ||
if ( observer ) { | ||
observer.disconnect(); | ||
} | ||
debouncedDisable.cancel(); | ||
}; | ||
}, [] ); | ||
|
||
return node; | ||
} |
This file contains 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