@impulse-ui-native/switch 2.7.0
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/LICENSE +21 -0
- package/README.md +44 -0
- package/dist/components/index.d.mts +6 -0
- package/dist/components/index.js +5 -0
- package/dist/components/index.js.map +1 -0
- package/dist/components/switch.d.mts +9 -0
- package/dist/components/switch.js +92 -0
- package/dist/components/switch.js.map +1 -0
- package/dist/components/switch.styles.d.mts +35 -0
- package/dist/components/switch.styles.js +38 -0
- package/dist/components/switch.styles.js.map +1 -0
- package/dist/hooks/index.d.mts +4 -0
- package/dist/hooks/index.js +7 -0
- package/dist/hooks/index.js.map +1 -0
- package/dist/hooks/use-switch-animation.hook.d.mts +25 -0
- package/dist/hooks/use-switch-animation.hook.js +63 -0
- package/dist/hooks/use-switch-animation.hook.js.map +1 -0
- package/dist/hooks/use-switch-tokens.hook.d.mts +31 -0
- package/dist/hooks/use-switch-tokens.hook.js +29 -0
- package/dist/hooks/use-switch-tokens.hook.js.map +1 -0
- package/dist/index.d.mts +6 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -0
- package/dist/types/index.d.mts +3 -0
- package/dist/types/index.js +2 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/switch.types.d.mts +15 -0
- package/dist/types/switch.types.js +1 -0
- package/dist/types/switch.types.js.map +1 -0
- package/package.json +65 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 ImpulseUI Native contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# @impulse-ui-native/switch
|
|
2
|
+
|
|
3
|
+
Accessible, token-aware animated switch controls for React Native and React Native Web.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
pnpm add @impulse-ui-native/switch react-native-reanimated react-native-worklets
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Complete the platform setup required by Reanimated and Worklets, rebuild the native application when necessary, and render the component inside `ThemeProvider` from `@impulse-ui-native/theme`.
|
|
12
|
+
|
|
13
|
+
## Main exports
|
|
14
|
+
|
|
15
|
+
- `Switch` supports controlled and uncontrolled on/off state.
|
|
16
|
+
- `SwitchProps` describes the component API.
|
|
17
|
+
|
|
18
|
+
## Usage
|
|
19
|
+
|
|
20
|
+
```tsx
|
|
21
|
+
import { useState } from "react";
|
|
22
|
+
|
|
23
|
+
import { Switch } from "@impulse-ui-native/switch";
|
|
24
|
+
|
|
25
|
+
export function NotificationsSwitch() {
|
|
26
|
+
const [checked, setChecked] = useState(false);
|
|
27
|
+
|
|
28
|
+
return (
|
|
29
|
+
<Switch
|
|
30
|
+
accessibilityLabel="Notifications"
|
|
31
|
+
checked={checked}
|
|
32
|
+
onCheckedChange={setChecked}
|
|
33
|
+
size="medium"
|
|
34
|
+
variant="filled"
|
|
35
|
+
/>
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
Use `defaultChecked` for uncontrolled state. The thumb position and track colors animate when the state changes. System reduced-motion preferences disable the transition while preserving the state change.
|
|
41
|
+
|
|
42
|
+
Set `loading` while an update is pending. Loading switches expose a busy accessibility state, show an activity indicator, and block further presses until loading ends. Disabled switches also block interaction.
|
|
43
|
+
|
|
44
|
+
The standalone switch does not render a label, so provide `accessibilityLabel` unless a surrounding labelled control supplies an accessible name.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/components/index.ts"],"sourcesContent":["export { Switch } from \"./switch\";\n"],"mappings":"AAAA,SAAS,cAAc;","names":[]}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import * as react from 'react';
|
|
2
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
3
|
+
import { SwitchProps } from '../types/switch.types.mjs';
|
|
4
|
+
import '@impulse-ui-native/primitives';
|
|
5
|
+
import '@impulse-ui-native/theme';
|
|
6
|
+
|
|
7
|
+
declare const Switch: react.MemoExoticComponent<({ accessibilityState, checked: checkedProp, defaultChecked, disabled, hitSlop, loading, onCheckedChange, onPress, size, style, variant, ...props }: SwitchProps) => react_jsx_runtime.JSX.Element>;
|
|
8
|
+
|
|
9
|
+
export { Switch };
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { jsx } from "react/jsx-runtime";
|
|
2
|
+
import { memo, useCallback, useMemo } from "react";
|
|
3
|
+
import { ActivityIndicator, StyleSheet } from "react-native";
|
|
4
|
+
import Animated from "react-native-reanimated";
|
|
5
|
+
import { useControllableState } from "@impulse-ui-native/core";
|
|
6
|
+
import { Pressable } from "@impulse-ui-native/primitives";
|
|
7
|
+
import { useThemedStyles } from "@impulse-ui-native/theme";
|
|
8
|
+
import { useSwitchAnimation, useSwitchTokens } from "../hooks";
|
|
9
|
+
import { switchStyles } from "./switch.styles";
|
|
10
|
+
const Switch = memo(function Switch2({
|
|
11
|
+
accessibilityState,
|
|
12
|
+
checked: checkedProp,
|
|
13
|
+
defaultChecked = false,
|
|
14
|
+
disabled,
|
|
15
|
+
hitSlop,
|
|
16
|
+
loading = false,
|
|
17
|
+
onCheckedChange,
|
|
18
|
+
onPress,
|
|
19
|
+
size = "medium",
|
|
20
|
+
style,
|
|
21
|
+
variant = "filled",
|
|
22
|
+
...props
|
|
23
|
+
}) {
|
|
24
|
+
const [checked, setChecked] = useControllableState({
|
|
25
|
+
prop: checkedProp,
|
|
26
|
+
defaultProp: defaultChecked,
|
|
27
|
+
onChange: onCheckedChange
|
|
28
|
+
});
|
|
29
|
+
const interactionDisabled = disabled === true || loading;
|
|
30
|
+
const {
|
|
31
|
+
animationDuration,
|
|
32
|
+
borderWidth,
|
|
33
|
+
colors,
|
|
34
|
+
loadingIndicatorColor,
|
|
35
|
+
sizeTokens
|
|
36
|
+
} = useSwitchTokens({ disabled: interactionDisabled, size, variant });
|
|
37
|
+
const styles = useThemedStyles(switchStyles, { size }, [size]);
|
|
38
|
+
const { thumbStyle, trackStyle } = useSwitchAnimation({
|
|
39
|
+
animationDuration,
|
|
40
|
+
borderWidth,
|
|
41
|
+
checked,
|
|
42
|
+
colors,
|
|
43
|
+
sizeTokens
|
|
44
|
+
});
|
|
45
|
+
const resolvedAccessibilityState = useMemo(
|
|
46
|
+
() => ({
|
|
47
|
+
...accessibilityState,
|
|
48
|
+
busy: loading,
|
|
49
|
+
checked,
|
|
50
|
+
disabled: interactionDisabled
|
|
51
|
+
}),
|
|
52
|
+
[accessibilityState, checked, interactionDisabled, loading]
|
|
53
|
+
);
|
|
54
|
+
const switchStyle = useCallback(
|
|
55
|
+
(state) => StyleSheet.flatten([
|
|
56
|
+
styles.root,
|
|
57
|
+
typeof style === "function" ? style(state) : style
|
|
58
|
+
]),
|
|
59
|
+
[style, styles.root]
|
|
60
|
+
);
|
|
61
|
+
const handlePress = useCallback(
|
|
62
|
+
(event) => {
|
|
63
|
+
setChecked((current) => !current);
|
|
64
|
+
onPress?.(event);
|
|
65
|
+
},
|
|
66
|
+
[onPress, setChecked]
|
|
67
|
+
);
|
|
68
|
+
return /* @__PURE__ */ jsx(
|
|
69
|
+
Pressable,
|
|
70
|
+
{
|
|
71
|
+
...props,
|
|
72
|
+
accessibilityRole: "switch",
|
|
73
|
+
accessibilityState: resolvedAccessibilityState,
|
|
74
|
+
disabled: interactionDisabled,
|
|
75
|
+
hitSlop: hitSlop ?? sizeTokens.hitSlop,
|
|
76
|
+
onPress: handlePress,
|
|
77
|
+
style: switchStyle,
|
|
78
|
+
children: /* @__PURE__ */ jsx(Animated.View, { pointerEvents: "none", style: [styles.track, trackStyle], children: /* @__PURE__ */ jsx(Animated.View, { style: [styles.thumb, thumbStyle], children: loading ? /* @__PURE__ */ jsx(
|
|
79
|
+
ActivityIndicator,
|
|
80
|
+
{
|
|
81
|
+
color: loadingIndicatorColor,
|
|
82
|
+
size: "small",
|
|
83
|
+
style: styles.loadingIndicator
|
|
84
|
+
}
|
|
85
|
+
) : null }) })
|
|
86
|
+
}
|
|
87
|
+
);
|
|
88
|
+
});
|
|
89
|
+
export {
|
|
90
|
+
Switch
|
|
91
|
+
};
|
|
92
|
+
//# sourceMappingURL=switch.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/components/switch.tsx"],"sourcesContent":["import type {\n AccessibilityState,\n GestureResponderEvent,\n PressableStateCallbackType,\n StyleProp,\n ViewStyle,\n} from \"react-native\";\nimport { memo, useCallback, useMemo } from \"react\";\nimport { ActivityIndicator, StyleSheet } from \"react-native\";\nimport Animated from \"react-native-reanimated\";\n\nimport { useControllableState } from \"@impulse-ui-native/core\";\nimport { Pressable } from \"@impulse-ui-native/primitives\";\nimport { useThemedStyles } from \"@impulse-ui-native/theme\";\n\nimport type { SwitchProps } from \"../types\";\nimport { useSwitchAnimation, useSwitchTokens } from \"../hooks\";\nimport { switchStyles } from \"./switch.styles\";\n\nexport const Switch = memo(function Switch({\n accessibilityState,\n checked: checkedProp,\n defaultChecked = false,\n disabled,\n hitSlop,\n loading = false,\n onCheckedChange,\n onPress,\n size = \"medium\",\n style,\n variant = \"filled\",\n ...props\n}: SwitchProps) {\n const [checked, setChecked] = useControllableState<boolean>({\n prop: checkedProp,\n defaultProp: defaultChecked,\n onChange: onCheckedChange,\n });\n const interactionDisabled = disabled === true || loading;\n const {\n animationDuration,\n borderWidth,\n colors,\n loadingIndicatorColor,\n sizeTokens,\n } = useSwitchTokens({ disabled: interactionDisabled, size, variant });\n const styles = useThemedStyles(switchStyles, { size }, [size]);\n const { thumbStyle, trackStyle } = useSwitchAnimation({\n animationDuration,\n borderWidth,\n checked,\n colors,\n sizeTokens,\n });\n\n const resolvedAccessibilityState = useMemo<AccessibilityState>(\n () => ({\n ...accessibilityState,\n busy: loading,\n checked,\n disabled: interactionDisabled,\n }),\n [accessibilityState, checked, interactionDisabled, loading],\n );\n\n const switchStyle = useCallback(\n (state: PressableStateCallbackType): StyleProp<ViewStyle> =>\n StyleSheet.flatten([\n styles.root,\n typeof style === \"function\" ? style(state) : style,\n ]),\n [style, styles.root],\n );\n\n const handlePress = useCallback(\n (event: GestureResponderEvent) => {\n setChecked((current) => !current);\n onPress?.(event);\n },\n [onPress, setChecked],\n );\n\n return (\n <Pressable\n {...props}\n accessibilityRole=\"switch\"\n accessibilityState={resolvedAccessibilityState}\n disabled={interactionDisabled}\n hitSlop={hitSlop ?? sizeTokens.hitSlop}\n onPress={handlePress}\n style={switchStyle}\n >\n <Animated.View pointerEvents=\"none\" style={[styles.track, trackStyle]}>\n <Animated.View style={[styles.thumb, thumbStyle]}>\n {loading ? (\n <ActivityIndicator\n color={loadingIndicatorColor}\n size=\"small\"\n style={styles.loadingIndicator}\n />\n ) : null}\n </Animated.View>\n </Animated.View>\n </Pressable>\n );\n});\n"],"mappings":"AA+FY;AAxFZ,SAAS,MAAM,aAAa,eAAe;AAC3C,SAAS,mBAAmB,kBAAkB;AAC9C,OAAO,cAAc;AAErB,SAAS,4BAA4B;AACrC,SAAS,iBAAiB;AAC1B,SAAS,uBAAuB;AAGhC,SAAS,oBAAoB,uBAAuB;AACpD,SAAS,oBAAoB;AAEtB,MAAM,SAAS,KAAK,SAASA,QAAO;AAAA,EACzC;AAAA,EACA,SAAS;AAAA,EACT,iBAAiB;AAAA,EACjB;AAAA,EACA;AAAA,EACA,UAAU;AAAA,EACV;AAAA,EACA;AAAA,EACA,OAAO;AAAA,EACP;AAAA,EACA,UAAU;AAAA,EACV,GAAG;AACL,GAAgB;AACd,QAAM,CAAC,SAAS,UAAU,IAAI,qBAA8B;AAAA,IAC1D,MAAM;AAAA,IACN,aAAa;AAAA,IACb,UAAU;AAAA,EACZ,CAAC;AACD,QAAM,sBAAsB,aAAa,QAAQ;AACjD,QAAM;AAAA,IACJ;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF,IAAI,gBAAgB,EAAE,UAAU,qBAAqB,MAAM,QAAQ,CAAC;AACpE,QAAM,SAAS,gBAAgB,cAAc,EAAE,KAAK,GAAG,CAAC,IAAI,CAAC;AAC7D,QAAM,EAAE,YAAY,WAAW,IAAI,mBAAmB;AAAA,IACpD;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF,CAAC;AAED,QAAM,6BAA6B;AAAA,IACjC,OAAO;AAAA,MACL,GAAG;AAAA,MACH,MAAM;AAAA,MACN;AAAA,MACA,UAAU;AAAA,IACZ;AAAA,IACA,CAAC,oBAAoB,SAAS,qBAAqB,OAAO;AAAA,EAC5D;AAEA,QAAM,cAAc;AAAA,IAClB,CAAC,UACC,WAAW,QAAQ;AAAA,MACjB,OAAO;AAAA,MACP,OAAO,UAAU,aAAa,MAAM,KAAK,IAAI;AAAA,IAC/C,CAAC;AAAA,IACH,CAAC,OAAO,OAAO,IAAI;AAAA,EACrB;AAEA,QAAM,cAAc;AAAA,IAClB,CAAC,UAAiC;AAChC,iBAAW,CAAC,YAAY,CAAC,OAAO;AAChC,gBAAU,KAAK;AAAA,IACjB;AAAA,IACA,CAAC,SAAS,UAAU;AAAA,EACtB;AAEA,SACE;AAAA,IAAC;AAAA;AAAA,MACE,GAAG;AAAA,MACJ,mBAAkB;AAAA,MAClB,oBAAoB;AAAA,MACpB,UAAU;AAAA,MACV,SAAS,WAAW,WAAW;AAAA,MAC/B,SAAS;AAAA,MACT,OAAO;AAAA,MAEP,8BAAC,SAAS,MAAT,EAAc,eAAc,QAAO,OAAO,CAAC,OAAO,OAAO,UAAU,GAClE,8BAAC,SAAS,MAAT,EAAc,OAAO,CAAC,OAAO,OAAO,UAAU,GAC5C,oBACC;AAAA,QAAC;AAAA;AAAA,UACC,OAAO;AAAA,UACP,MAAK;AAAA,UACL,OAAO,OAAO;AAAA;AAAA,MAChB,IACE,MACN,GACF;AAAA;AAAA,EACF;AAEJ,CAAC;","names":["Switch"]}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { AppTheme } from '@impulse-ui-native/theme';
|
|
2
|
+
import { SwitchThemeProps } from '../types/switch.types.mjs';
|
|
3
|
+
import '@impulse-ui-native/primitives';
|
|
4
|
+
|
|
5
|
+
declare function switchStyles(theme: AppTheme, props: SwitchThemeProps): {
|
|
6
|
+
root: {
|
|
7
|
+
width: number;
|
|
8
|
+
height: number;
|
|
9
|
+
};
|
|
10
|
+
track: {
|
|
11
|
+
position: "relative";
|
|
12
|
+
justifyContent: "center";
|
|
13
|
+
width: number;
|
|
14
|
+
height: number;
|
|
15
|
+
borderWidth: number;
|
|
16
|
+
borderRadius: number;
|
|
17
|
+
};
|
|
18
|
+
thumb: {
|
|
19
|
+
position: "absolute";
|
|
20
|
+
alignItems: "center";
|
|
21
|
+
justifyContent: "center";
|
|
22
|
+
left: number | undefined;
|
|
23
|
+
right: number | undefined;
|
|
24
|
+
width: number;
|
|
25
|
+
height: number;
|
|
26
|
+
borderRadius: number;
|
|
27
|
+
};
|
|
28
|
+
loadingIndicator: {
|
|
29
|
+
transform: {
|
|
30
|
+
scale: number;
|
|
31
|
+
}[];
|
|
32
|
+
};
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export { switchStyles };
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { I18nManager, StyleSheet } from "react-native";
|
|
2
|
+
function switchStyles(theme, props) {
|
|
3
|
+
const { size } = props;
|
|
4
|
+
const switchTokens = theme.components.switch;
|
|
5
|
+
const sizeTokens = switchTokens.sizes[size];
|
|
6
|
+
const thumbInset = sizeTokens.trackPadding;
|
|
7
|
+
return StyleSheet.create({
|
|
8
|
+
root: {
|
|
9
|
+
width: sizeTokens.width,
|
|
10
|
+
height: sizeTokens.height
|
|
11
|
+
},
|
|
12
|
+
track: {
|
|
13
|
+
position: "relative",
|
|
14
|
+
justifyContent: "center",
|
|
15
|
+
width: sizeTokens.width,
|
|
16
|
+
height: sizeTokens.height,
|
|
17
|
+
borderWidth: switchTokens.borderWidth,
|
|
18
|
+
borderRadius: switchTokens.borderRadius
|
|
19
|
+
},
|
|
20
|
+
thumb: {
|
|
21
|
+
position: "absolute",
|
|
22
|
+
alignItems: "center",
|
|
23
|
+
justifyContent: "center",
|
|
24
|
+
left: I18nManager.isRTL ? void 0 : thumbInset,
|
|
25
|
+
right: I18nManager.isRTL ? thumbInset : void 0,
|
|
26
|
+
width: sizeTokens.thumbSize,
|
|
27
|
+
height: sizeTokens.thumbSize,
|
|
28
|
+
borderRadius: switchTokens.thumbBorderRadius
|
|
29
|
+
},
|
|
30
|
+
loadingIndicator: {
|
|
31
|
+
transform: [{ scale: sizeTokens.loadingIndicatorScale }]
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
export {
|
|
36
|
+
switchStyles
|
|
37
|
+
};
|
|
38
|
+
//# sourceMappingURL=switch.styles.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/components/switch.styles.ts"],"sourcesContent":["import { I18nManager, StyleSheet } from \"react-native\";\n\nimport type { AppTheme } from \"@impulse-ui-native/theme\";\n\nimport type { SwitchThemeProps } from \"../types\";\n\nexport function switchStyles(theme: AppTheme, props: SwitchThemeProps) {\n const { size } = props;\n\n const switchTokens = theme.components.switch;\n const sizeTokens = switchTokens.sizes[size];\n const thumbInset = sizeTokens.trackPadding;\n\n return StyleSheet.create({\n root: {\n width: sizeTokens.width,\n height: sizeTokens.height,\n },\n\n track: {\n position: \"relative\",\n justifyContent: \"center\",\n\n width: sizeTokens.width,\n height: sizeTokens.height,\n\n borderWidth: switchTokens.borderWidth,\n borderRadius: switchTokens.borderRadius,\n },\n\n thumb: {\n position: \"absolute\",\n alignItems: \"center\",\n justifyContent: \"center\",\n left: I18nManager.isRTL ? undefined : thumbInset,\n right: I18nManager.isRTL ? thumbInset : undefined,\n\n width: sizeTokens.thumbSize,\n height: sizeTokens.thumbSize,\n\n borderRadius: switchTokens.thumbBorderRadius,\n },\n\n loadingIndicator: {\n transform: [{ scale: sizeTokens.loadingIndicatorScale }],\n },\n });\n}\n"],"mappings":"AAAA,SAAS,aAAa,kBAAkB;AAMjC,SAAS,aAAa,OAAiB,OAAyB;AACrE,QAAM,EAAE,KAAK,IAAI;AAEjB,QAAM,eAAe,MAAM,WAAW;AACtC,QAAM,aAAa,aAAa,MAAM,IAAI;AAC1C,QAAM,aAAa,WAAW;AAE9B,SAAO,WAAW,OAAO;AAAA,IACvB,MAAM;AAAA,MACJ,OAAO,WAAW;AAAA,MAClB,QAAQ,WAAW;AAAA,IACrB;AAAA,IAEA,OAAO;AAAA,MACL,UAAU;AAAA,MACV,gBAAgB;AAAA,MAEhB,OAAO,WAAW;AAAA,MAClB,QAAQ,WAAW;AAAA,MAEnB,aAAa,aAAa;AAAA,MAC1B,cAAc,aAAa;AAAA,IAC7B;AAAA,IAEA,OAAO;AAAA,MACL,UAAU;AAAA,MACV,YAAY;AAAA,MACZ,gBAAgB;AAAA,MAChB,MAAM,YAAY,QAAQ,SAAY;AAAA,MACtC,OAAO,YAAY,QAAQ,aAAa;AAAA,MAExC,OAAO,WAAW;AAAA,MAClB,QAAQ,WAAW;AAAA,MAEnB,cAAc,aAAa;AAAA,IAC7B;AAAA,IAEA,kBAAkB;AAAA,MAChB,WAAW,CAAC,EAAE,OAAO,WAAW,sBAAsB,CAAC;AAAA,IACzD;AAAA,EACF,CAAC;AACH;","names":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/hooks/index.ts"],"sourcesContent":["export { useSwitchAnimation } from \"./use-switch-animation.hook\";\nexport { useSwitchTokens } from \"./use-switch-tokens.hook\";\n"],"mappings":"AAAA,SAAS,0BAA0B;AACnC,SAAS,uBAAuB;","names":[]}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import * as react_native_reanimated_lib_typescript_hook_commonTypes from 'react-native-reanimated/lib/typescript/hook/commonTypes';
|
|
2
|
+
import { SwitchSizeTokens } from '@impulse-ui-native/theme';
|
|
3
|
+
import { SwitchAnimationColors } from './use-switch-tokens.hook.mjs';
|
|
4
|
+
|
|
5
|
+
interface UseSwitchAnimationOptions {
|
|
6
|
+
animationDuration: number;
|
|
7
|
+
borderWidth: number;
|
|
8
|
+
checked: boolean;
|
|
9
|
+
colors: SwitchAnimationColors;
|
|
10
|
+
sizeTokens: SwitchSizeTokens[keyof SwitchSizeTokens];
|
|
11
|
+
}
|
|
12
|
+
declare function useSwitchAnimation({ animationDuration, borderWidth, checked, colors, sizeTokens, }: UseSwitchAnimationOptions): {
|
|
13
|
+
thumbStyle: react_native_reanimated_lib_typescript_hook_commonTypes.AnimatedStyleHandle<{
|
|
14
|
+
backgroundColor: string;
|
|
15
|
+
transform: {
|
|
16
|
+
translateX: number;
|
|
17
|
+
}[];
|
|
18
|
+
}>;
|
|
19
|
+
trackStyle: react_native_reanimated_lib_typescript_hook_commonTypes.AnimatedStyleHandle<{
|
|
20
|
+
backgroundColor: string;
|
|
21
|
+
borderColor: string;
|
|
22
|
+
}>;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export { useSwitchAnimation };
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { useEffect } from "react";
|
|
2
|
+
import { I18nManager } from "react-native";
|
|
3
|
+
import {
|
|
4
|
+
Easing,
|
|
5
|
+
interpolateColor,
|
|
6
|
+
useAnimatedStyle,
|
|
7
|
+
useReducedMotion,
|
|
8
|
+
useSharedValue,
|
|
9
|
+
withTiming
|
|
10
|
+
} from "react-native-reanimated";
|
|
11
|
+
function useSwitchAnimation({
|
|
12
|
+
animationDuration,
|
|
13
|
+
borderWidth,
|
|
14
|
+
checked,
|
|
15
|
+
colors,
|
|
16
|
+
sizeTokens
|
|
17
|
+
}) {
|
|
18
|
+
const reduceMotion = useReducedMotion();
|
|
19
|
+
const progress = useSharedValue(checked ? 1 : 0);
|
|
20
|
+
const thumbTravel = sizeTokens.width - sizeTokens.thumbSize - sizeTokens.trackPadding * 2 - borderWidth * 2;
|
|
21
|
+
const direction = I18nManager.isRTL ? -1 : 1;
|
|
22
|
+
const trackStyle = useAnimatedStyle(
|
|
23
|
+
() => ({
|
|
24
|
+
backgroundColor: interpolateColor(
|
|
25
|
+
progress.value,
|
|
26
|
+
[0, 1],
|
|
27
|
+
[colors.inactiveBackgroundColor, colors.activeBackgroundColor]
|
|
28
|
+
),
|
|
29
|
+
borderColor: interpolateColor(
|
|
30
|
+
progress.value,
|
|
31
|
+
[0, 1],
|
|
32
|
+
[colors.inactiveBorderColor, colors.activeBorderColor]
|
|
33
|
+
)
|
|
34
|
+
}),
|
|
35
|
+
[colors]
|
|
36
|
+
);
|
|
37
|
+
const thumbStyle = useAnimatedStyle(
|
|
38
|
+
() => ({
|
|
39
|
+
backgroundColor: interpolateColor(
|
|
40
|
+
progress.value,
|
|
41
|
+
[0, 1],
|
|
42
|
+
[colors.inactiveThumbColor, colors.activeThumbColor]
|
|
43
|
+
),
|
|
44
|
+
transform: [
|
|
45
|
+
{
|
|
46
|
+
translateX: progress.value * thumbTravel * direction
|
|
47
|
+
}
|
|
48
|
+
]
|
|
49
|
+
}),
|
|
50
|
+
[colors, direction, thumbTravel]
|
|
51
|
+
);
|
|
52
|
+
useEffect(() => {
|
|
53
|
+
progress.value = withTiming(checked ? 1 : 0, {
|
|
54
|
+
duration: reduceMotion ? 0 : animationDuration,
|
|
55
|
+
easing: Easing.out(Easing.cubic)
|
|
56
|
+
});
|
|
57
|
+
}, [animationDuration, checked, progress, reduceMotion]);
|
|
58
|
+
return { thumbStyle, trackStyle };
|
|
59
|
+
}
|
|
60
|
+
export {
|
|
61
|
+
useSwitchAnimation
|
|
62
|
+
};
|
|
63
|
+
//# sourceMappingURL=use-switch-animation.hook.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/hooks/use-switch-animation.hook.ts"],"sourcesContent":["import { useEffect } from \"react\";\nimport { I18nManager } from \"react-native\";\nimport {\n Easing,\n interpolateColor,\n useAnimatedStyle,\n useReducedMotion,\n useSharedValue,\n withTiming,\n} from \"react-native-reanimated\";\n\nimport type { SwitchSizeTokens } from \"@impulse-ui-native/theme\";\n\nimport type { SwitchAnimationColors } from \"./use-switch-tokens.hook\";\n\ninterface UseSwitchAnimationOptions {\n animationDuration: number;\n borderWidth: number;\n checked: boolean;\n colors: SwitchAnimationColors;\n sizeTokens: SwitchSizeTokens[keyof SwitchSizeTokens];\n}\n\nexport function useSwitchAnimation({\n animationDuration,\n borderWidth,\n checked,\n colors,\n sizeTokens,\n}: UseSwitchAnimationOptions) {\n const reduceMotion = useReducedMotion();\n const progress = useSharedValue(checked ? 1 : 0);\n\n const thumbTravel =\n sizeTokens.width -\n sizeTokens.thumbSize -\n sizeTokens.trackPadding * 2 -\n borderWidth * 2;\n const direction = I18nManager.isRTL ? -1 : 1;\n\n const trackStyle = useAnimatedStyle(\n () => ({\n backgroundColor: interpolateColor(\n progress.value,\n [0, 1],\n [colors.inactiveBackgroundColor, colors.activeBackgroundColor],\n ),\n borderColor: interpolateColor(\n progress.value,\n [0, 1],\n [colors.inactiveBorderColor, colors.activeBorderColor],\n ),\n }),\n [colors],\n );\n\n const thumbStyle = useAnimatedStyle(\n () => ({\n backgroundColor: interpolateColor(\n progress.value,\n [0, 1],\n [colors.inactiveThumbColor, colors.activeThumbColor],\n ),\n transform: [\n {\n translateX: progress.value * thumbTravel * direction,\n },\n ],\n }),\n [colors, direction, thumbTravel],\n );\n\n useEffect(() => {\n // Reanimated shared values are mutable handles updated from React effects.\n // eslint-disable-next-line react-hooks/immutability\n progress.value = withTiming(checked ? 1 : 0, {\n duration: reduceMotion ? 0 : animationDuration,\n easing: Easing.out(Easing.cubic),\n });\n }, [animationDuration, checked, progress, reduceMotion]);\n\n return { thumbStyle, trackStyle };\n}\n"],"mappings":"AAAA,SAAS,iBAAiB;AAC1B,SAAS,mBAAmB;AAC5B;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAcA,SAAS,mBAAmB;AAAA,EACjC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,GAA8B;AAC5B,QAAM,eAAe,iBAAiB;AACtC,QAAM,WAAW,eAAe,UAAU,IAAI,CAAC;AAE/C,QAAM,cACJ,WAAW,QACX,WAAW,YACX,WAAW,eAAe,IAC1B,cAAc;AAChB,QAAM,YAAY,YAAY,QAAQ,KAAK;AAE3C,QAAM,aAAa;AAAA,IACjB,OAAO;AAAA,MACL,iBAAiB;AAAA,QACf,SAAS;AAAA,QACT,CAAC,GAAG,CAAC;AAAA,QACL,CAAC,OAAO,yBAAyB,OAAO,qBAAqB;AAAA,MAC/D;AAAA,MACA,aAAa;AAAA,QACX,SAAS;AAAA,QACT,CAAC,GAAG,CAAC;AAAA,QACL,CAAC,OAAO,qBAAqB,OAAO,iBAAiB;AAAA,MACvD;AAAA,IACF;AAAA,IACA,CAAC,MAAM;AAAA,EACT;AAEA,QAAM,aAAa;AAAA,IACjB,OAAO;AAAA,MACL,iBAAiB;AAAA,QACf,SAAS;AAAA,QACT,CAAC,GAAG,CAAC;AAAA,QACL,CAAC,OAAO,oBAAoB,OAAO,gBAAgB;AAAA,MACrD;AAAA,MACA,WAAW;AAAA,QACT;AAAA,UACE,YAAY,SAAS,QAAQ,cAAc;AAAA,QAC7C;AAAA,MACF;AAAA,IACF;AAAA,IACA,CAAC,QAAQ,WAAW,WAAW;AAAA,EACjC;AAEA,YAAU,MAAM;AAGd,aAAS,QAAQ,WAAW,UAAU,IAAI,GAAG;AAAA,MAC3C,UAAU,eAAe,IAAI;AAAA,MAC7B,QAAQ,OAAO,IAAI,OAAO,KAAK;AAAA,IACjC,CAAC;AAAA,EACH,GAAG,CAAC,mBAAmB,SAAS,UAAU,YAAY,CAAC;AAEvD,SAAO,EAAE,YAAY,WAAW;AAClC;","names":[]}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { ComponentSize, ComponentVariant } from '@impulse-ui-native/theme';
|
|
2
|
+
|
|
3
|
+
interface SwitchAnimationColors {
|
|
4
|
+
activeBackgroundColor: string;
|
|
5
|
+
activeBorderColor: string;
|
|
6
|
+
activeThumbColor: string;
|
|
7
|
+
inactiveBackgroundColor: string;
|
|
8
|
+
inactiveBorderColor: string;
|
|
9
|
+
inactiveThumbColor: string;
|
|
10
|
+
}
|
|
11
|
+
interface UseSwitchTokensOptions {
|
|
12
|
+
disabled: boolean;
|
|
13
|
+
size: ComponentSize;
|
|
14
|
+
variant: ComponentVariant;
|
|
15
|
+
}
|
|
16
|
+
declare function useSwitchTokens({ disabled, size, variant, }: UseSwitchTokensOptions): {
|
|
17
|
+
animationDuration: number;
|
|
18
|
+
borderWidth: number;
|
|
19
|
+
colors: SwitchAnimationColors;
|
|
20
|
+
loadingIndicatorColor: string;
|
|
21
|
+
sizeTokens: {
|
|
22
|
+
width: number;
|
|
23
|
+
height: number;
|
|
24
|
+
thumbSize: number;
|
|
25
|
+
trackPadding: number;
|
|
26
|
+
loadingIndicatorScale: number;
|
|
27
|
+
hitSlop: number;
|
|
28
|
+
};
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
export { type SwitchAnimationColors, useSwitchTokens };
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { useComponentsTokens } from "@impulse-ui-native/theme";
|
|
2
|
+
function useSwitchTokens({
|
|
3
|
+
disabled,
|
|
4
|
+
size,
|
|
5
|
+
variant
|
|
6
|
+
}) {
|
|
7
|
+
const tokens = useComponentsTokens().switch;
|
|
8
|
+
const sizeTokens = tokens.sizes[size];
|
|
9
|
+
const variantTokens = tokens.variants[variant];
|
|
10
|
+
const colors = {
|
|
11
|
+
activeBackgroundColor: disabled ? tokens.disabledBackgroundColor : variantTokens.backgroundColor,
|
|
12
|
+
activeBorderColor: disabled ? tokens.disabledBorderColor : variantTokens.borderColor,
|
|
13
|
+
activeThumbColor: disabled ? tokens.disabledThumbColor : variantTokens.color,
|
|
14
|
+
inactiveBackgroundColor: disabled ? tokens.disabledBackgroundColor : tokens.uncheckedBackgroundColor,
|
|
15
|
+
inactiveBorderColor: disabled ? tokens.disabledBorderColor : tokens.uncheckedBorderColor,
|
|
16
|
+
inactiveThumbColor: disabled ? tokens.disabledThumbColor : tokens.uncheckedThumbColor
|
|
17
|
+
};
|
|
18
|
+
return {
|
|
19
|
+
animationDuration: tokens.animationDuration,
|
|
20
|
+
borderWidth: tokens.borderWidth,
|
|
21
|
+
colors,
|
|
22
|
+
loadingIndicatorColor: tokens.loadingIndicatorColor,
|
|
23
|
+
sizeTokens
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
export {
|
|
27
|
+
useSwitchTokens
|
|
28
|
+
};
|
|
29
|
+
//# sourceMappingURL=use-switch-tokens.hook.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/hooks/use-switch-tokens.hook.ts"],"sourcesContent":["import type { ComponentSize, ComponentVariant } from \"@impulse-ui-native/theme\";\nimport { useComponentsTokens } from \"@impulse-ui-native/theme\";\n\nexport interface SwitchAnimationColors {\n activeBackgroundColor: string;\n activeBorderColor: string;\n activeThumbColor: string;\n inactiveBackgroundColor: string;\n inactiveBorderColor: string;\n inactiveThumbColor: string;\n}\n\ninterface UseSwitchTokensOptions {\n disabled: boolean;\n size: ComponentSize;\n variant: ComponentVariant;\n}\n\nexport function useSwitchTokens({\n disabled,\n size,\n variant,\n}: UseSwitchTokensOptions) {\n const tokens = useComponentsTokens().switch;\n const sizeTokens = tokens.sizes[size];\n const variantTokens = tokens.variants[variant];\n\n const colors: SwitchAnimationColors = {\n activeBackgroundColor: disabled\n ? tokens.disabledBackgroundColor\n : variantTokens.backgroundColor,\n activeBorderColor: disabled\n ? tokens.disabledBorderColor\n : variantTokens.borderColor,\n activeThumbColor: disabled\n ? tokens.disabledThumbColor\n : variantTokens.color,\n inactiveBackgroundColor: disabled\n ? tokens.disabledBackgroundColor\n : tokens.uncheckedBackgroundColor,\n inactiveBorderColor: disabled\n ? tokens.disabledBorderColor\n : tokens.uncheckedBorderColor,\n inactiveThumbColor: disabled\n ? tokens.disabledThumbColor\n : tokens.uncheckedThumbColor,\n };\n\n return {\n animationDuration: tokens.animationDuration,\n borderWidth: tokens.borderWidth,\n colors,\n loadingIndicatorColor: tokens.loadingIndicatorColor,\n sizeTokens,\n };\n}\n"],"mappings":"AACA,SAAS,2BAA2B;AAiB7B,SAAS,gBAAgB;AAAA,EAC9B;AAAA,EACA;AAAA,EACA;AACF,GAA2B;AACzB,QAAM,SAAS,oBAAoB,EAAE;AACrC,QAAM,aAAa,OAAO,MAAM,IAAI;AACpC,QAAM,gBAAgB,OAAO,SAAS,OAAO;AAE7C,QAAM,SAAgC;AAAA,IACpC,uBAAuB,WACnB,OAAO,0BACP,cAAc;AAAA,IAClB,mBAAmB,WACf,OAAO,sBACP,cAAc;AAAA,IAClB,kBAAkB,WACd,OAAO,qBACP,cAAc;AAAA,IAClB,yBAAyB,WACrB,OAAO,0BACP,OAAO;AAAA,IACX,qBAAqB,WACjB,OAAO,sBACP,OAAO;AAAA,IACX,oBAAoB,WAChB,OAAO,qBACP,OAAO;AAAA,EACb;AAEA,SAAO;AAAA,IACL,mBAAmB,OAAO;AAAA,IAC1B,aAAa,OAAO;AAAA,IACpB;AAAA,IACA,uBAAuB,OAAO;AAAA,IAC9B;AAAA,EACF;AACF;","names":[]}
|
package/dist/index.d.mts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["export * from \"./components\";\nexport * from \"./types\";\n"],"mappings":"AAAA,cAAc;AACd,cAAc;","names":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/types/index.ts"],"sourcesContent":["export * from \"./switch.types\";\n"],"mappings":"AAAA,cAAc;","names":[]}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { PressableCoreProps } from '@impulse-ui-native/primitives';
|
|
2
|
+
import { ComponentSize, ComponentVariant } from '@impulse-ui-native/theme';
|
|
3
|
+
|
|
4
|
+
interface SwitchProps extends Omit<PressableCoreProps, "children"> {
|
|
5
|
+
checked?: boolean;
|
|
6
|
+
defaultChecked?: boolean;
|
|
7
|
+
onCheckedChange?: (checked: boolean) => void;
|
|
8
|
+
size?: ComponentSize;
|
|
9
|
+
variant?: ComponentVariant;
|
|
10
|
+
}
|
|
11
|
+
interface SwitchThemeProps {
|
|
12
|
+
size: ComponentSize;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export type { SwitchProps, SwitchThemeProps };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
//# sourceMappingURL=switch.types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
package/package.json
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@impulse-ui-native/switch",
|
|
3
|
+
"version": "2.7.0",
|
|
4
|
+
"description": "Animated switch controls for ImpulseUI Native applications.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"react-native",
|
|
7
|
+
"switch",
|
|
8
|
+
"toggle",
|
|
9
|
+
"form",
|
|
10
|
+
"components",
|
|
11
|
+
"impulse-ui-native"
|
|
12
|
+
],
|
|
13
|
+
"license": "MIT",
|
|
14
|
+
"source": "src/index.ts",
|
|
15
|
+
"module": "dist/index.js",
|
|
16
|
+
"types": "dist/index.d.mts",
|
|
17
|
+
"react-native": "src/index.ts",
|
|
18
|
+
"exports": {
|
|
19
|
+
".": {
|
|
20
|
+
"types": "./dist/index.d.mts",
|
|
21
|
+
"react-native": "./src/index.ts",
|
|
22
|
+
"import": "./dist/index.js"
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
"files": [
|
|
26
|
+
"dist",
|
|
27
|
+
"README.md",
|
|
28
|
+
"LICENSE"
|
|
29
|
+
],
|
|
30
|
+
"sideEffects": false,
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"@impulse-ui-native/primitives": "2.7.0",
|
|
33
|
+
"@impulse-ui-native/core": "2.7.0",
|
|
34
|
+
"@impulse-ui-native/theme": "2.7.0"
|
|
35
|
+
},
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"@types/react": "~19.2.14",
|
|
38
|
+
"eslint": "^9.38.0",
|
|
39
|
+
"prettier": "^3.6.2",
|
|
40
|
+
"react": "19.2.3",
|
|
41
|
+
"react-native": "0.86.2",
|
|
42
|
+
"react-native-reanimated": "4.5.1",
|
|
43
|
+
"react-native-worklets": "0.10.1",
|
|
44
|
+
"typescript": "~6.0.3",
|
|
45
|
+
"@impulse-ui-native/eslint-config": "1.0.0",
|
|
46
|
+
"@impulse-ui-native/prettier-config": "1.0.0",
|
|
47
|
+
"@impulse-ui-native/tsconfig": "1.0.0"
|
|
48
|
+
},
|
|
49
|
+
"peerDependencies": {
|
|
50
|
+
"react": ">=18",
|
|
51
|
+
"react-native": ">=0.73",
|
|
52
|
+
"react-native-reanimated": ">=3.6.0",
|
|
53
|
+
"react-native-worklets": ">=0.5"
|
|
54
|
+
},
|
|
55
|
+
"publishConfig": {
|
|
56
|
+
"access": "public"
|
|
57
|
+
},
|
|
58
|
+
"prettier": "@impulse-ui-native/prettier-config",
|
|
59
|
+
"scripts": {
|
|
60
|
+
"build": "tsup --config tsup.config.ts",
|
|
61
|
+
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
62
|
+
"lint": "pnpm exec eslint . --fix",
|
|
63
|
+
"format": "prettier --write \"./**/*.{ts,tsx,js,jsx,json,md}\" --ignore-path ../../.prettierignore"
|
|
64
|
+
}
|
|
65
|
+
}
|