Files
accounted/lib/hooks/detail-pager-guards.ts
T
67febd5097 feat(ui): prev/next record navigation on detail pages (#1530)
* feat(ui): prev/next record navigation on detail pages

Customer feedback: stepping between invoices in a reskontra (56 -> 57 ->
58) required going back to the list for every record.

List pages now write their full ordered id array to sessionStorage when
a row is opened (Accounted:list-context:<scope>:<companyId>), and the
detail pages for kundfakturor, leverantorsfakturor, and verifikat show
a compact prev/next pager (chevrons + 'n av m') next to the back
control. ArrowLeft/ArrowRight step too, except while typing in a text
field or while a dialog is open. Navigation uses router.replace so
'tillbaka' returns to the list in one step. Deep links and new tabs
have no context: the pager hides and pages behave as before.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(pager): overlay-aware arrow guard, notes-draft safety, context on Visa detaljer

Review fixes on the detail-record pager:

- The keyboard guard matched any mounted [role=dialog], so the agent
  sheet (which stays mounted display:none once opened) killed arrow
  paging for the rest of the tab session, while open dropdown menus
  did not block at all. The guard now mirrors the AgentSheet Esc
  selector (data-state="open" variants incl. alertdialog and radix
  menu/select/listbox content) and also yields while focus sits inside
  a dialog/menu/listbox container. Extracted as pure functions in
  lib/hooks/detail-pager-guards.ts so the rules are testable in the
  node test environment.
- Arrow keys could unmount the verifikat page and destroy an unsaved
  notes draft once the textarea lost focus. useDetailPager and
  DetailPager now take a keyboard flag, and the verifikat page
  disables keyboard paging while editingNotes is active; the chevron
  buttons stay live.
- The expanded-row Visa detaljer link in JournalEntryList navigated
  without writing the list context, producing stale pager snapshots;
  it now calls rememberListContext like the voucher link.
- ListContext.listPath was written and strictly validated but never
  consumed: removed from the interface, all writers, and the read
  validation. Reads stay tolerant of extra properties so contexts
  stored by older builds still parse.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* refactor(pager): quiet wayfinding strip instead of buttons in the title cluster

The pager sat between the back arrow and the H1, which read as a toolbar
of three boxed buttons and made the title jump horizontally per record.
All three detail pages now share the verifikat page's pattern: a muted
back text-link on the left and the pager right-aligned on the same quiet
row. The pager itself drops to 16px glyphs, muted ink, and hides when the
list context holds a single record.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

---------

Co-authored-by: Jakob Wennberg <311770904+jakobwennberg-oss@users.noreply.github.com>
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-08-12 21:07:57 +02:00

94 lines
3.6 KiB
TypeScript

/**
* Pure keyboard-guard rules for useDetailPager, extracted from the hook so
* they are testable in the node test environment (the hook itself needs a
* browser to render). Only structural types: the hook passes the real
* document, tests pass plain objects.
*/
/**
* Overlays that are actually open. Mirrors the Esc guard selector in
* components/agent/AgentSheet.tsx: match on data-state="open", never on the
* container alone. The agent sheet renders role="dialog" and stays mounted
* with display:none once it has been opened, so keying off a bare
* [role="dialog"] would kill arrow paging for the rest of the tab session;
* conversely open dropdown menus (Radix menu/select/listbox content) carry
* no dialog role and must still block.
*/
const OPEN_OVERLAY_SELECTOR = [
'[data-radix-popper-content-wrapper] [data-state="open"]',
'[role="dialog"][data-state="open"]',
'[role="alertdialog"][data-state="open"]',
'[role="listbox"][data-state="open"]',
'[data-radix-menu-content][data-state="open"]',
'[data-radix-select-content][data-state="open"]',
].join(', ')
/**
* Containers whose focused content owns the arrow keys regardless of
* data-state, for overlays that do not mark themselves the Radix way.
* A display:none container can never hold focus, so a mounted-but-hidden
* sheet does not block through this path either.
*/
const FOCUS_CONTAINER_SELECTOR =
'[role="dialog"], [role="alertdialog"], [role="menu"], [role="listbox"]'
export interface OverlayProbeElement {
closest(selectors: string): unknown
}
export interface OverlayProbeDocument {
querySelector(selectors: string): unknown
readonly activeElement: OverlayProbeElement | null
}
/** True while an overlay that should own the keyboard is open or focused. */
export function isOverlayBlocking(doc: OverlayProbeDocument): boolean {
if (doc.querySelector(OPEN_OVERLAY_SELECTOR)) return true
const active = doc.activeElement
return Boolean(active?.closest(FOCUS_CONTAINER_SELECTOR))
}
/**
* True for targets that own arrow keys: text fields, selects, contentEditable.
* Duck-typed on tagName/isContentEditable instead of instanceof HTMLElement so
* the rule works without DOM globals (tests, SSR); the semantics are the same
* for real elements.
*/
export function isEditableTarget(target: unknown): boolean {
if (target === null || typeof target !== 'object') return false
const el = target as { tagName?: unknown; isContentEditable?: unknown }
if (el.isContentEditable === true) return true
return el.tagName === 'INPUT' || el.tagName === 'TEXTAREA' || el.tagName === 'SELECT'
}
export interface ArrowKeyEventLike {
key: string
defaultPrevented: boolean
isComposing: boolean
metaKey: boolean
ctrlKey: boolean
altKey: boolean
shiftKey: boolean
target: unknown
}
/**
* The full keydown decision: which paging action, if any, a keydown event
* should trigger. Returns null whenever anything nearer the user owns the key.
*/
export function resolveArrowKeyAction(
e: ArrowKeyEventLike,
doc: OverlayProbeDocument,
): 'prev' | 'next' | null {
if (e.key !== 'ArrowLeft' && e.key !== 'ArrowRight') return null
if (e.defaultPrevented || e.isComposing) return null
// Plain arrows only: modified arrows are browser/OS shortcuts
// (cmd+arrow is history navigation on macOS).
if (e.metaKey || e.ctrlKey || e.altKey || e.shiftKey) return null
// Never steal arrows from text editing (e.g. the notes textarea on the
// verifikat page).
if (isEditableTarget(e.target)) return null
if (isOverlayBlocking(doc)) return null
return e.key === 'ArrowLeft' ? 'prev' : 'next'
}