-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
circular-option-picker-option.tsx
122 lines (109 loc) · 2.77 KB
/
circular-option-picker-option.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
/**
* External dependencies
*/
import clsx from 'clsx';
import type { ForwardedRef } from 'react';
/**
* WordPress dependencies
*/
import { useInstanceId } from '@wordpress/compose';
import { forwardRef, useContext, useEffect } from '@wordpress/element';
import { Icon, check } from '@wordpress/icons';
/**
* Internal dependencies
*/
import { CircularOptionPickerContext } from './circular-option-picker-context';
import Button from '../button';
import { Composite } from '../composite';
import Tooltip from '../tooltip';
import type { OptionProps } from './types';
function UnforwardedOptionAsButton(
props: {
id?: string;
className?: string;
isPressed?: boolean;
},
forwardedRef: ForwardedRef< any >
) {
const { isPressed, ...additionalProps } = props;
return (
<Button
{ ...additionalProps }
aria-pressed={ isPressed }
ref={ forwardedRef }
/>
);
}
const OptionAsButton = forwardRef( UnforwardedOptionAsButton );
function UnforwardedOptionAsOption(
props: {
id: string;
className?: string;
isSelected?: boolean;
},
forwardedRef: ForwardedRef< any >
) {
const { id, isSelected, ...additionalProps } = props;
const { setActiveId, activeId } = useContext( CircularOptionPickerContext );
useEffect( () => {
if ( isSelected && ! activeId ) {
// The setTimeout call is necessary to make sure that this update
// doesn't get overridden by `Composite`'s internal logic, which picks
// an initial active item if one is not specifically set.
window.setTimeout( () => setActiveId?.( id ), 0 );
}
}, [ isSelected, setActiveId, activeId, id ] );
return (
<Composite.Item
render={
<Button
{ ...additionalProps }
role="option"
aria-selected={ !! isSelected }
ref={ forwardedRef }
/>
}
id={ id }
/>
);
}
const OptionAsOption = forwardRef( UnforwardedOptionAsOption );
export function Option( {
className,
isSelected,
selectedIconProps = {},
tooltipText,
...additionalProps
}: OptionProps ) {
const { baseId, setActiveId } = useContext( CircularOptionPickerContext );
const id = useInstanceId(
Option,
baseId || 'components-circular-option-picker__option'
);
const commonProps = {
id,
className: 'components-circular-option-picker__option',
...additionalProps,
};
const isListbox = setActiveId !== undefined;
const optionControl = isListbox ? (
<OptionAsOption { ...commonProps } isSelected={ isSelected } />
) : (
<OptionAsButton { ...commonProps } isPressed={ isSelected } />
);
return (
<div
className={ clsx(
className,
'components-circular-option-picker__option-wrapper'
) }
>
{ tooltipText ? (
<Tooltip text={ tooltipText }>{ optionControl }</Tooltip>
) : (
optionControl
) }
{ isSelected && <Icon icon={ check } { ...selectedIconProps } /> }
</div>
);
}