@tamagui/switch 2.7.7 → 3.0.0-beta.643.1

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.
Files changed (97) hide show
  1. package/dist/cjs/StyledContext.cjs +19 -23
  2. package/dist/cjs/StyledContext.native.cjs +33 -0
  3. package/dist/cjs/StyledContext.native.js +19 -22
  4. package/dist/cjs/StyledContext.native.js.map +1 -1
  5. package/dist/cjs/Switch.cjs +140 -86
  6. package/dist/cjs/Switch.native.cjs +163 -0
  7. package/dist/cjs/Switch.native.js +147 -90
  8. package/dist/cjs/Switch.native.js.map +1 -1
  9. package/dist/cjs/index.cjs +14 -25
  10. package/dist/cjs/index.native.cjs +31 -0
  11. package/dist/cjs/index.native.js +15 -25
  12. package/dist/cjs/index.native.js.map +1 -1
  13. package/dist/cjs/types.cjs +10 -11
  14. package/dist/cjs/types.native.cjs +19 -0
  15. package/dist/cjs/types.native.js +10 -10
  16. package/dist/cjs/types.native.js.map +1 -1
  17. package/dist/cjs/useSwitchNative.cjs +16 -19
  18. package/dist/cjs/useSwitchNative.native.cjs +65 -0
  19. package/dist/cjs/useSwitchNative.native.js +44 -54
  20. package/dist/cjs/useSwitchNative.native.js.map +1 -1
  21. package/dist/esm/StyledContext.mjs +6 -5
  22. package/dist/esm/StyledContext.mjs.map +1 -1
  23. package/dist/esm/StyledContext.native.js +6 -5
  24. package/dist/esm/StyledContext.native.js.map +1 -1
  25. package/dist/esm/Switch.mjs +119 -71
  26. package/dist/esm/Switch.mjs.map +1 -1
  27. package/dist/esm/Switch.native.js +126 -76
  28. package/dist/esm/Switch.native.js.map +1 -1
  29. package/dist/esm/index.js +8 -12
  30. package/dist/esm/index.mjs +8 -12
  31. package/dist/esm/index.native.js +8 -12
  32. package/dist/esm/types.mjs +0 -2
  33. package/dist/esm/types.native.js +0 -2
  34. package/dist/esm/useSwitchNative.mjs +2 -1
  35. package/dist/esm/useSwitchNative.mjs.map +1 -1
  36. package/dist/esm/useSwitchNative.native.js +30 -31
  37. package/dist/esm/useSwitchNative.native.js.map +1 -1
  38. package/dist/jsx/StyledContext.mjs +6 -5
  39. package/dist/jsx/StyledContext.mjs.map +1 -1
  40. package/dist/jsx/StyledContext.native.cjs +33 -0
  41. package/dist/jsx/StyledContext.native.js +19 -22
  42. package/dist/jsx/StyledContext.native.js.map +1 -1
  43. package/dist/jsx/Switch.mjs +119 -71
  44. package/dist/jsx/Switch.mjs.map +1 -1
  45. package/dist/jsx/Switch.native.cjs +163 -0
  46. package/dist/jsx/Switch.native.js +147 -90
  47. package/dist/jsx/Switch.native.js.map +1 -1
  48. package/dist/jsx/index.js +8 -12
  49. package/dist/jsx/index.mjs +8 -12
  50. package/dist/jsx/index.native.cjs +31 -0
  51. package/dist/jsx/index.native.js +15 -25
  52. package/dist/jsx/index.native.js.map +1 -1
  53. package/dist/jsx/types.mjs +0 -2
  54. package/dist/jsx/types.native.cjs +19 -0
  55. package/dist/jsx/types.native.js +10 -10
  56. package/dist/jsx/types.native.js.map +1 -1
  57. package/dist/jsx/useSwitchNative.mjs +2 -1
  58. package/dist/jsx/useSwitchNative.mjs.map +1 -1
  59. package/dist/jsx/useSwitchNative.native.cjs +65 -0
  60. package/dist/jsx/useSwitchNative.native.js +44 -54
  61. package/dist/jsx/useSwitchNative.native.js.map +1 -1
  62. package/package.json +7 -14
  63. package/src/StyledContext.tsx +1 -3
  64. package/src/Switch.tsx +190 -89
  65. package/src/index.ts +0 -20
  66. package/src/types.ts +3 -5
  67. package/types/StyledContext.d.ts +2 -3
  68. package/types/StyledContext.d.ts.map +1 -1
  69. package/types/Switch.d.ts +209 -37
  70. package/types/Switch.d.ts.map +1 -1
  71. package/types/index.d.ts +0 -146
  72. package/types/index.d.ts.map +1 -1
  73. package/types/types.d.ts +3 -4
  74. package/types/types.d.ts.map +1 -1
  75. package/dist/cjs/createSwitch.cjs +0 -186
  76. package/dist/cjs/createSwitch.native.js +0 -194
  77. package/dist/cjs/createSwitch.native.js.map +0 -1
  78. package/dist/esm/createSwitch.mjs +0 -150
  79. package/dist/esm/createSwitch.mjs.map +0 -1
  80. package/dist/esm/createSwitch.native.js +0 -155
  81. package/dist/esm/createSwitch.native.js.map +0 -1
  82. package/dist/esm/index.js.map +0 -1
  83. package/dist/esm/index.mjs.map +0 -1
  84. package/dist/esm/index.native.js.map +0 -1
  85. package/dist/esm/types.mjs.map +0 -1
  86. package/dist/esm/types.native.js.map +0 -1
  87. package/dist/jsx/createSwitch.mjs +0 -150
  88. package/dist/jsx/createSwitch.mjs.map +0 -1
  89. package/dist/jsx/createSwitch.native.js +0 -194
  90. package/dist/jsx/createSwitch.native.js.map +0 -1
  91. package/dist/jsx/index.js.map +0 -1
  92. package/dist/jsx/index.mjs.map +0 -1
  93. package/dist/jsx/types.mjs.map +0 -1
  94. package/src/Switch.draggable-tmp +0 -340
  95. package/src/createSwitch.tsx +0 -201
  96. package/types/createSwitch.d.ts +0 -141
  97. package/types/createSwitch.d.ts.map +0 -1
