@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.
- package/dist/cjs/StyledContext.cjs +19 -23
- package/dist/cjs/StyledContext.native.cjs +33 -0
- package/dist/cjs/StyledContext.native.js +19 -22
- package/dist/cjs/StyledContext.native.js.map +1 -1
- package/dist/cjs/Switch.cjs +140 -86
- package/dist/cjs/Switch.native.cjs +163 -0
- package/dist/cjs/Switch.native.js +147 -90
- package/dist/cjs/Switch.native.js.map +1 -1
- package/dist/cjs/index.cjs +14 -25
- package/dist/cjs/index.native.cjs +31 -0
- package/dist/cjs/index.native.js +15 -25
- package/dist/cjs/index.native.js.map +1 -1
- package/dist/cjs/types.cjs +10 -11
- package/dist/cjs/types.native.cjs +19 -0
- package/dist/cjs/types.native.js +10 -10
- package/dist/cjs/types.native.js.map +1 -1
- package/dist/cjs/useSwitchNative.cjs +16 -19
- package/dist/cjs/useSwitchNative.native.cjs +65 -0
- package/dist/cjs/useSwitchNative.native.js +44 -54
- package/dist/cjs/useSwitchNative.native.js.map +1 -1
- package/dist/esm/StyledContext.mjs +6 -5
- package/dist/esm/StyledContext.mjs.map +1 -1
- package/dist/esm/StyledContext.native.js +6 -5
- package/dist/esm/StyledContext.native.js.map +1 -1
- package/dist/esm/Switch.mjs +119 -71
- package/dist/esm/Switch.mjs.map +1 -1
- package/dist/esm/Switch.native.js +126 -76
- package/dist/esm/Switch.native.js.map +1 -1
- package/dist/esm/index.js +8 -12
- package/dist/esm/index.mjs +8 -12
- package/dist/esm/index.native.js +8 -12
- package/dist/esm/types.mjs +0 -2
- package/dist/esm/types.native.js +0 -2
- package/dist/esm/useSwitchNative.mjs +2 -1
- package/dist/esm/useSwitchNative.mjs.map +1 -1
- package/dist/esm/useSwitchNative.native.js +30 -31
- package/dist/esm/useSwitchNative.native.js.map +1 -1
- package/dist/jsx/StyledContext.mjs +6 -5
- package/dist/jsx/StyledContext.mjs.map +1 -1
- package/dist/jsx/StyledContext.native.cjs +33 -0
- package/dist/jsx/StyledContext.native.js +19 -22
- package/dist/jsx/StyledContext.native.js.map +1 -1
- package/dist/jsx/Switch.mjs +119 -71
- package/dist/jsx/Switch.mjs.map +1 -1
- package/dist/jsx/Switch.native.cjs +163 -0
- package/dist/jsx/Switch.native.js +147 -90
- package/dist/jsx/Switch.native.js.map +1 -1
- package/dist/jsx/index.js +8 -12
- package/dist/jsx/index.mjs +8 -12
- package/dist/jsx/index.native.cjs +31 -0
- package/dist/jsx/index.native.js +15 -25
- package/dist/jsx/index.native.js.map +1 -1
- package/dist/jsx/types.mjs +0 -2
- package/dist/jsx/types.native.cjs +19 -0
- package/dist/jsx/types.native.js +10 -10
- package/dist/jsx/types.native.js.map +1 -1
- package/dist/jsx/useSwitchNative.mjs +2 -1
- package/dist/jsx/useSwitchNative.mjs.map +1 -1
- package/dist/jsx/useSwitchNative.native.cjs +65 -0
- package/dist/jsx/useSwitchNative.native.js +44 -54
- package/dist/jsx/useSwitchNative.native.js.map +1 -1
- package/package.json +7 -14
- package/src/StyledContext.tsx +1 -3
- package/src/Switch.tsx +190 -89
- package/src/index.ts +0 -20
- package/src/types.ts +3 -5
- package/types/StyledContext.d.ts +2 -3
- package/types/StyledContext.d.ts.map +1 -1
- package/types/Switch.d.ts +209 -37
- package/types/Switch.d.ts.map +1 -1
- package/types/index.d.ts +0 -146
- package/types/index.d.ts.map +1 -1
- package/types/types.d.ts +3 -4
- package/types/types.d.ts.map +1 -1
- package/dist/cjs/createSwitch.cjs +0 -186
- package/dist/cjs/createSwitch.native.js +0 -194
- package/dist/cjs/createSwitch.native.js.map +0 -1
- package/dist/esm/createSwitch.mjs +0 -150
- package/dist/esm/createSwitch.mjs.map +0 -1
- package/dist/esm/createSwitch.native.js +0 -155
- package/dist/esm/createSwitch.native.js.map +0 -1
- package/dist/esm/index.js.map +0 -1
- package/dist/esm/index.mjs.map +0 -1
- package/dist/esm/index.native.js.map +0 -1
- package/dist/esm/types.mjs.map +0 -1
- package/dist/esm/types.native.js.map +0 -1
- package/dist/jsx/createSwitch.mjs +0 -150
- package/dist/jsx/createSwitch.mjs.map +0 -1
- package/dist/jsx/createSwitch.native.js +0 -194
- package/dist/jsx/createSwitch.native.js.map +0 -1
- package/dist/jsx/index.js.map +0 -1
- package/dist/jsx/index.mjs.map +0 -1
- package/dist/jsx/types.mjs.map +0 -1
- package/src/Switch.draggable-tmp +0 -340
- package/src/createSwitch.tsx +0 -201
- package/types/createSwitch.d.ts +0 -141
- package/types/createSwitch.d.ts.map +0 -1
package/src/Switch.tsx
CHANGED
|
@@ -1,91 +1,192 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
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
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
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
|
-
|
|
88
|
-
|
|
89
|
-
|
|
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 {
|
|
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
|
|
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
|
|
package/types/StyledContext.d.ts
CHANGED
|
@@ -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
|
-
|
|
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
|
|
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"}
|