-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
component.tsx
156 lines (145 loc) · 3.7 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
/**
* External dependencies
*/
import type { ForwardedRef } from 'react';
// eslint-disable-next-line no-restricted-imports
import { Radio } from 'reakit';
/**
* WordPress dependencies
*/
import { useInstanceId } from '@wordpress/compose';
/**
* Internal dependencies
*/
import {
contextConnect,
useContextSystem,
WordPressComponentProps,
} 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 WithToolTip = ( { showTooltip, text, children }: WithToolTipProps ) => {
if ( showTooltip && text ) {
return (
<Tooltip text={ text } position="top center">
{ children }
</Tooltip>
);
}
return <>{ children }</>;
};
function ToggleGroupControlOptionBase(
props: WordPressComponentProps<
ToggleGroupControlOptionBaseProps,
'button',
false
>,
forwardedRef: ForwardedRef< any >
) {
const toggleGroupControlContext = useToggleGroupControlContext();
const id = useInstanceId(
ToggleGroupControlOptionBase,
toggleGroupControlContext.baseId || 'toggle-group-control-option-base'
) as string;
const buttonProps = useContextSystem(
{ ...props, id },
'ToggleGroupControlOptionBase'
);
const {
isBlock = false,
isDeselectable = false,
size = 'default',
...otherContextProps /* context props for Ariakit Radio */
} = toggleGroupControlContext;
const {
className,
isIcon = false,
value,
children,
showTooltip = false,
...otherButtonProps
} = buttonProps;
const isPressed = otherContextProps.state === value;
const cx = useCx();
const labelViewClasses = cx( isBlock && styles.labelBlock );
const classes = cx(
styles.buttonView( { isDeselectable, isIcon, isPressed, size } ),
className
);
const buttonOnClick = () => {
if ( isDeselectable && isPressed ) {
otherContextProps.setState( undefined );
} else {
otherContextProps.setState( value );
}
};
const commonProps = {
...otherButtonProps,
className: classes,
'data-value': value,
ref: forwardedRef,
};
return (
<LabelView className={ labelViewClasses }>
<WithToolTip
showTooltip={ showTooltip }
text={ otherButtonProps[ 'aria-label' ] }
>
{ isDeselectable ? (
<button
{ ...commonProps }
aria-pressed={ isPressed }
type="button"
onClick={ buttonOnClick }
>
<ButtonContentView>{ children }</ButtonContentView>
</button>
) : (
<Radio
{ ...commonProps }
{
...otherContextProps /* these are only for Ariakit Radio */
}
as="button"
value={ value }
>
<ButtonContentView>{ children }</ButtonContentView>
</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;