@tamagui/toggle-group 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/Toggle.mjs +91 -0
- package/dist/esm/ToggleGroup.mjs +251 -0
- package/dist/esm/index.mjs +1 -0
- package/dist/jsx/Toggle.mjs +91 -0
- package/dist/jsx/ToggleGroup.mjs +251 -0
- package/dist/jsx/index.mjs +1 -0
- package/package.json +15 -15
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,91 @@
|
|
|
1
|
+
import { composeEventHandlers } from "@tamagui/helpers";
|
|
2
|
+
import { ThemeableStack } from "@tamagui/stacks";
|
|
3
|
+
import { useControllableState } from "@tamagui/use-controllable-state";
|
|
4
|
+
import { styled } from "@tamagui/web";
|
|
5
|
+
import * as React from "react";
|
|
6
|
+
import { jsx } from "react/jsx-runtime";
|
|
7
|
+
const NAME = "Toggle",
|
|
8
|
+
ToggleFrame = styled(ThemeableStack, {
|
|
9
|
+
name: NAME,
|
|
10
|
+
tag: "button",
|
|
11
|
+
variants: {
|
|
12
|
+
unstyled: {
|
|
13
|
+
false: {
|
|
14
|
+
pressTheme: !0,
|
|
15
|
+
backgroundColor: "$background",
|
|
16
|
+
alignItems: "center",
|
|
17
|
+
justifyContent: "center",
|
|
18
|
+
display: "flex",
|
|
19
|
+
borderColor: "$borderColor",
|
|
20
|
+
borderWidth: 1,
|
|
21
|
+
margin: -1,
|
|
22
|
+
hoverStyle: {
|
|
23
|
+
backgroundColor: "$backgroundHover"
|
|
24
|
+
},
|
|
25
|
+
pressStyle: {
|
|
26
|
+
backgroundColor: "$backgroundPress"
|
|
27
|
+
},
|
|
28
|
+
focusStyle: {
|
|
29
|
+
borderColor: "$borderColorFocus",
|
|
30
|
+
outlineColor: "$outlineColor",
|
|
31
|
+
outlineWidth: 2,
|
|
32
|
+
outlineStyle: "solid"
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
active: {
|
|
37
|
+
true: {
|
|
38
|
+
zIndex: 1,
|
|
39
|
+
hoverStyle: {
|
|
40
|
+
backgroundColor: "$background"
|
|
41
|
+
},
|
|
42
|
+
focusStyle: {
|
|
43
|
+
borderColor: "$borderColor",
|
|
44
|
+
backgroundColor: "$background"
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
},
|
|
48
|
+
orientation: {
|
|
49
|
+
horizontal: {
|
|
50
|
+
flexDirection: "row",
|
|
51
|
+
spaceDirection: "horizontal"
|
|
52
|
+
},
|
|
53
|
+
vertical: {
|
|
54
|
+
flexDirection: "column",
|
|
55
|
+
spaceDirection: "vertical"
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
},
|
|
59
|
+
defaultVariants: {
|
|
60
|
+
unstyled: process.env.TAMAGUI_HEADLESS === "1"
|
|
61
|
+
}
|
|
62
|
+
}),
|
|
63
|
+
Toggle = React.forwardRef(function (props, forwardedRef) {
|
|
64
|
+
const {
|
|
65
|
+
pressed: pressedProp,
|
|
66
|
+
defaultPressed = !1,
|
|
67
|
+
onPressedChange,
|
|
68
|
+
...buttonProps
|
|
69
|
+
} = props,
|
|
70
|
+
[pressed = !1, setPressed] = useControllableState({
|
|
71
|
+
prop: pressedProp,
|
|
72
|
+
onChange: onPressedChange,
|
|
73
|
+
defaultProp: defaultPressed
|
|
74
|
+
});
|
|
75
|
+
return /* @__PURE__ */jsx(ToggleFrame, {
|
|
76
|
+
...(!props.unstyled && {
|
|
77
|
+
theme: pressed ? "active" : null,
|
|
78
|
+
themeShallow: !0
|
|
79
|
+
}),
|
|
80
|
+
active: props.unstyled ? void 0 : pressed,
|
|
81
|
+
"aria-pressed": pressed,
|
|
82
|
+
"data-state": pressed ? "on" : "off",
|
|
83
|
+
"data-disabled": props.disabled ? "" : void 0,
|
|
84
|
+
...buttonProps,
|
|
85
|
+
ref: forwardedRef,
|
|
86
|
+
onPress: composeEventHandlers(props.onPress ?? void 0, () => {
|
|
87
|
+
props.disabled || setPressed(!pressed);
|
|
88
|
+
})
|
|
89
|
+
});
|
|
90
|
+
});
|
|
91
|
+
export { Toggle, ToggleFrame };
|
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
import { isWeb } from "@tamagui/constants";
|
|
2
|
+
import { registerFocusable } from "@tamagui/focusable";
|
|
3
|
+
import { getFontSize } from "@tamagui/font-size";
|
|
4
|
+
import { getSize } from "@tamagui/get-token";
|
|
5
|
+
import { Group, useGroupItem } from "@tamagui/group";
|
|
6
|
+
import { withStaticProperties } from "@tamagui/helpers";
|
|
7
|
+
import { useGetThemedIcon } from "@tamagui/helpers-tamagui";
|
|
8
|
+
import { RovingFocusGroup } from "@tamagui/roving-focus";
|
|
9
|
+
import { useControllableState } from "@tamagui/use-controllable-state";
|
|
10
|
+
import { useDirection } from "@tamagui/use-direction";
|
|
11
|
+
import { createStyledContext, getVariableValue, styled, useTheme } from "@tamagui/web";
|
|
12
|
+
import React from "react";
|
|
13
|
+
import { Toggle, ToggleFrame } from "./Toggle.mjs";
|
|
14
|
+
import { jsx } from "react/jsx-runtime";
|
|
15
|
+
const TOGGLE_GROUP_NAME = "ToggleGroup",
|
|
16
|
+
TOGGLE_GROUP_ITEM_NAME = "ToggleGroupItem",
|
|
17
|
+
TOGGLE_GROUP_CONTEXT = "ToggleGroup",
|
|
18
|
+
{
|
|
19
|
+
Provider: ToggleGroupItemProvider,
|
|
20
|
+
useStyledContext: useToggleGroupItemContext
|
|
21
|
+
} = createStyledContext(),
|
|
22
|
+
{
|
|
23
|
+
Provider: ToggleGroupContext,
|
|
24
|
+
useStyledContext: useToggleGroupContext
|
|
25
|
+
} = createStyledContext(),
|
|
26
|
+
ToggleGroupItem = ToggleFrame.extractable(React.forwardRef((props, forwardedRef) => {
|
|
27
|
+
const valueContext = useToggleGroupValueContext(props.__scopeToggleGroup),
|
|
28
|
+
context = useToggleGroupContext(props.__scopeToggleGroup),
|
|
29
|
+
pressed = valueContext?.value.includes(props.value),
|
|
30
|
+
disabled = context.disabled || props.disabled || !1,
|
|
31
|
+
groupItemProps = useGroupItem({
|
|
32
|
+
disabled
|
|
33
|
+
}),
|
|
34
|
+
size = props.size ?? context.size,
|
|
35
|
+
sizeProps = props.unstyled ? {} : {
|
|
36
|
+
width: void 0,
|
|
37
|
+
height: void 0,
|
|
38
|
+
padding: getVariableValue(size) * 0.6
|
|
39
|
+
},
|
|
40
|
+
iconSize = (typeof size == "number" ? size * 0.7 : getFontSize(size)) * 1.2,
|
|
41
|
+
theme = useTheme(),
|
|
42
|
+
getThemedIcon = useGetThemedIcon({
|
|
43
|
+
size: iconSize,
|
|
44
|
+
color: theme.color
|
|
45
|
+
}),
|
|
46
|
+
children = React.Children.toArray(props.children).map(child => props.disablePassStyles || !React.isValidElement(child) ? child : getThemedIcon(child)),
|
|
47
|
+
commonProps = {
|
|
48
|
+
pressed,
|
|
49
|
+
disabled,
|
|
50
|
+
...sizeProps,
|
|
51
|
+
...props,
|
|
52
|
+
children
|
|
53
|
+
},
|
|
54
|
+
inner = /* @__PURE__ */jsx(ToggleGroupItemImpl, {
|
|
55
|
+
...commonProps,
|
|
56
|
+
ref: forwardedRef,
|
|
57
|
+
focusable: !disabled,
|
|
58
|
+
disabled,
|
|
59
|
+
...groupItemProps
|
|
60
|
+
});
|
|
61
|
+
return /* @__PURE__ */jsx(ToggleGroupItemProvider, {
|
|
62
|
+
scope: props.__scopeToggleGroup,
|
|
63
|
+
children: context.rovingFocus ? /* @__PURE__ */jsx(RovingFocusGroup.Item, {
|
|
64
|
+
asChild: "except-style",
|
|
65
|
+
__scopeRovingFocusGroup: props.__scopeToggleGroup || TOGGLE_GROUP_CONTEXT,
|
|
66
|
+
focusable: !disabled,
|
|
67
|
+
active: pressed,
|
|
68
|
+
children: inner
|
|
69
|
+
}) : inner
|
|
70
|
+
});
|
|
71
|
+
}));
|
|
72
|
+
ToggleGroupItem.displayName = TOGGLE_GROUP_ITEM_NAME;
|
|
73
|
+
const ToggleGroupItemImpl = React.forwardRef((props, forwardedRef) => {
|
|
74
|
+
const {
|
|
75
|
+
__scopeToggleGroup,
|
|
76
|
+
value,
|
|
77
|
+
...itemProps
|
|
78
|
+
} = props,
|
|
79
|
+
valueContext = useToggleGroupValueContext(__scopeToggleGroup),
|
|
80
|
+
singleProps = {
|
|
81
|
+
"aria-pressed": void 0
|
|
82
|
+
},
|
|
83
|
+
typeProps = valueContext.type === "single" ? singleProps : void 0;
|
|
84
|
+
return /* @__PURE__ */jsx(Toggle, {
|
|
85
|
+
...typeProps,
|
|
86
|
+
...itemProps,
|
|
87
|
+
ref: forwardedRef,
|
|
88
|
+
onPressedChange: pressed => {
|
|
89
|
+
pressed ? valueContext.onItemActivate(value) : valueContext.onItemDeactivate(value);
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
}),
|
|
93
|
+
ToggleGroup = withStaticProperties(React.forwardRef((props, forwardedRef) => {
|
|
94
|
+
const {
|
|
95
|
+
type,
|
|
96
|
+
...toggleGroupProps
|
|
97
|
+
} = props;
|
|
98
|
+
if (isWeb || React.useEffect(() => {
|
|
99
|
+
if (props.id) return registerFocusable(props.id, {
|
|
100
|
+
// TODO: would be nice to focus on the first child later - could be done with reforest
|
|
101
|
+
// for now leaving it empty
|
|
102
|
+
focus: () => {}
|
|
103
|
+
});
|
|
104
|
+
}, [props.id]), type === "single") return /* @__PURE__ */jsx(ToggleGroupImplSingle, {
|
|
105
|
+
...toggleGroupProps,
|
|
106
|
+
ref: forwardedRef
|
|
107
|
+
});
|
|
108
|
+
if (type === "multiple") return /* @__PURE__ */jsx(ToggleGroupImplMultiple, {
|
|
109
|
+
...toggleGroupProps,
|
|
110
|
+
ref: forwardedRef
|
|
111
|
+
});
|
|
112
|
+
throw new Error(`Missing prop \`type\` expected on \`${TOGGLE_GROUP_NAME}\``);
|
|
113
|
+
}), {
|
|
114
|
+
Item: ToggleGroupItem
|
|
115
|
+
});
|
|
116
|
+
ToggleGroup.displayName = TOGGLE_GROUP_NAME;
|
|
117
|
+
const {
|
|
118
|
+
Provider: ToggleGroupValueProvider,
|
|
119
|
+
useStyledContext: useToggleGroupValueContext
|
|
120
|
+
} = createStyledContext(),
|
|
121
|
+
ToggleGroupImplSingle = React.forwardRef((props, forwardedRef) => {
|
|
122
|
+
const {
|
|
123
|
+
value: valueProp,
|
|
124
|
+
defaultValue,
|
|
125
|
+
onValueChange = () => {},
|
|
126
|
+
disableDeactivation = !1,
|
|
127
|
+
...toggleGroupSingleProps
|
|
128
|
+
} = props,
|
|
129
|
+
[value, setValue] = useControllableState({
|
|
130
|
+
prop: valueProp,
|
|
131
|
+
defaultProp: defaultValue,
|
|
132
|
+
onChange: onValueChange
|
|
133
|
+
});
|
|
134
|
+
return /* @__PURE__ */jsx(ToggleGroupValueProvider, {
|
|
135
|
+
scope: props.__scopeToggleGroup,
|
|
136
|
+
type: "single",
|
|
137
|
+
value: value ? [value] : [],
|
|
138
|
+
defaultValue: value,
|
|
139
|
+
onItemActivate: setValue,
|
|
140
|
+
onItemDeactivate: React.useCallback(() => disableDeactivation ? null : setValue(""), [setValue, disableDeactivation]),
|
|
141
|
+
children: /* @__PURE__ */jsx(ToggleGroupImpl, {
|
|
142
|
+
...toggleGroupSingleProps,
|
|
143
|
+
ref: forwardedRef
|
|
144
|
+
})
|
|
145
|
+
});
|
|
146
|
+
}),
|
|
147
|
+
ToggleGroupImplMultiple = React.forwardRef((props, forwardedRef) => {
|
|
148
|
+
const {
|
|
149
|
+
value: valueProp,
|
|
150
|
+
defaultValue,
|
|
151
|
+
onValueChange = () => {},
|
|
152
|
+
...toggleGroupMultipleProps
|
|
153
|
+
} = props,
|
|
154
|
+
[value = [], setValue] = useControllableState({
|
|
155
|
+
prop: valueProp,
|
|
156
|
+
defaultProp: defaultValue,
|
|
157
|
+
onChange: onValueChange
|
|
158
|
+
}),
|
|
159
|
+
handleButtonActivate = React.useCallback(itemValue => setValue((prevValue = []) => [...prevValue, itemValue]), [setValue]),
|
|
160
|
+
handleButtonDeactivate = React.useCallback(itemValue => setValue((prevValue = []) => prevValue.filter(value2 => value2 !== itemValue)), [setValue]);
|
|
161
|
+
return /* @__PURE__ */jsx(ToggleGroupValueProvider, {
|
|
162
|
+
scope: props.__scopeToggleGroup,
|
|
163
|
+
type: "multiple",
|
|
164
|
+
value,
|
|
165
|
+
defaultValue: value,
|
|
166
|
+
onItemActivate: handleButtonActivate,
|
|
167
|
+
onItemDeactivate: handleButtonDeactivate,
|
|
168
|
+
children: /* @__PURE__ */jsx(ToggleGroupImpl, {
|
|
169
|
+
...toggleGroupMultipleProps,
|
|
170
|
+
ref: forwardedRef
|
|
171
|
+
})
|
|
172
|
+
});
|
|
173
|
+
});
|
|
174
|
+
ToggleGroup.displayName = TOGGLE_GROUP_NAME;
|
|
175
|
+
const ToggleGroupImplElementFrame = styled(Group, {
|
|
176
|
+
name: TOGGLE_GROUP_NAME,
|
|
177
|
+
variants: {
|
|
178
|
+
unstyled: {
|
|
179
|
+
false: {
|
|
180
|
+
backgroundColor: "$background"
|
|
181
|
+
}
|
|
182
|
+
},
|
|
183
|
+
orientation: {
|
|
184
|
+
vertical: {
|
|
185
|
+
flexDirection: "column",
|
|
186
|
+
spaceDirection: "vertical"
|
|
187
|
+
},
|
|
188
|
+
horizontal: {
|
|
189
|
+
flexDirection: "row",
|
|
190
|
+
spaceDirection: "horizontal"
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
},
|
|
194
|
+
defaultVariants: {
|
|
195
|
+
unstyled: process.env.TAMAGUI_HEADLESS === "1"
|
|
196
|
+
}
|
|
197
|
+
}),
|
|
198
|
+
ToggleGroupImpl = ToggleGroupImplElementFrame.extractable(React.forwardRef((props, forwardedRef) => {
|
|
199
|
+
const {
|
|
200
|
+
__scopeToggleGroup,
|
|
201
|
+
disabled = !1,
|
|
202
|
+
orientation = "horizontal",
|
|
203
|
+
dir,
|
|
204
|
+
rovingFocus = !0,
|
|
205
|
+
loop = !0,
|
|
206
|
+
unstyled = !1,
|
|
207
|
+
size: sizeProp = "$true",
|
|
208
|
+
sizeAdjust = 0,
|
|
209
|
+
...toggleGroupProps
|
|
210
|
+
} = props,
|
|
211
|
+
direction = useDirection(dir),
|
|
212
|
+
commonProps = {
|
|
213
|
+
role: "group",
|
|
214
|
+
dir: direction,
|
|
215
|
+
...toggleGroupProps
|
|
216
|
+
},
|
|
217
|
+
adjustedSize = getVariableValue(getSize(sizeProp, {
|
|
218
|
+
shift: sizeAdjust
|
|
219
|
+
})),
|
|
220
|
+
size = Math.round(adjustedSize * 0.45);
|
|
221
|
+
return /* @__PURE__ */jsx(ToggleGroupContext, {
|
|
222
|
+
scope: __scopeToggleGroup,
|
|
223
|
+
rovingFocus,
|
|
224
|
+
disabled,
|
|
225
|
+
size,
|
|
226
|
+
children: rovingFocus ? /* @__PURE__ */jsx(RovingFocusGroup, {
|
|
227
|
+
asChild: "except-style",
|
|
228
|
+
__scopeRovingFocusGroup: __scopeToggleGroup || TOGGLE_GROUP_CONTEXT,
|
|
229
|
+
orientation,
|
|
230
|
+
dir: direction,
|
|
231
|
+
loop,
|
|
232
|
+
children: /* @__PURE__ */jsx(ToggleGroupImplElementFrame, {
|
|
233
|
+
"aria-orientation": orientation,
|
|
234
|
+
orientation,
|
|
235
|
+
axis: orientation,
|
|
236
|
+
ref: forwardedRef,
|
|
237
|
+
"data-disabled": disabled ? "" : void 0,
|
|
238
|
+
unstyled,
|
|
239
|
+
...commonProps
|
|
240
|
+
})
|
|
241
|
+
}) : /* @__PURE__ */jsx(ToggleGroupImplElementFrame, {
|
|
242
|
+
"aria-orientation": orientation,
|
|
243
|
+
ref: forwardedRef,
|
|
244
|
+
orientation,
|
|
245
|
+
"data-disabled": disabled ? "" : void 0,
|
|
246
|
+
unstyled,
|
|
247
|
+
...commonProps
|
|
248
|
+
})
|
|
249
|
+
});
|
|
250
|
+
}));
|
|
251
|
+
export { ToggleGroup };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./ToggleGroup.mjs";
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { composeEventHandlers } from "@tamagui/helpers";
|
|
2
|
+
import { ThemeableStack } from "@tamagui/stacks";
|
|
3
|
+
import { useControllableState } from "@tamagui/use-controllable-state";
|
|
4
|
+
import { styled } from "@tamagui/web";
|
|
5
|
+
import * as React from "react";
|
|
6
|
+
import { jsx } from "react/jsx-runtime";
|
|
7
|
+
const NAME = "Toggle",
|
|
8
|
+
ToggleFrame = styled(ThemeableStack, {
|
|
9
|
+
name: NAME,
|
|
10
|
+
tag: "button",
|
|
11
|
+
variants: {
|
|
12
|
+
unstyled: {
|
|
13
|
+
false: {
|
|
14
|
+
pressTheme: !0,
|
|
15
|
+
backgroundColor: "$background",
|
|
16
|
+
alignItems: "center",
|
|
17
|
+
justifyContent: "center",
|
|
18
|
+
display: "flex",
|
|
19
|
+
borderColor: "$borderColor",
|
|
20
|
+
borderWidth: 1,
|
|
21
|
+
margin: -1,
|
|
22
|
+
hoverStyle: {
|
|
23
|
+
backgroundColor: "$backgroundHover"
|
|
24
|
+
},
|
|
25
|
+
pressStyle: {
|
|
26
|
+
backgroundColor: "$backgroundPress"
|
|
27
|
+
},
|
|
28
|
+
focusStyle: {
|
|
29
|
+
borderColor: "$borderColorFocus",
|
|
30
|
+
outlineColor: "$outlineColor",
|
|
31
|
+
outlineWidth: 2,
|
|
32
|
+
outlineStyle: "solid"
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
active: {
|
|
37
|
+
true: {
|
|
38
|
+
zIndex: 1,
|
|
39
|
+
hoverStyle: {
|
|
40
|
+
backgroundColor: "$background"
|
|
41
|
+
},
|
|
42
|
+
focusStyle: {
|
|
43
|
+
borderColor: "$borderColor",
|
|
44
|
+
backgroundColor: "$background"
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
},
|
|
48
|
+
orientation: {
|
|
49
|
+
horizontal: {
|
|
50
|
+
flexDirection: "row",
|
|
51
|
+
spaceDirection: "horizontal"
|
|
52
|
+
},
|
|
53
|
+
vertical: {
|
|
54
|
+
flexDirection: "column",
|
|
55
|
+
spaceDirection: "vertical"
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
},
|
|
59
|
+
defaultVariants: {
|
|
60
|
+
unstyled: process.env.TAMAGUI_HEADLESS === "1"
|
|
61
|
+
}
|
|
62
|
+
}),
|
|
63
|
+
Toggle = React.forwardRef(function (props, forwardedRef) {
|
|
64
|
+
const {
|
|
65
|
+
pressed: pressedProp,
|
|
66
|
+
defaultPressed = !1,
|
|
67
|
+
onPressedChange,
|
|
68
|
+
...buttonProps
|
|
69
|
+
} = props,
|
|
70
|
+
[pressed = !1, setPressed] = useControllableState({
|
|
71
|
+
prop: pressedProp,
|
|
72
|
+
onChange: onPressedChange,
|
|
73
|
+
defaultProp: defaultPressed
|
|
74
|
+
});
|
|
75
|
+
return /* @__PURE__ */jsx(ToggleFrame, {
|
|
76
|
+
...(!props.unstyled && {
|
|
77
|
+
theme: pressed ? "active" : null,
|
|
78
|
+
themeShallow: !0
|
|
79
|
+
}),
|
|
80
|
+
active: props.unstyled ? void 0 : pressed,
|
|
81
|
+
"aria-pressed": pressed,
|
|
82
|
+
"data-state": pressed ? "on" : "off",
|
|
83
|
+
"data-disabled": props.disabled ? "" : void 0,
|
|
84
|
+
...buttonProps,
|
|
85
|
+
ref: forwardedRef,
|
|
86
|
+
onPress: composeEventHandlers(props.onPress ?? void 0, () => {
|
|
87
|
+
props.disabled || setPressed(!pressed);
|
|
88
|
+
})
|
|
89
|
+
});
|
|
90
|
+
});
|
|
91
|
+
export { Toggle, ToggleFrame };
|
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
import { isWeb } from "@tamagui/constants";
|
|
2
|
+
import { registerFocusable } from "@tamagui/focusable";
|
|
3
|
+
import { getFontSize } from "@tamagui/font-size";
|
|
4
|
+
import { getSize } from "@tamagui/get-token";
|
|
5
|
+
import { Group, useGroupItem } from "@tamagui/group";
|
|
6
|
+
import { withStaticProperties } from "@tamagui/helpers";
|
|
7
|
+
import { useGetThemedIcon } from "@tamagui/helpers-tamagui";
|
|
8
|
+
import { RovingFocusGroup } from "@tamagui/roving-focus";
|
|
9
|
+
import { useControllableState } from "@tamagui/use-controllable-state";
|
|
10
|
+
import { useDirection } from "@tamagui/use-direction";
|
|
11
|
+
import { createStyledContext, getVariableValue, styled, useTheme } from "@tamagui/web";
|
|
12
|
+
import React from "react";
|
|
13
|
+
import { Toggle, ToggleFrame } from "./Toggle.mjs";
|
|
14
|
+
import { jsx } from "react/jsx-runtime";
|
|
15
|
+
const TOGGLE_GROUP_NAME = "ToggleGroup",
|
|
16
|
+
TOGGLE_GROUP_ITEM_NAME = "ToggleGroupItem",
|
|
17
|
+
TOGGLE_GROUP_CONTEXT = "ToggleGroup",
|
|
18
|
+
{
|
|
19
|
+
Provider: ToggleGroupItemProvider,
|
|
20
|
+
useStyledContext: useToggleGroupItemContext
|
|
21
|
+
} = createStyledContext(),
|
|
22
|
+
{
|
|
23
|
+
Provider: ToggleGroupContext,
|
|
24
|
+
useStyledContext: useToggleGroupContext
|
|
25
|
+
} = createStyledContext(),
|
|
26
|
+
ToggleGroupItem = ToggleFrame.extractable(React.forwardRef((props, forwardedRef) => {
|
|
27
|
+
const valueContext = useToggleGroupValueContext(props.__scopeToggleGroup),
|
|
28
|
+
context = useToggleGroupContext(props.__scopeToggleGroup),
|
|
29
|
+
pressed = valueContext?.value.includes(props.value),
|
|
30
|
+
disabled = context.disabled || props.disabled || !1,
|
|
31
|
+
groupItemProps = useGroupItem({
|
|
32
|
+
disabled
|
|
33
|
+
}),
|
|
34
|
+
size = props.size ?? context.size,
|
|
35
|
+
sizeProps = props.unstyled ? {} : {
|
|
36
|
+
width: void 0,
|
|
37
|
+
height: void 0,
|
|
38
|
+
padding: getVariableValue(size) * 0.6
|
|
39
|
+
},
|
|
40
|
+
iconSize = (typeof size == "number" ? size * 0.7 : getFontSize(size)) * 1.2,
|
|
41
|
+
theme = useTheme(),
|
|
42
|
+
getThemedIcon = useGetThemedIcon({
|
|
43
|
+
size: iconSize,
|
|
44
|
+
color: theme.color
|
|
45
|
+
}),
|
|
46
|
+
children = React.Children.toArray(props.children).map(child => props.disablePassStyles || !React.isValidElement(child) ? child : getThemedIcon(child)),
|
|
47
|
+
commonProps = {
|
|
48
|
+
pressed,
|
|
49
|
+
disabled,
|
|
50
|
+
...sizeProps,
|
|
51
|
+
...props,
|
|
52
|
+
children
|
|
53
|
+
},
|
|
54
|
+
inner = /* @__PURE__ */jsx(ToggleGroupItemImpl, {
|
|
55
|
+
...commonProps,
|
|
56
|
+
ref: forwardedRef,
|
|
57
|
+
focusable: !disabled,
|
|
58
|
+
disabled,
|
|
59
|
+
...groupItemProps
|
|
60
|
+
});
|
|
61
|
+
return /* @__PURE__ */jsx(ToggleGroupItemProvider, {
|
|
62
|
+
scope: props.__scopeToggleGroup,
|
|
63
|
+
children: context.rovingFocus ? /* @__PURE__ */jsx(RovingFocusGroup.Item, {
|
|
64
|
+
asChild: "except-style",
|
|
65
|
+
__scopeRovingFocusGroup: props.__scopeToggleGroup || TOGGLE_GROUP_CONTEXT,
|
|
66
|
+
focusable: !disabled,
|
|
67
|
+
active: pressed,
|
|
68
|
+
children: inner
|
|
69
|
+
}) : inner
|
|
70
|
+
});
|
|
71
|
+
}));
|
|
72
|
+
ToggleGroupItem.displayName = TOGGLE_GROUP_ITEM_NAME;
|
|
73
|
+
const ToggleGroupItemImpl = React.forwardRef((props, forwardedRef) => {
|
|
74
|
+
const {
|
|
75
|
+
__scopeToggleGroup,
|
|
76
|
+
value,
|
|
77
|
+
...itemProps
|
|
78
|
+
} = props,
|
|
79
|
+
valueContext = useToggleGroupValueContext(__scopeToggleGroup),
|
|
80
|
+
singleProps = {
|
|
81
|
+
"aria-pressed": void 0
|
|
82
|
+
},
|
|
83
|
+
typeProps = valueContext.type === "single" ? singleProps : void 0;
|
|
84
|
+
return /* @__PURE__ */jsx(Toggle, {
|
|
85
|
+
...typeProps,
|
|
86
|
+
...itemProps,
|
|
87
|
+
ref: forwardedRef,
|
|
88
|
+
onPressedChange: pressed => {
|
|
89
|
+
pressed ? valueContext.onItemActivate(value) : valueContext.onItemDeactivate(value);
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
}),
|
|
93
|
+
ToggleGroup = withStaticProperties(React.forwardRef((props, forwardedRef) => {
|
|
94
|
+
const {
|
|
95
|
+
type,
|
|
96
|
+
...toggleGroupProps
|
|
97
|
+
} = props;
|
|
98
|
+
if (isWeb || React.useEffect(() => {
|
|
99
|
+
if (props.id) return registerFocusable(props.id, {
|
|
100
|
+
// TODO: would be nice to focus on the first child later - could be done with reforest
|
|
101
|
+
// for now leaving it empty
|
|
102
|
+
focus: () => {}
|
|
103
|
+
});
|
|
104
|
+
}, [props.id]), type === "single") return /* @__PURE__ */jsx(ToggleGroupImplSingle, {
|
|
105
|
+
...toggleGroupProps,
|
|
106
|
+
ref: forwardedRef
|
|
107
|
+
});
|
|
108
|
+
if (type === "multiple") return /* @__PURE__ */jsx(ToggleGroupImplMultiple, {
|
|
109
|
+
...toggleGroupProps,
|
|
110
|
+
ref: forwardedRef
|
|
111
|
+
});
|
|
112
|
+
throw new Error(`Missing prop \`type\` expected on \`${TOGGLE_GROUP_NAME}\``);
|
|
113
|
+
}), {
|
|
114
|
+
Item: ToggleGroupItem
|
|
115
|
+
});
|
|
116
|
+
ToggleGroup.displayName = TOGGLE_GROUP_NAME;
|
|
117
|
+
const {
|
|
118
|
+
Provider: ToggleGroupValueProvider,
|
|
119
|
+
useStyledContext: useToggleGroupValueContext
|
|
120
|
+
} = createStyledContext(),
|
|
121
|
+
ToggleGroupImplSingle = React.forwardRef((props, forwardedRef) => {
|
|
122
|
+
const {
|
|
123
|
+
value: valueProp,
|
|
124
|
+
defaultValue,
|
|
125
|
+
onValueChange = () => {},
|
|
126
|
+
disableDeactivation = !1,
|
|
127
|
+
...toggleGroupSingleProps
|
|
128
|
+
} = props,
|
|
129
|
+
[value, setValue] = useControllableState({
|
|
130
|
+
prop: valueProp,
|
|
131
|
+
defaultProp: defaultValue,
|
|
132
|
+
onChange: onValueChange
|
|
133
|
+
});
|
|
134
|
+
return /* @__PURE__ */jsx(ToggleGroupValueProvider, {
|
|
135
|
+
scope: props.__scopeToggleGroup,
|
|
136
|
+
type: "single",
|
|
137
|
+
value: value ? [value] : [],
|
|
138
|
+
defaultValue: value,
|
|
139
|
+
onItemActivate: setValue,
|
|
140
|
+
onItemDeactivate: React.useCallback(() => disableDeactivation ? null : setValue(""), [setValue, disableDeactivation]),
|
|
141
|
+
children: /* @__PURE__ */jsx(ToggleGroupImpl, {
|
|
142
|
+
...toggleGroupSingleProps,
|
|
143
|
+
ref: forwardedRef
|
|
144
|
+
})
|
|
145
|
+
});
|
|
146
|
+
}),
|
|
147
|
+
ToggleGroupImplMultiple = React.forwardRef((props, forwardedRef) => {
|
|
148
|
+
const {
|
|
149
|
+
value: valueProp,
|
|
150
|
+
defaultValue,
|
|
151
|
+
onValueChange = () => {},
|
|
152
|
+
...toggleGroupMultipleProps
|
|
153
|
+
} = props,
|
|
154
|
+
[value = [], setValue] = useControllableState({
|
|
155
|
+
prop: valueProp,
|
|
156
|
+
defaultProp: defaultValue,
|
|
157
|
+
onChange: onValueChange
|
|
158
|
+
}),
|
|
159
|
+
handleButtonActivate = React.useCallback(itemValue => setValue((prevValue = []) => [...prevValue, itemValue]), [setValue]),
|
|
160
|
+
handleButtonDeactivate = React.useCallback(itemValue => setValue((prevValue = []) => prevValue.filter(value2 => value2 !== itemValue)), [setValue]);
|
|
161
|
+
return /* @__PURE__ */jsx(ToggleGroupValueProvider, {
|
|
162
|
+
scope: props.__scopeToggleGroup,
|
|
163
|
+
type: "multiple",
|
|
164
|
+
value,
|
|
165
|
+
defaultValue: value,
|
|
166
|
+
onItemActivate: handleButtonActivate,
|
|
167
|
+
onItemDeactivate: handleButtonDeactivate,
|
|
168
|
+
children: /* @__PURE__ */jsx(ToggleGroupImpl, {
|
|
169
|
+
...toggleGroupMultipleProps,
|
|
170
|
+
ref: forwardedRef
|
|
171
|
+
})
|
|
172
|
+
});
|
|
173
|
+
});
|
|
174
|
+
ToggleGroup.displayName = TOGGLE_GROUP_NAME;
|
|
175
|
+
const ToggleGroupImplElementFrame = styled(Group, {
|
|
176
|
+
name: TOGGLE_GROUP_NAME,
|
|
177
|
+
variants: {
|
|
178
|
+
unstyled: {
|
|
179
|
+
false: {
|
|
180
|
+
backgroundColor: "$background"
|
|
181
|
+
}
|
|
182
|
+
},
|
|
183
|
+
orientation: {
|
|
184
|
+
vertical: {
|
|
185
|
+
flexDirection: "column",
|
|
186
|
+
spaceDirection: "vertical"
|
|
187
|
+
},
|
|
188
|
+
horizontal: {
|
|
189
|
+
flexDirection: "row",
|
|
190
|
+
spaceDirection: "horizontal"
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
},
|
|
194
|
+
defaultVariants: {
|
|
195
|
+
unstyled: process.env.TAMAGUI_HEADLESS === "1"
|
|
196
|
+
}
|
|
197
|
+
}),
|
|
198
|
+
ToggleGroupImpl = ToggleGroupImplElementFrame.extractable(React.forwardRef((props, forwardedRef) => {
|
|
199
|
+
const {
|
|
200
|
+
__scopeToggleGroup,
|
|
201
|
+
disabled = !1,
|
|
202
|
+
orientation = "horizontal",
|
|
203
|
+
dir,
|
|
204
|
+
rovingFocus = !0,
|
|
205
|
+
loop = !0,
|
|
206
|
+
unstyled = !1,
|
|
207
|
+
size: sizeProp = "$true",
|
|
208
|
+
sizeAdjust = 0,
|
|
209
|
+
...toggleGroupProps
|
|
210
|
+
} = props,
|
|
211
|
+
direction = useDirection(dir),
|
|
212
|
+
commonProps = {
|
|
213
|
+
role: "group",
|
|
214
|
+
dir: direction,
|
|
215
|
+
...toggleGroupProps
|
|
216
|
+
},
|
|
217
|
+
adjustedSize = getVariableValue(getSize(sizeProp, {
|
|
218
|
+
shift: sizeAdjust
|
|
219
|
+
})),
|
|
220
|
+
size = Math.round(adjustedSize * 0.45);
|
|
221
|
+
return /* @__PURE__ */jsx(ToggleGroupContext, {
|
|
222
|
+
scope: __scopeToggleGroup,
|
|
223
|
+
rovingFocus,
|
|
224
|
+
disabled,
|
|
225
|
+
size,
|
|
226
|
+
children: rovingFocus ? /* @__PURE__ */jsx(RovingFocusGroup, {
|
|
227
|
+
asChild: "except-style",
|
|
228
|
+
__scopeRovingFocusGroup: __scopeToggleGroup || TOGGLE_GROUP_CONTEXT,
|
|
229
|
+
orientation,
|
|
230
|
+
dir: direction,
|
|
231
|
+
loop,
|
|
232
|
+
children: /* @__PURE__ */jsx(ToggleGroupImplElementFrame, {
|
|
233
|
+
"aria-orientation": orientation,
|
|
234
|
+
orientation,
|
|
235
|
+
axis: orientation,
|
|
236
|
+
ref: forwardedRef,
|
|
237
|
+
"data-disabled": disabled ? "" : void 0,
|
|
238
|
+
unstyled,
|
|
239
|
+
...commonProps
|
|
240
|
+
})
|
|
241
|
+
}) : /* @__PURE__ */jsx(ToggleGroupImplElementFrame, {
|
|
242
|
+
"aria-orientation": orientation,
|
|
243
|
+
ref: forwardedRef,
|
|
244
|
+
orientation,
|
|
245
|
+
"data-disabled": disabled ? "" : void 0,
|
|
246
|
+
unstyled,
|
|
247
|
+
...commonProps
|
|
248
|
+
})
|
|
249
|
+
});
|
|
250
|
+
}));
|
|
251
|
+
export { ToggleGroup };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./ToggleGroup.mjs";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tamagui/toggle-group",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.89.0-1706308641099",
|
|
4
4
|
"sideEffects": [
|
|
5
5
|
"*.css"
|
|
6
6
|
],
|
|
@@ -32,25 +32,25 @@
|
|
|
32
32
|
}
|
|
33
33
|
},
|
|
34
34
|
"dependencies": {
|
|
35
|
-
"@tamagui/constants": "1.
|
|
36
|
-
"@tamagui/create-context": "1.
|
|
37
|
-
"@tamagui/focusable": "1.
|
|
38
|
-
"@tamagui/font-size": "1.
|
|
39
|
-
"@tamagui/get-token": "1.
|
|
40
|
-
"@tamagui/group": "1.
|
|
41
|
-
"@tamagui/helpers": "1.
|
|
42
|
-
"@tamagui/helpers-tamagui": "1.
|
|
43
|
-
"@tamagui/roving-focus": "1.
|
|
44
|
-
"@tamagui/stacks": "1.
|
|
45
|
-
"@tamagui/use-controllable-state": "1.
|
|
46
|
-
"@tamagui/use-direction": "1.
|
|
47
|
-
"@tamagui/web": "1.
|
|
35
|
+
"@tamagui/constants": "1.89.0-1706308641099",
|
|
36
|
+
"@tamagui/create-context": "1.89.0-1706308641099",
|
|
37
|
+
"@tamagui/focusable": "1.89.0-1706308641099",
|
|
38
|
+
"@tamagui/font-size": "1.89.0-1706308641099",
|
|
39
|
+
"@tamagui/get-token": "1.89.0-1706308641099",
|
|
40
|
+
"@tamagui/group": "1.89.0-1706308641099",
|
|
41
|
+
"@tamagui/helpers": "1.89.0-1706308641099",
|
|
42
|
+
"@tamagui/helpers-tamagui": "1.89.0-1706308641099",
|
|
43
|
+
"@tamagui/roving-focus": "1.89.0-1706308641099",
|
|
44
|
+
"@tamagui/stacks": "1.89.0-1706308641099",
|
|
45
|
+
"@tamagui/use-controllable-state": "1.89.0-1706308641099",
|
|
46
|
+
"@tamagui/use-direction": "1.89.0-1706308641099",
|
|
47
|
+
"@tamagui/web": "1.89.0-1706308641099"
|
|
48
48
|
},
|
|
49
49
|
"peerDependencies": {
|
|
50
50
|
"react": "*"
|
|
51
51
|
},
|
|
52
52
|
"devDependencies": {
|
|
53
|
-
"@tamagui/build": "1.
|
|
53
|
+
"@tamagui/build": "1.89.0-1706308641099",
|
|
54
54
|
"react": "^18.2.0"
|
|
55
55
|
},
|
|
56
56
|
"publishConfig": {
|