@tamagui/switch 1.57.8 → 1.58.2
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/dist/cjs/Switch.js +19 -219
- package/dist/cjs/Switch.js.map +2 -2
- package/dist/cjs/createSwitch.js +257 -0
- package/dist/cjs/createSwitch.js.map +6 -0
- package/dist/cjs/index.js +3 -1
- package/dist/cjs/index.js.map +1 -1
- package/dist/esm/Switch.js +19 -215
- package/dist/esm/Switch.js.map +2 -2
- package/dist/esm/createSwitch.js +232 -0
- package/dist/esm/createSwitch.js.map +6 -0
- package/dist/esm/index.js +1 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/jsx/Switch.js +19 -194
- package/dist/jsx/Switch.js.map +2 -2
- package/dist/jsx/createSwitch.js +219 -0
- package/dist/jsx/createSwitch.js.map +6 -0
- package/dist/jsx/index.js +1 -0
- package/dist/jsx/index.js.map +1 -1
- package/package.json +11 -11
- package/src/Switch.tsx +20 -284
- package/src/createSwitch.tsx +304 -0
- package/src/index.ts +1 -0
- package/types/Switch.d.ts +46 -191
- package/types/Switch.d.ts.map +1 -1
- package/types/createSwitch.d.ts +41 -0
- package/types/createSwitch.d.ts.map +1 -0
- package/types/index.d.ts +1 -0
- package/types/index.d.ts.map +1 -1
package/dist/esm/Switch.js
CHANGED
|
@@ -1,33 +1,10 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { useComposedRefs } from "@tamagui/compose-refs";
|
|
3
|
-
import {
|
|
4
|
-
getVariableValue,
|
|
5
|
-
isWeb,
|
|
6
|
-
styled,
|
|
7
|
-
withStaticProperties
|
|
8
|
-
} from "@tamagui/core";
|
|
9
|
-
import { createContextScope } from "@tamagui/create-context";
|
|
10
|
-
import { registerFocusable } from "@tamagui/focusable";
|
|
1
|
+
import { getVariableValue, styled } from "@tamagui/core";
|
|
11
2
|
import { getSize } from "@tamagui/get-token";
|
|
12
|
-
import { useLabelContext } from "@tamagui/label";
|
|
13
3
|
import { ThemeableStack, XStack } from "@tamagui/stacks";
|
|
14
|
-
import {
|
|
15
|
-
|
|
16
|
-
import * as React from "react";
|
|
17
|
-
import {
|
|
18
|
-
Switch as NativeSwitch,
|
|
19
|
-
Platform
|
|
20
|
-
} from "react-native";
|
|
21
|
-
const SWITCH_NAME = "Switch";
|
|
22
|
-
const getSwitchHeight = (val) => Math.round(getVariableValue(getSize(val)) * 0.65);
|
|
23
|
-
const getSwitchWidth = (val) => getSwitchHeight(val) * 2;
|
|
24
|
-
const scopeContexts = createContextScope(SWITCH_NAME);
|
|
25
|
-
const [createSwitchContext] = scopeContexts;
|
|
26
|
-
const createSwitchScope = scopeContexts[1];
|
|
27
|
-
const [SwitchProvider, useSwitchContext] = createSwitchContext(SWITCH_NAME);
|
|
28
|
-
const THUMB_NAME = "SwitchThumb";
|
|
29
|
-
const SwitchThumbFrame = styled(ThemeableStack, {
|
|
4
|
+
import { SwitchContext, createSwitch } from "./createSwitch";
|
|
5
|
+
const SwitchThumb = styled(ThemeableStack, {
|
|
30
6
|
name: "SwitchThumb",
|
|
7
|
+
context: SwitchContext,
|
|
31
8
|
variants: {
|
|
32
9
|
unstyled: {
|
|
33
10
|
false: {
|
|
@@ -50,35 +27,12 @@ const SwitchThumbFrame = styled(ThemeableStack, {
|
|
|
50
27
|
unstyled: false
|
|
51
28
|
}
|
|
52
29
|
});
|
|
53
|
-
const
|
|
54
|
-
|
|
55
|
-
function SwitchThumb2(props, forwardedRef) {
|
|
56
|
-
const { __scopeSwitch, size: sizeProp, ...thumbProps } = props;
|
|
57
|
-
const {
|
|
58
|
-
size: sizeContext,
|
|
59
|
-
disabled,
|
|
60
|
-
checked,
|
|
61
|
-
unstyled
|
|
62
|
-
} = useSwitchContext(THUMB_NAME, __scopeSwitch);
|
|
63
|
-
const size = sizeProp ?? sizeContext;
|
|
64
|
-
return /* @__PURE__ */ jsx(
|
|
65
|
-
SwitchThumbFrame,
|
|
66
|
-
{
|
|
67
|
-
unstyled,
|
|
68
|
-
size,
|
|
69
|
-
"data-state": getState(checked),
|
|
70
|
-
"data-disabled": disabled ? "" : void 0,
|
|
71
|
-
x: checked ? getVariableValue(getSwitchWidth(size)) - getVariableValue(getSwitchHeight(size)) : 0,
|
|
72
|
-
...thumbProps,
|
|
73
|
-
ref: forwardedRef
|
|
74
|
-
}
|
|
75
|
-
);
|
|
76
|
-
}
|
|
77
|
-
)
|
|
78
|
-
);
|
|
30
|
+
const getSwitchHeight = (val) => Math.round(getVariableValue(getSize(val)) * 0.65);
|
|
31
|
+
const getSwitchWidth = (val) => getSwitchHeight(val) * 2;
|
|
79
32
|
const SwitchFrame = styled(XStack, {
|
|
80
|
-
name:
|
|
33
|
+
name: "Switch",
|
|
81
34
|
tag: "button",
|
|
35
|
+
context: SwitchContext,
|
|
82
36
|
variants: {
|
|
83
37
|
unstyled: {
|
|
84
38
|
false: {
|
|
@@ -95,6 +49,12 @@ const SwitchFrame = styled(XStack, {
|
|
|
95
49
|
}
|
|
96
50
|
}
|
|
97
51
|
},
|
|
52
|
+
checked: {
|
|
53
|
+
true: {}
|
|
54
|
+
},
|
|
55
|
+
frameWidth: {
|
|
56
|
+
":number": () => null
|
|
57
|
+
},
|
|
98
58
|
size: {
|
|
99
59
|
"...size": (val) => {
|
|
100
60
|
const height = getSwitchHeight(val) + 4;
|
|
@@ -111,170 +71,14 @@ const SwitchFrame = styled(XStack, {
|
|
|
111
71
|
unstyled: false
|
|
112
72
|
}
|
|
113
73
|
});
|
|
114
|
-
const
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
__scopeSwitch,
|
|
119
|
-
labeledBy: ariaLabelledby,
|
|
120
|
-
name,
|
|
121
|
-
checked: checkedProp,
|
|
122
|
-
defaultChecked,
|
|
123
|
-
required,
|
|
124
|
-
disabled,
|
|
125
|
-
value = "on",
|
|
126
|
-
onCheckedChange,
|
|
127
|
-
size = "$true",
|
|
128
|
-
unstyled = false,
|
|
129
|
-
native: nativeProp,
|
|
130
|
-
nativeProps,
|
|
131
|
-
...switchProps
|
|
132
|
-
} = props;
|
|
133
|
-
const native = Array.isArray(nativeProp) ? nativeProp : [nativeProp];
|
|
134
|
-
const shouldRenderMobileNative = !isWeb && nativeProp === true || !isWeb && native.includes("mobile") || native.includes("android") && Platform.OS === "android" || native.includes("ios") && Platform.OS === "ios";
|
|
135
|
-
if (shouldRenderMobileNative) {
|
|
136
|
-
return /* @__PURE__ */ jsx(
|
|
137
|
-
NativeSwitch,
|
|
138
|
-
{
|
|
139
|
-
value: checkedProp,
|
|
140
|
-
onValueChange: onCheckedChange,
|
|
141
|
-
...nativeProps
|
|
142
|
-
}
|
|
143
|
-
);
|
|
144
|
-
}
|
|
145
|
-
const [button, setButton] = React.useState(null);
|
|
146
|
-
const composedRefs = useComposedRefs(forwardedRef, (node) => setButton(node));
|
|
147
|
-
const labelId = useLabelContext(button);
|
|
148
|
-
const labelledBy = ariaLabelledby || labelId;
|
|
149
|
-
const hasConsumerStoppedPropagationRef = React.useRef(false);
|
|
150
|
-
const isFormControl = isWeb ? button ? Boolean(button.closest("form")) : true : false;
|
|
151
|
-
const [checked = false, setChecked] = useControllableState({
|
|
152
|
-
prop: checkedProp,
|
|
153
|
-
defaultProp: defaultChecked || false,
|
|
154
|
-
onChange: onCheckedChange,
|
|
155
|
-
transition: true
|
|
156
|
-
});
|
|
157
|
-
if (!isWeb) {
|
|
158
|
-
React.useEffect(() => {
|
|
159
|
-
if (!props.id)
|
|
160
|
-
return;
|
|
161
|
-
return registerFocusable(props.id, {
|
|
162
|
-
focus: () => {
|
|
163
|
-
setChecked((x) => !x);
|
|
164
|
-
}
|
|
165
|
-
});
|
|
166
|
-
}, [props.id, setChecked]);
|
|
167
|
-
}
|
|
168
|
-
return /* @__PURE__ */ jsxs(
|
|
169
|
-
SwitchProvider,
|
|
170
|
-
{
|
|
171
|
-
scope: __scopeSwitch,
|
|
172
|
-
checked,
|
|
173
|
-
disabled,
|
|
174
|
-
size,
|
|
175
|
-
unstyled,
|
|
176
|
-
children: [
|
|
177
|
-
/* @__PURE__ */ jsx(
|
|
178
|
-
SwitchFrame,
|
|
179
|
-
{
|
|
180
|
-
unstyled,
|
|
181
|
-
size,
|
|
182
|
-
theme: checked ? "active" : null,
|
|
183
|
-
role: "switch",
|
|
184
|
-
"aria-checked": checked,
|
|
185
|
-
"aria-labelledby": labelledBy,
|
|
186
|
-
"aria-required": required,
|
|
187
|
-
"data-state": getState(checked),
|
|
188
|
-
"data-disabled": disabled ? "" : void 0,
|
|
189
|
-
disabled,
|
|
190
|
-
tabIndex: disabled ? void 0 : 0,
|
|
191
|
-
value,
|
|
192
|
-
...switchProps,
|
|
193
|
-
ref: composedRefs,
|
|
194
|
-
onPress: (event) => {
|
|
195
|
-
var _a;
|
|
196
|
-
(_a = props.onPress) == null ? void 0 : _a.call(props, event);
|
|
197
|
-
setChecked((prevChecked) => !prevChecked);
|
|
198
|
-
if (isWeb && isFormControl) {
|
|
199
|
-
hasConsumerStoppedPropagationRef.current = event.isPropagationStopped();
|
|
200
|
-
if (!hasConsumerStoppedPropagationRef.current)
|
|
201
|
-
event.stopPropagation();
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
),
|
|
206
|
-
isWeb && isFormControl && /* @__PURE__ */ jsx(
|
|
207
|
-
BubbleInput,
|
|
208
|
-
{
|
|
209
|
-
control: button,
|
|
210
|
-
bubbles: !hasConsumerStoppedPropagationRef.current,
|
|
211
|
-
name,
|
|
212
|
-
value,
|
|
213
|
-
checked,
|
|
214
|
-
required,
|
|
215
|
-
disabled,
|
|
216
|
-
style: { transform: "translateX(-100%)" }
|
|
217
|
-
}
|
|
218
|
-
)
|
|
219
|
-
]
|
|
220
|
-
}
|
|
221
|
-
);
|
|
222
|
-
}
|
|
223
|
-
),
|
|
224
|
-
{
|
|
225
|
-
// because they may set a variant to be checked, we need to make it also pass checked down
|
|
226
|
-
inlineProps: /* @__PURE__ */ new Set(["checked"])
|
|
227
|
-
}
|
|
228
|
-
);
|
|
229
|
-
const Switch = withStaticProperties(SwitchComponent, {
|
|
230
|
-
Thumb: SwitchThumb
|
|
74
|
+
const Switch = createSwitch({
|
|
75
|
+
Frame: SwitchFrame,
|
|
76
|
+
Thumb: SwitchThumb,
|
|
77
|
+
acceptsUnstyled: true
|
|
231
78
|
});
|
|
232
|
-
const BubbleInput = (props) => {
|
|
233
|
-
const { control, checked, bubbles = true, ...inputProps } = props;
|
|
234
|
-
const ref = React.useRef(null);
|
|
235
|
-
const prevChecked = usePrevious(checked);
|
|
236
|
-
React.useEffect(() => {
|
|
237
|
-
const input = ref.current;
|
|
238
|
-
const inputProto = window.HTMLInputElement.prototype;
|
|
239
|
-
const descriptor = Object.getOwnPropertyDescriptor(
|
|
240
|
-
inputProto,
|
|
241
|
-
"checked"
|
|
242
|
-
);
|
|
243
|
-
const setChecked = descriptor.set;
|
|
244
|
-
if (prevChecked !== checked && setChecked) {
|
|
245
|
-
const event = new Event("click", { bubbles });
|
|
246
|
-
setChecked.call(input, checked);
|
|
247
|
-
input.dispatchEvent(event);
|
|
248
|
-
}
|
|
249
|
-
}, [prevChecked, checked, bubbles]);
|
|
250
|
-
return /* @__PURE__ */ jsx(
|
|
251
|
-
"input",
|
|
252
|
-
{
|
|
253
|
-
type: "checkbox",
|
|
254
|
-
"aria-hidden": true,
|
|
255
|
-
defaultChecked: checked,
|
|
256
|
-
...inputProps,
|
|
257
|
-
tabIndex: -1,
|
|
258
|
-
ref,
|
|
259
|
-
style: {
|
|
260
|
-
...props.style,
|
|
261
|
-
// ...controlSize,
|
|
262
|
-
position: "absolute",
|
|
263
|
-
pointerEvents: "none",
|
|
264
|
-
opacity: 0,
|
|
265
|
-
margin: 0
|
|
266
|
-
}
|
|
267
|
-
}
|
|
268
|
-
);
|
|
269
|
-
};
|
|
270
|
-
function getState(checked) {
|
|
271
|
-
return checked ? "checked" : "unchecked";
|
|
272
|
-
}
|
|
273
79
|
export {
|
|
274
80
|
Switch,
|
|
275
81
|
SwitchFrame,
|
|
276
|
-
SwitchThumb
|
|
277
|
-
SwitchThumbFrame,
|
|
278
|
-
createSwitchScope
|
|
82
|
+
SwitchThumb
|
|
279
83
|
};
|
|
280
84
|
//# sourceMappingURL=Switch.js.map
|
package/dist/esm/Switch.js.map
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/Switch.tsx"],
|
|
4
|
-
"mappings": "
|
|
5
|
-
"names": [
|
|
4
|
+
"mappings": "AAAA,SAAqB,kBAAkB,cAAc;AACrD,SAAS,eAAe;AACxB,SAAS,gBAAgB,cAAc;AAEvC,SAAS,eAAe,oBAAoB;AAErC,MAAM,cAAc,OAAO,gBAAgB;AAAA,EAChD,MAAM;AAAA,EACN,SAAS;AAAA,EAET,UAAU;AAAA,IACR,UAAU;AAAA,MACR,OAAO;AAAA,QACL,MAAM;AAAA,QACN,iBAAiB;AAAA,QACjB,cAAc;AAAA,MAChB;AAAA,IACF;AAAA,IAEA,MAAM;AAAA,MACJ,WAAW,CAAC,QAAQ;AAClB,cAAM,OAAO,gBAAgB,GAAG;AAChC,eAAO;AAAA,UACL,QAAQ;AAAA,UACR,OAAO;AAAA,QACT;AAAA,MACF;AAAA,IACF;AAAA,EACF;AAAA,EAEA,iBAAiB;AAAA,IACf,UAAU;AAAA,EACZ;AACF,CAAC;AAED,MAAM,kBAAkB,CAAC,QACvB,KAAK,MAAM,iBAAiB,QAAQ,GAAG,CAAC,IAAI,IAAI;AAElD,MAAM,iBAAiB,CAAC,QAAoB,gBAAgB,GAAG,IAAI;AAE5D,MAAM,cAAc,OAAO,QAAQ;AAAA,EACxC,MAAM;AAAA,EACN,KAAK;AAAA,EACL,SAAS;AAAA,EAET,UAAU;AAAA,IACR,UAAU;AAAA,MACR,OAAO;AAAA,QACL,MAAM;AAAA,QACN,cAAc;AAAA,QACd,aAAa;AAAA,QACb,aAAa;AAAA,QACb,iBAAiB;AAAA,QAEjB,YAAY;AAAA,UACV,aAAa;AAAA,UACb,cAAc;AAAA,UACd,cAAc;AAAA,UACd,cAAc;AAAA,QAChB;AAAA,MACF;AAAA,IACF;AAAA,IAEA,SAAS;AAAA,MACP,MAAM,CAAC;AAAA,IACT;AAAA,IAEA,YAAY;AAAA,MACV,WAAW,MAAM;AAAA,IACnB;AAAA,IAEA,MAAM;AAAA,MACJ,WAAW,CAAC,QAAQ;AAClB,cAAM,SAAS,gBAAgB,GAAG,IAAI;AACtC,cAAM,QAAQ,eAAe,GAAG,IAAI;AACpC,eAAO;AAAA,UACL;AAAA,UACA,WAAW;AAAA,UACX;AAAA,QACF;AAAA,MACF;AAAA,IACF;AAAA,EACF;AAAA,EAEA,iBAAiB;AAAA,IACf,UAAU;AAAA,EACZ;AACF,CAAC;AAEM,MAAM,SAAS,aAAa;AAAA,EACjC,OAAO;AAAA,EACP,OAAO;AAAA,EACP,iBAAiB;AACnB,CAAC;",
|
|
5
|
+
"names": []
|
|
6
6
|
}
|
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { useComposedRefs } from "@tamagui/compose-refs";
|
|
3
|
+
import {
|
|
4
|
+
composeEventHandlers,
|
|
5
|
+
createStyledContext,
|
|
6
|
+
getVariableValue,
|
|
7
|
+
isWeb,
|
|
8
|
+
useProps,
|
|
9
|
+
withStaticProperties
|
|
10
|
+
} from "@tamagui/core";
|
|
11
|
+
import { registerFocusable } from "@tamagui/focusable";
|
|
12
|
+
import { getSize } from "@tamagui/get-token";
|
|
13
|
+
import { useLabelContext } from "@tamagui/label";
|
|
14
|
+
import { useControllableState } from "@tamagui/use-controllable-state";
|
|
15
|
+
import { usePrevious } from "@tamagui/use-previous";
|
|
16
|
+
import * as React from "react";
|
|
17
|
+
import {
|
|
18
|
+
Switch as NativeSwitch,
|
|
19
|
+
Platform
|
|
20
|
+
} from "react-native";
|
|
21
|
+
const SwitchContext = createStyledContext({
|
|
22
|
+
checked: false,
|
|
23
|
+
disabled: false,
|
|
24
|
+
size: void 0,
|
|
25
|
+
frameWidth: 60,
|
|
26
|
+
unstyled: false
|
|
27
|
+
});
|
|
28
|
+
function createSwitch({ Frame, Thumb, acceptsUnstyled }) {
|
|
29
|
+
const SwitchThumb = Thumb.styleable(function SwitchThumb2(props, forwardedRef) {
|
|
30
|
+
const { size: sizeProp, ...thumbProps } = props;
|
|
31
|
+
const { disabled, checked, unstyled, frameWidth } = React.useContext(SwitchContext);
|
|
32
|
+
const [thumbWidth, setThumbWidth] = React.useState(0);
|
|
33
|
+
return (
|
|
34
|
+
// @ts-ignore
|
|
35
|
+
/* @__PURE__ */ jsx(
|
|
36
|
+
Thumb,
|
|
37
|
+
{
|
|
38
|
+
theme: unstyled === false && checked ? "active" : null,
|
|
39
|
+
"data-state": getState(checked),
|
|
40
|
+
"data-disabled": disabled ? "" : void 0,
|
|
41
|
+
x: checked ? frameWidth - thumbWidth : 0,
|
|
42
|
+
...thumbProps,
|
|
43
|
+
onLayout: composeEventHandlers(
|
|
44
|
+
props.onLayout,
|
|
45
|
+
(e) => (
|
|
46
|
+
// @ts-ignore
|
|
47
|
+
setThumbWidth(e.nativeEvent.layout.width)
|
|
48
|
+
)
|
|
49
|
+
),
|
|
50
|
+
ref: forwardedRef
|
|
51
|
+
}
|
|
52
|
+
)
|
|
53
|
+
);
|
|
54
|
+
});
|
|
55
|
+
const SwitchComponent = Frame.extractable(
|
|
56
|
+
React.forwardRef(function SwitchFrame(propsIn, forwardedRef) {
|
|
57
|
+
const styledContext = React.useContext(SwitchContext);
|
|
58
|
+
const props = useProps(propsIn);
|
|
59
|
+
const {
|
|
60
|
+
labeledBy: ariaLabelledby,
|
|
61
|
+
name,
|
|
62
|
+
checked: checkedProp,
|
|
63
|
+
defaultChecked,
|
|
64
|
+
required,
|
|
65
|
+
disabled,
|
|
66
|
+
value = "on",
|
|
67
|
+
onCheckedChange,
|
|
68
|
+
size = styledContext.size ?? "$true",
|
|
69
|
+
unstyled = styledContext.unstyled ?? false,
|
|
70
|
+
native: nativeProp,
|
|
71
|
+
nativeProps,
|
|
72
|
+
...switchProps
|
|
73
|
+
} = props;
|
|
74
|
+
const leftBorderWidth = (() => {
|
|
75
|
+
let _ = void 0;
|
|
76
|
+
for (const key in switchProps) {
|
|
77
|
+
if (key === "borderWidth" || key === "borderLeftWidth") {
|
|
78
|
+
_ = switchProps[key];
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
if (acceptsUnstyled && _ === void 0 && unstyled === false) {
|
|
82
|
+
_ = 2;
|
|
83
|
+
}
|
|
84
|
+
if (typeof _ === "string") {
|
|
85
|
+
_ = getVariableValue(getSize(_));
|
|
86
|
+
}
|
|
87
|
+
return +_;
|
|
88
|
+
})();
|
|
89
|
+
const native = Array.isArray(nativeProp) ? nativeProp : [nativeProp];
|
|
90
|
+
const shouldRenderMobileNative = !isWeb && nativeProp === true || !isWeb && native.includes("mobile") || native.includes("android") && Platform.OS === "android" || native.includes("ios") && Platform.OS === "ios";
|
|
91
|
+
const [button, setButton] = React.useState(null);
|
|
92
|
+
const composedRefs = useComposedRefs(forwardedRef, (node) => setButton(node));
|
|
93
|
+
const labelId = useLabelContext(button);
|
|
94
|
+
const labelledBy = ariaLabelledby || labelId;
|
|
95
|
+
const hasConsumerStoppedPropagationRef = React.useRef(false);
|
|
96
|
+
const isFormControl = isWeb ? button ? Boolean(button.closest("form")) : true : false;
|
|
97
|
+
const [frameWidth, setFrameWidth] = React.useState(60);
|
|
98
|
+
const [checked = false, setChecked] = useControllableState({
|
|
99
|
+
prop: checkedProp,
|
|
100
|
+
defaultProp: defaultChecked || false,
|
|
101
|
+
onChange: onCheckedChange,
|
|
102
|
+
transition: true
|
|
103
|
+
});
|
|
104
|
+
if (shouldRenderMobileNative) {
|
|
105
|
+
return /* @__PURE__ */ jsx(
|
|
106
|
+
NativeSwitch,
|
|
107
|
+
{
|
|
108
|
+
value: checkedProp,
|
|
109
|
+
onValueChange: onCheckedChange,
|
|
110
|
+
...nativeProps
|
|
111
|
+
}
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
if (!isWeb) {
|
|
115
|
+
React.useEffect(() => {
|
|
116
|
+
if (!props.id)
|
|
117
|
+
return;
|
|
118
|
+
return registerFocusable(props.id, {
|
|
119
|
+
focus: () => {
|
|
120
|
+
setChecked((x) => !x);
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
}, [props.id, setChecked]);
|
|
124
|
+
}
|
|
125
|
+
return /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
126
|
+
/* @__PURE__ */ jsx(
|
|
127
|
+
Frame,
|
|
128
|
+
{
|
|
129
|
+
unstyled,
|
|
130
|
+
size,
|
|
131
|
+
checked,
|
|
132
|
+
disabled,
|
|
133
|
+
frameWidth: frameWidth - leftBorderWidth * 2,
|
|
134
|
+
theme: checked ? "active" : null,
|
|
135
|
+
themeShallow: true,
|
|
136
|
+
role: "switch",
|
|
137
|
+
"aria-checked": checked,
|
|
138
|
+
"aria-labelledby": labelledBy,
|
|
139
|
+
"aria-required": required,
|
|
140
|
+
"data-state": getState(checked),
|
|
141
|
+
"data-disabled": disabled ? "" : void 0,
|
|
142
|
+
tabIndex: disabled ? void 0 : 0,
|
|
143
|
+
value,
|
|
144
|
+
...switchProps,
|
|
145
|
+
ref: composedRefs,
|
|
146
|
+
onPress: composeEventHandlers(props.onPress, (event) => {
|
|
147
|
+
setChecked((prevChecked) => !prevChecked);
|
|
148
|
+
if (isWeb && isFormControl) {
|
|
149
|
+
hasConsumerStoppedPropagationRef.current = event.isPropagationStopped();
|
|
150
|
+
if (!hasConsumerStoppedPropagationRef.current)
|
|
151
|
+
event.stopPropagation();
|
|
152
|
+
}
|
|
153
|
+
}),
|
|
154
|
+
onLayout: composeEventHandlers(
|
|
155
|
+
props.onLayout,
|
|
156
|
+
(e) => (
|
|
157
|
+
// @ts-ignore
|
|
158
|
+
setFrameWidth(e.nativeEvent.layout.width)
|
|
159
|
+
)
|
|
160
|
+
)
|
|
161
|
+
}
|
|
162
|
+
),
|
|
163
|
+
isWeb && isFormControl && /* @__PURE__ */ jsx(
|
|
164
|
+
BubbleInput,
|
|
165
|
+
{
|
|
166
|
+
control: button,
|
|
167
|
+
bubbles: !hasConsumerStoppedPropagationRef.current,
|
|
168
|
+
name,
|
|
169
|
+
value,
|
|
170
|
+
checked,
|
|
171
|
+
required,
|
|
172
|
+
disabled,
|
|
173
|
+
style: { transform: "translateX(-100%)" }
|
|
174
|
+
}
|
|
175
|
+
)
|
|
176
|
+
] });
|
|
177
|
+
})
|
|
178
|
+
);
|
|
179
|
+
const BubbleInput = (props) => {
|
|
180
|
+
const { control, checked, bubbles = true, ...inputProps } = props;
|
|
181
|
+
const ref = React.useRef(null);
|
|
182
|
+
const prevChecked = usePrevious(checked);
|
|
183
|
+
React.useEffect(() => {
|
|
184
|
+
const input = ref.current;
|
|
185
|
+
const inputProto = window.HTMLInputElement.prototype;
|
|
186
|
+
const descriptor = Object.getOwnPropertyDescriptor(
|
|
187
|
+
inputProto,
|
|
188
|
+
"checked"
|
|
189
|
+
);
|
|
190
|
+
const setChecked = descriptor.set;
|
|
191
|
+
if (prevChecked !== checked && setChecked) {
|
|
192
|
+
const event = new Event("click", { bubbles });
|
|
193
|
+
setChecked.call(input, checked);
|
|
194
|
+
input.dispatchEvent(event);
|
|
195
|
+
}
|
|
196
|
+
}, [prevChecked, checked, bubbles]);
|
|
197
|
+
return (
|
|
198
|
+
// @ts-ignore
|
|
199
|
+
/* @__PURE__ */ jsx(
|
|
200
|
+
"input",
|
|
201
|
+
{
|
|
202
|
+
type: "checkbox",
|
|
203
|
+
"aria-hidden": true,
|
|
204
|
+
defaultChecked: checked,
|
|
205
|
+
...inputProps,
|
|
206
|
+
tabIndex: -1,
|
|
207
|
+
ref,
|
|
208
|
+
style: {
|
|
209
|
+
...props.style,
|
|
210
|
+
// ...controlSize,
|
|
211
|
+
position: "absolute",
|
|
212
|
+
pointerEvents: "none",
|
|
213
|
+
opacity: 0,
|
|
214
|
+
margin: 0
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
)
|
|
218
|
+
);
|
|
219
|
+
};
|
|
220
|
+
function getState(checked) {
|
|
221
|
+
return checked ? "checked" : "unchecked";
|
|
222
|
+
}
|
|
223
|
+
const Switch = withStaticProperties(SwitchComponent, {
|
|
224
|
+
Thumb: SwitchThumb
|
|
225
|
+
});
|
|
226
|
+
return Switch;
|
|
227
|
+
}
|
|
228
|
+
export {
|
|
229
|
+
SwitchContext,
|
|
230
|
+
createSwitch
|
|
231
|
+
};
|
|
232
|
+
//# sourceMappingURL=createSwitch.js.map
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/createSwitch.tsx"],
|
|
4
|
+
"mappings": "AA0EM,SA4GE,UA5GF,KA4GE,YA5GF;AA1EN,SAAS,uBAAuB;AAChC;AAAA,EAME;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,SAAS,yBAAyB;AAClC,SAAS,eAAe;AACxB,SAAS,uBAAuB;AAChC,SAAS,4BAA4B;AACrC,SAAS,mBAAmB;AAC5B,YAAY,WAAW;AACvB;AAAA,EACE,UAAU;AAAA,EAEV;AAAA,OACK;AAEA,MAAM,gBAAgB,oBAM1B;AAAA,EACD,SAAS;AAAA,EACT,UAAU;AAAA,EACV,MAAM;AAAA,EACN,YAAY;AAAA,EACZ,UAAU;AACZ,CAAC;AAuBM,SAAS,aAMd,EAAE,OAAO,OAAO,gBAAgB,GAAsD;AACtF,QAAM,cAAc,MAAM,UAAU,SAASA,aAAY,OAAO,cAAc;AAC5E,UAAM,EAAE,MAAM,UAAU,GAAG,WAAW,IAAI;AAC1C,UAAM,EAAE,UAAU,SAAS,UAAU,WAAW,IAAI,MAAM,WAAW,aAAa;AAClF,UAAM,CAAC,YAAY,aAAa,IAAI,MAAM,SAAS,CAAC;AACpD;AAAA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