@tamagui/switch 1.57.8 → 1.58.0
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/jsx/Switch.js
CHANGED
|
@@ -1,32 +1,10 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
getVariableValue,
|
|
4
|
-
isWeb,
|
|
5
|
-
styled,
|
|
6
|
-
withStaticProperties
|
|
7
|
-
} from "@tamagui/core";
|
|
8
|
-
import { createContextScope } from "@tamagui/create-context";
|
|
9
|
-
import { registerFocusable } from "@tamagui/focusable";
|
|
1
|
+
import { getVariableValue, styled } from "@tamagui/core";
|
|
10
2
|
import { getSize } from "@tamagui/get-token";
|
|
11
|
-
import { useLabelContext } from "@tamagui/label";
|
|
12
3
|
import { ThemeableStack, XStack } from "@tamagui/stacks";
|
|
13
|
-
import {
|
|
14
|
-
|
|
15
|
-
import * as React from "react";
|
|
16
|
-
import {
|
|
17
|
-
Switch as NativeSwitch,
|
|
18
|
-
Platform
|
|
19
|
-
} from "react-native";
|
|
20
|
-
const SWITCH_NAME = "Switch";
|
|
21
|
-
const getSwitchHeight = (val) => Math.round(getVariableValue(getSize(val)) * 0.65);
|
|
22
|
-
const getSwitchWidth = (val) => getSwitchHeight(val) * 2;
|
|
23
|
-
const scopeContexts = createContextScope(SWITCH_NAME);
|
|
24
|
-
const [createSwitchContext] = scopeContexts;
|
|
25
|
-
const createSwitchScope = scopeContexts[1];
|
|
26
|
-
const [SwitchProvider, useSwitchContext] = createSwitchContext(SWITCH_NAME);
|
|
27
|
-
const THUMB_NAME = "SwitchThumb";
|
|
28
|
-
const SwitchThumbFrame = styled(ThemeableStack, {
|
|
4
|
+
import { SwitchContext, createSwitch } from "./createSwitch";
|
|
5
|
+
const SwitchThumb = styled(ThemeableStack, {
|
|
29
6
|
name: "SwitchThumb",
|
|
7
|
+
context: SwitchContext,
|
|
30
8
|
variants: {
|
|
31
9
|
unstyled: {
|
|
32
10
|
false: {
|
|
@@ -49,32 +27,12 @@ const SwitchThumbFrame = styled(ThemeableStack, {
|
|
|
49
27
|
unstyled: false
|
|
50
28
|
}
|
|
51
29
|
});
|
|
52
|
-
const
|
|
53
|
-
|
|
54
|
-
function SwitchThumb2(props, forwardedRef) {
|
|
55
|
-
const { __scopeSwitch, size: sizeProp, ...thumbProps } = props;
|
|
56
|
-
const {
|
|
57
|
-
size: sizeContext,
|
|
58
|
-
disabled,
|
|
59
|
-
checked,
|
|
60
|
-
unstyled
|
|
61
|
-
} = useSwitchContext(THUMB_NAME, __scopeSwitch);
|
|
62
|
-
const size = sizeProp ?? sizeContext;
|
|
63
|
-
return <SwitchThumbFrame
|
|
64
|
-
unstyled={unstyled}
|
|
65
|
-
size={size}
|
|
66
|
-
data-state={getState(checked)}
|
|
67
|
-
data-disabled={disabled ? "" : void 0}
|
|
68
|
-
x={checked ? getVariableValue(getSwitchWidth(size)) - getVariableValue(getSwitchHeight(size)) : 0}
|
|
69
|
-
{...thumbProps}
|
|
70
|
-
ref={forwardedRef}
|
|
71
|
-
/>;
|
|
72
|
-
}
|
|
73
|
-
)
|
|
74
|
-
);
|
|
30
|
+
const getSwitchHeight = (val) => Math.round(getVariableValue(getSize(val)) * 0.65);
|
|
31
|
+
const getSwitchWidth = (val) => getSwitchHeight(val) * 2;
|
|
75
32
|
const SwitchFrame = styled(XStack, {
|
|
76
|
-
name:
|
|
33
|
+
name: "Switch",
|
|
77
34
|
tag: "button",
|
|
35
|
+
context: SwitchContext,
|
|
78
36
|
variants: {
|
|
79
37
|
unstyled: {
|
|
80
38
|
false: {
|
|
@@ -91,6 +49,12 @@ const SwitchFrame = styled(XStack, {
|
|
|
91
49
|
}
|
|
92
50
|
}
|
|
93
51
|
},
|
|
52
|
+
checked: {
|
|
53
|
+
true: {}
|
|
54
|
+
},
|
|
55
|
+
frameWidth: {
|
|
56
|
+
":number": () => null
|
|
57
|
+
},
|
|
94
58
|
size: {
|
|
95
59
|
"...size": (val) => {
|
|
96
60
|
const height = getSwitchHeight(val) + 4;
|
|
@@ -107,153 +71,14 @@ const SwitchFrame = styled(XStack, {
|
|
|
107
71
|
unstyled: false
|
|
108
72
|
}
|
|
109
73
|
});
|
|
110
|
-
const
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
__scopeSwitch,
|
|
115
|
-
labeledBy: ariaLabelledby,
|
|
116
|
-
name,
|
|
117
|
-
checked: checkedProp,
|
|
118
|
-
defaultChecked,
|
|
119
|
-
required,
|
|
120
|
-
disabled,
|
|
121
|
-
value = "on",
|
|
122
|
-
onCheckedChange,
|
|
123
|
-
size = "$true",
|
|
124
|
-
unstyled = false,
|
|
125
|
-
native: nativeProp,
|
|
126
|
-
nativeProps,
|
|
127
|
-
...switchProps
|
|
128
|
-
} = props;
|
|
129
|
-
const native = Array.isArray(nativeProp) ? nativeProp : [nativeProp];
|
|
130
|
-
const shouldRenderMobileNative = !isWeb && nativeProp === true || !isWeb && native.includes("mobile") || native.includes("android") && Platform.OS === "android" || native.includes("ios") && Platform.OS === "ios";
|
|
131
|
-
if (shouldRenderMobileNative) {
|
|
132
|
-
return <NativeSwitch
|
|
133
|
-
value={checkedProp}
|
|
134
|
-
onValueChange={onCheckedChange}
|
|
135
|
-
{...nativeProps}
|
|
136
|
-
/>;
|
|
137
|
-
}
|
|
138
|
-
const [button, setButton] = React.useState(null);
|
|
139
|
-
const composedRefs = useComposedRefs(forwardedRef, (node) => setButton(node));
|
|
140
|
-
const labelId = useLabelContext(button);
|
|
141
|
-
const labelledBy = ariaLabelledby || labelId;
|
|
142
|
-
const hasConsumerStoppedPropagationRef = React.useRef(false);
|
|
143
|
-
const isFormControl = isWeb ? button ? Boolean(button.closest("form")) : true : false;
|
|
144
|
-
const [checked = false, setChecked] = useControllableState({
|
|
145
|
-
prop: checkedProp,
|
|
146
|
-
defaultProp: defaultChecked || false,
|
|
147
|
-
onChange: onCheckedChange,
|
|
148
|
-
transition: true
|
|
149
|
-
});
|
|
150
|
-
if (!isWeb) {
|
|
151
|
-
React.useEffect(() => {
|
|
152
|
-
if (!props.id)
|
|
153
|
-
return;
|
|
154
|
-
return registerFocusable(props.id, {
|
|
155
|
-
focus: () => {
|
|
156
|
-
setChecked((x) => !x);
|
|
157
|
-
}
|
|
158
|
-
});
|
|
159
|
-
}, [props.id, setChecked]);
|
|
160
|
-
}
|
|
161
|
-
return <SwitchProvider
|
|
162
|
-
scope={__scopeSwitch}
|
|
163
|
-
checked={checked}
|
|
164
|
-
disabled={disabled}
|
|
165
|
-
size={size}
|
|
166
|
-
unstyled={unstyled}
|
|
167
|
-
>
|
|
168
|
-
<SwitchFrame
|
|
169
|
-
unstyled={unstyled}
|
|
170
|
-
size={size}
|
|
171
|
-
theme={checked ? "active" : null}
|
|
172
|
-
role="switch"
|
|
173
|
-
aria-checked={checked}
|
|
174
|
-
aria-labelledby={labelledBy}
|
|
175
|
-
aria-required={required}
|
|
176
|
-
data-state={getState(checked)}
|
|
177
|
-
data-disabled={disabled ? "" : void 0}
|
|
178
|
-
disabled={disabled}
|
|
179
|
-
tabIndex={disabled ? void 0 : 0}
|
|
180
|
-
value={value}
|
|
181
|
-
{...switchProps}
|
|
182
|
-
ref={composedRefs}
|
|
183
|
-
onPress={(event) => {
|
|
184
|
-
props.onPress?.(event);
|
|
185
|
-
setChecked((prevChecked) => !prevChecked);
|
|
186
|
-
if (isWeb && isFormControl) {
|
|
187
|
-
hasConsumerStoppedPropagationRef.current = event.isPropagationStopped();
|
|
188
|
-
if (!hasConsumerStoppedPropagationRef.current)
|
|
189
|
-
event.stopPropagation();
|
|
190
|
-
}
|
|
191
|
-
}}
|
|
192
|
-
/>
|
|
193
|
-
{isWeb && isFormControl && <BubbleInput
|
|
194
|
-
control={button}
|
|
195
|
-
bubbles={!hasConsumerStoppedPropagationRef.current}
|
|
196
|
-
name={name}
|
|
197
|
-
value={value}
|
|
198
|
-
checked={checked}
|
|
199
|
-
required={required}
|
|
200
|
-
disabled={disabled}
|
|
201
|
-
style={{ transform: "translateX(-100%)" }}
|
|
202
|
-
/>}
|
|
203
|
-
</SwitchProvider>;
|
|
204
|
-
}
|
|
205
|
-
),
|
|
206
|
-
{
|
|
207
|
-
// because they may set a variant to be checked, we need to make it also pass checked down
|
|
208
|
-
inlineProps: /* @__PURE__ */ new Set(["checked"])
|
|
209
|
-
}
|
|
210
|
-
);
|
|
211
|
-
const Switch = withStaticProperties(SwitchComponent, {
|
|
212
|
-
Thumb: SwitchThumb
|
|
74
|
+
const Switch = createSwitch({
|
|
75
|
+
Frame: SwitchFrame,
|
|
76
|
+
Thumb: SwitchThumb,
|
|
77
|
+
acceptsUnstyled: true
|
|
213
78
|
});
|
|
214
|
-
const BubbleInput = (props) => {
|
|
215
|
-
const { control, checked, bubbles = true, ...inputProps } = props;
|
|
216
|
-
const ref = React.useRef(null);
|
|
217
|
-
const prevChecked = usePrevious(checked);
|
|
218
|
-
React.useEffect(() => {
|
|
219
|
-
const input = ref.current;
|
|
220
|
-
const inputProto = window.HTMLInputElement.prototype;
|
|
221
|
-
const descriptor = Object.getOwnPropertyDescriptor(
|
|
222
|
-
inputProto,
|
|
223
|
-
"checked"
|
|
224
|
-
);
|
|
225
|
-
const setChecked = descriptor.set;
|
|
226
|
-
if (prevChecked !== checked && setChecked) {
|
|
227
|
-
const event = new Event("click", { bubbles });
|
|
228
|
-
setChecked.call(input, checked);
|
|
229
|
-
input.dispatchEvent(event);
|
|
230
|
-
}
|
|
231
|
-
}, [prevChecked, checked, bubbles]);
|
|
232
|
-
return <input
|
|
233
|
-
type="checkbox"
|
|
234
|
-
aria-hidden
|
|
235
|
-
defaultChecked={checked}
|
|
236
|
-
{...inputProps}
|
|
237
|
-
tabIndex={-1}
|
|
238
|
-
ref={ref}
|
|
239
|
-
style={{
|
|
240
|
-
...props.style,
|
|
241
|
-
// ...controlSize,
|
|
242
|
-
position: "absolute",
|
|
243
|
-
pointerEvents: "none",
|
|
244
|
-
opacity: 0,
|
|
245
|
-
margin: 0
|
|
246
|
-
}}
|
|
247
|
-
/>;
|
|
248
|
-
};
|
|
249
|
-
function getState(checked) {
|
|
250
|
-
return checked ? "checked" : "unchecked";
|
|
251
|
-
}
|
|
252
79
|
export {
|
|
253
80
|
Switch,
|
|
254
81
|
SwitchFrame,
|
|
255
|
-
SwitchThumb
|
|
256
|
-
SwitchThumbFrame,
|
|
257
|
-
createSwitchScope
|
|
82
|
+
SwitchThumb
|
|
258
83
|
};
|
|
259
84
|
//# sourceMappingURL=Switch.js.map
|
package/dist/jsx/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,SA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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,219 @@
|
|
|
1
|
+
import { useComposedRefs } from "@tamagui/compose-refs";
|
|
2
|
+
import {
|
|
3
|
+
composeEventHandlers,
|
|
4
|
+
createStyledContext,
|
|
5
|
+
getVariableValue,
|
|
6
|
+
isWeb,
|
|
7
|
+
useProps,
|
|
8
|
+
withStaticProperties
|
|
9
|
+
} from "@tamagui/core";
|
|
10
|
+
import { registerFocusable } from "@tamagui/focusable";
|
|
11
|
+
import { getSize } from "@tamagui/get-token";
|
|
12
|
+
import { useLabelContext } from "@tamagui/label";
|
|
13
|
+
import { useControllableState } from "@tamagui/use-controllable-state";
|
|
14
|
+
import { usePrevious } from "@tamagui/use-previous";
|
|
15
|
+
import * as React from "react";
|
|
16
|
+
import {
|
|
17
|
+
Switch as NativeSwitch,
|
|
18
|
+
Platform
|
|
19
|
+
} from "react-native";
|
|
20
|
+
const SwitchContext = createStyledContext({
|
|
21
|
+
checked: false,
|
|
22
|
+
disabled: false,
|
|
23
|
+
size: void 0,
|
|
24
|
+
frameWidth: 60,
|
|
25
|
+
unstyled: false
|
|
26
|
+
});
|
|
27
|
+
function createSwitch({ Frame, Thumb, acceptsUnstyled }) {
|
|
28
|
+
const SwitchThumb = Thumb.styleable(function SwitchThumb2(props, forwardedRef) {
|
|
29
|
+
const { size: sizeProp, ...thumbProps } = props;
|
|
30
|
+
const { disabled, checked, unstyled, frameWidth } = React.useContext(SwitchContext);
|
|
31
|
+
const [thumbWidth, setThumbWidth] = React.useState(0);
|
|
32
|
+
return (
|
|
33
|
+
// @ts-ignore
|
|
34
|
+
<Thumb
|
|
35
|
+
theme={unstyled === false && checked ? "active" : null}
|
|
36
|
+
data-state={getState(checked)}
|
|
37
|
+
data-disabled={disabled ? "" : void 0}
|
|
38
|
+
x={checked ? frameWidth - thumbWidth : 0}
|
|
39
|
+
{...thumbProps}
|
|
40
|
+
onLayout={composeEventHandlers(
|
|
41
|
+
props.onLayout,
|
|
42
|
+
(e) => (
|
|
43
|
+
// @ts-ignore
|
|
44
|
+
setThumbWidth(e.nativeEvent.layout.width)
|
|
45
|
+
)
|
|
46
|
+
)}
|
|
47
|
+
ref={forwardedRef}
|
|
48
|
+
/>
|
|
49
|
+
);
|
|
50
|
+
});
|
|
51
|
+
const SwitchComponent = Frame.extractable(
|
|
52
|
+
React.forwardRef(function SwitchFrame(propsIn, forwardedRef) {
|
|
53
|
+
const styledContext = React.useContext(SwitchContext);
|
|
54
|
+
const props = useProps(propsIn);
|
|
55
|
+
const {
|
|
56
|
+
labeledBy: ariaLabelledby,
|
|
57
|
+
name,
|
|
58
|
+
checked: checkedProp,
|
|
59
|
+
defaultChecked,
|
|
60
|
+
required,
|
|
61
|
+
disabled,
|
|
62
|
+
value = "on",
|
|
63
|
+
onCheckedChange,
|
|
64
|
+
size = styledContext.size ?? "$true",
|
|
65
|
+
unstyled = styledContext.unstyled ?? false,
|
|
66
|
+
native: nativeProp,
|
|
67
|
+
nativeProps,
|
|
68
|
+
...switchProps
|
|
69
|
+
} = props;
|
|
70
|
+
const leftBorderWidth = (() => {
|
|
71
|
+
let _ = void 0;
|
|
72
|
+
for (const key in switchProps) {
|
|
73
|
+
if (key === "borderWidth" || key === "borderLeftWidth") {
|
|
74
|
+
_ = switchProps[key];
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
if (acceptsUnstyled && _ === void 0 && unstyled === false) {
|
|
78
|
+
_ = 2;
|
|
79
|
+
}
|
|
80
|
+
if (typeof _ === "string") {
|
|
81
|
+
_ = getVariableValue(getSize(_));
|
|
82
|
+
}
|
|
83
|
+
return +_;
|
|
84
|
+
})();
|
|
85
|
+
const native = Array.isArray(nativeProp) ? nativeProp : [nativeProp];
|
|
86
|
+
const shouldRenderMobileNative = !isWeb && nativeProp === true || !isWeb && native.includes("mobile") || native.includes("android") && Platform.OS === "android" || native.includes("ios") && Platform.OS === "ios";
|
|
87
|
+
const [button, setButton] = React.useState(null);
|
|
88
|
+
const composedRefs = useComposedRefs(forwardedRef, (node) => setButton(node));
|
|
89
|
+
const labelId = useLabelContext(button);
|
|
90
|
+
const labelledBy = ariaLabelledby || labelId;
|
|
91
|
+
const hasConsumerStoppedPropagationRef = React.useRef(false);
|
|
92
|
+
const isFormControl = isWeb ? button ? Boolean(button.closest("form")) : true : false;
|
|
93
|
+
const [frameWidth, setFrameWidth] = React.useState(60);
|
|
94
|
+
const [checked = false, setChecked] = useControllableState({
|
|
95
|
+
prop: checkedProp,
|
|
96
|
+
defaultProp: defaultChecked || false,
|
|
97
|
+
onChange: onCheckedChange,
|
|
98
|
+
transition: true
|
|
99
|
+
});
|
|
100
|
+
if (shouldRenderMobileNative) {
|
|
101
|
+
return <NativeSwitch
|
|
102
|
+
value={checkedProp}
|
|
103
|
+
onValueChange={onCheckedChange}
|
|
104
|
+
{...nativeProps}
|
|
105
|
+
/>;
|
|
106
|
+
}
|
|
107
|
+
if (!isWeb) {
|
|
108
|
+
React.useEffect(() => {
|
|
109
|
+
if (!props.id)
|
|
110
|
+
return;
|
|
111
|
+
return registerFocusable(props.id, {
|
|
112
|
+
focus: () => {
|
|
113
|
+
setChecked((x) => !x);
|
|
114
|
+
}
|
|
115
|
+
});
|
|
116
|
+
}, [props.id, setChecked]);
|
|
117
|
+
}
|
|
118
|
+
return <>
|
|
119
|
+
{
|
|
120
|
+
/* @ts-ignore */
|
|
121
|
+
}
|
|
122
|
+
<Frame
|
|
123
|
+
unstyled={unstyled}
|
|
124
|
+
size={size}
|
|
125
|
+
checked={checked}
|
|
126
|
+
disabled={disabled}
|
|
127
|
+
frameWidth={frameWidth - leftBorderWidth * 2}
|
|
128
|
+
theme={checked ? "active" : null}
|
|
129
|
+
themeShallow
|
|
130
|
+
role="switch"
|
|
131
|
+
aria-checked={checked}
|
|
132
|
+
aria-labelledby={labelledBy}
|
|
133
|
+
aria-required={required}
|
|
134
|
+
data-state={getState(checked)}
|
|
135
|
+
data-disabled={disabled ? "" : void 0}
|
|
136
|
+
tabIndex={disabled ? void 0 : 0}
|
|
137
|
+
value={value}
|
|
138
|
+
{...switchProps}
|
|
139
|
+
ref={composedRefs}
|
|
140
|
+
onPress={composeEventHandlers(props.onPress, (event) => {
|
|
141
|
+
setChecked((prevChecked) => !prevChecked);
|
|
142
|
+
if (isWeb && isFormControl) {
|
|
143
|
+
hasConsumerStoppedPropagationRef.current = event.isPropagationStopped();
|
|
144
|
+
if (!hasConsumerStoppedPropagationRef.current)
|
|
145
|
+
event.stopPropagation();
|
|
146
|
+
}
|
|
147
|
+
})}
|
|
148
|
+
onLayout={composeEventHandlers(
|
|
149
|
+
props.onLayout,
|
|
150
|
+
(e) => (
|
|
151
|
+
// @ts-ignore
|
|
152
|
+
setFrameWidth(e.nativeEvent.layout.width)
|
|
153
|
+
)
|
|
154
|
+
)}
|
|
155
|
+
/>
|
|
156
|
+
{isWeb && isFormControl && <BubbleInput
|
|
157
|
+
control={button}
|
|
158
|
+
bubbles={!hasConsumerStoppedPropagationRef.current}
|
|
159
|
+
name={name}
|
|
160
|
+
value={value}
|
|
161
|
+
checked={checked}
|
|
162
|
+
required={required}
|
|
163
|
+
disabled={disabled}
|
|
164
|
+
style={{ transform: "translateX(-100%)" }}
|
|
165
|
+
/>}
|
|
166
|
+
</>;
|
|
167
|
+
})
|
|
168
|
+
);
|
|
169
|
+
const BubbleInput = (props) => {
|
|
170
|
+
const { control, checked, bubbles = true, ...inputProps } = props;
|
|
171
|
+
const ref = React.useRef(null);
|
|
172
|
+
const prevChecked = usePrevious(checked);
|
|
173
|
+
React.useEffect(() => {
|
|
174
|
+
const input = ref.current;
|
|
175
|
+
const inputProto = window.HTMLInputElement.prototype;
|
|
176
|
+
const descriptor = Object.getOwnPropertyDescriptor(
|
|
177
|
+
inputProto,
|
|
178
|
+
"checked"
|
|
179
|
+
);
|
|
180
|
+
const setChecked = descriptor.set;
|
|
181
|
+
if (prevChecked !== checked && setChecked) {
|
|
182
|
+
const event = new Event("click", { bubbles });
|
|
183
|
+
setChecked.call(input, checked);
|
|
184
|
+
input.dispatchEvent(event);
|
|
185
|
+
}
|
|
186
|
+
}, [prevChecked, checked, bubbles]);
|
|
187
|
+
return (
|
|
188
|
+
// @ts-ignore
|
|
189
|
+
<input
|
|
190
|
+
type="checkbox"
|
|
191
|
+
aria-hidden
|
|
192
|
+
defaultChecked={checked}
|
|
193
|
+
{...inputProps}
|
|
194
|
+
tabIndex={-1}
|
|
195
|
+
ref={ref}
|
|
196
|
+
style={{
|
|
197
|
+
...props.style,
|
|
198
|
+
// ...controlSize,
|
|
199
|
+
position: "absolute",
|
|
200
|
+
pointerEvents: "none",
|
|
201
|
+
opacity: 0,
|
|
202
|
+
margin: 0
|
|
203
|
+
}}
|
|
204
|
+
/>
|
|
205
|
+
);
|
|
206
|
+
};
|
|
207
|
+
function getState(checked) {
|
|
208
|
+
return checked ? "checked" : "unchecked";
|
|
209
|
+
}
|
|
210
|
+
const Switch = withStaticProperties(SwitchComponent, {
|
|
211
|
+
Thumb: SwitchThumb
|
|
212
|
+
});
|
|
213
|
+
return Switch;
|
|
214
|
+
}
|
|
215
|
+
export {
|
|
216
|
+
SwitchContext,
|
|
217
|
+
createSwitch
|
|
218
|
+
};
|
|
219
|
+
//# sourceMappingURL=createSwitch.js.map
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/createSwitch.tsx"],
|
|
4
|
+
"mappings": "AAAA,SAAS,uBAAuB;AAChC;AAAA,EAME;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,SAAS,yBAAyB;AAClC,SAAS,eAAe;A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;AAAA,IAEJ,CAAC;AAAA,EACH;AAaA,QAAM,cAAc,CAAC,UAA4B;AAC/C,UAAM,EAAE,SAAS,SAAS,UAAU,MAAM,GAAG,WAAW,IAAI;AAC5D,UAAM,MAAM,MAAM,OAAyB,IAAI;AAC/C,UAAM,cAAc,YAAY,OAAO;AAIvC,UAAM,UAAU,MAAM;AACpB,YAAM,QAAQ,IAAI;AAClB,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,CAAC;AAAA,QACC,KAAK;AAAA,QACL;AAAA,QACA,gBAAgB;AAAA,YACZ;AAAA,QACJ,UAAU;AAAA,QACV,KAAK;AAAA,QACL,OAAO;AAAA,UACL,GAAG,MAAM;AAAA;AAAA,UAET,UAAU;AAAA,UACV,eAAe;AAAA,UACf,SAAS;AAAA,UACT,QAAQ;AAAA,QACV;AAAA,MACF;AAAA;AAAA,EAEJ;AAEA,WAAS,SAAS,SAAkB;AAClC,WAAO,UAAU,YAAY;AAAA,EAC/B;AAEA,QAAM,SAAS,qBAAqB,iBAAiB;AAAA,IACnD,OAAO;AAAA,EACT,CAAC;AAED,SAAO;AACT;",
|
|
5
|
+
"names": ["SwitchThumb"]
|
|
6
|
+
}
|
package/dist/jsx/index.js
CHANGED
package/dist/jsx/index.js.map
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tamagui/switch",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.58.0",
|
|
4
4
|
"sideEffects": [
|
|
5
5
|
"*.css"
|
|
6
6
|
],
|
|
@@ -31,22 +31,22 @@
|
|
|
31
31
|
}
|
|
32
32
|
},
|
|
33
33
|
"dependencies": {
|
|
34
|
-
"@tamagui/compose-refs": "1.
|
|
35
|
-
"@tamagui/core": "1.
|
|
36
|
-
"@tamagui/create-context": "1.
|
|
37
|
-
"@tamagui/focusable": "1.
|
|
38
|
-
"@tamagui/get-token": "1.
|
|
39
|
-
"@tamagui/label": "1.
|
|
40
|
-
"@tamagui/stacks": "1.
|
|
41
|
-
"@tamagui/use-controllable-state": "1.
|
|
42
|
-
"@tamagui/use-previous": "1.
|
|
34
|
+
"@tamagui/compose-refs": "1.58.0",
|
|
35
|
+
"@tamagui/core": "1.58.0",
|
|
36
|
+
"@tamagui/create-context": "1.58.0",
|
|
37
|
+
"@tamagui/focusable": "1.58.0",
|
|
38
|
+
"@tamagui/get-token": "1.58.0",
|
|
39
|
+
"@tamagui/label": "1.58.0",
|
|
40
|
+
"@tamagui/stacks": "1.58.0",
|
|
41
|
+
"@tamagui/use-controllable-state": "1.58.0",
|
|
42
|
+
"@tamagui/use-previous": "1.58.0"
|
|
43
43
|
},
|
|
44
44
|
"peerDependencies": {
|
|
45
45
|
"react": "*",
|
|
46
46
|
"react-native": "*"
|
|
47
47
|
},
|
|
48
48
|
"devDependencies": {
|
|
49
|
-
"@tamagui/build": "1.
|
|
49
|
+
"@tamagui/build": "1.58.0",
|
|
50
50
|
"react": "^18.2.0",
|
|
51
51
|
"react-native": "^0.72.1"
|
|
52
52
|
},
|