-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
component.tsx
203 lines (186 loc) · 4.82 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
195
196
197
198
199
200
201
202
203
/**
* External dependencies
*/
import type { ForwardedRef } from 'react';
// eslint-disable-next-line no-restricted-imports
import { Radio } from '@ariakit/react/radio';
// eslint-disable-next-line no-restricted-imports
import { motion, useReducedMotion } from 'framer-motion';
/**
* WordPress dependencies
*/
import { useInstanceId } from '@wordpress/compose';
import { useMemo } from '@wordpress/element';
/**
* Internal dependencies
*/
import type { WordPressComponentProps } from '../../ui/context';
import { contextConnect, useContextSystem } from '../../ui/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 REDUCED_MOTION_TRANSITION_CONFIG = {
duration: 0,
};
const LAYOUT_ID = 'toggle-group-backdrop-shared-layout-id';
const WithToolTip = ( { showTooltip, text, children }: WithToolTipProps ) => {
if ( showTooltip && text ) {
return (
<Tooltip text={ text } position="top center">
{ children }
</Tooltip>
);
}
return <>{ children }</>;
};
function ToggleGroupControlOptionBase(
props: Omit<
WordPressComponentProps<
ToggleGroupControlOptionBaseProps,
'button',
false
>,
// the element's id is generated internally
'id'
>,
forwardedRef: ForwardedRef< any >
) {
const shouldReduceMotion = useReducedMotion();
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,
onFocus: onFocusProp,
...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 backdropClasses = useMemo( () => cx( styles.backdropView ), [ cx ] );
const buttonOnClick = () => {
if ( isDeselectable && isPressed ) {
toggleGroupControlContext.setValue( undefined );
} else {
toggleGroupControlContext.setValue( value );
}
};
const commonProps = {
...otherButtonProps,
className: itemClasses,
'data-value': value,
ref: forwardedRef,
};
return (
<LabelView className={ labelViewClasses }>
<WithToolTip
showTooltip={ showTooltip }
text={ otherButtonProps[ 'aria-label' ] }
>
{ isDeselectable ? (
<button
{ ...commonProps }
onFocus={ onFocusProp }
aria-pressed={ isPressed }
type="button"
onClick={ buttonOnClick }
>
<ButtonContentView>{ children }</ButtonContentView>
</button>
) : (
<Radio
render={
<button
{ ...commonProps }
onFocus={ ( event ) => {
onFocusProp?.( event );
if ( event.defaultPrevented ) return;
toggleGroupControlContext.setValue( value );
} }
/>
}
value={ value }
>
<ButtonContentView>{ children }</ButtonContentView>
</Radio>
) }
</WithToolTip>
{ /* Animated backdrop using framer motion's shared layout animation */ }
{ isPressed ? (
<motion.div
className={ backdropClasses }
transition={
shouldReduceMotion
? REDUCED_MOTION_TRANSITION_CONFIG
: undefined
}
role="presentation"
layoutId={ LAYOUT_ID }
/>
) : null }
</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;