Skip to content

Commit 5274a2c

Browse files
committed
feat(switch): ⚡ add Haptic feedback prop
1 parent 9b0c705 commit 5274a2c

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

src/components/switch/Switch.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { useControllableState } from "@chakra-ui/hooks";
1717

1818
import { AnimatedBox, Box, BoxProps, Text } from "../../primitives";
1919
import { getTextFontFamily, useTailwind, useTheme } from "../../theme";
20-
import { cx, styleAdapter } from "../../utils";
20+
import { cx, styleAdapter, useHaptic } from "../../utils";
2121
import { createComponent } from "../../utils/createComponent";
2222

2323
export type SwitchSize = "sm" | "md" | "lg" | "xl";
@@ -85,6 +85,12 @@ export interface SwitchProps extends BoxProps {
8585
* The Description of the switch component.
8686
*/
8787
description: string | null;
88+
/**
89+
* When set to true, The Tap creates a Touch Feedback
90+
* Check more -> https://docs.expo.dev/versions/latest/sdk/haptics/
91+
* @default true
92+
*/
93+
hapticEnabled: boolean;
8894
}
8995

9096
const SPRING_CONFIG = {
@@ -115,6 +121,7 @@ const RNSwitch: React.FC<Partial<SwitchProps>> = forwardRef<
115121
onStatePressedColor: onStatePressedColorFromProps,
116122
thumbTintColor: thumbTintColorFromProps,
117123
themeColor = "base",
124+
hapticEnabled = true,
118125
label,
119126
description,
120127
style,
@@ -128,6 +135,7 @@ const RNSwitch: React.FC<Partial<SwitchProps>> = forwardRef<
128135
});
129136

130137
const thumbAnimated = useSharedValue(switchState ? 1 : 0);
138+
const { hapticSelection } = useHaptic();
131139
/**
132140
* Setting Active/Inactive and Default Colors
133141
*/
@@ -283,6 +291,7 @@ const RNSwitch: React.FC<Partial<SwitchProps>> = forwardRef<
283291
thumbAnimated.value = withSpring(1, SPRING_CONFIG);
284292
}
285293
runOnJS(setSwitchState)(!switchState);
294+
hapticEnabled && runOnJS(hapticSelection)();
286295
})
287296
.onTouchesCancelled(() => {
288297
if (switchState) {

0 commit comments

Comments
 (0)