import React, { useState, useEffect } from "react"; import { motion } from…
- User
import React, { useState, useEffect } from "react"; import { motion } from "framer-motion";
export type IndexPath = { section: number; item: number; };
export type CardStackSection = { id: number; text: string; };
export const isEqual = (pathA: IndexPath, pathB: IndexPath): boolean => { return pathA.section === pathB.section && pathA.item === pathB.item; };
export const isLessThan = (pathA: IndexPath, pathB: IndexPath): boolean => { if (pathA.section < pathB.section) { return true; }
if (pathA.section === pathB.section && pathA.item < pathB.item) { return true; }
return false; };
export const isGreaterThan = (pathA: IndexPath, pathB: IndexPath): boolean => { if (pathA.section > pathB.section) { return true; }
if (pathA.section === pathB.section && pathA.item > pathB.item) { return true; }
return false; };
interface CardStackSliderProps { sections: CardStackSection[][]; }
const containerHeight = 300; const pageSize = 198; const cellSize = 198; const cellSpacing = 212; const stackSpacing = 194; const stackScale = 0.82; const depthScale = 0.9; const depthOffset = 17; const maxStackSize = 6;
const getCellIndices = (sections: CardStackSection[][]) => { const cellIndices: (IndexPath & CardStackSection)[] = [];
sections.forEach((section, sectionIndex) => { section.forEach((item, itemIndex) => { const indexPath = { section: sectionIndex, item: itemIndex, ...item, };
cellIndices.push(indexPath); });});
return cellIndices; };
const sectionsData = [ [ { id: 1, text: "Stack 1 Item 1" }, { id: 2, text: "Stack 1 Item 2" }, { id: 3, text: "Stack 1 Item 3" }, { id: 4, text: "Stack 1 Item 4" }, ], [ { id: 5, text: "Stack 2 Item 1" }, { id: 6, text: "Stack 2 Item 2" }, { id: 7, text: "Stack 2 Item 3" }, { id: 8, text: "Stack 2 Item 4" }, ], ];
const CardStackSlider: React.FC = ({ sections = sectionsData, }) => { const [offset, setOffset] = useState(0); const [touchStart, setTouchStart] = useState(0); const [touchEnd, setTouchEnd] = useState(0);
const cellIndices = getCellIndices(sections); const cellCount = cellIndices.length;
const minOffset = 0; const maxOffset = pageSize * Math.max(0, cellCount - 1);
const sanitizeOffset = (value: number) => Math.min(maxOffset, Math.max(minOffset, value)); const safeSetOffset = (value: number) => { setOffset(sanitizeOffset(value)); };
const bottomPage = Math.floor(offset / pageSize); const bottomIndex = cellIndices[bottomPage] ?? { item: 0, section: 0 }; const topPage = bottomPage + 1; const topIndex = cellIndices[topPage] ?? bottomIndex;
const pageProgress = (offset % pageSize) / pageSize;
const currentPage = Math.floor(offset / pageSize + 0.5); const currentIndex = cellIndices[currentPage] ?? { item: 0, section: 0 };
- Assistant