Skip to content

feat: opacity changeable #61

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export default ExampleDotPaginate;
| `activeDotColor` | `string` | required | Active Dot Color. dot will control by opacity |
| `inactiveDotColor` | `string` | undefined | InActive Dot Color. dot will control by opacity |
| `sizeRatio` | `number` | 1.0 | Customize Dot Size. minimum value is 1.0 (*recommend 1.0 ~ 2.0*) |
| `opacity` | `number` | 0.2 | Dot opacity for inactive dot |
| `vertical` | `boolean` | false | Dot direction |

## Contributing
Expand Down
35 changes: 26 additions & 9 deletions src/component/Dot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const Dot: React.FC<{
activeColor: string;
inactiveColor?: string;
sizeRatio: number;
opacity?: number;
}> = (props) => {
const [animVal] = useState(new Animated.Value(0));
const [animate, setAnimate] = useState(false);
Expand All @@ -37,8 +38,18 @@ const Dot: React.FC<{
return props.inactiveColor ?? props.activeColor;
});

const [dotOpacity, setDotOpacity] = useState<number>(() => {
if (props.curPage === props.idx) {
//its current active page now
return 1;
}

return props.opacity ?? 0.2;
});

const prevType = usePrevious(type);
const prevDotColor = usePrevious<string>(dotColor);
const prevOpacity = usePrevious<number>(dotOpacity);

useEffect(() => {
const nextType = getDotStyle({
Expand All @@ -47,25 +58,28 @@ const Dot: React.FC<{
maxPage: props.maxPage,
});

const nextAnimate =
nextType.size !== (prevType?.size || 3) ||
nextType.opacity !== (prevType?.opacity || 0.2);
const nextAnimate = nextType.size !== (prevType?.size || 3);
if (props.curPage === props.idx) {
setDotColor(props.activeColor);
setDotOpacity(1);
} else {
setDotColor(props.inactiveColor ?? props.activeColor);
setDotOpacity(props.opacity ?? 0.2);
}

setType(nextType);
setAnimate(nextAnimate);
}, [
dotOpacity,
prevType,
prevType?.opacity,
prevType?.size,
props.activeColor,
props.curPage,
props.idx,
props.inactiveColor,
props.maxPage,
props.opacity,
]);

useEffect(() => {
Expand Down Expand Up @@ -93,6 +107,11 @@ const Dot: React.FC<{
outputRange: [prevDotColor ?? props.activeColor, dotColor],
});

const opacity = animVal.interpolate({
inputRange: [0, 1],
outputRange: [prevOpacity ?? props.opacity, dotOpacity],
});

return {
width: size,
height: size,
Expand All @@ -104,20 +123,18 @@ const Dot: React.FC<{
type.size * props.sizeRatio * 0.5,
],
}),
opacity: animVal.interpolate({
inputRange: [0, 1],
outputRange: [prevType?.opacity || 0.2, type.opacity],
}),
opacity: opacity,
};
}, [
animVal,
dotColor,
dotOpacity,
prevDotColor,
prevType?.opacity,
prevOpacity,
prevType?.size,
props.activeColor,
props.opacity,
props.sizeRatio,
type.opacity,
type.size,
]);

Expand Down
5 changes: 4 additions & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export interface IDotContainerProps {
activeDotColor: string;
inactiveDotColor?: string;
vertical?: boolean;
opacity?: number;
}

const ONE_EMPTY_DOT_SIZE = defaultEmptyDotSize * defaultEmptyDotSize;
Expand Down Expand Up @@ -86,7 +87,7 @@ const DotContainer: React.FC<IDotContainerProps> = (props) => {
scrollTo(props.curPage);
}, [prevPage, props.curPage, props.maxPage, scrollTo]);

const { curPage, maxPage, activeDotColor, inactiveDotColor } = props;
const { curPage, maxPage, activeDotColor, inactiveDotColor, opacity } = props;
const list = useMemo(() => [...Array(maxPage).keys()], [maxPage]);

let normalizedPage = curPage;
Expand Down Expand Up @@ -114,6 +115,7 @@ const DotContainer: React.FC<IDotContainerProps> = (props) => {
maxPage={maxPage}
activeColor={activeDotColor}
inactiveColor={inactiveDotColor}
opacity={opacity}
/>
);
})}
Expand Down Expand Up @@ -152,6 +154,7 @@ const DotContainer: React.FC<IDotContainerProps> = (props) => {
maxPage={maxPage}
activeColor={activeDotColor}
inactiveColor={inactiveDotColor}
opacity={opacity}
/>
);
})}
Expand Down