-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
index.tsx
207 lines (189 loc) · 5.11 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/**
* External dependencies
*/
import classnames from 'classnames';
import type { ForwardedRef } from 'react';
/**
* WordPress dependencies
*/
import { useEffect, useRef, useState } from '@wordpress/element';
import { useMergeRefs } from '@wordpress/compose';
import deprecated from '@wordpress/deprecated';
/**
* Internal dependencies
*/
import { contextConnect, useContextSystem } from '../ui/context';
import Popover from '../popover';
import type { DropdownProps, DropdownInternalContext } from './types';
function useObservableState(
initialState: boolean,
onStateChange?: ( newState: boolean ) => void
) {
const [ state, setState ] = useState( initialState );
return [
state,
( value: boolean ) => {
setState( value );
if ( onStateChange ) {
onStateChange( value );
}
},
] as const;
}
const UnconnectedDropdown = (
props: DropdownProps,
forwardedRef: ForwardedRef< any >
) => {
const {
renderContent,
renderToggle,
className,
contentClassName,
expandOnMobile,
headerTitle,
focusOnMount,
popoverProps,
onClose,
onToggle,
style,
open: openProp,
// Deprecated props
position,
// From context system
variant,
} = useContextSystem< DropdownProps & DropdownInternalContext >(
props,
'Dropdown'
);
if ( position !== undefined ) {
deprecated( '`position` prop in wp.components.Dropdown', {
since: '6.2',
alternative: '`popoverProps.placement` prop',
hint: 'Note that the `position` prop will override any values passed through the `popoverProps.placement` prop.',
} );
}
// Use internal state instead of a ref to make sure that the component
// re-renders when the popover's anchor updates.
const [ fallbackPopoverAnchor, setFallbackPopoverAnchor ] =
useState< HTMLDivElement | null >( null );
const containerRef = useRef< HTMLDivElement >();
const [ isOpenState, setIsOpen ] = useObservableState( false, onToggle );
// Allow provided `isOpen` prop to override internal state.
const isOpen = openProp ?? isOpenState;
useEffect(
() => () => {
if ( onToggle && isOpen ) {
onToggle( false );
}
},
[ onToggle, isOpen ]
);
function toggle() {
setIsOpen( ! isOpen );
}
/**
* Closes the popover when focus leaves it unless the toggle was pressed or
* focus has moved to a separate dialog. The former is to let the toggle
* handle closing the popover and the latter is to preserve presence in
* case a dialog has opened, allowing focus to return when it's dismissed.
*/
function closeIfFocusOutside() {
if ( ! containerRef.current ) {
return;
}
const { ownerDocument } = containerRef.current;
const dialog =
ownerDocument?.activeElement?.closest( '[role="dialog"]' );
if (
! containerRef.current.contains( ownerDocument.activeElement ) &&
( ! dialog || dialog.contains( containerRef.current ) )
) {
close();
}
}
function close() {
if ( onClose ) {
onClose();
}
setIsOpen( false );
}
const args = { isOpen, onToggle: toggle, onClose: close };
const popoverPropsHaveAnchor =
!! popoverProps?.anchor ||
// Note: `anchorRef`, `getAnchorRect` and `anchorRect` are deprecated and
// be removed from `Popover` from WordPress 6.3
!! popoverProps?.anchorRef ||
!! popoverProps?.getAnchorRect ||
!! popoverProps?.anchorRect;
return (
<div
className={ className }
ref={ useMergeRefs( [
containerRef,
forwardedRef,
setFallbackPopoverAnchor,
] ) }
// Some UAs focus the closest focusable parent when the toggle is
// clicked. Making this div focusable ensures such UAs will focus
// it and `closeIfFocusOutside` can tell if the toggle was clicked.
tabIndex={ -1 }
style={ style }
>
{ renderToggle( args ) }
{ isOpen && (
<Popover
position={ position }
onClose={ close }
onFocusOutside={ closeIfFocusOutside }
expandOnMobile={ expandOnMobile }
headerTitle={ headerTitle }
focusOnMount={ focusOnMount }
// This value is used to ensure that the dropdowns
// align with the editor header by default.
offset={ 13 }
anchor={
! popoverPropsHaveAnchor
? fallbackPopoverAnchor
: undefined
}
variant={ variant }
{ ...popoverProps }
className={ classnames(
'components-dropdown__content',
popoverProps?.className,
contentClassName
) }
>
{ renderContent( args ) }
</Popover>
) }
</div>
);
};
/**
* Renders a button that opens a floating content modal when clicked.
*
* ```jsx
* import { Button, Dropdown } from '@wordpress/components';
*
* const MyDropdown = () => (
* <Dropdown
* className="my-container-class-name"
* contentClassName="my-dropdown-content-classname"
* popoverProps={ { placement: 'bottom-start' } }
* renderToggle={ ( { isOpen, onToggle } ) => (
* <Button
* variant="primary"
* onClick={ onToggle }
* aria-expanded={ isOpen }
* >
* Toggle Dropdown!
* </Button>
* ) }
* renderContent={ () => <div>This is the content of the dropdown.</div> }
* />
* );
* ```
*/
export const Dropdown = contextConnect( UnconnectedDropdown, 'Dropdown' );
export default Dropdown;