@tamagui/switch 1.13.2 → 1.13.4
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 +1 -295
- package/dist/cjs/Switch.js.map +2 -2
- package/dist/cjs/index.js +1 -18
- package/dist/cjs/index.js.map +2 -2
- package/dist/esm/Switch.js +1 -262
- package/dist/esm/Switch.js.map +2 -2
- package/dist/esm/Switch.mjs +1 -262
- package/dist/esm/Switch.mjs.map +2 -2
- package/dist/esm/index.js +1 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/index.mjs +1 -1
- package/dist/esm/index.mjs.map +1 -1
- package/dist/jsx/Switch.js +1 -244
- package/dist/jsx/Switch.js.map +2 -2
- package/dist/jsx/Switch.mjs +1 -244
- package/dist/jsx/Switch.mjs.map +2 -2
- package/dist/jsx/index.js +1 -1
- package/dist/jsx/index.js.map +1 -1
- package/dist/jsx/index.mjs +1 -1
- package/dist/jsx/index.mjs.map +1 -1
- package/package.json +10 -10
package/dist/cjs/Switch.js
CHANGED
|
@@ -1,296 +1,2 @@
|
|
|
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 Switch_exports = {};
|
|
30
|
-
__export(Switch_exports, {
|
|
31
|
-
Switch: () => Switch,
|
|
32
|
-
SwitchFrame: () => SwitchFrame,
|
|
33
|
-
SwitchThumb: () => SwitchThumb,
|
|
34
|
-
SwitchThumbFrame: () => SwitchThumbFrame,
|
|
35
|
-
createSwitchScope: () => createSwitchScope
|
|
36
|
-
});
|
|
37
|
-
module.exports = __toCommonJS(Switch_exports);
|
|
38
|
-
var import_jsx_runtime = require("react/jsx-runtime");
|
|
39
|
-
var import_react_use_previous = require("@radix-ui/react-use-previous");
|
|
40
|
-
var import_compose_refs = require("@tamagui/compose-refs");
|
|
41
|
-
var import_core = require("@tamagui/core");
|
|
42
|
-
var import_create_context = require("@tamagui/create-context");
|
|
43
|
-
var import_focusable = require("@tamagui/focusable");
|
|
44
|
-
var import_get_size = require("@tamagui/get-size");
|
|
45
|
-
var import_label = require("@tamagui/label");
|
|
46
|
-
var import_stacks = require("@tamagui/stacks");
|
|
47
|
-
var import_use_controllable_state = require("@tamagui/use-controllable-state");
|
|
48
|
-
var React = __toESM(require("react"));
|
|
49
|
-
const SWITCH_NAME = "Switch";
|
|
50
|
-
const getSwitchHeight = (val) => Math.round((0, import_core.getVariableValue)((0, import_get_size.getSize)(val)) * 0.65);
|
|
51
|
-
const getSwitchWidth = (val) => getSwitchHeight(val) * 2;
|
|
52
|
-
const scopeContexts = (0, import_create_context.createContextScope)(SWITCH_NAME);
|
|
53
|
-
const [createSwitchContext] = scopeContexts;
|
|
54
|
-
const createSwitchScope = scopeContexts[1];
|
|
55
|
-
const [SwitchProvider, useSwitchContext] = createSwitchContext(SWITCH_NAME);
|
|
56
|
-
const THUMB_NAME = "SwitchThumb";
|
|
57
|
-
const SwitchThumbFrame = (0, import_core.styled)(import_stacks.ThemeableStack, {
|
|
58
|
-
name: "SwitchThumb",
|
|
59
|
-
variants: {
|
|
60
|
-
unstyled: {
|
|
61
|
-
false: {
|
|
62
|
-
size: "$true",
|
|
63
|
-
backgroundColor: "$background",
|
|
64
|
-
borderRadius: 1e3
|
|
65
|
-
}
|
|
66
|
-
},
|
|
67
|
-
size: {
|
|
68
|
-
"...size": (val) => {
|
|
69
|
-
const size = getSwitchHeight(val);
|
|
70
|
-
return {
|
|
71
|
-
height: size,
|
|
72
|
-
width: size
|
|
73
|
-
};
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
},
|
|
77
|
-
defaultVariants: {
|
|
78
|
-
unstyled: false
|
|
79
|
-
}
|
|
80
|
-
});
|
|
81
|
-
const SwitchThumb = SwitchThumbFrame.extractable(
|
|
82
|
-
React.forwardRef(
|
|
83
|
-
(props, forwardedRef) => {
|
|
84
|
-
const { __scopeSwitch, size: sizeProp, ...thumbProps } = props;
|
|
85
|
-
const {
|
|
86
|
-
size: sizeContext,
|
|
87
|
-
disabled,
|
|
88
|
-
checked,
|
|
89
|
-
unstyled
|
|
90
|
-
} = useSwitchContext(THUMB_NAME, __scopeSwitch);
|
|
91
|
-
const size = sizeProp ?? sizeContext;
|
|
92
|
-
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
93
|
-
SwitchThumbFrame,
|
|
94
|
-
{
|
|
95
|
-
unstyled,
|
|
96
|
-
size,
|
|
97
|
-
theme: checked ? "active" : null,
|
|
98
|
-
"data-state": getState(checked),
|
|
99
|
-
"data-disabled": disabled ? "" : void 0,
|
|
100
|
-
...thumbProps,
|
|
101
|
-
x: checked ? (0, import_core.getVariableValue)(getSwitchWidth(size)) - (0, import_core.getVariableValue)(getSwitchHeight(size)) : 0,
|
|
102
|
-
ref: forwardedRef
|
|
103
|
-
}
|
|
104
|
-
);
|
|
105
|
-
}
|
|
106
|
-
)
|
|
107
|
-
);
|
|
108
|
-
SwitchThumb.displayName = THUMB_NAME;
|
|
109
|
-
const SwitchFrame = (0, import_core.styled)(import_stacks.XStack, {
|
|
110
|
-
name: SWITCH_NAME,
|
|
111
|
-
tag: "button",
|
|
112
|
-
variants: {
|
|
113
|
-
unstyled: {
|
|
114
|
-
false: {
|
|
115
|
-
size: "$true",
|
|
116
|
-
borderRadius: 1e3,
|
|
117
|
-
borderWidth: 2,
|
|
118
|
-
borderColor: "transparent",
|
|
119
|
-
backgroundColor: "$background",
|
|
120
|
-
focusStyle: {
|
|
121
|
-
borderColor: "$borderColorFocus"
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
},
|
|
125
|
-
size: {
|
|
126
|
-
"...size": (val) => {
|
|
127
|
-
const height = getSwitchHeight(val) + 4;
|
|
128
|
-
const width = getSwitchWidth(val) + 4;
|
|
129
|
-
return {
|
|
130
|
-
height,
|
|
131
|
-
minHeight: height,
|
|
132
|
-
width
|
|
133
|
-
};
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
},
|
|
137
|
-
defaultVariants: {
|
|
138
|
-
unstyled: false
|
|
139
|
-
}
|
|
140
|
-
});
|
|
141
|
-
const Switch = (0, import_core.withStaticProperties)(
|
|
142
|
-
SwitchFrame.extractable(
|
|
143
|
-
React.forwardRef(
|
|
144
|
-
(props, forwardedRef) => {
|
|
145
|
-
const {
|
|
146
|
-
__scopeSwitch,
|
|
147
|
-
labeledBy: ariaLabelledby,
|
|
148
|
-
name,
|
|
149
|
-
checked: checkedProp,
|
|
150
|
-
defaultChecked,
|
|
151
|
-
required,
|
|
152
|
-
disabled,
|
|
153
|
-
value = "on",
|
|
154
|
-
onCheckedChange,
|
|
155
|
-
size = "$true",
|
|
156
|
-
unstyled = false,
|
|
157
|
-
...switchProps
|
|
158
|
-
} = props;
|
|
159
|
-
const [button, setButton] = React.useState(null);
|
|
160
|
-
const composedRefs = (0, import_compose_refs.useComposedRefs)(
|
|
161
|
-
forwardedRef,
|
|
162
|
-
(node) => setButton(node)
|
|
163
|
-
);
|
|
164
|
-
const labelId = (0, import_label.useLabelContext)(button);
|
|
165
|
-
const labelledBy = ariaLabelledby || labelId;
|
|
166
|
-
const hasConsumerStoppedPropagationRef = React.useRef(false);
|
|
167
|
-
const isFormControl = import_core.isWeb ? button ? Boolean(button.closest("form")) : true : false;
|
|
168
|
-
const [checked = false, setChecked] = (0, import_use_controllable_state.useControllableState)({
|
|
169
|
-
prop: checkedProp,
|
|
170
|
-
defaultProp: defaultChecked || false,
|
|
171
|
-
onChange: onCheckedChange,
|
|
172
|
-
transition: true
|
|
173
|
-
});
|
|
174
|
-
if (!import_core.isWeb) {
|
|
175
|
-
React.useEffect(() => {
|
|
176
|
-
if (!props.id)
|
|
177
|
-
return;
|
|
178
|
-
return (0, import_focusable.registerFocusable)(props.id, {
|
|
179
|
-
focus: () => {
|
|
180
|
-
setChecked((x) => !x);
|
|
181
|
-
}
|
|
182
|
-
});
|
|
183
|
-
}, [props.id, setChecked]);
|
|
184
|
-
}
|
|
185
|
-
return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
|
|
186
|
-
SwitchProvider,
|
|
187
|
-
{
|
|
188
|
-
scope: __scopeSwitch,
|
|
189
|
-
checked,
|
|
190
|
-
disabled,
|
|
191
|
-
size,
|
|
192
|
-
unstyled,
|
|
193
|
-
children: [
|
|
194
|
-
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
195
|
-
SwitchFrame,
|
|
196
|
-
{
|
|
197
|
-
unstyled,
|
|
198
|
-
size,
|
|
199
|
-
role: "switch",
|
|
200
|
-
"aria-checked": checked,
|
|
201
|
-
"aria-labelledby": labelledBy,
|
|
202
|
-
"aria-required": required,
|
|
203
|
-
"data-state": getState(checked),
|
|
204
|
-
"data-disabled": disabled ? "" : void 0,
|
|
205
|
-
disabled,
|
|
206
|
-
theme: checked ? "active" : null,
|
|
207
|
-
themeShallow: true,
|
|
208
|
-
tabIndex: disabled ? void 0 : 0,
|
|
209
|
-
value,
|
|
210
|
-
...switchProps,
|
|
211
|
-
ref: composedRefs,
|
|
212
|
-
onPress: (event) => {
|
|
213
|
-
var _a;
|
|
214
|
-
(_a = props.onPress) == null ? void 0 : _a.call(props, event);
|
|
215
|
-
setChecked((prevChecked) => !prevChecked);
|
|
216
|
-
if (import_core.isWeb && isFormControl) {
|
|
217
|
-
hasConsumerStoppedPropagationRef.current = event.isPropagationStopped();
|
|
218
|
-
if (!hasConsumerStoppedPropagationRef.current)
|
|
219
|
-
event.stopPropagation();
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
),
|
|
224
|
-
import_core.isWeb && isFormControl && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
225
|
-
BubbleInput,
|
|
226
|
-
{
|
|
227
|
-
control: button,
|
|
228
|
-
bubbles: !hasConsumerStoppedPropagationRef.current,
|
|
229
|
-
name,
|
|
230
|
-
value,
|
|
231
|
-
checked,
|
|
232
|
-
required,
|
|
233
|
-
disabled,
|
|
234
|
-
style: { transform: "translateX(-100%)" }
|
|
235
|
-
}
|
|
236
|
-
)
|
|
237
|
-
]
|
|
238
|
-
}
|
|
239
|
-
);
|
|
240
|
-
}
|
|
241
|
-
)
|
|
242
|
-
),
|
|
243
|
-
{
|
|
244
|
-
Thumb: SwitchThumb
|
|
245
|
-
}
|
|
246
|
-
);
|
|
247
|
-
const BubbleInput = (props) => {
|
|
248
|
-
const { control, checked, bubbles = true, ...inputProps } = props;
|
|
249
|
-
const ref = React.useRef(null);
|
|
250
|
-
const prevChecked = (0, import_react_use_previous.usePrevious)(checked);
|
|
251
|
-
React.useEffect(() => {
|
|
252
|
-
const input = ref.current;
|
|
253
|
-
const inputProto = window.HTMLInputElement.prototype;
|
|
254
|
-
const descriptor = Object.getOwnPropertyDescriptor(
|
|
255
|
-
inputProto,
|
|
256
|
-
"checked"
|
|
257
|
-
);
|
|
258
|
-
const setChecked = descriptor.set;
|
|
259
|
-
if (prevChecked !== checked && setChecked) {
|
|
260
|
-
const event = new Event("click", { bubbles });
|
|
261
|
-
setChecked.call(input, checked);
|
|
262
|
-
input.dispatchEvent(event);
|
|
263
|
-
}
|
|
264
|
-
}, [prevChecked, checked, bubbles]);
|
|
265
|
-
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
266
|
-
"input",
|
|
267
|
-
{
|
|
268
|
-
type: "checkbox",
|
|
269
|
-
"aria-hidden": true,
|
|
270
|
-
defaultChecked: checked,
|
|
271
|
-
...inputProps,
|
|
272
|
-
tabIndex: -1,
|
|
273
|
-
ref,
|
|
274
|
-
style: {
|
|
275
|
-
...props.style,
|
|
276
|
-
// ...controlSize,
|
|
277
|
-
position: "absolute",
|
|
278
|
-
pointerEvents: "none",
|
|
279
|
-
opacity: 0,
|
|
280
|
-
margin: 0
|
|
281
|
-
}
|
|
282
|
-
}
|
|
283
|
-
);
|
|
284
|
-
};
|
|
285
|
-
function getState(checked) {
|
|
286
|
-
return checked ? "checked" : "unchecked";
|
|
287
|
-
}
|
|
288
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
289
|
-
0 && (module.exports = {
|
|
290
|
-
Switch,
|
|
291
|
-
SwitchFrame,
|
|
292
|
-
SwitchThumb,
|
|
293
|
-
SwitchThumbFrame,
|
|
294
|
-
createSwitchScope
|
|
295
|
-
});
|
|
1
|
+
"use strict";var j=Object.create;var w=Object.defineProperty;var J=Object.getOwnPropertyDescriptor;var K=Object.getOwnPropertyNames;var Q=Object.getPrototypeOf,Y=Object.prototype.hasOwnProperty;var Z=(e,t)=>{for(var o in t)w(e,o,{get:t[o],enumerable:!0})},R=(e,t,o,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let n of K(t))!Y.call(e,n)&&n!==o&&w(e,n,{get:()=>t[n],enumerable:!(r=J(t,n))||r.enumerable});return e};var ee=(e,t,o)=>(o=e!=null?j(Q(e)):{},R(t||!e||!e.__esModule?w(o,"default",{value:e,enumerable:!0}):o,e)),te=e=>R(w({},"__esModule",{value:!0}),e);var ae={};Z(ae,{Switch:()=>ce,SwitchFrame:()=>x,SwitchThumb:()=>T,SwitchThumbFrame:()=>z,createSwitchScope:()=>se});module.exports=te(ae);var u=require("react/jsx-runtime"),L=require("@radix-ui/react-use-previous"),V=require("@tamagui/compose-refs"),s=require("@tamagui/core"),_=require("@tamagui/create-context"),$=require("@tamagui/focusable"),W=require("@tamagui/get-size"),q=require("@tamagui/label"),P=require("@tamagui/stacks"),G=require("@tamagui/use-controllable-state"),d=ee(require("react"));const C="Switch",k=e=>Math.round((0,s.getVariableValue)((0,W.getSize)(e))*.65),I=e=>k(e)*2,H=(0,_.createContextScope)(C),[oe]=H,se=H[1],[ne,re]=oe(C),M="SwitchThumb",z=(0,s.styled)(P.ThemeableStack,{name:"SwitchThumb",variants:{unstyled:{false:{size:"$true",backgroundColor:"$background",borderRadius:1e3}},size:{"...size":e=>{const t=k(e);return{height:t,width:t}}}},defaultVariants:{unstyled:!1}}),T=z.extractable(d.forwardRef((e,t)=>{const{__scopeSwitch:o,size:r,...n}=e,{size:h,disabled:p,checked:c,unstyled:i}=re(M,o),a=r??h;return(0,u.jsx)(z,{unstyled:i,size:a,theme:c?"active":null,"data-state":F(c),"data-disabled":p?"":void 0,...n,x:c?(0,s.getVariableValue)(I(a))-(0,s.getVariableValue)(k(a)):0,ref:t})}));T.displayName=M;const x=(0,s.styled)(P.XStack,{name:C,tag:"button",variants:{unstyled:{false:{size:"$true",borderRadius:1e3,borderWidth:2,borderColor:"transparent",backgroundColor:"$background",focusStyle:{borderColor:"$borderColorFocus"}}},size:{"...size":e=>{const t=k(e)+4,o=I(e)+4;return{height:t,minHeight:t,width:o}}}},defaultVariants:{unstyled:!1}}),ce=(0,s.withStaticProperties)(x.extractable(d.forwardRef((e,t)=>{const{__scopeSwitch:o,labeledBy:r,name:n,checked:h,defaultChecked:p,required:c,disabled:i,value:a="on",onCheckedChange:f,size:m="$true",unstyled:v=!1,...N}=e,[S,O]=d.useState(null),A=(0,V.useComposedRefs)(t,l=>O(l)),D=(0,q.useLabelContext)(S),X=r||D,y=d.useRef(!1),B=s.isWeb?S?Boolean(S.closest("form")):!0:!1,[b=!1,g]=(0,G.useControllableState)({prop:h,defaultProp:p||!1,onChange:f,transition:!0});return s.isWeb||d.useEffect(()=>{if(e.id)return(0,$.registerFocusable)(e.id,{focus:()=>{g(l=>!l)}})},[e.id,g]),(0,u.jsxs)(ne,{scope:o,checked:b,disabled:i,size:m,unstyled:v,children:[(0,u.jsx)(x,{unstyled:v,size:m,role:"switch","aria-checked":b,"aria-labelledby":X,"aria-required":c,"data-state":F(b),"data-disabled":i?"":void 0,disabled:i,theme:b?"active":null,themeShallow:!0,tabIndex:i?void 0:0,value:a,...N,ref:A,onPress:l=>{var E;(E=e.onPress)==null||E.call(e,l),g(U=>!U),s.isWeb&&B&&(y.current=l.isPropagationStopped(),y.current||l.stopPropagation())}}),s.isWeb&&B&&(0,u.jsx)(ie,{control:S,bubbles:!y.current,name:n,value:a,checked:b,required:c,disabled:i,style:{transform:"translateX(-100%)"}})]})})),{Thumb:T}),ie=e=>{const{control:t,checked:o,bubbles:r=!0,...n}=e,h=d.useRef(null),p=(0,L.usePrevious)(o);return d.useEffect(()=>{const c=h.current,i=window.HTMLInputElement.prototype,f=Object.getOwnPropertyDescriptor(i,"checked").set;if(p!==o&&f){const m=new Event("click",{bubbles:r});f.call(c,o),c.dispatchEvent(m)}},[p,o,r]),(0,u.jsx)("input",{type:"checkbox","aria-hidden":!0,defaultChecked:o,...n,tabIndex:-1,ref:h,style:{...e.style,position:"absolute",pointerEvents:"none",opacity:0,margin:0}})};function F(e){return e?"checked":"unchecked"}0&&(module.exports={Switch,SwitchFrame,SwitchThumb,SwitchThumbFrame,createSwitchScope});
|
|
296
2
|
//# sourceMappingURL=Switch.js.map
|
package/dist/cjs/Switch.js.map
CHANGED
|
@@ -2,6 +2,6 @@
|
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/Switch.tsx"],
|
|
4
4
|
"sourcesContent": ["// via radix\n// https://github.com/radix-ui/primitives/blob/main/packages/react/switch/src/Switch.tsx\n\nimport { usePrevious } from '@radix-ui/react-use-previous'\nimport { useComposedRefs } from '@tamagui/compose-refs'\nimport {\n GetProps,\n SizeTokens,\n getVariableValue,\n isWeb,\n styled,\n withStaticProperties,\n} from '@tamagui/core'\nimport { ScopedProps, createContextScope } from '@tamagui/create-context'\nimport { registerFocusable } from '@tamagui/focusable'\nimport { getSize } from '@tamagui/get-size'\nimport { useLabelContext } from '@tamagui/label'\nimport { ThemeableStack, XStack } from '@tamagui/stacks'\nimport { useControllableState } from '@tamagui/use-controllable-state'\nimport * as React from 'react'\nimport { View } from 'react-native'\n\nconst SWITCH_NAME = 'Switch'\n\n// TODO make customizable\nconst getSwitchHeight = (val: SizeTokens) =>\n Math.round(getVariableValue(getSize(val)) * 0.65)\nconst getSwitchWidth = (val: SizeTokens) => getSwitchHeight(val) * 2\n\nconst scopeContexts = createContextScope(SWITCH_NAME)\nconst [createSwitchContext] = scopeContexts\nexport const createSwitchScope = scopeContexts[1]\n\nconst [SwitchProvider, useSwitchContext] = createSwitchContext<{\n checked: boolean\n disabled?: boolean\n size: SizeTokens\n unstyled?: boolean\n}>(SWITCH_NAME)\n\n/* -------------------------------------------------------------------------------------------------\n * SwitchThumb\n * -----------------------------------------------------------------------------------------------*/\n\nconst THUMB_NAME = 'SwitchThumb'\n\nexport const SwitchThumbFrame = styled(ThemeableStack, {\n name: 'SwitchThumb',\n\n variants: {\n unstyled: {\n false: {\n size: '$true',\n backgroundColor: '$background',\n borderRadius: 1000,\n },\n },\n\n size: {\n '...size': (val) => {\n const size = getSwitchHeight(val)\n return {\n height: size,\n width: size,\n }\n },\n },\n } as const,\n\n defaultVariants: {\n unstyled: false,\n },\n})\n\nexport type SwitchThumbProps = GetProps<typeof SwitchThumbFrame>\n\nexport const SwitchThumb = SwitchThumbFrame.extractable(\n React.forwardRef<React.ElementRef<'span'>, SwitchThumbProps>(\n (props: ScopedProps<SwitchThumbProps, 'Switch'>, forwardedRef) => {\n const { __scopeSwitch, size: sizeProp, ...thumbProps } = props\n const {\n size: sizeContext,\n disabled,\n checked,\n unstyled,\n } = useSwitchContext(THUMB_NAME, __scopeSwitch)\n const size = sizeProp ?? sizeContext\n return (\n <SwitchThumbFrame\n unstyled={unstyled}\n size={size}\n theme={checked ? 'active' : null}\n data-state={getState(checked)}\n data-disabled={disabled ? '' : undefined}\n {...thumbProps}\n x={\n checked\n ? getVariableValue(getSwitchWidth(size)) -\n getVariableValue(getSwitchHeight(size))\n : 0\n }\n ref={forwardedRef}\n />\n )\n }\n )\n)\n\nSwitchThumb.displayName = THUMB_NAME\n\n/* -------------------------------------------------------------------------------------------------\n * Switch\n * -----------------------------------------------------------------------------------------------*/\n\nexport const SwitchFrame = styled(XStack, {\n name: SWITCH_NAME,\n tag: 'button',\n\n variants: {\n unstyled: {\n false: {\n size: '$true',\n borderRadius: 1000,\n borderWidth: 2,\n borderColor: 'transparent',\n backgroundColor: '$background',\n\n focusStyle: {\n borderColor: '$borderColorFocus',\n },\n },\n },\n\n size: {\n '...size': (val) => {\n const height = getSwitchHeight(val) + 4\n const width = getSwitchWidth(val) + 4\n return {\n height,\n minHeight: height,\n width,\n }\n },\n },\n } as const,\n\n defaultVariants: {\n unstyled: false,\n },\n})\n\ntype SwitchButtonProps = GetProps<typeof SwitchFrame>\n\nexport type SwitchProps = SwitchButtonProps & {\n labeledBy?: string\n name?: string\n value?: string\n checked?: boolean\n defaultChecked?: boolean\n required?: boolean\n onCheckedChange?(checked: boolean): void\n}\n\nexport const Switch = withStaticProperties(\n SwitchFrame.extractable(\n React.forwardRef<HTMLButtonElement | View, SwitchProps>(\n (props: ScopedProps<SwitchProps, 'Switch'>, forwardedRef) => {\n const {\n __scopeSwitch,\n labeledBy: ariaLabelledby,\n name,\n checked: checkedProp,\n defaultChecked,\n required,\n disabled,\n value = 'on',\n onCheckedChange,\n size = '$true',\n unstyled = false,\n ...switchProps\n } = props\n const [button, setButton] = React.useState<HTMLButtonElement | null>(null)\n const composedRefs = useComposedRefs(forwardedRef, (node) =>\n setButton(node as any)\n )\n const labelId = useLabelContext(button)\n const labelledBy = ariaLabelledby || labelId\n const hasConsumerStoppedPropagationRef = React.useRef(false)\n // We set this to true by default so that events bubble to forms without JS (SSR)\n const isFormControl = isWeb\n ? button\n ? Boolean(button.closest('form'))\n : true\n : false\n const [checked = false, setChecked] = useControllableState({\n prop: checkedProp,\n defaultProp: defaultChecked || false,\n onChange: onCheckedChange,\n transition: true,\n })\n\n if (!isWeb) {\n // eslint-disable-next-line react-hooks/rules-of-hooks\n React.useEffect(() => {\n if (!props.id) return\n return registerFocusable(props.id, {\n focus: () => {\n setChecked((x) => !x)\n },\n })\n }, [props.id, setChecked])\n }\n\n return (\n <SwitchProvider\n scope={__scopeSwitch}\n checked={checked}\n disabled={disabled}\n size={size}\n unstyled={unstyled}\n >\n <SwitchFrame\n unstyled={unstyled}\n size={size}\n // @ts-ignore\n role=\"switch\"\n aria-checked={checked}\n aria-labelledby={labelledBy}\n aria-required={required}\n data-state={getState(checked)}\n data-disabled={disabled ? '' : undefined}\n disabled={disabled}\n theme={checked ? 'active' : null}\n themeShallow\n // @ts-ignore\n tabIndex={disabled ? undefined : 0}\n // @ts-ignore\n value={value}\n {...switchProps}\n ref={composedRefs}\n onPress={(event) => {\n props.onPress?.(event)\n setChecked((prevChecked) => !prevChecked)\n if (isWeb && isFormControl) {\n hasConsumerStoppedPropagationRef.current = event.isPropagationStopped()\n // if switch is in a form, stop propagation from the button so that we only propagate\n // one click event (from the input). We propagate changes from an input so that native\n // form validation works and form events reflect switch updates.\n if (!hasConsumerStoppedPropagationRef.current) event.stopPropagation()\n }\n }}\n />\n {isWeb && isFormControl && (\n <BubbleInput\n control={button}\n bubbles={!hasConsumerStoppedPropagationRef.current}\n name={name}\n value={value}\n checked={checked}\n required={required}\n disabled={disabled}\n // We transform because the input is absolutely positioned but we have\n // rendered it **after** the button. This pulls it back to sit on top\n // of the button.\n style={{ transform: 'translateX(-100%)' }}\n />\n )}\n </SwitchProvider>\n )\n }\n )\n ),\n {\n Thumb: SwitchThumb,\n }\n)\n\n/* ---------------------------------------------------------------------------------------------- */\n\ntype InputProps = any //Radix.ComponentPropsWithoutRef<'input'>\ninterface BubbleInputProps extends Omit<InputProps, 'checked'> {\n checked: boolean\n control: HTMLElement | null\n bubbles: boolean\n}\n\n// TODO make this native friendly\nconst BubbleInput = (props: BubbleInputProps) => {\n const { control, checked, bubbles = true, ...inputProps } = props\n const ref = React.useRef<HTMLInputElement>(null)\n const prevChecked = usePrevious(checked)\n // const controlSize = useSize(control)\n\n // Bubble checked change to parents (e.g form change event)\n React.useEffect(() => {\n const input = ref.current!\n const inputProto = window.HTMLInputElement.prototype\n const descriptor = Object.getOwnPropertyDescriptor(\n inputProto,\n 'checked'\n ) as PropertyDescriptor\n const setChecked = descriptor.set\n if (prevChecked !== checked && setChecked) {\n const event = new Event('click', { bubbles })\n setChecked.call(input, checked)\n input.dispatchEvent(event)\n }\n }, [prevChecked, checked, bubbles])\n\n return (\n <input\n type=\"checkbox\"\n aria-hidden\n defaultChecked={checked}\n {...inputProps}\n tabIndex={-1}\n ref={ref}\n style={{\n ...props.style,\n // ...controlSize,\n position: 'absolute',\n pointerEvents: 'none',\n opacity: 0,\n margin: 0,\n }}\n />\n )\n}\n\nfunction getState(checked: boolean) {\n return checked ? 'checked' : 'unchecked'\n}\n"],
|
|
5
|
-
"mappings": "
|
|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AlB,EAAM,UAAU,IAAM,CACpB,MAAMiD,EAAQD,EAAI,QACZE,EAAa,OAAO,iBAAiB,UAKrCX,EAJa,OAAO,yBACxBW,EACA,SACF,EAC8B,IAC9B,GAAIP,IAAgBzB,GAAWqB,EAAY,CACzC,MAAME,EAAQ,IAAI,MAAM,QAAS,CAAE,QAAAK,CAAQ,CAAC,EAC5CP,EAAW,KAAKU,EAAO/B,CAAO,EAC9B+B,EAAM,cAAcR,CAAK,CAC3B,CACF,EAAG,CAACE,EAAazB,EAAS4B,CAAO,CAAC,KAGhC,OAAC,SACC,KAAK,WACL,cAAW,GACX,eAAgB5B,EACf,GAAG6B,EACJ,SAAU,GACV,IAAKC,EACL,MAAO,CACL,GAAGrC,EAAM,MAET,SAAU,WACV,cAAe,OACf,QAAS,EACT,OAAQ,CACV,EACF,CAEJ,EAEA,SAASS,EAASF,EAAkB,CAClC,OAAOA,EAAU,UAAY,WAC/B",
|
|
6
|
+
"names": ["Switch_exports", "__export", "Switch", "SwitchFrame", "SwitchThumb", "SwitchThumbFrame", "createSwitchScope", "__toCommonJS", "import_jsx_runtime", "import_react_use_previous", "import_compose_refs", "import_core", "import_create_context", "import_focusable", "import_get_size", "import_label", "import_stacks", "import_use_controllable_state", "React", "SWITCH_NAME", "getSwitchHeight", "val", "getSwitchWidth", "scopeContexts", "createSwitchContext", "SwitchProvider", "useSwitchContext", "THUMB_NAME", "size", "props", "forwardedRef", "__scopeSwitch", "sizeProp", "thumbProps", "sizeContext", "disabled", "checked", "unstyled", "getState", "height", "width", "ariaLabelledby", "name", "checkedProp", "defaultChecked", "required", "value", "onCheckedChange", "switchProps", "button", "setButton", "composedRefs", "node", "labelId", "labelledBy", "hasConsumerStoppedPropagationRef", "isFormControl", "setChecked", "x", "event", "_a", "prevChecked", "BubbleInput", "control", "bubbles", "inputProps", "ref", "input", "inputProto"]
|
|
7
7
|
}
|
package/dist/cjs/index.js
CHANGED
|
@@ -1,19 +1,2 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __copyProps = (to, from, except, desc) => {
|
|
7
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
8
|
-
for (let key of __getOwnPropNames(from))
|
|
9
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
10
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
11
|
-
}
|
|
12
|
-
return to;
|
|
13
|
-
};
|
|
14
|
-
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
15
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
16
|
-
var src_exports = {};
|
|
17
|
-
module.exports = __toCommonJS(src_exports);
|
|
18
|
-
__reExport(src_exports, require("./Switch"), module.exports);
|
|
1
|
+
"use strict";var a=Object.defineProperty;var b=Object.getOwnPropertyDescriptor;var c=Object.getOwnPropertyNames;var d=Object.prototype.hasOwnProperty;var p=(r,o,f,x)=>{if(o&&typeof o=="object"||typeof o=="function")for(let e of c(o))!d.call(r,e)&&e!==f&&a(r,e,{get:()=>o[e],enumerable:!(x=b(o,e))||x.enumerable});return r},t=(r,o,f)=>(p(r,o,"default"),f&&p(f,o,"default"));var g=r=>p(a({},"__esModule",{value:!0}),r);var m={};module.exports=g(m);t(m,require("./Switch"),module.exports);
|
|
19
2
|
//# sourceMappingURL=index.js.map
|
package/dist/cjs/index.js.map
CHANGED
|
@@ -2,6 +2,6 @@
|
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/index.ts"],
|
|
4
4
|
"sourcesContent": ["export * from './Switch'\n"],
|
|
5
|
-
"mappings": "
|
|
6
|
-
"names": []
|
|
5
|
+
"mappings": "iaAAA,IAAAA,EAAA,kBAAAC,EAAAD,GAAAE,EAAAF,EAAc,oBAAd",
|
|
6
|
+
"names": ["src_exports", "__toCommonJS", "__reExport"]
|
|
7
7
|
}
|