@tamagui/checkbox 1.13.2 → 1.13.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/Checkbox.js +1 -322
- package/dist/cjs/Checkbox.js.map +2 -2
- package/dist/cjs/index.js +1 -18
- package/dist/cjs/index.js.map +2 -2
- package/dist/esm/Checkbox.js +1 -292
- package/dist/esm/Checkbox.js.map +2 -2
- package/dist/esm/Checkbox.mjs +1 -292
- package/dist/esm/Checkbox.mjs.map +2 -2
- package/dist/esm/index.js +1 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/index.mjs +1 -1
- package/dist/esm/index.mjs.map +1 -1
- package/dist/jsx/Checkbox.js +1 -271
- package/dist/jsx/Checkbox.js.map +2 -2
- package/dist/jsx/Checkbox.mjs +1 -271
- package/dist/jsx/Checkbox.mjs.map +2 -2
- package/dist/jsx/index.js +1 -1
- package/dist/jsx/index.js.map +1 -1
- package/dist/jsx/index.mjs +1 -1
- package/dist/jsx/index.mjs.map +1 -1
- package/package.json +11 -11
package/dist/jsx/Checkbox.js
CHANGED
|
@@ -1,272 +1,2 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
composeEventHandlers,
|
|
4
|
-
getVariableValue,
|
|
5
|
-
isWeb,
|
|
6
|
-
styled,
|
|
7
|
-
useComposedRefs,
|
|
8
|
-
useMediaPropsActive,
|
|
9
|
-
useTheme,
|
|
10
|
-
withStaticProperties
|
|
11
|
-
} from "@tamagui/core";
|
|
12
|
-
import { createContextScope } from "@tamagui/create-context";
|
|
13
|
-
import { registerFocusable } from "@tamagui/focusable";
|
|
14
|
-
import { getFontSize } from "@tamagui/font-size";
|
|
15
|
-
import { getSize, stepTokenUpOrDown } from "@tamagui/get-size";
|
|
16
|
-
import { useGetThemedIcon } from "@tamagui/helpers-tamagui";
|
|
17
|
-
import { useLabelContext } from "@tamagui/label";
|
|
18
|
-
import { ThemeableStack } from "@tamagui/stacks";
|
|
19
|
-
import { useControllableState } from "@tamagui/use-controllable-state";
|
|
20
|
-
import * as React from "react";
|
|
21
|
-
function isIndeterminate(checked) {
|
|
22
|
-
return checked === "indeterminate";
|
|
23
|
-
}
|
|
24
|
-
function getState(checked) {
|
|
25
|
-
return isIndeterminate(checked) ? "indeterminate" : checked ? "checked" : "unchecked";
|
|
26
|
-
}
|
|
27
|
-
const BubbleInput = (props) => {
|
|
28
|
-
const { checked, bubbles = true, control, isHidden, ...inputProps } = props;
|
|
29
|
-
const ref = React.useRef(null);
|
|
30
|
-
const prevChecked = usePrevious(checked);
|
|
31
|
-
React.useEffect(() => {
|
|
32
|
-
const input = ref.current;
|
|
33
|
-
const inputProto = window.HTMLInputElement.prototype;
|
|
34
|
-
const descriptor = Object.getOwnPropertyDescriptor(
|
|
35
|
-
inputProto,
|
|
36
|
-
"checked"
|
|
37
|
-
);
|
|
38
|
-
const setChecked = descriptor.set;
|
|
39
|
-
if (prevChecked !== checked && setChecked) {
|
|
40
|
-
const event = new Event("click", { bubbles });
|
|
41
|
-
input.indeterminate = isIndeterminate(checked);
|
|
42
|
-
setChecked.call(input, isIndeterminate(checked) ? false : checked);
|
|
43
|
-
input.dispatchEvent(event);
|
|
44
|
-
}
|
|
45
|
-
}, [prevChecked, checked, bubbles]);
|
|
46
|
-
return <input
|
|
47
|
-
type="checkbox"
|
|
48
|
-
defaultChecked={isIndeterminate(checked) ? false : checked}
|
|
49
|
-
{...inputProps}
|
|
50
|
-
tabIndex={-1}
|
|
51
|
-
ref={ref}
|
|
52
|
-
aria-hidden={isHidden}
|
|
53
|
-
style={{
|
|
54
|
-
...isHidden ? {
|
|
55
|
-
// ...controlSize,
|
|
56
|
-
position: "absolute",
|
|
57
|
-
pointerEvents: "none",
|
|
58
|
-
opacity: 0,
|
|
59
|
-
margin: 0
|
|
60
|
-
} : {
|
|
61
|
-
appearance: "auto",
|
|
62
|
-
accentColor: "var(--color6)"
|
|
63
|
-
},
|
|
64
|
-
...props.style
|
|
65
|
-
}}
|
|
66
|
-
/>;
|
|
67
|
-
};
|
|
68
|
-
const INDICATOR_NAME = "CheckboxIndicator";
|
|
69
|
-
const CheckboxIndicatorFrame = styled(ThemeableStack, {
|
|
70
|
-
// use Checkbox for easier themes
|
|
71
|
-
name: INDICATOR_NAME
|
|
72
|
-
});
|
|
73
|
-
const CheckboxIndicator = CheckboxIndicatorFrame.extractable(
|
|
74
|
-
React.forwardRef(
|
|
75
|
-
(props, forwardedRef) => {
|
|
76
|
-
const {
|
|
77
|
-
__scopeCheckbox,
|
|
78
|
-
children: childrenProp,
|
|
79
|
-
forceMount,
|
|
80
|
-
disablePassStyles,
|
|
81
|
-
...indicatorProps
|
|
82
|
-
} = props;
|
|
83
|
-
const context = useCheckboxContext(INDICATOR_NAME, __scopeCheckbox);
|
|
84
|
-
const iconSize = (typeof context.size === "number" ? context.size * 0.65 : getFontSize(context.size)) * context.scaleIcon;
|
|
85
|
-
const theme = useTheme();
|
|
86
|
-
const getThemedIcon = useGetThemedIcon({ size: iconSize, color: theme.color });
|
|
87
|
-
const childrens = React.Children.toArray(childrenProp);
|
|
88
|
-
const children = childrens.map((child) => {
|
|
89
|
-
if (disablePassStyles || !React.isValidElement(child)) {
|
|
90
|
-
return child;
|
|
91
|
-
}
|
|
92
|
-
return getThemedIcon(child);
|
|
93
|
-
});
|
|
94
|
-
if (forceMount || isIndeterminate(context.state) || context.state === true)
|
|
95
|
-
return <CheckboxIndicatorFrame
|
|
96
|
-
data-state={getState(context.state)}
|
|
97
|
-
data-disabled={context.disabled ? "" : void 0}
|
|
98
|
-
pointerEvents="none"
|
|
99
|
-
{...indicatorProps}
|
|
100
|
-
ref={forwardedRef}
|
|
101
|
-
>{children}</CheckboxIndicatorFrame>;
|
|
102
|
-
return null;
|
|
103
|
-
}
|
|
104
|
-
)
|
|
105
|
-
);
|
|
106
|
-
CheckboxIndicator.displayName = INDICATOR_NAME;
|
|
107
|
-
const CHECKBOX_NAME = "Checkbox";
|
|
108
|
-
const CheckboxFrame = styled(ThemeableStack, {
|
|
109
|
-
name: CHECKBOX_NAME,
|
|
110
|
-
tag: "button",
|
|
111
|
-
variants: {
|
|
112
|
-
unstyled: {
|
|
113
|
-
false: {
|
|
114
|
-
size: "$true",
|
|
115
|
-
backgroundColor: "$background",
|
|
116
|
-
alignItems: "center",
|
|
117
|
-
justifyContent: "center",
|
|
118
|
-
pressTheme: true,
|
|
119
|
-
focusable: true,
|
|
120
|
-
borderWidth: 1,
|
|
121
|
-
borderColor: "$borderColor",
|
|
122
|
-
hoverStyle: {
|
|
123
|
-
borderColor: "$borderColorHover"
|
|
124
|
-
},
|
|
125
|
-
focusStyle: {
|
|
126
|
-
borderColor: "$borderColorFocus"
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
},
|
|
130
|
-
size: {
|
|
131
|
-
"...size": (val, { tokens }) => {
|
|
132
|
-
const radiusToken = getVariableValue(getSize(val)) / 8;
|
|
133
|
-
return {
|
|
134
|
-
borderRadius: radiusToken
|
|
135
|
-
};
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
},
|
|
139
|
-
defaultVariants: {
|
|
140
|
-
unstyled: false
|
|
141
|
-
}
|
|
142
|
-
});
|
|
143
|
-
const [createCheckboxContext, createCheckboxScope] = createContextScope(CHECKBOX_NAME);
|
|
144
|
-
const [CheckboxProvider, useCheckboxContext] = createCheckboxContext(CHECKBOX_NAME);
|
|
145
|
-
const Checkbox = withStaticProperties(
|
|
146
|
-
CheckboxFrame.extractable(
|
|
147
|
-
React.forwardRef(
|
|
148
|
-
(props, forwardedRef) => {
|
|
149
|
-
const {
|
|
150
|
-
__scopeCheckbox,
|
|
151
|
-
labelledBy: ariaLabelledby,
|
|
152
|
-
name,
|
|
153
|
-
checked: checkedProp,
|
|
154
|
-
defaultChecked,
|
|
155
|
-
required,
|
|
156
|
-
scaleIcon = 1,
|
|
157
|
-
scaleSize = 0.45,
|
|
158
|
-
sizeAdjust = 0,
|
|
159
|
-
disabled,
|
|
160
|
-
value = "on",
|
|
161
|
-
onCheckedChange,
|
|
162
|
-
native,
|
|
163
|
-
...checkboxProps
|
|
164
|
-
} = props;
|
|
165
|
-
const [button, setButton] = React.useState(null);
|
|
166
|
-
const composedRefs = useComposedRefs(forwardedRef, (node) => setButton(node));
|
|
167
|
-
const hasConsumerStoppedPropagationRef = React.useRef(false);
|
|
168
|
-
const propsActive = useMediaPropsActive(props);
|
|
169
|
-
const isFormControl = isWeb ? button ? Boolean(button.closest("form")) : true : false;
|
|
170
|
-
const [checked = false, setChecked] = useControllableState({
|
|
171
|
-
prop: checkedProp,
|
|
172
|
-
defaultProp: defaultChecked,
|
|
173
|
-
onChange: onCheckedChange
|
|
174
|
-
});
|
|
175
|
-
const adjustedSize = getVariableValue(
|
|
176
|
-
stepTokenUpOrDown("size", propsActive.size, sizeAdjust)
|
|
177
|
-
);
|
|
178
|
-
const size = scaleSize ? Math.round(adjustedSize * scaleSize) : adjustedSize;
|
|
179
|
-
const labelId = useLabelContext(button);
|
|
180
|
-
const labelledBy = ariaLabelledby || labelId;
|
|
181
|
-
if (process.env.TAMAGUI_TARGET === "native") {
|
|
182
|
-
React.useEffect(() => {
|
|
183
|
-
if (!props.id)
|
|
184
|
-
return;
|
|
185
|
-
return registerFocusable(props.id, {
|
|
186
|
-
focusAndSelect: () => {
|
|
187
|
-
setChecked((x) => !x);
|
|
188
|
-
},
|
|
189
|
-
focus: () => {
|
|
190
|
-
}
|
|
191
|
-
});
|
|
192
|
-
}, [props.id, setChecked]);
|
|
193
|
-
}
|
|
194
|
-
return <CheckboxProvider
|
|
195
|
-
scope={__scopeCheckbox}
|
|
196
|
-
state={checked}
|
|
197
|
-
disabled={disabled}
|
|
198
|
-
size={size}
|
|
199
|
-
scaleIcon={scaleIcon}
|
|
200
|
-
>{isWeb && native ? <BubbleInput
|
|
201
|
-
control={button}
|
|
202
|
-
bubbles={!hasConsumerStoppedPropagationRef.current}
|
|
203
|
-
name={name}
|
|
204
|
-
value={value}
|
|
205
|
-
checked={checked}
|
|
206
|
-
required={required}
|
|
207
|
-
disabled={disabled}
|
|
208
|
-
id={props.id}
|
|
209
|
-
/> : <>
|
|
210
|
-
<CheckboxFrame
|
|
211
|
-
width={size}
|
|
212
|
-
height={size}
|
|
213
|
-
tag="button"
|
|
214
|
-
role="checkbox"
|
|
215
|
-
aria-labelledby={labelledBy}
|
|
216
|
-
aria-checked={isIndeterminate(checked) ? "mixed" : checked}
|
|
217
|
-
aria-required={required}
|
|
218
|
-
data-state={getState(checked)}
|
|
219
|
-
data-disabled={disabled ? "" : void 0}
|
|
220
|
-
disabled={disabled}
|
|
221
|
-
{...checkboxProps}
|
|
222
|
-
ref={composedRefs}
|
|
223
|
-
{...isWeb && {
|
|
224
|
-
type: "button",
|
|
225
|
-
value,
|
|
226
|
-
onKeyDown: composeEventHandlers(
|
|
227
|
-
props.onKeyDown,
|
|
228
|
-
(event) => {
|
|
229
|
-
if (event.key === "Enter")
|
|
230
|
-
event.preventDefault();
|
|
231
|
-
}
|
|
232
|
-
)
|
|
233
|
-
}}
|
|
234
|
-
onPress={composeEventHandlers(props.onPress, (event) => {
|
|
235
|
-
setChecked(
|
|
236
|
-
(prevChecked) => isIndeterminate(prevChecked) ? true : !prevChecked
|
|
237
|
-
);
|
|
238
|
-
if (isFormControl) {
|
|
239
|
-
hasConsumerStoppedPropagationRef.current = event.isPropagationStopped();
|
|
240
|
-
if (!hasConsumerStoppedPropagationRef.current)
|
|
241
|
-
event.stopPropagation();
|
|
242
|
-
}
|
|
243
|
-
})}
|
|
244
|
-
/>
|
|
245
|
-
{isWeb && isFormControl ? <BubbleInput
|
|
246
|
-
isHidden
|
|
247
|
-
control={button}
|
|
248
|
-
bubbles={!hasConsumerStoppedPropagationRef.current}
|
|
249
|
-
name={name}
|
|
250
|
-
value={value}
|
|
251
|
-
checked={checked}
|
|
252
|
-
required={required}
|
|
253
|
-
disabled={disabled}
|
|
254
|
-
/> : null}
|
|
255
|
-
</>}</CheckboxProvider>;
|
|
256
|
-
}
|
|
257
|
-
)
|
|
258
|
-
),
|
|
259
|
-
{
|
|
260
|
-
Indicator: CheckboxIndicator
|
|
261
|
-
}
|
|
262
|
-
);
|
|
263
|
-
Checkbox.displayName = CHECKBOX_NAME;
|
|
264
|
-
export {
|
|
265
|
-
BubbleInput,
|
|
266
|
-
Checkbox,
|
|
267
|
-
CheckboxFrame,
|
|
268
|
-
createCheckboxScope,
|
|
269
|
-
getState,
|
|
270
|
-
isIndeterminate
|
|
271
|
-
};
|
|
1
|
+
import{usePrevious as N}from"@radix-ui/react-use-previous";import{composeEventHandlers as E,getVariableValue as M,isWeb as P,styled as F,useComposedRefs as $,useMediaPropsActive as K,useTheme as U,withStaticProperties as W}from"@tamagui/core";import{createContextScope as X}from"@tamagui/create-context";import{registerFocusable as J}from"@tamagui/focusable";import{getFontSize as Q}from"@tamagui/font-size";import{getSize as Y,stepTokenUpOrDown as Z}from"@tamagui/get-size";import{useGetThemedIcon as ee}from"@tamagui/helpers-tamagui";import{useLabelContext as te}from"@tamagui/label";import{ThemeableStack as H}from"@tamagui/stacks";import{useControllableState as oe}from"@tamagui/use-controllable-state";import*as c from"react";function l(e){return e==="indeterminate"}function A(e){return l(e)?"indeterminate":e?"checked":"unchecked"}const B=e=>{const{checked:o,bubbles:s=!0,control:C,isHidden:a,...h}=e,u=c.useRef(null),t=N(o);return c.useEffect(()=>{const i=u.current,b=window.HTMLInputElement.prototype,r=Object.getOwnPropertyDescriptor(b,"checked").set;if(t!==o&&r){const d=new Event("click",{bubbles:s});i.indeterminate=l(o),r.call(i,l(o)?!1:o),i.dispatchEvent(d)}},[t,o,s]),<input type="checkbox"defaultChecked={l(o)?!1:o}{...h}tabIndex={-1}ref={u}aria-hidden={a}style={{...a?{position:"absolute",pointerEvents:"none",opacity:0,margin:0}:{appearance:"auto",accentColor:"var(--color6)"},...e.style}}/>},g="CheckboxIndicator",R=F(H,{name:g}),w=R.extractable(c.forwardRef((e,o)=>{const{__scopeCheckbox:s,children:C,forceMount:a,disablePassStyles:h,...u}=e,t=fe(g,s),i=(typeof t.size=="number"?t.size*.65:Q(t.size))*t.scaleIcon,b=U(),f=ee({size:i,color:b.color}),d=c.Children.toArray(C).map(m=>h||!c.isValidElement(m)?m:f(m));return a||l(t.state)||t.state===!0?<R data-state={A(t.state)}data-disabled={t.disabled?"":void 0}pointerEvents="none"{...u}ref={o}>{d}</R>:null}));w.displayName=g;const y="Checkbox",L=F(H,{name:y,tag:"button",variants:{unstyled:{false:{size:"$true",backgroundColor:"$background",alignItems:"center",justifyContent:"center",pressTheme:!0,focusable:!0,borderWidth:1,borderColor:"$borderColor",hoverStyle:{borderColor:"$borderColorHover"},focusStyle:{borderColor:"$borderColorFocus"}}},size:{"...size":(e,{tokens:o})=>({borderRadius:M(Y(e))/8})}},defaultVariants:{unstyled:!1}}),[re,ne]=X(y),[Ce,fe]=re(y),xe=W(L.extractable(c.forwardRef((e,o)=>{const{__scopeCheckbox:s,labelledBy:C,name:a,checked:h,defaultChecked:u,required:t,scaleIcon:i=1,scaleSize:b=.45,sizeAdjust:f=0,disabled:r,value:d="on",onCheckedChange:m,native:_,...D}=e,[k,O]=c.useState(null),G=$(o,n=>O(n)),x=c.useRef(!1),V=K(e),v=P?k?Boolean(k.closest("form")):!0:!1,[p=!1,S]=oe({prop:h,defaultProp:u,onChange:m}),T=M(Z("size",V.size,f)),I=b?Math.round(T*b):T,j=te(k),q=C||j;return process.env.TAMAGUI_TARGET==="native"&&c.useEffect(()=>{if(e.id)return J(e.id,{focusAndSelect:()=>{S(n=>!n)},focus:()=>{}})},[e.id,S]),<Ce scope={s}state={p}disabled={r}size={I}scaleIcon={i}>{P&&_?<B control={k}bubbles={!x.current}name={a}value={d}checked={p}required={t}disabled={r}id={e.id}/>:<><L width={I}height={I}tag="button"role="checkbox"aria-labelledby={q}aria-checked={l(p)?"mixed":p}aria-required={t}data-state={A(p)}data-disabled={r?"":void 0}disabled={r}{...D}ref={G}{...P&&{type:"button",value:d,onKeyDown:E(e.onKeyDown,n=>{n.key==="Enter"&&n.preventDefault()})}}onPress={E(e.onPress,n=>{S(z=>l(z)?!0:!z),v&&(x.current=n.isPropagationStopped(),x.current||n.stopPropagation())})}/>{P&&v?<B isHidden control={k}bubbles={!x.current}name={a}value={d}checked={p}required={t}disabled={r}/>:null}</>}</Ce>})),{Indicator:w});xe.displayName=y;export{B as BubbleInput,xe as Checkbox,L as CheckboxFrame,ne as createCheckboxScope,A as getState,l as isIndeterminate};
|
|
272
2
|
//# sourceMappingURL=Checkbox.js.map
|
package/dist/jsx/Checkbox.js.map
CHANGED
|
@@ -2,6 +2,6 @@
|
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/Checkbox.tsx"],
|
|
4
4
|
"sourcesContent": ["// fork of radix\n// https://github.com/radix-ui/primitives/tree/main/packages/react/checkbox/src/Checkbox.tsx\n\nimport { usePrevious } from '@radix-ui/react-use-previous'\nimport {\n GetProps,\n SizeTokens,\n TamaguiElement,\n composeEventHandlers,\n getVariableValue,\n isWeb,\n styled,\n useComposedRefs,\n useMediaPropsActive,\n useTheme,\n withStaticProperties,\n} from '@tamagui/core'\nimport type { Scope } from '@tamagui/create-context'\nimport { createContextScope } from '@tamagui/create-context'\nimport { registerFocusable } from '@tamagui/focusable'\nimport { getFontSize } from '@tamagui/font-size'\nimport { getSize, stepTokenUpOrDown } from '@tamagui/get-size'\nimport { useGetThemedIcon } from '@tamagui/helpers-tamagui'\nimport { useLabelContext } from '@tamagui/label'\nimport { ThemeableStack } from '@tamagui/stacks'\nimport { useControllableState } from '@tamagui/use-controllable-state'\nimport * as React from 'react'\n\nexport type CheckedState = boolean | 'indeterminate'\n\nexport function isIndeterminate(checked?: CheckedState): checked is 'indeterminate' {\n return checked === 'indeterminate'\n}\n\nexport function getState(checked: CheckedState) {\n return isIndeterminate(checked) ? 'indeterminate' : checked ? 'checked' : 'unchecked'\n}\n\ntype InputProps = any //Radix.ComponentPropsWithoutRef<'input'>\ninterface BubbleInputProps extends Omit<InputProps, 'checked'> {\n checked: CheckedState\n control: HTMLElement | null\n bubbles: boolean\n\n isHidden?: boolean\n}\n\nexport const BubbleInput = (props: BubbleInputProps) => {\n const { checked, bubbles = true, control, isHidden, ...inputProps } = props\n const ref = React.useRef<HTMLInputElement>(null)\n const prevChecked = usePrevious(checked)\n // const controlSize = useSize(control)\n\n // Bubble checked change to parents (e.g form change event)\n React.useEffect(() => {\n const input = ref.current!\n const inputProto = window.HTMLInputElement.prototype\n const descriptor = Object.getOwnPropertyDescriptor(\n inputProto,\n 'checked'\n ) as PropertyDescriptor\n const setChecked = descriptor.set\n\n if (prevChecked !== checked && setChecked) {\n const event = new Event('click', { bubbles })\n input.indeterminate = isIndeterminate(checked)\n setChecked.call(input, isIndeterminate(checked) ? false : checked)\n input.dispatchEvent(event)\n }\n }, [prevChecked, checked, bubbles])\n\n return (\n <input\n type=\"checkbox\"\n defaultChecked={isIndeterminate(checked) ? false : checked}\n {...inputProps}\n tabIndex={-1}\n ref={ref}\n aria-hidden={isHidden}\n style={{\n ...(isHidden\n ? {\n // ...controlSize,\n position: 'absolute',\n pointerEvents: 'none',\n opacity: 0,\n margin: 0,\n }\n : {\n appearance: 'auto',\n accentColor: 'var(--color6)',\n }),\n\n ...props.style,\n }}\n />\n )\n}\n\n/* -------------------------------------------------------------------------------------------------\n * CheckboxIndicator\n * -----------------------------------------------------------------------------------------------*/\n\nconst INDICATOR_NAME = 'CheckboxIndicator'\n\nconst CheckboxIndicatorFrame = styled(ThemeableStack, {\n // use Checkbox for easier themes\n name: INDICATOR_NAME,\n})\n\ntype CheckboxIndicatorFrameProps = GetProps<typeof CheckboxIndicatorFrame>\n\nexport type CheckboxIndicatorProps = CheckboxIndicatorFrameProps & {\n /**\n * Used to force mounting when more control is needed. Useful when\n * controlling animation with React animation libraries.\n */\n forceMount?: true\n /**\n * Used to disable passing styles down to children.\n */\n disablePassStyles?: boolean\n}\n\nconst CheckboxIndicator = CheckboxIndicatorFrame.extractable(\n React.forwardRef<TamaguiElement, CheckboxIndicatorProps>(\n (props: ScopedProps<CheckboxIndicatorProps>, forwardedRef) => {\n const {\n __scopeCheckbox,\n children: childrenProp,\n forceMount,\n disablePassStyles,\n ...indicatorProps\n } = props\n const context = useCheckboxContext(INDICATOR_NAME, __scopeCheckbox)\n const iconSize =\n (typeof context.size === 'number'\n ? context.size * 0.65\n : getFontSize(context.size)) * context.scaleIcon\n const theme = useTheme()\n const getThemedIcon = useGetThemedIcon({ size: iconSize, color: theme.color })\n\n const childrens = React.Children.toArray(childrenProp)\n const children = childrens.map((child) => {\n if (disablePassStyles || !React.isValidElement(child)) {\n return child\n }\n return getThemedIcon(child)\n })\n\n if (forceMount || isIndeterminate(context.state) || context.state === true)\n return (\n <CheckboxIndicatorFrame\n data-state={getState(context.state)}\n data-disabled={context.disabled ? '' : undefined}\n pointerEvents=\"none\"\n {...indicatorProps}\n ref={forwardedRef}\n >\n {children}\n </CheckboxIndicatorFrame>\n )\n\n return null\n }\n )\n)\n\nCheckboxIndicator.displayName = INDICATOR_NAME\n\n/* -------------------------------------------------------------------------------------------------\n * Checkbox\n * -----------------------------------------------------------------------------------------------*/\n\nconst CHECKBOX_NAME = 'Checkbox'\n\nexport const CheckboxFrame = styled(ThemeableStack, {\n name: CHECKBOX_NAME,\n tag: 'button',\n\n variants: {\n unstyled: {\n false: {\n size: '$true',\n backgroundColor: '$background',\n alignItems: 'center',\n justifyContent: 'center',\n pressTheme: true,\n focusable: true,\n borderWidth: 1,\n borderColor: '$borderColor',\n\n hoverStyle: {\n borderColor: '$borderColorHover',\n },\n\n focusStyle: {\n borderColor: '$borderColorFocus',\n },\n },\n },\n\n size: {\n '...size': (val, { tokens }) => {\n const radiusToken = getVariableValue(getSize(val)) / 8\n return {\n borderRadius: radiusToken,\n }\n },\n },\n } as const,\n\n defaultVariants: {\n unstyled: false,\n },\n})\n\ntype ScopedProps<P> = P & { __scopeCheckbox?: Scope }\nconst [createCheckboxContext, createCheckboxScope] = createContextScope(CHECKBOX_NAME)\n\ntype CheckboxContextValue = {\n state: CheckedState\n disabled?: boolean\n size: SizeTokens\n scaleIcon: number\n}\n\nconst [CheckboxProvider, useCheckboxContext] =\n createCheckboxContext<CheckboxContextValue>(CHECKBOX_NAME)\n\ntype CheckboxFrameProps = GetProps<typeof CheckboxFrame>\nexport interface CheckboxProps\n extends Omit<CheckboxFrameProps, 'checked' | 'defaultChecked'> {\n checked?: CheckedState\n defaultChecked?: CheckedState\n required?: boolean\n onCheckedChange?(checked: CheckedState): void\n labelledBy?: string\n name?: string\n value?: string\n native?: boolean\n scaleIcon?: number\n scaleSize?: number\n sizeAdjust?: number\n}\n\nexport const Checkbox = withStaticProperties(\n CheckboxFrame.extractable(\n React.forwardRef<HTMLButtonElement, CheckboxProps>(\n (props: ScopedProps<CheckboxProps>, forwardedRef) => {\n const {\n __scopeCheckbox,\n labelledBy: ariaLabelledby,\n name,\n checked: checkedProp,\n defaultChecked,\n required,\n scaleIcon = 1,\n scaleSize = 0.45,\n sizeAdjust = 0,\n disabled,\n value = 'on',\n onCheckedChange,\n native,\n ...checkboxProps\n } = props\n const [button, setButton] = React.useState<HTMLButtonElement | null>(null)\n const composedRefs = useComposedRefs(forwardedRef, (node) => setButton(node))\n const hasConsumerStoppedPropagationRef = React.useRef(false)\n const propsActive = useMediaPropsActive(props)\n // We set this to true by default so that events bubble to forms without JS (SSR)\n const isFormControl = isWeb\n ? button\n ? Boolean(button.closest('form'))\n : true\n : false\n const [checked = false, setChecked] = useControllableState({\n prop: checkedProp,\n defaultProp: defaultChecked!,\n onChange: onCheckedChange,\n })\n\n const adjustedSize = getVariableValue(\n stepTokenUpOrDown('size', propsActive.size, sizeAdjust)\n )\n const size = scaleSize ? Math.round(adjustedSize * scaleSize) : adjustedSize\n\n const labelId = useLabelContext(button)\n const labelledBy = ariaLabelledby || labelId\n\n if (process.env.TAMAGUI_TARGET === 'native') {\n // eslint-disable-next-line react-hooks/rules-of-hooks\n React.useEffect(() => {\n if (!props.id) return\n return registerFocusable(props.id, {\n focusAndSelect: () => {\n setChecked((x) => !x)\n },\n focus: () => {},\n })\n }, [props.id, setChecked])\n }\n\n return (\n <CheckboxProvider\n scope={__scopeCheckbox}\n state={checked}\n disabled={disabled}\n size={size}\n scaleIcon={scaleIcon}\n >\n {isWeb && native ? (\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 id={props.id}\n />\n ) : (\n <>\n <CheckboxFrame\n width={size}\n height={size}\n tag=\"button\"\n role=\"checkbox\"\n aria-labelledby={labelledBy}\n aria-checked={isIndeterminate(checked) ? 'mixed' : checked}\n aria-required={required}\n data-state={getState(checked)}\n data-disabled={disabled ? '' : undefined}\n disabled={disabled}\n {...checkboxProps}\n ref={composedRefs}\n {...(isWeb && {\n type: 'button',\n value,\n onKeyDown: composeEventHandlers(\n (props as React.HTMLProps<HTMLButtonElement>).onKeyDown,\n (event) => {\n // According to WAI ARIA, Checkboxes don't activate on enter keypress\n if (event.key === 'Enter') event.preventDefault()\n }\n ),\n })}\n onPress={composeEventHandlers(props.onPress as any, (event) => {\n setChecked((prevChecked) =>\n isIndeterminate(prevChecked) ? true : !prevChecked\n )\n if (isFormControl) {\n hasConsumerStoppedPropagationRef.current =\n event.isPropagationStopped()\n // if checkbox 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 checkbox updates.\n if (!hasConsumerStoppedPropagationRef.current)\n event.stopPropagation()\n }\n })}\n />\n\n {isWeb && isFormControl ? (\n <BubbleInput\n isHidden\n control={button}\n bubbles={!hasConsumerStoppedPropagationRef.current}\n name={name}\n value={value}\n checked={checked}\n required={required}\n disabled={disabled}\n />\n ) : null}\n </>\n )}\n </CheckboxProvider>\n )\n }\n )\n ),\n {\n Indicator: CheckboxIndicator,\n }\n)\n\nCheckbox.displayName = CHECKBOX_NAME\n\nexport { createCheckboxScope }\n"],
|
|
5
|
-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EAEAqB,GAAS,YAAcP",
|
|
6
|
+
"names": ["usePrevious", "composeEventHandlers", "getVariableValue", "isWeb", "styled", "useComposedRefs", "useMediaPropsActive", "useTheme", "withStaticProperties", "createContextScope", "registerFocusable", "getFontSize", "getSize", "stepTokenUpOrDown", "useGetThemedIcon", "useLabelContext", "ThemeableStack", "useControllableState", "React", "isIndeterminate", "checked", "getState", "BubbleInput", "props", "bubbles", "control", "isHidden", "inputProps", "ref", "prevChecked", "input", "inputProto", "setChecked", "event", "INDICATOR_NAME", "CheckboxIndicatorFrame", "CheckboxIndicator", "forwardedRef", "__scopeCheckbox", "childrenProp", "forceMount", "disablePassStyles", "indicatorProps", "context", "useCheckboxContext", "iconSize", "theme", "getThemedIcon", "children", "child", "CHECKBOX_NAME", "CheckboxFrame", "val", "tokens", "createCheckboxContext", "createCheckboxScope", "CheckboxProvider", "Checkbox", "ariaLabelledby", "name", "checkedProp", "defaultChecked", "required", "scaleIcon", "scaleSize", "sizeAdjust", "disabled", "value", "onCheckedChange", "native", "checkboxProps", "button", "setButton", "composedRefs", "node", "hasConsumerStoppedPropagationRef", "propsActive", "isFormControl", "adjustedSize", "size", "labelId", "labelledBy", "x"]
|
|
7
7
|
}
|
package/dist/jsx/Checkbox.mjs
CHANGED
|
@@ -1,272 +1,2 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
composeEventHandlers,
|
|
4
|
-
getVariableValue,
|
|
5
|
-
isWeb,
|
|
6
|
-
styled,
|
|
7
|
-
useComposedRefs,
|
|
8
|
-
useMediaPropsActive,
|
|
9
|
-
useTheme,
|
|
10
|
-
withStaticProperties
|
|
11
|
-
} from "@tamagui/core";
|
|
12
|
-
import { createContextScope } from "@tamagui/create-context";
|
|
13
|
-
import { registerFocusable } from "@tamagui/focusable";
|
|
14
|
-
import { getFontSize } from "@tamagui/font-size";
|
|
15
|
-
import { getSize, stepTokenUpOrDown } from "@tamagui/get-size";
|
|
16
|
-
import { useGetThemedIcon } from "@tamagui/helpers-tamagui";
|
|
17
|
-
import { useLabelContext } from "@tamagui/label";
|
|
18
|
-
import { ThemeableStack } from "@tamagui/stacks";
|
|
19
|
-
import { useControllableState } from "@tamagui/use-controllable-state";
|
|
20
|
-
import * as React from "react";
|
|
21
|
-
function isIndeterminate(checked) {
|
|
22
|
-
return checked === "indeterminate";
|
|
23
|
-
}
|
|
24
|
-
function getState(checked) {
|
|
25
|
-
return isIndeterminate(checked) ? "indeterminate" : checked ? "checked" : "unchecked";
|
|
26
|
-
}
|
|
27
|
-
const BubbleInput = (props) => {
|
|
28
|
-
const { checked, bubbles = true, control, isHidden, ...inputProps } = props;
|
|
29
|
-
const ref = React.useRef(null);
|
|
30
|
-
const prevChecked = usePrevious(checked);
|
|
31
|
-
React.useEffect(() => {
|
|
32
|
-
const input = ref.current;
|
|
33
|
-
const inputProto = window.HTMLInputElement.prototype;
|
|
34
|
-
const descriptor = Object.getOwnPropertyDescriptor(
|
|
35
|
-
inputProto,
|
|
36
|
-
"checked"
|
|
37
|
-
);
|
|
38
|
-
const setChecked = descriptor.set;
|
|
39
|
-
if (prevChecked !== checked && setChecked) {
|
|
40
|
-
const event = new Event("click", { bubbles });
|
|
41
|
-
input.indeterminate = isIndeterminate(checked);
|
|
42
|
-
setChecked.call(input, isIndeterminate(checked) ? false : checked);
|
|
43
|
-
input.dispatchEvent(event);
|
|
44
|
-
}
|
|
45
|
-
}, [prevChecked, checked, bubbles]);
|
|
46
|
-
return <input
|
|
47
|
-
type="checkbox"
|
|
48
|
-
defaultChecked={isIndeterminate(checked) ? false : checked}
|
|
49
|
-
{...inputProps}
|
|
50
|
-
tabIndex={-1}
|
|
51
|
-
ref={ref}
|
|
52
|
-
aria-hidden={isHidden}
|
|
53
|
-
style={{
|
|
54
|
-
...isHidden ? {
|
|
55
|
-
// ...controlSize,
|
|
56
|
-
position: "absolute",
|
|
57
|
-
pointerEvents: "none",
|
|
58
|
-
opacity: 0,
|
|
59
|
-
margin: 0
|
|
60
|
-
} : {
|
|
61
|
-
appearance: "auto",
|
|
62
|
-
accentColor: "var(--color6)"
|
|
63
|
-
},
|
|
64
|
-
...props.style
|
|
65
|
-
}}
|
|
66
|
-
/>;
|
|
67
|
-
};
|
|
68
|
-
const INDICATOR_NAME = "CheckboxIndicator";
|
|
69
|
-
const CheckboxIndicatorFrame = styled(ThemeableStack, {
|
|
70
|
-
// use Checkbox for easier themes
|
|
71
|
-
name: INDICATOR_NAME
|
|
72
|
-
});
|
|
73
|
-
const CheckboxIndicator = CheckboxIndicatorFrame.extractable(
|
|
74
|
-
React.forwardRef(
|
|
75
|
-
(props, forwardedRef) => {
|
|
76
|
-
const {
|
|
77
|
-
__scopeCheckbox,
|
|
78
|
-
children: childrenProp,
|
|
79
|
-
forceMount,
|
|
80
|
-
disablePassStyles,
|
|
81
|
-
...indicatorProps
|
|
82
|
-
} = props;
|
|
83
|
-
const context = useCheckboxContext(INDICATOR_NAME, __scopeCheckbox);
|
|
84
|
-
const iconSize = (typeof context.size === "number" ? context.size * 0.65 : getFontSize(context.size)) * context.scaleIcon;
|
|
85
|
-
const theme = useTheme();
|
|
86
|
-
const getThemedIcon = useGetThemedIcon({ size: iconSize, color: theme.color });
|
|
87
|
-
const childrens = React.Children.toArray(childrenProp);
|
|
88
|
-
const children = childrens.map((child) => {
|
|
89
|
-
if (disablePassStyles || !React.isValidElement(child)) {
|
|
90
|
-
return child;
|
|
91
|
-
}
|
|
92
|
-
return getThemedIcon(child);
|
|
93
|
-
});
|
|
94
|
-
if (forceMount || isIndeterminate(context.state) || context.state === true)
|
|
95
|
-
return <CheckboxIndicatorFrame
|
|
96
|
-
data-state={getState(context.state)}
|
|
97
|
-
data-disabled={context.disabled ? "" : void 0}
|
|
98
|
-
pointerEvents="none"
|
|
99
|
-
{...indicatorProps}
|
|
100
|
-
ref={forwardedRef}
|
|
101
|
-
>{children}</CheckboxIndicatorFrame>;
|
|
102
|
-
return null;
|
|
103
|
-
}
|
|
104
|
-
)
|
|
105
|
-
);
|
|
106
|
-
CheckboxIndicator.displayName = INDICATOR_NAME;
|
|
107
|
-
const CHECKBOX_NAME = "Checkbox";
|
|
108
|
-
const CheckboxFrame = styled(ThemeableStack, {
|
|
109
|
-
name: CHECKBOX_NAME,
|
|
110
|
-
tag: "button",
|
|
111
|
-
variants: {
|
|
112
|
-
unstyled: {
|
|
113
|
-
false: {
|
|
114
|
-
size: "$true",
|
|
115
|
-
backgroundColor: "$background",
|
|
116
|
-
alignItems: "center",
|
|
117
|
-
justifyContent: "center",
|
|
118
|
-
pressTheme: true,
|
|
119
|
-
focusable: true,
|
|
120
|
-
borderWidth: 1,
|
|
121
|
-
borderColor: "$borderColor",
|
|
122
|
-
hoverStyle: {
|
|
123
|
-
borderColor: "$borderColorHover"
|
|
124
|
-
},
|
|
125
|
-
focusStyle: {
|
|
126
|
-
borderColor: "$borderColorFocus"
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
},
|
|
130
|
-
size: {
|
|
131
|
-
"...size": (val, { tokens }) => {
|
|
132
|
-
const radiusToken = getVariableValue(getSize(val)) / 8;
|
|
133
|
-
return {
|
|
134
|
-
borderRadius: radiusToken
|
|
135
|
-
};
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
},
|
|
139
|
-
defaultVariants: {
|
|
140
|
-
unstyled: false
|
|
141
|
-
}
|
|
142
|
-
});
|
|
143
|
-
const [createCheckboxContext, createCheckboxScope] = createContextScope(CHECKBOX_NAME);
|
|
144
|
-
const [CheckboxProvider, useCheckboxContext] = createCheckboxContext(CHECKBOX_NAME);
|
|
145
|
-
const Checkbox = withStaticProperties(
|
|
146
|
-
CheckboxFrame.extractable(
|
|
147
|
-
React.forwardRef(
|
|
148
|
-
(props, forwardedRef) => {
|
|
149
|
-
const {
|
|
150
|
-
__scopeCheckbox,
|
|
151
|
-
labelledBy: ariaLabelledby,
|
|
152
|
-
name,
|
|
153
|
-
checked: checkedProp,
|
|
154
|
-
defaultChecked,
|
|
155
|
-
required,
|
|
156
|
-
scaleIcon = 1,
|
|
157
|
-
scaleSize = 0.45,
|
|
158
|
-
sizeAdjust = 0,
|
|
159
|
-
disabled,
|
|
160
|
-
value = "on",
|
|
161
|
-
onCheckedChange,
|
|
162
|
-
native,
|
|
163
|
-
...checkboxProps
|
|
164
|
-
} = props;
|
|
165
|
-
const [button, setButton] = React.useState(null);
|
|
166
|
-
const composedRefs = useComposedRefs(forwardedRef, (node) => setButton(node));
|
|
167
|
-
const hasConsumerStoppedPropagationRef = React.useRef(false);
|
|
168
|
-
const propsActive = useMediaPropsActive(props);
|
|
169
|
-
const isFormControl = isWeb ? button ? Boolean(button.closest("form")) : true : false;
|
|
170
|
-
const [checked = false, setChecked] = useControllableState({
|
|
171
|
-
prop: checkedProp,
|
|
172
|
-
defaultProp: defaultChecked,
|
|
173
|
-
onChange: onCheckedChange
|
|
174
|
-
});
|
|
175
|
-
const adjustedSize = getVariableValue(
|
|
176
|
-
stepTokenUpOrDown("size", propsActive.size, sizeAdjust)
|
|
177
|
-
);
|
|
178
|
-
const size = scaleSize ? Math.round(adjustedSize * scaleSize) : adjustedSize;
|
|
179
|
-
const labelId = useLabelContext(button);
|
|
180
|
-
const labelledBy = ariaLabelledby || labelId;
|
|
181
|
-
if (process.env.TAMAGUI_TARGET === "native") {
|
|
182
|
-
React.useEffect(() => {
|
|
183
|
-
if (!props.id)
|
|
184
|
-
return;
|
|
185
|
-
return registerFocusable(props.id, {
|
|
186
|
-
focusAndSelect: () => {
|
|
187
|
-
setChecked((x) => !x);
|
|
188
|
-
},
|
|
189
|
-
focus: () => {
|
|
190
|
-
}
|
|
191
|
-
});
|
|
192
|
-
}, [props.id, setChecked]);
|
|
193
|
-
}
|
|
194
|
-
return <CheckboxProvider
|
|
195
|
-
scope={__scopeCheckbox}
|
|
196
|
-
state={checked}
|
|
197
|
-
disabled={disabled}
|
|
198
|
-
size={size}
|
|
199
|
-
scaleIcon={scaleIcon}
|
|
200
|
-
>{isWeb && native ? <BubbleInput
|
|
201
|
-
control={button}
|
|
202
|
-
bubbles={!hasConsumerStoppedPropagationRef.current}
|
|
203
|
-
name={name}
|
|
204
|
-
value={value}
|
|
205
|
-
checked={checked}
|
|
206
|
-
required={required}
|
|
207
|
-
disabled={disabled}
|
|
208
|
-
id={props.id}
|
|
209
|
-
/> : <>
|
|
210
|
-
<CheckboxFrame
|
|
211
|
-
width={size}
|
|
212
|
-
height={size}
|
|
213
|
-
tag="button"
|
|
214
|
-
role="checkbox"
|
|
215
|
-
aria-labelledby={labelledBy}
|
|
216
|
-
aria-checked={isIndeterminate(checked) ? "mixed" : checked}
|
|
217
|
-
aria-required={required}
|
|
218
|
-
data-state={getState(checked)}
|
|
219
|
-
data-disabled={disabled ? "" : void 0}
|
|
220
|
-
disabled={disabled}
|
|
221
|
-
{...checkboxProps}
|
|
222
|
-
ref={composedRefs}
|
|
223
|
-
{...isWeb && {
|
|
224
|
-
type: "button",
|
|
225
|
-
value,
|
|
226
|
-
onKeyDown: composeEventHandlers(
|
|
227
|
-
props.onKeyDown,
|
|
228
|
-
(event) => {
|
|
229
|
-
if (event.key === "Enter")
|
|
230
|
-
event.preventDefault();
|
|
231
|
-
}
|
|
232
|
-
)
|
|
233
|
-
}}
|
|
234
|
-
onPress={composeEventHandlers(props.onPress, (event) => {
|
|
235
|
-
setChecked(
|
|
236
|
-
(prevChecked) => isIndeterminate(prevChecked) ? true : !prevChecked
|
|
237
|
-
);
|
|
238
|
-
if (isFormControl) {
|
|
239
|
-
hasConsumerStoppedPropagationRef.current = event.isPropagationStopped();
|
|
240
|
-
if (!hasConsumerStoppedPropagationRef.current)
|
|
241
|
-
event.stopPropagation();
|
|
242
|
-
}
|
|
243
|
-
})}
|
|
244
|
-
/>
|
|
245
|
-
{isWeb && isFormControl ? <BubbleInput
|
|
246
|
-
isHidden
|
|
247
|
-
control={button}
|
|
248
|
-
bubbles={!hasConsumerStoppedPropagationRef.current}
|
|
249
|
-
name={name}
|
|
250
|
-
value={value}
|
|
251
|
-
checked={checked}
|
|
252
|
-
required={required}
|
|
253
|
-
disabled={disabled}
|
|
254
|
-
/> : null}
|
|
255
|
-
</>}</CheckboxProvider>;
|
|
256
|
-
}
|
|
257
|
-
)
|
|
258
|
-
),
|
|
259
|
-
{
|
|
260
|
-
Indicator: CheckboxIndicator
|
|
261
|
-
}
|
|
262
|
-
);
|
|
263
|
-
Checkbox.displayName = CHECKBOX_NAME;
|
|
264
|
-
export {
|
|
265
|
-
BubbleInput,
|
|
266
|
-
Checkbox,
|
|
267
|
-
CheckboxFrame,
|
|
268
|
-
createCheckboxScope,
|
|
269
|
-
getState,
|
|
270
|
-
isIndeterminate
|
|
271
|
-
};
|
|
1
|
+
import{usePrevious as N}from"@radix-ui/react-use-previous";import{composeEventHandlers as E,getVariableValue as M,isWeb as P,styled as F,useComposedRefs as $,useMediaPropsActive as K,useTheme as U,withStaticProperties as W}from"@tamagui/core";import{createContextScope as X}from"@tamagui/create-context";import{registerFocusable as J}from"@tamagui/focusable";import{getFontSize as Q}from"@tamagui/font-size";import{getSize as Y,stepTokenUpOrDown as Z}from"@tamagui/get-size";import{useGetThemedIcon as ee}from"@tamagui/helpers-tamagui";import{useLabelContext as te}from"@tamagui/label";import{ThemeableStack as H}from"@tamagui/stacks";import{useControllableState as oe}from"@tamagui/use-controllable-state";import*as c from"react";function l(e){return e==="indeterminate"}function A(e){return l(e)?"indeterminate":e?"checked":"unchecked"}const B=e=>{const{checked:o,bubbles:s=!0,control:C,isHidden:a,...h}=e,u=c.useRef(null),t=N(o);return c.useEffect(()=>{const i=u.current,b=window.HTMLInputElement.prototype,r=Object.getOwnPropertyDescriptor(b,"checked").set;if(t!==o&&r){const d=new Event("click",{bubbles:s});i.indeterminate=l(o),r.call(i,l(o)?!1:o),i.dispatchEvent(d)}},[t,o,s]),<input type="checkbox"defaultChecked={l(o)?!1:o}{...h}tabIndex={-1}ref={u}aria-hidden={a}style={{...a?{position:"absolute",pointerEvents:"none",opacity:0,margin:0}:{appearance:"auto",accentColor:"var(--color6)"},...e.style}}/>},g="CheckboxIndicator",R=F(H,{name:g}),w=R.extractable(c.forwardRef((e,o)=>{const{__scopeCheckbox:s,children:C,forceMount:a,disablePassStyles:h,...u}=e,t=fe(g,s),i=(typeof t.size=="number"?t.size*.65:Q(t.size))*t.scaleIcon,b=U(),f=ee({size:i,color:b.color}),d=c.Children.toArray(C).map(m=>h||!c.isValidElement(m)?m:f(m));return a||l(t.state)||t.state===!0?<R data-state={A(t.state)}data-disabled={t.disabled?"":void 0}pointerEvents="none"{...u}ref={o}>{d}</R>:null}));w.displayName=g;const y="Checkbox",L=F(H,{name:y,tag:"button",variants:{unstyled:{false:{size:"$true",backgroundColor:"$background",alignItems:"center",justifyContent:"center",pressTheme:!0,focusable:!0,borderWidth:1,borderColor:"$borderColor",hoverStyle:{borderColor:"$borderColorHover"},focusStyle:{borderColor:"$borderColorFocus"}}},size:{"...size":(e,{tokens:o})=>({borderRadius:M(Y(e))/8})}},defaultVariants:{unstyled:!1}}),[re,ne]=X(y),[Ce,fe]=re(y),xe=W(L.extractable(c.forwardRef((e,o)=>{const{__scopeCheckbox:s,labelledBy:C,name:a,checked:h,defaultChecked:u,required:t,scaleIcon:i=1,scaleSize:b=.45,sizeAdjust:f=0,disabled:r,value:d="on",onCheckedChange:m,native:_,...D}=e,[k,O]=c.useState(null),G=$(o,n=>O(n)),x=c.useRef(!1),V=K(e),v=P?k?Boolean(k.closest("form")):!0:!1,[p=!1,S]=oe({prop:h,defaultProp:u,onChange:m}),T=M(Z("size",V.size,f)),I=b?Math.round(T*b):T,j=te(k),q=C||j;return process.env.TAMAGUI_TARGET==="native"&&c.useEffect(()=>{if(e.id)return J(e.id,{focusAndSelect:()=>{S(n=>!n)},focus:()=>{}})},[e.id,S]),<Ce scope={s}state={p}disabled={r}size={I}scaleIcon={i}>{P&&_?<B control={k}bubbles={!x.current}name={a}value={d}checked={p}required={t}disabled={r}id={e.id}/>:<><L width={I}height={I}tag="button"role="checkbox"aria-labelledby={q}aria-checked={l(p)?"mixed":p}aria-required={t}data-state={A(p)}data-disabled={r?"":void 0}disabled={r}{...D}ref={G}{...P&&{type:"button",value:d,onKeyDown:E(e.onKeyDown,n=>{n.key==="Enter"&&n.preventDefault()})}}onPress={E(e.onPress,n=>{S(z=>l(z)?!0:!z),v&&(x.current=n.isPropagationStopped(),x.current||n.stopPropagation())})}/>{P&&v?<B isHidden control={k}bubbles={!x.current}name={a}value={d}checked={p}required={t}disabled={r}/>:null}</>}</Ce>})),{Indicator:w});xe.displayName=y;export{B as BubbleInput,xe as Checkbox,L as CheckboxFrame,ne as createCheckboxScope,A as getState,l as isIndeterminate};
|
|
272
2
|
//# sourceMappingURL=Checkbox.mjs.map
|