@tamagui/switch 2.7.7 → 3.0.0-beta.643.1

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.
Files changed (97) hide show
  1. package/dist/cjs/StyledContext.cjs +19 -23
  2. package/dist/cjs/StyledContext.native.cjs +33 -0
  3. package/dist/cjs/StyledContext.native.js +19 -22
  4. package/dist/cjs/StyledContext.native.js.map +1 -1
  5. package/dist/cjs/Switch.cjs +140 -86
  6. package/dist/cjs/Switch.native.cjs +163 -0
  7. package/dist/cjs/Switch.native.js +147 -90
  8. package/dist/cjs/Switch.native.js.map +1 -1
  9. package/dist/cjs/index.cjs +14 -25
  10. package/dist/cjs/index.native.cjs +31 -0
  11. package/dist/cjs/index.native.js +15 -25
  12. package/dist/cjs/index.native.js.map +1 -1
  13. package/dist/cjs/types.cjs +10 -11
  14. package/dist/cjs/types.native.cjs +19 -0
  15. package/dist/cjs/types.native.js +10 -10
  16. package/dist/cjs/types.native.js.map +1 -1
  17. package/dist/cjs/useSwitchNative.cjs +16 -19
  18. package/dist/cjs/useSwitchNative.native.cjs +65 -0
  19. package/dist/cjs/useSwitchNative.native.js +44 -54
  20. package/dist/cjs/useSwitchNative.native.js.map +1 -1
  21. package/dist/esm/StyledContext.mjs +6 -5
  22. package/dist/esm/StyledContext.mjs.map +1 -1
  23. package/dist/esm/StyledContext.native.js +6 -5
  24. package/dist/esm/StyledContext.native.js.map +1 -1
  25. package/dist/esm/Switch.mjs +119 -71
  26. package/dist/esm/Switch.mjs.map +1 -1
  27. package/dist/esm/Switch.native.js +126 -76
  28. package/dist/esm/Switch.native.js.map +1 -1
  29. package/dist/esm/index.js +8 -12
  30. package/dist/esm/index.mjs +8 -12
  31. package/dist/esm/index.native.js +8 -12
  32. package/dist/esm/types.mjs +0 -2
  33. package/dist/esm/types.native.js +0 -2
  34. package/dist/esm/useSwitchNative.mjs +2 -1
  35. package/dist/esm/useSwitchNative.mjs.map +1 -1
  36. package/dist/esm/useSwitchNative.native.js +30 -31
  37. package/dist/esm/useSwitchNative.native.js.map +1 -1
  38. package/dist/jsx/StyledContext.mjs +6 -5
  39. package/dist/jsx/StyledContext.mjs.map +1 -1
  40. package/dist/jsx/StyledContext.native.cjs +33 -0
  41. package/dist/jsx/StyledContext.native.js +19 -22
  42. package/dist/jsx/StyledContext.native.js.map +1 -1
  43. package/dist/jsx/Switch.mjs +119 -71
  44. package/dist/jsx/Switch.mjs.map +1 -1
  45. package/dist/jsx/Switch.native.cjs +163 -0
  46. package/dist/jsx/Switch.native.js +147 -90
  47. package/dist/jsx/Switch.native.js.map +1 -1
  48. package/dist/jsx/index.js +8 -12
  49. package/dist/jsx/index.mjs +8 -12
  50. package/dist/jsx/index.native.cjs +31 -0
  51. package/dist/jsx/index.native.js +15 -25
  52. package/dist/jsx/index.native.js.map +1 -1
  53. package/dist/jsx/types.mjs +0 -2
  54. package/dist/jsx/types.native.cjs +19 -0
  55. package/dist/jsx/types.native.js +10 -10
  56. package/dist/jsx/types.native.js.map +1 -1
  57. package/dist/jsx/useSwitchNative.mjs +2 -1
  58. package/dist/jsx/useSwitchNative.mjs.map +1 -1
  59. package/dist/jsx/useSwitchNative.native.cjs +65 -0
  60. package/dist/jsx/useSwitchNative.native.js +44 -54
  61. package/dist/jsx/useSwitchNative.native.js.map +1 -1
  62. package/package.json +7 -14
  63. package/src/StyledContext.tsx +1 -3
  64. package/src/Switch.tsx +190 -89
  65. package/src/index.ts +0 -20
  66. package/src/types.ts +3 -5
  67. package/types/StyledContext.d.ts +2 -3
  68. package/types/StyledContext.d.ts.map +1 -1
  69. package/types/Switch.d.ts +209 -37
  70. package/types/Switch.d.ts.map +1 -1
  71. package/types/index.d.ts +0 -146
  72. package/types/index.d.ts.map +1 -1
  73. package/types/types.d.ts +3 -4
  74. package/types/types.d.ts.map +1 -1
  75. package/dist/cjs/createSwitch.cjs +0 -186
  76. package/dist/cjs/createSwitch.native.js +0 -194
  77. package/dist/cjs/createSwitch.native.js.map +0 -1
  78. package/dist/esm/createSwitch.mjs +0 -150
  79. package/dist/esm/createSwitch.mjs.map +0 -1
  80. package/dist/esm/createSwitch.native.js +0 -155
  81. package/dist/esm/createSwitch.native.js.map +0 -1
  82. package/dist/esm/index.js.map +0 -1
  83. package/dist/esm/index.mjs.map +0 -1
  84. package/dist/esm/index.native.js.map +0 -1
  85. package/dist/esm/types.mjs.map +0 -1
  86. package/dist/esm/types.native.js.map +0 -1
  87. package/dist/jsx/createSwitch.mjs +0 -150
  88. package/dist/jsx/createSwitch.mjs.map +0 -1
  89. package/dist/jsx/createSwitch.native.js +0 -194
  90. package/dist/jsx/createSwitch.native.js.map +0 -1
  91. package/dist/jsx/index.js.map +0 -1
  92. package/dist/jsx/index.mjs.map +0 -1
  93. package/dist/jsx/types.mjs.map +0 -1
  94. package/src/Switch.draggable-tmp +0 -340
  95. package/src/createSwitch.tsx +0 -201
  96. package/types/createSwitch.d.ts +0 -141
  97. package/types/createSwitch.d.ts.map +0 -1
