Skip to content

Commit

Permalink
feat(popper): initial wip of generic popper
Browse files Browse the repository at this point in the history
  • Loading branch information
mateoguzmana committed Aug 21, 2022
1 parent 0a6e31a commit 6f00491
Showing 1 changed file with 76 additions and 0 deletions.
76 changes: 76 additions & 0 deletions src/components/Popper.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import React, { memo, useCallback, useEffect } from 'react';
import { StyleSheet } from 'react-native';
import {
Canvas,
Group,
runSpring,
useComputedValue,
useValue,
} from '@shopify/react-native-skia';
import { screenHeight } from '../constants/dimensions';
import { screenWidth } from '../constants/dimensions';
import { shuffleArray } from '../utils/array';

const xGap = 40; // ideally the gap can be also set from an external component
const optimalNumberOfItems = Math.floor(screenWidth / xGap); // this one should remain the same as it is defined by the xGap
const itemsToRenderArray = [...Array(optimalNumberOfItems)]; // remains the sane
const yPositions = shuffleArray(itemsToRenderArray.map((_, i) => i * xGap)); // this one can also be very generic

interface RenderItemParams {
x: number;
y: number;
}

export interface PopperProps {
renderItem: (renderItemParams: RenderItemParams) => React.ReactElement;
}

function Popper({ renderItem }: PopperProps) {
const yPosition = useValue(screenHeight);

const changeItemPosition = useCallback(
() =>
runSpring(yPosition, -screenHeight, {
stiffness: 0.5,
}),
[yPosition]
);

const transform = useComputedValue(
() => [
{
translateY: yPosition.current,
},
],
[yPosition]
);

useEffect(() => {
changeItemPosition();
}, [changeItemPosition]);

if (!renderItem) return null;

return (
<Canvas style={styles.canvas}>
<Group transform={transform}>
{itemsToRenderArray.map((_, index) =>
renderItem({ x: xGap * index, y: yPositions[index] })
)}
</Group>
</Canvas>
);
}

const styles = StyleSheet.create({
canvas: {
position: 'absolute',
top: 0,
left: 0,
right: 0,
bottom: 0,
zIndex: -1,
},
});

export default memo(Popper);

0 comments on commit 6f00491

Please sign in to comment.