package/src/Switch.tsx CHANGED
@@ -1,91 +1,192 @@
1
- import type { SizeTokens } from '@tamagui/core'
2
- import { getVariableValue, styled } from '@tamagui/core'
3
- import { getSize } from '@tamagui/get-token'
4
- import { YStack } from '@tamagui/stacks'
5
-
6
- export const SwitchThumb = styled(
7
- YStack,
8
- {
9
- name: 'SwitchThumb',
10
-
11
- variants: {
12
- unstyled: {
13
- false: {
14
- size: '$true',
15
- backgroundColor: '$background',
16
- borderRadius: 1000,
17
- },
18
- },
19
-
20
- size: {
21
- '...size': (val) => {
22
- const size = getSwitchHeight(val)
23
- return {
24
- height: size,
25
- width: size,
26
- }
27
- },
28
- },
29
- } as const,
30
-
31
- defaultVariants: {
32
- unstyled: process.env.TAMAGUI_HEADLESS === '1',
33
- },
34
- },
35
- {
36
- accept: {
37
- activeStyle: 'style',
38
- } as const,
1
+ import type { GetProps } from '@tamagui/core'
2
+ import {
3
+ composeEventHandlers,
4
+ createStyledHOC,
5
+ getVariableValue,
6
+ isWeb,
7
+ styled,
8
+ Theme,
9
+ type ThemeProps,
10
+ View,
11
+ withStaticProperties,
12
+ } from '@tamagui/core'
13
+ import { useSwitch } from '@tamagui/switch-headless'
14
+ import { useControllableState } from '@tamagui/use-controllable-state'
15
+ import * as React from 'react'
16
+ import type { LayoutChangeEvent } from 'react-native'
17
+ import { SwitchStyledContext } from './StyledContext'
18
+ import type {
19
+ SwitchComponent as SwitchFrameComponent,
20
+ SwitchProps,
21
+ SwitchThumbComponent as SwitchThumbFrameComponent,
22
+ SwitchThumbProps,
23
+ } from './types'
24
+ import { useSwitchNative } from './useSwitchNative'
25
+
26
+ export const SwitchThumbFrame = styled(View, {
27
+ displayName: 'SwitchThumb',
28
+ context: SwitchStyledContext,
29
+ })
30
+
31
+ export const SwitchFrame = styled(View, {
32
+ displayName: 'Switch',
33
+ context: SwitchStyledContext,
34
+ render: 'button',
35
+ tabIndex: 0,
36
+ })
37
+
38
+ export function createSwitch(createProps: {
39
+ Frame?: SwitchFrameComponent
40
+ Thumb?: SwitchThumbFrameComponent
41
+ componentThemes?: {
42
+ frame?: ThemeProps['name']
43
+ thumb?: ThemeProps['name']
39
44
  }
40
- )
41
-
42
- const getSwitchHeight = (val: SizeTokens) =>
43
- Math.round(getVariableValue(getSize(val)) * 0.65)
44
-
45
- const getSwitchWidth = (val: SizeTokens) => getSwitchHeight(val) * 2
46
-
47
- export const SwitchFrame = styled(
48
- YStack,
49
- {
50
- name: 'Switch',
51
- render: 'button',
52
- tabIndex: 0,
53
-
54
- variants: {
55
- unstyled: {
56
- false: {
57
- borderRadius: 1000,
58
- backgroundColor: '$background',
59
-
60
- focusVisibleStyle: {
61
- outlineColor: '$outlineColor',
62
- outlineStyle: 'solid',
63
- outlineWidth: 2,
64
- },
65
- },
66
- },
67
-
68
- size: {
69
- '...size': (val, { props }) => {
70
- if (props['unstyled']) return
71
- const height = getSwitchHeight(val)
72
- const width = getSwitchWidth(val)
73
- return {
74
- height,
75
- minHeight: height,
76
- width,
77
- }
78
- },
79
- },
80
- } as const,
81
-
82
- defaultVariants: {
83
- unstyled: process.env.TAMAGUI_HEADLESS === '1',
45
+ }) {
46
+ const Frame = (createProps.Frame ?? SwitchFrame) as typeof SwitchFrame
47
+ const Thumb = (createProps.Thumb ?? SwitchThumbFrame) as typeof SwitchThumbFrame
48
+
49
+ Frame.staticConfig.context = SwitchStyledContext
50
+ Thumb.staticConfig.context = SwitchStyledContext
51
+
52
+ const SwitchThumbComponent = createStyledHOC(
53
+ Thumb,
54
+ function SwitchThumb(
55
+ props: Omit<GetProps<typeof Thumb>, keyof SwitchThumbProps> & SwitchThumbProps,
56
+ forwardedRef
57
+ ) {
58
+ const { size: sizeProp, activeStyle, theme, ...thumbProps } = props
59
+ const styledContext = SwitchStyledContext.useStyledContext()
60
+ const { size: sizeContext, active, disabled, frameWidth = 0 } = styledContext
61
+ const size = sizeProp ?? sizeContext ?? true
62
+ const initialChecked = React.useRef(active).current
63
+ const initialWidth = getVariableValue(props.width, 'size')
64
+ const [thumbWidth, setThumbWidth] = React.useState(
65
+ typeof initialWidth === 'number' ? initialWidth : 0
66
+ )
67
+ const distance = frameWidth - thumbWidth
68
+ const x = initialChecked ? (active ? 0 : -distance) : active ? distance : 0
69
+
70
+ const thumb = (
71
+ <Thumb
72
+ ref={forwardedRef}
73
+ size={size}
74
+ alignSelf={initialChecked ? 'flex-end' : 'flex-start'}
75
+ x={x}
76
+ onLayout={composeEventHandlers(props.onLayout, (event) => {
77
+ setThumbWidth(event.nativeEvent.layout.width)
78
+ })}
79
+ disabled={disabled}
80
+ {...thumbProps}
81
+ {...(active && activeStyle)}
82
+ />
83
+ )
84
+
85
+ const themeName = theme ?? createProps.componentThemes?.thumb
86
+ return themeName ? <Theme name={themeName}>{thumb}</Theme> : thumb
87
+ }
88
+ )
89
+
90
+ const SwitchComponent = createStyledHOC(
91
+ Frame,
92
+ function Switch(_props: SwitchProps, forwardedRef) {
93
+ const {
94
+ native,
95
+ nativeProps,
96
+ checked: checkedProp,
97
+ defaultChecked,
98
+ onCheckedChange,
99
+ activeStyle,
100
+ activeTheme,
101
+ theme,
102
+ ...props
103
+ } = _props
104
+ const [checked, setChecked] = useControllableState({
105
+ prop: checkedProp,
106
+ defaultProp: defaultChecked || false,
107
+ onChange: onCheckedChange,
108
+ transition: true,
109
+ })
110
+ const styledContext = React.useContext(SwitchStyledContext.context)
111
+ const [frameWidth, setFrameInnerWidth] = React.useState(0)
112
+ const { switchProps, bubbleInput, switchRef } = useSwitch(
113
+ props as any,
114
+ [checked, setChecked],
115
+ // @ts-ignore TODO tamagui react 19 type error
116
+ forwardedRef
117
+ )
118
+ const nativeSwitch = useSwitchNative({
119
+ id: props.id,
120
+ disabled: props.disabled,
121
+ native,
122
+ nativeProps,
123
+ checked,
124
+ setChecked,
125
+ })
126
+
127
+ if (nativeSwitch) {
128
+ return nativeSwitch
129
+ }
130
+
131
+ const disabled = props.disabled
132
+ const size = styledContext.size ?? props.size ?? true
133
+
134
+ const handleLayout = (event: LayoutChangeEvent) => {
135
+ const next = event.nativeEvent.layout.width
136
+ if (next !== frameWidth) {
137
+ setFrameInnerWidth(next)
138
+ }
139
+ }
140
+
141
+ const frame = (
142
+ <Frame
143
+ ref={switchRef}
144
+ render="button"
145
+ {...(isWeb && { type: 'button' })}
146
+ size={size}
147
+ {...props}
148
+ {...(switchProps as any)}
149
+ disabled={disabled}
150
+ {...(checked && activeStyle)}
151
+ >
152
+ <View alignSelf="stretch" flex={1} onLayout={handleLayout}>
153
+ {props.children}
154
+ </View>
155
+ </Frame>
156
+ )
157
+
158
+ const frameTheme =
159
+ (checked ? activeTheme : undefined) ?? theme ?? createProps.componentThemes?.frame
160
+
161
+ return (
162
+ <>
163
+ <SwitchStyledContext.Provider
164
+ size={size}
165
+ active={checked}
166
+ disabled={disabled}
167
+ frameWidth={frameWidth}
168
+ >
169
+ {frameTheme ? <Theme name={frameTheme}>{frame}</Theme> : frame}
170
+ </SwitchStyledContext.Provider>
171
+
172
+ {bubbleInput}
173
+ </>
174
+ )
84
175
  },
85
- },
86
- {
87
- accept: {
88
- activeStyle: 'style',
89
- } as const,
90
- }
91
- )
176
+ {
177
+ disableTheme: true,
178
+ }
179
+ )
180
+
181
+ return withStaticProperties(SwitchComponent, {
182
+ Frame,
183
+ Thumb: SwitchThumbComponent,
184
+ })
185
+ }
186
+
187
+ export const Switch = createSwitch({
188
+ Frame: SwitchFrame,
189
+ Thumb: SwitchThumbFrame,
190
+ })
191
+
192
+ export const SwitchThumb = Switch.Thumb
package/src/index.ts CHANGED
@@ -1,24 +1,4 @@
1
- import { createSwitch } from './createSwitch'
2
- import { SwitchFrame, SwitchThumb } from './Switch'
3
-
4
- export * from './createSwitch'
5
1
  export * from './StyledContext'
6
2
  export * from './Switch'
7
3
  export * from './types'
8
4
  export { useSwitchNative } from './useSwitchNative'
9
-
10
- /**
11
- * @summary A component that displays a switch that can be used to toggle between two states.
12
- * @see — Docs https://tamagui.dev/ui/switch
13
- *
14
- * @example
15
- * ```tsx
16
- * <Switch id={id} size={props.size} defaultChecked={props.defaultChecked}>
17
- * <Switch.Thumb transition="quicker" />
18
- * </Switch>
19
- * ```
20
- */
21
- export const Switch = createSwitch({
22
- Frame: SwitchFrame,
23
- Thumb: SwitchThumb,
24
- })
package/src/types.ts CHANGED
@@ -1,11 +1,10 @@
1
1
  import type { GetProps, NativeValue, SizeTokens, ViewProps } from '@tamagui/core'
2
2
  import type { SwitchExtraProps as HeadlessSwitchExtraProps } from '@tamagui/switch-headless'
3
3
  import type { SwitchProps as NativeSwitchProps, ViewStyle } from 'react-native'
4
- import type { SwitchThumb } from './Switch'
4
+ import type { SwitchThumbFrame } from './Switch'
5
5
 
6
6
  export type SwitchSharedProps = {
7
- size?: SizeTokens | number
8
- unstyled?: boolean
7
+ size?: SizeTokens | number | true
9
8
  }
10
9
 
11
10
  export type SwitchBaseProps = ViewProps & SwitchSharedProps
@@ -16,7 +15,7 @@ export type SwitchFrameActiveStyleProps = {
16
15
  }
17
16
 
18
17
  export type SwitchThumbActiveStyleProps = {
19
- activeStyle?: GetProps<typeof SwitchThumb>
18
+ activeStyle?: GetProps<typeof SwitchThumbFrame>
20
19
  }
21
20
 
22
21
  export type SwitchExtraProps = HeadlessSwitchExtraProps & {
@@ -32,7 +31,6 @@ export type SwitchThumbProps = SwitchThumbBaseProps &
32
31
  SwitchSharedProps &
33
32
  SwitchThumbActiveStyleProps
34
33
 
35
- // loose types for createSwitch generics - actual components have stricter types
36
34
  export type SwitchComponent = (props: any) => any
37
35
  export type SwitchThumbComponent = (props: any) => any
38
36
 
@@ -3,7 +3,6 @@ export declare const SwitchStyledContext: import("@tamagui/core").StyledContext<
3
3
  active?: boolean;
4
4
  disabled?: boolean;
5
5
  frameWidth?: number;
6
- size?: SizeTokens;
7
- unstyled?: boolean;
8
- }>;
6
+ size?: SizeTokens | number | true;
7
+ }, "active" | "disabled" | "frameWidth" | "size">;
9
8
  //# sourceMappingURL=StyledContext.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"StyledContext.d.ts","sourceRoot":"","sources":["../src/StyledContext.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,eAAe,CAAA;AAG/C,eAAO,MAAM,mBAAmB;aACrB,OAAO;eACL,OAAO;iBACL,MAAM;WACZ,UAAU;eACN,OAAO;EAOlB,CAAA"}
1
+ {"version":3,"file":"StyledContext.d.ts","sourceRoot":"","sources":["../src/StyledContext.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,eAAe,CAAA;AAG/C,eAAO,MAAM,mBAAmB;aACrB,OAAO;eACL,OAAO;iBACL,MAAM;WACZ,UAAU,GAAG,MAAM,GAAG,IAAI;iDAMjC,CAAA"}