Skip to content

Commit

Permalink
fix: refactor stack layout
Browse files Browse the repository at this point in the history
  • Loading branch information
dohooo committed Dec 29, 2021
1 parent f3b28cc commit dc975fe
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 35 deletions.
24 changes: 13 additions & 11 deletions src/hooks/useOffsetX.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,23 @@ export const useOffsetX = (opts: IOpts, visibleRanges: IVisibleRanges) => {
type = 'positive',
viewCount = Math.round((data.length - 1) / 2),
} = opts;

const ITEM_LENGTH = data.length;
const VALID_LENGTH = ITEM_LENGTH - 1;
const TOTAL_WIDTH = size * ITEM_LENGTH;
const HALF_WIDTH = 0.5 * size;

const positiveCount =
type === 'positive' ? viewCount : VALID_LENGTH - viewCount;

let startPos = size * index;
if (index > positiveCount) {
startPos = (index - ITEM_LENGTH) * size;
}

const MAX = positiveCount * size;
const MIN = -((VALID_LENGTH - positiveCount) * size);

const x = useDerivedValue(() => {
const { negativeRange, positiveRange } = visibleRanges.value;
if (
Expand All @@ -38,18 +50,8 @@ export const useOffsetX = (opts: IOpts, visibleRanges: IVisibleRanges) => {
) {
return Number.MAX_SAFE_INTEGER;
}
if (loop) {
const positiveCount =
type === 'positive' ? viewCount : VALID_LENGTH - viewCount;

let startPos = size * index;
if (index > positiveCount) {
startPos = (index - ITEM_LENGTH) * size;
}

const MAX = positiveCount * size;
const MIN = -((VALID_LENGTH - positiveCount) * size);

if (loop) {
const inputRange = [
-TOTAL_WIDTH,
MIN - HALF_WIDTH - startPos - Number.MIN_VALUE,
Expand Down
45 changes: 21 additions & 24 deletions src/layouts/StackLayout.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react';
import { Dimensions, StyleSheet } from 'react-native';
import Animated, {
Extrapolate,
interpolate,
runOnJS,
useAnimatedReaction,
Expand Down Expand Up @@ -53,51 +54,47 @@ export const StackLayout: React.FC<{
);

const offsetXStyle = useAnimatedStyle(() => {
const startPosition = (x.value - index * size) / size;
const value = x.value / size;
const showLength = 3;
const validLength = showLength - 1;

return {
transform: [
{
translateX: interpolate(
startPosition,
[-(index + 1), -index, 0],
[-(PAGE_WIDTH - size) * 2, 0, 0]
value,
[-1, 0, validLength],
[-PAGE_WIDTH, 0, 0],
Extrapolate.CLAMP
),
},
{
scale: interpolate(
startPosition,
[-index - 1, -index, 0, data.length - index],
[
1,
1,
1 - index * 0.09,
1 - (data.length - index) * 0.09,
]
value,
[0, validLength],
[1, 1 - validLength * 0.08],
Extrapolate.CLAMP
),
},
{
rotateZ: `${interpolate(
startPosition,
[-index - 1, -index, 0],
[-135, 0, 0]
value,
[-1, 0],
[-135, 0],
Extrapolate.CLAMP
)}deg`,
},
{
translateY: interpolate(
startPosition,
[-index - 1, -index, 0, data.length - index],
[
0,
0,
index * size * 0.12,
(data.length - index) * size * 0.12,
]
value,
[0, validLength],
[0, validLength * 30],
Extrapolate.CLAMP
),
},
],
zIndex: -interpolate(
startPosition,
(x.value - index * size) / size,
[-index - 1, -index - 0.5, -index, 0, data.length - index],
[
Number.MAX_VALUE,
Expand Down

0 comments on commit dc975fe

Please sign in to comment.