Refactor Stack: Content Flexibility, Autoplay and Layout Sync
I completely reworked the Stack component to make it flexible when working with arbitrary content (children), add the necessary autoplay functionality & synchronize content processing (e.g., for i18n)
AND AGAIN KEY CHANGES
-
Content Architecture & Flexibility Before: Component required structured
cardsData(array of objects withidandimg) and manually rendered<img>tags. After: Switched to accepting arbitrarychildren(React.ReactNode[]). The component now manages the order of the children's indices internally, making it a flexible container for any content (images, forms, custom UI) -
Autoplay & Interaction Control Added
autoplay(boolean) andautoplayDelay(number, ms) props to enable automatic card cycling. Autoplay is automatically paused when the user interacts with the stack (drag start) or hovers the mouse over the container (onMouseEnter), ensuring a better user experience -
Layout & Responsiveness Before: Required explicit
cardDimensionsprop, making responsiveness difficult and limiting component flexibility. After: RemovedcardDimensions. The component now usesw-full/h-fullinternally and relies on the CSS dimensions of the parent container to define the card size. This allows for native CSS responsiveness (e.g., using Tailwind classes likew-1/2oraspect-squareon the wrapper) -
Content Update Handling (i18n Ready) Implemented
useEffectto watch the number ofchildren. Technical explanation: If the content inside the cards changes (e.g., text translation viai18n), the component re-renders the children without losing the current stack order or interrupting the ongoing animation. The order is reset only if the count of cards changes
Testing
- Verify that arbitrary React nodes (e.g., divs with text, not just images) render and stack correctly
- Test the
autoplay={true}prop and ensure cards cycle automatically everyautoplayDelaymilliseconds - Verify that hovering the mouse over the stack successfully pauses the automatic cycling
- Test drag and click functionality to ensure the top card moves to the back correctly
- Check responsiveness by placing the component in a fluid container (e.g.,
w-1/2) to confirm card size adapts