@tamagui/switch 1.0.1-beta.103
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/Card.js +144 -0
- package/dist/cjs/Card.js.map +7 -0
- package/dist/cjs/Switch.js +262 -0
- package/dist/cjs/Switch.js.map +7 -0
- package/dist/cjs/index.js +18 -0
- package/dist/cjs/index.js.map +7 -0
- package/dist/esm/Card.js +119 -0
- package/dist/esm/Card.js.map +7 -0
- package/dist/esm/Switch.js +241 -0
- package/dist/esm/Switch.js.map +7 -0
- package/dist/esm/index.js +2 -0
- package/dist/esm/index.js.map +7 -0
- package/dist/jsx/Card.js +87 -0
- package/dist/jsx/Card.js.map +7 -0
- package/dist/jsx/Switch.js +159 -0
- package/dist/jsx/Switch.js.map +7 -0
- package/dist/jsx/index.js +2 -0
- package/dist/jsx/index.js.map +7 -0
- package/package.json +48 -0
- package/src/Switch.tsx +293 -0
- package/src/index.ts +1 -0
- package/types/Switch.d.ts +246 -0
- package/types/Switch.d.ts.map +1 -0
- package/types/index.d.ts +2 -0
- package/types/index.d.ts.map +1 -0
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defProps = Object.defineProperties;
|
|
3
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
4
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
7
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
8
|
+
var __spreadValues = (a, b) => {
|
|
9
|
+
for (var prop in b || (b = {}))
|
|
10
|
+
if (__hasOwnProp.call(b, prop))
|
|
11
|
+
__defNormalProp(a, prop, b[prop]);
|
|
12
|
+
if (__getOwnPropSymbols)
|
|
13
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
14
|
+
if (__propIsEnum.call(b, prop))
|
|
15
|
+
__defNormalProp(a, prop, b[prop]);
|
|
16
|
+
}
|
|
17
|
+
return a;
|
|
18
|
+
};
|
|
19
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
20
|
+
var __objRest = (source, exclude) => {
|
|
21
|
+
var target = {};
|
|
22
|
+
for (var prop in source)
|
|
23
|
+
if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
|
|
24
|
+
target[prop] = source[prop];
|
|
25
|
+
if (source != null && __getOwnPropSymbols)
|
|
26
|
+
for (var prop of __getOwnPropSymbols(source)) {
|
|
27
|
+
if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
|
|
28
|
+
target[prop] = source[prop];
|
|
29
|
+
}
|
|
30
|
+
return target;
|
|
31
|
+
};
|
|
32
|
+
import { usePrevious } from "@radix-ui/react-use-previous";
|
|
33
|
+
import { useComposedRefs } from "@tamagui/compose-refs";
|
|
34
|
+
import {
|
|
35
|
+
calc,
|
|
36
|
+
getSize,
|
|
37
|
+
getVariableValue,
|
|
38
|
+
isWeb,
|
|
39
|
+
styled,
|
|
40
|
+
withStaticProperties
|
|
41
|
+
} from "@tamagui/core";
|
|
42
|
+
import { createContextScope } from "@tamagui/create-context";
|
|
43
|
+
import { registerFocusable } from "@tamagui/focusable";
|
|
44
|
+
import { useLabelContext } from "@tamagui/label";
|
|
45
|
+
import { ThemeableStack, XStack } from "@tamagui/stacks";
|
|
46
|
+
import { useControllableState } from "@tamagui/use-controllable-state";
|
|
47
|
+
import * as React from "react";
|
|
48
|
+
const SWITCH_NAME = "Switch";
|
|
49
|
+
const getSwitchHeight = (val) => Math.round(getVariableValue(getSize(val)) * 0.65);
|
|
50
|
+
const getSwitchWidth = (val) => getSwitchHeight(val) * 2;
|
|
51
|
+
const scopeContexts = createContextScope(SWITCH_NAME);
|
|
52
|
+
const [createSwitchContext] = scopeContexts;
|
|
53
|
+
const createSwitchScope = scopeContexts[1];
|
|
54
|
+
const [SwitchProvider, useSwitchContext] = createSwitchContext(SWITCH_NAME);
|
|
55
|
+
const THUMB_NAME = "SwitchThumb";
|
|
56
|
+
const SwitchThumbFrame = styled(ThemeableStack, {
|
|
57
|
+
name: "SwitchThumb",
|
|
58
|
+
backgroundColor: "$background",
|
|
59
|
+
borderRadius: 1e3,
|
|
60
|
+
variants: {
|
|
61
|
+
size: {
|
|
62
|
+
"...size": (val) => ({
|
|
63
|
+
height: getSwitchHeight(val),
|
|
64
|
+
width: getSwitchHeight(val)
|
|
65
|
+
})
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
defaultVariants: {
|
|
69
|
+
size: "$4"
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
const SwitchThumb = SwitchThumbFrame.extractable(React.forwardRef((props, forwardedRef) => {
|
|
73
|
+
const _a = props, { __scopeSwitch } = _a, thumbProps = __objRest(_a, ["__scopeSwitch"]);
|
|
74
|
+
const { size, disabled, checked } = useSwitchContext(THUMB_NAME, __scopeSwitch);
|
|
75
|
+
return /* @__PURE__ */ React.createElement(SwitchThumbFrame, __spreadProps(__spreadValues({
|
|
76
|
+
size,
|
|
77
|
+
"data-state": getState(checked),
|
|
78
|
+
"data-disabled": disabled ? "" : void 0
|
|
79
|
+
}, thumbProps), {
|
|
80
|
+
x: checked ? getVariableValue(getSwitchWidth(size)) - getVariableValue(getSwitchHeight(size)) : 0,
|
|
81
|
+
ref: forwardedRef
|
|
82
|
+
}));
|
|
83
|
+
}));
|
|
84
|
+
SwitchThumb.displayName = THUMB_NAME;
|
|
85
|
+
const SwitchFrame = styled(XStack, {
|
|
86
|
+
name: "Switch",
|
|
87
|
+
tag: "button",
|
|
88
|
+
borderRadius: 1e3,
|
|
89
|
+
borderWidth: 2,
|
|
90
|
+
borderColor: "transparent",
|
|
91
|
+
backgroundColor: "$background",
|
|
92
|
+
focusStyle: {
|
|
93
|
+
borderColor: "$borderColorFocus"
|
|
94
|
+
},
|
|
95
|
+
variants: {
|
|
96
|
+
size: {
|
|
97
|
+
"...size": (val) => {
|
|
98
|
+
const height = calc(getSwitchHeight(val), "+", 4);
|
|
99
|
+
const width = calc(getSwitchWidth(val), "+", 4);
|
|
100
|
+
return {
|
|
101
|
+
height,
|
|
102
|
+
minHeight: height,
|
|
103
|
+
width
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
},
|
|
108
|
+
defaultVariants: {
|
|
109
|
+
size: "$4"
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
const Switch = withStaticProperties(SwitchFrame.extractable(React.forwardRef((props, forwardedRef) => {
|
|
113
|
+
const _a = props, {
|
|
114
|
+
__scopeSwitch,
|
|
115
|
+
labeledBy: ariaLabelledby,
|
|
116
|
+
name,
|
|
117
|
+
checked: checkedProp,
|
|
118
|
+
defaultChecked,
|
|
119
|
+
required,
|
|
120
|
+
disabled,
|
|
121
|
+
value = "on",
|
|
122
|
+
onCheckedChange,
|
|
123
|
+
size = "$4"
|
|
124
|
+
} = _a, switchProps = __objRest(_a, [
|
|
125
|
+
"__scopeSwitch",
|
|
126
|
+
"labeledBy",
|
|
127
|
+
"name",
|
|
128
|
+
"checked",
|
|
129
|
+
"defaultChecked",
|
|
130
|
+
"required",
|
|
131
|
+
"disabled",
|
|
132
|
+
"value",
|
|
133
|
+
"onCheckedChange",
|
|
134
|
+
"size"
|
|
135
|
+
]);
|
|
136
|
+
const [button, setButton] = React.useState(null);
|
|
137
|
+
const composedRefs = useComposedRefs(forwardedRef, (node) => setButton(node));
|
|
138
|
+
const labelId = useLabelContext(button);
|
|
139
|
+
const labelledBy = ariaLabelledby || labelId;
|
|
140
|
+
const hasConsumerStoppedPropagationRef = React.useRef(false);
|
|
141
|
+
const isFormControl = isWeb ? button ? Boolean(button.closest("form")) : true : false;
|
|
142
|
+
const [checked = false, setChecked] = useControllableState({
|
|
143
|
+
prop: checkedProp,
|
|
144
|
+
defaultProp: defaultChecked || false,
|
|
145
|
+
onChange: onCheckedChange
|
|
146
|
+
});
|
|
147
|
+
if (!isWeb) {
|
|
148
|
+
React.useEffect(() => {
|
|
149
|
+
if (!props.id)
|
|
150
|
+
return;
|
|
151
|
+
return registerFocusable(props.id, {
|
|
152
|
+
focus: () => {
|
|
153
|
+
setChecked((x) => !x);
|
|
154
|
+
}
|
|
155
|
+
});
|
|
156
|
+
}, [props.id, setChecked]);
|
|
157
|
+
}
|
|
158
|
+
return /* @__PURE__ */ React.createElement(SwitchProvider, {
|
|
159
|
+
scope: __scopeSwitch,
|
|
160
|
+
checked,
|
|
161
|
+
disabled,
|
|
162
|
+
size
|
|
163
|
+
}, /* @__PURE__ */ React.createElement(SwitchFrame, __spreadProps(__spreadValues({
|
|
164
|
+
size,
|
|
165
|
+
role: "switch",
|
|
166
|
+
"aria-checked": checked,
|
|
167
|
+
"aria-labelledby": labelledBy,
|
|
168
|
+
"aria-required": required,
|
|
169
|
+
"data-state": getState(checked),
|
|
170
|
+
"data-disabled": disabled ? "" : void 0,
|
|
171
|
+
disabled,
|
|
172
|
+
theme: checked ? "active" : null,
|
|
173
|
+
themeShallow: true,
|
|
174
|
+
tabIndex: disabled ? void 0 : 0,
|
|
175
|
+
value
|
|
176
|
+
}, switchProps), {
|
|
177
|
+
ref: composedRefs,
|
|
178
|
+
onPress: (event) => {
|
|
179
|
+
var _a2;
|
|
180
|
+
(_a2 = props.onPress) == null ? void 0 : _a2.call(props, event);
|
|
181
|
+
setChecked((prevChecked) => !prevChecked);
|
|
182
|
+
if (isWeb && isFormControl) {
|
|
183
|
+
hasConsumerStoppedPropagationRef.current = event.isPropagationStopped();
|
|
184
|
+
if (!hasConsumerStoppedPropagationRef.current)
|
|
185
|
+
event.stopPropagation();
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
})), isWeb && isFormControl && /* @__PURE__ */ React.createElement(BubbleInput, {
|
|
189
|
+
control: button,
|
|
190
|
+
bubbles: !hasConsumerStoppedPropagationRef.current,
|
|
191
|
+
name,
|
|
192
|
+
value,
|
|
193
|
+
checked,
|
|
194
|
+
required,
|
|
195
|
+
disabled,
|
|
196
|
+
style: { transform: "translateX(-100%)" }
|
|
197
|
+
}));
|
|
198
|
+
})), {
|
|
199
|
+
Thumb: SwitchThumb
|
|
200
|
+
});
|
|
201
|
+
const BubbleInput = (props) => {
|
|
202
|
+
const _a = props, { control, checked, bubbles = true } = _a, inputProps = __objRest(_a, ["control", "checked", "bubbles"]);
|
|
203
|
+
const ref = React.useRef(null);
|
|
204
|
+
const prevChecked = usePrevious(checked);
|
|
205
|
+
React.useEffect(() => {
|
|
206
|
+
const input = ref.current;
|
|
207
|
+
const inputProto = window.HTMLInputElement.prototype;
|
|
208
|
+
const descriptor = Object.getOwnPropertyDescriptor(inputProto, "checked");
|
|
209
|
+
const setChecked = descriptor.set;
|
|
210
|
+
if (prevChecked !== checked && setChecked) {
|
|
211
|
+
const event = new Event("click", { bubbles });
|
|
212
|
+
setChecked.call(input, checked);
|
|
213
|
+
input.dispatchEvent(event);
|
|
214
|
+
}
|
|
215
|
+
}, [prevChecked, checked, bubbles]);
|
|
216
|
+
return /* @__PURE__ */ React.createElement("input", __spreadProps(__spreadValues({
|
|
217
|
+
type: "checkbox",
|
|
218
|
+
"aria-hidden": true,
|
|
219
|
+
defaultChecked: checked
|
|
220
|
+
}, inputProps), {
|
|
221
|
+
tabIndex: -1,
|
|
222
|
+
ref,
|
|
223
|
+
style: __spreadProps(__spreadValues({}, props.style), {
|
|
224
|
+
position: "absolute",
|
|
225
|
+
pointerEvents: "none",
|
|
226
|
+
opacity: 0,
|
|
227
|
+
margin: 0
|
|
228
|
+
})
|
|
229
|
+
}));
|
|
230
|
+
};
|
|
231
|
+
function getState(checked) {
|
|
232
|
+
return checked ? "checked" : "unchecked";
|
|
233
|
+
}
|
|
234
|
+
export {
|
|
235
|
+
Switch,
|
|
236
|
+
SwitchFrame,
|
|
237
|
+
SwitchThumb,
|
|
238
|
+
SwitchThumbFrame,
|
|
239
|
+
createSwitchScope
|
|
240
|
+
};
|
|
241
|
+
//# sourceMappingURL=Switch.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/Switch.tsx"],
|
|
4
|
+
"sourcesContent": ["// via radix\n// https://github.com/radix-ui/primitives/blob/main/packages/react/switch/src/Switch.tsx\n\nimport { usePrevious } from '@radix-ui/react-use-previous'\nimport { useComposedRefs } from '@tamagui/compose-refs'\nimport {\n GetProps,\n SizeTokens,\n Theme,\n calc,\n getSize,\n getVariableValue,\n isWeb,\n styled,\n themeable,\n useTheme,\n useThemeName,\n withStaticProperties,\n} from '@tamagui/core'\nimport { ScopedProps, createContextScope } from '@tamagui/create-context'\nimport { registerFocusable } from '@tamagui/focusable'\nimport { useLabelContext } from '@tamagui/label'\nimport { ThemeableStack, XStack } from '@tamagui/stacks'\nimport { useControllableState } from '@tamagui/use-controllable-state'\nimport * as React from 'react'\nimport { View } from 'react-native'\n\nconst SWITCH_NAME = 'Switch'\n\n// TODO make customizable\nconst getSwitchHeight = (val: SizeTokens) => Math.round(getVariableValue(getSize(val)) * 0.65)\nconst getSwitchWidth = (val: SizeTokens) => getSwitchHeight(val) * 2\n\nconst scopeContexts = createContextScope(SWITCH_NAME)\nconst [createSwitchContext] = scopeContexts\nexport const createSwitchScope = scopeContexts[1]\n\nconst [SwitchProvider, useSwitchContext] = createSwitchContext<{\n checked: boolean\n disabled?: boolean\n size: SizeTokens\n}>(SWITCH_NAME)\n\n/* -------------------------------------------------------------------------------------------------\n * SwitchThumb\n * -----------------------------------------------------------------------------------------------*/\n\nconst THUMB_NAME = 'SwitchThumb'\n\nexport const SwitchThumbFrame = styled(ThemeableStack, {\n name: 'SwitchThumb',\n backgroundColor: '$background',\n borderRadius: 1000,\n\n variants: {\n size: {\n '...size': (val) => ({\n height: getSwitchHeight(val),\n width: getSwitchHeight(val),\n }),\n },\n },\n\n defaultVariants: {\n size: '$4',\n },\n})\n\nexport type SwitchThumbProps = GetProps<typeof SwitchThumbFrame>\n\nexport const SwitchThumb = SwitchThumbFrame.extractable(\n React.forwardRef<React.ElementRef<'span'>, SwitchThumbProps>(\n (props: ScopedProps<SwitchThumbProps, 'Switch'>, forwardedRef) => {\n const { __scopeSwitch, ...thumbProps } = props\n const { size, disabled, checked } = useSwitchContext(THUMB_NAME, __scopeSwitch)\n return (\n <SwitchThumbFrame\n size={size}\n data-state={getState(checked)}\n data-disabled={disabled ? '' : undefined}\n {...thumbProps}\n x={\n checked\n ? getVariableValue(getSwitchWidth(size)) - getVariableValue(getSwitchHeight(size))\n : 0\n }\n ref={forwardedRef}\n />\n )\n }\n )\n)\n\nSwitchThumb.displayName = THUMB_NAME\n\n/* -------------------------------------------------------------------------------------------------\n * Switch\n * -----------------------------------------------------------------------------------------------*/\n\nexport const SwitchFrame = styled(XStack, {\n name: 'Switch',\n tag: 'button',\n borderRadius: 1000,\n borderWidth: 2,\n borderColor: 'transparent',\n backgroundColor: '$background',\n\n focusStyle: {\n borderColor: '$borderColorFocus',\n },\n\n variants: {\n size: {\n '...size': (val) => {\n const height = calc(getSwitchHeight(val), '+', 4)\n const width = calc(getSwitchWidth(val), '+', 4)\n return {\n height,\n minHeight: height,\n width,\n }\n },\n },\n },\n\n defaultVariants: {\n size: '$4',\n },\n})\n\ntype SwitchButtonProps = GetProps<typeof SwitchFrame>\n\nexport type SwitchProps = SwitchButtonProps & {\n labeledBy?: string\n name?: string\n value?: string\n checked?: boolean\n defaultChecked?: boolean\n required?: boolean\n onCheckedChange?(checked: boolean): void\n}\n\nexport const Switch = withStaticProperties(\n SwitchFrame.extractable(\n React.forwardRef<HTMLButtonElement | View, SwitchProps>(\n (props: ScopedProps<SwitchProps, 'Switch'>, forwardedRef) => {\n const {\n __scopeSwitch,\n labeledBy: ariaLabelledby,\n name,\n checked: checkedProp,\n defaultChecked,\n required,\n disabled,\n value = 'on',\n onCheckedChange,\n size = '$4',\n ...switchProps\n } = props\n const [button, setButton] = React.useState<HTMLButtonElement | null>(null)\n const composedRefs = useComposedRefs(forwardedRef, (node) => setButton(node as any))\n const labelId = useLabelContext(button)\n const labelledBy = ariaLabelledby || labelId\n const hasConsumerStoppedPropagationRef = React.useRef(false)\n // We set this to true by default so that events bubble to forms without JS (SSR)\n const isFormControl = isWeb ? (button ? Boolean(button.closest('form')) : true) : false\n const [checked = false, setChecked] = useControllableState({\n prop: checkedProp,\n defaultProp: defaultChecked || false,\n onChange: onCheckedChange,\n })\n\n if (!isWeb) {\n // eslint-disable-next-line react-hooks/rules-of-hooks\n React.useEffect(() => {\n if (!props.id) return\n return registerFocusable(props.id, {\n focus: () => {\n setChecked((x) => !x)\n },\n })\n }, [props.id, setChecked])\n }\n\n return (\n <SwitchProvider scope={__scopeSwitch} checked={checked} disabled={disabled} size={size}>\n <SwitchFrame\n size={size}\n // @ts-ignore\n role=\"switch\"\n aria-checked={checked}\n aria-labelledby={labelledBy}\n aria-required={required}\n data-state={getState(checked)}\n data-disabled={disabled ? '' : undefined}\n disabled={disabled}\n theme={checked ? 'active' : null}\n themeShallow\n // @ts-ignore\n tabIndex={disabled ? undefined : 0}\n // @ts-ignore\n value={value}\n {...switchProps}\n ref={composedRefs}\n onPress={(event) => {\n props.onPress?.(event)\n setChecked((prevChecked) => !prevChecked)\n if (isWeb && isFormControl) {\n hasConsumerStoppedPropagationRef.current = event.isPropagationStopped()\n // if switch is in a form, stop propagation from the button so that we only propagate\n // one click event (from the input). We propagate changes from an input so that native\n // form validation works and form events reflect switch updates.\n if (!hasConsumerStoppedPropagationRef.current) event.stopPropagation()\n }\n }}\n />\n {isWeb && isFormControl && (\n <BubbleInput\n control={button}\n bubbles={!hasConsumerStoppedPropagationRef.current}\n name={name}\n value={value}\n checked={checked}\n required={required}\n disabled={disabled}\n // We transform because the input is absolutely positioned but we have\n // rendered it **after** the button. This pulls it back to sit on top\n // of the button.\n style={{ transform: 'translateX(-100%)' }}\n />\n )}\n </SwitchProvider>\n )\n }\n )\n ),\n {\n Thumb: SwitchThumb,\n }\n)\n\n/* ---------------------------------------------------------------------------------------------- */\n\ntype InputProps = any //Radix.ComponentPropsWithoutRef<'input'>\ninterface BubbleInputProps extends Omit<InputProps, 'checked'> {\n checked: boolean\n control: HTMLElement | null\n bubbles: boolean\n}\n\n// TODO make this native friendly\nconst BubbleInput = (props: BubbleInputProps) => {\n const { control, checked, bubbles = true, ...inputProps } = props\n const ref = React.useRef<HTMLInputElement>(null)\n const prevChecked = usePrevious(checked)\n // const controlSize = useSize(control)\n\n // Bubble checked change to parents (e.g form change event)\n React.useEffect(() => {\n const input = ref.current!\n const inputProto = window.HTMLInputElement.prototype\n const descriptor = Object.getOwnPropertyDescriptor(inputProto, 'checked') as PropertyDescriptor\n const setChecked = descriptor.set\n if (prevChecked !== checked && setChecked) {\n const event = new Event('click', { bubbles })\n setChecked.call(input, checked)\n input.dispatchEvent(event)\n }\n }, [prevChecked, checked, bubbles])\n\n return (\n <input\n type=\"checkbox\"\n aria-hidden\n defaultChecked={checked}\n {...inputProps}\n tabIndex={-1}\n ref={ref}\n style={{\n ...props.style,\n // ...controlSize,\n position: 'absolute',\n pointerEvents: 'none',\n opacity: 0,\n margin: 0,\n }}\n />\n )\n}\n\nfunction getState(checked: boolean) {\n return checked ? 'checked' : 'unchecked'\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAGA;AACA;AACA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAcA;AACA;AACA;AACA;AACA;AACA;AAGA,MAAM,cAAc;AAGpB,MAAM,kBAAkB,CAAC,QAAoB,KAAK,MAAM,iBAAiB,QAAQ,GAAG,CAAC,IAAI,IAAI;AAC7F,MAAM,iBAAiB,CAAC,QAAoB,gBAAgB,GAAG,IAAI;AAEnE,MAAM,gBAAgB,mBAAmB,WAAW;AACpD,MAAM,CAAC,uBAAuB;AACvB,MAAM,oBAAo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W,KAAK,OAAO,OAAO;AAC9B,YAAM,cAAc,KAAK;AAAA,IAC3B;AAAA,EACF,GAAG,CAAC,aAAa,SAAS,OAAO,CAAC;AAElC,SACE,oCAAC;AAAA,IACC,MAAK;AAAA,IACL,eAAW;AAAA,IACX,gBAAgB;AAAA,KACZ,aAJL;AAAA,IAKC,UAAU;AAAA,IACV;AAAA,IACA,OAAO,iCACF,MAAM,QADJ;AAAA,MAGL,UAAU;AAAA,MACV,eAAe;AAAA,MACf,SAAS;AAAA,MACT,QAAQ;AAAA,IACV;AAAA,IACF;AAEJ;AAEA,kBAAkB,SAAkB;AAClC,SAAO,UAAU,YAAY;AAC/B;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
package/dist/jsx/Card.js
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import {
|
|
2
|
+
isTamaguiElement,
|
|
3
|
+
styled,
|
|
4
|
+
themeable,
|
|
5
|
+
withStaticProperties
|
|
6
|
+
} from "@tamagui/core";
|
|
7
|
+
import { createContextScope } from "@tamagui/create-context";
|
|
8
|
+
import { ThemeableStack } from "@tamagui/stacks";
|
|
9
|
+
import React, { cloneElement, forwardRef } from "react";
|
|
10
|
+
React["createElement"];
|
|
11
|
+
const CARD_NAME = "CARD";
|
|
12
|
+
const [createCardContext, createCardScope] = createContextScope(CARD_NAME);
|
|
13
|
+
const [CardProvider, useCardContext] = createCardContext(CARD_NAME);
|
|
14
|
+
const CardFrame = styled(ThemeableStack, {
|
|
15
|
+
name: "Card",
|
|
16
|
+
backgroundColor: "$background",
|
|
17
|
+
position: "relative",
|
|
18
|
+
overflow: "hidden",
|
|
19
|
+
variants: {
|
|
20
|
+
size: {
|
|
21
|
+
"...size": (val, { tokens }) => {
|
|
22
|
+
var _a;
|
|
23
|
+
return {
|
|
24
|
+
borderRadius: (_a = tokens.radius[val]) != null ? _a : val
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
defaultVariants: {
|
|
30
|
+
size: "$4"
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
const CardHeader = styled(ThemeableStack, {
|
|
34
|
+
name: "CardHeader",
|
|
35
|
+
zIndex: 10,
|
|
36
|
+
backgroundColor: "transparent",
|
|
37
|
+
marginBottom: "auto",
|
|
38
|
+
variants: {
|
|
39
|
+
size: {
|
|
40
|
+
"...size": (val, { tokens }) => {
|
|
41
|
+
var _a;
|
|
42
|
+
return {
|
|
43
|
+
padding: (_a = tokens.space[val]) != null ? _a : val
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
const CardFooter = styled(CardHeader, {
|
|
50
|
+
name: "CardFooter",
|
|
51
|
+
zIndex: 5,
|
|
52
|
+
flexDirection: "row",
|
|
53
|
+
marginTop: "auto",
|
|
54
|
+
marginBottom: 0
|
|
55
|
+
});
|
|
56
|
+
const CardBackground = styled(ThemeableStack, {
|
|
57
|
+
name: "CardBackground",
|
|
58
|
+
zIndex: 0,
|
|
59
|
+
fullscreen: true,
|
|
60
|
+
overflow: "hidden",
|
|
61
|
+
pointerEvents: "none",
|
|
62
|
+
padding: 0
|
|
63
|
+
});
|
|
64
|
+
const Card = withStaticProperties(CardFrame.extractable(themeable(forwardRef(({ size, __scopeCard, children, ...props }, ref) => {
|
|
65
|
+
return <CardProvider scope={__scopeCard} size={size}><CardFrame ref={ref} {...props}>{React.Children.map(children, (child) => {
|
|
66
|
+
if (isTamaguiElement(child) && !child.props.size) {
|
|
67
|
+
return cloneElement(child, {
|
|
68
|
+
size
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
return child;
|
|
72
|
+
})}</CardFrame></CardProvider>;
|
|
73
|
+
}))), {
|
|
74
|
+
Header: CardHeader,
|
|
75
|
+
Footer: CardFooter,
|
|
76
|
+
Background: CardBackground
|
|
77
|
+
});
|
|
78
|
+
export {
|
|
79
|
+
Card,
|
|
80
|
+
CardBackground,
|
|
81
|
+
CardFooter,
|
|
82
|
+
CardFrame,
|
|
83
|
+
CardHeader,
|
|
84
|
+
createCardScope,
|
|
85
|
+
useCardContext
|
|
86
|
+
};
|
|
87
|
+
//# sourceMappingURL=Card.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/Card.tsx"],
|
|
4
|
+
"sourcesContent": ["import {\n GetProps,\n SizeTokens,\n isTamaguiElement,\n styled,\n themeable,\n withStaticProperties,\n} from '@tamagui/core'\nimport { ScopedProps, createContextScope } from '@tamagui/create-context'\nimport { ThemeableStack } from '@tamagui/stacks'\nimport React, { cloneElement, forwardRef } from 'react'\nimport { View } from 'react-native'\n\n// bugfix esbuild strips react jsx: 'preserve'\nReact['createElement']\n\nconst CARD_NAME = 'CARD'\n\ntype CardContextValue = {\n size?: SizeTokens\n}\n\nconst [createCardContext, createCardScope] = createContextScope(CARD_NAME)\nconst [CardProvider, useCardContext] = createCardContext<CardContextValue>(CARD_NAME)\n\nexport const CardFrame = styled(ThemeableStack, {\n name: 'Card',\n backgroundColor: '$background',\n position: 'relative',\n overflow: 'hidden',\n\n variants: {\n size: {\n '...size': (val, { tokens }) => {\n return {\n borderRadius: tokens.radius[val] ?? val,\n }\n },\n },\n },\n\n defaultVariants: {\n size: '$4',\n },\n})\n\nexport const CardHeader = styled(ThemeableStack, {\n name: 'CardHeader',\n zIndex: 10,\n backgroundColor: 'transparent',\n marginBottom: 'auto',\n\n variants: {\n size: {\n '...size': (val, { tokens }) => {\n return {\n padding: tokens.space[val] ?? val,\n }\n },\n },\n },\n})\n\nexport const CardFooter = styled(CardHeader, {\n name: 'CardFooter',\n zIndex: 5,\n flexDirection: 'row',\n marginTop: 'auto',\n marginBottom: 0,\n})\n\nexport const CardBackground = styled(ThemeableStack, {\n name: 'CardBackground',\n zIndex: 0,\n fullscreen: true,\n overflow: 'hidden',\n pointerEvents: 'none',\n padding: 0,\n})\n\nexport type CardHeaderProps = GetProps<typeof CardHeader>\nexport type CardFooterProps = GetProps<typeof CardFooter>\n\nexport type CardProps = GetProps<typeof CardFrame>\n\nexport const Card = withStaticProperties(\n CardFrame.extractable(\n themeable(\n forwardRef<HTMLElement | View, ScopedProps<CardProps, 'Card'>>(\n ({ size, __scopeCard, children, ...props }, ref) => {\n return (\n <CardProvider scope={__scopeCard} size={size}>\n <CardFrame ref={ref} {...props}>\n {React.Children.map(children, (child) => {\n if (isTamaguiElement(child) && !child.props.size) {\n return cloneElement(child, {\n size,\n })\n }\n return child\n })}\n </CardFrame>\n </CardProvider>\n )\n }\n )\n )\n ),\n {\n Header: CardHeader,\n Footer: CardFooter,\n Background: CardBackground,\n }\n)\n\nexport { createCardScope, useCardContext }\n"],
|
|
5
|
+
"mappings": "AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAQA;AACA;AACA;AAIA,MAAM;AAEN,MAAM,YAAY;AAMlB,MAAM,CAAC,mBAAmB,mBAAmB,mBAAmB,SAAS;AACzE,MAAM,CAAC,cAAc,kBAAkB,kBAAoC,SAAS;AAE7E,MAAM,YAAY,OAAO,gBAAgB;AAAA,EAC9C,MAAM;AAAA,EACN,iBAAiB;AAAA,EACjB,UAAU;AAAA,EACV,UAAU;AAAA,EAEV,UAAU;AAAA,IACR,MAAM;AAAA,MACJ,WAAW,CAAC,KAAK,EAAE,aAAa;AAjCtC;AAkCQ,eAAO;AAAA,UACL,cAAc,aAAO,OAAO,SAAd,YAAsB;AAAA,QACtC;AAAA,MACF;AAAA,IACF;AAAA,EACF;AAAA,EAEA,iBAAiB;AAAA,IACf,MAAM;AAAA,EACR;AACF,CAAC;AAEM,MAAM,aAAa,OAAO,gBAAgB;AAAA,EAC/C,MAAM;AAAA,EACN,QAAQ;AAAA,EACR,iBAAiB;AAAA,EACjB,cAAc;AAAA,EAEd,UAAU;AAAA,IACR,MAAM;AAAA,MACJ,WAAW,CAAC,KAAK,EAAE,aAAa;AAtDtC;AAuDQ,eAAO;AAAA,UACL,SAAS,aAAO,MAAM,SAAb,YAAqB;AAAA,QAChC;AAAA,MACF;AAAA,IACF;AAAA,EACF;AACF,CAAC;AAEM,MAAM,aAAa,OAAO,YAAY;AAAA,EAC3C,MAAM;AAAA,EACN,QAAQ;AAAA,EACR,eAAe;AAAA,EACf,WAAW;AAAA,EACX,cAAc;AAChB,CAAC;AAEM,MAAM,iBAAiB,OAAO,gBAAgB;AAAA,EACnD,MAAM;AAAA,EACN,QAAQ;AAAA,EACR,YAAY;AAAA,EACZ,UAAU;AAAA,EACV,eAAe;AAAA,EACf,SAAS;AACX,CAAC;AAOM,MAAM,OAAO,qBAClB,UAAU,YACR,UACE,WACE,CAAC,EAAE,MAAM,aAAa,aAAa,SAAS,QAAQ;AAClD,SACE,CAAC,aAAa,OAAO,aAAa,MAAM,MACtC,CAAC,UAAU,KAAK,SAAS,QACtB,MAAM,SAAS,IAAI,UAAU,CAAC,UAAU;AACvC,QAAI,iBAAiB,KAAK,KAAK,CAAC,MAAM,MAAM,MAAM;AAChD,aAAO,aAAa,OAAO;AAAA,QACzB;AAAA,MACF,CAAC;AAAA,IACH;AACA,WAAO;AAAA,EACT,CAAC,EACH,EATC,UAUH,EAXC;AAaL,CACF,CACF,CACF,GACA;AAAA,EACE,QAAQ;AAAA,EACR,QAAQ;AAAA,EACR,YAAY;AACd,CACF;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import { usePrevious } from "@radix-ui/react-use-previous";
|
|
2
|
+
import { useComposedRefs } from "@tamagui/compose-refs";
|
|
3
|
+
import {
|
|
4
|
+
calc,
|
|
5
|
+
getSize,
|
|
6
|
+
getVariableValue,
|
|
7
|
+
isWeb,
|
|
8
|
+
styled,
|
|
9
|
+
withStaticProperties
|
|
10
|
+
} from "@tamagui/core";
|
|
11
|
+
import { createContextScope } from "@tamagui/create-context";
|
|
12
|
+
import { registerFocusable } from "@tamagui/focusable";
|
|
13
|
+
import { useLabelContext } from "@tamagui/label";
|
|
14
|
+
import { ThemeableStack, XStack } from "@tamagui/stacks";
|
|
15
|
+
import { useControllableState } from "@tamagui/use-controllable-state";
|
|
16
|
+
import * as React from "react";
|
|
17
|
+
const SWITCH_NAME = "Switch";
|
|
18
|
+
const getSwitchHeight = (val) => Math.round(getVariableValue(getSize(val)) * 0.65);
|
|
19
|
+
const getSwitchWidth = (val) => getSwitchHeight(val) * 2;
|
|
20
|
+
const scopeContexts = createContextScope(SWITCH_NAME);
|
|
21
|
+
const [createSwitchContext] = scopeContexts;
|
|
22
|
+
const createSwitchScope = scopeContexts[1];
|
|
23
|
+
const [SwitchProvider, useSwitchContext] = createSwitchContext(SWITCH_NAME);
|
|
24
|
+
const THUMB_NAME = "SwitchThumb";
|
|
25
|
+
const SwitchThumbFrame = styled(ThemeableStack, {
|
|
26
|
+
name: "SwitchThumb",
|
|
27
|
+
backgroundColor: "$background",
|
|
28
|
+
borderRadius: 1e3,
|
|
29
|
+
variants: {
|
|
30
|
+
size: {
|
|
31
|
+
"...size": (val) => ({
|
|
32
|
+
height: getSwitchHeight(val),
|
|
33
|
+
width: getSwitchHeight(val)
|
|
34
|
+
})
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
defaultVariants: {
|
|
38
|
+
size: "$4"
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
const SwitchThumb = SwitchThumbFrame.extractable(React.forwardRef((props, forwardedRef) => {
|
|
42
|
+
const { __scopeSwitch, ...thumbProps } = props;
|
|
43
|
+
const { size, disabled, checked } = useSwitchContext(THUMB_NAME, __scopeSwitch);
|
|
44
|
+
return <SwitchThumbFrame size={size} data-state={getState(checked)} data-disabled={disabled ? "" : void 0} {...thumbProps} x={checked ? getVariableValue(getSwitchWidth(size)) - getVariableValue(getSwitchHeight(size)) : 0} ref={forwardedRef} />;
|
|
45
|
+
}));
|
|
46
|
+
SwitchThumb.displayName = THUMB_NAME;
|
|
47
|
+
const SwitchFrame = styled(XStack, {
|
|
48
|
+
name: "Switch",
|
|
49
|
+
tag: "button",
|
|
50
|
+
borderRadius: 1e3,
|
|
51
|
+
borderWidth: 2,
|
|
52
|
+
borderColor: "transparent",
|
|
53
|
+
backgroundColor: "$background",
|
|
54
|
+
focusStyle: {
|
|
55
|
+
borderColor: "$borderColorFocus"
|
|
56
|
+
},
|
|
57
|
+
variants: {
|
|
58
|
+
size: {
|
|
59
|
+
"...size": (val) => {
|
|
60
|
+
const height = calc(getSwitchHeight(val), "+", 4);
|
|
61
|
+
const width = calc(getSwitchWidth(val), "+", 4);
|
|
62
|
+
return {
|
|
63
|
+
height,
|
|
64
|
+
minHeight: height,
|
|
65
|
+
width
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
},
|
|
70
|
+
defaultVariants: {
|
|
71
|
+
size: "$4"
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
const Switch = withStaticProperties(SwitchFrame.extractable(React.forwardRef((props, forwardedRef) => {
|
|
75
|
+
const {
|
|
76
|
+
__scopeSwitch,
|
|
77
|
+
labeledBy: ariaLabelledby,
|
|
78
|
+
name,
|
|
79
|
+
checked: checkedProp,
|
|
80
|
+
defaultChecked,
|
|
81
|
+
required,
|
|
82
|
+
disabled,
|
|
83
|
+
value = "on",
|
|
84
|
+
onCheckedChange,
|
|
85
|
+
size = "$4",
|
|
86
|
+
...switchProps
|
|
87
|
+
} = props;
|
|
88
|
+
const [button, setButton] = React.useState(null);
|
|
89
|
+
const composedRefs = useComposedRefs(forwardedRef, (node) => setButton(node));
|
|
90
|
+
const labelId = useLabelContext(button);
|
|
91
|
+
const labelledBy = ariaLabelledby || labelId;
|
|
92
|
+
const hasConsumerStoppedPropagationRef = React.useRef(false);
|
|
93
|
+
const isFormControl = isWeb ? button ? Boolean(button.closest("form")) : true : false;
|
|
94
|
+
const [checked = false, setChecked] = useControllableState({
|
|
95
|
+
prop: checkedProp,
|
|
96
|
+
defaultProp: defaultChecked || false,
|
|
97
|
+
onChange: onCheckedChange
|
|
98
|
+
});
|
|
99
|
+
if (!isWeb) {
|
|
100
|
+
React.useEffect(() => {
|
|
101
|
+
if (!props.id)
|
|
102
|
+
return;
|
|
103
|
+
return registerFocusable(props.id, {
|
|
104
|
+
focus: () => {
|
|
105
|
+
setChecked((x) => !x);
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
}, [props.id, setChecked]);
|
|
109
|
+
}
|
|
110
|
+
return <SwitchProvider scope={__scopeSwitch} checked={checked} disabled={disabled} size={size}>
|
|
111
|
+
<SwitchFrame size={size} role="switch" aria-checked={checked} aria-labelledby={labelledBy} aria-required={required} data-state={getState(checked)} data-disabled={disabled ? "" : void 0} disabled={disabled} theme={checked ? "active" : null} themeShallow tabIndex={disabled ? void 0 : 0} value={value} {...switchProps} ref={composedRefs} onPress={(event) => {
|
|
112
|
+
var _a;
|
|
113
|
+
(_a = props.onPress) == null ? void 0 : _a.call(props, event);
|
|
114
|
+
setChecked((prevChecked) => !prevChecked);
|
|
115
|
+
if (isWeb && isFormControl) {
|
|
116
|
+
hasConsumerStoppedPropagationRef.current = event.isPropagationStopped();
|
|
117
|
+
if (!hasConsumerStoppedPropagationRef.current)
|
|
118
|
+
event.stopPropagation();
|
|
119
|
+
}
|
|
120
|
+
}} />
|
|
121
|
+
{isWeb && isFormControl && <BubbleInput control={button} bubbles={!hasConsumerStoppedPropagationRef.current} name={name} value={value} checked={checked} required={required} disabled={disabled} style={{ transform: "translateX(-100%)" }} />}
|
|
122
|
+
</SwitchProvider>;
|
|
123
|
+
})), {
|
|
124
|
+
Thumb: SwitchThumb
|
|
125
|
+
});
|
|
126
|
+
const BubbleInput = (props) => {
|
|
127
|
+
const { control, checked, bubbles = true, ...inputProps } = props;
|
|
128
|
+
const ref = React.useRef(null);
|
|
129
|
+
const prevChecked = usePrevious(checked);
|
|
130
|
+
React.useEffect(() => {
|
|
131
|
+
const input = ref.current;
|
|
132
|
+
const inputProto = window.HTMLInputElement.prototype;
|
|
133
|
+
const descriptor = Object.getOwnPropertyDescriptor(inputProto, "checked");
|
|
134
|
+
const setChecked = descriptor.set;
|
|
135
|
+
if (prevChecked !== checked && setChecked) {
|
|
136
|
+
const event = new Event("click", { bubbles });
|
|
137
|
+
setChecked.call(input, checked);
|
|
138
|
+
input.dispatchEvent(event);
|
|
139
|
+
}
|
|
140
|
+
}, [prevChecked, checked, bubbles]);
|
|
141
|
+
return <input type="checkbox" aria-hidden defaultChecked={checked} {...inputProps} tabIndex={-1} ref={ref} style={{
|
|
142
|
+
...props.style,
|
|
143
|
+
position: "absolute",
|
|
144
|
+
pointerEvents: "none",
|
|
145
|
+
opacity: 0,
|
|
146
|
+
margin: 0
|
|
147
|
+
}} />;
|
|
148
|
+
};
|
|
149
|
+
function getState(checked) {
|
|
150
|
+
return checked ? "checked" : "unchecked";
|
|
151
|
+
}
|
|
152
|
+
export {
|
|
153
|
+
Switch,
|
|
154
|
+
SwitchFrame,
|
|
155
|
+
SwitchThumb,
|
|
156
|
+
SwitchThumbFrame,
|
|
157
|
+
createSwitchScope
|
|
158
|
+
};
|
|
159
|
+
//# sourceMappingURL=Switch.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/Switch.tsx"],
|
|
4
|
+
"sourcesContent": ["// via radix\n// https://github.com/radix-ui/primitives/blob/main/packages/react/switch/src/Switch.tsx\n\nimport { usePrevious } from '@radix-ui/react-use-previous'\nimport { useComposedRefs } from '@tamagui/compose-refs'\nimport {\n GetProps,\n SizeTokens,\n Theme,\n calc,\n getSize,\n getVariableValue,\n isWeb,\n styled,\n themeable,\n useTheme,\n useThemeName,\n withStaticProperties,\n} from '@tamagui/core'\nimport { ScopedProps, createContextScope } from '@tamagui/create-context'\nimport { registerFocusable } from '@tamagui/focusable'\nimport { useLabelContext } from '@tamagui/label'\nimport { ThemeableStack, XStack } from '@tamagui/stacks'\nimport { useControllableState } from '@tamagui/use-controllable-state'\nimport * as React from 'react'\nimport { View } from 'react-native'\n\nconst SWITCH_NAME = 'Switch'\n\n// TODO make customizable\nconst getSwitchHeight = (val: SizeTokens) => Math.round(getVariableValue(getSize(val)) * 0.65)\nconst getSwitchWidth = (val: SizeTokens) => getSwitchHeight(val) * 2\n\nconst scopeContexts = createContextScope(SWITCH_NAME)\nconst [createSwitchContext] = scopeContexts\nexport const createSwitchScope = scopeContexts[1]\n\nconst [SwitchProvider, useSwitchContext] = createSwitchContext<{\n checked: boolean\n disabled?: boolean\n size: SizeTokens\n}>(SWITCH_NAME)\n\n/* -------------------------------------------------------------------------------------------------\n * SwitchThumb\n * -----------------------------------------------------------------------------------------------*/\n\nconst THUMB_NAME = 'SwitchThumb'\n\nexport const SwitchThumbFrame = styled(ThemeableStack, {\n name: 'SwitchThumb',\n backgroundColor: '$background',\n borderRadius: 1000,\n\n variants: {\n size: {\n '...size': (val) => ({\n height: getSwitchHeight(val),\n width: getSwitchHeight(val),\n }),\n },\n },\n\n defaultVariants: {\n size: '$4',\n },\n})\n\nexport type SwitchThumbProps = GetProps<typeof SwitchThumbFrame>\n\nexport const SwitchThumb = SwitchThumbFrame.extractable(\n React.forwardRef<React.ElementRef<'span'>, SwitchThumbProps>(\n (props: ScopedProps<SwitchThumbProps, 'Switch'>, forwardedRef) => {\n const { __scopeSwitch, ...thumbProps } = props\n const { size, disabled, checked } = useSwitchContext(THUMB_NAME, __scopeSwitch)\n return (\n <SwitchThumbFrame\n size={size}\n data-state={getState(checked)}\n data-disabled={disabled ? '' : undefined}\n {...thumbProps}\n x={\n checked\n ? getVariableValue(getSwitchWidth(size)) - getVariableValue(getSwitchHeight(size))\n : 0\n }\n ref={forwardedRef}\n />\n )\n }\n )\n)\n\nSwitchThumb.displayName = THUMB_NAME\n\n/* -------------------------------------------------------------------------------------------------\n * Switch\n * -----------------------------------------------------------------------------------------------*/\n\nexport const SwitchFrame = styled(XStack, {\n name: 'Switch',\n tag: 'button',\n borderRadius: 1000,\n borderWidth: 2,\n borderColor: 'transparent',\n backgroundColor: '$background',\n\n focusStyle: {\n borderColor: '$borderColorFocus',\n },\n\n variants: {\n size: {\n '...size': (val) => {\n const height = calc(getSwitchHeight(val), '+', 4)\n const width = calc(getSwitchWidth(val), '+', 4)\n return {\n height,\n minHeight: height,\n width,\n }\n },\n },\n },\n\n defaultVariants: {\n size: '$4',\n },\n})\n\ntype SwitchButtonProps = GetProps<typeof SwitchFrame>\n\nexport type SwitchProps = SwitchButtonProps & {\n labeledBy?: string\n name?: string\n value?: string\n checked?: boolean\n defaultChecked?: boolean\n required?: boolean\n onCheckedChange?(checked: boolean): void\n}\n\nexport const Switch = withStaticProperties(\n SwitchFrame.extractable(\n React.forwardRef<HTMLButtonElement | View, SwitchProps>(\n (props: ScopedProps<SwitchProps, 'Switch'>, forwardedRef) => {\n const {\n __scopeSwitch,\n labeledBy: ariaLabelledby,\n name,\n checked: checkedProp,\n defaultChecked,\n required,\n disabled,\n value = 'on',\n onCheckedChange,\n size = '$4',\n ...switchProps\n } = props\n const [button, setButton] = React.useState<HTMLButtonElement | null>(null)\n const composedRefs = useComposedRefs(forwardedRef, (node) => setButton(node as any))\n const labelId = useLabelContext(button)\n const labelledBy = ariaLabelledby || labelId\n const hasConsumerStoppedPropagationRef = React.useRef(false)\n // We set this to true by default so that events bubble to forms without JS (SSR)\n const isFormControl = isWeb ? (button ? Boolean(button.closest('form')) : true) : false\n const [checked = false, setChecked] = useControllableState({\n prop: checkedProp,\n defaultProp: defaultChecked || false,\n onChange: onCheckedChange,\n })\n\n if (!isWeb) {\n // eslint-disable-next-line react-hooks/rules-of-hooks\n React.useEffect(() => {\n if (!props.id) return\n return registerFocusable(props.id, {\n focus: () => {\n setChecked((x) => !x)\n },\n })\n }, [props.id, setChecked])\n }\n\n return (\n <SwitchProvider scope={__scopeSwitch} checked={checked} disabled={disabled} size={size}>\n <SwitchFrame\n size={size}\n // @ts-ignore\n role=\"switch\"\n aria-checked={checked}\n aria-labelledby={labelledBy}\n aria-required={required}\n data-state={getState(checked)}\n data-disabled={disabled ? '' : undefined}\n disabled={disabled}\n theme={checked ? 'active' : null}\n themeShallow\n // @ts-ignore\n tabIndex={disabled ? undefined : 0}\n // @ts-ignore\n value={value}\n {...switchProps}\n ref={composedRefs}\n onPress={(event) => {\n props.onPress?.(event)\n setChecked((prevChecked) => !prevChecked)\n if (isWeb && isFormControl) {\n hasConsumerStoppedPropagationRef.current = event.isPropagationStopped()\n // if switch is in a form, stop propagation from the button so that we only propagate\n // one click event (from the input). We propagate changes from an input so that native\n // form validation works and form events reflect switch updates.\n if (!hasConsumerStoppedPropagationRef.current) event.stopPropagation()\n }\n }}\n />\n {isWeb && isFormControl && (\n <BubbleInput\n control={button}\n bubbles={!hasConsumerStoppedPropagationRef.current}\n name={name}\n value={value}\n checked={checked}\n required={required}\n disabled={disabled}\n // We transform because the input is absolutely positioned but we have\n // rendered it **after** the button. This pulls it back to sit on top\n // of the button.\n style={{ transform: 'translateX(-100%)' }}\n />\n )}\n </SwitchProvider>\n )\n }\n )\n ),\n {\n Thumb: SwitchThumb,\n }\n)\n\n/* ---------------------------------------------------------------------------------------------- */\n\ntype InputProps = any //Radix.ComponentPropsWithoutRef<'input'>\ninterface BubbleInputProps extends Omit<InputProps, 'checked'> {\n checked: boolean\n control: HTMLElement | null\n bubbles: boolean\n}\n\n// TODO make this native friendly\nconst BubbleInput = (props: BubbleInputProps) => {\n const { control, checked, bubbles = true, ...inputProps } = props\n const ref = React.useRef<HTMLInputElement>(null)\n const prevChecked = usePrevious(checked)\n // const controlSize = useSize(control)\n\n // Bubble checked change to parents (e.g form change event)\n React.useEffect(() => {\n const input = ref.current!\n const inputProto = window.HTMLInputElement.prototype\n const descriptor = Object.getOwnPropertyDescriptor(inputProto, 'checked') as PropertyDescriptor\n const setChecked = descriptor.set\n if (prevChecked !== checked && setChecked) {\n const event = new Event('click', { bubbles })\n setChecked.call(input, checked)\n input.dispatchEvent(event)\n }\n }, [prevChecked, checked, bubbles])\n\n return (\n <input\n type=\"checkbox\"\n aria-hidden\n defaultChecked={checked}\n {...inputProps}\n tabIndex={-1}\n ref={ref}\n style={{\n ...props.style,\n // ...controlSize,\n position: 'absolute',\n pointerEvents: 'none',\n opacity: 0,\n margin: 0,\n }}\n />\n )\n}\n\nfunction getState(checked: boolean) {\n return checked ? 'checked' : 'unchecked'\n}\n"],
|
|
5
|
+
"mappings": "AAGA;AACA;AACA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAcA;AACA;AACA;AACA;AACA;AACA;AAGA,MAAM,cAAc;AAGpB,MAAM,kBAAkB,CAAC,QAAoB,KAAK,MAAM,iBAAiB,QAAQ,GAAG,CAAC,IAAI,IAAI;AAC7F,MAAM,iBAAiB,CAAC,QAAoB,gBAAgB,GAAG,IAAI;AAEnE,MAAM,gBAAgB,mBAAmB,WAAW;AACpD,MAAM,CAAC,uBAAuB;AACvB,MAAM,oBAAoB,cAAc;AAE/C,MAAM,CAAC,gBAAgB,oBAAoB,oBAIxC,WAAW;AAMd,MAAM,aAAa;AAEZ,MAAM,mBAAmB,OAAO,gBAAgB;AAAA,EACrD,MAAM;AAAA,EACN,iBAAiB;AAAA,EACjB,cAAc;AAAA,EAEd,UAAU;AAAA,IACR,MAAM;AAAA,MACJ,WAAW,CAAC,QAAS;AAAA,QACnB,QAAQ,gBAAgB,GAAG;AAAA,QAC3B,OAAO,gBAAgB,GAAG;AAAA,MAC5B;AAAA,IACF;AAAA,EACF;AAAA,EAEA,iBAAiB;AAAA,IACf,MAAM;AAAA,EACR;AACF,CAAC;AAIM,MAAM,cAAc,iBAAiB,YAC1C,MAAM,WACJ,CAAC,OAAgD,iBAAiB;AAChE,QAAM,EAAE,kBAAkB,eAAe;AACzC,QAAM,EAAE,MAAM,UAAU,YAAY,iBAAiB,YAAY,aAAa;AAC9E,SACE,CAAC,iBACC,MAAM,MACN,YAAY,SAAS,OAAO,GAC5B,eAAe,WAAW,KAAK,YAC3B,YACJ,GACE,UACI,iBAAiB,eA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AAM;AACpB,UAAM,QAAQ,IAAI;AAClB,UAAM,aAAa,OAAO,iBAAiB;AAC3C,UAAM,aAAa,OAAO,yBAAyB,YAAY,SAAS;AACxE,UAAM,aAAa,WAAW;AAC9B,QAAI,gBAAgB,WAAW,YAAY;AACzC,YAAM,QAAQ,IAAI,MAAM,SAAS,EAAE,QAAQ,CAAC;AAC5C,iBAAW,KAAK,OAAO,OAAO;AAC9B,YAAM,cAAc,KAAK;AAAA,IAC3B;AAAA,EACF,GAAG,CAAC,aAAa,SAAS,OAAO,CAAC;AAElC,SACE,CAAC,MACC,KAAK,WACL,YACA,gBAAgB,aACZ,YACJ,UAAU,IACV,KAAK,KACL,OAAO;AAAA,IACL,GAAG,MAAM;AAAA,IAET,UAAU;AAAA,IACV,eAAe;AAAA,IACf,SAAS;AAAA,IACT,QAAQ;AAAA,EACV,GACF;AAEJ;AAEA,kBAAkB,SAAkB;AAClC,SAAO,UAAU,YAAY;AAC/B;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@tamagui/switch",
|
|
3
|
+
"version": "1.0.1-beta.103",
|
|
4
|
+
"sideEffects": [
|
|
5
|
+
"*.css"
|
|
6
|
+
],
|
|
7
|
+
"source": "src/index.ts",
|
|
8
|
+
"types": "./types/index.d.ts",
|
|
9
|
+
"main": "dist/cjs",
|
|
10
|
+
"module": "dist/esm",
|
|
11
|
+
"module:jsx": "dist/jsx",
|
|
12
|
+
"files": [
|
|
13
|
+
"src",
|
|
14
|
+
"types",
|
|
15
|
+
"dist"
|
|
16
|
+
],
|
|
17
|
+
"scripts": {
|
|
18
|
+
"build": "tamagui-build",
|
|
19
|
+
"watch": "tamagui-build --watch",
|
|
20
|
+
"clean": "tamagui-build clean",
|
|
21
|
+
"clean:build": "tamagui-build clean:build"
|
|
22
|
+
},
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"@radix-ui/react-use-previous": "^0.1.1",
|
|
25
|
+
"@tamagui/compose-refs": "^1.0.1-beta.103",
|
|
26
|
+
"@tamagui/core": "^1.0.1-beta.103",
|
|
27
|
+
"@tamagui/create-context": "^1.0.1-beta.103",
|
|
28
|
+
"@tamagui/focusable": "^1.0.1-beta.103",
|
|
29
|
+
"@tamagui/label": "^1.0.1-beta.103",
|
|
30
|
+
"@tamagui/stacks": "^1.0.1-beta.103",
|
|
31
|
+
"@tamagui/use-controllable-state": "^1.0.1-beta.103"
|
|
32
|
+
},
|
|
33
|
+
"peerDependencies": {
|
|
34
|
+
"react": "*",
|
|
35
|
+
"react-dom": "*",
|
|
36
|
+
"react-native": "*"
|
|
37
|
+
},
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"@tamagui/build": "^1.0.1-beta.103",
|
|
40
|
+
"@types/react-native": "^0.69.2",
|
|
41
|
+
"react": "*",
|
|
42
|
+
"react-dom": "*",
|
|
43
|
+
"react-native": "*"
|
|
44
|
+
},
|
|
45
|
+
"publishConfig": {
|
|
46
|
+
"access": "public"
|
|
47
|
+
}
|
|
48
|
+
}
|