@tamagui/switch 1.123.14 → 1.123.16
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/createSwitch.cjs +21 -8
- package/dist/cjs/createSwitch.js +20 -13
- package/dist/cjs/createSwitch.js.map +1 -1
- package/dist/cjs/createSwitch.native.js +15 -10
- package/dist/cjs/createSwitch.native.js.map +2 -2
- package/dist/esm/createSwitch.js +22 -13
- package/dist/esm/createSwitch.js.map +1 -1
- package/dist/esm/createSwitch.mjs +22 -9
- package/dist/esm/createSwitch.mjs.map +1 -1
- package/dist/esm/createSwitch.native.js +16 -11
- package/dist/esm/createSwitch.native.js.map +2 -2
- package/dist/jsx/createSwitch.js +22 -13
- package/dist/jsx/createSwitch.js.map +1 -1
- package/dist/jsx/createSwitch.mjs +22 -9
- package/dist/jsx/createSwitch.mjs.map +1 -1
- package/dist/jsx/createSwitch.native.js +16 -11
- package/dist/jsx/createSwitch.native.js.map +2 -2
- package/package.json +13 -13
- package/src/createSwitch.tsx +39 -13
- package/types/createSwitch.d.ts.map +1 -1
|
@@ -77,7 +77,8 @@ function createSwitch(createProps) {
|
|
|
77
77
|
unstyled = process.env.TAMAGUI_HEADLESS === "1" ? !0 : unstyledProp ?? unstyledContext ?? !1,
|
|
78
78
|
size = sizeProp ?? sizeContext ?? "$true",
|
|
79
79
|
initialChecked = React.useRef(checked).current,
|
|
80
|
-
|
|
80
|
+
initialWidth = (0, import_core.getVariableValue)(props.width, "size"),
|
|
81
|
+
[thumbWidth, setThumbWidth] = React.useState(typeof initialWidth == "number" ? initialWidth : 0),
|
|
81
82
|
distance = frameWidth - thumbWidth,
|
|
82
83
|
x = initialChecked ? checked ? 0 : -distance : checked ? distance : 0;
|
|
83
84
|
return /* @__PURE__ */(0, import_jsx_runtime.jsx)(Thumb, {
|
|
@@ -91,7 +92,10 @@ function createSwitch(createProps) {
|
|
|
91
92
|
}),
|
|
92
93
|
alignSelf: initialChecked ? "flex-end" : "flex-start",
|
|
93
94
|
x,
|
|
94
|
-
onLayout: (0, import_core.composeEventHandlers)(props.onLayout, e =>
|
|
95
|
+
onLayout: (0, import_core.composeEventHandlers)(props.onLayout, e => {
|
|
96
|
+
const next = e.nativeEvent.layout.width;
|
|
97
|
+
next !== thumbWidth && setThumbWidth(next);
|
|
98
|
+
}),
|
|
95
99
|
checked,
|
|
96
100
|
disabled,
|
|
97
101
|
...thumbProps
|
|
@@ -112,8 +116,16 @@ function createSwitch(createProps) {
|
|
|
112
116
|
onChange: onCheckedChange,
|
|
113
117
|
transition: !0
|
|
114
118
|
}),
|
|
115
|
-
styledContext = React.useContext(import_StyledContext.SwitchStyledContext.context)
|
|
116
|
-
|
|
119
|
+
styledContext = React.useContext(import_StyledContext.SwitchStyledContext.context);
|
|
120
|
+
let estimatedInitialWidth = 0;
|
|
121
|
+
const estWidth = (0, import_core.getVariableValue)((0, import_core.getShorthandValue)(props, "width"), "size");
|
|
122
|
+
if (estWidth) {
|
|
123
|
+
const estPad = (0, import_core.getShorthandValue)(props, "paddingHorizontal") ?? (0, import_core.getShorthandValue)(props, "padding") ?? 0,
|
|
124
|
+
estLeftPad = (0, import_core.getShorthandValue)(props, "paddingLeft") ?? estPad ?? 0,
|
|
125
|
+
estRightPad = (0, import_core.getShorthandValue)(props, "paddingRight") ?? estPad ?? 0;
|
|
126
|
+
estimatedInitialWidth = estWidth - (estLeftPad ? (0, import_core.getVariableValue)(estLeftPad) : 0) - (estRightPad ? (0, import_core.getVariableValue)(estRightPad) : 0);
|
|
127
|
+
}
|
|
128
|
+
const [frameWidth, setFrameInnerWidth] = React.useState(estimatedInitialWidth),
|
|
117
129
|
{
|
|
118
130
|
switchProps,
|
|
119
131
|
bubbleInput,
|
|
@@ -133,9 +145,10 @@ function createSwitch(createProps) {
|
|
|
133
145
|
disabled,
|
|
134
146
|
frameWidth
|
|
135
147
|
}), [checked, disabled, frameWidth]),
|
|
136
|
-
handleLayout =
|
|
137
|
-
|
|
138
|
-
|
|
148
|
+
handleLayout = e => {
|
|
149
|
+
const next = e.nativeEvent.layout.width;
|
|
150
|
+
next !== frameWidth && setFrameInnerWidth(next);
|
|
151
|
+
},
|
|
139
152
|
unstyled = styledContext.unstyled ?? props.unstyled ?? !1;
|
|
140
153
|
return /* @__PURE__ */(0, import_jsx_runtime.jsxs)(SwitchContext.Provider, {
|
|
141
154
|
value,
|
|
@@ -160,7 +173,7 @@ function createSwitch(createProps) {
|
|
|
160
173
|
children: /* @__PURE__ */(0, import_jsx_runtime.jsx)(import_react_native.View, {
|
|
161
174
|
style: measureContainerStyle,
|
|
162
175
|
onLayout: handleLayout,
|
|
163
|
-
children: props.children
|
|
176
|
+
children: frameWidth ? props.children : null
|
|
164
177
|
})
|
|
165
178
|
}), bubbleInput]
|
|
166
179
|
});
|
package/dist/cjs/createSwitch.js
CHANGED
|
@@ -43,7 +43,9 @@ function createSwitch(createProps) {
|
|
|
43
43
|
), Frame.staticConfig.context = import_StyledContext.SwitchStyledContext, Thumb.staticConfig.context = import_StyledContext.SwitchStyledContext;
|
|
44
44
|
const SwitchThumbComponent = Thumb.styleable(
|
|
45
45
|
function(props, forwardedRef) {
|
|
46
|
-
const { size: sizeProp, unstyled: unstyledProp, nativeID, ...thumbProps } = props, context = React.useContext(SwitchContext), { checked, disabled, frameWidth } = context, styledContext = import_StyledContext.SwitchStyledContext.useStyledContext(), { unstyled: unstyledContext, size: sizeContext } = styledContext, unstyled = process.env.TAMAGUI_HEADLESS === "1" ? !0 : unstyledProp ?? unstyledContext ?? !1, size = sizeProp ?? sizeContext ?? "$true", initialChecked = React.useRef(checked).current,
|
|
46
|
+
const { size: sizeProp, unstyled: unstyledProp, nativeID, ...thumbProps } = props, context = React.useContext(SwitchContext), { checked, disabled, frameWidth } = context, styledContext = import_StyledContext.SwitchStyledContext.useStyledContext(), { unstyled: unstyledContext, size: sizeContext } = styledContext, unstyled = process.env.TAMAGUI_HEADLESS === "1" ? !0 : unstyledProp ?? unstyledContext ?? !1, size = sizeProp ?? sizeContext ?? "$true", initialChecked = React.useRef(checked).current, initialWidth = (0, import_core.getVariableValue)(props.width, "size"), [thumbWidth, setThumbWidth] = React.useState(
|
|
47
|
+
typeof initialWidth == "number" ? initialWidth : 0
|
|
48
|
+
), distance = frameWidth - thumbWidth, x = initialChecked ? checked ? 0 : -distance : checked ? distance : 0;
|
|
47
49
|
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
48
50
|
Thumb,
|
|
49
51
|
{
|
|
@@ -57,10 +59,10 @@ function createSwitch(createProps) {
|
|
|
57
59
|
},
|
|
58
60
|
alignSelf: initialChecked ? "flex-end" : "flex-start",
|
|
59
61
|
x,
|
|
60
|
-
onLayout: (0, import_core.composeEventHandlers)(
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
),
|
|
62
|
+
onLayout: (0, import_core.composeEventHandlers)(props.onLayout, (e) => {
|
|
63
|
+
const next = e.nativeEvent.layout.width;
|
|
64
|
+
next !== thumbWidth && setThumbWidth(next);
|
|
65
|
+
}),
|
|
64
66
|
checked,
|
|
65
67
|
disabled,
|
|
66
68
|
...thumbProps
|
|
@@ -81,7 +83,14 @@ function createSwitch(createProps) {
|
|
|
81
83
|
defaultProp: defaultChecked || !1,
|
|
82
84
|
onChange: onCheckedChange,
|
|
83
85
|
transition: !0
|
|
84
|
-
}), styledContext = React.useContext(import_StyledContext.SwitchStyledContext.context)
|
|
86
|
+
}), styledContext = React.useContext(import_StyledContext.SwitchStyledContext.context);
|
|
87
|
+
let estimatedInitialWidth = 0;
|
|
88
|
+
const estWidth = (0, import_core.getVariableValue)((0, import_core.getShorthandValue)(props, "width"), "size");
|
|
89
|
+
if (estWidth) {
|
|
90
|
+
const estPad = (0, import_core.getShorthandValue)(props, "paddingHorizontal") ?? (0, import_core.getShorthandValue)(props, "padding") ?? 0, estLeftPad = (0, import_core.getShorthandValue)(props, "paddingLeft") ?? estPad ?? 0, estRightPad = (0, import_core.getShorthandValue)(props, "paddingRight") ?? estPad ?? 0;
|
|
91
|
+
estimatedInitialWidth = estWidth - (estLeftPad ? (0, import_core.getVariableValue)(estLeftPad) : 0) - (estRightPad ? (0, import_core.getVariableValue)(estRightPad) : 0);
|
|
92
|
+
}
|
|
93
|
+
const [frameWidth, setFrameInnerWidth] = React.useState(estimatedInitialWidth), { switchProps, bubbleInput, switchRef } = (0, import_switch_headless.useSwitch)(
|
|
85
94
|
// @ts-ignore
|
|
86
95
|
props,
|
|
87
96
|
[checked, setChecked],
|
|
@@ -92,12 +101,10 @@ function createSwitch(createProps) {
|
|
|
92
101
|
const disabled = props.disabled, value = React.useMemo(
|
|
93
102
|
() => ({ checked, disabled, frameWidth }),
|
|
94
103
|
[checked, disabled, frameWidth]
|
|
95
|
-
), handleLayout =
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
[setFrameWidth]
|
|
100
|
-
), unstyled = styledContext.unstyled ?? props.unstyled ?? !1;
|
|
104
|
+
), handleLayout = (e) => {
|
|
105
|
+
const next = e.nativeEvent.layout.width;
|
|
106
|
+
next !== frameWidth && setFrameInnerWidth(next);
|
|
107
|
+
}, unstyled = styledContext.unstyled ?? props.unstyled ?? !1;
|
|
101
108
|
return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(SwitchContext.Provider, { value, children: [
|
|
102
109
|
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
103
110
|
Frame,
|
|
@@ -117,7 +124,7 @@ function createSwitch(createProps) {
|
|
|
117
124
|
},
|
|
118
125
|
checked,
|
|
119
126
|
disabled,
|
|
120
|
-
children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_native.View, { style: measureContainerStyle, onLayout: handleLayout, children: props.children })
|
|
127
|
+
children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_native.View, { style: measureContainerStyle, onLayout: handleLayout, children: frameWidth ? props.children : null })
|
|
121
128
|
}
|
|
122
129
|
),
|
|
123
130
|
bubbleInput
|
|
@@ -1,6 +1,6 @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A,IAEJ;AAAA,IACA;AAAA,MACE,cAAc;AAAA,IAChB;AAAA,EACF;AAEA,aAAO,kCAAqB,iBAAiB;AAAA,IAC3C,OAAO;AAAA,EACT,CAAC;AACH;AAEA,MAAM,wBAAwB;AAAA,EAC5B,WAAW;AAAA,EACX,MAAM;AACR;",
|
|
5
5
|
"names": ["DefaultSwitchFrame", "NativeSwitch"]
|
|
6
6
|
}
|
|
@@ -36,7 +36,7 @@ function createSwitch(createProps) {
|
|
|
36
36
|
var { disableActiveTheme, Frame = import_Switch.SwitchFrame, Thumb = import_Switch.SwitchThumb } = createProps;
|
|
37
37
|
process.env.NODE_ENV === "development" && (Frame !== import_Switch.SwitchFrame && Frame.staticConfig.context && Frame.staticConfig.context !== import_StyledContext.SwitchStyledContext || Thumb !== import_Switch.SwitchThumb && Thumb.staticConfig.context && Thumb.staticConfig.context !== import_StyledContext.SwitchStyledContext) && console.warn("Warning: createSwitch() needs to control context to pass checked state from Frame to Thumb, any custom context passed will be overridden."), Frame.staticConfig.context = import_StyledContext.SwitchStyledContext, Thumb.staticConfig.context = import_StyledContext.SwitchStyledContext;
|
|
38
38
|
var SwitchThumbComponent = Thumb.styleable(function(props, forwardedRef) {
|
|
39
|
-
var { size: sizeProp, unstyled: unstyledProp, nativeID, ...thumbProps } = props, context = React.useContext(SwitchContext), { checked, disabled, frameWidth } = context, styledContext = import_StyledContext.SwitchStyledContext.useStyledContext(), { unstyled: unstyledContext, size: sizeContext } = styledContext, _ref, unstyled = process.env.TAMAGUI_HEADLESS === "1" ? !0 : (_ref = unstyledProp ?? unstyledContext) !== null && _ref !== void 0 ? _ref : !1, _ref1, size = (_ref1 = sizeProp ?? sizeContext) !== null && _ref1 !== void 0 ? _ref1 : "$true", initialChecked = React.useRef(checked).current, [thumbWidth, setThumbWidth] = React.useState(0), distance = frameWidth - thumbWidth, x = initialChecked ? checked ? 0 : -distance : checked ? distance : 0;
|
|
39
|
+
var { size: sizeProp, unstyled: unstyledProp, nativeID, ...thumbProps } = props, context = React.useContext(SwitchContext), { checked, disabled, frameWidth } = context, styledContext = import_StyledContext.SwitchStyledContext.useStyledContext(), { unstyled: unstyledContext, size: sizeContext } = styledContext, _ref, unstyled = process.env.TAMAGUI_HEADLESS === "1" ? !0 : (_ref = unstyledProp ?? unstyledContext) !== null && _ref !== void 0 ? _ref : !1, _ref1, size = (_ref1 = sizeProp ?? sizeContext) !== null && _ref1 !== void 0 ? _ref1 : "$true", initialChecked = React.useRef(checked).current, initialWidth = (0, import_core.getVariableValue)(props.width, "size"), [thumbWidth, setThumbWidth] = React.useState(typeof initialWidth == "number" ? initialWidth : 0), distance = frameWidth - thumbWidth, x = initialChecked ? checked ? 0 : -distance : checked ? distance : 0;
|
|
40
40
|
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Thumb, {
|
|
41
41
|
ref: forwardedRef,
|
|
42
42
|
unstyled,
|
|
@@ -49,7 +49,8 @@ function createSwitch(createProps) {
|
|
|
49
49
|
alignSelf: initialChecked ? "flex-end" : "flex-start",
|
|
50
50
|
x,
|
|
51
51
|
onLayout: (0, import_core.composeEventHandlers)(props.onLayout, function(e) {
|
|
52
|
-
|
|
52
|
+
var next = e.nativeEvent.layout.width;
|
|
53
|
+
next !== thumbWidth && setThumbWidth(next);
|
|
53
54
|
}),
|
|
54
55
|
// expected variants
|
|
55
56
|
checked,
|
|
@@ -62,7 +63,12 @@ function createSwitch(createProps) {
|
|
|
62
63
|
defaultProp: defaultChecked || !1,
|
|
63
64
|
onChange: onCheckedChange,
|
|
64
65
|
transition: !0
|
|
65
|
-
}), styledContext = React.useContext(import_StyledContext.SwitchStyledContext.context),
|
|
66
|
+
}), styledContext = React.useContext(import_StyledContext.SwitchStyledContext.context), estimatedInitialWidth = 0, estWidth = (0, import_core.getVariableValue)((0, import_core.getShorthandValue)(props, "width"), "size");
|
|
67
|
+
if (estWidth) {
|
|
68
|
+
var _getShorthandValue, _ref, estPad = (_ref = (_getShorthandValue = (0, import_core.getShorthandValue)(props, "paddingHorizontal")) !== null && _getShorthandValue !== void 0 ? _getShorthandValue : (0, import_core.getShorthandValue)(props, "padding")) !== null && _ref !== void 0 ? _ref : 0, _getShorthandValue1, _ref1, estLeftPad = (_ref1 = (_getShorthandValue1 = (0, import_core.getShorthandValue)(props, "paddingLeft")) !== null && _getShorthandValue1 !== void 0 ? _getShorthandValue1 : estPad) !== null && _ref1 !== void 0 ? _ref1 : 0, _getShorthandValue2, _ref2, estRightPad = (_ref2 = (_getShorthandValue2 = (0, import_core.getShorthandValue)(props, "paddingRight")) !== null && _getShorthandValue2 !== void 0 ? _getShorthandValue2 : estPad) !== null && _ref2 !== void 0 ? _ref2 : 0;
|
|
69
|
+
estimatedInitialWidth = estWidth - (estLeftPad ? (0, import_core.getVariableValue)(estLeftPad) : 0) - (estRightPad ? (0, import_core.getVariableValue)(estRightPad) : 0);
|
|
70
|
+
}
|
|
71
|
+
var [frameWidth, setFrameInnerWidth] = React.useState(estimatedInitialWidth), { switchProps, bubbleInput, switchRef } = (0, import_switch_headless.useSwitch)(
|
|
66
72
|
// @ts-ignore
|
|
67
73
|
props,
|
|
68
74
|
[
|
|
@@ -103,11 +109,10 @@ function createSwitch(createProps) {
|
|
|
103
109
|
checked,
|
|
104
110
|
disabled,
|
|
105
111
|
frameWidth
|
|
106
|
-
]), handleLayout =
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
]), _styledContext_unstyled, _ref, unstyled = (_ref = (_styledContext_unstyled = styledContext.unstyled) !== null && _styledContext_unstyled !== void 0 ? _styledContext_unstyled : props.unstyled) !== null && _ref !== void 0 ? _ref : !1, _styledContext_size, _ref1;
|
|
112
|
+
]), handleLayout = function(e) {
|
|
113
|
+
var next = e.nativeEvent.layout.width;
|
|
114
|
+
next !== frameWidth && setFrameInnerWidth(next);
|
|
115
|
+
}, _styledContext_unstyled, _ref3, unstyled = (_ref3 = (_styledContext_unstyled = styledContext.unstyled) !== null && _styledContext_unstyled !== void 0 ? _styledContext_unstyled : props.unstyled) !== null && _ref3 !== void 0 ? _ref3 : !1, _styledContext_size, _ref4;
|
|
111
116
|
return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(SwitchContext.Provider, {
|
|
112
117
|
value,
|
|
113
118
|
children: [
|
|
@@ -118,7 +123,7 @@ function createSwitch(createProps) {
|
|
|
118
123
|
type: "button"
|
|
119
124
|
},
|
|
120
125
|
...!unstyled && {
|
|
121
|
-
size: (
|
|
126
|
+
size: (_ref4 = (_styledContext_size = styledContext.size) !== null && _styledContext_size !== void 0 ? _styledContext_size : props.size) !== null && _ref4 !== void 0 ? _ref4 : "$true"
|
|
122
127
|
},
|
|
123
128
|
unstyled,
|
|
124
129
|
...props,
|
|
@@ -133,7 +138,7 @@ function createSwitch(createProps) {
|
|
|
133
138
|
children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_native.View, {
|
|
134
139
|
style: measureContainerStyle,
|
|
135
140
|
onLayout: handleLayout,
|
|
136
|
-
children: props.children
|
|
141
|
+
children: frameWidth ? props.children : null
|
|
137
142
|
})
|
|
138
143
|
}),
|
|
139
144
|
bubbleInput
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/Users/n8/tamagui/code/ui/switch/src/createSwitch.tsx"],
|
|
4
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;uDACA,
|
|
5
|
-
"names": ["SwitchContext", "React", "createContext", "checked", "disabled", "frameWidth", "createSwitch", "createProps", "disableActiveTheme", "Frame", "DefaultSwitchFrame", "Thumb", "SwitchThumb", "process", "env", "NODE_ENV", "staticConfig", "context", "SwitchStyledContext", "console", "warn", "SwitchThumbComponent", "styleable", "props", "forwardedRef", "size", "sizeProp", "unstyled", "unstyledProp", "nativeID", "thumbProps", "useContext", "styledContext", "useStyledContext", "unstyledContext", "sizeContext", "TAMAGUI_HEADLESS", "initialChecked", "useRef", "current", "thumbWidth", "setThumbWidth", "useState",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AAXL,YAAWK,SAAAA,0BAAAA,cAAcL,cAAQ,QAAtBK,4BAAAA,SAAAA,0BAA0BT,MAAMI,cAAQ,QAAxCK,UAAAA,SAAAA,QAA4C,IAS/CA,qBAAAA;AAPd,WACE,uCAAA8D,MAAC9F,cAAc+F,UAAQ;MAACT;;QACtB,uCAAAtC,KAACvC,OAAAA;UACCwC,KAAK+B;UACLgB,KAAI;UACH,GAAIC,qBAAS;YAAEC,MAAM;UAAS;UAC9B,GAAI,CAACvE,YAAY;YAChBF,OAAMO,SAAAA,sBAAAA,cAAcP,UAAI,QAAlBO,wBAAAA,SAAAA,sBAAsBT,MAAME,UAAI,QAAhCO,UAAAA,SAAAA,QAAoC;UAC5C;UACAL;UACC,GAAGJ;UACH,GAAIuD;UACJ,GAAI,CAACtE,sBACJ,CAACe,MAAMI,YAAY;YACjBuB,OAAO/C,UAAU,WAAW;YAC5BgG,cAAc;UAChB;;UAEFhG;UACAC;oBAEA,uCAAA4C,KAACoD,0BAAAA;YAAKC,OAAOC;YAAuBlD,UAAUyC;sBAC1CxF,aAAoBkB,MAAMgF,WAAb;;;QAIlBxB;;;EAGP,GACA;IACEyB,cAAc;EAChB,CAAA;AAGF,aAAOC,kCAAqB/C,iBAAiB;IAC3C/C,OAAOU;EACT,CAAA;AACF;AAEA,IAAMiF,wBAAwB;EAC5BnD,WAAW;EACXuD,MAAM;AACR;",
|
|
5
|
+
"names": ["SwitchContext", "React", "createContext", "checked", "disabled", "frameWidth", "createSwitch", "createProps", "disableActiveTheme", "Frame", "DefaultSwitchFrame", "Thumb", "SwitchThumb", "process", "env", "NODE_ENV", "staticConfig", "context", "SwitchStyledContext", "console", "warn", "SwitchThumbComponent", "styleable", "props", "forwardedRef", "size", "sizeProp", "unstyled", "unstyledProp", "nativeID", "thumbProps", "useContext", "styledContext", "useStyledContext", "unstyledContext", "sizeContext", "TAMAGUI_HEADLESS", "initialChecked", "useRef", "current", "initialWidth", "getVariableValue", "width", "thumbWidth", "setThumbWidth", "useState", "distance", "x", "_jsx", "ref", "theme", "alignSelf", "onLayout", "composeEventHandlers", "e", "next", "nativeEvent", "layout", "SwitchComponent", "_props", "native", "nativeProps", "checkedProp", "defaultChecked", "onCheckedChange", "setChecked", "useControllableState", "prop", "defaultProp", "onChange", "transition", "estimatedInitialWidth", "estWidth", "getShorthandValue", "estPad", "estLeftPad", "estRightPad", "setFrameInnerWidth", "switchProps", "bubbleInput", "switchRef", "useSwitch", "useEffect", "id", "registerFocusable", "focusAndSelect", "value", "focus", "renderNative", "shouldRenderNativePlatform", "NativeSwitch", "onValueChange", "useMemo", "handleLayout", "_jsxs", "Provider", "tag", "isWeb", "type", "themeShallow", "View", "style", "measureContainerStyle", "children", "disableTheme", "withStaticProperties", "flex"]
|
|
6
6
|
}
|
package/dist/esm/createSwitch.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import {
|
|
2
2
|
composeEventHandlers,
|
|
3
|
+
getShorthandValue,
|
|
4
|
+
getVariableValue,
|
|
3
5
|
isWeb,
|
|
4
6
|
shouldRenderNativePlatform,
|
|
5
7
|
withStaticProperties
|
|
@@ -28,7 +30,9 @@ function createSwitch(createProps) {
|
|
|
28
30
|
), Frame.staticConfig.context = SwitchStyledContext, Thumb.staticConfig.context = SwitchStyledContext;
|
|
29
31
|
const SwitchThumbComponent = Thumb.styleable(
|
|
30
32
|
function(props, forwardedRef) {
|
|
31
|
-
const { size: sizeProp, unstyled: unstyledProp, nativeID, ...thumbProps } = props, context = React.useContext(SwitchContext), { checked, disabled, frameWidth } = context, styledContext = SwitchStyledContext.useStyledContext(), { unstyled: unstyledContext, size: sizeContext } = styledContext, unstyled = process.env.TAMAGUI_HEADLESS === "1" ? !0 : unstyledProp ?? unstyledContext ?? !1, size = sizeProp ?? sizeContext ?? "$true", initialChecked = React.useRef(checked).current, [thumbWidth, setThumbWidth] = React.useState(
|
|
33
|
+
const { size: sizeProp, unstyled: unstyledProp, nativeID, ...thumbProps } = props, context = React.useContext(SwitchContext), { checked, disabled, frameWidth } = context, styledContext = SwitchStyledContext.useStyledContext(), { unstyled: unstyledContext, size: sizeContext } = styledContext, unstyled = process.env.TAMAGUI_HEADLESS === "1" ? !0 : unstyledProp ?? unstyledContext ?? !1, size = sizeProp ?? sizeContext ?? "$true", initialChecked = React.useRef(checked).current, initialWidth = getVariableValue(props.width, "size"), [thumbWidth, setThumbWidth] = React.useState(
|
|
34
|
+
typeof initialWidth == "number" ? initialWidth : 0
|
|
35
|
+
), distance = frameWidth - thumbWidth, x = initialChecked ? checked ? 0 : -distance : checked ? distance : 0;
|
|
32
36
|
return /* @__PURE__ */ jsx(
|
|
33
37
|
Thumb,
|
|
34
38
|
{
|
|
@@ -42,10 +46,10 @@ function createSwitch(createProps) {
|
|
|
42
46
|
},
|
|
43
47
|
alignSelf: initialChecked ? "flex-end" : "flex-start",
|
|
44
48
|
x,
|
|
45
|
-
onLayout: composeEventHandlers(
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
),
|
|
49
|
+
onLayout: composeEventHandlers(props.onLayout, (e) => {
|
|
50
|
+
const next = e.nativeEvent.layout.width;
|
|
51
|
+
next !== thumbWidth && setThumbWidth(next);
|
|
52
|
+
}),
|
|
49
53
|
checked,
|
|
50
54
|
disabled,
|
|
51
55
|
...thumbProps
|
|
@@ -66,7 +70,14 @@ function createSwitch(createProps) {
|
|
|
66
70
|
defaultProp: defaultChecked || !1,
|
|
67
71
|
onChange: onCheckedChange,
|
|
68
72
|
transition: !0
|
|
69
|
-
}), styledContext = React.useContext(SwitchStyledContext.context)
|
|
73
|
+
}), styledContext = React.useContext(SwitchStyledContext.context);
|
|
74
|
+
let estimatedInitialWidth = 0;
|
|
75
|
+
const estWidth = getVariableValue(getShorthandValue(props, "width"), "size");
|
|
76
|
+
if (estWidth) {
|
|
77
|
+
const estPad = getShorthandValue(props, "paddingHorizontal") ?? getShorthandValue(props, "padding") ?? 0, estLeftPad = getShorthandValue(props, "paddingLeft") ?? estPad ?? 0, estRightPad = getShorthandValue(props, "paddingRight") ?? estPad ?? 0;
|
|
78
|
+
estimatedInitialWidth = estWidth - (estLeftPad ? getVariableValue(estLeftPad) : 0) - (estRightPad ? getVariableValue(estRightPad) : 0);
|
|
79
|
+
}
|
|
80
|
+
const [frameWidth, setFrameInnerWidth] = React.useState(estimatedInitialWidth), { switchProps, bubbleInput, switchRef } = useSwitch(
|
|
70
81
|
// @ts-ignore
|
|
71
82
|
props,
|
|
72
83
|
[checked, setChecked],
|
|
@@ -77,12 +88,10 @@ function createSwitch(createProps) {
|
|
|
77
88
|
const disabled = props.disabled, value = React.useMemo(
|
|
78
89
|
() => ({ checked, disabled, frameWidth }),
|
|
79
90
|
[checked, disabled, frameWidth]
|
|
80
|
-
), handleLayout =
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
[setFrameWidth]
|
|
85
|
-
), unstyled = styledContext.unstyled ?? props.unstyled ?? !1;
|
|
91
|
+
), handleLayout = (e) => {
|
|
92
|
+
const next = e.nativeEvent.layout.width;
|
|
93
|
+
next !== frameWidth && setFrameInnerWidth(next);
|
|
94
|
+
}, unstyled = styledContext.unstyled ?? props.unstyled ?? !1;
|
|
86
95
|
return /* @__PURE__ */ jsxs(SwitchContext.Provider, { value, children: [
|
|
87
96
|
/* @__PURE__ */ jsx(
|
|
88
97
|
Frame,
|
|
@@ -102,7 +111,7 @@ function createSwitch(createProps) {
|
|
|
102
111
|
},
|
|
103
112
|
checked,
|
|
104
113
|
disabled,
|
|
105
|
-
children: /* @__PURE__ */ jsx(View, { style: measureContainerStyle, onLayout: handleLayout, children: props.children })
|
|
114
|
+
children: /* @__PURE__ */ jsx(View, { style: measureContainerStyle, onLayout: handleLayout, children: frameWidth ? props.children : null })
|
|
106
115
|
}
|
|
107
116
|
),
|
|
108
117
|
bubbleInput
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/createSwitch.tsx"],
|
|
4
|
-
"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AAA,QACF;AAAA,QAEC;AAAA,SACH;AAAA,IAEJ;AAAA,IACA;AAAA,MACE,cAAc;AAAA,IAChB;AAAA,EACF;AAEA,SAAO,qBAAqB,iBAAiB;AAAA,IAC3C,OAAO;AAAA,EACT,CAAC;AACH;AAEA,MAAM,wBAAwB;AAAA,EAC5B,WAAW;AAAA,EACX,MAAM;AACR;",
|
|
5
5
|
"names": []
|
|
6
6
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { composeEventHandlers, isWeb, shouldRenderNativePlatform, withStaticProperties } from "@tamagui/core";
|
|
1
|
+
import { composeEventHandlers, getShorthandValue, getVariableValue, isWeb, shouldRenderNativePlatform, withStaticProperties } from "@tamagui/core";
|
|
2
2
|
import { useSwitch } from "@tamagui/switch-headless";
|
|
3
3
|
import { useControllableState } from "@tamagui/use-controllable-state";
|
|
4
4
|
import * as React from "react";
|
|
@@ -39,7 +39,8 @@ function createSwitch(createProps) {
|
|
|
39
39
|
unstyled = process.env.TAMAGUI_HEADLESS === "1" ? !0 : unstyledProp ?? unstyledContext ?? !1,
|
|
40
40
|
size = sizeProp ?? sizeContext ?? "$true",
|
|
41
41
|
initialChecked = React.useRef(checked).current,
|
|
42
|
-
|
|
42
|
+
initialWidth = getVariableValue(props.width, "size"),
|
|
43
|
+
[thumbWidth, setThumbWidth] = React.useState(typeof initialWidth == "number" ? initialWidth : 0),
|
|
43
44
|
distance = frameWidth - thumbWidth,
|
|
44
45
|
x = initialChecked ? checked ? 0 : -distance : checked ? distance : 0;
|
|
45
46
|
return /* @__PURE__ */jsx(Thumb, {
|
|
@@ -53,7 +54,10 @@ function createSwitch(createProps) {
|
|
|
53
54
|
}),
|
|
54
55
|
alignSelf: initialChecked ? "flex-end" : "flex-start",
|
|
55
56
|
x,
|
|
56
|
-
onLayout: composeEventHandlers(props.onLayout, e =>
|
|
57
|
+
onLayout: composeEventHandlers(props.onLayout, e => {
|
|
58
|
+
const next = e.nativeEvent.layout.width;
|
|
59
|
+
next !== thumbWidth && setThumbWidth(next);
|
|
60
|
+
}),
|
|
57
61
|
checked,
|
|
58
62
|
disabled,
|
|
59
63
|
...thumbProps
|
|
@@ -74,8 +78,16 @@ function createSwitch(createProps) {
|
|
|
74
78
|
onChange: onCheckedChange,
|
|
75
79
|
transition: !0
|
|
76
80
|
}),
|
|
77
|
-
styledContext = React.useContext(SwitchStyledContext.context)
|
|
78
|
-
|
|
81
|
+
styledContext = React.useContext(SwitchStyledContext.context);
|
|
82
|
+
let estimatedInitialWidth = 0;
|
|
83
|
+
const estWidth = getVariableValue(getShorthandValue(props, "width"), "size");
|
|
84
|
+
if (estWidth) {
|
|
85
|
+
const estPad = getShorthandValue(props, "paddingHorizontal") ?? getShorthandValue(props, "padding") ?? 0,
|
|
86
|
+
estLeftPad = getShorthandValue(props, "paddingLeft") ?? estPad ?? 0,
|
|
87
|
+
estRightPad = getShorthandValue(props, "paddingRight") ?? estPad ?? 0;
|
|
88
|
+
estimatedInitialWidth = estWidth - (estLeftPad ? getVariableValue(estLeftPad) : 0) - (estRightPad ? getVariableValue(estRightPad) : 0);
|
|
89
|
+
}
|
|
90
|
+
const [frameWidth, setFrameInnerWidth] = React.useState(estimatedInitialWidth),
|
|
79
91
|
{
|
|
80
92
|
switchProps,
|
|
81
93
|
bubbleInput,
|
|
@@ -95,9 +107,10 @@ function createSwitch(createProps) {
|
|
|
95
107
|
disabled,
|
|
96
108
|
frameWidth
|
|
97
109
|
}), [checked, disabled, frameWidth]),
|
|
98
|
-
handleLayout =
|
|
99
|
-
|
|
100
|
-
|
|
110
|
+
handleLayout = e => {
|
|
111
|
+
const next = e.nativeEvent.layout.width;
|
|
112
|
+
next !== frameWidth && setFrameInnerWidth(next);
|
|
113
|
+
},
|
|
101
114
|
unstyled = styledContext.unstyled ?? props.unstyled ?? !1;
|
|
102
115
|
return /* @__PURE__ */jsxs(SwitchContext.Provider, {
|
|
103
116
|
value,
|
|
@@ -122,7 +135,7 @@ function createSwitch(createProps) {
|
|
|
122
135
|
children: /* @__PURE__ */jsx(View, {
|
|
123
136
|
style: measureContainerStyle,
|
|
124
137
|
onLayout: handleLayout,
|
|
125
|
-
children: props.children
|
|
138
|
+
children: frameWidth ? props.children : null
|
|
126
139
|
})
|
|
127
140
|
}), bubbleInput]
|
|
128
141
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["composeEventHandlers","isWeb","shouldRenderNativePlatform","withStaticProperties","useSwitch","useControllableState","React","Switch","NativeSwitch","View","SwitchStyledContext","SwitchFrame","DefaultSwitchFrame","SwitchThumb","jsx","jsxs","SwitchContext","createContext","checked","disabled","frameWidth","createSwitch","createProps","disableActiveTheme","Frame","Thumb","process","env","NODE_ENV","staticConfig","context","console","warn","SwitchThumbComponent","styleable","props","forwardedRef","size","sizeProp","unstyled","unstyledProp","nativeID","thumbProps","useContext","styledContext","useStyledContext","unstyledContext","sizeContext","TAMAGUI_HEADLESS","initialChecked","useRef","current","thumbWidth","setThumbWidth","useState","distance","x","ref","theme","alignSelf","onLayout","e","
|
|
1
|
+
{"version":3,"names":["composeEventHandlers","getShorthandValue","getVariableValue","isWeb","shouldRenderNativePlatform","withStaticProperties","useSwitch","useControllableState","React","Switch","NativeSwitch","View","SwitchStyledContext","SwitchFrame","DefaultSwitchFrame","SwitchThumb","jsx","jsxs","SwitchContext","createContext","checked","disabled","frameWidth","createSwitch","createProps","disableActiveTheme","Frame","Thumb","process","env","NODE_ENV","staticConfig","context","console","warn","SwitchThumbComponent","styleable","props","forwardedRef","size","sizeProp","unstyled","unstyledProp","nativeID","thumbProps","useContext","styledContext","useStyledContext","unstyledContext","sizeContext","TAMAGUI_HEADLESS","initialChecked","useRef","current","initialWidth","width","thumbWidth","setThumbWidth","useState","distance","x","ref","theme","alignSelf","onLayout","e","next","nativeEvent","layout","SwitchComponent","_props","native","nativeProps","checkedProp","defaultChecked","onCheckedChange","setChecked","prop","defaultProp","onChange","transition","estimatedInitialWidth","estWidth","estPad","estLeftPad","estRightPad","setFrameInnerWidth","switchProps","bubbleInput","switchRef","renderNative","value","onValueChange","useMemo","handleLayout","Provider","children","tag","type","themeShallow","style","measureContainerStyle","disableTheme","flex"],"sources":["../../src/createSwitch.tsx"],"sourcesContent":[null],"mappings":"AACA,SACEA,oBAAA,EACAC,iBAAA,EACAC,gBAAA,EACAC,KAAA,EACAC,0BAAA,EACAC,oBAAA,QACK;AAMP,SAASC,SAAA,QAAiB;AAC1B,SAASC,oBAAA,QAA4B;AACrC,YAAYC,KAAA,MAAW;AAMvB,SAASC,MAAA,IAAUC,YAAA,EAAcC,IAAA,QAAY;AAC7C,SAASC,mBAAA,QAA2B;AACpC,SAASC,WAAA,IAAeC,kBAAA,EAAoBC,WAAA,QAAmB;AAwFvD,SAAAC,GAAA,EA8GAC,IAAA,QA9GA;AApED,MAAMC,aAAA,GAAgBV,KAAA,CAAMW,aAAA,CAIhC;EACDC,OAAA,EAAS;EACTC,QAAA,EAAU;EACVC,UAAA,EAAY;AACd,CAAC;AAKM,SAASC,aAGdC,WAAA,EAAqE;EACrE,MAAM;IACJC,kBAAA;IACAC,KAAA,GAAQZ,kBAAA;IACRa,KAAA,GAAQZ;EACV,IAAIS,WAAA;EAMAI,OAAA,CAAQC,GAAA,CAAIC,QAAA,KAAa,kBAExBJ,KAAA,KAAUZ,kBAAA,IACTY,KAAA,CAAMK,YAAA,CAAaC,OAAA,IACnBN,KAAA,CAAMK,YAAA,CAAaC,OAAA,KAAYpB,mBAAA,IAChCe,KAAA,KAAUZ,WAAA,IACTY,KAAA,CAAMI,YAAA,CAAaC,OAAA,IACnBL,KAAA,CAAMI,YAAA,CAAaC,OAAA,KAAYpB,mBAAA,KAEjCqB,OAAA,CAAQC,IAAA,CACN,2IACF,GAIJR,KAAA,CAAMK,YAAA,CAAaC,OAAA,GAAUpB,mBAAA,EAC7Be,KAAA,CAAMI,YAAA,CAAaC,OAAA,GAAUpB,mBAAA;EAE7B,MAAMuB,oBAAA,GAAuBR,KAAA,CAAMS,SAAA,C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E,QAAA;UACC,GAAGJ,KAAA;UACH,GAAIkD,WAAA;UACJ,IAAI,CAAC9D,kBAAA,IACJ,CAACY,KAAA,CAAMI,QAAA,IAAY;YACjBqB,KAAA,EAAO1C,OAAA,GAAU,WAAW;YAC5B+E,YAAA,EAAc;UAChB;UAEF/E,OAAA;UACAC,QAAA;UAEA2E,QAAA,iBAAAhF,GAAA,CAACL,IAAA;YAAKyF,KAAA,EAAOC,qBAAA;YAAuBrC,QAAA,EAAU8B,YAAA;YAC3CE,QAAA,EAAC1E,UAAA,GAAoBe,KAAA,CAAM2D,QAAA,GAAb;UAAA,CACjB;QAAA,CACF,GAECR,WAAA;MAAA,CACH;IAEJ,GACA;MACEc,YAAA,EAAc;IAChB,CACF;EAEA,OAAOjG,oBAAA,CAAqBgE,eAAA,EAAiB;IAC3C1C,KAAA,EAAOQ;EACT,CAAC;AACH;AAEA,MAAMkE,qBAAA,GAAwB;EAC5BtC,SAAA,EAAW;EACXwC,IAAA,EAAM;AACR","ignoreList":[]}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
-
import { composeEventHandlers, isWeb, shouldRenderNativePlatform, withStaticProperties } from "@tamagui/core";
|
|
2
|
+
import { composeEventHandlers, getShorthandValue, getVariableValue, isWeb, shouldRenderNativePlatform, withStaticProperties } from "@tamagui/core";
|
|
3
3
|
import { registerFocusable } from "@tamagui/focusable";
|
|
4
4
|
import { useSwitch } from "@tamagui/switch-headless";
|
|
5
5
|
import { useControllableState } from "@tamagui/use-controllable-state";
|
|
@@ -16,7 +16,7 @@ function createSwitch(createProps) {
|
|
|
16
16
|
var { disableActiveTheme, Frame = DefaultSwitchFrame, Thumb = SwitchThumb } = createProps;
|
|
17
17
|
process.env.NODE_ENV === "development" && (Frame !== DefaultSwitchFrame && Frame.staticConfig.context && Frame.staticConfig.context !== SwitchStyledContext || Thumb !== SwitchThumb && Thumb.staticConfig.context && Thumb.staticConfig.context !== SwitchStyledContext) && console.warn("Warning: createSwitch() needs to control context to pass checked state from Frame to Thumb, any custom context passed will be overridden."), Frame.staticConfig.context = SwitchStyledContext, Thumb.staticConfig.context = SwitchStyledContext;
|
|
18
18
|
var SwitchThumbComponent = Thumb.styleable(function(props, forwardedRef) {
|
|
19
|
-
var { size: sizeProp, unstyled: unstyledProp, nativeID, ...thumbProps } = props, context = React.useContext(SwitchContext), { checked, disabled, frameWidth } = context, styledContext = SwitchStyledContext.useStyledContext(), { unstyled: unstyledContext, size: sizeContext } = styledContext, _ref, unstyled = process.env.TAMAGUI_HEADLESS === "1" ? !0 : (_ref = unstyledProp ?? unstyledContext) !== null && _ref !== void 0 ? _ref : !1, _ref1, size = (_ref1 = sizeProp ?? sizeContext) !== null && _ref1 !== void 0 ? _ref1 : "$true", initialChecked = React.useRef(checked).current, [thumbWidth, setThumbWidth] = React.useState(0), distance = frameWidth - thumbWidth, x = initialChecked ? checked ? 0 : -distance : checked ? distance : 0;
|
|
19
|
+
var { size: sizeProp, unstyled: unstyledProp, nativeID, ...thumbProps } = props, context = React.useContext(SwitchContext), { checked, disabled, frameWidth } = context, styledContext = SwitchStyledContext.useStyledContext(), { unstyled: unstyledContext, size: sizeContext } = styledContext, _ref, unstyled = process.env.TAMAGUI_HEADLESS === "1" ? !0 : (_ref = unstyledProp ?? unstyledContext) !== null && _ref !== void 0 ? _ref : !1, _ref1, size = (_ref1 = sizeProp ?? sizeContext) !== null && _ref1 !== void 0 ? _ref1 : "$true", initialChecked = React.useRef(checked).current, initialWidth = getVariableValue(props.width, "size"), [thumbWidth, setThumbWidth] = React.useState(typeof initialWidth == "number" ? initialWidth : 0), distance = frameWidth - thumbWidth, x = initialChecked ? checked ? 0 : -distance : checked ? distance : 0;
|
|
20
20
|
return /* @__PURE__ */ _jsx(Thumb, {
|
|
21
21
|
ref: forwardedRef,
|
|
22
22
|
unstyled,
|
|
@@ -29,7 +29,8 @@ function createSwitch(createProps) {
|
|
|
29
29
|
alignSelf: initialChecked ? "flex-end" : "flex-start",
|
|
30
30
|
x,
|
|
31
31
|
onLayout: composeEventHandlers(props.onLayout, function(e) {
|
|
32
|
-
|
|
32
|
+
var next = e.nativeEvent.layout.width;
|
|
33
|
+
next !== thumbWidth && setThumbWidth(next);
|
|
33
34
|
}),
|
|
34
35
|
// expected variants
|
|
35
36
|
checked,
|
|
@@ -42,7 +43,12 @@ function createSwitch(createProps) {
|
|
|
42
43
|
defaultProp: defaultChecked || !1,
|
|
43
44
|
onChange: onCheckedChange,
|
|
44
45
|
transition: !0
|
|
45
|
-
}), styledContext = React.useContext(SwitchStyledContext.context),
|
|
46
|
+
}), styledContext = React.useContext(SwitchStyledContext.context), estimatedInitialWidth = 0, estWidth = getVariableValue(getShorthandValue(props, "width"), "size");
|
|
47
|
+
if (estWidth) {
|
|
48
|
+
var _getShorthandValue, _ref, estPad = (_ref = (_getShorthandValue = getShorthandValue(props, "paddingHorizontal")) !== null && _getShorthandValue !== void 0 ? _getShorthandValue : getShorthandValue(props, "padding")) !== null && _ref !== void 0 ? _ref : 0, _getShorthandValue1, _ref1, estLeftPad = (_ref1 = (_getShorthandValue1 = getShorthandValue(props, "paddingLeft")) !== null && _getShorthandValue1 !== void 0 ? _getShorthandValue1 : estPad) !== null && _ref1 !== void 0 ? _ref1 : 0, _getShorthandValue2, _ref2, estRightPad = (_ref2 = (_getShorthandValue2 = getShorthandValue(props, "paddingRight")) !== null && _getShorthandValue2 !== void 0 ? _getShorthandValue2 : estPad) !== null && _ref2 !== void 0 ? _ref2 : 0;
|
|
49
|
+
estimatedInitialWidth = estWidth - (estLeftPad ? getVariableValue(estLeftPad) : 0) - (estRightPad ? getVariableValue(estRightPad) : 0);
|
|
50
|
+
}
|
|
51
|
+
var [frameWidth, setFrameInnerWidth] = React.useState(estimatedInitialWidth), { switchProps, bubbleInput, switchRef } = useSwitch(
|
|
46
52
|
// @ts-ignore
|
|
47
53
|
props,
|
|
48
54
|
[
|
|
@@ -83,11 +89,10 @@ function createSwitch(createProps) {
|
|
|
83
89
|
checked,
|
|
84
90
|
disabled,
|
|
85
91
|
frameWidth
|
|
86
|
-
]), handleLayout =
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
]), _styledContext_unstyled, _ref, unstyled = (_ref = (_styledContext_unstyled = styledContext.unstyled) !== null && _styledContext_unstyled !== void 0 ? _styledContext_unstyled : props.unstyled) !== null && _ref !== void 0 ? _ref : !1, _styledContext_size, _ref1;
|
|
92
|
+
]), handleLayout = function(e) {
|
|
93
|
+
var next = e.nativeEvent.layout.width;
|
|
94
|
+
next !== frameWidth && setFrameInnerWidth(next);
|
|
95
|
+
}, _styledContext_unstyled, _ref3, unstyled = (_ref3 = (_styledContext_unstyled = styledContext.unstyled) !== null && _styledContext_unstyled !== void 0 ? _styledContext_unstyled : props.unstyled) !== null && _ref3 !== void 0 ? _ref3 : !1, _styledContext_size, _ref4;
|
|
91
96
|
return /* @__PURE__ */ _jsxs(SwitchContext.Provider, {
|
|
92
97
|
value,
|
|
93
98
|
children: [
|
|
@@ -98,7 +103,7 @@ function createSwitch(createProps) {
|
|
|
98
103
|
type: "button"
|
|
99
104
|
},
|
|
100
105
|
...!unstyled && {
|
|
101
|
-
size: (
|
|
106
|
+
size: (_ref4 = (_styledContext_size = styledContext.size) !== null && _styledContext_size !== void 0 ? _styledContext_size : props.size) !== null && _ref4 !== void 0 ? _ref4 : "$true"
|
|
102
107
|
},
|
|
103
108
|
unstyled,
|
|
104
109
|
...props,
|
|
@@ -113,7 +118,7 @@ function createSwitch(createProps) {
|
|
|
113
118
|
children: /* @__PURE__ */ _jsx(View, {
|
|
114
119
|
style: measureContainerStyle,
|
|
115
120
|
onLayout: handleLayout,
|
|
116
|
-
children: props.children
|
|
121
|
+
children: frameWidth ? props.children : null
|
|
117
122
|
})
|
|
118
123
|
}),
|
|
119
124
|
bubbleInput
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/Users/n8/tamagui/code/ui/switch/src/createSwitch.tsx"],
|
|
4
|
-
"mappings": ";AACA,SACEA,sBACAC,OACAC,4BACAC,4BACK;AACP,SAASC,yBAAyB;AAKlC,SAASC,iBAAiB;AAC1B,SAASC,4BAA4B;AACrC,YAAYC,WAAW;AAMvB,SAASC,UAAUC,cAAcC,YAAY;
|
|
5
|
-
"names": ["composeEventHandlers", "isWeb", "shouldRenderNativePlatform", "withStaticProperties", "registerFocusable", "useSwitch", "useControllableState", "React", "Switch", "NativeSwitch", "View", "SwitchStyledContext", "SwitchFrame", "DefaultSwitchFrame", "SwitchThumb", "SwitchContext", "createContext", "checked", "disabled", "frameWidth", "createSwitch", "createProps", "disableActiveTheme", "Frame", "Thumb", "process", "env", "NODE_ENV", "staticConfig", "context", "console", "warn", "SwitchThumbComponent", "styleable", "props", "forwardedRef", "size", "sizeProp", "unstyled", "unstyledProp", "nativeID", "thumbProps", "useContext", "styledContext", "useStyledContext", "unstyledContext", "sizeContext", "TAMAGUI_HEADLESS", "initialChecked", "useRef", "current", "thumbWidth", "setThumbWidth", "useState", "distance", "x", "ref", "theme", "alignSelf", "onLayout", "e", "
|
|
4
|
+
"mappings": ";AACA,SACEA,sBACAC,mBACAC,kBACAC,OACAC,4BACAC,4BACK;AACP,SAASC,yBAAyB;AAKlC,SAASC,iBAAiB;AAC1B,SAASC,4BAA4B;AACrC,YAAYC,WAAW;AAMvB,SAASC,UAAUC,cAAcC,YAAY;AAC7C,SAASC,2BAA2B;AACpC,SAASC,eAAeC,oBAAoBC,mBAAmB;AAoBxD,IAAMC,gBAAgBR,sBAAMS,cAIhC;EACDC,SAAS;EACTC,UAAU;EACVC,YAAY;AACd,CAAA;AAKO,SAASC,aAGdC,aAAmE;AACnE,MAAM,EACJC,oBACAC,QAAQV,oBACRW,QAAQV,YAAW,IACjBO;AAMJ,EAAII,QAAQC,IAAIC,aAAa,kBAExBJ,UAAUV,sBACTU,MAAMK,aAAaC,WACnBN,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OAAO1C,UAAU,WAAW;YAC5BkF,cAAc;UAChB;;UAEFlF;UACAC;oBAEA,qBAACR,MAAAA;YAAK0F,OAAOC;YAAuBxC,UAAUkC;sBAC1C5E,aAAoBe,MAAMoE,WAAb;;;QAIlBjB;;;EAGP,GACA;IACEkB,cAAc;EAChB,CAAA;AAGF,SAAOpG,qBAAqB+D,iBAAiB;IAC3C1C,OAAOQ;EACT,CAAA;AACF;AAEA,IAAMqE,wBAAwB;EAC5BzC,WAAW;EACX4C,MAAM;AACR;",
|
|
5
|
+
"names": ["composeEventHandlers", "getShorthandValue", "getVariableValue", "isWeb", "shouldRenderNativePlatform", "withStaticProperties", "registerFocusable", "useSwitch", "useControllableState", "React", "Switch", "NativeSwitch", "View", "SwitchStyledContext", "SwitchFrame", "DefaultSwitchFrame", "SwitchThumb", "SwitchContext", "createContext", "checked", "disabled", "frameWidth", "createSwitch", "createProps", "disableActiveTheme", "Frame", "Thumb", "process", "env", "NODE_ENV", "staticConfig", "context", "console", "warn", "SwitchThumbComponent", "styleable", "props", "forwardedRef", "size", "sizeProp", "unstyled", "unstyledProp", "nativeID", "thumbProps", "useContext", "styledContext", "useStyledContext", "unstyledContext", "sizeContext", "TAMAGUI_HEADLESS", "initialChecked", "useRef", "current", "initialWidth", "width", "thumbWidth", "setThumbWidth", "useState", "distance", "x", "ref", "theme", "alignSelf", "onLayout", "e", "next", "nativeEvent", "layout", "SwitchComponent", "_props", "native", "nativeProps", "checkedProp", "defaultChecked", "onCheckedChange", "setChecked", "prop", "defaultProp", "onChange", "transition", "estimatedInitialWidth", "estWidth", "estPad", "estLeftPad", "estRightPad", "setFrameInnerWidth", "switchProps", "bubbleInput", "switchRef", "useEffect", "id", "focusAndSelect", "value", "focus", "renderNative", "onValueChange", "useMemo", "handleLayout", "Provider", "tag", "type", "themeShallow", "style", "measureContainerStyle", "children", "disableTheme", "flex"]
|
|
6
6
|
}
|
package/dist/jsx/createSwitch.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import {
|
|
2
2
|
composeEventHandlers,
|
|
3
|
+
getShorthandValue,
|
|
4
|
+
getVariableValue,
|
|
3
5
|
isWeb,
|
|
4
6
|
shouldRenderNativePlatform,
|
|
5
7
|
withStaticProperties
|
|
@@ -28,7 +30,9 @@ function createSwitch(createProps) {
|
|
|
28
30
|
), Frame.staticConfig.context = SwitchStyledContext, Thumb.staticConfig.context = SwitchStyledContext;
|
|
29
31
|
const SwitchThumbComponent = Thumb.styleable(
|
|
30
32
|
function(props, forwardedRef) {
|
|
31
|
-
const { size: sizeProp, unstyled: unstyledProp, nativeID, ...thumbProps } = props, context = React.useContext(SwitchContext), { checked, disabled, frameWidth } = context, styledContext = SwitchStyledContext.useStyledContext(), { unstyled: unstyledContext, size: sizeContext } = styledContext, unstyled = process.env.TAMAGUI_HEADLESS === "1" ? !0 : unstyledProp ?? unstyledContext ?? !1, size = sizeProp ?? sizeContext ?? "$true", initialChecked = React.useRef(checked).current, [thumbWidth, setThumbWidth] = React.useState(
|
|
33
|
+
const { size: sizeProp, unstyled: unstyledProp, nativeID, ...thumbProps } = props, context = React.useContext(SwitchContext), { checked, disabled, frameWidth } = context, styledContext = SwitchStyledContext.useStyledContext(), { unstyled: unstyledContext, size: sizeContext } = styledContext, unstyled = process.env.TAMAGUI_HEADLESS === "1" ? !0 : unstyledProp ?? unstyledContext ?? !1, size = sizeProp ?? sizeContext ?? "$true", initialChecked = React.useRef(checked).current, initialWidth = getVariableValue(props.width, "size"), [thumbWidth, setThumbWidth] = React.useState(
|
|
34
|
+
typeof initialWidth == "number" ? initialWidth : 0
|
|
35
|
+
), distance = frameWidth - thumbWidth, x = initialChecked ? checked ? 0 : -distance : checked ? distance : 0;
|
|
32
36
|
return /* @__PURE__ */ jsx(
|
|
33
37
|
Thumb,
|
|
34
38
|
{
|
|
@@ -42,10 +46,10 @@ function createSwitch(createProps) {
|
|
|
42
46
|
},
|
|
43
47
|
alignSelf: initialChecked ? "flex-end" : "flex-start",
|
|
44
48
|
x,
|
|
45
|
-
onLayout: composeEventHandlers(
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
),
|
|
49
|
+
onLayout: composeEventHandlers(props.onLayout, (e) => {
|
|
50
|
+
const next = e.nativeEvent.layout.width;
|
|
51
|
+
next !== thumbWidth && setThumbWidth(next);
|
|
52
|
+
}),
|
|
49
53
|
checked,
|
|
50
54
|
disabled,
|
|
51
55
|
...thumbProps
|
|
@@ -66,7 +70,14 @@ function createSwitch(createProps) {
|
|
|
66
70
|
defaultProp: defaultChecked || !1,
|
|
67
71
|
onChange: onCheckedChange,
|
|
68
72
|
transition: !0
|
|
69
|
-
}), styledContext = React.useContext(SwitchStyledContext.context)
|
|
73
|
+
}), styledContext = React.useContext(SwitchStyledContext.context);
|
|
74
|
+
let estimatedInitialWidth = 0;
|
|
75
|
+
const estWidth = getVariableValue(getShorthandValue(props, "width"), "size");
|
|
76
|
+
if (estWidth) {
|
|
77
|
+
const estPad = getShorthandValue(props, "paddingHorizontal") ?? getShorthandValue(props, "padding") ?? 0, estLeftPad = getShorthandValue(props, "paddingLeft") ?? estPad ?? 0, estRightPad = getShorthandValue(props, "paddingRight") ?? estPad ?? 0;
|
|
78
|
+
estimatedInitialWidth = estWidth - (estLeftPad ? getVariableValue(estLeftPad) : 0) - (estRightPad ? getVariableValue(estRightPad) : 0);
|
|
79
|
+
}
|
|
80
|
+
const [frameWidth, setFrameInnerWidth] = React.useState(estimatedInitialWidth), { switchProps, bubbleInput, switchRef } = useSwitch(
|
|
70
81
|
// @ts-ignore
|
|
71
82
|
props,
|
|
72
83
|
[checked, setChecked],
|
|
@@ -77,12 +88,10 @@ function createSwitch(createProps) {
|
|
|
77
88
|
const disabled = props.disabled, value = React.useMemo(
|
|
78
89
|
() => ({ checked, disabled, frameWidth }),
|
|
79
90
|
[checked, disabled, frameWidth]
|
|
80
|
-
), handleLayout =
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
[setFrameWidth]
|
|
85
|
-
), unstyled = styledContext.unstyled ?? props.unstyled ?? !1;
|
|
91
|
+
), handleLayout = (e) => {
|
|
92
|
+
const next = e.nativeEvent.layout.width;
|
|
93
|
+
next !== frameWidth && setFrameInnerWidth(next);
|
|
94
|
+
}, unstyled = styledContext.unstyled ?? props.unstyled ?? !1;
|
|
86
95
|
return /* @__PURE__ */ jsxs(SwitchContext.Provider, { value, children: [
|
|
87
96
|
/* @__PURE__ */ jsx(
|
|
88
97
|
Frame,
|
|
@@ -102,7 +111,7 @@ function createSwitch(createProps) {
|
|
|
102
111
|
},
|
|
103
112
|
checked,
|
|
104
113
|
disabled,
|
|
105
|
-
children: /* @__PURE__ */ jsx(View, { style: measureContainerStyle, onLayout: handleLayout, children: props.children })
|
|
114
|
+
children: /* @__PURE__ */ jsx(View, { style: measureContainerStyle, onLayout: handleLayout, children: frameWidth ? props.children : null })
|
|
106
115
|
}
|
|
107
116
|
),
|
|
108
117
|
bubbleInput
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/createSwitch.tsx"],
|
|
4
|
-
"mappings": "AACA;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,OAAkC;AAKlC,SAAS,iBAAiB;AAC1B,SAAS,4BAA4B;AACrC,YAAY,WAAW;AAMvB,SAAS,UAAU,cAAc,YAAY;
|
|
4
|
+
"mappings": "AACA;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,OAAkC;AAKlC,SAAS,iBAAiB;AAC1B,SAAS,4BAA4B;AACrC,YAAY,WAAW;AAMvB,SAAS,UAAU,cAAc,YAAY;AAC7C,SAAS,2BAA2B;AACpC,SAAS,eAAe,oBAAoB,mBAAmB;AAwFvD,cA8GA,YA9GA;AApED,MAAM,gBAAgB,MAAM,cAIhC;AAAA,EACD,SAAS;AAAA,EACT,UAAU;AAAA,EACV,Y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AAAA,QACvC,CAAC,SAAS,UAAU,UAAU;AAAA,MAChC,GAEM,eAAe,CAAC,MAAyB;AAC7C,cAAM,OAAO,EAAE,YAAY,OAAO;AAClC,QAAI,SAAS,cACX,mBAAmB,IAAI;AAAA,MAE3B,GAEM,WAAW,cAAc,YAAY,MAAM,YAAY;AAE7D,aACE,qBAAC,cAAc,UAAd,EAAuB,OACtB;AAAA;AAAA,UAAC;AAAA;AAAA,YACC,KAAK;AAAA,YACL,KAAI;AAAA,YACH,GAAI,SAAS,EAAE,MAAM,SAAS;AAAA,YAC9B,GAAI,CAAC,YAAY;AAAA,cAChB,MAAM,cAAc,QAAQ,MAAM,QAAQ;AAAA,YAC5C;AAAA,YACA;AAAA,YACC,GAAG;AAAA,YACH,GAAI;AAAA,YACJ,GAAI,CAAC,sBACJ,CAAC,MAAM,YAAY;AAAA,cACjB,OAAO,UAAU,WAAW;AAAA,cAC5B,cAAc;AAAA,YAChB;AAAA,YAEF;AAAA,YACA;AAAA,YAEA,8BAAC,QAAK,OAAO,uBAAuB,UAAU,cAC3C,UAAC,aAAoB,MAAM,WAAb,MACjB;AAAA;AAAA,QACF;AAAA,QAEC;AAAA,SACH;AAAA,IAEJ;AAAA,IACA;AAAA,MACE,cAAc;AAAA,IAChB;AAAA,EACF;AAEA,SAAO,qBAAqB,iBAAiB;AAAA,IAC3C,OAAO;AAAA,EACT,CAAC;AACH;AAEA,MAAM,wBAAwB;AAAA,EAC5B,WAAW;AAAA,EACX,MAAM;AACR;",
|
|
5
5
|
"names": []
|
|
6
6
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { composeEventHandlers, isWeb, shouldRenderNativePlatform, withStaticProperties } from "@tamagui/core";
|
|
1
|
+
import { composeEventHandlers, getShorthandValue, getVariableValue, isWeb, shouldRenderNativePlatform, withStaticProperties } from "@tamagui/core";
|
|
2
2
|
import { useSwitch } from "@tamagui/switch-headless";
|
|
3
3
|
import { useControllableState } from "@tamagui/use-controllable-state";
|
|
4
4
|
import * as React from "react";
|
|
@@ -39,7 +39,8 @@ function createSwitch(createProps) {
|
|
|
39
39
|
unstyled = process.env.TAMAGUI_HEADLESS === "1" ? !0 : unstyledProp ?? unstyledContext ?? !1,
|
|
40
40
|
size = sizeProp ?? sizeContext ?? "$true",
|
|
41
41
|
initialChecked = React.useRef(checked).current,
|
|
42
|
-
|
|
42
|
+
initialWidth = getVariableValue(props.width, "size"),
|
|
43
|
+
[thumbWidth, setThumbWidth] = React.useState(typeof initialWidth == "number" ? initialWidth : 0),
|
|
43
44
|
distance = frameWidth - thumbWidth,
|
|
44
45
|
x = initialChecked ? checked ? 0 : -distance : checked ? distance : 0;
|
|
45
46
|
return /* @__PURE__ */jsx(Thumb, {
|
|
@@ -53,7 +54,10 @@ function createSwitch(createProps) {
|
|
|
53
54
|
}),
|
|
54
55
|
alignSelf: initialChecked ? "flex-end" : "flex-start",
|
|
55
56
|
x,
|
|
56
|
-
onLayout: composeEventHandlers(props.onLayout, e =>
|
|
57
|
+
onLayout: composeEventHandlers(props.onLayout, e => {
|
|
58
|
+
const next = e.nativeEvent.layout.width;
|
|
59
|
+
next !== thumbWidth && setThumbWidth(next);
|
|
60
|
+
}),
|
|
57
61
|
checked,
|
|
58
62
|
disabled,
|
|
59
63
|
...thumbProps
|
|
@@ -74,8 +78,16 @@ function createSwitch(createProps) {
|
|
|
74
78
|
onChange: onCheckedChange,
|
|
75
79
|
transition: !0
|
|
76
80
|
}),
|
|
77
|
-
styledContext = React.useContext(SwitchStyledContext.context)
|
|
78
|
-
|
|
81
|
+
styledContext = React.useContext(SwitchStyledContext.context);
|
|
82
|
+
let estimatedInitialWidth = 0;
|
|
83
|
+
const estWidth = getVariableValue(getShorthandValue(props, "width"), "size");
|
|
84
|
+
if (estWidth) {
|
|
85
|
+
const estPad = getShorthandValue(props, "paddingHorizontal") ?? getShorthandValue(props, "padding") ?? 0,
|
|
86
|
+
estLeftPad = getShorthandValue(props, "paddingLeft") ?? estPad ?? 0,
|
|
87
|
+
estRightPad = getShorthandValue(props, "paddingRight") ?? estPad ?? 0;
|
|
88
|
+
estimatedInitialWidth = estWidth - (estLeftPad ? getVariableValue(estLeftPad) : 0) - (estRightPad ? getVariableValue(estRightPad) : 0);
|
|
89
|
+
}
|
|
90
|
+
const [frameWidth, setFrameInnerWidth] = React.useState(estimatedInitialWidth),
|
|
79
91
|
{
|
|
80
92
|
switchProps,
|
|
81
93
|
bubbleInput,
|
|
@@ -95,9 +107,10 @@ function createSwitch(createProps) {
|
|
|
95
107
|
disabled,
|
|
96
108
|
frameWidth
|
|
97
109
|
}), [checked, disabled, frameWidth]),
|
|
98
|
-
handleLayout =
|
|
99
|
-
|
|
100
|
-
|
|
110
|
+
handleLayout = e => {
|
|
111
|
+
const next = e.nativeEvent.layout.width;
|
|
112
|
+
next !== frameWidth && setFrameInnerWidth(next);
|
|
113
|
+
},
|
|
101
114
|
unstyled = styledContext.unstyled ?? props.unstyled ?? !1;
|
|
102
115
|
return /* @__PURE__ */jsxs(SwitchContext.Provider, {
|
|
103
116
|
value,
|
|
@@ -122,7 +135,7 @@ function createSwitch(createProps) {
|
|
|
122
135
|
children: /* @__PURE__ */jsx(View, {
|
|
123
136
|
style: measureContainerStyle,
|
|
124
137
|
onLayout: handleLayout,
|
|
125
|
-
children: props.children
|
|
138
|
+
children: frameWidth ? props.children : null
|
|
126
139
|
})
|
|
127
140
|
}), bubbleInput]
|
|
128
141
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["composeEventHandlers","isWeb","shouldRenderNativePlatform","withStaticProperties","useSwitch","useControllableState","React","Switch","NativeSwitch","View","SwitchStyledContext","SwitchFrame","DefaultSwitchFrame","SwitchThumb","jsx","jsxs","SwitchContext","createContext","checked","disabled","frameWidth","createSwitch","createProps","disableActiveTheme","Frame","Thumb","process","env","NODE_ENV","staticConfig","context","console","warn","SwitchThumbComponent","styleable","props","forwardedRef","size","sizeProp","unstyled","unstyledProp","nativeID","thumbProps","useContext","styledContext","useStyledContext","unstyledContext","sizeContext","TAMAGUI_HEADLESS","initialChecked","useRef","current","thumbWidth","setThumbWidth","useState","distance","x","ref","theme","alignSelf","onLayout","e","
|
|
1
|
+
{"version":3,"names":["composeEventHandlers","getShorthandValue","getVariableValue","isWeb","shouldRenderNativePlatform","withStaticProperties","useSwitch","useControllableState","React","Switch","NativeSwitch","View","SwitchStyledContext","SwitchFrame","DefaultSwitchFrame","SwitchThumb","jsx","jsxs","SwitchContext","createContext","checked","disabled","frameWidth","createSwitch","createProps","disableActiveTheme","Frame","Thumb","process","env","NODE_ENV","staticConfig","context","console","warn","SwitchThumbComponent","styleable","props","forwardedRef","size","sizeProp","unstyled","unstyledProp","nativeID","thumbProps","useContext","styledContext","useStyledContext","unstyledContext","sizeContext","TAMAGUI_HEADLESS","initialChecked","useRef","current","initialWidth","width","thumbWidth","setThumbWidth","useState","distance","x","ref","theme","alignSelf","onLayout","e","next","nativeEvent","layout","SwitchComponent","_props","native","nativeProps","checkedProp","defaultChecked","onCheckedChange","setChecked","prop","defaultProp","onChange","transition","estimatedInitialWidth","estWidth","estPad","estLeftPad","estRightPad","setFrameInnerWidth","switchProps","bubbleInput","switchRef","renderNative","value","onValueChange","useMemo","handleLayout","Provider","children","tag","type","themeShallow","style","measureContainerStyle","disableTheme","flex"],"sources":["../../src/createSwitch.tsx"],"sourcesContent":[null],"mappings":"AACA,SACEA,oBAAA,EACAC,iBAAA,EACAC,gBAAA,EACAC,KAAA,EACAC,0BAAA,EACAC,oBAAA,QACK;AAMP,SAASC,SAAA,QAAiB;AAC1B,SAASC,oBAAA,QAA4B;AACrC,YAAYC,KAAA,MAAW;AAMvB,SAASC,MAAA,IAAUC,YAAA,EAAcC,IAAA,QAAY;AAC7C,SAASC,mBAAA,QAA2B;AACpC,SAASC,WAAA,IAAeC,kBAAA,EAAoBC,WAAA,QAAmB;AAwFvD,SAAAC,GAAA,EA8GAC,IAAA,QA9GA;AApED,MAAMC,aAAA,GAAgBV,KAAA,CAAMW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AAC;AACH;AAEA,MAAMkE,qBAAA,GAAwB;EAC5BtC,SAAA,EAAW;EACXwC,IAAA,EAAM;AACR","ignoreList":[]}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
-
import { composeEventHandlers, isWeb, shouldRenderNativePlatform, withStaticProperties } from "@tamagui/core";
|
|
2
|
+
import { composeEventHandlers, getShorthandValue, getVariableValue, isWeb, shouldRenderNativePlatform, withStaticProperties } from "@tamagui/core";
|
|
3
3
|
import { registerFocusable } from "@tamagui/focusable";
|
|
4
4
|
import { useSwitch } from "@tamagui/switch-headless";
|
|
5
5
|
import { useControllableState } from "@tamagui/use-controllable-state";
|
|
@@ -16,7 +16,7 @@ function createSwitch(createProps) {
|
|
|
16
16
|
var { disableActiveTheme, Frame = DefaultSwitchFrame, Thumb = SwitchThumb } = createProps;
|
|
17
17
|
process.env.NODE_ENV === "development" && (Frame !== DefaultSwitchFrame && Frame.staticConfig.context && Frame.staticConfig.context !== SwitchStyledContext || Thumb !== SwitchThumb && Thumb.staticConfig.context && Thumb.staticConfig.context !== SwitchStyledContext) && console.warn("Warning: createSwitch() needs to control context to pass checked state from Frame to Thumb, any custom context passed will be overridden."), Frame.staticConfig.context = SwitchStyledContext, Thumb.staticConfig.context = SwitchStyledContext;
|
|
18
18
|
var SwitchThumbComponent = Thumb.styleable(function(props, forwardedRef) {
|
|
19
|
-
var { size: sizeProp, unstyled: unstyledProp, nativeID, ...thumbProps } = props, context = React.useContext(SwitchContext), { checked, disabled, frameWidth } = context, styledContext = SwitchStyledContext.useStyledContext(), { unstyled: unstyledContext, size: sizeContext } = styledContext, _ref, unstyled = process.env.TAMAGUI_HEADLESS === "1" ? !0 : (_ref = unstyledProp ?? unstyledContext) !== null && _ref !== void 0 ? _ref : !1, _ref1, size = (_ref1 = sizeProp ?? sizeContext) !== null && _ref1 !== void 0 ? _ref1 : "$true", initialChecked = React.useRef(checked).current, [thumbWidth, setThumbWidth] = React.useState(0), distance = frameWidth - thumbWidth, x = initialChecked ? checked ? 0 : -distance : checked ? distance : 0;
|
|
19
|
+
var { size: sizeProp, unstyled: unstyledProp, nativeID, ...thumbProps } = props, context = React.useContext(SwitchContext), { checked, disabled, frameWidth } = context, styledContext = SwitchStyledContext.useStyledContext(), { unstyled: unstyledContext, size: sizeContext } = styledContext, _ref, unstyled = process.env.TAMAGUI_HEADLESS === "1" ? !0 : (_ref = unstyledProp ?? unstyledContext) !== null && _ref !== void 0 ? _ref : !1, _ref1, size = (_ref1 = sizeProp ?? sizeContext) !== null && _ref1 !== void 0 ? _ref1 : "$true", initialChecked = React.useRef(checked).current, initialWidth = getVariableValue(props.width, "size"), [thumbWidth, setThumbWidth] = React.useState(typeof initialWidth == "number" ? initialWidth : 0), distance = frameWidth - thumbWidth, x = initialChecked ? checked ? 0 : -distance : checked ? distance : 0;
|
|
20
20
|
return /* @__PURE__ */ _jsx(Thumb, {
|
|
21
21
|
ref: forwardedRef,
|
|
22
22
|
unstyled,
|
|
@@ -29,7 +29,8 @@ function createSwitch(createProps) {
|
|
|
29
29
|
alignSelf: initialChecked ? "flex-end" : "flex-start",
|
|
30
30
|
x,
|
|
31
31
|
onLayout: composeEventHandlers(props.onLayout, function(e) {
|
|
32
|
-
|
|
32
|
+
var next = e.nativeEvent.layout.width;
|
|
33
|
+
next !== thumbWidth && setThumbWidth(next);
|
|
33
34
|
}),
|
|
34
35
|
// expected variants
|
|
35
36
|
checked,
|
|
@@ -42,7 +43,12 @@ function createSwitch(createProps) {
|
|
|
42
43
|
defaultProp: defaultChecked || !1,
|
|
43
44
|
onChange: onCheckedChange,
|
|
44
45
|
transition: !0
|
|
45
|
-
}), styledContext = React.useContext(SwitchStyledContext.context),
|
|
46
|
+
}), styledContext = React.useContext(SwitchStyledContext.context), estimatedInitialWidth = 0, estWidth = getVariableValue(getShorthandValue(props, "width"), "size");
|
|
47
|
+
if (estWidth) {
|
|
48
|
+
var _getShorthandValue, _ref, estPad = (_ref = (_getShorthandValue = getShorthandValue(props, "paddingHorizontal")) !== null && _getShorthandValue !== void 0 ? _getShorthandValue : getShorthandValue(props, "padding")) !== null && _ref !== void 0 ? _ref : 0, _getShorthandValue1, _ref1, estLeftPad = (_ref1 = (_getShorthandValue1 = getShorthandValue(props, "paddingLeft")) !== null && _getShorthandValue1 !== void 0 ? _getShorthandValue1 : estPad) !== null && _ref1 !== void 0 ? _ref1 : 0, _getShorthandValue2, _ref2, estRightPad = (_ref2 = (_getShorthandValue2 = getShorthandValue(props, "paddingRight")) !== null && _getShorthandValue2 !== void 0 ? _getShorthandValue2 : estPad) !== null && _ref2 !== void 0 ? _ref2 : 0;
|
|
49
|
+
estimatedInitialWidth = estWidth - (estLeftPad ? getVariableValue(estLeftPad) : 0) - (estRightPad ? getVariableValue(estRightPad) : 0);
|
|
50
|
+
}
|
|
51
|
+
var [frameWidth, setFrameInnerWidth] = React.useState(estimatedInitialWidth), { switchProps, bubbleInput, switchRef } = useSwitch(
|
|
46
52
|
// @ts-ignore
|
|
47
53
|
props,
|
|
48
54
|
[
|
|
@@ -83,11 +89,10 @@ function createSwitch(createProps) {
|
|
|
83
89
|
checked,
|
|
84
90
|
disabled,
|
|
85
91
|
frameWidth
|
|
86
|
-
]), handleLayout =
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
]), _styledContext_unstyled, _ref, unstyled = (_ref = (_styledContext_unstyled = styledContext.unstyled) !== null && _styledContext_unstyled !== void 0 ? _styledContext_unstyled : props.unstyled) !== null && _ref !== void 0 ? _ref : !1, _styledContext_size, _ref1;
|
|
92
|
+
]), handleLayout = function(e) {
|
|
93
|
+
var next = e.nativeEvent.layout.width;
|
|
94
|
+
next !== frameWidth && setFrameInnerWidth(next);
|
|
95
|
+
}, _styledContext_unstyled, _ref3, unstyled = (_ref3 = (_styledContext_unstyled = styledContext.unstyled) !== null && _styledContext_unstyled !== void 0 ? _styledContext_unstyled : props.unstyled) !== null && _ref3 !== void 0 ? _ref3 : !1, _styledContext_size, _ref4;
|
|
91
96
|
return /* @__PURE__ */ _jsxs(SwitchContext.Provider, {
|
|
92
97
|
value,
|
|
93
98
|
children: [
|
|
@@ -98,7 +103,7 @@ function createSwitch(createProps) {
|
|
|
98
103
|
type: "button"
|
|
99
104
|
},
|
|
100
105
|
...!unstyled && {
|
|
101
|
-
size: (
|
|
106
|
+
size: (_ref4 = (_styledContext_size = styledContext.size) !== null && _styledContext_size !== void 0 ? _styledContext_size : props.size) !== null && _ref4 !== void 0 ? _ref4 : "$true"
|
|
102
107
|
},
|
|
103
108
|
unstyled,
|
|
104
109
|
...props,
|
|
@@ -113,7 +118,7 @@ function createSwitch(createProps) {
|
|
|
113
118
|
children: /* @__PURE__ */ _jsx(View, {
|
|
114
119
|
style: measureContainerStyle,
|
|
115
120
|
onLayout: handleLayout,
|
|
116
|
-
children: props.children
|
|
121
|
+
children: frameWidth ? props.children : null
|
|
117
122
|
})
|
|
118
123
|
}),
|
|
119
124
|
bubbleInput
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/Users/n8/tamagui/code/ui/switch/src/createSwitch.tsx"],
|
|
4
|
-
"mappings": ";AACA,SACEA,sBACAC,OACAC,4BACAC,4BACK;AACP,SAASC,yBAAyB;AAKlC,SAASC,iBAAiB;AAC1B,SAASC,4BAA4B;AACrC,YAAYC,WAAW;AAMvB,SAASC,UAAUC,cAAcC,YAAY;
|
|
5
|
-
"names": ["composeEventHandlers", "isWeb", "shouldRenderNativePlatform", "withStaticProperties", "registerFocusable", "useSwitch", "useControllableState", "React", "Switch", "NativeSwitch", "View", "SwitchStyledContext", "SwitchFrame", "DefaultSwitchFrame", "SwitchThumb", "SwitchContext", "createContext", "checked", "disabled", "frameWidth", "createSwitch", "createProps", "disableActiveTheme", "Frame", "Thumb", "process", "env", "NODE_ENV", "staticConfig", "context", "console", "warn", "SwitchThumbComponent", "styleable", "props", "forwardedRef", "size", "sizeProp", "unstyled", "unstyledProp", "nativeID", "thumbProps", "useContext", "styledContext", "useStyledContext", "unstyledContext", "sizeContext", "TAMAGUI_HEADLESS", "initialChecked", "useRef", "current", "thumbWidth", "setThumbWidth", "useState", "distance", "x", "ref", "theme", "alignSelf", "onLayout", "e", "
|
|
4
|
+
"mappings": ";AACA,SACEA,sBACAC,mBACAC,kBACAC,OACAC,4BACAC,4BACK;AACP,SAASC,yBAAyB;AAKlC,SAASC,iBAAiB;AAC1B,SAASC,4BAA4B;AACrC,YAAYC,WAAW;AAMvB,SAASC,UAAUC,cAAcC,YAAY;AAC7C,SAASC,2BAA2B;AACpC,SAASC,eAAeC,oBAAoBC,mBAAmB;AAoBxD,IAAMC,gBAAgBR,sBAAMS,cAIhC;EACDC,SAAS;EACTC,UAAU;EACVC,YAAY;AACd,CAAA;AAKO,SAASC,aAGdC,aAAmE;AACnE,MAAM,EACJC,oBACAC,QAAQV,oBACRW,QAAQV,YAAW,IACjBO;AAMJ,EAAII,QAAQC,IAAIC,aAAa,kBAExBJ,UAAUV,sBACTU,MAAMK,aAAaC,WACnBN,MAAMK,aAAaC,YAAYlB,uBAChCa,UAAUV,eACTU,MAAMI,aAAaC,WACnBL,MAAMI,aAAaC,YAAYlB,wBAEjCmB,QAAQC,KACN,2IAA2I,GAKjJR,MAAMK,aAAaC,UAAUlB,qBAC7Ba,MAAMI,aAAaC,UAAUlB;AAE7B,MAAMqB,uBAAuBR,MAAMS,UACjC,SAAqBC,OAAOC,cAAY;AACtC,QAAM,EAAEC,MAAMC,UAAUC,UAAUC,cAAcC,UAAU,GAAGC,WAAAA,IAAeP,OACtEL,UAAUtB,MAAMmC,WAAW3B,aAAAA,GAC3B,EAAEE,SAASC,UAAUC,WAAU,IAAKU,SACpCc,gBAAgBhC,oBAAoBiC,iBAAgB,GACpD,EAAEN,UAAUO,iBAAiBT,MAAMU,YAAW,IAAKH,eAIlDJ,MAHDD,WACJb,QAAQC,IAAIqB,qBAA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QAASC;QAAUC;MAAW;OACvC;MAACF;MAASC;MAAUC;KAAW,GAG3B4E,eAAe,SAACjC,GAAAA;AACpB,UAAMC,OAAOD,EAAEE,YAAYC,OAAOb;AAClC,MAAIW,SAAS5C,cACXgE,mBAAmBpB,IAAAA;IAEvB,GAEiBpB,yBAAAA,OAAXL,YAAWK,SAAAA,0BAAAA,cAAcL,cAAQ,QAAtBK,4BAAAA,SAAAA,0BAA0BT,MAAMI,cAAQ,QAAxCK,UAAAA,SAAAA,QAA4C,IAS/CA,qBAAAA;AAPd,WACE,sBAAC5B,cAAciF,UAAQ;MAACN;;QACtB,qBAACnE,OAAAA;UACCmC,KAAK4B;UACLW,KAAI;UACH,GAAIhG,SAAS;YAAEiG,MAAM;UAAS;UAC9B,GAAI,CAAC5D,YAAY;YAChBF,OAAMO,SAAAA,sBAAAA,cAAcP,UAAI,QAAlBO,wBAAAA,SAAAA,sBAAsBT,MAAME,UAAI,QAAhCO,UAAAA,SAAAA,QAAoC;UAC5C;UACAL;UACC,GAAGJ;UACH,GAAIkD;UACJ,GAAI,CAAC9D,sBACJ,CAACY,MAAMI,YAAY;YACjBqB,OAAO1C,UAAU,WAAW;YAC5BkF,cAAc;UAChB;;UAEFlF;UACAC;oBAEA,qBAACR,MAAAA;YAAK0F,OAAOC;YAAuBxC,UAAUkC;sBAC1C5E,aAAoBe,MAAMoE,WAAb;;;QAIlBjB;;;EAGP,GACA;IACEkB,cAAc;EAChB,CAAA;AAGF,SAAOpG,qBAAqB+D,iBAAiB;IAC3C1C,OAAOQ;EACT,CAAA;AACF;AAEA,IAAMqE,wBAAwB;EAC5BzC,WAAW;EACX4C,MAAM;AACR;",
|
|
5
|
+
"names": ["composeEventHandlers", "getShorthandValue", "getVariableValue", "isWeb", "shouldRenderNativePlatform", "withStaticProperties", "registerFocusable", "useSwitch", "useControllableState", "React", "Switch", "NativeSwitch", "View", "SwitchStyledContext", "SwitchFrame", "DefaultSwitchFrame", "SwitchThumb", "SwitchContext", "createContext", "checked", "disabled", "frameWidth", "createSwitch", "createProps", "disableActiveTheme", "Frame", "Thumb", "process", "env", "NODE_ENV", "staticConfig", "context", "console", "warn", "SwitchThumbComponent", "styleable", "props", "forwardedRef", "size", "sizeProp", "unstyled", "unstyledProp", "nativeID", "thumbProps", "useContext", "styledContext", "useStyledContext", "unstyledContext", "sizeContext", "TAMAGUI_HEADLESS", "initialChecked", "useRef", "current", "initialWidth", "width", "thumbWidth", "setThumbWidth", "useState", "distance", "x", "ref", "theme", "alignSelf", "onLayout", "e", "next", "nativeEvent", "layout", "SwitchComponent", "_props", "native", "nativeProps", "checkedProp", "defaultChecked", "onCheckedChange", "setChecked", "prop", "defaultProp", "onChange", "transition", "estimatedInitialWidth", "estWidth", "estPad", "estLeftPad", "estRightPad", "setFrameInnerWidth", "switchProps", "bubbleInput", "switchRef", "useEffect", "id", "focusAndSelect", "value", "focus", "renderNative", "onValueChange", "useMemo", "handleLayout", "Provider", "tag", "type", "themeShallow", "style", "measureContainerStyle", "children", "disableTheme", "flex"]
|
|
6
6
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tamagui/switch",
|
|
3
|
-
"version": "1.123.
|
|
3
|
+
"version": "1.123.16",
|
|
4
4
|
"removeSideEffects": true,
|
|
5
5
|
"sideEffects": [
|
|
6
6
|
"*.css"
|
|
@@ -37,23 +37,23 @@
|
|
|
37
37
|
}
|
|
38
38
|
},
|
|
39
39
|
"dependencies": {
|
|
40
|
-
"@tamagui/compose-refs": "1.123.
|
|
41
|
-
"@tamagui/constants": "1.123.
|
|
42
|
-
"@tamagui/core": "1.123.
|
|
43
|
-
"@tamagui/focusable": "1.123.
|
|
44
|
-
"@tamagui/get-token": "1.123.
|
|
45
|
-
"@tamagui/helpers": "1.123.
|
|
46
|
-
"@tamagui/label": "1.123.
|
|
47
|
-
"@tamagui/stacks": "1.123.
|
|
48
|
-
"@tamagui/switch-headless": "1.123.
|
|
49
|
-
"@tamagui/use-controllable-state": "1.123.
|
|
50
|
-
"@tamagui/use-previous": "1.123.
|
|
40
|
+
"@tamagui/compose-refs": "1.123.16",
|
|
41
|
+
"@tamagui/constants": "1.123.16",
|
|
42
|
+
"@tamagui/core": "1.123.16",
|
|
43
|
+
"@tamagui/focusable": "1.123.16",
|
|
44
|
+
"@tamagui/get-token": "1.123.16",
|
|
45
|
+
"@tamagui/helpers": "1.123.16",
|
|
46
|
+
"@tamagui/label": "1.123.16",
|
|
47
|
+
"@tamagui/stacks": "1.123.16",
|
|
48
|
+
"@tamagui/switch-headless": "1.123.16",
|
|
49
|
+
"@tamagui/use-controllable-state": "1.123.16",
|
|
50
|
+
"@tamagui/use-previous": "1.123.16"
|
|
51
51
|
},
|
|
52
52
|
"peerDependencies": {
|
|
53
53
|
"react": "*"
|
|
54
54
|
},
|
|
55
55
|
"devDependencies": {
|
|
56
|
-
"@tamagui/build": "1.123.
|
|
56
|
+
"@tamagui/build": "1.123.16",
|
|
57
57
|
"react": "*"
|
|
58
58
|
},
|
|
59
59
|
"publishConfig": {
|
package/src/createSwitch.tsx
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import type { NativeValue, SizeTokens, StackProps } from '@tamagui/core'
|
|
2
2
|
import {
|
|
3
3
|
composeEventHandlers,
|
|
4
|
+
getShorthandValue,
|
|
5
|
+
getVariableValue,
|
|
4
6
|
isWeb,
|
|
5
7
|
shouldRenderNativePlatform,
|
|
6
8
|
withStaticProperties,
|
|
@@ -19,7 +21,6 @@ import type {
|
|
|
19
21
|
ViewStyle,
|
|
20
22
|
} from 'react-native'
|
|
21
23
|
import { Switch as NativeSwitch, View } from 'react-native'
|
|
22
|
-
|
|
23
24
|
import { SwitchStyledContext } from './StyledContext'
|
|
24
25
|
import { SwitchFrame as DefaultSwitchFrame, SwitchThumb } from './Switch'
|
|
25
26
|
|
|
@@ -101,9 +102,13 @@ export function createSwitch<
|
|
|
101
102
|
|
|
102
103
|
const initialChecked = React.useRef(checked).current
|
|
103
104
|
|
|
104
|
-
const
|
|
105
|
+
const initialWidth = getVariableValue(props.width, 'size')
|
|
106
|
+
const [thumbWidth, setThumbWidth] = React.useState(
|
|
107
|
+
typeof initialWidth === 'number' ? initialWidth : 0
|
|
108
|
+
)
|
|
105
109
|
const distance = frameWidth - thumbWidth
|
|
106
110
|
const x = initialChecked ? (checked ? 0 : -distance) : checked ? distance : 0
|
|
111
|
+
|
|
107
112
|
return (
|
|
108
113
|
<Thumb
|
|
109
114
|
ref={forwardedRef}
|
|
@@ -117,9 +122,12 @@ export function createSwitch<
|
|
|
117
122
|
})}
|
|
118
123
|
alignSelf={initialChecked ? 'flex-end' : 'flex-start'}
|
|
119
124
|
x={x}
|
|
120
|
-
onLayout={composeEventHandlers(props.onLayout, (e) =>
|
|
121
|
-
|
|
122
|
-
|
|
125
|
+
onLayout={composeEventHandlers(props.onLayout, (e) => {
|
|
126
|
+
const next = e.nativeEvent.layout.width
|
|
127
|
+
if (next !== thumbWidth) {
|
|
128
|
+
setThumbWidth(next)
|
|
129
|
+
}
|
|
130
|
+
})}
|
|
123
131
|
// expected variants
|
|
124
132
|
checked={checked}
|
|
125
133
|
disabled={disabled}
|
|
@@ -148,7 +156,25 @@ export function createSwitch<
|
|
|
148
156
|
|
|
149
157
|
const styledContext = React.useContext(SwitchStyledContext.context)
|
|
150
158
|
|
|
151
|
-
|
|
159
|
+
let estimatedInitialWidth = 0
|
|
160
|
+
|
|
161
|
+
const estWidth = getVariableValue(getShorthandValue(props, 'width'), 'size')
|
|
162
|
+
|
|
163
|
+
if (estWidth) {
|
|
164
|
+
const estPad =
|
|
165
|
+
getShorthandValue(props, 'paddingHorizontal') ??
|
|
166
|
+
getShorthandValue(props, 'padding') ??
|
|
167
|
+
0
|
|
168
|
+
const estLeftPad = getShorthandValue(props, 'paddingLeft') ?? estPad ?? 0
|
|
169
|
+
const estRightPad = getShorthandValue(props, 'paddingRight') ?? estPad ?? 0
|
|
170
|
+
estimatedInitialWidth =
|
|
171
|
+
estWidth -
|
|
172
|
+
(estLeftPad ? getVariableValue(estLeftPad) : 0) -
|
|
173
|
+
(estRightPad ? getVariableValue(estRightPad) : 0)
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// this is actually inner width
|
|
177
|
+
const [frameWidth, setFrameInnerWidth] = React.useState(estimatedInitialWidth)
|
|
152
178
|
|
|
153
179
|
const { switchProps, bubbleInput, switchRef } = useSwitch(
|
|
154
180
|
// @ts-ignore
|
|
@@ -184,12 +210,12 @@ export function createSwitch<
|
|
|
184
210
|
[checked, disabled, frameWidth]
|
|
185
211
|
)
|
|
186
212
|
|
|
187
|
-
const handleLayout =
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
213
|
+
const handleLayout = (e: LayoutChangeEvent) => {
|
|
214
|
+
const next = e.nativeEvent.layout.width
|
|
215
|
+
if (next !== frameWidth) {
|
|
216
|
+
setFrameInnerWidth(next)
|
|
217
|
+
}
|
|
218
|
+
}
|
|
193
219
|
|
|
194
220
|
const unstyled = styledContext.unstyled ?? props.unstyled ?? false
|
|
195
221
|
|
|
@@ -215,7 +241,7 @@ export function createSwitch<
|
|
|
215
241
|
disabled={disabled}
|
|
216
242
|
>
|
|
217
243
|
<View style={measureContainerStyle} onLayout={handleLayout}>
|
|
218
|
-
{props.children}
|
|
244
|
+
{!frameWidth ? null : props.children}
|
|
219
245
|
</View>
|
|
220
246
|
</Frame>
|
|
221
247
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createSwitch.d.ts","sourceRoot":"","sources":["../src/createSwitch.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,eAAe,CAAA;
|
|
1
|
+
{"version":3,"file":"createSwitch.d.ts","sourceRoot":"","sources":["../src/createSwitch.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,UAAU,EAAE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