|
| 1 | +import React, { useMemo, useState } from "react"; |
| 2 | + |
| 3 | +import { TransformWrapper, TransformComponent } from "../../../components"; |
| 4 | +import { |
| 5 | + Controls, |
| 6 | + normalizeArgs, |
| 7 | + viewerChrome, |
| 8 | + FreeMovementIcon, |
| 9 | + LockVerticalIcon, |
| 10 | + LockHorizontalIcon, |
| 11 | +} from "../../utils"; |
| 12 | + |
| 13 | +type AxisMode = "free" | "vertical" | "horizontal"; |
| 14 | + |
| 15 | +const viewer: React.CSSProperties = { |
| 16 | + ...viewerChrome, |
| 17 | + width: "560px", |
| 18 | + height: "440px", |
| 19 | + maxWidth: "90vw", |
| 20 | + maxHeight: "78vh", |
| 21 | +}; |
| 22 | + |
| 23 | +const font = "system-ui, -apple-system, sans-serif"; |
| 24 | + |
| 25 | +function GridCanvas() { |
| 26 | + const cells = useMemo(() => { |
| 27 | + const out: { key: string; row: number; col: number }[] = []; |
| 28 | + for (let row = 0; row < 6; row += 1) { |
| 29 | + for (let col = 0; col < 8; col += 1) { |
| 30 | + out.push({ key: `${row}-${col}`, row, col }); |
| 31 | + } |
| 32 | + } |
| 33 | + return out; |
| 34 | + }, []); |
| 35 | + |
| 36 | + return ( |
| 37 | + <div |
| 38 | + style={{ |
| 39 | + width: 1400, |
| 40 | + height: 900, |
| 41 | + display: "grid", |
| 42 | + gridTemplateColumns: "repeat(8, 1fr)", |
| 43 | + gridTemplateRows: "repeat(6, 1fr)", |
| 44 | + gap: 3, |
| 45 | + padding: 24, |
| 46 | + boxSizing: "border-box", |
| 47 | + background: |
| 48 | + "linear-gradient(145deg, #12122a 0%, #0d1528 40%, #0a1020 100%)", |
| 49 | + }} |
| 50 | + > |
| 51 | + {cells.map(({ key, row, col }) => ( |
| 52 | + <div |
| 53 | + key={key} |
| 54 | + style={{ |
| 55 | + borderRadius: 10, |
| 56 | + background: `rgba(${40 + col * 18}, ${60 + row * 12}, ${120 + (row + col) * 8}, 0.35)`, |
| 57 | + border: "1px solid rgba(255,255,255,0.06)", |
| 58 | + display: "flex", |
| 59 | + alignItems: "center", |
| 60 | + justifyContent: "center", |
| 61 | + fontFamily: font, |
| 62 | + fontSize: 13, |
| 63 | + fontWeight: 600, |
| 64 | + color: "rgba(255,255,255,0.45)", |
| 65 | + }} |
| 66 | + > |
| 67 | + {col + 1},{row + 1} |
| 68 | + </div> |
| 69 | + ))} |
| 70 | + </div> |
| 71 | + ); |
| 72 | +} |
| 73 | + |
| 74 | +function axisButtons( |
| 75 | + mode: AxisMode, |
| 76 | + setMode: (m: AxisMode) => void, |
| 77 | +) { |
| 78 | + const btn = ( |
| 79 | + id: AxisMode, |
| 80 | + label: string, |
| 81 | + Icon: React.FC, |
| 82 | + ) => ({ |
| 83 | + label, |
| 84 | + icon: <Icon />, |
| 85 | + onClick: () => setMode(mode === id ? "free" : id), |
| 86 | + active: mode === id, |
| 87 | + "data-tooltip": label, |
| 88 | + }); |
| 89 | + |
| 90 | + return [ |
| 91 | + btn("free", "Free pan", FreeMovementIcon), |
| 92 | + btn("vertical", "Vertical only", LockVerticalIcon), |
| 93 | + btn("horizontal", "Horizontal only", LockHorizontalIcon), |
| 94 | + ]; |
| 95 | +} |
| 96 | + |
| 97 | +export const Example: React.FC<Record<string, unknown>> = (args) => { |
| 98 | + const normalized = normalizeArgs(args); |
| 99 | + const [mode, setMode] = useState<AxisMode>("free"); |
| 100 | + |
| 101 | + const panning = { |
| 102 | + ...normalized.panning, |
| 103 | + lockAxisX: mode === "vertical", |
| 104 | + lockAxisY: mode === "horizontal", |
| 105 | + }; |
| 106 | + |
| 107 | + return ( |
| 108 | + <div style={{ fontFamily: font }}> |
| 109 | + <TransformWrapper {...normalized} centerOnInit panning={panning}> |
| 110 | + {(utils) => ( |
| 111 | + <div style={{ position: "relative", display: "inline-block" }}> |
| 112 | + <Controls |
| 113 | + {...utils} |
| 114 | + extraButtons={axisButtons(mode, setMode)} |
| 115 | + /> |
| 116 | + <TransformComponent |
| 117 | + wrapperStyle={viewer} |
| 118 | + contentStyle={{ width: 1400, height: 900 }} |
| 119 | + > |
| 120 | + <GridCanvas /> |
| 121 | + </TransformComponent> |
| 122 | + </div> |
| 123 | + )} |
| 124 | + </TransformWrapper> |
| 125 | + </div> |
| 126 | + ); |
| 127 | +}; |
0 commit comments