-
Notifications
You must be signed in to change notification settings - Fork 53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Slider range #35
Open
Kiro-tagama
wants to merge
7
commits into
Mobilecn-UI:main
Choose a base branch
from
Kiro-tagama:slider-range
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Slider range #35
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e221041
package config
Kiro-tagama 3b53851
up
Kiro-tagama a46bc27
base
Kiro-tagama ba48d73
update animaation and calcs
Kiro-tagama 99e9e35
slider beta 1
Kiro-tagama fd75ede
remove comments
Kiro-tagama dc89a91
edited value of prevTranslation
Kiro-tagama File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
import debounce from 'lodash.debounce'; | ||
import React, { useCallback, useEffect, useMemo, useState } from 'react'; | ||
import { Text, View } from 'react-native'; | ||
import { | ||
Gesture, | ||
GestureDetector, | ||
GestureHandlerRootView, | ||
} from 'react-native-gesture-handler'; | ||
import Animated, { | ||
useAnimatedStyle, | ||
useSharedValue, | ||
} from 'react-native-reanimated'; | ||
|
||
import { cn } from '../lib/utils'; | ||
|
||
interface SliderProps { | ||
minimumValue: number; | ||
maximumValue: number; | ||
value: number; | ||
onValueChange?: (value: number) => void; | ||
thumbVisible?: boolean; | ||
} | ||
|
||
const clamp = (value: number, min: number, max: number) => | ||
Math.max(min, Math.min(value, max)); | ||
|
||
function Slider({ | ||
value, | ||
onValueChange, | ||
minimumValue = 0, | ||
maximumValue = 100, | ||
thumbVisible = true, | ||
}: SliderProps) { | ||
const [sliderWidth, setSliderWidth] = useState(0); | ||
|
||
const calcPosition = useCallback( | ||
(v: number) => | ||
(sliderWidth * (v - minimumValue)) / (maximumValue - minimumValue), | ||
[sliderWidth, minimumValue, maximumValue] | ||
); | ||
|
||
const translationX = useSharedValue(calcPosition(value)); | ||
const prevTranslationX = useSharedValue(0); | ||
const isDragging = useSharedValue(false); | ||
|
||
const debounceOnValueChange = useCallback( | ||
debounce((value: number) => { | ||
if (onValueChange) onValueChange(value); | ||
}, 100), | ||
[onValueChange] | ||
); | ||
|
||
useEffect(() => { | ||
translationX.value = calcPosition(value); | ||
return () => debounceOnValueChange.cancel(); | ||
}, [value, calcPosition, debounceOnValueChange]); | ||
|
||
const panGesture = useMemo( | ||
() => | ||
Gesture.Pan() | ||
.minDistance(0) | ||
.onStart(() => { | ||
prevTranslationX.value = calcPosition(value); | ||
isDragging.value = true; | ||
}) | ||
.onUpdate(async event => { | ||
const positionValue = prevTranslationX.value + event.translationX; | ||
const clampedPosition = clamp(positionValue, 0, sliderWidth); | ||
translationX.value = clampedPosition; | ||
}) | ||
.onEnd(async () => { | ||
isDragging.value = false; | ||
const calcReturn = | ||
((maximumValue - minimumValue) * translationX.value) / sliderWidth; | ||
if (onValueChange) await debounceOnValueChange(calcReturn); | ||
}) | ||
.runOnJS(true), | ||
[calcPosition, sliderWidth, value, debounceOnValueChange] | ||
); | ||
|
||
const animatedStyles = useAnimatedStyle( | ||
() => ({ | ||
transform: [{ translateX: translationX.value }], | ||
}), | ||
[translationX] | ||
); | ||
const sizeAnimatedStyles = useAnimatedStyle( | ||
() => ({ | ||
width: translationX.value, | ||
}), | ||
[translationX] | ||
); | ||
|
||
return ( | ||
<> | ||
<GestureHandlerRootView | ||
style={{ | ||
height: 20, | ||
paddingVertical: 18, | ||
justifyContent: 'center', | ||
}} | ||
> | ||
<View | ||
onLayout={e => setSliderWidth(e.nativeEvent.layout.width)} | ||
className={cn( | ||
'mx-3 h-2 rounded-xl w-auto bg-foreground/20 justify-center' | ||
)} | ||
> | ||
<Animated.View | ||
style={sizeAnimatedStyles} | ||
className={cn( | ||
'h-2 rounded-l-xl bg-foreground absolute', | ||
value === maximumValue ? 'rounded-r-xl' : null | ||
)} | ||
/> | ||
<GestureDetector gesture={panGesture}> | ||
<Animated.View | ||
style={[animatedStyles]} | ||
className={cn( | ||
'h-6 w-6 -mx-3 rounded-full bg-background border border-foreground', | ||
thumbVisible ? '' : ' bg-transparent border-transparent' | ||
)} | ||
/> | ||
</GestureDetector> | ||
</View> | ||
</GestureHandlerRootView> | ||
{/* Debugging info - remove or hide in production */} | ||
{/* <View style={{ marginVertical: 40, backgroundColor: 'red' }}> | ||
<Text>sliderWidth:{sliderWidth.toFixed(2)}</Text> | ||
<Text>value:{value.toFixed(2)}</Text> | ||
<Text>max:{maximumValue}</Text> | ||
<Text>translationX.value:{translationX.value.toFixed(2)}</Text> | ||
<Text>prevTranslationX.value:{prevTranslationX.value.toFixed(2)}</Text> | ||
</View> */} | ||
</> | ||
); | ||
} | ||
|
||
export { Slider }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove the commented parts