From 0edd9e9e817e5d51b32ba0c4a02a2ae7a1ef6ea5 Mon Sep 17 00:00:00 2001 From: Menghuan1918 Date: Sat, 14 Nov 2026 00:30:33 +0800 Subject: [PATCH] refactor(clues): browse and pack rows use the shared row/state components The pack shelf and the browse results each carried their own row shell, their own empty state and their own error+retry line. They now share ListRow, EmptyState and ErrorBlock, and the local copies are gone. ListRow grew the two contracts those callers actually need: - titleLabel so the accessible name can be "Manage pack: X" while the visible title stays "X"; - primary so the roving-focus marker (data-list-primary + its tab stop) lands on the title button, which is what useListNavigation focuses; - a navigating row keeps name + subtitle inside that button, so the whole two-line caption is one click target and one accessible name. Removed from clues-tab.module.css: the .packRow/.packOpen/.packGlyph/ .packName/.packMeta/.packCount/.packReference family plus the two coarse- pointer orphans left behind. tests/clues-tab-css.spec.ts now asserts that contract against the shared patterns sheet, where the rules actually live. --- src/client/clues/tab/browser.tsx | 106 +++++++++++----------- src/client/clues/tab/clues-tab.module.css | 84 +---------------- src/client/clues/tab/packs.tsx | 78 ++++++++-------- src/client/ui/patterns.module.css | 25 +++-- src/client/ui/patterns.tsx | 78 +++++++++++----- tests/clues-tab-css.spec.ts | 12 ++- tests/ui-patterns.spec.tsx | 23 ++++- 7 files changed, 194 insertions(+), 212 deletions(-) diff --git a/src/client/clues/tab/browser.tsx b/src/client/clues/tab/browser.tsx index afdef124..bd8f261b 100644 --- a/src/client/clues/tab/browser.tsx +++ b/src/client/clues/tab/browser.tsx @@ -71,7 +71,9 @@ import { } from './browser-shared.tsx' import { ResourceInlineMeta } from '../resource-summary.tsx' import { ResourceFilters, resourceFilterState } from '../resource-filters.tsx' -import { ActionButton, IconButton, Checkbox, Menu, SearchField } from '../../ui/index.ts' +import { + ActionButton, Checkbox, EmptyState, ErrorBlock, IconButton, ListRow, Menu, SearchField, +} from '../../ui/index.ts' import { useListNavigation } from '../../ui/use-list-navigation.ts' import { SourceReadiness } from '../sources/readiness.tsx' import { WorkbenchSection } from '../../ui/WorkbenchSection.tsx' @@ -1208,13 +1210,15 @@ export function ClueBrowser(props: ClueBrowserProps): ReactNode { )} {state.kind === 'page' && state.result.items.length === 0 && ( -
- -

{state.view.kind === 'search' || filters?.active === true ? t('clueBrowserNoResults') : t('clueBrowserEmpty')}

- {filters?.active === true && { changeFilter(filters.clearedScope) }}>{t('clueClearFilters')}} - {state.view.kind === 'search' && { setQuery(''); setView({ kind: 'list' }); queryInputRef.current?.focus() }}>{t('clueClearSearch')}} -
+ } + title={state.view.kind === 'search' || filters?.active === true ? t('clueBrowserNoResults') : t('clueBrowserEmpty')} + actions={<> + {filters?.active === true && { changeFilter(filters.clearedScope) }}>{t('clueClearFilters')}} + {state.view.kind === 'search' && { setQuery(''); setView({ kind: 'list' }); queryInputRef.current?.focus() }}>{t('clueClearSearch')}} + } + /> )} {state.kind === 'resolved' && (
@@ -1241,52 +1245,51 @@ export function ClueBrowser(props: ClueBrowserProps): ReactNode { const pinned = rowPinned(candidate) const key = candidateKey(candidate) return ( -
  • { openRowMenu(event, candidate) }} onKeyDown={event => { rowMenuKey(event, candidate) }}> - {selectionMode && } - - - {pinned && {t('clueBrowserPinnedBadge')}} - - - } - onClick={event => { - const box = event.currentTarget.getBoundingClientRect() - setRowMenu({ candidate, x: box.left, y: box.bottom }) - }} /> - + } + meta={pinned ? {t('clueBrowserPinnedBadge')} : undefined} + actions={<> + { void insertCandidate(candidate) }} + icon={pendingAction?.key === key && pendingAction.kind === 'insert' + ? : } + /> +
  • ) })} @@ -1294,10 +1297,7 @@ export function ClueBrowser(props: ClueBrowserProps): ReactNode { )}
    {moreLoading && } - {moreError !== null &&
    - {moreError} - -
    } + {moreError !== null && }
    {state.kind === 'page' && state.result.limited === true &&
    {t('clueBrowserLimited')} diff --git a/src/client/clues/tab/clues-tab.module.css b/src/client/clues/tab/clues-tab.module.css index b2902e81..e7652b9c 100644 --- a/src/client/clues/tab/clues-tab.module.css +++ b/src/client/clues/tab/clues-tab.module.css @@ -150,7 +150,6 @@ .iconButton:hover:not(:disabled), .toolButton:hover:not(:disabled):not([aria-disabled='true']), .stateAction:hover:not(:disabled), -.packReference:hover, .scopeButton:hover { background: var(--dsw-alias-interactive-bg-hover); color: var(--dsw-alias-label-primary); @@ -174,8 +173,6 @@ .stateAction:focus-visible, .workspaceSelect:focus-visible, .scopeButton:focus-visible, -.packOpen:focus-visible, -.packReference:focus-visible, .rowTitle:focus-visible { outline: 2px solid var(--dsw-alias-brand-primary); outline-offset: 1px; @@ -239,74 +236,6 @@ padding: 0; list-style: none; } -.packRow { - display: flex; - align-items: center; - gap: 6px; - min-width: 0; - min-height: 30px; - padding-inline: 6px; - border-radius: 4px; -} -.packRow:hover, -.packRow:focus-within { - background: var(--dsw-alias-interactive-bg-hover); -} -.packGlyph { - flex: none; - color: var(--dsw-alias-label-tertiary); -} -.packOpen { - display: flex; - align-items: center; - flex: 1; - gap: 6px; - min-width: 0; - min-height: 30px; - box-sizing: border-box; - padding: 0; - border: 0; - border-radius: 4px; - background: transparent; - color: var(--dsw-alias-label-primary); - font: inherit; - text-align: start; - cursor: pointer; -} -.packOpen:focus-visible { - outline-offset: -2px; -} -.packName { - overflow: hidden; - text-overflow: ellipsis; - white-space: nowrap; - font-size: 13px; - line-height: 20px; - font-weight: 500; -} -.packMeta { - display: flex; - align-items: center; - flex: none; - gap: var(--ds-sidebar-space-2); - min-height: 30px; - box-sizing: border-box; -} -.packMeta .packCount { - max-width: 9ch; - overflow: hidden; - text-overflow: ellipsis; - white-space: nowrap; - font-variant-numeric: tabular-nums; -} -.packCount { - color: var(--dsw-alias-label-tertiary); - font-size: 11px; - line-height: 16px; -} -.packReference { - composes: iconButton iconButtonMd from '../../ui/kit.module.css'; -} .browserBody { display: flex; flex: 1; @@ -798,15 +727,12 @@ .searchBox .searchInput, .selectionReview summary, .querySubmit, - .packOpen, - .packReference, .iconButton { min-height: 44px; } .rowActions .iconButton, .iconButton, - .querySubmit, - .packReference { + .querySubmit { width: 44px; } .resultRow, @@ -836,14 +762,6 @@ scrollbar-width: thin; padding-block: 4px; } -.packScroll > .emptyState { - flex-direction: row; - gap: var(--ds-sidebar-space-3); - margin: 0; - padding: var(--ds-sidebar-gutter); - text-align: start; -} -.packScroll > .emptyState p { flex: 1; } .loadSentinel { min-height: 1px; color: var(--dsw-alias-label-tertiary); diff --git a/src/client/clues/tab/packs.tsx b/src/client/clues/tab/packs.tsx index 5248ee3e..3eda59b8 100644 --- a/src/client/clues/tab/packs.tsx +++ b/src/client/clues/tab/packs.tsx @@ -41,7 +41,10 @@ import { clueErrorMessage } from '../copy.ts' import { cluesApi, invalidateClueRequests, peekClueRequest } from '../api.ts' import { openCreatePackWindow } from '../windows/create-pack.tsx' import { openShareWindow } from '../windows/share.tsx' -import { ActionButton, ConfirmDialog, IconButton, LoadingIndicator, Menu, SearchField } from '../../ui/index.ts' +import { + ActionButton, ConfirmDialog, EmptyState, ErrorBlock, IconButton, ListRow, LoadingIndicator, Menu, + SearchField, StatusLine, +} from '../../ui/index.ts' import { useScrollLoad } from '../../ui/use-scroll-load.ts' import { useListNavigation } from '../../ui/use-list-navigation.ts' import { t } from '../../locales.ts' @@ -383,26 +386,26 @@ export function CluePacksSection(props: CluePacksSectionProps): ReactNode {
    - {phase === 'loading' && ( -
    - )} + {phase === 'loading' && } />} {error !== null && ( -
    - {error} - -
    + }} + /> )} {phase === 'ready' && visiblePacks.length === 0 && ( -
    - -

    {search.trim() === '' ? t('cluePacksEmpty') : t('cluePacksNoMatch')}

    - {search.trim() === '' - ? - : } -
    + } + title={search.trim() === '' ? t('cluePacksEmpty') : t('cluePacksNoMatch')} + actions={search.trim() === '' + ? {t('cluePacksNew')} + : { setSearch(''); resetVisible() }}>{t('clueClearSearch')}} + /> )} {shown.length > 0 && ( @@ -411,7 +414,6 @@ export function CluePacksSection(props: CluePacksSectionProps): ReactNode { {shown.map((pack, index) => (
  • { setPackMenu({ pack, ...contextMenuPoint(event) }) }} @@ -420,29 +422,25 @@ export function CluePacksSection(props: CluePacksSectionProps): ReactNode { if (point !== undefined) setPackMenu({ pack, ...point }) }} > - -
    - {t('cluePacksMembers', { n: pack.members.length })} - -
    + { onOpenPack(pack) }} + leading={} + meta={{t('cluePacksMembers', { n: pack.members.length })}} + actions={( + { if (onInsertPack(pack)) rememberUse(pack.id) }} + icon={} + /> + )} + />
  • ))} diff --git a/src/client/ui/patterns.module.css b/src/client/ui/patterns.module.css index a3541bd6..2dbbae1f 100644 --- a/src/client/ui/patterns.module.css +++ b/src/client/ui/patterns.module.css @@ -98,21 +98,33 @@ gap: 1px; } -/* The title is a real button when the row navigates: keyboard operation and a - visible focus ring come for free, and the row's own padding stays intact. */ +/* The title text. Truncation lives here, so a caller's locator can land on the + element that actually clips — a wrapper would inherit the ellipsis but not + the measured overflow. */ +.listRowName { + min-width: 0; + white-space: nowrap; + overflow: hidden; + text-overflow: ellipsis; +} + +/* A navigating row makes the whole caption (name + subtitle) one button: + keyboard operation and a visible focus ring come for free, and the row's own + padding stays intact. */ .listRowTitle { appearance: none; - display: block; + display: flex; + flex-direction: column; + align-items: stretch; + gap: 1px; min-width: 0; + flex: 1 1 auto; padding: 0; border: 0; background: none; color: inherit; font: inherit; text-align: start; - white-space: nowrap; - overflow: hidden; - text-overflow: ellipsis; cursor: pointer; } @@ -123,6 +135,7 @@ } .listRowSubtitle { + min-width: 0; color: var(--dsw-alias-label-tertiary); font: var(--dsw-font-xxxs-11); white-space: nowrap; diff --git a/src/client/ui/patterns.tsx b/src/client/ui/patterns.tsx index 10ec3396..84211f19 100644 --- a/src/client/ui/patterns.tsx +++ b/src/client/ui/patterns.tsx @@ -106,6 +106,18 @@ export interface ListRowProps extends DataAttributes { /** Glyph or status dot at the leading edge. */ readonly leading?: ReactNode readonly title: ReactNode + /** + * `data-*` for the title element itself. The title owns the ellipsis, so a + * caller's locator must land on the title, not on a wrapper inside it — a + * nested span would inherit the truncation but not the measured style. + */ + readonly titleData?: DataAttributes + /** + * Accessible name of the title button when the visible title alone would be + * ambiguous ("Release clues" → "Manage pack: Release clues"). Ignored for a + * plain title, which is text rather than a control. + */ + readonly titleLabel?: string /** Secondary line under the title (a subtitle, not a second fact column). */ readonly subtitle?: ReactNode /** Facts at the trailing edge, before the actions. */ @@ -122,9 +134,15 @@ export interface ListRowProps extends DataAttributes { readonly selected?: boolean /** A row that exists but is not actionable yet (unresolved reference). */ readonly dimmed?: boolean - readonly onContextMenu?: (event: React.MouseEvent) => void - /** Roving tabindex: only one row of a list is in the tab order. */ - readonly primaryTabIndex?: boolean + readonly onContextMenu?: (event: React.MouseEvent) => void + /** + * Roving-focus membership for {@link useListNavigation}: the title button + * carries `data-list-primary` and is the list's one Tab stop when `true`, + * and leaves the tab order when `false`. The hook's contract is exactly + * "mark each title with data-list-primary", so the row honours it here + * instead of making every caller reach into the button. + */ + readonly primary?: boolean readonly className?: string } @@ -137,8 +155,8 @@ export interface ListRowProps extends DataAttributes { */ export function ListRow(props: ListRowProps): ReactNode { const { - leading, title, subtitle, meta, actions, titleAs = 'plain', onTitleClick, - selected, dimmed, onContextMenu, primaryTabIndex, className, ...dataProps + leading, title, titleData, titleLabel, subtitle, meta, actions, titleAs = 'plain', onTitleClick, + selected, dimmed, onContextMenu, primary, className, ...dataProps } = props return (
    {leading !== undefined && {leading}} - - {titleAs === 'button' - ? ( - - ) - : {title}} - {subtitle !== undefined && {subtitle}} - + {titleAs === 'button' + ? ( + // A navigating row puts the subtitle INSIDE the button: the whole + // two-line caption is then one click target and one accessible + // name, exactly like the browse rows this replaces. + + ) + : ( + + {title} + {subtitle !== undefined && {subtitle}} + + )} {meta !== undefined && {meta}} {actions !== undefined && {actions}}
    @@ -171,8 +199,10 @@ export function ListRow(props: ListRowProps): ReactNode { /** A text title must not inherit the button's pointer cursor. */ const TITLE_TEXT = { cursor: 'inherit' } as const +/** The title text itself: truncation happens here, on whatever owns the row. */ + /** Props of {@link EmptyState}. */ -export interface EmptyStateProps { +export interface EmptyStateProps extends DataAttributes { readonly icon?: ReactNode readonly title: ReactNode readonly description?: ReactNode @@ -183,9 +213,9 @@ export interface EmptyStateProps { } /** The one empty state: glyph, one-line title, optional description, actions. */ -export function EmptyState({ icon, title, description, actions, tone = 'well', className }: EmptyStateProps): ReactNode { +export function EmptyState({ icon, title, description, actions, tone = 'well', className, ...dataProps }: EmptyStateProps): ReactNode { return ( -
    +
    {icon !== undefined && {icon}} {title} {description !== undefined && {description}} @@ -195,7 +225,7 @@ export function EmptyState({ icon, title, description, actions, tone = 'well', c } /** Props of {@link ErrorBlock}. */ -export interface ErrorBlockProps { +export interface ErrorBlockProps extends DataAttributes { readonly message: ReactNode /** A sanitized second line (a reason code, a dependency id) — never a secret. */ readonly detail?: ReactNode @@ -213,9 +243,9 @@ export interface ErrorBlockProps { * belongs to, and the four hand-written spellings this replaces had already * drifted in exactly that way (two put the button above, two below). */ -export function ErrorBlock({ message, detail, retryLabel, onRetry, busy, className }: ErrorBlockProps): ReactNode { +export function ErrorBlock({ message, detail, retryLabel, onRetry, busy, className, ...dataProps }: ErrorBlockProps): ReactNode { return ( -
    +
    {message} {detail !== undefined && {detail}} diff --git a/tests/clues-tab-css.spec.ts b/tests/clues-tab-css.spec.ts index e3f11d21..3a96b37f 100644 --- a/tests/clues-tab-css.spec.ts +++ b/tests/clues-tab-css.spec.ts @@ -18,6 +18,9 @@ const sections = read('src/client/ui/workbench-section.module.css') const switcher = read('src/client/ui/source-switcher.module.css') const search = read('src/client/ui/search-field.module.css') const controls = read('src/client/ui/controls.module.css') +// The pack shelf's rows are the shared ListRow recipe now, so its density and +// touch-target contract lives in the shared patterns sheet, not in the tab's. +const patterns = read('src/client/ui/patterns.module.css') describe('clue tab container-query layout', () => { it('makes the tab root an inline-size container and never a viewport column', () => { @@ -61,7 +64,10 @@ describe('clue workbench density and affordances', () => { it('keeps 30px rows and reveals row actions on hover, focus or touch', () => { expect(tab).toMatch(/\.resultRow,[\s\S]*?\.pinRow\s*\{[^}]*min-height:\s*30px/) - expect(tab).toMatch(/\.packRow\s*\{[^}]*min-height:\s*30px/) + // A pack row IS a ListRow: 30px, hover/focus-revealed actions, untouched at rest. + expect(patterns).toMatch(/\.listRow\s*\{[^}]*min-height:\s*30px/) + expect(patterns).toMatch(/\.listRowActions\s*\{[^}]*opacity:\s*0/) + expect(patterns).toMatch(/\.listRow:hover \.listRowActions,[\s\S]*?\{\s*opacity:\s*1/) const actions = /\.rowActions\s*\{([^}]+)\}/.exec(tab)?.[1] expect(actions).toBeDefined() expect(actions).toMatch(/opacity:\s*0/) @@ -74,7 +80,9 @@ describe('clue workbench density and affordances', () => { expect(tab).toMatch(/@media[^}]*pointer:\s*coarse/) expect(tab).toContain('min-height: 44px') expect(tab).toMatch(/@media[^}]*pointer:\s*coarse[\s\S]*\.rowActions \.iconButton,[^}]*width:\s*44px/) - expect(tab).toMatch(/@media[^}]*pointer:\s*coarse[\s\S]*\.packReference[^}]*width:\s*44px/) + // The ListRow's own coarse-pointer rule: the pack reference button is a + // 44px target through the row, and the row itself grows to 44px. + expect(patterns).toMatch(/@media \(pointer: coarse\) \{\s*\.listRow \{ min-height: 44px; \}/) }) it('uses the shared 32px search field with a flat query wrapper', () => { diff --git a/tests/ui-patterns.spec.tsx b/tests/ui-patterns.spec.tsx index 22ccf45a..20aa515f 100644 --- a/tests/ui-patterns.spec.tsx +++ b/tests/ui-patterns.spec.tsx @@ -97,10 +97,25 @@ describe('ListRow', () => { expect(onClick).toHaveBeenCalledOnce() }) - it('keeps the roving tabindex contract: one row in the tab order', () => { - view = renderRoot(createElement(ListRow, { title: 'First', titleAs: 'button', primaryTabIndex: true })) - view.rerender(createElement(ListRow, { title: 'First', titleAs: 'button', primaryTabIndex: false })) - expect(view.container.querySelector('button')!.tabIndex).toBe(-1) + it('marks the roving-focus entry on the title button, not on the row shell', () => { + // useListNavigation focuses the element carrying data-list-primary, so the + // marker and the tab stop must both land on the real button. + view = renderRoot(createElement(ListRow, { title: 'First', titleAs: 'button', primary: true })) + const entry = view.container.querySelector('button')! + expect(entry.hasAttribute('data-list-primary')).toBe(true) + expect(entry.tabIndex).toBe(0) + + view.rerender(createElement(ListRow, { title: 'Second', titleAs: 'button', primary: false })) + const other = view.container.querySelector('button')! + expect(other.hasAttribute('data-list-primary')).toBe(true) + expect(other.tabIndex).toBe(-1) + }) + + it('leaves the tab order alone when the row is not part of a roving list', () => { + view = renderRoot(createElement(ListRow, { title: 'Plain', titleAs: 'button' })) + const button = view.container.querySelector('button')! + expect(button.hasAttribute('data-list-primary')).toBe(false) + expect(button.tabIndex).toBe(0) }) it('mounts every slot it is given and nothing it is not', () => {