@react-spectrum/s2 3.0.0-nightly-e3ed3c7f6-250130 → 3.0.0-nightly-016590a4a-250131

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,350 @@
1
+ /*
2
+ * Copyright 2024 Adobe. All rights reserved.
3
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
+ * you may not use this file except in compliance with the License. You may obtain a copy
5
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+ *
7
+ * Unless required by applicable law or agreed to in writing, software distributed under
8
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
+ * OF ANY KIND, either express or implied. See the License for the specific language
10
+ * governing permissions and limitations under the License.
11
+ */
12
+ import {
13
+ PopoverProps as AriaPopoverProps,
14
+ Select as AriaSelect,
15
+ SelectProps as AriaSelectProps,
16
+ Button,
17
+ ContextValue,
18
+ DEFAULT_SLOT,
19
+ ListBox,
20
+ ListBoxItem,
21
+ ListBoxItemProps,
22
+ ListBoxProps,
23
+ Provider,
24
+ SelectValue
25
+ } from 'react-aria-components';
26
+ import {centerBaseline} from './CenterBaseline';
27
+ import {
28
+ checkmark,
29
+ description,
30
+ icon,
31
+ label,
32
+ menuitem,
33
+ sectionHeader,
34
+ sectionHeading
35
+ } from './Menu';
36
+ import CheckmarkIcon from '../ui-icons/Checkmark';
37
+ import ChevronIcon from '../ui-icons/Chevron';
38
+ import {edgeToText, focusRing, size, style} from '../style' with {type: 'macro'};
39
+ import {fieldInput, StyleProps} from './style-utils' with {type: 'macro'};
40
+ import {
41
+ FieldLabel
42
+ } from './Field';
43
+ import {FocusableRef, FocusableRefValue, SpectrumLabelableProps} from '@react-types/shared';
44
+ import {forwardRefType} from './types';
45
+ import {HeaderContext, HeadingContext, Text, TextContext} from './Content';
46
+ import {IconContext} from './Icon';
47
+ import {Placement} from 'react-aria';
48
+ import {PopoverBase} from './Popover';
49
+ import {pressScale} from './pressScale';
50
+ import {raw} from '../style/style-macro' with {type: 'macro'};
51
+ import React, {createContext, forwardRef, ReactNode, useContext, useRef} from 'react';
52
+ import {useFocusableRef} from '@react-spectrum/utils';
53
+ import {useFormProps} from './Form';
54
+ import {useSpectrumContextProps} from './useSpectrumContextProps';
55
+ export interface PickerStyleProps {
56
+ }
57
+ export interface PickerProps<T extends object> extends
58
+ Omit<AriaSelectProps<T>, 'children' | 'style' | 'className' | 'placeholder'>,
59
+ PickerStyleProps,
60
+ StyleProps,
61
+ SpectrumLabelableProps,
62
+ Pick<ListBoxProps<T>, 'items'>,
63
+ Pick<AriaPopoverProps, 'shouldFlip'> {
64
+ /** The contents of the collection. */
65
+ children: ReactNode | ((item: T) => ReactNode),
66
+ /**
67
+ * Direction the menu will render relative to the Picker.
68
+ *
69
+ * @default 'bottom'
70
+ */
71
+ direction?: 'bottom' | 'top',
72
+ /**
73
+ * Alignment of the menu relative to the input target.
74
+ *
75
+ * @default 'start'
76
+ */
77
+ align?: 'start' | 'end',
78
+ /** Width of the menu. By default, matches width of the trigger. Note that the minimum width of the dropdown is always equal to the trigger's width. */
79
+ menuWidth?: number,
80
+ /** Density of the tabs, affects the height of the picker. */
81
+ density: 'compact' | 'regular',
82
+ /**
83
+ * If the tab picker should only display icon and no text for the button label.
84
+ * @default 'show
85
+ */
86
+ labelBehavior?: 'show' | 'hide',
87
+ /** Id for the SelectedValue so we can label using it. */
88
+ valueId?: string
89
+ }
90
+ export const PickerContext = createContext<ContextValue<Partial<PickerProps<any>>, FocusableRefValue<HTMLButtonElement>>>(null);
91
+ const inputButton = style({
92
+ ...focusRing(),
93
+ ...fieldInput(),
94
+ outlineStyle: {
95
+ default: 'none',
96
+ isFocusVisible: 'solid'
97
+ },
98
+ position: 'relative',
99
+ font: 'ui',
100
+ display: 'flex',
101
+ textAlign: 'start',
102
+ borderStyle: 'none',
103
+ borderRadius: 'sm',
104
+ alignItems: 'center',
105
+ transition: 'default',
106
+ columnGap: 'text-to-visual',
107
+ paddingX: 0,
108
+ backgroundColor: 'transparent',
109
+ color: {
110
+ default: 'neutral',
111
+ isDisabled: 'disabled'
112
+ },
113
+ maxWidth: {
114
+ isQuiet: 'max'
115
+ },
116
+ disableTapHighlight: true,
117
+ height: {
118
+ default: 48,
119
+ density: {
120
+ compact: 32
121
+ }
122
+ },
123
+ boxSizing: 'border-box'
124
+ });
125
+ export let menu = style({
126
+ outlineStyle: 'none',
127
+ display: 'grid',
128
+ gridTemplateColumns: [edgeToText(32), 'auto', 'auto', 'minmax(0, 1fr)', 'auto', 'auto', 'auto', edgeToText(32)],
129
+ boxSizing: 'border-box',
130
+ maxHeight: '[inherit]',
131
+ overflow: 'auto',
132
+ padding: 8,
133
+ fontFamily: 'sans',
134
+ fontSize: 'control'
135
+ });
136
+ const valueStyles = style({
137
+ flexGrow: 0,
138
+ truncate: true,
139
+ display: 'flex',
140
+ alignItems: 'center',
141
+ height: 'full'
142
+ });
143
+ const iconStyles = style({
144
+ flexShrink: 0,
145
+ rotate: 90,
146
+ '--iconPrimary': {
147
+ type: 'fill',
148
+ value: 'currentColor'
149
+ }
150
+ });
151
+ const iconCenterWrapper = style({
152
+ display: 'flex',
153
+ gridArea: 'icon',
154
+ paddingStart: {
155
+ labelBehavior: {
156
+ hide: size(6)
157
+ }
158
+ }
159
+ });
160
+ let InsideSelectValueContext = createContext(false);
161
+ function Picker<T extends object>(props: PickerProps<T>, ref: FocusableRef<HTMLButtonElement>) {
162
+ [props, ref] = useSpectrumContextProps(props, ref, PickerContext);
163
+ let domRef = useFocusableRef(ref);
164
+ props = useFormProps(props);
165
+ let {
166
+ direction = 'bottom',
167
+ align = 'start',
168
+ shouldFlip = true,
169
+ children,
170
+ items,
171
+ density,
172
+ labelBehavior = 'show',
173
+ valueId,
174
+ ...pickerProps
175
+ } = props;
176
+ let isQuiet = true;
177
+ const menuOffset: number = 6;
178
+ const size = 'M';
179
+ let ariaLabelledby = props['aria-labelledby'] ?? '';
180
+ return (
181
+ <div>
182
+ <AriaSelect
183
+ {...pickerProps}
184
+ aria-labelledby={`${labelBehavior === 'hide' ? valueId : ''} ${ariaLabelledby}`}>
185
+ {({isOpen}) => (
186
+ <>
187
+ <FieldLabel isQuiet={isQuiet} />
188
+ <Button
189
+ ref={domRef}
190
+ style={renderProps => pressScale(domRef)(renderProps)}
191
+ // Prevent press scale from sticking while Picker is open.
192
+ // @ts-ignore
193
+ isPressed={false}
194
+ className={renderProps => inputButton({
195
+ ...renderProps,
196
+ size: 'M',
197
+ isOpen,
198
+ isQuiet,
199
+ density
200
+ })}>
201
+ <SelectValue className={valueStyles + ' ' + raw('&> * {display: none;}')}>
202
+ {({defaultChildren}) => {
203
+ return (
204
+ <Provider
205
+ values={[
206
+ [IconContext, {
207
+ slots: {
208
+ icon: {
209
+ render: centerBaseline({slot: 'icon', styles: iconCenterWrapper({labelBehavior})}),
210
+ styles: icon
211
+ }
212
+ }
213
+ }],
214
+ [TextContext, {
215
+ slots: {
216
+ // Default slot is useful when converting other collections to PickerItems.
217
+ [DEFAULT_SLOT]: {
218
+ id: valueId,
219
+ styles: style({
220
+ display: {
221
+ default: 'block',
222
+ labelBehavior: {
223
+ hide: 'none'
224
+ }
225
+ },
226
+ flexGrow: 1,
227
+ truncate: true
228
+ })({labelBehavior})
229
+ }
230
+ }
231
+ }],
232
+ [InsideSelectValueContext, true]
233
+ ]}>
234
+ {defaultChildren}
235
+ </Provider>
236
+ );
237
+ }}
238
+ </SelectValue>
239
+ <ChevronIcon
240
+ size={size}
241
+ className={iconStyles} />
242
+ </Button>
243
+ <PopoverBase
244
+ hideArrow
245
+ offset={menuOffset}
246
+ placement={`${direction} ${align}` as Placement}
247
+ shouldFlip={shouldFlip}
248
+ styles={style({
249
+ marginStart: -12,
250
+ minWidth: 192,
251
+ width: '[calc(var(--trigger-width) + (-2 * self(marginStart)))]'
252
+ })}>
253
+ <Provider
254
+ values={[
255
+ [HeaderContext, {styles: sectionHeader({size})}],
256
+ [HeadingContext, {styles: sectionHeading}],
257
+ [TextContext, {
258
+ slots: {
259
+ description: {styles: description({size})}
260
+ }
261
+ }]
262
+ ]}>
263
+ <ListBox
264
+ items={items}
265
+ className={menu}>
266
+ {children}
267
+ </ListBox>
268
+ </Provider>
269
+ </PopoverBase>
270
+ </>
271
+ )}
272
+ </AriaSelect>
273
+ <TabLine isDisabled={props.isDisabled} />
274
+ </div>
275
+ );
276
+ }
277
+ /**
278
+ * Pickers allow users to choose a single option from a collapsible list of options when space is limited.
279
+ */
280
+ let _Picker = /*#__PURE__*/ (forwardRef as forwardRefType)(Picker);
281
+ export {_Picker as Picker};
282
+
283
+
284
+ const selectedIndicator = style({
285
+ backgroundColor: {
286
+ default: 'neutral',
287
+ isDisabled: 'disabled',
288
+ forcedColors: {
289
+ default: 'Highlight',
290
+ isDisabled: 'GrayText'
291
+ }
292
+ },
293
+ height: '[2px]',
294
+ borderStyle: 'none',
295
+ borderRadius: 'full',
296
+ marginTop: '[-2px]',
297
+ transitionDuration: 130,
298
+ transitionTimingFunction: 'in-out'
299
+ });
300
+ function TabLine(props) {
301
+ return <div className={selectedIndicator(props)} />;
302
+ }
303
+
304
+
305
+ export interface PickerItemProps extends Omit<ListBoxItemProps, 'children' | 'style' | 'className'>, StyleProps {
306
+ children: ReactNode
307
+ }
308
+ export function PickerItem(props: PickerItemProps) {
309
+ let ref = useRef(null);
310
+ let isLink = props.href != null;
311
+ const size = 'M';
312
+ return (
313
+ <ListBoxItem
314
+ {...props}
315
+ ref={ref}
316
+ textValue={props.textValue || (typeof props.children === 'string' ? props.children as string : undefined)}
317
+ style={pressScale(ref, props.UNSAFE_style)}
318
+ className={renderProps => (props.UNSAFE_className || '') + menuitem({...renderProps, size, isLink}, props.styles)}>
319
+ {(renderProps) => {
320
+ let {children} = props;
321
+ return (
322
+ <DefaultProvider
323
+ context={IconContext}
324
+ value={{slots: {
325
+ icon: {render: centerBaseline({slot: 'icon', styles: iconCenterWrapper({})}), styles: icon}
326
+ }}}>
327
+ <DefaultProvider
328
+ context={TextContext}
329
+ value={{
330
+ slots: {
331
+ [DEFAULT_SLOT]: {styles: label({size})}
332
+ }
333
+ }}>
334
+ {!isLink && <CheckmarkIcon size={size} className={checkmark({...renderProps, size})} />}
335
+ {typeof children === 'string' ? <Text>{children}</Text> : children}
336
+ </DefaultProvider>
337
+ </DefaultProvider>
338
+ );
339
+ }}
340
+ </ListBoxItem>
341
+ );
342
+ }
343
+ // A Context.Provider that only sets a value if not inside SelectValue.
344
+ function DefaultProvider({context, value, children}: {context: React.Context<any>, value: any, children: any}) {
345
+ let inSelectValue = useContext(InsideSelectValueContext);
346
+ if (inSelectValue) {
347
+ return children;
348
+ }
349
+ return <context.Provider value={value}>{children}</context.Provider>;
350
+ }