@qijenchen/design-system 0.1.0-beta.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (119) hide show
  1. package/package.json +93 -0
  2. package/src/README.md +32 -0
  3. package/src/components/Accordion/accordion.tsx +104 -0
  4. package/src/components/Alert/alert.tsx +188 -0
  5. package/src/components/AppShell/_demo-helpers.tsx +198 -0
  6. package/src/components/AppShell/app-shell.tsx +364 -0
  7. package/src/components/AspectRatio/aspect-ratio.tsx +58 -0
  8. package/src/components/Avatar/avatar.tsx +368 -0
  9. package/src/components/Badge/badge.tsx +104 -0
  10. package/src/components/Breadcrumb/breadcrumb.tsx +609 -0
  11. package/src/components/BulkActionBar/bulk-action-bar.tsx +156 -0
  12. package/src/components/Button/button-group.tsx +96 -0
  13. package/src/components/Button/button.tsx +539 -0
  14. package/src/components/Calendar/calendar.tsx +411 -0
  15. package/src/components/Carousel/carousel.tsx +371 -0
  16. package/src/components/Chart/chart.tsx +376 -0
  17. package/src/components/Checkbox/checkbox-group.tsx +94 -0
  18. package/src/components/Checkbox/checkbox.tsx +237 -0
  19. package/src/components/Chip/chip.tsx +359 -0
  20. package/src/components/CircularProgress/circular-progress.tsx +204 -0
  21. package/src/components/Coachmark/coachmark.tsx +255 -0
  22. package/src/components/Combobox/combobox.tsx +826 -0
  23. package/src/components/Command/command.tsx +187 -0
  24. package/src/components/DataTable/active-editor-controller.ts +72 -0
  25. package/src/components/DataTable/cell-registry.tsx +520 -0
  26. package/src/components/DataTable/column-types.ts +180 -0
  27. package/src/components/DataTable/data-table-column-visibility-panel.tsx +261 -0
  28. package/src/components/DataTable/data-table-filter-panel.tsx +813 -0
  29. package/src/components/DataTable/data-table-interaction-layer.tsx +483 -0
  30. package/src/components/DataTable/data-table-sort-manager.tsx +210 -0
  31. package/src/components/DataTable/data-table.css +165 -0
  32. package/src/components/DataTable/data-table.tsx +2924 -0
  33. package/src/components/DataTable/filter-operators.ts +225 -0
  34. package/src/components/DataTable/filter-tree.ts +313 -0
  35. package/src/components/DataTable/lib/column-meta.ts +79 -0
  36. package/src/components/DateGrid/date-grid.tsx +209 -0
  37. package/src/components/DatePicker/date-picker.tsx +1114 -0
  38. package/src/components/DescriptionList/description-list.tsx +141 -0
  39. package/src/components/Dialog/dialog.tsx +267 -0
  40. package/src/components/DropdownMenu/dropdown-menu.tsx +475 -0
  41. package/src/components/Empty/empty.tsx +108 -0
  42. package/src/components/Field/field-context.ts +136 -0
  43. package/src/components/Field/field-types.ts +52 -0
  44. package/src/components/Field/field-wrapper.tsx +348 -0
  45. package/src/components/Field/field.tsx +535 -0
  46. package/src/components/FieldControlGroup/field-control-group.tsx +136 -0
  47. package/src/components/FileItem/file-item.tsx +322 -0
  48. package/src/components/FileUpload/file-upload.tsx +326 -0
  49. package/src/components/FileViewer/file-viewer-types.ts +76 -0
  50. package/src/components/FileViewer/file-viewer.tsx +1065 -0
  51. package/src/components/FileViewer/image-renderer.tsx +256 -0
  52. package/src/components/HoverCard/hover-card.tsx +79 -0
  53. package/src/components/Input/input.tsx +233 -0
  54. package/src/components/LinkInput/link-input.tsx +304 -0
  55. package/src/components/Menu/menu-item.tsx +334 -0
  56. package/src/components/NameCard/name-card.tsx +319 -0
  57. package/src/components/Notice/notice.tsx +196 -0
  58. package/src/components/NumberInput/number-input.tsx +203 -0
  59. package/src/components/OverflowIndicator/overflow-indicator.tsx +156 -0
  60. package/src/components/PeoplePicker/avatar-stack-overflow.ts +100 -0
  61. package/src/components/PeoplePicker/people-picker-helpers.ts +76 -0
  62. package/src/components/PeoplePicker/people-picker.tsx +455 -0
  63. package/src/components/PeoplePicker/person-display.tsx +358 -0
  64. package/src/components/Popover/popover.tsx +183 -0
  65. package/src/components/ProgressBar/progress-bar.tsx +157 -0
  66. package/src/components/README.md +58 -0
  67. package/src/components/RadioGroup/radio-group.tsx +261 -0
  68. package/src/components/Rating/rating.tsx +295 -0
  69. package/src/components/ScrollArea/scroll-area.tsx +110 -0
  70. package/src/components/SegmentedControl/segmented-control.tsx +304 -0
  71. package/src/components/Select/select.tsx +658 -0
  72. package/src/components/SelectMenu/select-menu.tsx +430 -0
  73. package/src/components/SelectionControl/selection-item.tsx +261 -0
  74. package/src/components/Separator/separator.tsx +48 -0
  75. package/src/components/Sheet/sheet.tsx +240 -0
  76. package/src/components/Sidebar/sidebar.tsx +1280 -0
  77. package/src/components/Skeleton/skeleton.tsx +35 -0
  78. package/src/components/Slider/slider.tsx +158 -0
  79. package/src/components/Steps/steps.tsx +850 -0
  80. package/src/components/Switch/switch.tsx +285 -0
  81. package/src/components/Tabs/tabs.tsx +515 -0
  82. package/src/components/Tag/tag.tsx +246 -0
  83. package/src/components/Textarea/textarea.tsx +280 -0
  84. package/src/components/TimePicker/time-columns.tsx +260 -0
  85. package/src/components/TimePicker/time-picker.tsx +419 -0
  86. package/src/components/Toast/toast.tsx +129 -0
  87. package/src/components/Tooltip/tooltip.tsx +68 -0
  88. package/src/components/TreeView/tree-view.tsx +1031 -0
  89. package/src/hooks/use-controllable.ts +40 -0
  90. package/src/hooks/use-is-narrow-viewport.ts +19 -0
  91. package/src/hooks/use-is-touch-device.ts +21 -0
  92. package/src/hooks/use-overflow-items.ts +256 -0
  93. package/src/index.ts +85 -0
  94. package/src/lib/README.md +82 -0
  95. package/src/lib/drag-visual.ts +272 -0
  96. package/src/lib/i18n/README.md +60 -0
  97. package/src/lib/i18n/i18n-context.tsx +129 -0
  98. package/src/lib/multi-select-ordering.ts +61 -0
  99. package/src/lib/utils.ts +93 -0
  100. package/src/patterns/README.md +67 -0
  101. package/src/patterns/element-anatomy/item-anatomy.tsx +744 -0
  102. package/src/patterns/header-canonical/chrome-header.tsx +175 -0
  103. package/src/patterns/header-canonical/header-canonical.css +27 -0
  104. package/src/patterns/horizontal-overflow/horizontal-overflow.tsx +217 -0
  105. package/src/patterns/overlay-surface/overlay-surface.tsx +191 -0
  106. package/src/patterns/resize-handle/resize-handle.tsx +188 -0
  107. package/src/stories-helpers/anatomy/anatomy-utils.tsx +64 -0
  108. package/src/tokens/README.md +53 -0
  109. package/src/tokens/color/primitives.css +429 -0
  110. package/src/tokens/color/semantic.css +539 -0
  111. package/src/tokens/elevation/overlay-geometry.ts +13 -0
  112. package/src/tokens/layoutSpace/layoutSpace.css +36 -0
  113. package/src/tokens/motion/motion.css +30 -0
  114. package/src/tokens/motion/motion.ts +17 -0
  115. package/src/tokens/opacity/opacity.css +23 -0
  116. package/src/tokens/radius/radius.css +19 -0
  117. package/src/tokens/typography/typography.css +118 -0
  118. package/src/tokens/uiSize/icon-size.ts +52 -0
  119. package/src/tokens/uiSize/uiSize.css +125 -0
