UNPKG

@wordpress/components

Version:
77 lines (73 loc) 2.02 kB
import _extends from "@babel/runtime/helpers/esm/extends"; import { createElement } from "@wordpress/element"; /** * WordPress dependencies */ import { createContext } from '@wordpress/element'; /** * Internal dependencies */ import { disabledStyles } from './styles/disabled-styles'; import { useCx } from '../utils'; const Context = createContext(false); const { Consumer, Provider } = Context; /** * `Disabled` is a component which disables descendant tabbable elements and * prevents pointer interaction. * * _Note: this component may not behave as expected in browsers that don't * support the `inert` HTML attribute. We recommend adding the official WICG * polyfill when using this component in your project._ * * @see https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/inert * * ```jsx * import { Button, Disabled, TextControl } from '@wordpress/components'; * import { useState } from '@wordpress/element'; * * const MyDisabled = () => { * const [ isDisabled, setIsDisabled ] = useState( true ); * * let input = <TextControl label="Input" onChange={ () => {} } />; * if ( isDisabled ) { * input = <Disabled>{ input }</Disabled>; * } * * const toggleDisabled = () => { * setIsDisabled( ( state ) => ! state ); * }; * * return ( * <div> * { input } * <Button variant="primary" onClick={ toggleDisabled }> * Toggle Disabled * </Button> * </div> * ); * }; * ``` */ function Disabled(_ref) { let { className, children, isDisabled = true, ...props } = _ref; const cx = useCx(); return createElement(Provider, { value: isDisabled }, createElement("div", _extends({ // @ts-ignore Reason: inert is a recent HTML attribute inert: isDisabled ? 'true' : undefined, className: isDisabled ? cx(disabledStyles, className, 'components-disabled') : undefined }, props), children)); } Disabled.Context = Context; Disabled.Consumer = Consumer; export default Disabled; //# sourceMappingURL=index.js.map