@tamagui/switch 1.30.0 → 1.30.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/Switch.js +109 -114
- package/dist/cjs/Switch.js.map +1 -1
- package/dist/esm/Switch.js +109 -114
- package/dist/esm/Switch.js.map +1 -1
- package/dist/jsx/Switch.js +97 -102
- package/dist/jsx/Switch.js.map +1 -1
- package/dist/jsx/Switch.mjs +97 -102
- package/dist/jsx/Switch.mjs.map +1 -1
- package/package.json +10 -10
- package/src/Switch.tsx +120 -123
- package/types/Switch.d.ts.map +1 -1
package/src/Switch.tsx
CHANGED
|
@@ -172,136 +172,133 @@ export type SwitchProps = SwitchButtonProps & {
|
|
|
172
172
|
onCheckedChange?(checked: boolean): void
|
|
173
173
|
}
|
|
174
174
|
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
/>
|
|
208
|
-
)
|
|
209
|
-
}
|
|
210
|
-
const [button, setButton] = React.useState<HTMLButtonElement | null>(null)
|
|
211
|
-
const composedRefs = useComposedRefs(forwardedRef, (node) =>
|
|
212
|
-
setButton(node as any)
|
|
175
|
+
const SwitchComponent = SwitchFrame.extractable(
|
|
176
|
+
React.forwardRef<HTMLButtonElement | View, SwitchProps>(
|
|
177
|
+
(props: ScopedProps<SwitchProps, 'Switch'>, forwardedRef) => {
|
|
178
|
+
const {
|
|
179
|
+
__scopeSwitch,
|
|
180
|
+
labeledBy: ariaLabelledby,
|
|
181
|
+
name,
|
|
182
|
+
checked: checkedProp,
|
|
183
|
+
defaultChecked,
|
|
184
|
+
required,
|
|
185
|
+
disabled,
|
|
186
|
+
value = 'on',
|
|
187
|
+
onCheckedChange,
|
|
188
|
+
size = '$true',
|
|
189
|
+
unstyled = false,
|
|
190
|
+
native: nativeProp,
|
|
191
|
+
nativeProps,
|
|
192
|
+
...switchProps
|
|
193
|
+
} = props
|
|
194
|
+
const native = Array.isArray(nativeProp) ? nativeProp : [nativeProp]
|
|
195
|
+
const shouldRenderMobileNative =
|
|
196
|
+
(!isWeb && nativeProp === true) ||
|
|
197
|
+
native.includes('mobile') ||
|
|
198
|
+
(native.includes('android') && Platform.OS === 'android') ||
|
|
199
|
+
(native.includes('ios') && Platform.OS === 'ios')
|
|
200
|
+
if (shouldRenderMobileNative) {
|
|
201
|
+
return (
|
|
202
|
+
<NativeSwitch
|
|
203
|
+
value={checkedProp}
|
|
204
|
+
onValueChange={onCheckedChange}
|
|
205
|
+
{...nativeProps}
|
|
206
|
+
/>
|
|
213
207
|
)
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
React.useEffect(() => {
|
|
233
|
-
if (!props.id) return
|
|
234
|
-
return registerFocusable(props.id, {
|
|
235
|
-
focus: () => {
|
|
236
|
-
setChecked((x) => !x)
|
|
237
|
-
},
|
|
238
|
-
})
|
|
239
|
-
}, [props.id, setChecked])
|
|
240
|
-
}
|
|
208
|
+
}
|
|
209
|
+
const [button, setButton] = React.useState<HTMLButtonElement | null>(null)
|
|
210
|
+
const composedRefs = useComposedRefs(forwardedRef, (node) => setButton(node as any))
|
|
211
|
+
const labelId = useLabelContext(button)
|
|
212
|
+
const labelledBy = ariaLabelledby || labelId
|
|
213
|
+
const hasConsumerStoppedPropagationRef = React.useRef(false)
|
|
214
|
+
// We set this to true by default so that events bubble to forms without JS (SSR)
|
|
215
|
+
const isFormControl = isWeb
|
|
216
|
+
? button
|
|
217
|
+
? Boolean(button.closest('form'))
|
|
218
|
+
: true
|
|
219
|
+
: false
|
|
220
|
+
const [checked = false, setChecked] = useControllableState({
|
|
221
|
+
prop: checkedProp,
|
|
222
|
+
defaultProp: defaultChecked || false,
|
|
223
|
+
onChange: onCheckedChange,
|
|
224
|
+
transition: true,
|
|
225
|
+
})
|
|
241
226
|
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
227
|
+
if (!isWeb) {
|
|
228
|
+
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
229
|
+
React.useEffect(() => {
|
|
230
|
+
if (!props.id) return
|
|
231
|
+
return registerFocusable(props.id, {
|
|
232
|
+
focus: () => {
|
|
233
|
+
setChecked((x) => !x)
|
|
234
|
+
},
|
|
235
|
+
})
|
|
236
|
+
}, [props.id, setChecked])
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
return (
|
|
240
|
+
<SwitchProvider
|
|
241
|
+
scope={__scopeSwitch}
|
|
242
|
+
checked={checked}
|
|
243
|
+
disabled={disabled}
|
|
244
|
+
size={size}
|
|
245
|
+
unstyled={unstyled}
|
|
246
|
+
>
|
|
247
|
+
<SwitchFrame
|
|
248
248
|
unstyled={unstyled}
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
249
|
+
size={size}
|
|
250
|
+
theme={checked ? 'active' : null}
|
|
251
|
+
// @ts-ignore
|
|
252
|
+
role="switch"
|
|
253
|
+
aria-checked={checked}
|
|
254
|
+
aria-labelledby={labelledBy}
|
|
255
|
+
aria-required={required}
|
|
256
|
+
data-state={getState(checked)}
|
|
257
|
+
data-disabled={disabled ? '' : undefined}
|
|
258
|
+
disabled={disabled}
|
|
259
|
+
// @ts-ignore
|
|
260
|
+
tabIndex={disabled ? undefined : 0}
|
|
261
|
+
// @ts-ignore
|
|
262
|
+
value={value}
|
|
263
|
+
{...switchProps}
|
|
264
|
+
ref={composedRefs}
|
|
265
|
+
onPress={(event) => {
|
|
266
|
+
props.onPress?.(event)
|
|
267
|
+
setChecked((prevChecked) => !prevChecked)
|
|
268
|
+
if (isWeb && isFormControl) {
|
|
269
|
+
hasConsumerStoppedPropagationRef.current = event.isPropagationStopped()
|
|
270
|
+
// if switch is in a form, stop propagation from the button so that we only propagate
|
|
271
|
+
// one click event (from the input). We propagate changes from an input so that native
|
|
272
|
+
// form validation works and form events reflect switch updates.
|
|
273
|
+
if (!hasConsumerStoppedPropagationRef.current) event.stopPropagation()
|
|
274
|
+
}
|
|
275
|
+
}}
|
|
276
|
+
/>
|
|
277
|
+
{isWeb && isFormControl && (
|
|
278
|
+
<BubbleInput
|
|
279
|
+
control={button}
|
|
280
|
+
bubbles={!hasConsumerStoppedPropagationRef.current}
|
|
281
|
+
name={name}
|
|
265
282
|
value={value}
|
|
266
|
-
{
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
// if switch is in a form, stop propagation from the button so that we only propagate
|
|
274
|
-
// one click event (from the input). We propagate changes from an input so that native
|
|
275
|
-
// form validation works and form events reflect switch updates.
|
|
276
|
-
if (!hasConsumerStoppedPropagationRef.current) event.stopPropagation()
|
|
277
|
-
}
|
|
278
|
-
}}
|
|
283
|
+
checked={checked}
|
|
284
|
+
required={required}
|
|
285
|
+
disabled={disabled}
|
|
286
|
+
// We transform because the input is absolutely positioned but we have
|
|
287
|
+
// rendered it **after** the button. This pulls it back to sit on top
|
|
288
|
+
// of the button.
|
|
289
|
+
style={{ transform: 'translateX(-100%)' }}
|
|
279
290
|
/>
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
value={value}
|
|
286
|
-
checked={checked}
|
|
287
|
-
required={required}
|
|
288
|
-
disabled={disabled}
|
|
289
|
-
// We transform because the input is absolutely positioned but we have
|
|
290
|
-
// rendered it **after** the button. This pulls it back to sit on top
|
|
291
|
-
// of the button.
|
|
292
|
-
style={{ transform: 'translateX(-100%)' }}
|
|
293
|
-
/>
|
|
294
|
-
)}
|
|
295
|
-
</SwitchProvider>
|
|
296
|
-
)
|
|
297
|
-
}
|
|
298
|
-
)
|
|
299
|
-
),
|
|
300
|
-
{
|
|
301
|
-
Thumb: SwitchThumb,
|
|
302
|
-
}
|
|
291
|
+
)}
|
|
292
|
+
</SwitchProvider>
|
|
293
|
+
)
|
|
294
|
+
}
|
|
295
|
+
)
|
|
303
296
|
)
|
|
304
297
|
|
|
298
|
+
export const Switch = withStaticProperties(SwitchComponent, {
|
|
299
|
+
Thumb: SwitchThumb,
|
|
300
|
+
})
|
|
301
|
+
|
|
305
302
|
/* ---------------------------------------------------------------------------------------------- */
|
|
306
303
|
|
|
307
304
|
type InputProps = any //Radix.ComponentPropsWithoutRef<'input'>
|
package/types/Switch.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Switch.d.ts","sourceRoot":"","sources":["../src/Switch.tsx"],"names":[],"mappings":"AAKA,OAAO,EACL,QAAQ,EACR,WAAW,EACX,UAAU,EAKX,MAAM,eAAe,CAAA;AAOtB,OAAO,KAAK,KAAK,MAAM,OAAO,CAAA;AAC9B,OAAO,EAEL,WAAW,IAAI,iBAAiB,EAEhC,IAAI,EACL,MAAM,cAAc,CAAA;AAWrB,eAAO,MAAM,iBAAiB,+CAAmB,CAAA;AAejD,eAAO,MAAM,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