@react-spectrum/s2 3.0.0-nightly-b0f156972-241202 → 3.0.0-nightly-e14088a7e-241204

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,321 @@
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
+
13
+ import {
14
+ PopoverProps as AriaPopoverProps,
15
+ Select as AriaSelect,
16
+ SelectProps as AriaSelectProps,
17
+ Button,
18
+ ContextValue,
19
+ DEFAULT_SLOT,
20
+ ListBox,
21
+ ListBoxItem,
22
+ ListBoxItemProps,
23
+ ListBoxProps,
24
+ Provider,
25
+ SelectValue
26
+ } from 'react-aria-components';
27
+ import {centerBaseline} from './CenterBaseline';
28
+ import {
29
+ checkmark,
30
+ description,
31
+ icon,
32
+ iconCenterWrapper,
33
+ label,
34
+ menuitem,
35
+ sectionHeader,
36
+ sectionHeading
37
+ } from './Menu';
38
+ import CheckmarkIcon from '../ui-icons/Checkmark';
39
+ import ChevronIcon from '../ui-icons/Chevron';
40
+ import {edgeToText, focusRing, style} from '../style' with {type: 'macro'};
41
+ import {fieldInput, StyleProps} from './style-utils' with {type: 'macro'};
42
+ import {
43
+ FieldLabel
44
+ } from './Field';
45
+ import {FocusableRef, FocusableRefValue, SpectrumLabelableProps} from '@react-types/shared';
46
+ import {forwardRefType} from './types';
47
+ import {HeaderContext, HeadingContext, Text, TextContext} from './Content';
48
+ import {IconContext} from './Icon';
49
+ // @ts-ignore
50
+ import intlMessages from '../intl/*.json';
51
+ import {Placement} from 'react-aria';
52
+ import {PopoverBase} from './Popover';
53
+ import {pressScale} from './pressScale';
54
+ import {raw} from '../style/style-macro' with {type: 'macro'};
55
+ import React, {createContext, forwardRef, ReactNode, useContext, useRef} from 'react';
56
+ import {useFocusableRef} from '@react-spectrum/utils';
57
+ import {useFormProps} from './Form';
58
+ import {useLocalizedStringFormatter} from '@react-aria/i18n';
59
+ import {useSpectrumContextProps} from './useSpectrumContextProps';
60
+
61
+
62
+ export interface PickerStyleProps {
63
+ }
64
+
65
+ export interface PickerProps<T extends object> extends
66
+ Omit<AriaSelectProps<T>, 'children' | 'style' | 'className'>,
67
+ PickerStyleProps,
68
+ StyleProps,
69
+ SpectrumLabelableProps,
70
+ Pick<ListBoxProps<T>, 'items'>,
71
+ Pick<AriaPopoverProps, 'shouldFlip'> {
72
+ /** The contents of the collection. */
73
+ children: ReactNode | ((item: T) => ReactNode),
74
+ /**
75
+ * Direction the menu will render relative to the Picker.
76
+ *
77
+ * @default 'bottom'
78
+ */
79
+ direction?: 'bottom' | 'top',
80
+ /**
81
+ * Alignment of the menu relative to the input target.
82
+ *
83
+ * @default 'start'
84
+ */
85
+ align?: 'start' | 'end',
86
+ /** 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. */
87
+ menuWidth?: number,
88
+ /** Density of the tabs, affects the height of the picker. */
89
+ density: 'compact' | 'regular'
90
+ }
91
+
92
+ export const PickerContext = createContext<ContextValue<Partial<PickerProps<any>>, FocusableRefValue<HTMLButtonElement>>>(null);
93
+
94
+ const inputButton = style({
95
+ ...focusRing(),
96
+ ...fieldInput(),
97
+ outlineStyle: {
98
+ default: 'none',
99
+ isFocusVisible: 'solid'
100
+ },
101
+ position: 'relative',
102
+ font: 'ui',
103
+ display: 'flex',
104
+ textAlign: 'start',
105
+ borderStyle: 'none',
106
+ borderRadius: 'sm',
107
+ alignItems: 'center',
108
+ transition: 'default',
109
+ columnGap: 'text-to-visual',
110
+ paddingX: 0,
111
+ backgroundColor: 'transparent',
112
+ color: {
113
+ default: 'neutral',
114
+ isDisabled: 'disabled'
115
+ },
116
+ maxWidth: {
117
+ isQuiet: 'max'
118
+ },
119
+ disableTapHighlight: true,
120
+ height: {
121
+ default: 48,
122
+ density: {
123
+ compact: 32
124
+ }
125
+ },
126
+ boxSizing: 'border-box'
127
+ });
128
+
129
+ export let menu = style({
130
+ outlineStyle: 'none',
131
+ display: 'grid',
132
+ gridTemplateColumns: [edgeToText(32), 'auto', 'auto', 'minmax(0, 1fr)', 'auto', 'auto', 'auto', edgeToText(32)],
133
+ boxSizing: 'border-box',
134
+ maxHeight: '[inherit]',
135
+ overflow: 'auto',
136
+ padding: 8,
137
+ fontFamily: 'sans',
138
+ fontSize: 'control'
139
+ });
140
+
141
+ const valueStyles = style({
142
+ flexGrow: 0,
143
+ truncate: true,
144
+ display: 'flex',
145
+ alignItems: 'center',
146
+ height: 'full'
147
+ });
148
+
149
+ const iconStyles = style({
150
+ flexShrink: 0,
151
+ rotate: 90,
152
+ '--iconPrimary': {
153
+ type: 'fill',
154
+ value: 'currentColor'
155
+ }
156
+ });
157
+
158
+ let InsideSelectValueContext = createContext(false);
159
+
160
+ function Picker<T extends object>(props: PickerProps<T>, ref: FocusableRef<HTMLButtonElement>) {
161
+ let stringFormatter = useLocalizedStringFormatter(intlMessages, '@react-spectrum/s2');
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
+ placeholder = stringFormatter.format('picker.placeholder'),
172
+ density,
173
+ ...pickerProps
174
+ } = props;
175
+ let isQuiet = true;
176
+
177
+ const menuOffset: number = 6;
178
+ const size = 'M';
179
+
180
+ return (
181
+ <AriaSelect
182
+ {...pickerProps}
183
+ placeholder={placeholder}>
184
+ {({isOpen}) => (
185
+ <>
186
+ <FieldLabel isQuiet={isQuiet} />
187
+ <Button
188
+ ref={domRef}
189
+ style={renderProps => pressScale(domRef)(renderProps)}
190
+ // Prevent press scale from sticking while Picker is open.
191
+ // @ts-ignore
192
+ isPressed={false}
193
+ className={renderProps => inputButton({
194
+ ...renderProps,
195
+ size: 'M',
196
+ isOpen,
197
+ isQuiet,
198
+ density
199
+ })}>
200
+ <SelectValue className={valueStyles + ' ' + raw('&> * {display: none;}')}>
201
+ {({defaultChildren}) => {
202
+ return (
203
+ <Provider
204
+ values={[
205
+ [IconContext, {
206
+ slots: {
207
+ icon: {
208
+ render: centerBaseline({slot: 'icon', styles: iconCenterWrapper}),
209
+ styles: icon
210
+ }
211
+ }
212
+ }],
213
+ [TextContext, {
214
+ slots: {
215
+ // Default slot is useful when converting other collections to PickerItems.
216
+ [DEFAULT_SLOT]: {styles: style({
217
+ display: 'block',
218
+ flexGrow: 1,
219
+ truncate: true
220
+ })}
221
+ }
222
+ }],
223
+ [InsideSelectValueContext, true]
224
+ ]}>
225
+ {defaultChildren}
226
+ </Provider>
227
+ );
228
+ }}
229
+ </SelectValue>
230
+ <ChevronIcon
231
+ size={size}
232
+ className={iconStyles} />
233
+ </Button>
234
+ <PopoverBase
235
+ hideArrow
236
+ offset={menuOffset}
237
+ placement={`${direction} ${align}` as Placement}
238
+ shouldFlip={shouldFlip}
239
+ styles={style({
240
+ marginStart: -12,
241
+ minWidth: 192,
242
+ width: '[calc(var(--trigger-width) + (-2 * self(marginStart)))]'
243
+ })}>
244
+ <Provider
245
+ values={[
246
+ [HeaderContext, {styles: sectionHeader({size})}],
247
+ [HeadingContext, {styles: sectionHeading}],
248
+ [TextContext, {
249
+ slots: {
250
+ description: {styles: description({size})}
251
+ }
252
+ }]
253
+ ]}>
254
+ <ListBox
255
+ items={items}
256
+ className={menu}>
257
+ {children}
258
+ </ListBox>
259
+ </Provider>
260
+ </PopoverBase>
261
+ </>
262
+ )}
263
+ </AriaSelect>
264
+ );
265
+ }
266
+
267
+ /**
268
+ * Pickers allow users to choose a single option from a collapsible list of options when space is limited.
269
+ */
270
+ let _Picker = /*#__PURE__*/ (forwardRef as forwardRefType)(Picker);
271
+ export {_Picker as Picker};
272
+
273
+ export interface PickerItemProps extends Omit<ListBoxItemProps, 'children' | 'style' | 'className'>, StyleProps {
274
+ children: ReactNode
275
+ }
276
+
277
+ export function PickerItem(props: PickerItemProps) {
278
+ let ref = useRef(null);
279
+ let isLink = props.href != null;
280
+ const size = 'M';
281
+ return (
282
+ <ListBoxItem
283
+ {...props}
284
+ ref={ref}
285
+ textValue={props.textValue || (typeof props.children === 'string' ? props.children as string : undefined)}
286
+ style={pressScale(ref, props.UNSAFE_style)}
287
+ className={renderProps => (props.UNSAFE_className || '') + menuitem({...renderProps, size, isLink}, props.styles)}>
288
+ {(renderProps) => {
289
+ let {children} = props;
290
+ return (
291
+ <DefaultProvider
292
+ context={IconContext}
293
+ value={{slots: {
294
+ icon: {render: centerBaseline({slot: 'icon', styles: iconCenterWrapper}), styles: icon}
295
+ }}}>
296
+ <DefaultProvider
297
+ context={TextContext}
298
+ value={{
299
+ slots: {
300
+ [DEFAULT_SLOT]: {styles: label({size})}
301
+ }
302
+ }}>
303
+ {!isLink && <CheckmarkIcon size={size} className={checkmark({...renderProps, size})} />}
304
+ {typeof children === 'string' ? <Text>{children}</Text> : children}
305
+ </DefaultProvider>
306
+ </DefaultProvider>
307
+ );
308
+ }}
309
+ </ListBoxItem>
310
+ );
311
+ }
312
+
313
+ // A Context.Provider that only sets a value if not inside SelectValue.
314
+ function DefaultProvider({context, value, children}: {context: React.Context<any>, value: any, children: any}) {
315
+ let inSelectValue = useContext(InsideSelectValueContext);
316
+ if (inSelectValue) {
317
+ return children;
318
+ }
319
+
320
+ return <context.Provider value={value}>{children}</context.Provider>;
321
+ }