@@ -0,0 +1,483 @@
1
+ /**
2
+ * DataTableInteractionLayer — Slice D Step 1A scaffold(per `.claude/planning/datatable-spreadsheet-rfc.md`)
3
+ *
4
+ * Singleton overlay root inside DataTable(M21 private,不抽 global pattern)。
5
+ * 5 sub-layer children(per RFC §Overlay Geometry):
6
+ * - HoverCellRect(z 1):hover 邊框,「one geometry owner, two paint owners」(Contract 8)
7
+ * - SelectionRect / RangeRect(z 1.5):spreadsheet mode 才顯示(Contract 5,defer)
8
+ * - ActiveEditorHost(z 3):portal active edit Field(Slice C wire-up)
9
+ * - NestedPortalRegistry(z 4):date/select popup 註冊為 inside editor(Contract 7)
10
+ *
11
+ * Slice D Step 1A scope:
12
+ * - getCellRect(cellId)geometry source(per Contract 8)
13
+ * - HoverCellRect 1 layer 先做(Contract 8 / Contract 15 cellClickEntersEdit predicate)
14
+ * - 默認 disabled via `experimentalSpreadsheetOverlay` flag,不破現有 outline
15
+ * - 後續 Step 1B/C/D 漸進切換 + 拆 outline
16
+ */
17
+
18
+ import * as React from 'react'
19
+
20
+ type CellId = string
21
+
22
+ interface CellRect {
23
+ x: number
24
+ y: number
25
+ width: number
26
+ height: number
27
+ }
28
+
29
+ interface DataTableInteractionLayerProps {
30
+ /** Flag default false:不破現有 outline。enable 後 hover/editor 走 overlay。 */
31
+ enabled: boolean
32
+ /** Container ref;layer absolute position 對 container origin */
33
+ containerRef: React.RefObject<HTMLDivElement | null>
34
+ /** Per Contract 15:`cellClickEntersEdit` predicate;hover overlay 顯示與否 */
35
+ cellClickEntersEdit?: (cellId: CellId) => boolean
36
+ /**
37
+ * Slice D Step 3 scaffold(2026-05-10):active editor cell id。null = 無 active editor。
38
+ * 當前只 render rect placeholder layer(z-index 3,higher than hover ring z-index 1)。
39
+ * Future:ActiveEditorHost portal active edit Field per Contract 8 「two paint owners」。
40
+ * Wire-up 走 codex Q-7 string-first canary(text cell first,picker types 漸進)。
41
+ */
42
+ activeEditorCellId?: CellId | null
43
+ /**
44
+ * Slice D Step 3.x flag:啟用 ActiveEditorHost dashed scaffold rect rendering。
45
+ * 2026-05-10 bug fix(user 圖1 雙 ring 同時顯示):dashed scaffold 之前綁
46
+ * `enabled`(= experimentalSpreadsheetOverlay)→ 任何 cell 進 edit mode 就 leak
47
+ * 出 dashed rect。改 gate 給 `experimentalActiveEditorController`(Step 3.3 真
48
+ * portal Field 工作),hover overlay scope 不會看到 dashed scaffold。
49
+ */
50
+ activeEditorEnabled?: boolean
51
+ /**
52
+ * Slice D Step 4(spreadsheet semantics,2026-05-10 user 圖1 ask + RFC Contract 5):
53
+ * Excel-like 選取 cell ID。Click 1 select / Click 2 enter edit。
54
+ * Layer renders solid border SelectionRect(per user「不要 dash 直接實的就好」)。
55
+ */
56
+ selectedCellId?: CellId | null
57
+ /**
58
+ * Slice D Step 4 range support(per user「應該支援 range」+ codex Q2.1 Airtable/AG Grid
59
+ * cite):range cell IDs from anchor↔focus rectangle。Layer renders bg-fill RangeRect
60
+ * (z 1,bg `--primary-subtle`)+ selection border on focus cell。
61
+ */
62
+ rangeCellIds?: CellId[]
63
+ /**
64
+ * Slice D Step 5(D.3 portal Field,2026-05-10 user 拍板「在乎完美乾淨」+ codex Q6.1
65
+ * 撤回 defer):active editor render callback。當 activeEditorEnabled + activeEditorCellId
66
+ * truthy → layer call this 拿 React node(已 bind value/onCommit/onCancel from controller),
67
+ * 在 ActiveEditorHost portal(z 3,float rect)render 之。
68
+ *
69
+ * Per codex Q6.2 outline:cell 永遠 mode="display"(SSOT preserved);portal host 渲
70
+ * mode="edit" 同 registry component;float pass-through + z-index 3 cover display below。
71
+ */
72
+ activeEditorRender?: (cellId: CellId, rect: CellRect) => React.ReactNode
73
+ }
74
+
75
+ /**
76
+ * `getCellRect(cellId)` geometry source(per Contract 8)。
77
+ *
78
+ * 從 DOM 量(per-call `getBoundingClientRect()`)而非 layoutCache,因為:
79
+ * 1. cell DOM 是 React-rendered,getBoundingClientRect 永遠是 source of truth
80
+ * 2. layoutCache 需另寫 invalidation logic,added complexity
81
+ * 3. Hover overlay re-render 頻率低(per cell hover,不是 per frame)
82
+ *
83
+ * 0.5px sub-pixel snap per RFC §Overlay Geometry。
84
+ */
85
+ function getCellRect(containerEl: HTMLElement | null, cellId: CellId): CellRect | null {
86
+ if (!containerEl) return null
87
+ const cellEl = containerEl.querySelector<HTMLElement>(`[data-cell-id="${cellId}"]`)
88
+ if (!cellEl) return null
89
+ // Page-absolute coords for viewport-fixed layer(per Step 1C bug fix:layer
90
+ // 用 position:fixed,讓 paint owner 跟 reference frame 解耦 outer container 的 positioning)。
91
+ // 2026-05-10 v3 fix(user 圖5 SSOT consistency):**float coords no rounding**。
92
+ // Cell 自身 layout 用 sub-pixel float position(CSS 不 snap to integer)— 跨 cell 累積
93
+ // 常見 144.328 / 244.0 / 524.172 等 fractional。任何 Math.round / floor / ceil 都引入
94
+ // varying rounding error(visual audit verified:dx=0.17/0.5/0.83 不一致)。
95
+ // Float pass-through + outline + outline-offset:-1px(下方 paint)→ overlay 永遠跟
96
+ // cell exact pixel-perfect overlap,user verbatim「在內容起始位置不變的前提下讓
97
+ // overlay 的邊框直接剛好壓住 cell 邊框」達成。
98
+ const rect = cellEl.getBoundingClientRect()
99
+ return {
100
+ x: rect.x,
101
+ y: rect.y,
102
+ width: rect.width,
103
+ height: rect.height,
104
+ }
105
+ }
106
+
107
+ /**
108
+ * Issue 6 viewport clip(2026-05-10):cell 跟 panel(left/center/right)bounding rect 一起取,
109
+ * layer 用 panel rect 當 clip viewport intersect cell rect → overlay 永不畫到 panel 外。
110
+ *
111
+ * Per codex 13-issues verdict:「getCellRect 找最近 scroll viewport;intersect(cellRect, viewportRect),
112
+ * 空 → 不 render,部分 → clipped rect render。」
113
+ *
114
+ * 對齊 AG Grid `cellsForRangeSet` viewport-aware paint / Glide DataGrid clip helpers /
115
+ * Notion sticky-cell virtualizer mask。
116
+ */
117
+ interface CellGeometry {
118
+ /** Cell exact viewport rect(no rounding,float pass-through per Bug 5 SSOT) */
119
+ rect: CellRect
120
+ /** Nearest panel container rect(left/center/right body)— clip viewport for overlays */
121
+ clipRect: CellRect
122
+ /** Panel identifier(`left` / `center` / `right`)— grouped range outer ring 分段用 */
123
+ panel: 'left' | 'center' | 'right'
124
+ }
125
+
126
+ function getCellGeometry(containerEl: HTMLElement | null, cellId: CellId): CellGeometry | null {
127
+ if (!containerEl) return null
128
+ const cellEl = containerEl.querySelector<HTMLElement>(`[data-cell-id="${cellId}"]`)
129
+ if (!cellEl) return null
130
+ const panelEl = cellEl.closest<HTMLElement>('[data-datatable-panel]')
131
+ if (!panelEl) return null
132
+ const panel = panelEl.dataset.datatablePanel as 'left' | 'center' | 'right'
133
+ const cellR = cellEl.getBoundingClientRect()
134
+ const panelR = panelEl.getBoundingClientRect()
135
+ return {
136
+ rect: { x: cellR.x, y: cellR.y, width: cellR.width, height: cellR.height },
137
+ clipRect: { x: panelR.x, y: panelR.y, width: panelR.width, height: panelR.height },
138
+ panel,
139
+ }
140
+ }
141
+
142
+
143
+ /**
144
+ * Reactive rect sync hook — 確保 overlay rect 跨 RWD / scroll / resize 永遠對齊 cell(2026-05-10
145
+ * user 拍板「所有 overlay 在各種 RWD 和捲動時都能處在正確位置上」)。
146
+ *
147
+ * 監聽 3 source(rAF coalesce 避 frame storm):
148
+ * 1. window scroll(capture mode 抓 nested table body scroll + page scroll)
149
+ * 2. window resize(viewport size change → cell width 變)
150
+ * 3. ResizeObserver on container(column resize / table dimension 變)
151
+ *
152
+ * 每次事件 fire → setVersion(v+1) → 觸發 layer re-render → rect 用最新 getBoundingClientRect
153
+ * 重算。getBoundingClientRect 本身永遠 return 當下 viewport-relative coords,沒 stale cache。
154
+ *
155
+ * Per codex(brief: codex-brief-rwd-future-2026-05-10):rAF coalesce 不需要 debounce,scroll
156
+ * 跟到不延遲才對(Glide / AG Grid 同 idiom)。
157
+ */
158
+ function useReactiveRect(containerRef: React.RefObject<HTMLElement | null>, enabled: boolean) {
159
+ const [, setVersion] = React.useState(0)
160
+ React.useEffect(() => {
161
+ if (!enabled) return
162
+ let rafId: number | null = null
163
+ const force = () => {
164
+ if (rafId !== null) return
165
+ rafId = requestAnimationFrame(() => {
166
+ rafId = null
167
+ setVersion((v) => v + 1)
168
+ })
169
+ }
170
+ window.addEventListener('scroll', force, { capture: true, passive: true })
171
+ window.addEventListener('resize', force, { passive: true })
172
+ const container = containerRef.current
173
+ let ro: ResizeObserver | null = null
174
+ if (container && typeof ResizeObserver !== 'undefined') {
175
+ ro = new ResizeObserver(force)
176
+ ro.observe(container)
177
+ }
178
+ return () => {
179
+ window.removeEventListener('scroll', force, { capture: true } as EventListenerOptions)
180
+ window.removeEventListener('resize', force)
181
+ if (ro) ro.disconnect()
182
+ if (rafId !== null) cancelAnimationFrame(rafId)
183
+ }
184
+ }, [enabled, containerRef])
185
+ }
186
+
187
+ /**
188
+ * Hover state hook — 監聽 container hover events,track hovered cell id。
189
+ *
190
+ * Implementation:event delegation on container(不 per-cell mount listeners),
191
+ * 利用 `data-cell-id` attribute + closest()。
192
+ */
193
+ function useHoveredCell(
194
+ containerRef: React.RefObject<HTMLElement | null>,
195
+ enabled: boolean,
196
+ ): CellId | null {
197
+ const [hoveredCellId, setHoveredCellId] = React.useState<CellId | null>(null)
198
+
199
+ React.useEffect(() => {
200
+ if (!enabled) { setHoveredCellId(null); return }
201
+ const container = containerRef.current
202
+ if (!container) return
203
+
204
+ const handleMouseOver = (e: MouseEvent) => {
205
+ const target = e.target as HTMLElement | null
206
+ const cellEl = target?.closest<HTMLElement>('[data-cell-id]')
207
+ const id = cellEl?.dataset.cellId ?? null
208
+ setHoveredCellId(id)
209
+ }
210
+ const handleMouseLeave = () => setHoveredCellId(null)
211
+
212
+ container.addEventListener('mouseover', handleMouseOver)
213
+ container.addEventListener('mouseleave', handleMouseLeave)
214
+ return () => {
215
+ container.removeEventListener('mouseover', handleMouseOver)
216
+ container.removeEventListener('mouseleave', handleMouseLeave)
217
+ }
218
+ }, [enabled, containerRef])
219
+
220
+ return hoveredCellId
221
+ }
222
+
223
+ /**
224
+ * Singleton interaction layer。
225
+ *
226
+ * Slice D Step 1A:HoverCellRect only。
227
+ * Slice D Step 2/3/4:SelectionRect / ActiveEditorHost / RangeRect 漸進加入。
228
+ */
229
+ export function DataTableInteractionLayer({
230
+ enabled,
231
+ containerRef,
232
+ cellClickEntersEdit,
233
+ activeEditorCellId,
234
+ activeEditorEnabled = false,
235
+ activeEditorRender,
236
+ selectedCellId = null,
237
+ // rangeCellIds retired 2026-05-10(outer ring 已 retire,range visual 靠 cell-bg `[data-range-cell]`)
238
+ rangeCellIds: _rangeCellIds,
239
+ }: DataTableInteractionLayerProps) {
240
+ const hoveredCellId = useHoveredCell(containerRef, enabled)
241
+ // 2026-05-10 RWD/scroll sync(per user mandate「所有 overlay 在各種 RWD 和捲動時都處在正確位置」):
242
+ // window scroll(capture)+ resize + ResizeObserver(container)→ rAF coalesced re-render → rect 重算
243
+ useReactiveRect(containerRef, enabled)
244
+
245
+ // Contract 14 state precedence:editing > selected > hover(per RFC §State Precedence Matrix)
246
+ const isEditingHovered = activeEditorCellId != null && activeEditorCellId === hoveredCellId
247
+ const isSelectedHovered = selectedCellId != null && selectedCellId === hoveredCellId
248
+ const shouldShowHover = enabled && hoveredCellId != null && !isEditingHovered && !isSelectedHovered
249
+ && (cellClickEntersEdit ? cellClickEntersEdit(hoveredCellId) : true)
250
+
251
+ // Issue 6 viewport clip(2026-05-10):每個 overlay rect 都跟最近 panel(left/center/right body)
252
+ // intersect。Cell scroll 出 panel viewport(H scroll / pinned 範圍)→ rect null → 不 render;
253
+ // 部分 → 用 ClipMask wrap 在 panel viewport 內裁切,outline 永遠在 cell 真邊但被 mask 限制視覺。
254
+ const hoverGeo = shouldShowHover
255
+ ? getCellGeometry(containerRef.current, hoveredCellId!)
256
+ : null
257
+
258
+ // Slice D Step 3 scaffold:ActiveEditorHost rect placeholder(per Contract 8 active editor paint owner)。
259
+ // 2026-05-10 bug fix:gate by `activeEditorEnabled` flag(Step 3.3 portal Field work)。
260
+ // Issue 6:active editor host **不**做 viewport clip(per codex「editor 必可見」),
261
+ // 仍走原 path 從 getCellRect 取 viewport coords。User 應將 active editor cell 滾到 viewport 內。
262
+ const activeEditorRect = activeEditorEnabled && activeEditorCellId != null
263
+ ? getCellRect(containerRef.current, activeEditorCellId)
264
+ : null
265
+
266
+ // Slice D Step 4(spreadsheet semantics):SelectionRect + RangeRect with viewport clip。
267
+ const selectedGeo = enabled && selectedCellId != null
268
+ ? getCellGeometry(containerRef.current, selectedCellId)
269
+ : null
270
+
271
+ // 2026-05-10 retire `rangeOuterRingsByPanel` calc + render(per user 抓 range 2px outer
272
+ // ring 不需要 — cell-bg `--primary-subtle` 已給「這些 cell 在範圍內」訊號,outer ring 是
273
+ // redundant visual)。`CellRangeOuterRing` primitive 已 retire(下方刪),getCellGeometry 仍
274
+ // 給 hover / selected overlay 使用,保留。Future 若需 outer ring 復用,從 git history 取
275
+ // commit `763b3ac`(Issue 6 viewport clip)的 implementation。
276
+
277
+ if (!enabled) return null
278
+
279
+ // Layer absolute fixed-position relative to viewport(避 outer container 缺 position:relative
280
+ // 找錯 reference frame)。getCellRect 已 return container-relative,但這裡 set top/left 用
281
+ // page coords 一致 — 改用 viewport-fixed 簡化。
282
+ return (
283
+ <div
284
+ aria-hidden
285
+ style={{ position: 'fixed', inset: 0, pointerEvents: 'none', zIndex: 1 }}
286
+ >
287
+ {/* HoverRing — kind=hover(1px var(--border-hover)) z 1。Wrap 在 ClipMask(panel viewport)
288
+ 內 → cell scroll 出 panel 時 outline 自動被 mask 裁切,不會 leak 到 panel 外。 */}
289
+ {hoverGeo && (
290
+ <ClipMask clipRect={hoverGeo.clipRect}>
291
+ <CellRingOverlay rect={toRelRect(hoverGeo.rect, hoverGeo.clipRect)} kind="hover" />
292
+ </ClipMask>
293
+ )}
294
+ {/* SelectionRing — kind=selected(1px var(--primary)) z 2,SSOT 同 hover wide */}
295
+ {selectedGeo && (
296
+ <ClipMask clipRect={selectedGeo.clipRect}>
297
+ <CellRingOverlay
298
+ rect={toRelRect(selectedGeo.rect, selectedGeo.clipRect)}
299
+ kind="selected"
300
+ cellId={selectedCellId!}
301
+ />
302
+ </ClipMask>
303
+ )}
304
+ {/* 2026-05-10 retire RangeOuterRing(per user 抓 image 4 + verbatim「range 的 cell 本來就有顏色變化,
305
+ 那樣就夠了,不需要再有 2px 藍色的框」)。Range visual now relies purely on cell-bg
306
+ (`--primary-subtle` via `[data-range-cell]` CSS in `data-table.css`)+ focus cell 2px selected
307
+ border。Outer 2px primary ring 從 Issue 6 ship 但 visual 太重 — bg-fill 已給「這些 cell 在範圍內」
308
+ 訊號,outer ring 是 redundant。`rangeOuterRingsByPanel` 計算保留但不 render(future 若需 reinstate
309
+ 只需打開 .map)。 */}
310
+ {/* {rangeOuterRingsByPanel.map((group) => (
311
+ <ClipMask key={group.panel} clipRect={group.clipRect}>
312
+ <CellRangeOuterRing rect={toRelRect(group.bbox, group.clipRect)} />
313
+ </ClipMask>
314
+ ))} */}
315
+ {/* ActiveEditorHost — z 3,float rect,pointerEvents:auto opaque host(NOT a ring)。
316
+ Issue 6:active editor 不 clip(editor 必可見) — user 滾出 viewport 自負責。 */}
317
+ {activeEditorRect && (
318
+ <ActiveEditorHost rect={activeEditorRect}>
319
+ {activeEditorRender ? activeEditorRender(activeEditorCellId!, activeEditorRect) : null}
320
+ </ActiveEditorHost>
321
+ )}
322
+ </div>
323
+ )
324
+ }
325
+
326
+ /**
327
+ * `toRelRect(cellRect, clipRect)` — Issue 6 helper(2026-05-10):
328
+ * 把 viewport coords cell rect 轉成 ClipMask containing-block 內 relative coords。
329
+ * ClipMask 用 panel rect 做 absolute container + overflow:hidden,內部 child absolute 定位
330
+ * 以 mask 為 reference frame,所以 cellRect.x - clipRect.x 才對齊 cell 真實位置。
331
+ */
332
+ function toRelRect(cellRect: CellRect, clipRect: CellRect): CellRect {
333
+ return {
334
+ x: cellRect.x - clipRect.x,
335
+ y: cellRect.y - clipRect.y,
336
+ width: cellRect.width,
337
+ height: cellRect.height,
338
+ }
339
+ }
340
+
341
+ // ── Private primitives(per codex unified-ring-overlay-2026-05-10 Final A')──────
342
+ //
343
+ // 三個 private primitive 各自 own 一個 paint concern:
344
+ // 1. CellRingOverlay — outline ring(hover / selected / future focus / error)
345
+ // 2. CellRangeFill — bg fill(range cells in spreadsheet mode)
346
+ // 3. ActiveEditorHost — opaque host(portal Field edit)
347
+ //
348
+ // SSOT pattern(共 3 primitives):rect float pass-through + boxSizing:border-box +
349
+ // pointerEvents 視 kind / position:absolute on viewport-fixed layer。
350
+ //
351
+ // Token mapping 集中 `CELL_RING_STYLES`(per codex Q3 verdict 不開 global `--cell-ring-*` token,
352
+ // 在 primitive 內 kind → semantic token mapping)。
353
+
354
+ const CELL_RING_STYLES = {
355
+ hover: { width: 1, color: 'var(--border-hover)', zIndex: 1 },
356
+ selected: { width: 1, color: 'var(--primary)', zIndex: 2 },
357
+ // future kinds(per codex Q6 outline,加 entry 即可擴展):
358
+ // focus: { width: 2, color: 'var(--primary)', zIndex: 2 },
359
+ // error: { width: 1, color: 'var(--error)', zIndex: 2 },
360
+ } as const
361
+
362
+ type CellRingKind = keyof typeof CELL_RING_STYLES
363
+
364
+ function rectStyle(rect: CellRect): React.CSSProperties {
365
+ return {
366
+ position: 'absolute',
367
+ left: rect.x,
368
+ top: rect.y,
369
+ width: rect.width,
370
+ height: rect.height,
371
+ }
372
+ }
373
+
374
+ /**
375
+ * CellRingOverlay — paint owner for hover / selected / future focus / error rings。
376
+ *
377
+ * 共用 SSOT(per Bug 5 fix + codex Q2 unified primitive):
378
+ * - Float pass-through rect(getCellRect 不 round)
379
+ * - outline + outline-offset:`-${width}px` paint 在 cell 既有 border 上 in-place
380
+ * - boxSizing:border-box(避免 outline 影響 layout)
381
+ * - pointerEvents:none(透視點擊穿透)
382
+ * - transition:none(避 fade flash)
383
+ */
384
+ function CellRingOverlay({ rect, kind, cellId }: {
385
+ rect: CellRect
386
+ kind: CellRingKind
387
+ cellId?: CellId
388
+ }) {
389
+ const ring = CELL_RING_STYLES[kind]
390
+ const dataAttr = kind === 'selected' && cellId ? { 'data-selected-cell-id': cellId } : {}
391
+ return (
392
+ <div
393
+ aria-hidden
394
+ style={{
395
+ ...rectStyle(rect),
396
+ outline: `${ring.width}px solid ${ring.color}`,
397
+ outlineOffset: `-${ring.width}px`,
398
+ boxSizing: 'border-box',
399
+ pointerEvents: 'none',
400
+ transition: 'none',
401
+ zIndex: ring.zIndex,
402
+ }}
403
+ {...dataAttr}
404
+ />
405
+ )
406
+ }
407
+
408
+ // `CellRangeOuterRing` primitive retired 2026-05-10(per user 「range cell 本來就有顏色變化
409
+ // 那樣就夠了,不需要再有 2px 藍色的框」)。Source 留 git history commit `763b3ac`(Issue 6
410
+ // viewport clip ship)若需 reinstate。
411
+
412
+ /**
413
+ * ClipMask — Issue 6 primitive(2026-05-10):panel viewport 裁切容器。
414
+ *
415
+ * 用在 hover / selected / range outer ring overlay,避免 cell 滾出 panel viewport(H scroll /
416
+ * pinned 範圍)時 overlay 仍漂浮在 panel 外。Layer 是 `position: fixed` viewport-anchor,
417
+ * ClipMask 用 panel rect 當 absolute container + `overflow: hidden` mask;child overlay 用
418
+ * `toRelRect(cellRect, clipRect)` 算出 mask-relative 座標,在 mask 內精準定位但被 mask 邊緣裁切。
419
+ *
420
+ * 對齊 AG Grid `cellsForRangeSet` viewport-aware paint / Glide DataGrid clip helpers /
421
+ * Notion sticky-cell virtualizer mask。
422
+ */
423
+ function ClipMask({ clipRect, children }: { clipRect: CellRect; children: React.ReactNode }) {
424
+ return (
425
+ <div
426
+ aria-hidden
427
+ style={{
428
+ position: 'absolute',
429
+ left: clipRect.x,
430
+ top: clipRect.y,
431
+ width: clipRect.width,
432
+ height: clipRect.height,
433
+ overflow: 'hidden',
434
+ pointerEvents: 'none',
435
+ }}
436
+ >
437
+ {children}
438
+ </div>
439
+ )
440
+ }
441
+
442
+ /**
443
+ * ActiveEditorHost — opaque host for portal Field edit(D.3,Slice D Step 5)。
444
+ *
445
+ * Per codex Q2 verdict:不該進 CellRingOverlay primitive — 它是 host 不是 ring。
446
+ * - pointerEvents:auto(child Field 接收 click / keyboard)
447
+ * - background:var(--canvas)(opaque cover display Field below;Cell SSOT 保留)
448
+ * - z-index 3(above hover / selected / range)
449
+ * - children = activeEditorRender 的回傳 React node
450
+ *
451
+ * Empty children fallback = dashed debug indicator(no portal Field provided)。
452
+ */
453
+ function ActiveEditorHost({ rect, children }: { rect: CellRect; children: React.ReactNode }) {
454
+ if (!children) {
455
+ return (
456
+ <div
457
+ aria-hidden
458
+ style={{
459
+ ...rectStyle(rect),
460
+ pointerEvents: 'none',
461
+ zIndex: 3,
462
+ border: '1px dashed var(--primary)',
463
+ boxSizing: 'border-box',
464
+ }}
465
+ data-active-editor-host-scaffold
466
+ />
467
+ )
468
+ }
469
+ return (
470
+ <div
471
+ style={{
472
+ ...rectStyle(rect),
473
+ pointerEvents: 'auto',
474
+ zIndex: 3,
475
+ boxSizing: 'border-box',
476
+ background: 'var(--canvas)',
477
+ }}
478
+ data-active-editor-host
479
+ >
480
+ {children}
481
+ </div>
482
+ )
483
+ }
@@ -0,0 +1,210 @@
1
+ // @benchmark-unverified-blanket: file-level retraction per M22 (d) — claims herein not individually URL-cited; treat as unverified visual/usage rumor unless retrofit per-claim. Hook escape preserved.
2
+ // same-row-mixed-allow: header chrome corner buttons(refresh/close)跟 row inline actions(drag/trash)不在同 row
3
+ import * as React from 'react'
4
+ import { Plus, Trash2, X as XIcon, RotateCcw, GripVertical } from 'lucide-react'
5
+ import type { ColumnDef, SortingState } from '@tanstack/react-table'
6
+ import { DndContext, closestCenter, type DragEndEvent } from '@dnd-kit/core'
7
+ import { SortableContext, useSortable, verticalListSortingStrategy } from '@dnd-kit/sortable'
8
+ import { CSS } from '@dnd-kit/utilities'
9
+ import { cn } from '@/lib/utils'
10
+ import { dragSourceStyle, dragHandleCursorClass } from '@/design-system/lib/drag-visual'
11
+ import { Button } from '@/design-system/components/Button/button'
12
+ import { Select, type SelectOption } from '@/design-system/components/Select/select'
13
+ import { SurfaceHeader, SurfaceBody } from '@/design-system/patterns/overlay-surface/overlay-surface'
14
+ import { ButtonDivider } from '@/design-system/components/Button/button-group'
15
+ import { PopoverTitle, PopoverClose } from '@/design-system/components/Popover/popover'
16
+ import { ItemInlineActionButton } from '@/design-system/patterns/element-anatomy/item-anatomy'
17
+ import { getColumnId, getColumnLabel } from './lib/column-meta'
18
+
19
+ /**
20
+ * DataTableSortManager — Notion-style 多欄排序管理 panel
21
+ *
22
+ * 對齊 ref/進階篩選/sort.png 設計:
23
+ * Header(title + refresh + close)/ list(field + direction + delete + reorder)/
24
+ * footer(+ 加排序)。
25
+ *
26
+ * Source-of-truth: TanStack `SortingState`(同 `useReactTable.state.sorting`)。
27
+ * 跟 cell click sort 共享 state — single source-of-truth across cell + panel。
28
+ *
29
+ * MVP: reorder 用 ↑/↓ button(DnD 留 phase 2 跟 column reorder 一起做)。
30
+ */
31
+
32
+ interface SortColumn {
33
+ id: string
34
+ label: string
35
+ enableSorting?: boolean
36
+ }
37
+
38
+ export interface DataTableSortManagerProps<TData> {
39
+ /** 可排序欄位來源(讀 columnDef.header / id);會自動排除 enableSorting=false */
40
+ columns: ColumnDef<TData, any>[]
41
+ /** 當前排序 state(TanStack SortingState) */
42
+ sorting: SortingState
43
+ /** 排序變更 callback */
44
+ onSortingChange: (next: SortingState) => void
45
+ /** Refresh 按鈕點擊(可選 — 重置或外部 refetch) */
46
+ onReset?: () => void
47
+ /** Close 按鈕點擊(若有 — 通常是包在 Popover 外層的 close 行為) */
48
+ onClose?: () => void
49
+ className?: string
50
+ }
51
+
52
+ function extractColumns<TData>(columns: ColumnDef<TData, any>[]): SortColumn[] {
53
+ const out: SortColumn[] = []
54
+ for (const col of columns) {
55
+ const id = getColumnId(col)
56
+ if (!id || id === '__select__') continue
57
+ if (col.enableSorting === false) continue
58
+ out.push({ id, label: getColumnLabel(col, id), enableSorting: true })
59
+ }
60
+ return out
61
+ }
62
+
63
+ const DIRECTION_OPTIONS: SelectOption[] = [
64
+ { value: 'asc', label: '升冪' },
65
+ { value: 'desc', label: '降冪' },
66
+ ]
67
+
68
+ export function DataTableSortManager<TData>({
69
+ columns,
70
+ sorting,
71
+ onSortingChange,
72
+ onReset,
73
+ onClose,
74
+ className,
75
+ }: DataTableSortManagerProps<TData>) {
76
+ const sortableColumns = React.useMemo(() => extractColumns(columns), [columns])
77
+ const fieldOptions: SelectOption[] = React.useMemo(
78
+ () => sortableColumns.map((c) => ({ value: c.id, label: c.label })),
79
+ [sortableColumns]
80
+ )
81
+
82
+ const updateAt = (index: number, patch: Partial<{ id: string; desc: boolean }>) => {
83
+ const next = sorting.map((s, i) => (i === index ? { ...s, ...patch } : s))
84
+ onSortingChange(next)
85
+ }
86
+ const removeAt = (index: number) => {
87
+ onSortingChange(sorting.filter((_, i) => i !== index))
88
+ }
89
+ const handleDragEnd = (event: DragEndEvent) => {
90
+ const { active, over } = event
91
+ if (!over || active.id === over.id) return
92
+ const oldIndex = sorting.findIndex((s) => s.id === active.id)
93
+ const newIndex = sorting.findIndex((s) => s.id === over.id)
94
+ if (oldIndex < 0 || newIndex < 0) return
95
+ const next = [...sorting]
96
+ const [moved] = next.splice(oldIndex, 1)
97
+ next.splice(newIndex, 0, moved)
98
+ onSortingChange(next)
99
+ }
100
+ const addSort = () => {
101
+ const used = new Set(sorting.map((s) => s.id))
102
+ const firstUnused = sortableColumns.find((c) => !used.has(c.id))
103
+ if (!firstUnused) return
104
+ onSortingChange([...sorting, { id: firstUnused.id, desc: false }])
105
+ }
106
+
107
+ // K11 fix(2026-05-04): viewport-aware scroll chain invariant — 詳 overlay-surface.spec.md
108
+ // 2026-05-23 Phase A.4 Decision 2: w-[480px] → token `--data-table-sort-panel-width`(SSOT in uiSize.css)
109
+ return (
110
+ <div className={cn('flex flex-col h-full min-h-0 w-[var(--data-table-sort-panel-width)]', className)}>
111
+ {/* Popover 派輕量 chrome — slot 縮 20 匹配 PopoverTitle text-body line-height,header 自然 ~45px */}
112
+ <SurfaceHeader className="[--chrome-slot-h:1.25rem]">
113
+ <PopoverTitle className="flex-1">排序</PopoverTitle>
114
+ {onReset && sorting.length > 0 && (
115
+ <>
116
+ <Button variant="text" size="sm" iconOnly startIcon={RotateCcw} aria-label="重置" onClick={onReset} />
117
+ {onClose && <ButtonDivider />}
118
+ </>
119
+ )}
120
+ {onClose && (
121
+ <PopoverClose asChild>
122
+ <Button data-dismiss iconOnly dismiss size="sm" startIcon={XIcon} aria-label="關閉" onClick={onClose} />
123
+ </PopoverClose>
124
+ )}
125
+ </SurfaceHeader>
126
+
127
+ {/* Body — 條件 list + inline 加排序 CTA(對齊 filter panel canonical Q3+Q6,2026-05-04)
128
+ 無條件時 CTA 直接顯示,不需要 Empty 大區塊 */}
129
+ <SurfaceBody className="flex flex-col gap-[var(--layout-space-tight)]">
130
+ {sorting.length > 0 && (
131
+ <DndContext collisionDetection={closestCenter} onDragEnd={handleDragEnd}>
132
+ <SortableContext items={sorting.map(s => s.id)} strategy={verticalListSortingStrategy}>
133
+ {sorting.map((sort, index) => {
134
+ const usedByOthers = new Set(sorting.filter((_, i) => i !== index).map((s) => s.id))
135
+ const optionsForRow = fieldOptions.filter((o) => !usedByOthers.has(o.value))
136
+ return (
137
+ <SortRow
138
+ key={sort.id}
139
+ sort={sort}
140
+ optionsForRow={optionsForRow}
141
+ onChangeId={(v) => updateAt(index, { id: v })}
142
+ onChangeDir={(v) => updateAt(index, { desc: v === 'desc' })}
143
+ onRemove={() => removeAt(index)}
144
+ />
145
+ )
146
+ })}
147
+ </SortableContext>
148
+ </DndContext>
149
+ )}
150
+
151
+ {/* B1 加排序 → tertiary,parallel filter「加篩選」(root-level CTA 對等視覺重量) */}
152
+ <div>
153
+ <Button
154
+ variant="tertiary"
155
+ size="sm"
156
+ startIcon={Plus}
157
+ onClick={addSort}
158
+ disabled={sorting.length >= sortableColumns.length}
159
+ >
160
+ 加排序
161
+ </Button>
162
+ </div>
163
+ </SurfaceBody>
164
+ </div>
165
+ )
166
+ }
167
+
168
+ DataTableSortManager.displayName = 'DataTableSortManager'
169
+
170
+ // SortRow:DnD-enabled row。GripVertical 為 drag listener handle(對齊 Notion / Airtable 拖曳 idiom)。
171
+ function SortRow({
172
+ sort, optionsForRow, onChangeId, onChangeDir, onRemove,
173
+ }: {
174
+ sort: { id: string; desc: boolean }
175
+ optionsForRow: SelectOption[]
176
+ onChangeId: (v: string) => void
177
+ onChangeDir: (v: string) => void
178
+ onRemove: () => void
179
+ }) {
180
+ const { attributes, listeners, setNodeRef, transform, transition, isDragging } = useSortable({ id: sort.id })
181
+ // 對齊 lib/drag-visual.ts SSOT:source dim 用 `--opacity-disabled` token(不 hardcode 0.5)
182
+ const style: React.CSSProperties = {
183
+ transform: CSS.Transform.toString(transform),
184
+ transition,
185
+ ...dragSourceStyle(isDragging),
186
+ }
187
+ // **#5 fix(2026-05-04)**:row 內水平 gap = gap-2 (8px),layoutSpace 規則 5 緊密相關
188
+ // SurfaceBody row↔row vertical 仍 tight(12)— 不同 row 不緊密
189
+ return (
190
+ <div ref={setNodeRef} style={style} className="flex items-center gap-2">
191
+ <ItemInlineActionButton
192
+ icon={GripVertical}
193
+ size="sm"
194
+ aria-label="拖曳重排"
195
+ className={dragHandleCursorClass}
196
+ {...attributes}
197
+ {...listeners}
198
+ />
199
+ <div className="flex-1 min-w-0">
200
+ <Select size="sm" options={optionsForRow} value={sort.id} onChange={onChangeId} />
201
+ </div>
202
+ <div className="w-32 shrink-0">
203
+ {/* minRows={2} — 升冪/降冪只 2 選項,顯式縮 menu 高度(Q5) */}
204
+ <Select size="sm" options={DIRECTION_OPTIONS} value={sort.desc ? 'desc' : 'asc'} onChange={onChangeDir} minRows={2} />
205
+ </div>
206
+ {/* Trash 用 text Button(Q4 對齊 filter panel)— form-control row 必 Field 同高 */}
207
+ <Button variant="text" size="sm" iconOnly startIcon={Trash2} aria-label="刪除" onClick={onRemove} />
208
+ </div>
209
+ )
210
+ }