@@ -1,74 +1,122 @@
1
- import { getVariableValue, styled } from "@tamagui/core";
2
- import { getSize } from "@tamagui/get-token";
3
- import { YStack } from "@tamagui/stacks";
4
- const SwitchThumb = styled(YStack, {
5
- name: "SwitchThumb",
6
- variants: {
7
- unstyled: {
8
- false: {
9
- size: "$true",
10
- backgroundColor: "$background",
11
- borderRadius: 1e3
12
- }
13
- },
14
- size: {
15
- "...size": val => {
16
- const size = getSwitchHeight(val);
17
- return {
18
- height: size,
19
- width: size
20
- };
21
- }
22
- }
23
- },
24
- defaultVariants: {
25
- unstyled: process.env.TAMAGUI_HEADLESS === "1"
26
- }
27
- }, {
28
- accept: {
29
- activeStyle: "style"
30
- }
1
+ import { Theme, View, composeEventHandlers, createStyledHOC, getVariableValue, isWeb, styled, withStaticProperties } from "@tamagui/core";
2
+ import { useSwitch } from "@tamagui/switch-headless";
3
+ import { useControllableState } from "@tamagui/use-controllable-state";
4
+ import * as React from "react";
5
+ import { SwitchStyledContext } from "./StyledContext.mjs";
6
+ import { useSwitchNative } from "./useSwitchNative.mjs";
7
+ import { Fragment, jsx, jsxs } from "react/jsx-runtime";
8
+
9
+ const SwitchThumbFrame = styled(View, {
10
+ displayName: "SwitchThumb",
11
+ context: SwitchStyledContext
31
12
  });
32
- const getSwitchHeight = val => Math.round(getVariableValue(getSize(val)) * 0.65);
33
- const getSwitchWidth = val => getSwitchHeight(val) * 2;
34
- const SwitchFrame = styled(YStack, {
35
- name: "Switch",
36
- render: "button",
37
- tabIndex: 0,
38
- variants: {
39
- unstyled: {
40
- false: {
41
- borderRadius: 1e3,
42
- backgroundColor: "$background",
43
- focusVisibleStyle: {
44
- outlineColor: "$outlineColor",
45
- outlineStyle: "solid",
46
- outlineWidth: 2
47
- }
48
- }
49
- },
50
- size: {
51
- "...size": (val, {
52
- props
53
- }) => {
54
- if (props["unstyled"]) return;
55
- const height = getSwitchHeight(val);
56
- const width = getSwitchWidth(val);
57
- return {
58
- height,
59
- minHeight: height,
60
- width
61
- };
62
- }
63
- }
64
- },
65
- defaultVariants: {
66
- unstyled: process.env.TAMAGUI_HEADLESS === "1"
67
- }
68
- }, {
69
- accept: {
70
- activeStyle: "style"
71
- }
13
+ const SwitchFrame = styled(View, {
14
+ displayName: "Switch",
15
+ context: SwitchStyledContext,
16
+ render: "button",
17
+ tabIndex: 0
72
18
  });
73
- export { SwitchFrame, SwitchThumb };
19
+ function createSwitch(createProps) {
20
+ const Frame = createProps.Frame ?? SwitchFrame;
21
+ const Thumb = createProps.Thumb ?? SwitchThumbFrame;
22
+ Frame.staticConfig.context = SwitchStyledContext;
23
+ Thumb.staticConfig.context = SwitchStyledContext;
24
+ const SwitchThumbComponent = createStyledHOC(Thumb, function SwitchThumb2(props, forwardedRef) {
25
+ const { size: sizeProp, activeStyle, theme, ...thumbProps } = props;
26
+ const styledContext = SwitchStyledContext.useStyledContext();
27
+ const { size: sizeContext, active, disabled, frameWidth = 0 } = styledContext;
28
+ const size = sizeProp ?? sizeContext ?? true;
29
+ const initialChecked = React.useRef(active).current;
30
+ const initialWidth = getVariableValue(props.width, "size");
31
+ const [thumbWidth, setThumbWidth] = React.useState(typeof initialWidth === "number" ? initialWidth : 0);
32
+ const distance = frameWidth - thumbWidth;
33
+ const x = initialChecked ? active ? 0 : -distance : active ? distance : 0;
34
+ const thumb = /* @__PURE__ */ jsx(Thumb, {
35
+ ref: forwardedRef,
36
+ size,
37
+ alignSelf: initialChecked ? "flex-end" : "flex-start",
38
+ x,
39
+ onLayout: composeEventHandlers(props.onLayout, (event) => {
40
+ setThumbWidth(event.nativeEvent.layout.width);
41
+ }),
42
+ disabled,
43
+ ...thumbProps,
44
+ ...active && activeStyle
45
+ });
46
+ const themeName = theme ?? createProps.componentThemes?.thumb;
47
+ return themeName ? /* @__PURE__ */ jsx(Theme, {
48
+ name: themeName,
49
+ children: thumb
50
+ }) : thumb;
51
+ });
52
+ const SwitchComponent = createStyledHOC(Frame, function Switch2(_props, forwardedRef) {
53
+ const { native, nativeProps, checked: checkedProp, defaultChecked, onCheckedChange, activeStyle, activeTheme, theme, ...props } = _props;
54
+ const [checked, setChecked] = useControllableState({
55
+ prop: checkedProp,
56
+ defaultProp: defaultChecked || false,
57
+ onChange: onCheckedChange,
58
+ transition: true
59
+ });
60
+ const styledContext = React.useContext(SwitchStyledContext.context);
61
+ const [frameWidth, setFrameInnerWidth] = React.useState(0);
62
+ const { switchProps, bubbleInput, switchRef } = useSwitch(props, [checked, setChecked], forwardedRef);
63
+ const nativeSwitch = useSwitchNative({
64
+ id: props.id,
65
+ disabled: props.disabled,
66
+ native,
67
+ nativeProps,
68
+ checked,
69
+ setChecked
70
+ });
71
+ if (nativeSwitch) {
72
+ return nativeSwitch;
73
+ }
74
+ const disabled = props.disabled;
75
+ const size = styledContext.size ?? props.size ?? true;
76
+ const handleLayout = (event) => {
77
+ const next = event.nativeEvent.layout.width;
78
+ if (next !== frameWidth) {
79
+ setFrameInnerWidth(next);
80
+ }
81
+ };
82
+ const frame = /* @__PURE__ */ jsx(Frame, {
83
+ ref: switchRef,
84
+ render: "button",
85
+ ...isWeb && { type: "button" },
86
+ size,
87
+ ...props,
88
+ ...switchProps,
89
+ disabled,
90
+ ...checked && activeStyle,
91
+ children: /* @__PURE__ */ jsx(View, {
92
+ alignSelf: "stretch",
93
+ flex: 1,
94
+ onLayout: handleLayout,
95
+ children: props.children
96
+ })
97
+ });
98
+ const frameTheme = (checked ? activeTheme : void 0) ?? theme ?? createProps.componentThemes?.frame;
99
+ return /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx(SwitchStyledContext.Provider, {
100
+ size,
101
+ active: checked,
102
+ disabled,
103
+ frameWidth,
104
+ children: frameTheme ? /* @__PURE__ */ jsx(Theme, {
105
+ name: frameTheme,
106
+ children: frame
107
+ }) : frame
108
+ }), bubbleInput] });
109
+ }, { disableTheme: true });
110
+ return withStaticProperties(SwitchComponent, {
111
+ Frame,
112
+ Thumb: SwitchThumbComponent
113
+ });
114
+ }
115
+ const Switch = createSwitch({
116
+ Frame: SwitchFrame,
117
+ Thumb: SwitchThumbFrame
118
+ });
119
+ const SwitchThumb = Switch.Thumb;
120
+
121
+ export { Switch, SwitchFrame, SwitchThumb, SwitchThumbFrame, createSwitch };
74
122
  //# sourceMappingURL=Switch.mjs.map
@@ -1 +1 @@
1
- {"version":3,"names":["getVariableValue","styled","getSize","YStack","SwitchThumb","name","variants","unstyled","false","size","backgroundColor","borderRadius","val","getSwitchHeight","height","width","defaultVariants","process","env","TAMAGUI_HEADLESS","accept","activeStyle","Math","round","getSwitchWidth","SwitchFrame","render","tabIndex","focusVisibleStyle","outlineColor","outlineStyle","outlineWidth","...size","props","minHeight"],"sources":["../../src/Switch.tsx"],"sourcesContent":[null],"mappings":"AACA,SAASA,gBAAA,EAAkBC,MAAA,QAAc;AACzC,SAASC,OAAA,QAAe;AACxB,SAASC,MAAA,QAAc;AAEhB,MAAMC,WAAA,GAAcH,MAAA,CACzBE,MAAA,EACA;EACEE,IAAA,EAAM;EAENC,QA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AA,EAAUU,OAAA,CAAQC,GAAA,CAAIC,gBAAA,KAAqB;EAC7C;AACF,GACA;EACEC,MAAA,EAAQ;IACNC,WAAA,EAAa;EACf;AACF,CACF","ignoreList":[]}
1
+ {"version":3,"file":"Switch.js","names":[],"sources":["jsx/Switch.js"],"sourcesContent":["import {\n composeEventHandlers,\n createStyledHOC,\n getVariableValue,\n isWeb,\n styled,\n Theme,\n View,\n withStaticProperties\n} from \"@tamagui/core\";\nimport { useSwitch } from \"@tamagui/switch-headless\";\nimport { useControllableState } from \"@tamagui/use-controllable-state\";\nimport * as React from \"react\";\nimport { SwitchStyledContext } from \"./StyledContext\";\nimport { useSwitchNative } from \"./useSwitchNative\";\nimport { Fragment, jsx, jsxs } from \"react/jsx-runtime\";\nconst SwitchThumbFrame = styled(View, {\n displayName: \"SwitchThumb\",\n context: SwitchStyledContext\n});\nconst SwitchFrame = styled(View, {\n displayName: \"Switch\",\n context: SwitchStyledContext,\n render: \"button\",\n tabIndex: 0\n});\nfunction createSwitch(createProps) {\n const Frame = createProps.Frame ?? SwitchFrame;\n const Thumb = createProps.Thumb ?? SwitchThumbFrame;\n Frame.staticConfig.context = SwitchStyledContext;\n Thumb.staticConfig.context = SwitchStyledContext;\n const SwitchThumbComponent = createStyledHOC(\n Thumb,\n function SwitchThumb2(props, forwardedRef) {\n const { size: sizeProp, activeStyle, theme, ...thumbProps } = props;\n const styledContext = SwitchStyledContext.useStyledContext();\n const { size: sizeContext, active, disabled, frameWidth = 0 } = styledContext;\n const size = sizeProp ?? sizeContext ?? true;\n const initialChecked = React.useRef(active).current;\n const initialWidth = getVariableValue(props.width, \"size\");\n const [thumbWidth, setThumbWidth] = React.useState(\n typeof initialWidth === \"number\" ? initialWidth : 0\n );\n const distance = frameWidth - thumbWidth;\n const x = initialChecked ? active ? 0 : -distance : active ? distance : 0;\n const thumb = /* @__PURE__ */ jsx(\n Thumb,\n {\n ref: forwardedRef,\n size,\n alignSelf: initialChecked ? \"flex-end\" : \"flex-start\",\n x,\n onLayout: composeEventHandlers(props.onLayout, (event) => {\n setThumbWidth(event.nativeEvent.layout.width);\n }),\n disabled,\n ...thumbProps,\n ...active && activeStyle\n }\n );\n const themeName = theme ?? createProps.componentThemes?.thumb;\n return themeName ? /* @__PURE__ */ jsx(Theme, { name: themeName, children: thumb }) : thumb;\n }\n );\n const SwitchComponent = createStyledHOC(\n Frame,\n function Switch2(_props, forwardedRef) {\n const {\n native,\n nativeProps,\n checked: checkedProp,\n defaultChecked,\n onCheckedChange,\n activeStyle,\n activeTheme,\n theme,\n ...props\n } = _props;\n const [checked, setChecked] = useControllableState({\n prop: checkedProp,\n defaultProp: defaultChecked || false,\n onChange: onCheckedChange,\n transition: true\n });\n const styledContext = React.useContext(SwitchStyledContext.context);\n const [frameWidth, setFrameInnerWidth] = React.useState(0);\n const { switchProps, bubbleInput, switchRef } = useSwitch(\n props,\n [checked, setChecked],\n // @ts-ignore TODO tamagui react 19 type error\n forwardedRef\n );\n const nativeSwitch = useSwitchNative({\n id: props.id,\n disabled: props.disabled,\n native,\n nativeProps,\n checked,\n setChecked\n });\n if (nativeSwitch) {\n return nativeSwitch;\n }\n const disabled = props.disabled;\n const size = styledContext.size ?? props.size ?? true;\n const handleLayout = (event) => {\n const next = event.nativeEvent.layout.width;\n if (next !== frameWidth) {\n setFrameInnerWidth(next);\n }\n };\n const frame = /* @__PURE__ */ jsx(\n Frame,\n {\n ref: switchRef,\n render: \"button\",\n ...isWeb && { type: \"button\" },\n size,\n ...props,\n ...switchProps,\n disabled,\n ...checked && activeStyle,\n children: /* @__PURE__ */ jsx(View, { alignSelf: \"stretch\", flex: 1, onLayout: handleLayout, children: props.children })\n }\n );\n const frameTheme = (checked ? activeTheme : void 0) ?? theme ?? createProps.componentThemes?.frame;\n return /* @__PURE__ */ jsxs(Fragment, { children: [\n /* @__PURE__ */ jsx(\n SwitchStyledContext.Provider,\n {\n size,\n active: checked,\n disabled,\n frameWidth,\n children: frameTheme ? /* @__PURE__ */ jsx(Theme, { name: frameTheme, children: frame }) : frame\n }\n ),\n bubbleInput\n ] });\n },\n {\n disableTheme: true\n }\n );\n return withStaticProperties(SwitchComponent, {\n Frame,\n Thumb: SwitchThumbComponent\n });\n}\nconst Switch = createSwitch({\n Frame: SwitchFrame,\n Thumb: SwitchThumbFrame\n});\nconst SwitchThumb = Switch.Thumb;\nexport {\n Switch,\n SwitchFrame,\n SwitchThumb,\n SwitchThumbFrame,\n createSwitch\n};\n//# sourceMappingURL=Switch.js.map\n"],"mappings":";;;;;;;;;AAgBA,MAAM,mBAAmB,OAAO,MAAM;CACpC,aAAa;CACb,SAAS;AACX,CAAC;AACD,MAAM,cAAc,OAAO,MAAM;CAC/B,aAAa;CACb,SAAS;CACT,QAAQ;CACR,UAAU;AACZ,CAAC;AACD,SAAS,aAAa,aAAa;CACjC,MAAM,QAAQ,YAAY,SAAS;CACnC,MAAM,QAAQ,YAAY,SAAS;CACnC,MAAM,aAAa,UAAU;CAC7B,MAAM,aAAa,UAAU;CAC7B,MAAM,uBAAuB,gBAC3B,OACA,SAAS,aAAa,OAAO,cAAc;EACzC,MAAM,EAAE,MAAM,UAAU,aAAa,OAAO,GAAG,eAAe;EAC9D,MAAM,gBAAgB,oBAAoB,iBAAiB;EAC3D,MAAM,EAAE,MAAM,aAAa,QAAQ,UAAU,aAAa,MAAM;EAChE,MAAM,OAAO,YAAY,eAAe;EACxC,MAAM,iBAAiB,MAAM,OAAO,MAAM,EAAE;EAC5C,MAAM,eAAe,iBAAiB,MAAM,OAAO,MAAM;EACzD,MAAM,CAAC,YAAY,iBAAiB,MAAM,SACxC,OAAO,iBAAiB,WAAW,eAAe,CACpD;EACA,MAAM,WAAW,aAAa;EAC9B,MAAM,IAAI,iBAAiB,SAAS,IAAI,CAAC,WAAW,SAAS,WAAW;EACxE,MAAM,QAAwB,oBAC5B,OACA;GACE,KAAK;GACL;GACA,WAAW,iBAAiB,aAAa;GACzC;GACA,UAAU,qBAAqB,MAAM,WAAW,UAAU;IACxD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AiB;EAC7F,OAAuB,qBAAK,UAAU,EAAE,UAAU,CAChC,oBACd,oBAAoB,UACpB;GACE;GACA,QAAQ;GACR;GACA;GACA,UAAU,aAA6B,oBAAI,OAAO;IAAE,MAAM;IAAY,UAAU;GAAM,CAAC,IAAI;EAC7F,CACF,GACA,WACF,EAAE,CAAC;CACL,GACA,EACE,cAAc,KAChB,CACF;CACA,OAAO,qBAAqB,iBAAiB;EAC3C;EACA,OAAO;CACT,CAAC;AACH;AACA,MAAM,SAAS,aAAa;CAC1B,OAAO;CACP,OAAO;AACT,CAAC;AACD,MAAM,cAAc,OAAO"}
@@ -0,0 +1,163 @@
1
+ "use strict";
2
+
3
+
4
+ var __create = Object.create;
5
+ var __defProp = Object.defineProperty;
6
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
7
+ var __getOwnPropNames = Object.getOwnPropertyNames;
8
+ var __getProtoOf = Object.getPrototypeOf;
9
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
10
+ var __export = (target, all) => {
11
+ for (var name in all) __defProp(target, name, {
12
+ get: all[name],
13
+ enumerable: true
14
+ });
15
+ };
16
+ var __copyProps = (to, from, except, desc) => {
17
+ if (from && typeof from === "object" || typeof from === "function") {
18
+ for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
19
+ get: () => from[key],
20
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
21
+ });
22
+ }
23
+ return to;
24
+ };
25
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
26
+ value: mod,
27
+ enumerable: true
28
+ }) : target, mod));
29
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
30
+ var Switch_exports = {};
31
+ __export(Switch_exports, {
32
+ Switch: () => Switch,
33
+ SwitchFrame: () => SwitchFrame,
34
+ SwitchThumb: () => SwitchThumb,
35
+ SwitchThumbFrame: () => SwitchThumbFrame,
36
+ createSwitch: () => createSwitch
37
+ });
38
+ module.exports = __toCommonJS(Switch_exports);
39
+ var import_jsx_runtime = require("react/jsx-runtime");
40
+ var import_core = require("@tamagui/core");
41
+ var import_switch_headless = require("@tamagui/switch-headless");
42
+ var import_use_controllable_state = require("@tamagui/use-controllable-state");
43
+ var React = __toESM(require("react"), 1);
44
+ var import_StyledContext = require("./StyledContext.native.js");
45
+ var import_useSwitchNative = require("./useSwitchNative.native.js");
46
+ var SwitchThumbFrame = (0, import_core.styled)(import_core.View, {
47
+ displayName: "SwitchThumb",
48
+ context: import_StyledContext.SwitchStyledContext
49
+ });
50
+ var SwitchFrame = (0, import_core.styled)(import_core.View, {
51
+ displayName: "Switch",
52
+ context: import_StyledContext.SwitchStyledContext,
53
+ render: "button",
54
+ tabIndex: 0
55
+ });
56
+ function createSwitch(createProps) {
57
+ var _createProps_Frame;
58
+ var Frame = (_createProps_Frame = createProps.Frame) !== null && _createProps_Frame !== void 0 ? _createProps_Frame : SwitchFrame;
59
+ var _createProps_Thumb;
60
+ var Thumb = (_createProps_Thumb = createProps.Thumb) !== null && _createProps_Thumb !== void 0 ? _createProps_Thumb : SwitchThumbFrame;
61
+ Frame.staticConfig.context = import_StyledContext.SwitchStyledContext;
62
+ Thumb.staticConfig.context = import_StyledContext.SwitchStyledContext;
63
+ var SwitchThumbComponent = (0, import_core.createStyledHOC)(Thumb, function SwitchThumb2(props, forwardedRef) {
64
+ var _createProps_componentThemes;
65
+ var { size: sizeProp, activeStyle, theme, ...thumbProps } = props;
66
+ var styledContext = import_StyledContext.SwitchStyledContext.useStyledContext();
67
+ var { size: sizeContext, active, disabled, frameWidth = 0 } = styledContext;
68
+ var _ref;
69
+ var size = (_ref = sizeProp !== null && sizeProp !== void 0 ? sizeProp : sizeContext) !== null && _ref !== void 0 ? _ref : true;
70
+ var initialChecked = React.useRef(active).current;
71
+ var initialWidth = (0, import_core.getVariableValue)(props.width, "size");
72
+ var [thumbWidth, setThumbWidth] = React.useState(typeof initialWidth === "number" ? initialWidth : 0);
73
+ var distance = frameWidth - thumbWidth;
74
+ var x = initialChecked ? active ? 0 : -distance : active ? distance : 0;
75
+ var thumb = /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Thumb, {
76
+ ref: forwardedRef,
77
+ size,
78
+ alignSelf: initialChecked ? "flex-end" : "flex-start",
79
+ x,
80
+ onLayout: (0, import_core.composeEventHandlers)(props.onLayout, function(event) {
81
+ setThumbWidth(event.nativeEvent.layout.width);
82
+ }),
83
+ disabled,
84
+ ...thumbProps,
85
+ ...active && activeStyle
86
+ });
87
+ var themeName = theme !== null && theme !== void 0 ? theme : (_createProps_componentThemes = createProps.componentThemes) === null || _createProps_componentThemes === void 0 ? void 0 : _createProps_componentThemes.thumb;
88
+ return themeName ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_core.Theme, {
89
+ name: themeName,
90
+ children: thumb
91
+ }) : thumb;
92
+ });
93
+ var SwitchComponent = (0, import_core.createStyledHOC)(Frame, function Switch2(_props, forwardedRef) {
94
+ var _createProps_componentThemes;
95
+ var { native, nativeProps, checked: checkedProp, defaultChecked, onCheckedChange, activeStyle, activeTheme, theme, ...props } = _props;
96
+ var [checked, setChecked] = (0, import_use_controllable_state.useControllableState)({
97
+ prop: checkedProp,
98
+ defaultProp: defaultChecked || false,
99
+ onChange: onCheckedChange,
100
+ transition: true
101
+ });
102
+ var styledContext = React.useContext(import_StyledContext.SwitchStyledContext.context);
103
+ var [frameWidth, setFrameInnerWidth] = React.useState(0);
104
+ var { switchProps, bubbleInput, switchRef } = (0, import_switch_headless.useSwitch)(props, [checked, setChecked], forwardedRef);
105
+ var nativeSwitch = (0, import_useSwitchNative.useSwitchNative)({
106
+ id: props.id,
107
+ disabled: props.disabled,
108
+ native,
109
+ nativeProps,
110
+ checked,
111
+ setChecked
112
+ });
113
+ if (nativeSwitch) {
114
+ return nativeSwitch;
115
+ }
116
+ var disabled = props.disabled;
117
+ var _styledContext_size, _ref;
118
+ var size = (_ref = (_styledContext_size = styledContext.size) !== null && _styledContext_size !== void 0 ? _styledContext_size : props.size) !== null && _ref !== void 0 ? _ref : true;
119
+ var handleLayout = function(event) {
120
+ var next = event.nativeEvent.layout.width;
121
+ if (next !== frameWidth) {
122
+ setFrameInnerWidth(next);
123
+ }
124
+ };
125
+ var frame = /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Frame, {
126
+ ref: switchRef,
127
+ render: "button",
128
+ ...import_core.isWeb && { type: "button" },
129
+ size,
130
+ ...props,
131
+ ...switchProps,
132
+ disabled,
133
+ ...checked && activeStyle,
134
+ children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_core.View, {
135
+ alignSelf: "stretch",
136
+ flex: 1,
137
+ onLayout: handleLayout,
138
+ children: props.children
139
+ })
140
+ });
141
+ var _ref1, _ref2;
142
+ var frameTheme = (_ref2 = (_ref1 = checked ? activeTheme : void 0) !== null && _ref1 !== void 0 ? _ref1 : theme) !== null && _ref2 !== void 0 ? _ref2 : (_createProps_componentThemes = createProps.componentThemes) === null || _createProps_componentThemes === void 0 ? void 0 : _createProps_componentThemes.frame;
143
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_StyledContext.SwitchStyledContext.Provider, {
144
+ size,
145
+ active: checked,
146
+ disabled,
147
+ frameWidth,
148
+ children: frameTheme ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_core.Theme, {
149
+ name: frameTheme,
150
+ children: frame
151
+ }) : frame
152
+ }), bubbleInput] });
153
+ }, { disableTheme: true });
154
+ return (0, import_core.withStaticProperties)(SwitchComponent, {
155
+ Frame,
156
+ Thumb: SwitchThumbComponent
157
+ });
158
+ }
159
+ var Switch = createSwitch({
160
+ Frame: SwitchFrame,
161
+ Thumb: SwitchThumbFrame
162
+ });
163
+ var SwitchThumb = Switch.Thumb;
@@ -1,108 +1,165 @@
1
1
  "use strict";
