|
| 1 | +import React from 'react' |
| 2 | +import { createPortal } from 'react-dom' |
| 3 | +import type { GridInteractions } from './useGridInteractions' |
| 4 | +import type { |
| 5 | + SpreadsheetTable, |
| 6 | + SpreadsheetTableColumn, |
| 7 | +} from './spreadsheetTable' |
| 8 | + |
| 9 | +interface CellContextMenuProps { |
| 10 | + x: number |
| 11 | + y: number |
| 12 | + column: SpreadsheetTableColumn |
| 13 | + table: SpreadsheetTable |
| 14 | + interactions: GridInteractions |
| 15 | + onClose: () => void |
| 16 | +} |
| 17 | + |
| 18 | +export function CellContextMenu({ |
| 19 | + x, |
| 20 | + y, |
| 21 | + column, |
| 22 | + table, |
| 23 | + interactions, |
| 24 | + onClose, |
| 25 | +}: CellContextMenuProps) { |
| 26 | + const menuRef = React.useRef<HTMLDivElement>(null) |
| 27 | + |
| 28 | + React.useEffect(() => { |
| 29 | + const handlePointerDown = (event: PointerEvent) => { |
| 30 | + if (!menuRef.current?.contains(event.target as Node)) onClose() |
| 31 | + } |
| 32 | + const handleKeyDown = (event: KeyboardEvent) => { |
| 33 | + if (event.key === 'Escape') onClose() |
| 34 | + } |
| 35 | + |
| 36 | + document.addEventListener('pointerdown', handlePointerDown) |
| 37 | + document.addEventListener('keydown', handleKeyDown) |
| 38 | + return () => { |
| 39 | + document.removeEventListener('pointerdown', handlePointerDown) |
| 40 | + document.removeEventListener('keydown', handleKeyDown) |
| 41 | + } |
| 42 | + }, [onClose]) |
| 43 | + |
| 44 | + const run = (action: () => void | Promise<void>) => { |
| 45 | + onClose() |
| 46 | + void action() |
| 47 | + } |
| 48 | + |
| 49 | + return createPortal( |
| 50 | + <div |
| 51 | + ref={menuRef} |
| 52 | + className="cell-context-menu" |
| 53 | + role="menu" |
| 54 | + aria-label="Cell actions" |
| 55 | + style={{ |
| 56 | + left: Math.min(x, window.innerWidth - 218), |
| 57 | + top: Math.min(y, window.innerHeight - 286), |
| 58 | + }} |
| 59 | + > |
| 60 | + <button |
| 61 | + type="button" |
| 62 | + role="menuitem" |
| 63 | + onClick={() => run(interactions.cutToClipboard)} |
| 64 | + > |
| 65 | + <span>✂</span> Cut <kbd>Ctrl+X</kbd> |
| 66 | + </button> |
| 67 | + <button |
| 68 | + type="button" |
| 69 | + role="menuitem" |
| 70 | + onClick={() => run(interactions.copyToClipboard)} |
| 71 | + > |
| 72 | + <span>▣</span> Copy <kbd>Ctrl+C</kbd> |
| 73 | + </button> |
| 74 | + <button |
| 75 | + type="button" |
| 76 | + role="menuitem" |
| 77 | + onClick={() => run(interactions.pasteFromClipboard)} |
| 78 | + > |
| 79 | + <span>▤</span> Paste <kbd>Ctrl+V</kbd> |
| 80 | + </button> |
| 81 | + <div className="menu-rule" /> |
| 82 | + <button |
| 83 | + type="button" |
| 84 | + role="menuitem" |
| 85 | + onClick={() => run(interactions.clearSelection)} |
| 86 | + > |
| 87 | + <span>⌫</span> Clear contents |
| 88 | + </button> |
| 89 | + <div className="menu-rule" /> |
| 90 | + <button |
| 91 | + type="button" |
| 92 | + role="menuitem" |
| 93 | + onClick={() => |
| 94 | + run(() => table.setSorting([{ id: column.id, desc: false }])) |
| 95 | + } |
| 96 | + > |
| 97 | + <span>↑</span> Sort ascending |
| 98 | + </button> |
| 99 | + <button |
| 100 | + type="button" |
| 101 | + role="menuitem" |
| 102 | + onClick={() => |
| 103 | + run(() => table.setSorting([{ id: column.id, desc: true }])) |
| 104 | + } |
| 105 | + > |
| 106 | + <span>↓</span> Sort descending |
| 107 | + </button> |
| 108 | + </div>, |
| 109 | + document.body, |
| 110 | + ) |
| 111 | +} |
0 commit comments