@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/cjs/Switch.js
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __create = Object.create;
|
|
3
2
|
var __defProp = Object.defineProperty;
|
|
4
3
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
4
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
7
5
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
6
|
var __export = (target, all) => {
|
|
9
7
|
for (var name in all)
|
|
@@ -17,46 +15,21 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
17
15
|
}
|
|
18
16
|
return to;
|
|
19
17
|
};
|
|
20
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
-
mod
|
|
27
|
-
));
|
|
28
18
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
19
|
var Switch_exports = {};
|
|
30
20
|
__export(Switch_exports, {
|
|
31
21
|
Switch: () => Switch,
|
|
32
22
|
SwitchFrame: () => SwitchFrame,
|
|
33
|
-
SwitchThumb: () => SwitchThumb
|
|
34
|
-
SwitchThumbFrame: () => SwitchThumbFrame,
|
|
35
|
-
createSwitchScope: () => createSwitchScope
|
|
23
|
+
SwitchThumb: () => SwitchThumb
|
|
36
24
|
});
|
|
37
25
|
module.exports = __toCommonJS(Switch_exports);
|
|
38
|
-
var import_jsx_runtime = require("react/jsx-runtime");
|
|
39
|
-
var import_compose_refs = require("@tamagui/compose-refs");
|
|
40
26
|
var import_core = require("@tamagui/core");
|
|
41
|
-
var import_create_context = require("@tamagui/create-context");
|
|
42
|
-
var import_focusable = require("@tamagui/focusable");
|
|
43
27
|
var import_get_token = require("@tamagui/get-token");
|
|
44
|
-
var import_label = require("@tamagui/label");
|
|
45
28
|
var import_stacks = require("@tamagui/stacks");
|
|
46
|
-
var
|
|
47
|
-
|
|
48
|
-
var React = __toESM(require("react"));
|
|
49
|
-
var import_react_native = require("react-native");
|
|
50
|
-
const SWITCH_NAME = "Switch";
|
|
51
|
-
const getSwitchHeight = (val) => Math.round((0, import_core.getVariableValue)((0, import_get_token.getSize)(val)) * 0.65);
|
|
52
|
-
const getSwitchWidth = (val) => getSwitchHeight(val) * 2;
|
|
53
|
-
const scopeContexts = (0, import_create_context.createContextScope)(SWITCH_NAME);
|
|
54
|
-
const [createSwitchContext] = scopeContexts;
|
|
55
|
-
const createSwitchScope = scopeContexts[1];
|
|
56
|
-
const [SwitchProvider, useSwitchContext] = createSwitchContext(SWITCH_NAME);
|
|
57
|
-
const THUMB_NAME = "SwitchThumb";
|
|
58
|
-
const SwitchThumbFrame = (0, import_core.styled)(import_stacks.ThemeableStack, {
|
|
29
|
+
var import_createSwitch = require("./createSwitch");
|
|
30
|
+
const SwitchThumb = (0, import_core.styled)(import_stacks.ThemeableStack, {
|
|
59
31
|
name: "SwitchThumb",
|
|
32
|
+
context: import_createSwitch.SwitchContext,
|
|
60
33
|
variants: {
|
|
61
34
|
unstyled: {
|
|
62
35
|
false: {
|
|
@@ -79,35 +52,12 @@ const SwitchThumbFrame = (0, import_core.styled)(import_stacks.ThemeableStack, {
|
|
|
79
52
|
unstyled: false
|
|
80
53
|
}
|
|
81
54
|
});
|
|
82
|
-
const
|
|
83
|
-
|
|
84
|
-
function SwitchThumb2(props, forwardedRef) {
|
|
85
|
-
const { __scopeSwitch, size: sizeProp, ...thumbProps } = props;
|
|
86
|
-
const {
|
|
87
|
-
size: sizeContext,
|
|
88
|
-
disabled,
|
|
89
|
-
checked,
|
|
90
|
-
unstyled
|
|
91
|
-
} = useSwitchContext(THUMB_NAME, __scopeSwitch);
|
|
92
|
-
const size = sizeProp ?? sizeContext;
|
|
93
|
-
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
94
|
-
SwitchThumbFrame,
|
|
95
|
-
{
|
|
96
|
-
unstyled,
|
|
97
|
-
size,
|
|
98
|
-
"data-state": getState(checked),
|
|
99
|
-
"data-disabled": disabled ? "" : void 0,
|
|
100
|
-
x: checked ? (0, import_core.getVariableValue)(getSwitchWidth(size)) - (0, import_core.getVariableValue)(getSwitchHeight(size)) : 0,
|
|
101
|
-
...thumbProps,
|
|
102
|
-
ref: forwardedRef
|
|
103
|
-
}
|
|
104
|
-
);
|
|
105
|
-
}
|
|
106
|
-
)
|
|
107
|
-
);
|
|
55
|
+
const getSwitchHeight = (val) => Math.round((0, import_core.getVariableValue)((0, import_get_token.getSize)(val)) * 0.65);
|
|
56
|
+
const getSwitchWidth = (val) => getSwitchHeight(val) * 2;
|
|
108
57
|
const SwitchFrame = (0, import_core.styled)(import_stacks.XStack, {
|
|
109
|
-
name:
|
|
58
|
+
name: "Switch",
|
|
110
59
|
tag: "button",
|
|
60
|
+
context: import_createSwitch.SwitchContext,
|
|
111
61
|
variants: {
|
|
112
62
|
unstyled: {
|
|
113
63
|
false: {
|
|
@@ -124,6 +74,12 @@ const SwitchFrame = (0, import_core.styled)(import_stacks.XStack, {
|
|
|
124
74
|
}
|
|
125
75
|
}
|
|
126
76
|
},
|
|
77
|
+
checked: {
|
|
78
|
+
true: {}
|
|
79
|
+
},
|
|
80
|
+
frameWidth: {
|
|
81
|
+
":number": () => null
|
|
82
|
+
},
|
|
127
83
|
size: {
|
|
128
84
|
"...size": (val) => {
|
|
129
85
|
const height = getSwitchHeight(val) + 4;
|
|
@@ -140,171 +96,15 @@ const SwitchFrame = (0, import_core.styled)(import_stacks.XStack, {
|
|
|
140
96
|
unstyled: false
|
|
141
97
|
}
|
|
142
98
|
});
|
|
143
|
-
const
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
__scopeSwitch,
|
|
148
|
-
labeledBy: ariaLabelledby,
|
|
149
|
-
name,
|
|
150
|
-
checked: checkedProp,
|
|
151
|
-
defaultChecked,
|
|
152
|
-
required,
|
|
153
|
-
disabled,
|
|
154
|
-
value = "on",
|
|
155
|
-
onCheckedChange,
|
|
156
|
-
size = "$true",
|
|
157
|
-
unstyled = false,
|
|
158
|
-
native: nativeProp,
|
|
159
|
-
nativeProps,
|
|
160
|
-
...switchProps
|
|
161
|
-
} = props;
|
|
162
|
-
const native = Array.isArray(nativeProp) ? nativeProp : [nativeProp];
|
|
163
|
-
const shouldRenderMobileNative = !import_core.isWeb && nativeProp === true || !import_core.isWeb && native.includes("mobile") || native.includes("android") && import_react_native.Platform.OS === "android" || native.includes("ios") && import_react_native.Platform.OS === "ios";
|
|
164
|
-
if (shouldRenderMobileNative) {
|
|
165
|
-
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
166
|
-
import_react_native.Switch,
|
|
167
|
-
{
|
|
168
|
-
value: checkedProp,
|
|
169
|
-
onValueChange: onCheckedChange,
|
|
170
|
-
...nativeProps
|
|
171
|
-
}
|
|
172
|
-
);
|
|
173
|
-
}
|
|
174
|
-
const [button, setButton] = React.useState(null);
|
|
175
|
-
const composedRefs = (0, import_compose_refs.useComposedRefs)(forwardedRef, (node) => setButton(node));
|
|
176
|
-
const labelId = (0, import_label.useLabelContext)(button);
|
|
177
|
-
const labelledBy = ariaLabelledby || labelId;
|
|
178
|
-
const hasConsumerStoppedPropagationRef = React.useRef(false);
|
|
179
|
-
const isFormControl = import_core.isWeb ? button ? Boolean(button.closest("form")) : true : false;
|
|
180
|
-
const [checked = false, setChecked] = (0, import_use_controllable_state.useControllableState)({
|
|
181
|
-
prop: checkedProp,
|
|
182
|
-
defaultProp: defaultChecked || false,
|
|
183
|
-
onChange: onCheckedChange,
|
|
184
|
-
transition: true
|
|
185
|
-
});
|
|
186
|
-
if (!import_core.isWeb) {
|
|
187
|
-
React.useEffect(() => {
|
|
188
|
-
if (!props.id)
|
|
189
|
-
return;
|
|
190
|
-
return (0, import_focusable.registerFocusable)(props.id, {
|
|
191
|
-
focus: () => {
|
|
192
|
-
setChecked((x) => !x);
|
|
193
|
-
}
|
|
194
|
-
});
|
|
195
|
-
}, [props.id, setChecked]);
|
|
196
|
-
}
|
|
197
|
-
return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
|
|
198
|
-
SwitchProvider,
|
|
199
|
-
{
|
|
200
|
-
scope: __scopeSwitch,
|
|
201
|
-
checked,
|
|
202
|
-
disabled,
|
|
203
|
-
size,
|
|
204
|
-
unstyled,
|
|
205
|
-
children: [
|
|
206
|
-
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
207
|
-
SwitchFrame,
|
|
208
|
-
{
|
|
209
|
-
unstyled,
|
|
210
|
-
size,
|
|
211
|
-
theme: checked ? "active" : null,
|
|
212
|
-
role: "switch",
|
|
213
|
-
"aria-checked": checked,
|
|
214
|
-
"aria-labelledby": labelledBy,
|
|
215
|
-
"aria-required": required,
|
|
216
|
-
"data-state": getState(checked),
|
|
217
|
-
"data-disabled": disabled ? "" : void 0,
|
|
218
|
-
disabled,
|
|
219
|
-
tabIndex: disabled ? void 0 : 0,
|
|
220
|
-
value,
|
|
221
|
-
...switchProps,
|
|
222
|
-
ref: composedRefs,
|
|
223
|
-
onPress: (event) => {
|
|
224
|
-
var _a;
|
|
225
|
-
(_a = props.onPress) == null ? void 0 : _a.call(props, event);
|
|
226
|
-
setChecked((prevChecked) => !prevChecked);
|
|
227
|
-
if (import_core.isWeb && isFormControl) {
|
|
228
|
-
hasConsumerStoppedPropagationRef.current = event.isPropagationStopped();
|
|
229
|
-
if (!hasConsumerStoppedPropagationRef.current)
|
|
230
|
-
event.stopPropagation();
|
|
231
|
-
}
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
),
|
|
235
|
-
import_core.isWeb && isFormControl && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
236
|
-
BubbleInput,
|
|
237
|
-
{
|
|
238
|
-
control: button,
|
|
239
|
-
bubbles: !hasConsumerStoppedPropagationRef.current,
|
|
240
|
-
name,
|
|
241
|
-
value,
|
|
242
|
-
checked,
|
|
243
|
-
required,
|
|
244
|
-
disabled,
|
|
245
|
-
style: { transform: "translateX(-100%)" }
|
|
246
|
-
}
|
|
247
|
-
)
|
|
248
|
-
]
|
|
249
|
-
}
|
|
250
|
-
);
|
|
251
|
-
}
|
|
252
|
-
),
|
|
253
|
-
{
|
|
254
|
-
// because they may set a variant to be checked, we need to make it also pass checked down
|
|
255
|
-
inlineProps: /* @__PURE__ */ new Set(["checked"])
|
|
256
|
-
}
|
|
257
|
-
);
|
|
258
|
-
const Switch = (0, import_core.withStaticProperties)(SwitchComponent, {
|
|
259
|
-
Thumb: SwitchThumb
|
|
99
|
+
const Switch = (0, import_createSwitch.createSwitch)({
|
|
100
|
+
Frame: SwitchFrame,
|
|
101
|
+
Thumb: SwitchThumb,
|
|
102
|
+
acceptsUnstyled: true
|
|
260
103
|
});
|
|
261
|
-
const BubbleInput = (props) => {
|
|
262
|
-
const { control, checked, bubbles = true, ...inputProps } = props;
|
|
263
|
-
const ref = React.useRef(null);
|
|
264
|
-
const prevChecked = (0, import_use_previous.usePrevious)(checked);
|
|
265
|
-
React.useEffect(() => {
|
|
266
|
-
const input = ref.current;
|
|
267
|
-
const inputProto = window.HTMLInputElement.prototype;
|
|
268
|
-
const descriptor = Object.getOwnPropertyDescriptor(
|
|
269
|
-
inputProto,
|
|
270
|
-
"checked"
|
|
271
|
-
);
|
|
272
|
-
const setChecked = descriptor.set;
|
|
273
|
-
if (prevChecked !== checked && setChecked) {
|
|
274
|
-
const event = new Event("click", { bubbles });
|
|
275
|
-
setChecked.call(input, checked);
|
|
276
|
-
input.dispatchEvent(event);
|
|
277
|
-
}
|
|
278
|
-
}, [prevChecked, checked, bubbles]);
|
|
279
|
-
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
280
|
-
"input",
|
|
281
|
-
{
|
|
282
|
-
type: "checkbox",
|
|
283
|
-
"aria-hidden": true,
|
|
284
|
-
defaultChecked: checked,
|
|
285
|
-
...inputProps,
|
|
286
|
-
tabIndex: -1,
|
|
287
|
-
ref,
|
|
288
|
-
style: {
|
|
289
|
-
...props.style,
|
|
290
|
-
// ...controlSize,
|
|
291
|
-
position: "absolute",
|
|
292
|
-
pointerEvents: "none",
|
|
293
|
-
opacity: 0,
|
|
294
|
-
margin: 0
|
|
295
|
-
}
|
|
296
|
-
}
|
|
297
|
-
);
|
|
298
|
-
};
|
|
299
|
-
function getState(checked) {
|
|
300
|
-
return checked ? "checked" : "unchecked";
|
|
301
|
-
}
|
|
302
104
|
// Annotate the CommonJS export names for ESM import in node:
|
|
303
105
|
0 && (module.exports = {
|
|
304
106
|
Switch,
|
|
305
107
|
SwitchFrame,
|
|
306
|
-
SwitchThumb
|
|
307
|
-
SwitchThumbFrame,
|
|
308
|
-
createSwitchScope
|
|
108
|
+
SwitchThumb
|
|
309
109
|
});
|
|
310
110
|
//# sourceMappingURL=Switch.js.map
|
package/dist/cjs/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;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,kBAAqD;AACrD,uBAAwB;AACxB,oBAAuC;AAEvC,0BAA4C;AAErC,MAAM,kBAAc,oBAAO,8BAAgB;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,UAAM,kCAAiB,0BAAQ,GAAG,CAAC,IAAI,IAAI;AAElD,MAAM,iBAAiB,CAAC,QAAoB,gBAAgB,GAAG,IAAI;AAE5D,MAAM,kBAAc,oBAAO,sBAAQ;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,aAAS,kCAAa;AAAA,EACjC,OAAO;AAAA,EACP,OAAO;AAAA,EACP,iBAAiB;AACnB,CAAC;",
|
|
5
|
+
"names": []
|
|
6
6
|
}
|
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
var createSwitch_exports = {};
|
|
30
|
+
__export(createSwitch_exports, {
|
|
31
|
+
SwitchContext: () => SwitchContext,
|
|
32
|
+
createSwitch: () => createSwitch
|
|
33
|
+
});
|
|
34
|
+
module.exports = __toCommonJS(createSwitch_exports);
|
|
35
|
+
var import_jsx_runtime = (
|
|
36
|
+
// @ts-ignore
|
|
37
|
+
require("react/jsx-runtime")
|
|
38
|
+
);
|
|
39
|
+
var import_compose_refs = require("@tamagui/compose-refs");
|
|
40
|
+
var import_core = require("@tamagui/core");
|
|
41
|
+
var import_focusable = require("@tamagui/focusable");
|
|
42
|
+
var import_get_token = require("@tamagui/get-token");
|
|
43
|
+
var import_label = require("@tamagui/label");
|
|
44
|
+
var import_use_controllable_state = require("@tamagui/use-controllable-state");
|
|
45
|
+
var import_use_previous = require("@tamagui/use-previous");
|
|
46
|
+
var React = __toESM(require("react"));
|
|
47
|
+
var import_react_native = require("react-native");
|
|
48
|
+
const SwitchContext = (0, import_core.createStyledContext)({
|
|
49
|
+
checked: false,
|
|
50
|
+
disabled: false,
|
|
51
|
+
size: void 0,
|
|
52
|
+
frameWidth: 60,
|
|
53
|
+
unstyled: false
|
|
54
|
+
});
|
|
55
|
+
function createSwitch({ Frame, Thumb, acceptsUnstyled }) {
|
|
56
|
+
const SwitchThumb = Thumb.styleable(function SwitchThumb2(props, forwardedRef) {
|
|
57
|
+
const { size: sizeProp, ...thumbProps } = props;
|
|
58
|
+
const { disabled, checked, unstyled, frameWidth } = React.useContext(SwitchContext);
|
|
59
|
+
const [thumbWidth, setThumbWidth] = React.useState(0);
|
|
60
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
61
|
+
Thumb,
|
|
62
|
+
{
|
|
63
|
+
theme: unstyled === false && checked ? "active" : null,
|
|
64
|
+
"data-state": getState(checked),
|
|
65
|
+
"data-disabled": disabled ? "" : void 0,
|
|
66
|
+
x: checked ? frameWidth - thumbWidth : 0,
|
|
67
|
+
...thumbProps,
|
|
68
|
+
onLayout: (0, import_core.composeEventHandlers)(
|
|
69
|
+
props.onLayout,
|
|
70
|
+
(e) => (
|
|
71
|
+
// @ts-ignore
|
|
72
|
+
setThumbWidth(e.nativeEvent.layout.width)
|
|
73
|
+
)
|
|
74
|
+
),
|
|
75
|
+
ref: forwardedRef
|
|
76
|
+
}
|
|
77
|
+
);
|
|
78
|
+
});
|
|
79
|
+
const SwitchComponent = Frame.extractable(
|
|
80
|
+
React.forwardRef(function SwitchFrame(propsIn, forwardedRef) {
|
|
81
|
+
const styledContext = React.useContext(SwitchContext);
|
|
82
|
+
const props = (0, import_core.useProps)(propsIn);
|
|
83
|
+
const {
|
|
84
|
+
labeledBy: ariaLabelledby,
|
|
85
|
+
name,
|
|
86
|
+
checked: checkedProp,
|
|
87
|
+
defaultChecked,
|
|
88
|
+
required,
|
|
89
|
+
disabled,
|
|
90
|
+
value = "on",
|
|
91
|
+
onCheckedChange,
|
|
92
|
+
size = styledContext.size ?? "$true",
|
|
93
|
+
unstyled = styledContext.unstyled ?? false,
|
|
94
|
+
native: nativeProp,
|
|
95
|
+
nativeProps,
|
|
96
|
+
...switchProps
|
|
97
|
+
} = props;
|
|
98
|
+
const leftBorderWidth = (() => {
|
|
99
|
+
let _ = void 0;
|
|
100
|
+
for (const key in switchProps) {
|
|
101
|
+
if (key === "borderWidth" || key === "borderLeftWidth") {
|
|
102
|
+
_ = switchProps[key];
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
if (acceptsUnstyled && _ === void 0 && unstyled === false) {
|
|
106
|
+
_ = 2;
|
|
107
|
+
}
|
|
108
|
+
if (typeof _ === "string") {
|
|
109
|
+
_ = (0, import_core.getVariableValue)((0, import_get_token.getSize)(_));
|
|
110
|
+
}
|
|
111
|
+
return +_;
|
|
112
|
+
})();
|
|
113
|
+
const native = Array.isArray(nativeProp) ? nativeProp : [nativeProp];
|
|
114
|
+
const shouldRenderMobileNative = !import_core.isWeb && nativeProp === true || !import_core.isWeb && native.includes("mobile") || native.includes("android") && import_react_native.Platform.OS === "android" || native.includes("ios") && import_react_native.Platform.OS === "ios";
|
|
115
|
+
const [button, setButton] = React.useState(null);
|
|
116
|
+
const composedRefs = (0, import_compose_refs.useComposedRefs)(forwardedRef, (node) => setButton(node));
|
|
117
|
+
const labelId = (0, import_label.useLabelContext)(button);
|
|
118
|
+
const labelledBy = ariaLabelledby || labelId;
|
|
119
|
+
const hasConsumerStoppedPropagationRef = React.useRef(false);
|
|
120
|
+
const isFormControl = import_core.isWeb ? button ? Boolean(button.closest("form")) : true : false;
|
|
121
|
+
const [frameWidth, setFrameWidth] = React.useState(60);
|
|
122
|
+
const [checked = false, setChecked] = (0, import_use_controllable_state.useControllableState)({
|
|
123
|
+
prop: checkedProp,
|
|
124
|
+
defaultProp: defaultChecked || false,
|
|
125
|
+
onChange: onCheckedChange,
|
|
126
|
+
transition: true
|
|
127
|
+
});
|
|
128
|
+
if (shouldRenderMobileNative) {
|
|
129
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
130
|
+
import_react_native.Switch,
|
|
131
|
+
{
|
|
132
|
+
value: checkedProp,
|
|
133
|
+
onValueChange: onCheckedChange,
|
|
134
|
+
...nativeProps
|
|
135
|
+
}
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
if (!import_core.isWeb) {
|
|
139
|
+
React.useEffect(() => {
|
|
140
|
+
if (!props.id)
|
|
141
|
+
return;
|
|
142
|
+
return (0, import_focusable.registerFocusable)(props.id, {
|
|
143
|
+
focus: () => {
|
|
144
|
+
setChecked((x) => !x);
|
|
145
|
+
}
|
|
146
|
+
});
|
|
147
|
+
}, [props.id, setChecked]);
|
|
148
|
+
}
|
|
149
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [
|
|
150
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
151
|
+
Frame,
|
|
152
|
+
{
|
|
153
|
+
unstyled,
|
|
154
|
+
size,
|
|
155
|
+
checked,
|
|
156
|
+
disabled,
|
|
157
|
+
frameWidth: frameWidth - leftBorderWidth * 2,
|
|
158
|
+
theme: checked ? "active" : null,
|
|
159
|
+
themeShallow: true,
|
|
160
|
+
role: "switch",
|
|
161
|
+
"aria-checked": checked,
|
|
162
|
+
"aria-labelledby": labelledBy,
|
|
163
|
+
"aria-required": required,
|
|
164
|
+
"data-state": getState(checked),
|
|
165
|
+
"data-disabled": disabled ? "" : void 0,
|
|
166
|
+
tabIndex: disabled ? void 0 : 0,
|
|
167
|
+
value,
|
|
168
|
+
...switchProps,
|
|
169
|
+
ref: composedRefs,
|
|
170
|
+
onPress: (0, import_core.composeEventHandlers)(props.onPress, (event) => {
|
|
171
|
+
setChecked((prevChecked) => !prevChecked);
|
|
172
|
+
if (import_core.isWeb && isFormControl) {
|
|
173
|
+
hasConsumerStoppedPropagationRef.current = event.isPropagationStopped();
|
|
174
|
+
if (!hasConsumerStoppedPropagationRef.current)
|
|
175
|
+
event.stopPropagation();
|
|
176
|
+
}
|
|
177
|
+
}),
|
|
178
|
+
onLayout: (0, import_core.composeEventHandlers)(
|
|
179
|
+
props.onLayout,
|
|
180
|
+
(e) => (
|
|
181
|
+
// @ts-ignore
|
|
182
|
+
setFrameWidth(e.nativeEvent.layout.width)
|
|
183
|
+
)
|
|
184
|
+
)
|
|
185
|
+
}
|
|
186
|
+
),
|
|
187
|
+
import_core.isWeb && isFormControl && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
188
|
+
BubbleInput,
|
|
189
|
+
{
|
|
190
|
+
control: button,
|
|
191
|
+
bubbles: !hasConsumerStoppedPropagationRef.current,
|
|
192
|
+
name,
|
|
193
|
+
value,
|
|
194
|
+
checked,
|
|
195
|
+
required,
|
|
196
|
+
disabled,
|
|
197
|
+
style: { transform: "translateX(-100%)" }
|
|
198
|
+
}
|
|
199
|
+
)
|
|
200
|
+
] });
|
|
201
|
+
})
|
|
202
|
+
);
|
|
203
|
+
const BubbleInput = (props) => {
|
|
204
|
+
const { control, checked, bubbles = true, ...inputProps } = props;
|
|
205
|
+
const ref = React.useRef(null);
|
|
206
|
+
const prevChecked = (0, import_use_previous.usePrevious)(checked);
|
|
207
|
+
React.useEffect(() => {
|
|
208
|
+
const input = ref.current;
|
|
209
|
+
const inputProto = window.HTMLInputElement.prototype;
|
|
210
|
+
const descriptor = Object.getOwnPropertyDescriptor(
|
|
211
|
+
inputProto,
|
|
212
|
+
"checked"
|
|
213
|
+
);
|
|
214
|
+
const setChecked = descriptor.set;
|
|
215
|
+
if (prevChecked !== checked && setChecked) {
|
|
216
|
+
const event = new Event("click", { bubbles });
|
|
217
|
+
setChecked.call(input, checked);
|
|
218
|
+
input.dispatchEvent(event);
|
|
219
|
+
}
|
|
220
|
+
}, [prevChecked, checked, bubbles]);
|
|
221
|
+
return (
|
|
222
|
+
// @ts-ignore
|
|
223
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
224
|
+
"input",
|
|
225
|
+
{
|
|
226
|
+
type: "checkbox",
|
|
227
|
+
"aria-hidden": true,
|
|
228
|
+
defaultChecked: checked,
|
|
229
|
+
...inputProps,
|
|
230
|
+
tabIndex: -1,
|
|
231
|
+
ref,
|
|
232
|
+
style: {
|
|
233
|
+
...props.style,
|
|
234
|
+
// ...controlSize,
|
|
235
|
+
position: "absolute",
|
|
236
|
+
pointerEvents: "none",
|
|
237
|
+
opacity: 0,
|
|
238
|
+
margin: 0
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
)
|
|
242
|
+
);
|
|
243
|
+
};
|
|
244
|
+
function getState(checked) {
|
|
245
|
+
return checked ? "checked" : "unchecked";
|
|
246
|
+
}
|
|
247
|
+
const Switch = (0, import_core.withStaticProperties)(SwitchComponent, {
|
|
248
|
+
Thumb: SwitchThumb
|
|
249
|
+
});
|
|
250
|
+
return Switch;
|
|
251
|
+
}
|
|
252
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
253
|
+
0 && (module.exports = {
|
|
254
|
+
SwitchContext,
|
|
255
|
+
createSwitch
|
|
256
|
+
});
|
|
257
|
+
//# sourceMappingURL=createSwitch.js.map
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/createSwitch.tsx"],
|
|
4
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AA0EM;AAAA;AAAA;AAAA;AA1EN,0BAAgC;AAChC,kBAYO;AACP,uBAAkC;AAClC,uBAAwB;AACxB,mBAAgC;AAChC,oCAAqC;AACrC,0BAA4B;AAC5B,YAAuB;AACvB,0BAIO;AAEA,MAAM,oBAAgB,iCAM1B;AAAA,EACD,SAAS;AAAA,EACT,UAAU;AAAA,EACV,MAAM;AAAA,EACN,YAAY;AAAA,EACZ,UAAU;AACZ,CAAC;AAuBM,SAAS,aAMd,EAAE,OAAO,OA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AClB,YAAM,aAAa,OAAO,iBAAiB;AAC3C,YAAM,aAAa,OAAO;AAAA,QACxB;AAAA,QACA;AAAA,MACF;AACA,YAAM,aAAa,WAAW;AAC9B,UAAI,gBAAgB,WAAW,YAAY;AACzC,cAAM,QAAQ,IAAI,MAAM,SAAS,EAAE,QAAQ,CAAC;AAC5C,mBAAW,KAAK,OAAO,OAAO;AAC9B,cAAM,cAAc,KAAK;AAAA,MAC3B;AAAA,IACF,GAAG,CAAC,aAAa,SAAS,OAAO,CAAC;AAElC;AAAA;AAAA,MAEE;AAAA,QAAC;AAAA;AAAA,UACC,MAAK;AAAA,UACL,eAAW;AAAA,UACX,gBAAgB;AAAA,UACf,GAAG;AAAA,UACJ,UAAU;AAAA,UACV;AAAA,UACA,OAAO;AAAA,YACL,GAAG,MAAM;AAAA;AAAA,YAET,UAAU;AAAA,YACV,eAAe;AAAA,YACf,SAAS;AAAA,YACT,QAAQ;AAAA,UACV;AAAA;AAAA,MACF;AAAA;AAAA,EAEJ;AAEA,WAAS,SAAS,SAAkB;AAClC,WAAO,UAAU,YAAY;AAAA,EAC/B;AAEA,QAAM,aAAS,kCAAqB,iBAAiB;AAAA,IACnD,OAAO;AAAA,EACT,CAAC;AAED,SAAO;AACT;",
|
|
5
|
+
"names": ["SwitchThumb", "NativeSwitch"]
|
|
6
|
+
}
|
package/dist/cjs/index.js
CHANGED
|
@@ -16,8 +16,10 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
16
16
|
var src_exports = {};
|
|
17
17
|
module.exports = __toCommonJS(src_exports);
|
|
18
18
|
__reExport(src_exports, require("./Switch"), module.exports);
|
|
19
|
+
__reExport(src_exports, require("./createSwitch"), module.exports);
|
|
19
20
|
// Annotate the CommonJS export names for ESM import in node:
|
|
20
21
|
0 && (module.exports = {
|
|
21
|
-
...require("./Switch")
|
|
22
|
+
...require("./Switch"),
|
|
23
|
+
...require("./createSwitch")
|
|
22
24
|
});
|
|
23
25
|
//# sourceMappingURL=index.js.map
|