@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/src/Switch.tsx CHANGED
@@ -172,136 +172,133 @@ export type SwitchProps = SwitchButtonProps & {
172
172
  onCheckedChange?(checked: boolean): void
173
173
  }
174
174
 
175
- export const Switch = withStaticProperties(
176
- SwitchFrame.extractable(
177
- React.forwardRef<HTMLButtonElement | View, SwitchProps>(
178
- (props: ScopedProps<SwitchProps, 'Switch'>, forwardedRef) => {
179
- const {
180
- __scopeSwitch,
181
- labeledBy: ariaLabelledby,
182
- name,
183
- checked: checkedProp,
184
- defaultChecked,
185
- required,
186
- disabled,
187
- value = 'on',
188
- onCheckedChange,
189
- size = '$true',
190
- unstyled = false,
191
- native: nativeProp,
192
- nativeProps,
193
- ...switchProps
194
- } = props
195
- const native = Array.isArray(nativeProp) ? nativeProp : [nativeProp]
196
- const shouldRenderMobileNative =
197
- (!isWeb && nativeProp === true) ||
198
- native.includes('mobile') ||
199
- (native.includes('android') && Platform.OS === 'android') ||
200
- (native.includes('ios') && Platform.OS === 'ios')
201
- if (shouldRenderMobileNative) {
202
- return (
203
- <NativeSwitch
204
- value={checkedProp}
205
- onValueChange={onCheckedChange}
206
- {...nativeProps}
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
- const labelId = useLabelContext(button)
215
- const labelledBy = ariaLabelledby || labelId
216
- const hasConsumerStoppedPropagationRef = React.useRef(false)
217
- // We set this to true by default so that events bubble to forms without JS (SSR)
218
- const isFormControl = isWeb
219
- ? button
220
- ? Boolean(button.closest('form'))
221
- : true
222
- : false
223
- const [checked = false, setChecked] = useControllableState({
224
- prop: checkedProp,
225
- defaultProp: defaultChecked || false,
226
- onChange: onCheckedChange,
227
- transition: true,
228
- })
229
-
230
- if (!isWeb) {
231
- // eslint-disable-next-line react-hooks/rules-of-hooks
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
- return (
243
- <SwitchProvider
244
- scope={__scopeSwitch}
245
- checked={checked}
246
- disabled={disabled}
247
- size={size}
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
- <SwitchFrame
251
- unstyled={unstyled}
252
- size={size}
253
- theme={checked ? 'active' : null}
254
- // @ts-ignore
255
- role="switch"
256
- aria-checked={checked}
257
- aria-labelledby={labelledBy}
258
- aria-required={required}
259
- data-state={getState(checked)}
260
- data-disabled={disabled ? '' : undefined}
261
- disabled={disabled}
262
- // @ts-ignore
263
- tabIndex={disabled ? undefined : 0}
264
- // @ts-ignore
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
- {...switchProps}
267
- ref={composedRefs}
268
- onPress={(event) => {
269
- props.onPress?.(event)
270
- setChecked((prevChecked) => !prevChecked)
271
- if (isWeb && isFormControl) {
272
- hasConsumerStoppedPropagationRef.current = event.isPropagationStopped()
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
- {isWeb && isFormControl && (
281
- <BubbleInput
282
- control={button}
283
- bubbles={!hasConsumerStoppedPropagationRef.current}
284
- name={name}
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'>
@@ -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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