@tamagui/switch 1.82.1 → 1.82.3

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.
@@ -123,155 +123,157 @@ export function createSwitch<F extends SwitchComponent, T extends SwitchThumbCom
123
123
  )
124
124
  })
125
125
 
126
- const SwitchComponent = Frame.styleable<SwitchExtraProps>(function SwitchFrame(
127
- propsIn,
128
- forwardedRef
129
- ) {
130
- const styledContext = React.useContext(SwitchContext)
131
- const props = useProps(propsIn, {
132
- noNormalize: true,
133
- noExpand: true,
134
- resolveValues: 'none',
135
- forComponent: Frame,
136
- })
137
- const {
138
- labeledBy: ariaLabelledby,
139
- name,
140
- checked: checkedProp,
141
- defaultChecked,
142
- required,
143
- disabled,
144
- value = 'on',
145
- onCheckedChange,
146
- size = styledContext.size ?? '$true',
147
- unstyled = styledContext.unstyled ?? false,
148
- native: nativeProp,
149
- nativeProps,
150
- children,
151
- ...switchProps
152
- } = props
126
+ const SwitchComponent = Frame.styleable<SwitchExtraProps>(
127
+ function SwitchFrame(propsIn, forwardedRef) {
128
+ const styledContext = React.useContext(SwitchContext)
129
+ const props = useProps(propsIn, {
130
+ noNormalize: true,
131
+ noExpand: true,
132
+ resolveValues: 'none',
133
+ forComponent: Frame,
134
+ })
135
+ const {
136
+ labeledBy: ariaLabelledby,
137
+ name,
138
+ checked: checkedProp,
139
+ defaultChecked,
140
+ required,
141
+ disabled,
142
+ value = 'on',
143
+ onCheckedChange,
144
+ size = styledContext.size ?? '$true',
145
+ unstyled = styledContext.unstyled ?? false,
146
+ native: nativeProp,
147
+ nativeProps,
148
+ children,
149
+ ...switchProps
150
+ } = props
153
151
 
154
- const native = Array.isArray(nativeProp) ? nativeProp : [nativeProp]
152
+ const native = Array.isArray(nativeProp) ? nativeProp : [nativeProp]
155
153
 
156
- const shouldRenderMobileNative =
157
- (!isWeb && nativeProp === true) ||
158
- (!isWeb && native.includes('mobile')) ||
159
- (native.includes('android') && Platform.OS === 'android') ||
160
- (native.includes('ios') && Platform.OS === 'ios')
154
+ const shouldRenderMobileNative =
155
+ (!isWeb && nativeProp === true) ||
156
+ (!isWeb && native.includes('mobile')) ||
157
+ (native.includes('android') && Platform.OS === 'android') ||
158
+ (native.includes('ios') && Platform.OS === 'ios')
161
159
 
162
- const [button, setButton] = React.useState<HTMLButtonElement | null>(null)
163
- const composedRefs = useComposedRefs(forwardedRef, setButton)
164
- const labelId = useLabelContext(button)
165
- const labelledBy = ariaLabelledby || labelId
166
- const hasConsumerStoppedPropagationRef = React.useRef(false)
167
- // We set this to true by default so that events bubble to forms without JS (SSR)
168
- const isFormControl = isWeb
169
- ? button
170
- ? Boolean(button.closest('form'))
171
- : true
172
- : false
160
+ const [button, setButton] = React.useState<HTMLButtonElement | null>(null)
161
+ const composedRefs = useComposedRefs(forwardedRef, setButton)
162
+ const labelId = useLabelContext(button)
163
+ const labelledBy = ariaLabelledby || labelId
164
+ const hasConsumerStoppedPropagationRef = React.useRef(false)
165
+ // We set this to true by default so that events bubble to forms without JS (SSR)
166
+ const isFormControl = isWeb
167
+ ? button
168
+ ? Boolean(button.closest('form'))
169
+ : true
170
+ : false
173
171
 
174
- const [frameWidth, setFrameWidth] = React.useState(0)
172
+ const [frameWidth, setFrameWidth] = React.useState(0)
175
173
 
176
- const [checked = false, setChecked] = useControllableState({
177
- prop: checkedProp,
178
- defaultProp: defaultChecked || false,
179
- onChange: onCheckedChange,
180
- transition: true,
181
- })
174
+ const [checked = false, setChecked] = useControllableState({
175
+ prop: checkedProp,
176
+ defaultProp: defaultChecked || false,
177
+ onChange: onCheckedChange,
178
+ transition: true,
179
+ })
182
180
 
183
- if (shouldRenderMobileNative) {
184
- return (
185
- <NativeSwitch
186
- value={checkedProp}
187
- onValueChange={onCheckedChange}
188
- {...nativeProps}
189
- />
190
- )
191
- }
181
+ if (shouldRenderMobileNative) {
182
+ return (
183
+ <NativeSwitch
184
+ value={checkedProp}
185
+ onValueChange={onCheckedChange}
186
+ {...nativeProps}
187
+ />
188
+ )
189
+ }
192
190
 
193
- if (!isWeb) {
194
- // eslint-disable-next-line react-hooks/rules-of-hooks
195
- React.useEffect(() => {
196
- if (!props.id) return
197
- return registerFocusable(props.id, {
198
- focus: () => {
199
- setChecked((x) => !x)
200
- },
201
- })
202
- }, [props.id, setChecked])
203
- }
191
+ if (!isWeb) {
192
+ // eslint-disable-next-line react-hooks/rules-of-hooks
193
+ React.useEffect(() => {
194
+ if (!props.id) return
195
+ return registerFocusable(props.id, {
196
+ focus: () => {
197
+ setChecked((x) => !x)
198
+ },
199
+ })
200
+ }, [props.id, setChecked])
201
+ }
204
202
 
205
- const isInsideButton = React.useContext(ButtonNestingContext)
203
+ const isInsideButton = React.useContext(ButtonNestingContext)
206
204
 
207
- return (
208
- <>
209
- <ButtonNestingContext.Provider value={true}>
210
- {/* @ts-expect-error todo */}
211
- <Frame
212
- tag={isInsideButton ? 'span' : 'button'}
213
- unstyled={unstyled}
214
- size={size}
215
- checked={checked}
216
- disabled={disabled}
217
- frameWidth={frameWidth}
218
- themeShallow
219
- {...(!disableActiveTheme && {
220
- theme: checked ? 'active' : null,
221
- themeShallow: true,
222
- })}
223
- role="switch"
224
- aria-checked={checked}
225
- aria-labelledby={labelledBy}
226
- aria-required={required}
227
- data-state={getState(checked)}
228
- data-disabled={disabled ? '' : undefined}
229
- // @ts-ignore
230
- tabIndex={disabled ? undefined : 0}
231
- // @ts-ignore
232
- value={value}
233
- {...switchProps}
234
- ref={composedRefs}
235
- onPress={composeEventHandlers(props.onPress, (event) => {
236
- setChecked((prevChecked) => !prevChecked)
237
- if (isWeb && isFormControl) {
238
- hasConsumerStoppedPropagationRef.current = event.isPropagationStopped()
239
- // if switch is in a form, stop propagation from the button so that we only propagate
240
- // one click event (from the input). We propagate changes from an input so that native
241
- // form validation works and form events reflect switch updates.
242
- if (!hasConsumerStoppedPropagationRef.current) event.stopPropagation()
243
- }
244
- })}
245
- >
246
- <YStack
247
- alignSelf="stretch"
248
- flex={1}
249
- onLayout={(e) => {
250
- setFrameWidth(e.nativeEvent.layout.width)
251
- }}
205
+ return (
206
+ <>
207
+ <ButtonNestingContext.Provider value={true}>
208
+ {/* @ts-expect-error todo */}
209
+ <Frame
210
+ tag={isInsideButton ? 'span' : 'button'}
211
+ unstyled={unstyled}
212
+ size={size}
213
+ checked={checked}
214
+ disabled={disabled}
215
+ frameWidth={frameWidth}
216
+ themeShallow
217
+ {...(!disableActiveTheme && {
218
+ theme: checked ? 'active' : null,
219
+ themeShallow: true,
220
+ })}
221
+ role="switch"
222
+ aria-checked={checked}
223
+ aria-labelledby={labelledBy}
224
+ aria-required={required}
225
+ data-state={getState(checked)}
226
+ data-disabled={disabled ? '' : undefined}
227
+ // @ts-ignore
228
+ tabIndex={disabled ? undefined : 0}
229
+ // @ts-ignore
230
+ value={value}
231
+ {...switchProps}
232
+ ref={composedRefs}
233
+ onPress={composeEventHandlers(props.onPress, (event) => {
234
+ setChecked((prevChecked) => !prevChecked)
235
+ if (isWeb && isFormControl) {
236
+ hasConsumerStoppedPropagationRef.current = event.isPropagationStopped()
237
+ // if switch is in a form, stop propagation from the button so that we only propagate
238
+ // one click event (from the input). We propagate changes from an input so that native
239
+ // form validation works and form events reflect switch updates.
240
+ if (!hasConsumerStoppedPropagationRef.current) event.stopPropagation()
241
+ }
242
+ })}
252
243
  >
253
- {typeof children === 'function' ? children(checked) : children}
254
- </YStack>
255
- </Frame>
256
- </ButtonNestingContext.Provider>
257
- {isWeb && isFormControl && (
258
- <BubbleInput
259
- control={button}
260
- bubbles={!hasConsumerStoppedPropagationRef.current}
261
- name={name}
262
- value={value}
263
- checked={checked}
264
- required={required}
265
- disabled={disabled}
266
- // We transform because the input is absolutely positioned but we have
267
- // rendered it **after** the button. This pulls it back to sit on top
268
- // of the button.
269
- style={{ transform: 'translateX(-100%)' }}
270
- />
271
- )}
272
- </>
273
- )
274
- })
244
+ <YStack
245
+ alignSelf="stretch"
246
+ flex={1}
247
+ onLayout={(e) => {
248
+ setFrameWidth(e.nativeEvent.layout.width)
249
+ }}
250
+ >
251
+ {typeof children === 'function' ? children(checked) : children}
252
+ </YStack>
253
+ </Frame>
254
+ </ButtonNestingContext.Provider>
255
+ {isWeb && isFormControl && (
256
+ <BubbleInput
257
+ control={button}
258
+ bubbles={!hasConsumerStoppedPropagationRef.current}
259
+ name={name}
260
+ value={value}
261
+ checked={checked}
262
+ required={required}
263
+ disabled={disabled}
264
+ // We transform because the input is absolutely positioned but we have
265
+ // rendered it **after** the button. This pulls it back to sit on top
266
+ // of the button.
267
+ style={{ transform: 'translateX(-100%)' }}
268
+ />
269
+ )}
270
+ </>
271
+ )
272
+ },
273
+ {
274
+ disableTheme: true,
275
+ }
276
+ )
275
277
 
