react-native-gesture-handler#GestureHandlerRootView TypeScript Examples
The following examples show how to use
react-native-gesture-handler#GestureHandlerRootView.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: index.tsx From jellyfin-audio-player with MIT License | 6 votes |
function MusicStack() {
const defaultStyles = useDefaultStyles();
return (
<GestureHandlerRootView style={{ flex: 1 }}>
<Stack.Navigator initialRouteName="RecentAlbums" screenOptions={{
headerTintColor: THEME_COLOR,
headerTitleStyle: defaultStyles.stackHeader,
cardStyle: defaultStyles.view,
}}>
<Stack.Screen name="RecentAlbums" component={RecentAlbums} options={{ headerTitle: t('recent-albums') }} />
<Stack.Screen name="Albums" component={Albums} options={{ headerTitle: t('albums') }} />
<Stack.Screen name="Album" component={Album} options={{ headerTitle: t('album') }} />
<Stack.Screen name="Playlists" component={Playlists} options={{ headerTitle: t('playlists') }} />
<Stack.Screen name="Playlist" component={Playlist} options={{ headerTitle: t('playlist') }} />
</Stack.Navigator>
<NowPlaying />
</GestureHandlerRootView>
);
}
Example #2
Source File: index.tsx From jellyfin-audio-player with MIT License | 6 votes |
export default function Player() {
return (
<GestureHandlerRootView style={{ flex: 1 }}>
<Queue header={(
<>
<NowPlaying />
<ConnectionNotice />
<StreamStatus />
<ProgressBar />
<MediaControls />
</>
)} />
</GestureHandlerRootView>
);
}
Example #3
Source File: App.tsx From react-native-wagmi-charts with MIT License | 5 votes |
export default function App() {
const [selected, setSelected] = React.useState('');
return (
<GestureHandlerRootView style={{ flex: 1 }}>
<BumbagNativeProvider>
<Box.Safe flex="1">
<Level
verticalBelow=""
paddingX="major-2"
paddingY="major-2"
alignY="center"
>
<Heading.H5 key={'heading'}>
React Native WAGMI Charts ?
</Heading.H5>
{selected ? (
<Button size="small" onPress={() => setSelected('')}>
Back
</Button>
) : null}
</Level>
<Box.Scroll>
{!selected && (
<Box paddingX="major-2" marginTop="major-6">
<Heading.H6 marginBottom="major-2">
Click a chart below to get started
</Heading.H6>
<Button onPress={() => setSelected('candlestick')}>
<Text>Candlestick</Text>
</Button>
<Button onPress={() => setSelected('line')}>
<Text>Line</Text>
</Button>
</Box>
)}
<Box marginTop="major-2">
{selected === 'candlestick' && <CandlestickChart />}
{selected === 'line' && <LineChart />}
</Box>
</Box.Scroll>
</Box.Safe>
</BumbagNativeProvider>
</GestureHandlerRootView>
);
}
Example #4
Source File: RangeSlider.tsx From react-native-range-slider-expo with MIT License | 4 votes |
RangeSlider = memo(({
min, max, fromValueOnChange, toValueOnChange,
step = 1,
styleSize = 'medium',
fromKnobColor = '#00a2ff',
toKnobColor = '#00a2ff',
inRangeBarColor = 'rgb(100,100,100)',
outOfRangeBarColor = 'rgb(200,200,200)',
valueLabelsBackgroundColor = '#3a4766',
rangeLabelsTextColor = 'rgb(60,60,60)',
showRangeLabels = true,
showValueLabels = true,
initialFromValue,
initialToValue,
knobSize: _knobSize,
knobBubbleTextStyle = {},
containerStyle: customContainerStyle = {},
barHeight: customBarHeight,
labelFormatter,
}: SliderProps) => {
// settings
const [wasInitialized, setWasInitialized] = useState(false);
const [knobSize, setknobSize] = useState(0);
const [barHeight, setBarHeight] = useState(0);
const [stepInPixels, setStepInPixels] = useState(0);
// rtl settings
const [flexDirection, setFlexDirection] = useState<"row" | "row-reverse" | "column" | "column-reverse" | undefined>('row');
const [fromValueOffset, setFromValueOffset] = useState(0);
const [toValueOffset, setToValueOffset] = useState(0);
const [sliderWidth, setSliderWidth] = useState(0);
const [fromElevation, setFromElevation] = useState(3);
const [toElevation, setToElevation] = useState(3);
// animation values
const [translateXfromValue] = useState(new Animated.Value(0));
const [translateXtoValue] = useState(new Animated.Value(0));
const [fromValueScale] = useState(new Animated.Value(0.01));
const [toValueScale] = useState(new Animated.Value(0.01));
const [rightBarScaleX] = useState(new Animated.Value(0.01));
const [leftBarScaleX] = useState(new Animated.Value(0.01));
// refs
const toValueTextRef = React.createRef<TextInput>();
const fromValueTextRef = React.createRef<TextInput>();
const opacity = React.useRef<Animated.Value>(new Animated.Value(0)).current;
const {formatLabel, decimalRound} = useUtils({step, labelFormatter});
// initalizing settings
useEffect(() => {
setFlexDirection(osRtl ? 'row-reverse' : 'row');
}, [knobSize]);
useEffect(() => {
if (wasInitialized) {
const stepSize = setStepSize(max, min, step);
fromValueTextRef.current?.setNativeProps({ text: formatLabel(min) });
toValueTextRef.current?.setNativeProps({ text: formatLabel(min) });
if (typeof initialFromValue === 'number' && initialFromValue >= min && initialFromValue <= max) {
const offset = ((initialFromValue - min) / step) * stepSize - (knobSize / 2);
setFromValueStatic(offset, knobSize, stepSize);
setValueText(offset + knobSize, true);
}
if (typeof initialToValue === 'number' && initialToValue >= min && initialToValue <= max && typeof initialFromValue === 'number' && initialToValue > initialFromValue) {
const offset = ((initialToValue - min) / step) * stepSize - (knobSize / 2);
setToValueStatic(offset, knobSize, stepSize);
setValueText(offset, false);
}
Animated.timing(opacity, {
toValue: 1,
duration: 64,
useNativeDriver: true
}).start();
}
}, [min, max, step, initialFromValue, initialToValue, wasInitialized]);
useEffect(() => {
const sizeBasedOnStyleSize = typeof styleSize === 'number' ? styleSize : styleSize === 'small' ? SMALL_SIZE : styleSize === 'medium' ? MEDIUM_SIZE : LARGE_SIZE;
const size = _knobSize ?? sizeBasedOnStyleSize;
setknobSize(customBarHeight ? Math.max(customBarHeight, size) : size);
setBarHeight(customBarHeight ?? sizeBasedOnStyleSize / 3)
translateXfromValue.setValue(-size / 4);
}, [styleSize, customBarHeight]);
// initalizing settings helpers
const setFromValueStatic = (newOffset: number, knobSize: number, stepInPixels: number) => {
newOffset = Math.floor((newOffset + (knobSize / 2)) / stepInPixels) * stepInPixels - (knobSize / 2);
setFromValue(newOffset);
setFromValueOffset(newOffset);
const changeTo = Math.floor(((newOffset + (knobSize / 2)) * (max - min) / sliderWidth) / step) * step + min;
fromValueOnChange(decimalRound(changeTo));
}
const setFromValue = (newOffset: number) => {
translateXfromValue.setValue(newOffset);
leftBarScaleX.setValue((newOffset + (knobSize / 2)) / sliderWidth + 0.01);
}
const setToValueStatic = (newOffset: number, knobSize: number, stepInPixels: number) => {
newOffset = Math.ceil((newOffset + (knobSize / 2)) / stepInPixels) * stepInPixels - (knobSize / 2);
setToValue(newOffset);
setToValueOffset(newOffset);
const changeTo = Math.ceil(((newOffset + (knobSize / 2)) * (max - min) / sliderWidth) / step) * step + min;
toValueOnChange(decimalRound(changeTo));
}
const setToValue = (newOffset: number) => {
translateXtoValue.setValue(newOffset);
rightBarScaleX.setValue(1.01 - ((newOffset + (knobSize / 2)) / sliderWidth));
}
const setStepSize = (max: number, min: number, step: number) => {
const numberOfSteps = ((max - min) / step);
const stepSize = sliderWidth / numberOfSteps;
setStepInPixels(stepSize);
return stepSize;
}
const setValueText = (totalOffset: number, from = true) => {
const isFrom = from && fromValueTextRef != null;
const isTo = !from && toValueTextRef != null;
if (isFrom || isTo) {
const numericValue: number = Math[isFrom ? 'floor' : 'ceil'](((totalOffset + (knobSize / 2)) * (max - min) / sliderWidth) / step) * step + min;
const text = formatLabel(numericValue);
(isFrom ? fromValueTextRef : toValueTextRef).current?.setNativeProps({ text });
}
}
// from value gesture events ------------------------------------------------------------------------
const onGestureEventFromValue = (event: PanGestureHandlerGestureEvent) => {
let totalOffset = event.nativeEvent.translationX + fromValueOffset;
if (totalOffset >= -knobSize / 2 && totalOffset < toValueOffset) {
translateXfromValue.setValue(totalOffset);
setValueText(totalOffset, true);
leftBarScaleX.setValue((totalOffset + (knobSize / 2)) / sliderWidth + 0.01);
}
}
const onHandlerStateChangeFromValue = (event: PanGestureHandlerGestureEvent) => {
if (event.nativeEvent.state === State.BEGAN) {
scaleTo(fromValueScale, 1);
setElevations(6, 5);
}
if (event.nativeEvent.state === State.END) {
let newOffset = event.nativeEvent.translationX + fromValueOffset;
newOffset = Math.floor((newOffset + (knobSize / 2)) / stepInPixels) * stepInPixels - (knobSize / 2);
if (newOffset < -knobSize / 2) {
newOffset = -knobSize / 2;
} else if (newOffset >= toValueOffset) {
newOffset = toValueOffset - stepInPixels;
}
setFromValueStatic(newOffset, knobSize, stepInPixels)
scaleTo(fromValueScale, 0.01);
}
}
// ------------------------------------------------------------------------------------------------
// to value gesture events ------------------------------------------------------------------------
const onGestureEventToValue = (event: PanGestureHandlerGestureEvent) => {
const totalOffset = event.nativeEvent.translationX + toValueOffset;
if (totalOffset <= sliderWidth - knobSize / 2 && totalOffset > fromValueOffset) {
translateXtoValue.setValue(totalOffset);
setValueText(totalOffset, false);
rightBarScaleX.setValue(1.01 - ((totalOffset + (knobSize / 2)) / sliderWidth));
}
}
const onHandlerStateChangeToValue = (event: PanGestureHandlerGestureEvent) => {
if (event.nativeEvent.state === State.BEGAN) {
scaleTo(toValueScale, 1);
setElevations(5, 6);
}
if (event.nativeEvent.state === State.END) {
let newOffset = event.nativeEvent.translationX + toValueOffset;
newOffset = Math.ceil((newOffset + (knobSize / 2)) / stepInPixels) * stepInPixels - (knobSize / 2);
if (newOffset > sliderWidth - knobSize / 2) {
newOffset = sliderWidth - knobSize / 2;
} else if (newOffset <= fromValueOffset) {
newOffset = fromValueOffset + stepInPixels;
}
setToValueOffset(newOffset);
translateXtoValue.setValue(newOffset);
rightBarScaleX.setValue(1.01 - ((newOffset + (knobSize / 2)) / sliderWidth));
scaleTo(toValueScale, 0.01);
const changeTo = Math.ceil(((newOffset + (knobSize / 2)) * (max - min) / sliderWidth) / step) * step + min;
toValueOnChange(decimalRound(changeTo));
}
}
// ------------------------------------------------------------------------------------------------
// gesture events help functions ------------------------------------------------------------------
const scaleTo = (param: Animated.Value, toValue: number) => Animated.timing(param, {
toValue,
duration: 150,
useNativeDriver: true
}).start();
const setElevations = (fromValue: number, toValue: number) => {
setFromElevation(fromValue);
setToElevation(toValue)
}
// ------------------------------------------------------------------------------------------------
// setting bar width ------------------------------------------------------------------------------
const onLayout = (event: LayoutChangeEvent) => {
if (wasInitialized === false) {
const { width } = event.nativeEvent.layout;
setSliderWidth(width);
translateXtoValue.setValue(width - knobSize / 2);
setToValueOffset(width - knobSize / 2);
setWasInitialized(true);
}
}
// ------------------------------------------------------------------------------------------------
const padding = useMemo(() => styleSize === 'large' ? 17 : styleSize === 'medium' ? 24 : 31, [styleSize]);
return (
<GestureHandlerRootView>
<Animated.View style={[styles.container, { opacity, padding }, customContainerStyle]}>
{
showValueLabels &&
<View style={{ width: '100%', height: 1, flexDirection }}>
<KnobBubble {...{ knobSize, valueLabelsBackgroundColor }}
translateX={translateXfromValue}
scale={fromValueScale}
textInputRef={fromValueTextRef}
textStyle={knobBubbleTextStyle}
/>
<KnobBubble {...{ knobSize, valueLabelsBackgroundColor }}
translateX={translateXtoValue}
scale={toValueScale}
textInputRef={toValueTextRef}
textStyle={knobBubbleTextStyle}
/>
</View>
}
<View style={{ width: '100%', height: knobSize, marginVertical: 4, position: 'relative', flexDirection, alignItems: 'center' }}>
<View style={{ position: 'absolute', backgroundColor: inRangeBarColor, left: knobSize / 4, marginLeft: -knobSize / 4, right: knobSize / 4, height: barHeight }} onLayout={onLayout} />
<Animated.View style={{ position: 'absolute', left: knobSize / 4, marginLeft: -knobSize / 4, right: knobSize / 4, height: barHeight, backgroundColor: outOfRangeBarColor, transform: [{ translateX: sliderWidth / 2 }, { scaleX: rightBarScaleX }, { translateX: -sliderWidth / 2 }] }} />
<Animated.View style={{ position: 'absolute', left: -knobSize / 4, width: knobSize / 2, height: barHeight, borderRadius: barHeight, backgroundColor: outOfRangeBarColor }} />
<Animated.View style={{ width: sliderWidth, height: barHeight, backgroundColor: outOfRangeBarColor, transform: [{ translateX: -sliderWidth / 2 }, { scaleX: leftBarScaleX }, { translateX: sliderWidth / 2 }] }} />
<Animated.View style={{ position: 'absolute', left: sliderWidth - knobSize / 4, width: knobSize / 2, height: barHeight, borderRadius: barHeight, backgroundColor: outOfRangeBarColor }} />
<PanGestureHandler onGestureEvent={onGestureEventFromValue} onHandlerStateChange={onHandlerStateChangeFromValue}>
<Animated.View style={[styles.knob, { height: knobSize, width: knobSize, borderRadius: knobSize, backgroundColor: fromKnobColor, elevation: fromElevation, transform: [{ translateX: translateXfromValue }] }]} />
</PanGestureHandler>
<PanGestureHandler onGestureEvent={onGestureEventToValue} onHandlerStateChange={onHandlerStateChangeToValue}>
<Animated.View style={[styles.knob, { height: knobSize, width: knobSize, borderRadius: knobSize, backgroundColor: toKnobColor, elevation: toElevation, transform: [{ translateX: translateXtoValue }] }]} />
</PanGestureHandler>
</View>
{
showRangeLabels &&
<View style={{ width: '100%', flexDirection, justifyContent: 'space-between' }}>
<Text style={{ color: rangeLabelsTextColor, fontWeight: "bold", fontSize }}>{min}</Text>
<Text style={{ color: rangeLabelsTextColor, fontWeight: "bold", fontSize }}>{max}</Text>
</View>
}
</Animated.View>
</GestureHandlerRootView>
);
})
Example #5
Source File: Slider.tsx From react-native-range-slider-expo with MIT License | 4 votes |
Slider = gestureHandlerRootHOC(({
min, max, valueOnChange,
step = 1,
styleSize = 'medium',
knobColor = '#00a2ff',
inRangeBarColor = 'rgb(200,200,200)',
outOfRangeBarColor = 'rgb(100,100,100)',
knobBubbleTextStyle = {},
valueLabelsBackgroundColor = '#3a4766',
rangeLabelsTextColor = 'rgb(60,60,60)',
showRangeLabels = true,
showValueLabels = true,
initialValue,
containerStyle: customContainerStyle = {},
labelFormatter,
}: SliderProps) => {
// settings
const [stepInPixels, setStepInPixels] = useState(0);
const [knobSize, setknobSize] = useState(0);
const [fontSize] = useState(15);
// rtl settings
const [flexDirection, setFlexDirection] = useState<"row" | "row-reverse" | "column" | "column-reverse" | undefined>('row');
const [svgOffset, setSvgOffset] = useState<object>({ left: (knobSize - 40) / 2 });
const [valueOffset, setValueOffset] = useState(0);
const [sliderWidth, setSliderWidth] = useState(0);
// animation values
const [translateX] = useState(new Animated.Value(0));
const [valueLabelScale] = useState(new Animated.Value(0.01));
const [inRangeScaleX] = useState(new Animated.Value(0.01));
// refs
const valueTextRef = React.createRef<TextInput>();
const opacity = React.useRef<Animated.Value>(new Animated.Value(0)).current;
const {decimalRound, formatLabel} = useUtils({step, labelFormatter});
// initalizing settings
useEffect(() => {
setFlexDirection(osRtl ? 'row-reverse' : 'row');
setSvgOffset(osRtl ? { right: (knobSize - 40) / 2 } : { left: (knobSize - 40) / 2 });
}, [knobSize]);
useEffect(() => {
if (sliderWidth > 0) {
const stepSize = setStepSize(max, min, step);
valueTextRef.current?.setNativeProps({ text: formatLabel(min) });
if (typeof initialValue === 'number' && initialValue >= min && initialValue <= max) {
const offset = ((initialValue - min) / step) * stepSize - (knobSize / 2);
setValueStatic(offset, knobSize, stepSize);
setValueText(offset);
}
Animated.timing(opacity, {
toValue: 1,
duration: 64,
useNativeDriver: true
}).start();
}
}, [min, max, step, initialValue, sliderWidth]);
useEffect(() => {
const size = typeof styleSize === 'number' ? styleSize : styleSize === 'small' ? SMALL_SIZE : styleSize === 'medium' ? MEDIUM_SIZE : LARGE_SIZE;
setknobSize(size);
translateX.setValue(-size / 4);
}, [styleSize]);
const setValueStatic = (newOffset: number, knobSize: number, stepInPixels: number) => {
newOffset = Math.round((newOffset + (knobSize / 2)) / stepInPixels) * stepInPixels - (knobSize / 2);
settingValue(newOffset);
setValueOffset(newOffset);
const changeTo = Math.round(((newOffset + (knobSize / 2)) * (max - min) / sliderWidth) / step) * step + min;
valueOnChange(decimalRound(changeTo));
}
const settingValue = (newOffset: number) => {
translateX.setValue(newOffset);
inRangeScaleX.setValue((newOffset + (knobSize / 2)) / sliderWidth + 0.01);
}
const setValueText = (totalOffset: number) => {
const numericValue: number = Math.floor(((totalOffset + (knobSize / 2)) * (max - min) / sliderWidth) / step) * step + min;
const text = formatLabel(numericValue);
valueTextRef.current?.setNativeProps({ text });
}
const setStepSize = (max: number, min: number, step: number) => {
const numberOfSteps = ((max - min) / step);
const stepSize = sliderWidth / numberOfSteps;
setStepInPixels(stepSize);
return stepSize;
}
// value gesture events ------------------------------------------------------------------------
const onGestureEvent = (event: PanGestureHandlerGestureEvent) => {
let totalOffset = event.nativeEvent.translationX + valueOffset;
if (totalOffset >= - knobSize / 2 && totalOffset <= sliderWidth - knobSize / 2) {
translateX.setValue(totalOffset);
if (valueTextRef != null) {
const labelValue = Math.round(((totalOffset + (knobSize / 2)) * (max - min) / sliderWidth) / step) * step + min;
valueTextRef.current?.setNativeProps({ text: formatLabel(labelValue) });
}
inRangeScaleX.setValue((totalOffset + (knobSize / 2)) / sliderWidth + 0.01);
}
}
const onHandlerStateChange = (event: PanGestureHandlerGestureEvent) => {
if (event.nativeEvent.state === State.BEGAN) {
scaleTo(valueLabelScale, 1);
}
if (event.nativeEvent.state === State.END) {
let newOffset = event.nativeEvent.translationX + valueOffset;
newOffset = Math.round((newOffset + (knobSize / 2)) / stepInPixels) * stepInPixels - (knobSize / 2);
if (newOffset < -knobSize / 2) {
newOffset = -knobSize / 2;
} else if (newOffset >= sliderWidth - knobSize / 2) {
newOffset = sliderWidth - knobSize / 2;
}
setValueStatic(newOffset, knobSize, stepInPixels);
scaleTo(valueLabelScale, 0.01);
}
}
// ------------------------------------------------------------------------------------------------
// gesture events help functions ------------------------------------------------------------------
const scaleTo = (param: Animated.Value, toValue: number) => Animated.timing(param,
{
toValue,
duration: 150,
useNativeDriver: true
}
).start();
// ------------------------------------------------------------------------------------------------
// setting bar width ------------------------------------------------------------------------------
const onLayout = (event: LayoutChangeEvent) => {
setSliderWidth(event.nativeEvent.layout.width);
}
// ------------------------------------------------------------------------------------------------
const padding = useMemo(() => styleSize === 'large' ? 17 : styleSize === 'medium' ? 24 : 31, [styleSize]);
return (
<GestureHandlerRootView>
<Animated.View style={[styles.container, { opacity, padding }, customContainerStyle]}>
{
showValueLabels &&
<View style={{ width: '100%', height: 1, flexDirection }}>
<Animated.View
style={{ position: 'absolute', bottom: 0, left: 0, transform: [{ translateX }, { scale: valueLabelScale }] }}
>
<Svg width={40} height={56} style={{ ...svgOffset, justifyContent: 'center', alignItems: 'center' }} >
<Path
d="M20.368027196163986,55.24077513402203 C20.368027196163986,55.00364778429386 37.12897994729114,42.11537830086061 39.19501224411266,22.754628132990383 C41.26104454093417,3.393877965120147 24.647119286738516,0.571820003300814 20.368027196163986,0.7019902620266703 C16.088935105589453,0.8321519518460209 -0.40167016290734386,3.5393865664909434 0.7742997013327574,21.806127302984205 C1.950269565572857,40.07286803947746 20.368027196163986,55.4779024837502 20.368027196163986,55.24077513402203 z"
strokeWidth={1}
fill={valueLabelsBackgroundColor}
stroke={valueLabelsBackgroundColor}
/>
</Svg>
<TextInput style={[styles.knobBubbleText, svgOffset, knobBubbleTextStyle]} ref={valueTextRef} />
</Animated.View>
</View>
}
<View style={{ width: '100%', height: knobSize, marginVertical: 4, position: 'relative', flexDirection, alignItems: 'center' }}>
<View style={[styles.bar, { backgroundColor: inRangeBarColor, left: knobSize / 4, marginLeft: -knobSize / 4, right: knobSize / 4, height: knobSize / 3 }]} onLayout={onLayout} />
<Animated.View style={{ width: sliderWidth, height: knobSize / 3, backgroundColor: outOfRangeBarColor, transform: [{ translateX: -sliderWidth / 2 }, { scaleX: inRangeScaleX }, { translateX: sliderWidth / 2 }] }} />
<Animated.View style={{ position: 'absolute', left: -knobSize / 4, width: knobSize / 2.5, height: knobSize / 3, borderRadius: knobSize / 3, backgroundColor: outOfRangeBarColor }} />
<PanGestureHandler {...{ onGestureEvent, onHandlerStateChange }}>
<Animated.View style={[styles.knob, { height: knobSize, width: knobSize, borderRadius: knobSize, backgroundColor: knobColor, transform: [{ translateX }] }]} />
</PanGestureHandler>
</View>
{
showRangeLabels &&
<View style={{ width: '100%', flexDirection, justifyContent: 'space-between' }}>
<Text style={{ color: rangeLabelsTextColor, fontWeight: "bold", fontSize, marginLeft: -7 }}>{min}</Text>
<Text style={{ color: rangeLabelsTextColor, fontWeight: "bold", fontSize, marginRight: 7 }}>{max}</Text>
</View>
}
</Animated.View>
</GestureHandlerRootView>
);
})
Example #6
Source File: TextSlider.tsx From react-native-range-slider-expo with MIT License | 4 votes |
TextualSlider = gestureHandlerRootHOC(({
values, valueOnChange,
styleSize = 'medium',
knobColor = '#00a2ff',
inRangeBarColor = 'rgb(200,200,200)',
outOfRangeBarColor = 'rgb(100,100,100)',
valueLabelsTextColor = 'white',
valueLabelsBackgroundColor = '#3a4766',
rangeLabelsStyle,
showRangeLabels = true,
showValueLabels = true,
initialValue
}: TextualSliderProps) => {
// settings
const [stepInPixels, setStepInPixels] = useState(0);
const [knobSize, setknobSize] = useState(0);
const [max, setMax] = useState(1);
// rtl settings
const [flexDirection, setFlexDirection] = useState<"row" | "row-reverse" | "column" | "column-reverse" | undefined>('row');
const [svgOffset, setSvgOffset] = useState<object>({ left: (knobSize - 40) / 2 });
const [valueOffset, setValueOffset] = useState(0);
const [TextualSliderWidth, setTextualSliderWidth] = useState(0);
// animation values
const [translateX] = useState(new Animated.Value(0));
const [valueLabelScale] = useState(new Animated.Value(0.01));
const [inRangeScaleX] = useState(new Animated.Value(0.01));
// refs
const valueTextRef = React.createRef<TextInput>();
const opacity = React.useRef<Animated.Value>(new Animated.Value(0)).current;
// initalizing settings
useEffect(() => {
setMax(values.length - 1);
setFlexDirection(osRtl ? 'row-reverse' : 'row');
setSvgOffset(osRtl ? { right: (knobSize - 40) / 2 } : { left: (knobSize - 40) / 2 });
}, []);
useEffect(() => {
if (TextualSliderWidth > 0) {
const stepSize = setStepSize(max, min, step);
valueTextRef.current?.setNativeProps({ text: values[min].text });
if (typeof initialValue === 'number' && initialValue >= min && initialValue <= max) {
const offset = ((initialValue - min) / step) * stepSize - (knobSize / 2);
setValueStatic(offset, knobSize, stepSize);
setValueText(offset);
}
Animated.timing(opacity, {
toValue: 1,
duration: 64,
useNativeDriver: true
}).start();
}
}, [min, max, step, initialValue, TextualSliderWidth]);
useEffect(() => {
const size = typeof styleSize === 'number' ? styleSize : styleSize === 'small' ? SMALL_SIZE : styleSize === 'medium' ? MEDIUM_SIZE : LARGE_SIZE;
setknobSize(size);
translateX.setValue(-size / 4);
}, [styleSize]);
const setValueStatic = (newOffset: number, knobSize: number, stepInPixels: number) => {
newOffset = Math.round((newOffset + (knobSize / 2)) / stepInPixels) * stepInPixels - (knobSize / 2);
settingValue(newOffset);
setValueOffset(newOffset);
const index = Math.round(((newOffset + (knobSize / 2)) * (max - min) / TextualSliderWidth) / step) * step + min;
valueOnChange(values[index]);
}
const settingValue = (newOffset: number) => {
translateX.setValue(newOffset);
inRangeScaleX.setValue((newOffset + (knobSize / 2)) / TextualSliderWidth + 0.01);
}
const setValueText = (totalOffset: number) => {
const numericValue: number = Math.floor(((totalOffset + (knobSize / 2)) * (max - min) / TextualSliderWidth) / step) * step + min;
valueTextRef.current?.setNativeProps({ text: values[numericValue].text });
}
const setStepSize = (max: number, min: number, step: number) => {
const numberOfSteps = ((max - min) / step);
const stepSize = TextualSliderWidth / numberOfSteps;
setStepInPixels(stepSize);
return stepSize;
}
// value gesture events ------------------------------------------------------------------------
const onGestureEvent = (event: PanGestureHandlerGestureEvent) => {
let totalOffset = event.nativeEvent.translationX + valueOffset;
if (totalOffset >= - knobSize / 2 && totalOffset <= TextualSliderWidth - knobSize / 2) {
translateX.setValue(totalOffset);
if (valueTextRef != null) {
const index = Math.round(((totalOffset + (knobSize / 2)) * (max - min) / TextualSliderWidth) / step) * step + min;
valueTextRef.current?.setNativeProps({ text: values[index].text });
}
inRangeScaleX.setValue((totalOffset + (knobSize / 2)) / TextualSliderWidth + 0.01);
}
}
const onHandlerStateChange = (event: PanGestureHandlerGestureEvent) => {
if (event.nativeEvent.state === State.BEGAN) {
scaleTo(valueLabelScale, 1);
}
if (event.nativeEvent.state === State.END) {
let newOffset = event.nativeEvent.translationX + valueOffset;
newOffset = Math.round((newOffset + (knobSize / 2)) / stepInPixels) * stepInPixels - (knobSize / 2);
if (newOffset < -knobSize / 2) {
newOffset = -knobSize / 2;
} else if (newOffset >= TextualSliderWidth - knobSize / 2) {
newOffset = TextualSliderWidth - knobSize / 2;
}
setValueStatic(newOffset, knobSize, stepInPixels);
scaleTo(valueLabelScale, 0.01);
}
}
// ------------------------------------------------------------------------------------------------
// gesture events help functions ------------------------------------------------------------------
const scaleTo = (param: Animated.Value, toValue: number) => Animated.timing(param,
{
toValue,
duration: 150,
useNativeDriver: true
}
).start();
// ------------------------------------------------------------------------------------------------
// setting bar width ------------------------------------------------------------------------------
const onLayout = (event: LayoutChangeEvent) => {
setTextualSliderWidth(event.nativeEvent.layout.width);
}
// ------------------------------------------------------------------------------------------------
const labelOpacity = valueLabelScale.interpolate({
inputRange: [0.1, 1],
outputRange: [0, 1]
})
return (
<GestureHandlerRootView>
<Animated.View style={[styles.container, { opacity, padding: styleSize === 'large' ? 7 : styleSize === 'medium' ? 14 : 21 }]}>
{
showValueLabels &&
<View style={{ width: '100%', flexDirection }}>
<Animated.View
style={{ position: 'absolute', bottom: 0, left: 0, opacity: labelOpacity, transform: [{ translateX }, { scale: valueLabelScale }] }}
>
<View style={{ width: '100%', alignItems: 'center' }}>
<TextInput style={{ ...svgOffset, color: valueLabelsTextColor, fontWeight: 'bold', backgroundColor: valueLabelsBackgroundColor, paddingHorizontal: 20, paddingVertical: 5, borderRadius: 3 }} ref={valueTextRef} />
</View>
</Animated.View>
</View>
}
<View style={{ width: '100%', height: knobSize, marginVertical: 4, position: 'relative', flexDirection, alignItems: 'center' }}>
<View style={[styles.bar, { backgroundColor: inRangeBarColor, left: knobSize / 4, marginLeft: -knobSize / 4, right: knobSize / 4, height: knobSize / 3 }]} onLayout={onLayout} />
<Animated.View style={{ width: TextualSliderWidth, height: knobSize / 3, backgroundColor: outOfRangeBarColor, transform: [{ translateX: -TextualSliderWidth / 2 }, { scaleX: inRangeScaleX }, { translateX: TextualSliderWidth / 2 }] }} />
<Animated.View style={{ position: 'absolute', left: -knobSize / 4, width: knobSize / 2.5, height: knobSize / 3, borderRadius: knobSize / 3, backgroundColor: outOfRangeBarColor }} />
<PanGestureHandler {...{ onGestureEvent, onHandlerStateChange }}>
<Animated.View style={[styles.knob, { height: knobSize, width: knobSize, borderRadius: knobSize, backgroundColor: knobColor, transform: [{ translateX }] }]} />
</PanGestureHandler>
</View>
{
showRangeLabels &&
<View style={{ width: '100%', flexDirection, justifyContent: 'space-between' }}>
<Text style={[rangeLabelsStyle, { fontWeight: "bold", marginLeft: -7 }]}>{values.length > 1 ? values[0].text : ''}</Text>
<Text style={[rangeLabelsStyle, { fontWeight: "bold" }]}>{values.length > 1 ? values[max].text : ''}</Text>
</View>
}
</Animated.View>
</GestureHandlerRootView>
);
})