11import type { PropsWithChildren } from "react" ;
22import React from "react" ;
33import type { ViewStyle } from "react-native" ;
4+ import type { SharedValue } from "react-native-reanimated" ;
45import Animated , {
56 Extrapolation ,
67 interpolate ,
78 interpolateColor ,
8- SharedValue ,
99 useAnimatedStyle ,
1010 runOnJS ,
1111 useSharedValue ,
1212 useDerivedValue ,
1313} from "react-native-reanimated" ;
14+
1415import type { DefaultStyle } from "react-native-reanimated/lib/typescript/hook/commonTypes" ;
1516
16- export type DotStyle = Omit < ViewStyle , "width" | "height" > & {
17- width ?: number
18- height ?: number
17+ export type DotStyle = Omit <
18+ ViewStyle ,
19+ "width" | "height" | "backgroundColor" | "borderRadius"
20+ > & {
21+ width ?: number ;
22+ height ?: number ;
23+ backgroundColor ?: string ;
24+ borderRadius ?: number ;
1925} ;
2026
2127export const PaginationItem : React . FC <
22- PropsWithChildren < {
23- index : number
24- count : number
25- size ?: number
26- animValue : SharedValue < number >
27- horizontal ?: boolean
28- dotStyle ?: DotStyle
29- activeDotStyle ?: DotStyle
30- customReanimatedStyle ?: (
31- progress : number ,
32- index : number ,
33- length : number ,
34- ) => DefaultStyle
35- } >
28+ PropsWithChildren < {
29+ index : number ;
30+ count : number ;
31+ size ?: number ;
32+ animValue : SharedValue < number > ;
33+ horizontal ?: boolean ;
34+ dotStyle ?: DotStyle ;
35+ activeDotStyle ?: DotStyle ;
36+ customReanimatedStyle ?: (
37+ progress : number ,
38+ index : number ,
39+ length : number ,
40+ ) => DefaultStyle ;
41+ } >
3642> = ( props ) => {
3743 const defaultDotSize = 10 ;
3844 const {
@@ -48,8 +54,9 @@ PropsWithChildren<{
4854 } = props ;
4955 const customReanimatedStyleRef = useSharedValue < DefaultStyle > ( { } ) ;
5056 const handleCustomAnimation = ( progress : number ) => {
51- customReanimatedStyleRef . value = customReanimatedStyle ?.( progress , index , count ) ?? { } ;
52- }
57+ customReanimatedStyleRef . value =
58+ customReanimatedStyle ?.( progress , index , count ) ?? { } ;
59+ } ;
5360
5461 useDerivedValue ( ( ) => {
5562 runOnJS ( handleCustomAnimation ) ( animValue ?. value ) ;
@@ -71,58 +78,66 @@ PropsWithChildren<{
7178 ...restActiveDotStyle
7279 } = activeDotStyle ?? { } ;
7380 let val = Math . abs ( animValue ?. value - index ) ;
74- if ( index === 0 && animValue ?. value > count - 1 ) {
81+ if ( index === 0 && animValue ?. value > count - 1 )
7582 val = Math . abs ( animValue ?. value - count ) ;
76- }
83+
7784 const inputRange = [ 0 , 1 , 2 ] ;
7885 const restStyle = ( val === 0 ? restActiveDotStyle : restDotStyle ) ?? { } ;
7986
8087 return {
81- width : interpolate (
82- val ,
83- inputRange ,
84- [ activeWidth , width , width ] ,
85- Extrapolation . CLAMP ,
86- ) ,
87- height : interpolate (
88- val ,
89- inputRange ,
90- [ activeHeight , height , height ] ,
91- Extrapolation . CLAMP ,
92- ) ,
93- borderRadius : interpolate (
94- val ,
95- inputRange ,
96- [ activeBorderRadius , borderRadius , borderRadius ] ,
97- Extrapolation . CLAMP ,
98- ) ,
99- backgroundColor : interpolateColor (
100- val ,
101- inputRange ,
102- [ activeBackgroundColor , backgroundColor , backgroundColor ] ,
103- ) ,
104- ...restStyle ,
105- ...( customReanimatedStyleRef . value ?? { } ) ,
106- transform : [
107- ...restStyle ?. transform ?? [ ] ,
108- ...customReanimatedStyleRef . value ?. transform ?? [ ] ,
109- ]
88+ width : interpolate (
89+ val ,
90+ inputRange ,
91+ [ activeWidth , width , width ] ,
92+ Extrapolation . CLAMP ,
93+ ) ,
94+ height : interpolate (
95+ val ,
96+ inputRange ,
97+ [ activeHeight , height , height ] ,
98+ Extrapolation . CLAMP ,
99+ ) ,
100+ borderRadius : interpolate (
101+ val ,
102+ inputRange ,
103+ [ activeBorderRadius ?? borderRadius ?? 0 , borderRadius ?? 0 , borderRadius ?? 0 ] ,
104+ Extrapolation . CLAMP ,
105+ ) ,
106+ backgroundColor : interpolateColor ( val , inputRange , [
107+ activeBackgroundColor ,
108+ backgroundColor ,
109+ backgroundColor ,
110+ ] ) ,
111+ ...restStyle ,
112+ ...( customReanimatedStyleRef . value ?? { } ) ,
113+ transform : [
114+ ...( restStyle ?. transform ?? [ ] ) ,
115+ ...( customReanimatedStyleRef . value ?. transform ?? [ ] ) ,
116+ ] ,
110117 } ;
111- } , [ animValue , index , count , horizontal , dotStyle , activeDotStyle , customReanimatedStyle ] ) ;
118+ } , [
119+ animValue ,
120+ index ,
121+ count ,
122+ horizontal ,
123+ dotStyle ,
124+ activeDotStyle ,
125+ customReanimatedStyle ,
126+ ] ) ;
112127
113128 return (
114129 < Animated . View
115130 style = { [
116- {
117- overflow : "hidden" ,
118- transform : [
119- {
120- rotateZ : horizontal ? "90deg" : "0deg" ,
121- } ,
122- ] ,
123- } ,
124- dotStyle ,
125- animStyle ,
131+ {
132+ overflow : "hidden" ,
133+ transform : [
134+ {
135+ rotateZ : horizontal ? "90deg" : "0deg" ,
136+ } ,
137+ ] ,
138+ } ,
139+ dotStyle ,
140+ animStyle ,
126141 ] }
127142 >
128143 { children }
0 commit comments