@tamagui/select 1.88.13 → 1.89.0-1706308641099
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.
- package/LICENSE +21 -0
- package/dist/esm/BubbleSelect.mjs +23 -0
- package/dist/esm/Select.mjs +359 -0
- package/dist/esm/SelectContent.mjs +47 -0
- package/dist/esm/SelectImpl.mjs +233 -0
- package/dist/esm/SelectItem.mjs +121 -0
- package/dist/esm/SelectItemText.mjs +52 -0
- package/dist/esm/SelectScrollButton.mjs +100 -0
- package/dist/esm/SelectTrigger.mjs +53 -0
- package/dist/esm/SelectViewport.mjs +117 -0
- package/dist/esm/constants.mjs +8 -0
- package/dist/esm/context.mjs +23 -0
- package/dist/esm/index.mjs +2 -0
- package/dist/esm/types.mjs +0 -0
- package/dist/esm/useSelectBreakpointActive.mjs +10 -0
- package/dist/jsx/BubbleSelect.mjs +23 -0
- package/dist/jsx/Select.mjs +359 -0
- package/dist/jsx/SelectContent.mjs +47 -0
- package/dist/jsx/SelectImpl.mjs +233 -0
- package/dist/jsx/SelectItem.mjs +121 -0
- package/dist/jsx/SelectItemText.mjs +52 -0
- package/dist/jsx/SelectScrollButton.mjs +100 -0
- package/dist/jsx/SelectTrigger.mjs +53 -0
- package/dist/jsx/SelectViewport.mjs +117 -0
- package/dist/jsx/constants.mjs +8 -0
- package/dist/jsx/context.mjs +23 -0
- package/dist/jsx/index.mjs +2 -0
- package/dist/jsx/types.mjs +0 -0
- package/dist/jsx/useSelectBreakpointActive.mjs +10 -0
- package/package.json +23 -23
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020 Nate Wienert
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { useComposedRefs } from "@tamagui/compose-refs";
|
|
2
|
+
import { usePrevious } from "@tamagui/use-previous";
|
|
3
|
+
import * as React from "react";
|
|
4
|
+
const BubbleSelect = React.forwardRef((props, forwardedRef) => {
|
|
5
|
+
const {
|
|
6
|
+
value,
|
|
7
|
+
...selectProps
|
|
8
|
+
} = props,
|
|
9
|
+
ref = React.useRef(null),
|
|
10
|
+
composedRefs = useComposedRefs(forwardedRef, ref),
|
|
11
|
+
prevValue = usePrevious(value);
|
|
12
|
+
return React.useEffect(() => {
|
|
13
|
+
const select = ref.current,
|
|
14
|
+
selectProto = window.HTMLSelectElement.prototype,
|
|
15
|
+
setValue = Object.getOwnPropertyDescriptor(selectProto, "value").set;
|
|
16
|
+
if (prevValue !== value && setValue) {
|
|
17
|
+
const event = new Event("change", {
|
|
18
|
+
bubbles: !0
|
|
19
|
+
});
|
|
20
|
+
setValue.call(select, value), select.dispatchEvent(event);
|
|
21
|
+
}
|
|
22
|
+
}, [prevValue, value]), null;
|
|
23
|
+
});
|
|
@@ -0,0 +1,359 @@
|
|
|
1
|
+
import { Adapt, useAdaptParent } from "@tamagui/adapt";
|
|
2
|
+
import { useComposedRefs } from "@tamagui/compose-refs";
|
|
3
|
+
import { isWeb, useIsomorphicLayoutEffect } from "@tamagui/constants";
|
|
4
|
+
import { getVariableValue, styled, useEvent, useGet } from "@tamagui/core";
|
|
5
|
+
import { getSpace } from "@tamagui/get-token";
|
|
6
|
+
import { withStaticProperties } from "@tamagui/helpers";
|
|
7
|
+
import { ListItem } from "@tamagui/list-item";
|
|
8
|
+
import { PortalHost } from "@tamagui/portal";
|
|
9
|
+
import { Separator } from "@tamagui/separator";
|
|
10
|
+
import { Sheet, SheetController } from "@tamagui/sheet";
|
|
11
|
+
import { ThemeableStack, XStack, YStack } from "@tamagui/stacks";
|
|
12
|
+
import { Paragraph, SizableText } from "@tamagui/text";
|
|
13
|
+
import { useControllableState } from "@tamagui/use-controllable-state";
|
|
14
|
+
import * as React from "react";
|
|
15
|
+
import { useDebounce } from "@tamagui/use-debounce";
|
|
16
|
+
import { SELECT_NAME } from "./constants.mjs";
|
|
17
|
+
import { SelectItemParentProvider, SelectProvider, createSelectContext, useSelectContext, useSelectItemParentContext } from "./context.mjs";
|
|
18
|
+
import { SelectContent } from "./SelectContent.mjs";
|
|
19
|
+
import { SelectInlineImpl } from "./SelectImpl.mjs";
|
|
20
|
+
import { SelectItem, useSelectItemContext } from "./SelectItem.mjs";
|
|
21
|
+
import { ITEM_TEXT_NAME, SelectItemText } from "./SelectItemText.mjs";
|
|
22
|
+
import { SelectScrollDownButton, SelectScrollUpButton } from "./SelectScrollButton.mjs";
|
|
23
|
+
import { SelectTrigger } from "./SelectTrigger.mjs";
|
|
24
|
+
import { SelectViewport } from "./SelectViewport.mjs";
|
|
25
|
+
import { useSelectBreakpointActive, useShowSelectSheet } from "./useSelectBreakpointActive.mjs";
|
|
26
|
+
import { Fragment, jsx } from "react/jsx-runtime";
|
|
27
|
+
const VALUE_NAME = "SelectValue",
|
|
28
|
+
SelectValueFrame = styled(SizableText, {
|
|
29
|
+
name: VALUE_NAME,
|
|
30
|
+
userSelect: "none"
|
|
31
|
+
}),
|
|
32
|
+
SelectValue = SelectValueFrame.styleable(function ({
|
|
33
|
+
__scopeSelect,
|
|
34
|
+
children: childrenProp,
|
|
35
|
+
placeholder,
|
|
36
|
+
...props
|
|
37
|
+
}, forwardedRef) {
|
|
38
|
+
const context = useSelectContext(VALUE_NAME, __scopeSelect),
|
|
39
|
+
itemParentContext = useSelectItemParentContext(VALUE_NAME, __scopeSelect),
|
|
40
|
+
composedRefs = useComposedRefs(forwardedRef, context.onValueNodeChange),
|
|
41
|
+
children = childrenProp ?? context.selectedItem,
|
|
42
|
+
selectValueChildren = context.value == null || context.value === "" ? placeholder ?? children : children;
|
|
43
|
+
return /* @__PURE__ */jsx(SelectValueFrame, {
|
|
44
|
+
...(!props.unstyled && {
|
|
45
|
+
size: itemParentContext.size,
|
|
46
|
+
ellipse: !0,
|
|
47
|
+
// we don't want events from the portalled `SelectValue` children to bubble
|
|
48
|
+
// through the item they came from
|
|
49
|
+
pointerEvents: "none"
|
|
50
|
+
}),
|
|
51
|
+
ref: composedRefs,
|
|
52
|
+
...props,
|
|
53
|
+
children: unwrapSelectItem(selectValueChildren)
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
function unwrapSelectItem(selectValueChildren) {
|
|
57
|
+
return React.Children.map(selectValueChildren, child => {
|
|
58
|
+
if (child) {
|
|
59
|
+
if (child.type?.staticConfig?.componentName === ITEM_TEXT_NAME) return child.props.children;
|
|
60
|
+
if (child.props?.children) return unwrapSelectItem(child.props.children);
|
|
61
|
+
}
|
|
62
|
+
return child;
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
const SelectIcon = styled(XStack, {
|
|
66
|
+
name: "SelectIcon",
|
|
67
|
+
// @ts-ignore
|
|
68
|
+
"aria-hidden": !0,
|
|
69
|
+
children: /* @__PURE__ */jsx(Paragraph, {
|
|
70
|
+
children: "\u25BC"
|
|
71
|
+
})
|
|
72
|
+
}),
|
|
73
|
+
ITEM_INDICATOR_NAME = "SelectItemIndicator",
|
|
74
|
+
SelectItemIndicatorFrame = styled(XStack, {
|
|
75
|
+
name: ITEM_TEXT_NAME
|
|
76
|
+
}),
|
|
77
|
+
SelectItemIndicator = React.forwardRef((props, forwardedRef) => {
|
|
78
|
+
const {
|
|
79
|
+
__scopeSelect,
|
|
80
|
+
...itemIndicatorProps
|
|
81
|
+
} = props,
|
|
82
|
+
context = useSelectItemParentContext(ITEM_INDICATOR_NAME, __scopeSelect),
|
|
83
|
+
itemContext = useSelectItemContext(ITEM_INDICATOR_NAME, __scopeSelect);
|
|
84
|
+
return context.shouldRenderWebNative ? null : itemContext.isSelected ? /* @__PURE__ */jsx(SelectItemIndicatorFrame, {
|
|
85
|
+
"aria-hidden": !0,
|
|
86
|
+
...itemIndicatorProps,
|
|
87
|
+
ref: forwardedRef
|
|
88
|
+
}) : null;
|
|
89
|
+
});
|
|
90
|
+
SelectItemIndicator.displayName = ITEM_INDICATOR_NAME;
|
|
91
|
+
const GROUP_NAME = "SelectGroup",
|
|
92
|
+
[SelectGroupContextProvider, useSelectGroupContext] = createSelectContext(GROUP_NAME),
|
|
93
|
+
SelectGroupFrame = styled(YStack, {
|
|
94
|
+
name: GROUP_NAME,
|
|
95
|
+
width: "100%"
|
|
96
|
+
}),
|
|
97
|
+
NativeSelectTextFrame = styled(SizableText, {
|
|
98
|
+
tag: "select",
|
|
99
|
+
backgroundColor: "$background",
|
|
100
|
+
borderColor: "$borderColor",
|
|
101
|
+
hoverStyle: {
|
|
102
|
+
backgroundColor: "$backgroundHover"
|
|
103
|
+
}
|
|
104
|
+
}),
|
|
105
|
+
NativeSelectFrame = styled(ThemeableStack, {
|
|
106
|
+
name: "NativeSelect",
|
|
107
|
+
bordered: !0,
|
|
108
|
+
userSelect: "none",
|
|
109
|
+
outlineWidth: 0,
|
|
110
|
+
paddingRight: 10,
|
|
111
|
+
variants: {
|
|
112
|
+
size: {
|
|
113
|
+
"...size": (val, extras) => {
|
|
114
|
+
const {
|
|
115
|
+
tokens
|
|
116
|
+
} = extras,
|
|
117
|
+
paddingHorizontal = getVariableValue(tokens.space[val]);
|
|
118
|
+
return {
|
|
119
|
+
borderRadius: tokens.radius[val] ?? val,
|
|
120
|
+
minHeight: tokens.size[val],
|
|
121
|
+
paddingRight: paddingHorizontal + 20,
|
|
122
|
+
paddingLeft: paddingHorizontal,
|
|
123
|
+
paddingVertical: getSpace(val, {
|
|
124
|
+
shift: -3
|
|
125
|
+
})
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
},
|
|
130
|
+
defaultVariants: {
|
|
131
|
+
size: "$2"
|
|
132
|
+
}
|
|
133
|
+
}),
|
|
134
|
+
SelectGroup = React.forwardRef((props, forwardedRef) => {
|
|
135
|
+
const {
|
|
136
|
+
__scopeSelect,
|
|
137
|
+
...groupProps
|
|
138
|
+
} = props,
|
|
139
|
+
groupId = React.useId(),
|
|
140
|
+
context = useSelectContext(GROUP_NAME, __scopeSelect),
|
|
141
|
+
itemParentContext = useSelectItemParentContext(GROUP_NAME, __scopeSelect),
|
|
142
|
+
size = itemParentContext.size ?? "$true",
|
|
143
|
+
nativeSelectRef = React.useRef(null),
|
|
144
|
+
content = itemParentContext.shouldRenderWebNative ?
|
|
145
|
+
// @ts-expect-error until we support typing based on tag
|
|
146
|
+
/* @__PURE__ */
|
|
147
|
+
jsx(NativeSelectFrame, {
|
|
148
|
+
asChild: !0,
|
|
149
|
+
size,
|
|
150
|
+
value: context.value,
|
|
151
|
+
children: /* @__PURE__ */jsx(NativeSelectTextFrame, {
|
|
152
|
+
onChange: event => {
|
|
153
|
+
itemParentContext.onChange(event.currentTarget.value);
|
|
154
|
+
},
|
|
155
|
+
size,
|
|
156
|
+
ref: nativeSelectRef,
|
|
157
|
+
style: {
|
|
158
|
+
color: "var(--color)",
|
|
159
|
+
// @ts-ignore
|
|
160
|
+
appearance: "none"
|
|
161
|
+
},
|
|
162
|
+
children: props.children
|
|
163
|
+
})
|
|
164
|
+
}) : /* @__PURE__ */jsx(SelectGroupFrame, {
|
|
165
|
+
role: "group",
|
|
166
|
+
"aria-labelledby": groupId,
|
|
167
|
+
...groupProps,
|
|
168
|
+
ref: forwardedRef
|
|
169
|
+
});
|
|
170
|
+
return /* @__PURE__ */jsx(SelectGroupContextProvider, {
|
|
171
|
+
scope: __scopeSelect,
|
|
172
|
+
id: groupId || "",
|
|
173
|
+
children: content
|
|
174
|
+
});
|
|
175
|
+
});
|
|
176
|
+
SelectGroup.displayName = GROUP_NAME;
|
|
177
|
+
const LABEL_NAME = "SelectLabel",
|
|
178
|
+
SelectLabel = React.forwardRef((props, forwardedRef) => {
|
|
179
|
+
const {
|
|
180
|
+
__scopeSelect,
|
|
181
|
+
...labelProps
|
|
182
|
+
} = props,
|
|
183
|
+
context = useSelectItemParentContext(LABEL_NAME, __scopeSelect),
|
|
184
|
+
groupContext = useSelectGroupContext(LABEL_NAME, __scopeSelect);
|
|
185
|
+
return context.shouldRenderWebNative ? null : /* @__PURE__ */jsx(ListItem, {
|
|
186
|
+
tag: "div",
|
|
187
|
+
componentName: LABEL_NAME,
|
|
188
|
+
fontWeight: "800",
|
|
189
|
+
id: groupContext.id,
|
|
190
|
+
size: context.size,
|
|
191
|
+
...labelProps,
|
|
192
|
+
ref: forwardedRef
|
|
193
|
+
});
|
|
194
|
+
});
|
|
195
|
+
SelectLabel.displayName = LABEL_NAME;
|
|
196
|
+
const SelectSeparator = styled(Separator, {
|
|
197
|
+
name: "SelectSeparator"
|
|
198
|
+
}),
|
|
199
|
+
SelectSheetController = props => {
|
|
200
|
+
const context = useSelectContext("SelectSheetController", props.__scopeSelect),
|
|
201
|
+
showSheet = useShowSelectSheet(context),
|
|
202
|
+
breakpointActive = useSelectBreakpointActive(context.sheetBreakpoint),
|
|
203
|
+
getShowSheet = useGet(showSheet);
|
|
204
|
+
return /* @__PURE__ */jsx(SheetController, {
|
|
205
|
+
onOpenChange: val => {
|
|
206
|
+
getShowSheet() && props.onOpenChange(val);
|
|
207
|
+
},
|
|
208
|
+
open: context.open,
|
|
209
|
+
hidden: breakpointActive === !1,
|
|
210
|
+
children: props.children
|
|
211
|
+
});
|
|
212
|
+
},
|
|
213
|
+
SelectSheetImpl = props => /* @__PURE__ */jsx(Fragment, {
|
|
214
|
+
children: props.children
|
|
215
|
+
}),
|
|
216
|
+
Select = withStaticProperties(props => {
|
|
217
|
+
const {
|
|
218
|
+
__scopeSelect,
|
|
219
|
+
native,
|
|
220
|
+
children,
|
|
221
|
+
open: openProp,
|
|
222
|
+
defaultOpen,
|
|
223
|
+
onOpenChange,
|
|
224
|
+
value: valueProp,
|
|
225
|
+
defaultValue,
|
|
226
|
+
onValueChange,
|
|
227
|
+
disablePreventBodyScroll,
|
|
228
|
+
size: sizeProp = "$true",
|
|
229
|
+
onActiveChange,
|
|
230
|
+
dir
|
|
231
|
+
} = props,
|
|
232
|
+
id = React.useId(),
|
|
233
|
+
scopeKey = __scopeSelect ? Object.keys(__scopeSelect)[0] ?? id : id,
|
|
234
|
+
{
|
|
235
|
+
when,
|
|
236
|
+
AdaptProvider
|
|
237
|
+
} = useAdaptParent({
|
|
238
|
+
Contents: React.useCallback(() => /* @__PURE__ */jsx(PortalHost, {
|
|
239
|
+
name: `${scopeKey}SheetContents`
|
|
240
|
+
}), [scopeKey])
|
|
241
|
+
}),
|
|
242
|
+
sheetBreakpoint = when,
|
|
243
|
+
SelectImpl = useSelectBreakpointActive(sheetBreakpoint) || !isWeb ? SelectSheetImpl : SelectInlineImpl,
|
|
244
|
+
forceUpdate = React.useReducer(() => ({}), {})[1],
|
|
245
|
+
[selectedItem, setSelectedItem] = React.useState(null),
|
|
246
|
+
[open, setOpen] = useControllableState({
|
|
247
|
+
prop: openProp,
|
|
248
|
+
defaultProp: defaultOpen || !1,
|
|
249
|
+
onChange: onOpenChange
|
|
250
|
+
}),
|
|
251
|
+
[value, setValue] = useControllableState({
|
|
252
|
+
prop: valueProp,
|
|
253
|
+
defaultProp: defaultValue || "",
|
|
254
|
+
onChange: onValueChange,
|
|
255
|
+
transition: !0
|
|
256
|
+
});
|
|
257
|
+
React.useEffect(() => {
|
|
258
|
+
open && emitValue(value);
|
|
259
|
+
}, [open]), React.useEffect(() => {
|
|
260
|
+
emitValue(value);
|
|
261
|
+
}, [value]);
|
|
262
|
+
const [activeIndex, setActiveIndex] = React.useState(0),
|
|
263
|
+
[emitValue, valueSubscribe] = useEmitter(),
|
|
264
|
+
[emitActiveIndex, activeIndexSubscribe] = useEmitter(),
|
|
265
|
+
selectedIndexRef = React.useRef(null),
|
|
266
|
+
activeIndexRef = React.useRef(null),
|
|
267
|
+
listContentRef = React.useRef([]),
|
|
268
|
+
[selectedIndex, setSelectedIndex] = React.useState(0),
|
|
269
|
+
[valueNode, setValueNode] = React.useState(null);
|
|
270
|
+
useIsomorphicLayoutEffect(() => {
|
|
271
|
+
selectedIndexRef.current = selectedIndex, activeIndexRef.current = activeIndex;
|
|
272
|
+
});
|
|
273
|
+
const shouldRenderWebNative = isWeb && (native === !0 || native === "web" || Array.isArray(native) && native.includes("web")),
|
|
274
|
+
setActiveIndexDebounced = useDebounce(index => {
|
|
275
|
+
setActiveIndex(prev => prev !== index ? (typeof index == "number" && emitActiveIndex(index), index) : prev);
|
|
276
|
+
}, 1, {}, []);
|
|
277
|
+
return /* @__PURE__ */jsx(AdaptProvider, {
|
|
278
|
+
children: /* @__PURE__ */jsx(SelectItemParentProvider, {
|
|
279
|
+
scope: __scopeSelect,
|
|
280
|
+
initialValue: React.useMemo(() => value, []),
|
|
281
|
+
size: sizeProp,
|
|
282
|
+
activeIndexSubscribe,
|
|
283
|
+
valueSubscribe,
|
|
284
|
+
setOpen,
|
|
285
|
+
onChange: React.useCallback(val => {
|
|
286
|
+
setValue(val), emitValue(val);
|
|
287
|
+
}, []),
|
|
288
|
+
onActiveChange: useEvent((...args) => {
|
|
289
|
+
onActiveChange?.(...args);
|
|
290
|
+
}),
|
|
291
|
+
setSelectedIndex,
|
|
292
|
+
setValueAtIndex: React.useCallback((index, value2) => {
|
|
293
|
+
listContentRef.current[index] = value2;
|
|
294
|
+
}, []),
|
|
295
|
+
shouldRenderWebNative,
|
|
296
|
+
children: /* @__PURE__ */jsx(SelectProvider, {
|
|
297
|
+
scope: __scopeSelect,
|
|
298
|
+
disablePreventBodyScroll,
|
|
299
|
+
dir,
|
|
300
|
+
blockSelection: !1,
|
|
301
|
+
fallback: !1,
|
|
302
|
+
selectedItem,
|
|
303
|
+
setSelectedItem,
|
|
304
|
+
forceUpdate,
|
|
305
|
+
valueNode,
|
|
306
|
+
onValueNodeChange: setValueNode,
|
|
307
|
+
scopeKey,
|
|
308
|
+
sheetBreakpoint,
|
|
309
|
+
activeIndex,
|
|
310
|
+
selectedIndex,
|
|
311
|
+
setActiveIndex: setActiveIndexDebounced,
|
|
312
|
+
value,
|
|
313
|
+
open,
|
|
314
|
+
native,
|
|
315
|
+
children: /* @__PURE__ */jsx(SelectSheetController, {
|
|
316
|
+
onOpenChange: setOpen,
|
|
317
|
+
__scopeSelect,
|
|
318
|
+
children: shouldRenderWebNative ? children : /* @__PURE__ */jsx(SelectImpl, {
|
|
319
|
+
activeIndexRef,
|
|
320
|
+
listContentRef,
|
|
321
|
+
selectedIndexRef,
|
|
322
|
+
...props,
|
|
323
|
+
open,
|
|
324
|
+
value,
|
|
325
|
+
children
|
|
326
|
+
})
|
|
327
|
+
})
|
|
328
|
+
})
|
|
329
|
+
})
|
|
330
|
+
});
|
|
331
|
+
}, {
|
|
332
|
+
Adapt,
|
|
333
|
+
Content: SelectContent,
|
|
334
|
+
Group: SelectGroup,
|
|
335
|
+
Icon: SelectIcon,
|
|
336
|
+
Item: SelectItem,
|
|
337
|
+
ItemIndicator: SelectItemIndicator,
|
|
338
|
+
ItemText: SelectItemText,
|
|
339
|
+
Label: SelectLabel,
|
|
340
|
+
ScrollDownButton: SelectScrollDownButton,
|
|
341
|
+
ScrollUpButton: SelectScrollUpButton,
|
|
342
|
+
Trigger: SelectTrigger,
|
|
343
|
+
Value: SelectValue,
|
|
344
|
+
Viewport: SelectViewport,
|
|
345
|
+
Sheet: Sheet.Controlled
|
|
346
|
+
});
|
|
347
|
+
function useEmitter() {
|
|
348
|
+
const listeners = React.useRef();
|
|
349
|
+
listeners.current || (listeners.current = /* @__PURE__ */new Set());
|
|
350
|
+
const emit = value => {
|
|
351
|
+
listeners.current.forEach(l => l(value));
|
|
352
|
+
},
|
|
353
|
+
subscribe = React.useCallback(listener => (listeners.current.add(listener), () => {
|
|
354
|
+
listeners.current.delete(listener);
|
|
355
|
+
}), []);
|
|
356
|
+
return [emit, subscribe];
|
|
357
|
+
}
|
|
358
|
+
Select.displayName = SELECT_NAME;
|
|
359
|
+
export { Select, SelectGroupFrame, SelectIcon, SelectSeparator };
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { FloatingOverlay, FloatingPortal } from "@floating-ui/react";
|
|
2
|
+
import { Theme, useIsTouchDevice, useThemeName } from "@tamagui/core";
|
|
3
|
+
import { FocusScope } from "@tamagui/focus-scope";
|
|
4
|
+
import { useMemo } from "react";
|
|
5
|
+
import { useSelectContext, useSelectItemParentContext } from "./context.mjs";
|
|
6
|
+
import { useShowSelectSheet } from "./useSelectBreakpointActive.mjs";
|
|
7
|
+
import { Fragment, jsx } from "react/jsx-runtime";
|
|
8
|
+
const CONTENT_NAME = "SelectContent",
|
|
9
|
+
SelectContent = ({
|
|
10
|
+
children,
|
|
11
|
+
__scopeSelect,
|
|
12
|
+
zIndex = 1e3,
|
|
13
|
+
...focusScopeProps
|
|
14
|
+
}) => {
|
|
15
|
+
const context = useSelectContext(CONTENT_NAME, __scopeSelect),
|
|
16
|
+
itemParentContext = useSelectItemParentContext(CONTENT_NAME, __scopeSelect),
|
|
17
|
+
themeName = useThemeName(),
|
|
18
|
+
showSheet = useShowSelectSheet(context),
|
|
19
|
+
contents = /* @__PURE__ */jsx(Theme, {
|
|
20
|
+
forceClassName: !0,
|
|
21
|
+
name: themeName,
|
|
22
|
+
children
|
|
23
|
+
}),
|
|
24
|
+
touch = useIsTouchDevice(),
|
|
25
|
+
overlayStyle = useMemo(() => ({
|
|
26
|
+
zIndex,
|
|
27
|
+
pointerEvents: context.open ? "auto" : "none"
|
|
28
|
+
}), [context.open]);
|
|
29
|
+
return itemParentContext.shouldRenderWebNative ? /* @__PURE__ */jsx(Fragment, {
|
|
30
|
+
children
|
|
31
|
+
}) : showSheet ? context.open ? /* @__PURE__ */jsx(Fragment, {
|
|
32
|
+
children: contents
|
|
33
|
+
}) : null : /* @__PURE__ */jsx(FloatingPortal, {
|
|
34
|
+
children: /* @__PURE__ */jsx(FloatingOverlay, {
|
|
35
|
+
style: overlayStyle,
|
|
36
|
+
lockScroll: !context.disablePreventBodyScroll && !!context.open && !touch,
|
|
37
|
+
children: /* @__PURE__ */jsx(FocusScope, {
|
|
38
|
+
loop: !0,
|
|
39
|
+
enabled: !!context.open,
|
|
40
|
+
trapped: !0,
|
|
41
|
+
...focusScopeProps,
|
|
42
|
+
children: contents
|
|
43
|
+
})
|
|
44
|
+
})
|
|
45
|
+
});
|
|
46
|
+
};
|
|
47
|
+
export { SelectContent };
|
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
import { flip, inner, offset, shift, size, useClick, useDismiss, useFloating, useInnerOffset, useInteractions, useListNavigation, useRole, useTypeahead } from "@floating-ui/react";
|
|
2
|
+
import { isClient, isWeb, useIsomorphicLayoutEffect } from "@tamagui/constants";
|
|
3
|
+
import { useEvent, useIsTouchDevice } from "@tamagui/core";
|
|
4
|
+
import * as React from "react";
|
|
5
|
+
import { flushSync } from "react-dom";
|
|
6
|
+
import { SCROLL_ARROW_THRESHOLD, WINDOW_PADDING } from "./constants.mjs";
|
|
7
|
+
import { SelectItemParentProvider, SelectProvider, useSelectContext, useSelectItemParentContext } from "./context.mjs";
|
|
8
|
+
import { jsx } from "react/jsx-runtime";
|
|
9
|
+
const SelectInlineImpl = props => {
|
|
10
|
+
const {
|
|
11
|
+
__scopeSelect,
|
|
12
|
+
children,
|
|
13
|
+
open = !1,
|
|
14
|
+
selectedIndexRef,
|
|
15
|
+
listContentRef
|
|
16
|
+
} = props,
|
|
17
|
+
selectContext = useSelectContext("SelectSheetImpl", __scopeSelect),
|
|
18
|
+
selectItemParentContext = useSelectItemParentContext("SelectSheetImpl", __scopeSelect),
|
|
19
|
+
{
|
|
20
|
+
setActiveIndex,
|
|
21
|
+
selectedIndex,
|
|
22
|
+
activeIndex,
|
|
23
|
+
forceUpdate
|
|
24
|
+
} = selectContext,
|
|
25
|
+
{
|
|
26
|
+
setOpen,
|
|
27
|
+
setSelectedIndex
|
|
28
|
+
} = selectItemParentContext,
|
|
29
|
+
[scrollTop, setScrollTop] = React.useState(0),
|
|
30
|
+
touch = useIsTouchDevice(),
|
|
31
|
+
listItemsRef = React.useRef([]),
|
|
32
|
+
overflowRef = React.useRef(null),
|
|
33
|
+
upArrowRef = React.useRef(null),
|
|
34
|
+
downArrowRef = React.useRef(null),
|
|
35
|
+
allowSelectRef = React.useRef(!1),
|
|
36
|
+
allowMouseUpRef = React.useRef(!0),
|
|
37
|
+
selectTimeoutRef = React.useRef(),
|
|
38
|
+
state = React.useRef({
|
|
39
|
+
isMouseOutside: !1
|
|
40
|
+
}),
|
|
41
|
+
[controlledScrolling, setControlledScrolling] = React.useState(!1),
|
|
42
|
+
[fallback, setFallback] = React.useState(!1),
|
|
43
|
+
[innerOffset, setInnerOffset] = React.useState(0),
|
|
44
|
+
[blockSelection, setBlockSelection] = React.useState(!1),
|
|
45
|
+
floatingStyle = React.useRef({});
|
|
46
|
+
useIsomorphicLayoutEffect(() => {
|
|
47
|
+
queueMicrotask(() => {
|
|
48
|
+
open || (setScrollTop(0), setFallback(!1), setActiveIndex(null), setControlledScrolling(!1));
|
|
49
|
+
});
|
|
50
|
+
}, [open, setActiveIndex]), isWeb && isClient && useIsomorphicLayoutEffect(() => {
|
|
51
|
+
if (!open) return;
|
|
52
|
+
const mouseUp = e => {
|
|
53
|
+
state.current.isMouseOutside && setOpen(!1);
|
|
54
|
+
};
|
|
55
|
+
return document.addEventListener("mouseup", mouseUp), () => {
|
|
56
|
+
document.removeEventListener("mouseup", mouseUp);
|
|
57
|
+
};
|
|
58
|
+
}, [open]);
|
|
59
|
+
const flipOrShiftMiddlewares = [touch ? shift({
|
|
60
|
+
crossAxis: !0,
|
|
61
|
+
padding: WINDOW_PADDING
|
|
62
|
+
}) : flip({
|
|
63
|
+
padding: WINDOW_PADDING
|
|
64
|
+
})],
|
|
65
|
+
{
|
|
66
|
+
x,
|
|
67
|
+
y,
|
|
68
|
+
strategy,
|
|
69
|
+
context,
|
|
70
|
+
refs,
|
|
71
|
+
update
|
|
72
|
+
} = useFloating({
|
|
73
|
+
open,
|
|
74
|
+
onOpenChange: setOpen,
|
|
75
|
+
placement: "bottom-start",
|
|
76
|
+
middleware: [size({
|
|
77
|
+
apply({
|
|
78
|
+
rects: {
|
|
79
|
+
reference: {
|
|
80
|
+
width
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}) {
|
|
84
|
+
floatingStyle.current = {
|
|
85
|
+
minWidth: width + 8
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
}), ...flipOrShiftMiddlewares, inner({
|
|
89
|
+
listRef: listItemsRef,
|
|
90
|
+
overflowRef,
|
|
91
|
+
index: selectedIndex,
|
|
92
|
+
offset: innerOffset,
|
|
93
|
+
// onFallbackChange: setFallback,
|
|
94
|
+
padding: 10,
|
|
95
|
+
minItemsVisible: touch ? 10 : 4,
|
|
96
|
+
referenceOverflowThreshold: 20
|
|
97
|
+
}), offset({
|
|
98
|
+
crossAxis: -5
|
|
99
|
+
})]
|
|
100
|
+
});
|
|
101
|
+
useIsomorphicLayoutEffect(() => (window.addEventListener("resize", update), open && update(), () => window.removeEventListener("resize", update)), [update, open]);
|
|
102
|
+
const floatingRef = refs.floating,
|
|
103
|
+
showUpArrow = open && scrollTop > SCROLL_ARROW_THRESHOLD,
|
|
104
|
+
showDownArrow = open && floatingRef.current && scrollTop < floatingRef.current.scrollHeight - floatingRef.current.clientHeight - SCROLL_ARROW_THRESHOLD,
|
|
105
|
+
onMatch = useEvent(index => (open ? setActiveIndex : setSelectedIndex)(index)),
|
|
106
|
+
interactionsProps = [useClick(context, {
|
|
107
|
+
event: "mousedown",
|
|
108
|
+
keyboardHandlers: !1
|
|
109
|
+
}), useDismiss(context, {
|
|
110
|
+
outsidePress: !1
|
|
111
|
+
}), useRole(context, {
|
|
112
|
+
role: "listbox"
|
|
113
|
+
}), useInnerOffset(context, {
|
|
114
|
+
enabled: !fallback && (!!showUpArrow || !!showDownArrow),
|
|
115
|
+
onChange: setInnerOffset,
|
|
116
|
+
overflowRef,
|
|
117
|
+
scrollRef: refs.floating
|
|
118
|
+
}), useListNavigation(context, {
|
|
119
|
+
listRef: listItemsRef,
|
|
120
|
+
activeIndex: activeIndex || 0,
|
|
121
|
+
selectedIndex,
|
|
122
|
+
onNavigate: setActiveIndex
|
|
123
|
+
}), useTypeahead(context, {
|
|
124
|
+
listRef: listContentRef,
|
|
125
|
+
onMatch,
|
|
126
|
+
selectedIndex,
|
|
127
|
+
activeIndex
|
|
128
|
+
})],
|
|
129
|
+
interactions = useInteractions(
|
|
130
|
+
// unfortunately these memos will just always break due to floating-ui context always changing :/
|
|
131
|
+
React.useMemo(() => interactionsProps, interactionsProps)),
|
|
132
|
+
interactionsContext = React.useMemo(() => ({
|
|
133
|
+
...interactions,
|
|
134
|
+
getReferenceProps() {
|
|
135
|
+
return interactions.getReferenceProps({
|
|
136
|
+
ref: refs.reference,
|
|
137
|
+
className: "SelectTrigger",
|
|
138
|
+
onKeyDown(event) {
|
|
139
|
+
(event.key === "Enter" || event.code === "Space" || event.key === " " && !context.dataRef.current.typing) && (event.preventDefault(), setOpen(!0));
|
|
140
|
+
}
|
|
141
|
+
});
|
|
142
|
+
},
|
|
143
|
+
getFloatingProps(props2) {
|
|
144
|
+
return interactions.getFloatingProps({
|
|
145
|
+
ref: refs.floating,
|
|
146
|
+
className: "Select",
|
|
147
|
+
...props2,
|
|
148
|
+
style: {
|
|
149
|
+
position: strategy,
|
|
150
|
+
top: y ?? "",
|
|
151
|
+
left: x ?? "",
|
|
152
|
+
outline: 0,
|
|
153
|
+
scrollbarWidth: "none",
|
|
154
|
+
...floatingStyle.current,
|
|
155
|
+
...props2?.style
|
|
156
|
+
},
|
|
157
|
+
onPointerEnter() {
|
|
158
|
+
setControlledScrolling(!1), state.current.isMouseOutside = !1;
|
|
159
|
+
},
|
|
160
|
+
onPointerLeave() {
|
|
161
|
+
state.current.isMouseOutside = !0;
|
|
162
|
+
},
|
|
163
|
+
onPointerMove() {
|
|
164
|
+
state.current.isMouseOutside = !1, setControlledScrolling(!1);
|
|
165
|
+
},
|
|
166
|
+
onKeyDown() {
|
|
167
|
+
setControlledScrolling(!0);
|
|
168
|
+
},
|
|
169
|
+
onContextMenu(e) {
|
|
170
|
+
e.preventDefault();
|
|
171
|
+
},
|
|
172
|
+
onScroll(event) {
|
|
173
|
+
flushSync(() => setScrollTop(event.currentTarget.scrollTop));
|
|
174
|
+
}
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
}), [refs.reference.current, x, y, refs.floating.current, interactions]);
|
|
178
|
+
return useIsomorphicLayoutEffect(() => {
|
|
179
|
+
if (open) return selectTimeoutRef.current = setTimeout(() => {
|
|
180
|
+
allowSelectRef.current = !0;
|
|
181
|
+
}, 300), () => {
|
|
182
|
+
clearTimeout(selectTimeoutRef.current);
|
|
183
|
+
};
|
|
184
|
+
allowSelectRef.current = !1, allowMouseUpRef.current = !0, setInnerOffset(0), setFallback(!1), setBlockSelection(!1);
|
|
185
|
+
}, [open]), useIsomorphicLayoutEffect(() => {
|
|
186
|
+
!open && state.current.isMouseOutside && (state.current.isMouseOutside = !1);
|
|
187
|
+
}, [open]), useIsomorphicLayoutEffect(() => {
|
|
188
|
+
function onPointerDown(e) {
|
|
189
|
+
const target = e.target;
|
|
190
|
+
refs.floating.current?.contains(target) || upArrowRef.current?.contains(target) || downArrowRef.current?.contains(target) || (setOpen(!1), setControlledScrolling(!1));
|
|
191
|
+
}
|
|
192
|
+
if (open) return document.addEventListener("pointerdown", onPointerDown), () => {
|
|
193
|
+
document.removeEventListener("pointerdown", onPointerDown);
|
|
194
|
+
};
|
|
195
|
+
}, [open, refs, setOpen]), React.useEffect(() => {
|
|
196
|
+
open && controlledScrolling && activeIndex != null && listItemsRef.current[activeIndex]?.scrollIntoView({
|
|
197
|
+
block: "nearest"
|
|
198
|
+
}), setScrollTop(refs.floating.current?.scrollTop ?? 0);
|
|
199
|
+
}, [open, refs, controlledScrolling, activeIndex]), React.useEffect(() => {
|
|
200
|
+
open && fallback && selectedIndex != null && listItemsRef.current[selectedIndex]?.scrollIntoView({
|
|
201
|
+
block: "nearest"
|
|
202
|
+
});
|
|
203
|
+
}, [open, fallback, selectedIndex]), useIsomorphicLayoutEffect(() => {
|
|
204
|
+
refs.floating.current && fallback && (refs.floating.current.style.maxHeight = "");
|
|
205
|
+
}, [refs, fallback]), /* @__PURE__ */jsx(SelectProvider, {
|
|
206
|
+
scope: __scopeSelect,
|
|
207
|
+
...selectContext,
|
|
208
|
+
setScrollTop,
|
|
209
|
+
setInnerOffset,
|
|
210
|
+
fallback,
|
|
211
|
+
floatingContext: context,
|
|
212
|
+
activeIndex,
|
|
213
|
+
canScrollDown: !!showDownArrow,
|
|
214
|
+
canScrollUp: !!showUpArrow,
|
|
215
|
+
controlledScrolling,
|
|
216
|
+
blockSelection,
|
|
217
|
+
upArrowRef,
|
|
218
|
+
downArrowRef,
|
|
219
|
+
update,
|
|
220
|
+
children: /* @__PURE__ */jsx(SelectItemParentProvider, {
|
|
221
|
+
scope: __scopeSelect,
|
|
222
|
+
...selectItemParentContext,
|
|
223
|
+
allowMouseUpRef,
|
|
224
|
+
allowSelectRef,
|
|
225
|
+
dataRef: context.dataRef,
|
|
226
|
+
interactions: interactionsContext,
|
|
227
|
+
listRef: listItemsRef,
|
|
228
|
+
selectTimeoutRef,
|
|
229
|
+
children
|
|
230
|
+
})
|
|
231
|
+
});
|
|
232
|
+
};
|
|
233
|
+
export { SelectInlineImpl };
|