-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
index.tsx
83 lines (77 loc) · 1.99 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/**
* WordPress dependencies
*/
import { createContext } from '@wordpress/element';
/**
* Internal dependencies
*/
import { disabledStyles } from './styles/disabled-styles';
import type { DisabledProps } from './types';
import type { WordPressComponentProps } from '../context';
import { useCx } from '../utils';
const Context = createContext< boolean >( 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( {
className,
children,
isDisabled = true,
...props
}: WordPressComponentProps< DisabledProps, 'div' > ) {
const cx = useCx();
return (
<Provider value={ isDisabled }>
<div
// @ts-ignore Reason: inert is a recent HTML attribute
inert={ isDisabled ? 'true' : undefined }
className={
isDisabled
? cx( disabledStyles, className, 'components-disabled' )
: undefined
}
{ ...props }
>
{ children }
</div>
</Provider>
);
}
Disabled.Context = Context;
Disabled.Consumer = Consumer;
export default Disabled;