276
278
  /* ---------------------------------------------------------------------------------------------- */
277
279
 
@@ -1 +1 @@
1
- {"version":3,"file":"createSwitch.d.ts","sourceRoot":"","sources":["../src/createSwitch.tsx"],"names":[],"mappings":"AAEA,OAAO,EACL,WAAW,EACX,UAAU,EACV,UAAU,EACV,iCAAiC,EAElC,MAAM,eAAe,CAAA;AAOtB,OAAO,KAAK,KAAK,MAAM,OAAO,CAAA;AAC9B,OAAO,EAEL,WAAW,IAAI,iBAAiB,EAEjC,MAAM,cAAc,CAAA;AAKrB,KAAK,iBAAiB,GAAG;IACvB,IAAI,CAAC,EAAE,UAAU,GAAG,MAAM,CAAA;IAC1B,QAAQ,CAAC,EAAE,OAAO,CAAA;CACnB,CAAA;AAED,KAAK,eAAe,GAAG,UAAU,GAAG,iBAAiB,CAAA;AAErD,MAAM,MAAM,gBAAgB,GAAG;IAC7B,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,OAAO,CAAC,EAAE,OAAO,CAAA;IACjB,cAAc,CAAC,EAAE,OAAO,CAAA;IACxB,QAAQ,CAAC,EAAE,OAAO,CAAA;IAClB,MAAM,CAAC,EAAE,WAAW,CAAC,QAAQ,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