|
| 1 | +// Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | + |
| 3 | +'use client'; |
| 4 | + |
| 5 | +import {createDragToSnapPoints} from '@astryxdesign/core'; |
| 6 | + |
| 7 | +const PLACES = ['Hand', 'Table', 'Discard pile']; |
| 8 | + |
| 9 | +/** |
| 10 | + * A card that snaps between three places, in plain JavaScript: no React |
| 11 | + * below this line. It returns a function that takes it down again. |
| 12 | + */ |
| 13 | +function mountCard(table: HTMLElement, buttons: HTMLElement) { |
| 14 | + const card = document.createElement('div'); |
| 15 | + card.textContent = 'Q\u2665'; |
| 16 | + Object.assign(card.style, { |
| 17 | + width: '96px', |
| 18 | + height: '136px', |
| 19 | + display: 'grid', |
| 20 | + placeItems: 'center', |
| 21 | + font: 'bold 36px system-ui, sans-serif', |
| 22 | + // The theme's surface and text, so the card reads in light and dark. |
| 23 | + borderRadius: 'var(--radius-container)', |
| 24 | + backgroundColor: 'var(--color-background-surface)', |
| 25 | + color: 'var(--color-text-primary)', |
| 26 | + boxShadow: '0 2px 8px var(--color-shadow)', |
| 27 | + touchAction: 'none', |
| 28 | + cursor: 'grab', |
| 29 | + userSelect: 'none', |
| 30 | + }); |
| 31 | + table.append(card); |
| 32 | + const status = document.createElement('div'); |
| 33 | + status.setAttribute('aria-live', 'polite'); |
| 34 | + Object.assign(status.style, {position: 'absolute', clip: 'rect(0 0 0 0)'}); |
| 35 | + table.append(status); |
| 36 | + |
| 37 | + // The places run in reading order, so leftward in a right-to-left layout. |
| 38 | + // There the snap points are negative, so they sort the other way round. |
| 39 | + const steps = [0, 110, 220]; |
| 40 | + const isRtl = getComputedStyle(table).direction === 'rtl'; |
| 41 | + const placeAt = (index: number) => (isRtl ? steps.length - 1 - index : index); |
| 42 | + const snap = createDragToSnapPoints(card, { |
| 43 | + axis: 'x', |
| 44 | + snapPoints: isRtl ? steps.map(step => -step) : steps, |
| 45 | + onSettle: index => { |
| 46 | + status.textContent = `The card is on the ${PLACES[placeAt(index)].toLowerCase()}.`; |
| 47 | + }, |
| 48 | + }); |
| 49 | + const controls = PLACES.map((name, index) => { |
| 50 | + const button = document.createElement('button'); |
| 51 | + button.type = 'button'; |
| 52 | + button.textContent = name; |
| 53 | + button.addEventListener('click', () => snap.settleTo(placeAt(index))); |
| 54 | + buttons.append(button); |
| 55 | + return button; |
| 56 | + }); |
| 57 | + return () => { |
| 58 | + snap.destroy(); |
| 59 | + for (const button of controls) { |
| 60 | + button.remove(); |
| 61 | + } |
| 62 | + card.remove(); |
| 63 | + status.remove(); |
| 64 | + }; |
| 65 | +} |
| 66 | + |
| 67 | +/** |
| 68 | + * Mounts the card when the wrapper attaches, and takes it down when it |
| 69 | + * detaches: its cleanup is the callback ref's. |
| 70 | + */ |
| 71 | +function mountTable(wrapper: HTMLDivElement | null) { |
| 72 | + if (wrapper == null) { |
| 73 | + return undefined; |
| 74 | + } |
| 75 | + const table = wrapper.firstElementChild as HTMLElement; |
| 76 | + const buttons = wrapper.lastElementChild as HTMLElement; |
| 77 | + return mountCard(table, buttons); |
| 78 | +} |
| 79 | + |
| 80 | +export default function DragToSnapPointsCardVanilla() { |
| 81 | + return ( |
| 82 | + <div |
| 83 | + ref={mountTable} |
| 84 | + style={{display: 'grid', gap: 16, padding: 16, maxWidth: 380}}> |
| 85 | + <div |
| 86 | + style={{ |
| 87 | + position: 'relative', |
| 88 | + height: 150, |
| 89 | + display: 'flex', |
| 90 | + alignItems: 'center', |
| 91 | + }} |
| 92 | + /> |
| 93 | + <div style={{display: 'flex', gap: 8}} /> |
| 94 | + </div> |
| 95 | + ); |
| 96 | +} |
0 commit comments