-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathcomponent.tsx
194 lines (177 loc) · 4.81 KB
/
component.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
/**
* External dependencies
*/
import type { ForwardedRef } from 'react';
import * as Ariakit from '@ariakit/react';
/**
* WordPress dependencies
*/
import { useInstanceId } from '@wordpress/compose';
import { useLayoutEffect, useMemo, useRef } from '@wordpress/element';
/**
* Internal dependencies
*/
import type { WordPressComponentProps } from '../../context';
import { contextConnect, useContextSystem } from '../../context';
import type {
ToggleGroupControlOptionBaseProps,
WithToolTipProps,
} from '../types';
import { useToggleGroupControlContext } from '../context';
import * as styles from './styles';
import { useCx } from '../../utils/hooks';
import Tooltip from '../../tooltip';
const { ButtonContentView, LabelView } = styles;
const WithToolTip = ( { showTooltip, text, children }: WithToolTipProps ) => {
if ( showTooltip && text ) {
return (
<Tooltip text={ text } placement="top">
{ children }
</Tooltip>
);
}
return <>{ children }</>;
};
function ToggleGroupControlOptionBase(
props: Omit<
WordPressComponentProps<
ToggleGroupControlOptionBaseProps,
'button',
false
>,
// the element's id is generated internally
| 'id'
// due to how the component works, only the `disabled` prop should be used
| 'aria-disabled'
>,
forwardedRef: ForwardedRef< any >
) {
const toggleGroupControlContext = useToggleGroupControlContext();
const id = useInstanceId(
ToggleGroupControlOptionBase,
toggleGroupControlContext.baseId || 'toggle-group-control-option-base'
);
const buttonProps = useContextSystem(
{ ...props, id },
'ToggleGroupControlOptionBase'
);
const {
isBlock = false,
isDeselectable = false,
size = 'default',
} = toggleGroupControlContext;
const {
className,
isIcon = false,
value,
children,
showTooltip = false,
disabled,
...otherButtonProps
} = buttonProps;
const isPressed = toggleGroupControlContext.value === value;
const cx = useCx();
const labelViewClasses = useMemo(
() => cx( isBlock && styles.labelBlock ),
[ cx, isBlock ]
);
const itemClasses = useMemo(
() =>
cx(
styles.buttonView( {
isDeselectable,
isIcon,
isPressed,
size,
} ),
className
),
[ cx, isDeselectable, isIcon, isPressed, size, className ]
);
const buttonOnClick = () => {
if ( isDeselectable && isPressed ) {
toggleGroupControlContext.setValue( undefined );
} else {
toggleGroupControlContext.setValue( value );
}
};
const commonProps = {
...otherButtonProps,
className: itemClasses,
'data-value': value,
ref: forwardedRef,
};
const labelRef = useRef< HTMLDivElement | null >( null );
useLayoutEffect( () => {
if ( isPressed && labelRef.current ) {
toggleGroupControlContext.setSelectedElement( labelRef.current );
}
}, [ isPressed, toggleGroupControlContext ] );
return (
<LabelView ref={ labelRef } className={ labelViewClasses }>
<WithToolTip
showTooltip={ showTooltip }
text={ otherButtonProps[ 'aria-label' ] }
>
{ isDeselectable ? (
<button
{ ...commonProps }
disabled={ disabled }
aria-pressed={ isPressed }
type="button"
onClick={ buttonOnClick }
>
<ButtonContentView>{ children }</ButtonContentView>
</button>
) : (
<Ariakit.Radio
disabled={ disabled }
onFocusVisible={ () => {
const selectedValueIsEmpty =
toggleGroupControlContext.value === null ||
toggleGroupControlContext.value === '';
// Conditions ensure that the first visible focus to a radio group
// without a selected option will not automatically select the option.
if (
! selectedValueIsEmpty ||
toggleGroupControlContext.activeItemIsNotFirstItem?.()
) {
toggleGroupControlContext.setValue( value );
}
} }
render={ <button type="button" { ...commonProps } /> }
value={ value }
>
<ButtonContentView>{ children }</ButtonContentView>
</Ariakit.Radio>
) }
</WithToolTip>
</LabelView>
);
}
/**
* `ToggleGroupControlOptionBase` is a form component and is meant to be used as an internal,
* generic component for any children of `ToggleGroupControl`.
*
* @example
* ```jsx
* import {
* __experimentalToggleGroupControl as ToggleGroupControl,
* __experimentalToggleGroupControlOptionBase as ToggleGroupControlOptionBase,
* } from '@wordpress/components';
*
* function Example() {
* return (
* <ToggleGroupControl label="my label" value="vertical" isBlock>
* <ToggleGroupControlOption value="horizontal" label="Horizontal" />
* <ToggleGroupControlOption value="vertical" label="Vertical" />
* </ToggleGroupControl>
* );
* }
* ```
*/
const ConnectedToggleGroupControlOptionBase = contextConnect(
ToggleGroupControlOptionBase,
'ToggleGroupControlOptionBase'
);
export default ConnectedToggleGroupControlOptionBase;