2
2
 
3
+
4
+ var __create = Object.create;
3
5
  var __defProp = Object.defineProperty;
4
6
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
7
  var __getOwnPropNames = Object.getOwnPropertyNames;
8
+ var __getProtoOf = Object.getPrototypeOf;
6
9
  var __hasOwnProp = Object.prototype.hasOwnProperty;
7
10
  var __export = (target, all) => {
8
- for (var name in all) __defProp(target, name, {
9
- get: all[name],
10
- enumerable: true
11
- });
11
+ for (var name in all) __defProp(target, name, {
12
+ get: all[name],
13
+ enumerable: true
14
+ });
12
15
  };
13
16
  var __copyProps = (to, from, except, desc) => {
14
- if (from && typeof from === "object" || typeof from === "function") {
15
- for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
16
- get: () => from[key],
17
- enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
18
- });
19
- }
20
- return to;
17
+ if (from && typeof from === "object" || typeof from === "function") {
18
+ for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
19
+ get: () => from[key],
20
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
21
+ });
22
+ }
23
+ return to;
21
24
  };
22
- var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
23
- value: true
24
- }), mod);
25
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
26
+ value: mod,
27
+ enumerable: true
28
+ }) : target, mod));
29
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
25
30
  var Switch_exports = {};
26
31
  __export(Switch_exports, {
27
- SwitchFrame: () => SwitchFrame,
28
- SwitchThumb: () => SwitchThumb
32
+ Switch: () => Switch,
33
+ SwitchFrame: () => SwitchFrame,
34
+ SwitchThumb: () => SwitchThumb,
35
+ SwitchThumbFrame: () => SwitchThumbFrame,
36
+ createSwitch: () => createSwitch
29
37
  });
