@jamadd/react-native-template-ui 0.8.4 → 0.8.5
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/package.json +2 -1
- package/src/components/button/AnimatedPressable.tsx +7 -0
- package/src/components/button/AnimatedThemedPressable.tsx +12 -0
- package/src/components/button/ThemedButton.tsx +45 -0
- package/src/components/button/ThemedIconButton.tsx +33 -0
- package/src/components/button/ThemedIconTextButton.tsx +56 -0
- package/src/components/button/ThemedPressable.tsx +12 -0
- package/src/components/icon/ThemedIcon.tsx +31 -0
- package/src/components/list/List.tsx +55 -0
- package/src/components/loading/ThemedLoading.tsx +29 -0
- package/src/components/overlay/actionSheet/ActionSheet.tsx +31 -0
- package/src/components/overlay/actionSheet/ActionSheetContentGesture.tsx +15 -0
- package/src/components/overlay/actionSheet/ActionSheetHeader.tsx +72 -0
- package/src/components/overlay/actionSheet/ActionSheetListView.tsx +54 -0
- package/src/components/overlay/actionSheet/ActionSheetListViewContent.tsx +32 -0
- package/src/components/overlay/actionSheet/ActionSheetOptionList.tsx +51 -0
- package/src/components/overlay/actionSheet/ActionSheetOptionListItem.tsx +43 -0
- package/src/components/overlay/actionSheet/ActionSheetOptions.tsx +61 -0
- package/src/components/overlay/actionSheet/ActionSheetScrollView.tsx +57 -0
- package/src/components/overlay/actionSheet/ActionSheetScrollViewContent.tsx +38 -0
- package/src/components/overlay/actionSheet/ActionSheetWrap.tsx +75 -0
- package/src/components/overlay/alert/Alert.tsx +86 -0
- package/src/components/overlay/alert/AlertButton.tsx +41 -0
- package/src/components/overlay/alert/AlertButtons.tsx +32 -0
- package/src/components/overlay/alert/AlertContent.tsx +60 -0
- package/src/components/overlay/alert/AlertWrap.tsx +93 -0
- package/src/components/overlay/modal/ThemedLoadingModal.tsx +47 -0
- package/src/components/overlay/modal/ThemedModal.tsx +42 -0
- package/src/components/overlay/toast/ThemedToast.tsx +160 -0
- package/src/components/provider/GestureProvider.tsx +9 -0
- package/src/components/provider/InsetsProvider.tsx +11 -0
- package/src/components/provider/ReactNativeTemplateProviders.tsx +20 -0
- package/src/components/provider/ThemeProvider.tsx +8 -0
- package/src/components/separator/ThemedSeparator.tsx +24 -0
- package/src/components/slider/Slider.tsx +229 -0
- package/src/components/slider/ThemedSlider.tsx +103 -0
- package/src/components/slider/ThemedSliderStepIndicator.tsx +24 -0
- package/src/components/switch/ThemedSwitch.tsx +49 -0
- package/src/components/text/Text.tsx +7 -0
- package/src/components/text/ThemedIconText.tsx +58 -0
- package/src/components/text/ThemedText.tsx +7 -0
- package/src/components/view/AnimatedThemedView.tsx +18 -0
- package/src/components/view/AnimatedView.tsx +7 -0
- package/src/components/view/Box.tsx +7 -0
- package/src/components/view/ThemedScreenWrap.tsx +48 -0
- package/src/components/view/ThemedScreenWrap.web.tsx +41 -0
- package/src/components/view/ThemedScrollView.tsx +54 -0
- package/src/components/view/ThemedView.tsx +19 -0
- package/src/hooks/button.ts +152 -0
- package/src/hooks/list.ts +42 -0
- package/src/hooks/overlay.ts +471 -0
- package/src/hooks/react.ts +34 -0
- package/src/hooks/reanimated.ts +6 -0
- package/src/hooks/separator.ts +29 -0
- package/src/hooks/style.ts +79 -0
- package/src/hooks/switch.ts +119 -0
- package/src/hooks/theme.ts +65 -0
- package/src/hooks/view.ts +28 -0
- package/src/index.ts +59 -0
- package/src/test/utils/const.ts +9 -0
- package/src/test/utils/func.ts +55 -0
- package/src/test/utils/testingLib.tsx +17 -0
- package/src/types/button.ts +50 -0
- package/src/types/global.d.ts +5 -0
- package/src/types/icon.ts +21 -0
- package/src/types/list.ts +61 -0
- package/src/types/loading.ts +14 -0
- package/src/types/overlay.ts +216 -0
- package/src/types/provider.ts +6 -0
- package/src/types/react.ts +8 -0
- package/src/types/reanimated.ts +11 -0
- package/src/types/separator.ts +28 -0
- package/src/types/slider.ts +59 -0
- package/src/types/style.ts +26 -0
- package/src/types/switch.ts +31 -0
- package/src/types/text.ts +25 -0
- package/src/types/theme.ts +39 -0
- package/src/types/view.ts +58 -0
- package/src/utils/button/const.ts +10 -0
- package/src/utils/overlay/const.ts +77 -0
- package/src/utils/overlay/func.ts +107 -0
- package/src/utils/reanimated/func.ts +39 -0
- package/src/utils/slider/const.ts +23 -0
- package/src/utils/style/const.ts +4 -0
- package/src/utils/style/func.ts +37 -0
- package/src/utils/switch/const.ts +2 -0
- package/src/utils/theme/const.ts +189 -0
- package/src/utils/theme/restyle.ts +87 -0
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import { useEffect, type FC } from 'react';
|
|
2
|
+
import {
|
|
3
|
+
interpolate,
|
|
4
|
+
useAnimatedStyle,
|
|
5
|
+
withTiming,
|
|
6
|
+
} from 'react-native-reanimated';
|
|
7
|
+
import { scheduleOnRN } from 'react-native-worklets';
|
|
8
|
+
import { useAnimationSharedVal } from '../../../hooks/reanimated';
|
|
9
|
+
import { useInsetsStyle } from '../../../hooks/style';
|
|
10
|
+
import type { ThemedToastProps } from '../../../types/overlay';
|
|
11
|
+
import type { AnimatedThemedViewProps } from '../../../types/view';
|
|
12
|
+
import {
|
|
13
|
+
OverlayDismissResultType,
|
|
14
|
+
overlayMaxWidthPercent,
|
|
15
|
+
ToastDuration,
|
|
16
|
+
ToastType,
|
|
17
|
+
} from '../../../utils/overlay/const';
|
|
18
|
+
import ThemedText from '../../text/ThemedText';
|
|
19
|
+
import AnimatedThemedView from '../../view/AnimatedThemedView';
|
|
20
|
+
|
|
21
|
+
const ThemedToast: FC<ThemedToastProps> = ({
|
|
22
|
+
type = ToastType.Bottom,
|
|
23
|
+
duration = ToastDuration.Short,
|
|
24
|
+
onDismiss,
|
|
25
|
+
top: propsTop,
|
|
26
|
+
bottom: propsBottom,
|
|
27
|
+
left: propsLeft,
|
|
28
|
+
right: propsRight,
|
|
29
|
+
insetsPadding,
|
|
30
|
+
insetPaddingTop,
|
|
31
|
+
insetPaddingBottom,
|
|
32
|
+
insetPaddingLeft,
|
|
33
|
+
insetPaddingRight,
|
|
34
|
+
text,
|
|
35
|
+
textVariant = 'textS',
|
|
36
|
+
textFontSize,
|
|
37
|
+
textFontWeight,
|
|
38
|
+
textColor = 'textOverlay',
|
|
39
|
+
textStyle,
|
|
40
|
+
textProps,
|
|
41
|
+
...props
|
|
42
|
+
}) => {
|
|
43
|
+
const showSharedVal = useAnimationSharedVal();
|
|
44
|
+
const { paddingTop, paddingBottom, paddingLeft, paddingRight } =
|
|
45
|
+
useInsetsStyle({
|
|
46
|
+
insets: true,
|
|
47
|
+
insetsPadding: insetsPadding ?? 'm',
|
|
48
|
+
insetPaddingTop,
|
|
49
|
+
insetPaddingBottom,
|
|
50
|
+
insetPaddingLeft,
|
|
51
|
+
insetPaddingRight,
|
|
52
|
+
});
|
|
53
|
+
const animatedStyle = useAnimatedStyle(() => {
|
|
54
|
+
let outputY = 0;
|
|
55
|
+
switch (type) {
|
|
56
|
+
case ToastType.Top:
|
|
57
|
+
case ToastType.TopLeft:
|
|
58
|
+
case ToastType.TopRight:
|
|
59
|
+
if (paddingTop) {
|
|
60
|
+
outputY = -(paddingTop as number) * 2;
|
|
61
|
+
}
|
|
62
|
+
break;
|
|
63
|
+
case ToastType.Bottom:
|
|
64
|
+
case ToastType.BottomLeft:
|
|
65
|
+
case ToastType.BottomRight:
|
|
66
|
+
if (paddingBottom) {
|
|
67
|
+
outputY = (paddingBottom as number) * 2;
|
|
68
|
+
}
|
|
69
|
+
break;
|
|
70
|
+
default:
|
|
71
|
+
break;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return {
|
|
75
|
+
opacity: showSharedVal.value,
|
|
76
|
+
transform: [
|
|
77
|
+
{ translateY: interpolate(showSharedVal.value, [0, 1], [outputY, 0]) },
|
|
78
|
+
],
|
|
79
|
+
};
|
|
80
|
+
}, [type, showSharedVal, paddingTop, paddingBottom]);
|
|
81
|
+
useEffect(() => {
|
|
82
|
+
showSharedVal.set(withTiming(1));
|
|
83
|
+
const timeout = setTimeout(() => {
|
|
84
|
+
showSharedVal.set(
|
|
85
|
+
withTiming(0, undefined, () => {
|
|
86
|
+
if (onDismiss) {
|
|
87
|
+
scheduleOnRN(onDismiss, {
|
|
88
|
+
resultType: OverlayDismissResultType.Toast,
|
|
89
|
+
type,
|
|
90
|
+
text,
|
|
91
|
+
duration,
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
})
|
|
95
|
+
);
|
|
96
|
+
}, duration);
|
|
97
|
+
|
|
98
|
+
return () => {
|
|
99
|
+
clearTimeout(timeout);
|
|
100
|
+
};
|
|
101
|
+
}, [text]);
|
|
102
|
+
|
|
103
|
+
const top: AnimatedThemedViewProps['top'] = [
|
|
104
|
+
ToastType.Top,
|
|
105
|
+
ToastType.TopLeft,
|
|
106
|
+
ToastType.TopRight,
|
|
107
|
+
].includes(type)
|
|
108
|
+
? (propsTop ?? paddingTop)
|
|
109
|
+
: undefined;
|
|
110
|
+
const bottom: AnimatedThemedViewProps['bottom'] = [
|
|
111
|
+
ToastType.Bottom,
|
|
112
|
+
ToastType.BottomLeft,
|
|
113
|
+
ToastType.BottomRight,
|
|
114
|
+
].includes(type)
|
|
115
|
+
? (propsBottom ?? paddingBottom)
|
|
116
|
+
: undefined;
|
|
117
|
+
const left: AnimatedThemedViewProps['left'] = [
|
|
118
|
+
ToastType.TopLeft,
|
|
119
|
+
ToastType.BottomLeft,
|
|
120
|
+
].includes(type)
|
|
121
|
+
? (propsLeft ?? paddingLeft)
|
|
122
|
+
: undefined;
|
|
123
|
+
const right: AnimatedThemedViewProps['right'] = [
|
|
124
|
+
ToastType.TopRight,
|
|
125
|
+
ToastType.BottomRight,
|
|
126
|
+
].includes(type)
|
|
127
|
+
? (propsRight ?? paddingRight)
|
|
128
|
+
: undefined;
|
|
129
|
+
|
|
130
|
+
return (
|
|
131
|
+
<AnimatedThemedView
|
|
132
|
+
position={'absolute'}
|
|
133
|
+
top={top}
|
|
134
|
+
bottom={bottom}
|
|
135
|
+
left={left}
|
|
136
|
+
right={right}
|
|
137
|
+
alignSelf={'center'}
|
|
138
|
+
maxWidth={overlayMaxWidthPercent}
|
|
139
|
+
paddingVertical={'xxs'}
|
|
140
|
+
paddingHorizontal={'s'}
|
|
141
|
+
backgroundColor={'backgroundOverlay'}
|
|
142
|
+
animatedStyle={animatedStyle}
|
|
143
|
+
{...props}
|
|
144
|
+
>
|
|
145
|
+
<ThemedText
|
|
146
|
+
numberOfLines={1}
|
|
147
|
+
variant={textVariant}
|
|
148
|
+
fontSize={textFontSize}
|
|
149
|
+
fontWeight={textFontWeight}
|
|
150
|
+
color={textColor}
|
|
151
|
+
style={textStyle}
|
|
152
|
+
{...textProps}
|
|
153
|
+
>
|
|
154
|
+
{text}
|
|
155
|
+
</ThemedText>
|
|
156
|
+
</AnimatedThemedView>
|
|
157
|
+
);
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
export default ThemedToast;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { FC } from 'react';
|
|
2
|
+
import { GestureHandlerRootView } from 'react-native-gesture-handler';
|
|
3
|
+
import type { GestureHandlerRootViewProps } from 'react-native-gesture-handler/lib/typescript/components/GestureHandlerRootView';
|
|
4
|
+
|
|
5
|
+
const GestureProvider: FC<GestureHandlerRootViewProps> = (props) => (
|
|
6
|
+
<GestureHandlerRootView {...props} />
|
|
7
|
+
);
|
|
8
|
+
|
|
9
|
+
export default GestureProvider;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { FC } from 'react';
|
|
2
|
+
import {
|
|
3
|
+
SafeAreaProvider,
|
|
4
|
+
type SafeAreaProviderProps,
|
|
5
|
+
} from 'react-native-safe-area-context';
|
|
6
|
+
|
|
7
|
+
const InsetsProvider: FC<SafeAreaProviderProps> = (props) => (
|
|
8
|
+
<SafeAreaProvider {...props} />
|
|
9
|
+
);
|
|
10
|
+
|
|
11
|
+
export default InsetsProvider;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { FC } from 'react';
|
|
2
|
+
import type { ReactNativeTemplateProvidersProps } from '../../types/provider';
|
|
3
|
+
import GestureProvider from './GestureProvider';
|
|
4
|
+
import InsetsProvider from './InsetsProvider';
|
|
5
|
+
import ThemeProvider from './ThemeProvider';
|
|
6
|
+
|
|
7
|
+
const ReactNativeTemplateProviders: FC<ReactNativeTemplateProvidersProps> = ({
|
|
8
|
+
theme,
|
|
9
|
+
children,
|
|
10
|
+
}) => {
|
|
11
|
+
return (
|
|
12
|
+
<GestureProvider>
|
|
13
|
+
<InsetsProvider>
|
|
14
|
+
<ThemeProvider theme={theme}>{children}</ThemeProvider>
|
|
15
|
+
</InsetsProvider>
|
|
16
|
+
</GestureProvider>
|
|
17
|
+
);
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export default ReactNativeTemplateProviders;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { ThemeProvider as RestyleThemeProvider } from '@shopify/restyle';
|
|
2
|
+
import type { ComponentProps, FC } from 'react';
|
|
3
|
+
|
|
4
|
+
const ThemeProvider: FC<ComponentProps<typeof RestyleThemeProvider>> = (
|
|
5
|
+
props
|
|
6
|
+
) => <RestyleThemeProvider {...props} />;
|
|
7
|
+
|
|
8
|
+
export default ThemeProvider;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { type FC } from 'react';
|
|
2
|
+
import type { ThemedSeparatorProps } from '../../types/separator';
|
|
3
|
+
import { BorderSize } from '../../utils/theme/const';
|
|
4
|
+
import ThemedView from '../view/ThemedView';
|
|
5
|
+
|
|
6
|
+
const ThemedSeparator: FC<ThemedSeparatorProps> = ({
|
|
7
|
+
size = BorderSize.S,
|
|
8
|
+
vertical = false,
|
|
9
|
+
backgroundColor = 'separator',
|
|
10
|
+
bg,
|
|
11
|
+
...props
|
|
12
|
+
}) => {
|
|
13
|
+
return (
|
|
14
|
+
<ThemedView
|
|
15
|
+
alignSelf={'stretch'}
|
|
16
|
+
width={vertical ? size : undefined}
|
|
17
|
+
height={vertical ? undefined : size}
|
|
18
|
+
backgroundColor={bg ?? backgroundColor}
|
|
19
|
+
{...props}
|
|
20
|
+
/>
|
|
21
|
+
);
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export default ThemedSeparator;
|
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
import { useMappingHelper } from '@shopify/flash-list';
|
|
2
|
+
import { memo, useLayoutEffect, useMemo, useState, type FC } from 'react';
|
|
3
|
+
import { Gesture, GestureDetector } from 'react-native-gesture-handler';
|
|
4
|
+
import {
|
|
5
|
+
useAnimatedStyle,
|
|
6
|
+
useSharedValue,
|
|
7
|
+
withTiming,
|
|
8
|
+
} from 'react-native-reanimated';
|
|
9
|
+
import { scheduleOnRN } from 'react-native-worklets';
|
|
10
|
+
import { useWindowDimensionsWidth } from '../../hooks/style';
|
|
11
|
+
import { useViewRef } from '../../hooks/view';
|
|
12
|
+
import type {
|
|
13
|
+
NumberSlider,
|
|
14
|
+
SliderProps,
|
|
15
|
+
StringSlider,
|
|
16
|
+
} from '../../types/slider';
|
|
17
|
+
import {
|
|
18
|
+
sliderStepIndicatorDefaultWidth,
|
|
19
|
+
sliderThumbDefaultSize,
|
|
20
|
+
SliderTrackSize,
|
|
21
|
+
} from '../../utils/slider/const';
|
|
22
|
+
import AnimatedThemedView from '../view/AnimatedThemedView';
|
|
23
|
+
import ThemedView from '../view/ThemedView';
|
|
24
|
+
import ThemedSliderStepIndicator from './ThemedSliderStepIndicator';
|
|
25
|
+
|
|
26
|
+
const Slider: FC<SliderProps> = ({
|
|
27
|
+
range,
|
|
28
|
+
steps = 1,
|
|
29
|
+
stepIndicator,
|
|
30
|
+
stepIndicatorProps,
|
|
31
|
+
snapToStepAnimated = true,
|
|
32
|
+
onValueChange,
|
|
33
|
+
sliderWrapProps,
|
|
34
|
+
trackSize = SliderTrackSize.M,
|
|
35
|
+
trackActiveColor = 'themePri',
|
|
36
|
+
trackActiveProps,
|
|
37
|
+
trackInactiveColor = 'separator',
|
|
38
|
+
trackInactiveProps,
|
|
39
|
+
thumbSize = sliderThumbDefaultSize,
|
|
40
|
+
thumbColor = 'themePri',
|
|
41
|
+
thumbProps,
|
|
42
|
+
gestureActivateDuration = 40,
|
|
43
|
+
setSelectedVal,
|
|
44
|
+
selectedValSharedVal,
|
|
45
|
+
}) => {
|
|
46
|
+
const windowWidth = useWindowDimensionsWidth();
|
|
47
|
+
const isNumRange = typeof range[0] === 'number';
|
|
48
|
+
const processedRange = isNumRange ? range.sort() : range;
|
|
49
|
+
const totalSteps = isNumRange
|
|
50
|
+
? Math.abs(
|
|
51
|
+
(processedRange as NumberSlider['range'])[1] -
|
|
52
|
+
(processedRange as NumberSlider['range'])[0]
|
|
53
|
+
) / steps
|
|
54
|
+
: processedRange.length - 1;
|
|
55
|
+
const stepsFractionDigits = useMemo(() => {
|
|
56
|
+
if (!isNumRange || Number.isInteger(steps)) {
|
|
57
|
+
return 0;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const stepsStrSplit = steps.toString().split('.');
|
|
61
|
+
if (stepsStrSplit.length < 2) {
|
|
62
|
+
return 0;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return stepsStrSplit[1]?.length ?? 0;
|
|
66
|
+
}, [isNumRange, steps]);
|
|
67
|
+
const thumbSizeHalf = thumbSize / 2;
|
|
68
|
+
const { getMappingKey } = useMappingHelper();
|
|
69
|
+
const trackViewRef = useViewRef();
|
|
70
|
+
const [sliderWidth, setSliderWidth] = useState<number>();
|
|
71
|
+
const [trackWidth, setTrackWidth] = useState<number>();
|
|
72
|
+
const [stepWidth, setStepWidth] = useState<number>();
|
|
73
|
+
const xSharedVal = useSharedValue(-(trackWidth ?? 0));
|
|
74
|
+
const trackAnimatedStyle = useAnimatedStyle(
|
|
75
|
+
() => ({ transform: [{ translateX: xSharedVal.get() }] }),
|
|
76
|
+
[trackWidth]
|
|
77
|
+
);
|
|
78
|
+
const thumbAnimatedStyle = useAnimatedStyle(
|
|
79
|
+
() => ({ transform: [{ translateX: xSharedVal.get() - thumbSizeHalf }] }),
|
|
80
|
+
[thumbSizeHalf]
|
|
81
|
+
);
|
|
82
|
+
useLayoutEffect(() => {
|
|
83
|
+
trackViewRef.current?.measure((_x, _y, w) => {
|
|
84
|
+
const tempWidth = w - thumbSize;
|
|
85
|
+
const tempStepWidth = tempWidth / totalSteps;
|
|
86
|
+
setSliderWidth(w + thumbSize + thumbSizeHalf);
|
|
87
|
+
setTrackWidth(tempWidth);
|
|
88
|
+
setStepWidth(tempStepWidth);
|
|
89
|
+
let index = 0;
|
|
90
|
+
if (isNumRange) {
|
|
91
|
+
const numRange = processedRange as NumberSlider['range'];
|
|
92
|
+
const numCurrentValue =
|
|
93
|
+
selectedValSharedVal?.get() as NumberSlider['defaultValue'];
|
|
94
|
+
index = numCurrentValue ? (numCurrentValue - numRange[0]) / steps : 0;
|
|
95
|
+
} else {
|
|
96
|
+
const strRange = processedRange as StringSlider['range'];
|
|
97
|
+
const strCurrentValue =
|
|
98
|
+
selectedValSharedVal?.get() as StringSlider['defaultValue'];
|
|
99
|
+
index = strCurrentValue ? strRange.indexOf(strCurrentValue) : 0;
|
|
100
|
+
}
|
|
101
|
+
xSharedVal.set(-(totalSteps - index) * tempStepWidth);
|
|
102
|
+
});
|
|
103
|
+
}, [thumbSize, isNumRange, processedRange, totalSteps]);
|
|
104
|
+
|
|
105
|
+
const updateSelectedVal = (x: number) => {
|
|
106
|
+
if (!stepWidth) {
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
if (isNumRange) {
|
|
111
|
+
const numRange = processedRange as NumberSlider['range'];
|
|
112
|
+
const numIndex = Math.round(x / stepWidth);
|
|
113
|
+
const tempSelectedVal = Number(
|
|
114
|
+
(numRange[1] - numIndex * steps).toFixed(stepsFractionDigits)
|
|
115
|
+
);
|
|
116
|
+
setSelectedVal?.(tempSelectedVal);
|
|
117
|
+
onValueChange(tempSelectedVal);
|
|
118
|
+
} else {
|
|
119
|
+
const strRange = processedRange as StringSlider['range'];
|
|
120
|
+
const strIndex = Math.round(x / stepWidth);
|
|
121
|
+
const tempSelectedVal = strRange[totalSteps - strIndex];
|
|
122
|
+
setSelectedVal?.(tempSelectedVal);
|
|
123
|
+
onValueChange(tempSelectedVal!);
|
|
124
|
+
}
|
|
125
|
+
};
|
|
126
|
+
const updateXSharedVal = (x: number, animated = false) => {
|
|
127
|
+
'worklet';
|
|
128
|
+
const tempX = Math.min(0, Math.max(x, -(trackWidth ?? 0)));
|
|
129
|
+
xSharedVal.set(animated ? withTiming(tempX) : tempX);
|
|
130
|
+
scheduleOnRN(updateSelectedVal, Math.abs(tempX));
|
|
131
|
+
};
|
|
132
|
+
const touchStart = useSharedValue(0);
|
|
133
|
+
const gesture = Gesture.Simultaneous(
|
|
134
|
+
Gesture.LongPress()
|
|
135
|
+
.minDuration(gestureActivateDuration)
|
|
136
|
+
.maxDistance(windowWidth * 10)
|
|
137
|
+
.shouldCancelWhenOutside(false)
|
|
138
|
+
.onStart(({ x }) => {
|
|
139
|
+
if (sliderWidth) {
|
|
140
|
+
updateXSharedVal(-(sliderWidth - (x + thumbSize + thumbSizeHalf)));
|
|
141
|
+
}
|
|
142
|
+
})
|
|
143
|
+
.onEnd(({ x }) => {
|
|
144
|
+
if (sliderWidth && stepWidth) {
|
|
145
|
+
const nearestStep = Math.round(
|
|
146
|
+
-(sliderWidth - (x + thumbSize + thumbSizeHalf)) / stepWidth
|
|
147
|
+
);
|
|
148
|
+
updateXSharedVal(nearestStep * stepWidth, snapToStepAnimated);
|
|
149
|
+
}
|
|
150
|
+
}),
|
|
151
|
+
Gesture.Pan()
|
|
152
|
+
.manualActivation(true)
|
|
153
|
+
.shouldCancelWhenOutside(false)
|
|
154
|
+
.onBegin(() => {
|
|
155
|
+
touchStart.set(Date.now());
|
|
156
|
+
})
|
|
157
|
+
.onTouchesMove(({}, state) => {
|
|
158
|
+
const timeToCheck = Date.now() - touchStart.get();
|
|
159
|
+
if (timeToCheck >= gestureActivateDuration) {
|
|
160
|
+
state.activate();
|
|
161
|
+
} else {
|
|
162
|
+
state.fail();
|
|
163
|
+
}
|
|
164
|
+
})
|
|
165
|
+
.onChange(({ changeX }) => {
|
|
166
|
+
updateXSharedVal(xSharedVal.get() + changeX);
|
|
167
|
+
})
|
|
168
|
+
.onEnd(() => {
|
|
169
|
+
if (stepWidth) {
|
|
170
|
+
const nearestStep = Math.round(xSharedVal.get() / stepWidth);
|
|
171
|
+
updateXSharedVal(nearestStep * stepWidth, snapToStepAnimated);
|
|
172
|
+
}
|
|
173
|
+
})
|
|
174
|
+
);
|
|
175
|
+
|
|
176
|
+
return (
|
|
177
|
+
<GestureDetector gesture={gesture}>
|
|
178
|
+
<ThemedView
|
|
179
|
+
ref={trackViewRef}
|
|
180
|
+
alignItems={'center'}
|
|
181
|
+
justifyContent={'center'}
|
|
182
|
+
alignSelf={'stretch'}
|
|
183
|
+
height={thumbSize}
|
|
184
|
+
{...sliderWrapProps}
|
|
185
|
+
>
|
|
186
|
+
<ThemedView
|
|
187
|
+
position={'absolute'}
|
|
188
|
+
width={trackWidth}
|
|
189
|
+
height={trackSize}
|
|
190
|
+
overflow={'hidden'}
|
|
191
|
+
backgroundColor={trackInactiveColor}
|
|
192
|
+
{...trackActiveProps}
|
|
193
|
+
>
|
|
194
|
+
<AnimatedThemedView
|
|
195
|
+
height={trackSize}
|
|
196
|
+
backgroundColor={trackActiveColor}
|
|
197
|
+
animatedStyle={trackAnimatedStyle}
|
|
198
|
+
{...trackInactiveProps}
|
|
199
|
+
/>
|
|
200
|
+
</ThemedView>
|
|
201
|
+
{!!stepIndicator &&
|
|
202
|
+
processedRange.map((value, index) => (
|
|
203
|
+
<ThemedSliderStepIndicator
|
|
204
|
+
key={getMappingKey(value, index)}
|
|
205
|
+
left={
|
|
206
|
+
index * (stepWidth ?? 0) +
|
|
207
|
+
thumbSizeHalf -
|
|
208
|
+
sliderStepIndicatorDefaultWidth / 2
|
|
209
|
+
}
|
|
210
|
+
height={thumbSize}
|
|
211
|
+
{...stepIndicatorProps}
|
|
212
|
+
/>
|
|
213
|
+
))}
|
|
214
|
+
<AnimatedThemedView
|
|
215
|
+
position={'absolute'}
|
|
216
|
+
right={-thumbSizeHalf}
|
|
217
|
+
width={thumbSize}
|
|
218
|
+
aspectRatio={1}
|
|
219
|
+
opacity={0.8}
|
|
220
|
+
backgroundColor={thumbColor}
|
|
221
|
+
animatedStyle={thumbAnimatedStyle}
|
|
222
|
+
{...thumbProps}
|
|
223
|
+
/>
|
|
224
|
+
</ThemedView>
|
|
225
|
+
</GestureDetector>
|
|
226
|
+
);
|
|
227
|
+
};
|
|
228
|
+
|
|
229
|
+
export default memo(Slider);
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { memo, useLayoutEffect, type FC } from 'react';
|
|
2
|
+
import { useSharedValue } from 'react-native-reanimated';
|
|
3
|
+
import { useDeferredState } from '../../hooks/react';
|
|
4
|
+
import { useThemeSpacing } from '../../hooks/theme';
|
|
5
|
+
import type { SliderProps, ThemedSliderProps } from '../../types/slider';
|
|
6
|
+
import type { ThemedTextProps } from '../../types/text';
|
|
7
|
+
import type { ThemedViewProps } from '../../types/view';
|
|
8
|
+
import {
|
|
9
|
+
sliderThumbDefaultSize,
|
|
10
|
+
SliderValueDisplayMode,
|
|
11
|
+
} from '../../utils/slider/const';
|
|
12
|
+
import ThemedView from '../view/ThemedView';
|
|
13
|
+
import Slider from './Slider';
|
|
14
|
+
|
|
15
|
+
const ThemedText: FC<ThemedTextProps> = require('../text/ThemedText').default;
|
|
16
|
+
|
|
17
|
+
const MemoThemedText = memo(ThemedText);
|
|
18
|
+
|
|
19
|
+
const ThemedSlider: FC<ThemedSliderProps> = ({
|
|
20
|
+
defaultValue,
|
|
21
|
+
wrapProps: { paddingHorizontal, px, ...wrapProps } = {},
|
|
22
|
+
valueDisplayMode = SliderValueDisplayMode.None,
|
|
23
|
+
valueDisplayWrapProps,
|
|
24
|
+
valueTextProps,
|
|
25
|
+
valueDescription,
|
|
26
|
+
valueDescriptionTextProps,
|
|
27
|
+
...props
|
|
28
|
+
}) => {
|
|
29
|
+
const { thumbSize } = props;
|
|
30
|
+
const themeSpacing = useThemeSpacing();
|
|
31
|
+
|
|
32
|
+
const {
|
|
33
|
+
state: selectedVal,
|
|
34
|
+
setState: setSelectedVal,
|
|
35
|
+
deferredState: deferredSelectedValue,
|
|
36
|
+
} = useDeferredState(defaultValue);
|
|
37
|
+
const selectedValSharedVal = useSharedValue(selectedVal);
|
|
38
|
+
useLayoutEffect(() => {
|
|
39
|
+
selectedValSharedVal.set(selectedVal);
|
|
40
|
+
}, [selectedVal]);
|
|
41
|
+
|
|
42
|
+
const thumbSizeHalf = (thumbSize ?? sliderThumbDefaultSize) / 2;
|
|
43
|
+
const wrapStyle: ThemedViewProps['style'] = {
|
|
44
|
+
marginHorizontal:
|
|
45
|
+
typeof paddingHorizontal === 'string'
|
|
46
|
+
? themeSpacing[paddingHorizontal]
|
|
47
|
+
: typeof px === 'string'
|
|
48
|
+
? themeSpacing[px]
|
|
49
|
+
: thumbSizeHalf,
|
|
50
|
+
};
|
|
51
|
+
const isValueDisplayModeNone =
|
|
52
|
+
valueDisplayMode === SliderValueDisplayMode.None;
|
|
53
|
+
const isValueDisplayModeTop = valueDisplayMode === SliderValueDisplayMode.Top;
|
|
54
|
+
const isValueDisplayModeBottom =
|
|
55
|
+
valueDisplayMode === SliderValueDisplayMode.Bottom;
|
|
56
|
+
const valueDisplayComponent = !isValueDisplayModeNone && (
|
|
57
|
+
<ThemedView
|
|
58
|
+
flexDirection={'row'}
|
|
59
|
+
alignItems={'center'}
|
|
60
|
+
alignSelf={'stretch'}
|
|
61
|
+
{...valueDisplayWrapProps}
|
|
62
|
+
>
|
|
63
|
+
{!!valueDescription && (
|
|
64
|
+
<MemoThemedText
|
|
65
|
+
variant={'textS'}
|
|
66
|
+
flex={1}
|
|
67
|
+
textAlign={'center'}
|
|
68
|
+
{...valueDescriptionTextProps}
|
|
69
|
+
>
|
|
70
|
+
{valueDescription}
|
|
71
|
+
</MemoThemedText>
|
|
72
|
+
)}
|
|
73
|
+
<MemoThemedText
|
|
74
|
+
variant={'textSBold'}
|
|
75
|
+
flex={1}
|
|
76
|
+
textAlign={'center'}
|
|
77
|
+
{...valueTextProps}
|
|
78
|
+
>
|
|
79
|
+
{deferredSelectedValue}
|
|
80
|
+
</MemoThemedText>
|
|
81
|
+
</ThemedView>
|
|
82
|
+
);
|
|
83
|
+
const sliderProps = {
|
|
84
|
+
...props,
|
|
85
|
+
setSelectedVal,
|
|
86
|
+
selectedValSharedVal,
|
|
87
|
+
} as SliderProps;
|
|
88
|
+
|
|
89
|
+
return (
|
|
90
|
+
<ThemedView
|
|
91
|
+
paddingVertical={'s'}
|
|
92
|
+
gap={'xs'}
|
|
93
|
+
style={wrapStyle}
|
|
94
|
+
{...wrapProps}
|
|
95
|
+
>
|
|
96
|
+
{isValueDisplayModeTop && valueDisplayComponent}
|
|
97
|
+
<Slider {...sliderProps} />
|
|
98
|
+
{isValueDisplayModeBottom && valueDisplayComponent}
|
|
99
|
+
</ThemedView>
|
|
100
|
+
);
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
export default ThemedSlider;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/** @internal */
|
|
2
|
+
import type { FC } from 'react';
|
|
3
|
+
import { memo } from 'react';
|
|
4
|
+
import type { ThemedSliderStepIndicatorProps } from '../../types/slider';
|
|
5
|
+
import { sliderStepIndicatorDefaultWidth } from '../../utils/slider/const';
|
|
6
|
+
import ThemedView from '../view/ThemedView';
|
|
7
|
+
|
|
8
|
+
const ThemedSliderStepIndicator: FC<ThemedSliderStepIndicatorProps> = ({
|
|
9
|
+
width = sliderStepIndicatorDefaultWidth,
|
|
10
|
+
height,
|
|
11
|
+
...props
|
|
12
|
+
}) => {
|
|
13
|
+
return (
|
|
14
|
+
<ThemedView
|
|
15
|
+
position={'absolute'}
|
|
16
|
+
width={width}
|
|
17
|
+
height={height}
|
|
18
|
+
backgroundColor={'themePri'}
|
|
19
|
+
{...props}
|
|
20
|
+
/>
|
|
21
|
+
);
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export default memo(ThemedSliderStepIndicator);
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type { FC } from 'react';
|
|
2
|
+
import { useThemedSwitch } from '../../hooks/switch';
|
|
3
|
+
import type { ThemedSwitchProps } from '../../types/switch';
|
|
4
|
+
import { SwitchSize } from '../../utils/theme/const';
|
|
5
|
+
import AnimatedThemedPressable from '../button/AnimatedThemedPressable';
|
|
6
|
+
import AnimatedThemedView from '../view/AnimatedThemedView';
|
|
7
|
+
|
|
8
|
+
const ThemedSwitch: FC<ThemedSwitchProps> = ({
|
|
9
|
+
onPress,
|
|
10
|
+
enabled,
|
|
11
|
+
size = SwitchSize.M,
|
|
12
|
+
onPressDelayConfig,
|
|
13
|
+
customEnableAnimation,
|
|
14
|
+
customDisableAnimation,
|
|
15
|
+
customColors,
|
|
16
|
+
thumbProps,
|
|
17
|
+
style,
|
|
18
|
+
...props
|
|
19
|
+
}) => {
|
|
20
|
+
const { toggleSwitch, trackAnimatedStyle, thumbAnimatedStyle } =
|
|
21
|
+
useThemedSwitch(
|
|
22
|
+
onPress,
|
|
23
|
+
enabled,
|
|
24
|
+
size,
|
|
25
|
+
onPressDelayConfig,
|
|
26
|
+
customEnableAnimation,
|
|
27
|
+
customDisableAnimation,
|
|
28
|
+
customColors
|
|
29
|
+
);
|
|
30
|
+
|
|
31
|
+
return (
|
|
32
|
+
<AnimatedThemedPressable
|
|
33
|
+
onPress={toggleSwitch}
|
|
34
|
+
style={style}
|
|
35
|
+
animatedStyle={trackAnimatedStyle}
|
|
36
|
+
role={'switch'}
|
|
37
|
+
{...props}
|
|
38
|
+
>
|
|
39
|
+
<AnimatedThemedView
|
|
40
|
+
width={size}
|
|
41
|
+
aspectRatio={1}
|
|
42
|
+
animatedStyle={thumbAnimatedStyle}
|
|
43
|
+
{...thumbProps}
|
|
44
|
+
/>
|
|
45
|
+
</AnimatedThemedPressable>
|
|
46
|
+
);
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
export default ThemedSwitch;
|