// Copyright (c) Meta Platforms, Inc. and affiliates. 'use client'; import {useCallback, useRef, useState, type Ref} from 'react'; import * as stylex from '@stylexjs/stylex'; import {createPanGesture} from '@astryxdesign/core'; import {Card} from '@astryxdesign/core/Card'; import {Center} from '@astryxdesign/core/Center'; import {HStack} from '@astryxdesign/core/HStack'; import {usePanGesture, type PanGestureEvent} from '@astryxdesign/core/hooks'; import { colorVars, durationVars, easeVars, radiusVars, } from '@astryxdesign/core/theme/tokens.stylex'; const styles = stylex.create({ // A wash under the stage, so the box on top reads in light and dark themes. stage: { backgroundColor: colorVars['--color-neutral'], borderRadius: radiusVars['--radius-container'], }, box: {cursor: 'grab', userSelect: 'none'}, // Let go, the box springs back, unless the person reduces motion. springBack: { transition: { default: `translate ${durationVars['--duration-medium']} ${easeVars['--ease-move']}`, '@media (prefers-reduced-motion: reduce)': 'none', }, }, drag: (x: number, y: number) => ({translate: `${x}px ${y}px`}), }); type Drag = {x: number; y: number; held: boolean}; const REST: Drag = {x: 0, y: 0, held: false}; /** The box follows a held drag and goes back to rest when it ends. */ function nextDrag({state, translation}: PanGestureEvent): Drag { const held = state === 'began' || state === 'changed'; return held ? {...translation, held} : REST; } /** The same stage and card for both forms; the card names its API. */ function DraggableBox(props: { name: string; ref: Ref; drag: Drag; }) { const {name, ref, drag} = props; return (
{name}
); } function WithHook() { const box = useRef(null); const [drag, setDrag] = useState(REST); usePanGesture(box, {onPan: event => setDrag(nextDrag(event))}); return ; } function WithFactory() { const [drag, setDrag] = useState(REST); // A callback ref: its cleanup takes the pan down with the box. const draggable = useCallback((box: HTMLDivElement | null) => { if (box == null) { return undefined; } const pan = createPanGesture(box, { onPan: event => setDrag(nextDrag(event)), }); return () => pan.destroy(); }, []); return ; } export default function PanDraggableBox() { return ( ); }