30
38
  module.exports = __toCommonJS(Switch_exports);
39
+ var import_jsx_runtime = require("react/jsx-runtime");
31
40
  var import_core = require("@tamagui/core");
32
- var import_get_token = require("@tamagui/get-token");
33
- var import_stacks = require("@tamagui/stacks");
34
- var SwitchThumb = (0, import_core.styled)(import_stacks.YStack, {
35
- name: "SwitchThumb",
36
- variants: {
37
- unstyled: {
38
- false: {
39
- size: "$true",
40
- backgroundColor: "$background",
41
- borderRadius: 1e3
42
- }
43
- },
44
- size: {
45
- "...size": function (val) {
46
- var size = getSwitchHeight(val);
47
- return {
48
- height: size,
49
- width: size
50
- };
51
- }
52
- }
53
- },
54
- defaultVariants: {
55
- unstyled: process.env.TAMAGUI_HEADLESS === "1"
56
- }
57
- }, {
58
- accept: {
59
- activeStyle: "style"
60
- }
41
+ var import_switch_headless = require("@tamagui/switch-headless");
42
+ var import_use_controllable_state = require("@tamagui/use-controllable-state");
43
+ var React = __toESM(require("react"), 1);
44
+ var import_StyledContext = require("./StyledContext.native.js");
45
+ var import_useSwitchNative = require("./useSwitchNative.native.js");
46
+ var SwitchThumbFrame = (0, import_core.styled)(import_core.View, {
47
+ displayName: "SwitchThumb",
48
+ context: import_StyledContext.SwitchStyledContext
61
49
  });
62
- var getSwitchHeight = function (val) {
63
- return Math.round((0, import_core.getVariableValue)((0, import_get_token.getSize)(val)) * 0.65);
64
- };
65
- var getSwitchWidth = function (val) {
66
- return getSwitchHeight(val) * 2;
67
- };
68
- var SwitchFrame = (0, import_core.styled)(import_stacks.YStack, {
69
- name: "Switch",
70
- render: "button",
71
- tabIndex: 0,
72
- variants: {
73
- unstyled: {
74
- false: {
75
- borderRadius: 1e3,
76
- backgroundColor: "$background",
77
- focusVisibleStyle: {
78
- outlineColor: "$outlineColor",
79
- outlineStyle: "solid",
80
- outlineWidth: 2
81
- }
82
- }
83
- },
84
- size: {
85
- "...size": function (val, param) {
86
- var {
87
- props
88
- } = param;
89
- if (props["unstyled"]) return;
90
- var height = getSwitchHeight(val);
91
- var width = getSwitchWidth(val);
92
- return {
93
- height,
94
- minHeight: height,
95
- width
96
- };
97
- }
98
- }
99
- },
100
- defaultVariants: {
101
- unstyled: process.env.TAMAGUI_HEADLESS === "1"
102
- }
103
- }, {
104
- accept: {
105
- activeStyle: "style"
106
- }
50
+ var SwitchFrame = (0, import_core.styled)(import_core.View, {
51
+ displayName: "Switch",
52
+ context: import_StyledContext.SwitchStyledContext,
53
+ render: "button",
54
+ tabIndex: 0
107
55
  });
56
+ function createSwitch(createProps) {
57
+ var _createProps_Frame;
58
+ var Frame = (_createProps_Frame = createProps.Frame) !== null && _createProps_Frame !== void 0 ? _createProps_Frame : SwitchFrame;
59
+ var _createProps_Thumb;
60
+ var Thumb = (_createProps_Thumb = createProps.Thumb) !== null && _createProps_Thumb !== void 0 ? _createProps_Thumb : SwitchThumbFrame;
61
+ Frame.staticConfig.context = import_StyledContext.SwitchStyledContext;
62
+ Thumb.staticConfig.context = import_StyledContext.SwitchStyledContext;
63
+ var SwitchThumbComponent = (0, import_core.createStyledHOC)(Thumb, function SwitchThumb2(props, forwardedRef) {
64
+ var _createProps_componentThemes;
65
+ var { size: sizeProp, activeStyle, theme, ...thumbProps } = props;
66
+ var styledContext = import_StyledContext.SwitchStyledContext.useStyledContext();
67
+ var { size: sizeContext, active, disabled, frameWidth = 0 } = styledContext;
68
+ var _ref;
69
+ var size = (_ref = sizeProp !== null && sizeProp !== void 0 ? sizeProp : sizeContext) !== null && _ref !== void 0 ? _ref : true;
70
+ var initialChecked = React.useRef(active).current;
71
+ var initialWidth = (0, import_core.getVariableValue)(props.width, "size");
72
+ var [thumbWidth, setThumbWidth] = React.useState(typeof initialWidth === "number" ? initialWidth : 0);
73
+ var distance = frameWidth - thumbWidth;
74
+ var x = initialChecked ? active ? 0 : -distance : active ? distance : 0;
75
+ var thumb = /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Thumb, {
76
+ ref: forwardedRef,
77
+ size,
78
+ alignSelf: initialChecked ? "flex-end" : "flex-start",
79
+ x,
80
+ onLayout: (0, import_core.composeEventHandlers)(props.onLayout, function(event) {
81
+ setThumbWidth(event.nativeEvent.layout.width);
82
+ }),
83
+ disabled,
84
+ ...thumbProps,
85
+ ...active && activeStyle
86
+ });
87
+ var themeName = theme !== null && theme !== void 0 ? theme : (_createProps_componentThemes = createProps.componentThemes) === null || _createProps_componentThemes === void 0 ? void 0 : _createProps_componentThemes.thumb;
88
+ return themeName ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_core.Theme, {
89
+ name: themeName,
90
+ children: thumb
91
+ }) : thumb;
92
+ });
93
+ var SwitchComponent = (0, import_core.createStyledHOC)(Frame, function Switch2(_props, forwardedRef) {
94
+ var _createProps_componentThemes;
95
+ var { native, nativeProps, checked: checkedProp, defaultChecked, onCheckedChange, activeStyle, activeTheme, theme, ...props } = _props;
96
+ var [checked, setChecked] = (0, import_use_controllable_state.useControllableState)({
97
+ prop: checkedProp,
98
+ defaultProp: defaultChecked || false,
99
+ onChange: onCheckedChange,
100
+ transition: true
101
+ });
102
+ var styledContext = React.useContext(import_StyledContext.SwitchStyledContext.context);
103
+ var [frameWidth, setFrameInnerWidth] = React.useState(0);
104
+ var { switchProps, bubbleInput, switchRef } = (0, import_switch_headless.useSwitch)(props, [checked, setChecked], forwardedRef);
105
+ var nativeSwitch = (0, import_useSwitchNative.useSwitchNative)({
106
+ id: props.id,
107
+ disabled: props.disabled,
108
+ native,
109
+ nativeProps,
110
+ checked,
111
+ setChecked
112
+ });
113
+ if (nativeSwitch) {
114
+ return nativeSwitch;
115
+ }
116
+ var disabled = props.disabled;
117
+ var _styledContext_size, _ref;
118
+ var size = (_ref = (_styledContext_size = styledContext.size) !== null && _styledContext_size !== void 0 ? _styledContext_size : props.size) !== null && _ref !== void 0 ? _ref : true;
119
+ var handleLayout = function(event) {
120
+ var next = event.nativeEvent.layout.width;
121
+ if (next !== frameWidth) {
122
+ setFrameInnerWidth(next);
123
+ }
124
+ };
125
+ var frame = /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Frame, {
126
+ ref: switchRef,
127
+ render: "button",
128
+ ...import_core.isWeb && { type: "button" },
129
+ size,
130
+ ...props,
131
+ ...switchProps,
132
+ disabled,
133
+ ...checked && activeStyle,
134
+ children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_core.View, {
135
+ alignSelf: "stretch",
136
+ flex: 1,
137
+ onLayout: handleLayout,
138
+ children: props.children
139
+ })
140
+ });
141
+ var _ref1, _ref2;
142
+ var frameTheme = (_ref2 = (_ref1 = checked ? activeTheme : void 0) !== null && _ref1 !== void 0 ? _ref1 : theme) !== null && _ref2 !== void 0 ? _ref2 : (_createProps_componentThemes = createProps.componentThemes) === null || _createProps_componentThemes === void 0 ? void 0 : _createProps_componentThemes.frame;
143
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_StyledContext.SwitchStyledContext.Provider, {
144
+ size,
145
+ active: checked,
146
+ disabled,
147
+ frameWidth,
148
+ children: frameTheme ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_core.Theme, {
149
+ name: frameTheme,
150
+ children: frame
151
+ }) : frame
152
+ }), bubbleInput] });
153
+ }, { disableTheme: true });
154
+ return (0, import_core.withStaticProperties)(SwitchComponent, {
155
+ Frame,
156
+ Thumb: SwitchThumbComponent
157
+ });
158
+ }
159
+ var Switch = createSwitch({
160
+ Frame: SwitchFrame,
161
+ Thumb: SwitchThumbFrame
162
+ });
163
+ var SwitchThumb = Switch.Thumb;
164
+
108
165
  //# sourceMappingURL=Switch.native.js.map