// Copyright (c) Meta Platforms, Inc. and affiliates. 'use client'; import {useLayoutEffect, useRef, useState} from 'react'; import {Button} from '@astryxdesign/core/Button'; import {Card} from '@astryxdesign/core/Card'; import {useDragToSnapPoints} from '@astryxdesign/core/hooks'; import {HStack, VStack} from '@astryxdesign/core/Layout'; import {Text} from '@astryxdesign/core/Text'; import {VisuallyHidden} from '@astryxdesign/core/VisuallyHidden'; const PLACES = ['Hand', 'Table', 'Discard pile']; /** How far along the row each place sits, in px from where the card rests. */ const STEPS = [0, 110, 220]; export default function DragToSnapPointsCard() { const ref = useRef(null); const [place, setPlace] = useState(0); // The places run in reading order, so leftward in a right-to-left layout. // There the snap points are negative, so they sort the other way round. // The direction is the one the card renders in. const [isRtl, setIsRtl] = useState(false); // Reads the direction the browser computed for the card once it is laid // out. useLayoutEffect(() => { if (ref.current != null) { setIsRtl(getComputedStyle(ref.current).direction === 'rtl'); } }, []); const placeAt = (index: number) => (isRtl ? STEPS.length - 1 - index : index); const {settleTo} = useDragToSnapPoints(ref, { axis: 'x', snapPoints: isRtl ? STEPS.map(step => -step) : STEPS, onSettle: index => setPlace(placeAt(index)), }); return ( Drag the card sideways and let go: it settles on the nearest place, or the next one after a quick flick. Q {'\u2665'} {PLACES.map((name, index) => (