mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-11-20 15:06:19 -05:00
ui: nine leaf files off Semantic, and the components they needed
Phase 2, part 2. Baseline 72 -> 63.
Migrated: Tables, SortableTable, DonatePage, Share, Headline, ErrorBoundary,
and the QR code, VIN decoder, and temperature calculators.
New in the library, all in the gallery and tested:
SearchBox Semantic's `<Search category>`, keeping its grouped `results`
shape because four call sites already build it. A real
combobox: arrows move, Enter takes the highlighted suggestion
or submits the raw text, Escape closes without clearing.
Pagination Semantic's prop names, but onPageChange gets the page number
rather than an (event, data) pair.
TabBar the strip and its house style only; routing stays at the call
site, which is what decides what is active.
IconStack two glyphs composed into one symbol, named once for assistive
technology (Semantic's IconGroup).
Table.HeaderCell gains `sorted` and `onSort`.
That last one is a fix, not just a port. Semantic's `sorted` prop set
`aria-sort`; migrating a table by hand dropped it, leaving a clickable <th>
that no keyboard could reach and no screen reader could describe. Putting it
in the library means the next table gets it right without anyone remembering.
Night mode holes closed along the way: the QR codes on the donate, share, and
calculator pages are white by necessity — a scanner needs the contrast — and
they render as SVG, which the media filter does not target. In night mode that
was a lit panel. They now carry `.media`, so the filter remaps the block as a
unit and the contrast survives the mapping.
Also: `paypal` joins the icon map rather than being imported ad hoc in a page,
and FileBrowser stops passing Semantic-only table props into a DOM element.
This commit is contained in:
parent
efe84d17f1
commit
18dc90b4c2
19 changed files with 1030 additions and 224 deletions
|
|
@ -1,5 +1,6 @@
|
|||
import React from 'react';
|
||||
import {Table as MTable, TableProps as MTableProps} from '@mantine/core';
|
||||
import {Icon} from './Icon';
|
||||
|
||||
/*
|
||||
* Tables.
|
||||
|
|
@ -54,19 +55,55 @@ function Cell({numeric, style, ...props}: CellProps) {
|
|||
/>
|
||||
}
|
||||
|
||||
export type SortDirection = 'ascending' | 'descending';
|
||||
|
||||
export interface HeaderCellProps extends React.ComponentPropsWithoutRef<'th'> {
|
||||
/** This column's current sort, or null/undefined when it is not the sorted one. */
|
||||
sorted?: SortDirection | null;
|
||||
/** Makes the header a sort control. Receives nothing; the caller knows its column. */
|
||||
onSort?: () => void;
|
||||
}
|
||||
|
||||
/**
|
||||
* A header cell, optionally a sort control.
|
||||
*
|
||||
* When `onSort` is given the label becomes a real `<button>` — a click handler on
|
||||
* the `<th>` alone is unreachable by keyboard — and the cell carries `aria-sort`,
|
||||
* so the sort state is announced rather than left to the arrow glyph.
|
||||
*/
|
||||
function HeaderCell({sorted, onSort, children, ...props}: HeaderCellProps) {
|
||||
if (!onSort) return <MTable.Th {...props}>{children}</MTable.Th>
|
||||
|
||||
return <MTable.Th
|
||||
// `none` (not omitted) on the other columns tells assistive tech they are
|
||||
// sortable but not currently sorted.
|
||||
aria-sort={sorted ? sorted : 'none'}
|
||||
{...props}
|
||||
>
|
||||
<button type='button' className='wrolpi-th-sort' onClick={onSort}>
|
||||
{children}
|
||||
<Icon
|
||||
name={sorted === 'descending' ? 'arrow down' : 'arrow up'}
|
||||
size={14}
|
||||
className={sorted ? undefined : 'wrolpi-th-sort-idle'}
|
||||
/>
|
||||
</button>
|
||||
</MTable.Th>
|
||||
}
|
||||
|
||||
export const Table = Object.assign(TableBase, {
|
||||
Header: MTable.Thead,
|
||||
Body: MTable.Tbody,
|
||||
Footer: MTable.Tfoot,
|
||||
Row,
|
||||
Cell,
|
||||
HeaderCell: MTable.Th,
|
||||
HeaderCell,
|
||||
// Mantine's own names, for new code.
|
||||
Thead: MTable.Thead,
|
||||
Tbody: MTable.Tbody,
|
||||
Tfoot: MTable.Tfoot,
|
||||
Tr: Row,
|
||||
Td: Cell,
|
||||
Th: MTable.Th,
|
||||
Th: HeaderCell,
|
||||
ScrollContainer: MTable.ScrollContainer,
|
||||
});
|
||||
|
|
|
|||
|
|
@ -92,6 +92,7 @@ const semanticNames: Record<string, keyof typeof Tabler> = {
|
|||
'map outline': 'IconMap',
|
||||
'microchip': 'IconCpu',
|
||||
'minus': 'IconMinus',
|
||||
'paypal': 'IconBrandPaypal',
|
||||
'play': 'IconPlayerPlay',
|
||||
'plug': 'IconPlug',
|
||||
'plus': 'IconPlus',
|
||||
|
|
@ -134,6 +135,29 @@ const semanticNames: Record<string, keyof typeof Tabler> = {
|
|||
'x': 'IconX',
|
||||
};
|
||||
|
||||
export interface IconStackProps {
|
||||
/** The main glyph. */
|
||||
children: React.ReactNode;
|
||||
/** A smaller glyph pinned to the bottom-right, qualifying the first. */
|
||||
corner: React.ReactNode;
|
||||
/** Accessible name for the pair; the two glyphs mean one thing together. */
|
||||
label: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Two icons composed into one symbol — a wifi glyph with a question mark on it,
|
||||
* say. Replaces Semantic's IconGroup.
|
||||
*
|
||||
* The corner glyph gets a background matching the surface so it stays legible
|
||||
* over the icon beneath it.
|
||||
*/
|
||||
export function IconStack({children, corner, label}: IconStackProps) {
|
||||
return <span className='wrolpi-icon-stack' role='img' aria-label={label}>
|
||||
{children}
|
||||
<span className='wrolpi-icon-stack-corner' aria-hidden='true'>{corner}</span>
|
||||
</span>
|
||||
}
|
||||
|
||||
export interface IconProps extends Omit<React.SVGProps<SVGSVGElement>, 'name' | 'ref'> {
|
||||
/** A Semantic UI icon name, for migrated call sites. */
|
||||
name?: string;
|
||||
|
|
|
|||
74
app/src/components/ui/Navigation.tsx
Normal file
74
app/src/components/ui/Navigation.tsx
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
import React from 'react';
|
||||
import {Pagination as MPagination} from '@mantine/core';
|
||||
|
||||
/*
|
||||
* Navigation: pagination and tab bars.
|
||||
*
|
||||
* Neither knows anything about routing — the app owns its links. The tab bar
|
||||
* takes rendered children and supplies only the strip and its house style, so
|
||||
* call sites keep using react-router's NavLink and its `isActive` render prop.
|
||||
*/
|
||||
|
||||
// --------------------------------------------------------------- Pagination
|
||||
|
||||
export interface PaginationProps {
|
||||
/** 1-based. */
|
||||
activePage: number;
|
||||
totalPages?: number;
|
||||
onPageChange: (page: number) => void;
|
||||
/** Pages shown either side of the active one. Fewer on a phone. */
|
||||
siblingRange?: number;
|
||||
/** Include jump-to-first and jump-to-last controls. */
|
||||
showFirstAndLast?: boolean;
|
||||
size?: string;
|
||||
disabled?: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* Page controls. Replaces Semantic's Pagination, keeping its prop names
|
||||
* (`activePage`, `totalPages`, `onPageChange`) so call sites migrate as a
|
||||
* rename — but `onPageChange` receives the page number directly rather than
|
||||
* Semantic's `(event, data)` pair.
|
||||
*/
|
||||
export function Pagination({
|
||||
activePage,
|
||||
totalPages,
|
||||
onPageChange,
|
||||
siblingRange = 2,
|
||||
showFirstAndLast = false,
|
||||
size = 'sm',
|
||||
disabled,
|
||||
}: PaginationProps) {
|
||||
return <MPagination
|
||||
className='wrolpi-pagination'
|
||||
value={activePage}
|
||||
// A single empty page still renders, so the control never collapses to nothing
|
||||
// and shift the layout when results arrive.
|
||||
total={totalPages || 1}
|
||||
onChange={onPageChange}
|
||||
siblings={siblingRange}
|
||||
withEdges={showFirstAndLast}
|
||||
size={size}
|
||||
disabled={disabled}
|
||||
radius={0}
|
||||
getItemProps={page => ({'aria-label': `Page ${page}`})}
|
||||
/>
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------- Tab bars
|
||||
|
||||
/** A row of tabs. Children are the links; routing stays with the caller. */
|
||||
export function TabBar({children, right}: {children: React.ReactNode; right?: React.ReactNode}) {
|
||||
return <nav className='wrolpi-tabs'>
|
||||
<div className='wrolpi-tabs-links'>{children}</div>
|
||||
{right && <div className='wrolpi-tabs-right'>{right}</div>}
|
||||
</nav>
|
||||
}
|
||||
|
||||
/**
|
||||
* Class name for one tab. Exported rather than baked into a component so a
|
||||
* caller can hand it straight to NavLink's `className` render prop, which is
|
||||
* how the app decides what is active.
|
||||
*/
|
||||
export const tabClassName = (active: boolean): string =>
|
||||
`wrolpi-tab${active ? ' wrolpi-tab-active' : ''}`;
|
||||
230
app/src/components/ui/SearchBox.tsx
Normal file
230
app/src/components/ui/SearchBox.tsx
Normal file
|
|
@ -0,0 +1,230 @@
|
|||
import React, {useEffect, useId, useRef, useState} from 'react';
|
||||
import {Icon} from './Icon';
|
||||
import {IconButton} from './Button';
|
||||
import {Loader} from './Feedback';
|
||||
|
||||
/*
|
||||
* The search box, with optional grouped suggestions.
|
||||
*
|
||||
* Replaces Semantic's `<Search category>`, and deliberately keeps its `results`
|
||||
* shape — `{groupKey: {name, results: [{title, description}]}}` — because four
|
||||
* call sites already build that structure from the API.
|
||||
*
|
||||
* Suggestions are a combobox: the input keeps focus and owns the keyboard, while
|
||||
* `aria-activedescendant` points at the highlighted option. Arrow keys move,
|
||||
* Enter takes the highlighted suggestion or submits what was typed, Escape
|
||||
* closes the list without clearing the text.
|
||||
*/
|
||||
|
||||
export interface SearchResult {
|
||||
/** The value put into the input when chosen. Also the visible first line. */
|
||||
title: string;
|
||||
/** Second line, for context: a channel name, a domain. */
|
||||
description?: string;
|
||||
|
||||
[key: string]: any;
|
||||
}
|
||||
|
||||
export interface SearchResultGroup {
|
||||
/** Group heading. */
|
||||
name: string;
|
||||
results: SearchResult[];
|
||||
}
|
||||
|
||||
export type SearchResults = Record<string, SearchResultGroup>;
|
||||
|
||||
export interface SearchBoxProps {
|
||||
value?: string;
|
||||
onChange?: (value: string) => void;
|
||||
/** Enter, or the search button. Receives the current text. */
|
||||
onSubmit?: (value: string) => void;
|
||||
/** Called with the chosen suggestion. */
|
||||
onResultSelect?: (result: SearchResult) => void;
|
||||
onBlur?: React.FocusEventHandler<HTMLInputElement>;
|
||||
results?: SearchResults;
|
||||
/** Suggestions are being fetched; shows a spinner rather than "no results". */
|
||||
loading?: boolean;
|
||||
placeholder?: string;
|
||||
/** Show a button that empties the input. */
|
||||
clearable?: boolean;
|
||||
/** Force the clear button's disabled state; defaults to "nothing typed". */
|
||||
clearDisabled?: boolean | null;
|
||||
onClear?: () => void;
|
||||
disabled?: boolean;
|
||||
required?: boolean;
|
||||
inputRef?: React.Ref<HTMLInputElement>;
|
||||
/** Accessible name. Falls back to the placeholder. */
|
||||
label?: string;
|
||||
className?: string;
|
||||
autoFocus?: boolean;
|
||||
name?: string;
|
||||
}
|
||||
|
||||
/** Flatten the groups into the order they are displayed, for keyboard movement. */
|
||||
const flatten = (results?: SearchResults): SearchResult[] =>
|
||||
Object.values(results ?? {}).flatMap(group => group?.results ?? []);
|
||||
|
||||
export function SearchBox({
|
||||
value,
|
||||
onChange,
|
||||
onSubmit,
|
||||
onResultSelect,
|
||||
onBlur,
|
||||
results,
|
||||
loading,
|
||||
placeholder = 'Search...',
|
||||
clearable,
|
||||
clearDisabled = null,
|
||||
onClear,
|
||||
disabled,
|
||||
required,
|
||||
inputRef,
|
||||
label,
|
||||
className,
|
||||
autoFocus,
|
||||
name,
|
||||
}: SearchBoxProps) {
|
||||
const [open, setOpen] = useState(false);
|
||||
const [highlighted, setHighlighted] = useState(-1);
|
||||
const containerRef = useRef<HTMLDivElement>(null);
|
||||
const listId = useId();
|
||||
|
||||
const text = value ?? '';
|
||||
const flat = flatten(results);
|
||||
const hasSuggestions = onResultSelect !== undefined || !!results;
|
||||
|
||||
// Close when focus or a click leaves the box entirely. A blur handler alone would
|
||||
// fire before a click on a suggestion registers.
|
||||
useEffect(() => {
|
||||
if (!open) return;
|
||||
const onDocumentClick = (event: MouseEvent) => {
|
||||
if (!containerRef.current?.contains(event.target as Node)) setOpen(false);
|
||||
};
|
||||
document.addEventListener('mousedown', onDocumentClick);
|
||||
return () => document.removeEventListener('mousedown', onDocumentClick);
|
||||
}, [open]);
|
||||
|
||||
const choose = (result: SearchResult) => {
|
||||
setOpen(false);
|
||||
setHighlighted(-1);
|
||||
onResultSelect?.(result);
|
||||
}
|
||||
|
||||
const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
|
||||
setHighlighted(-1);
|
||||
if (hasSuggestions) setOpen(true);
|
||||
onChange?.(event.currentTarget.value);
|
||||
}
|
||||
|
||||
const handleKeyDown = (event: React.KeyboardEvent<HTMLInputElement>) => {
|
||||
if (event.key === 'ArrowDown' || event.key === 'ArrowUp') {
|
||||
if (!flat.length) return;
|
||||
event.preventDefault();
|
||||
setOpen(true);
|
||||
const step = event.key === 'ArrowDown' ? 1 : -1;
|
||||
setHighlighted(current => {
|
||||
// Wrap, so holding a key cannot strand the user at either end.
|
||||
const next = current + step;
|
||||
if (next < 0) return flat.length - 1;
|
||||
if (next >= flat.length) return 0;
|
||||
return next;
|
||||
});
|
||||
} else if (event.key === 'Enter') {
|
||||
event.preventDefault();
|
||||
if (open && highlighted >= 0 && flat[highlighted]) {
|
||||
choose(flat[highlighted]);
|
||||
} else {
|
||||
setOpen(false);
|
||||
onSubmit?.(text);
|
||||
}
|
||||
} else if (event.key === 'Escape') {
|
||||
// Closes the list; the typed text is left alone.
|
||||
setOpen(false);
|
||||
setHighlighted(-1);
|
||||
}
|
||||
}
|
||||
|
||||
const handleClear = () => {
|
||||
onChange?.('');
|
||||
onSubmit?.('');
|
||||
onClear?.();
|
||||
setOpen(false);
|
||||
}
|
||||
|
||||
let index = -1;
|
||||
|
||||
return <div
|
||||
ref={containerRef}
|
||||
className={['wrolpi-searchbox', className].filter(Boolean).join(' ')}
|
||||
>
|
||||
<div className='wrolpi-searchbox-control'>
|
||||
<span className='wrolpi-searchbox-icon'><Icon name='search'/></span>
|
||||
<input
|
||||
ref={inputRef}
|
||||
type='text'
|
||||
name={name}
|
||||
className='wrolpi-searchbox-input'
|
||||
placeholder={placeholder}
|
||||
aria-label={label ?? placeholder}
|
||||
value={text}
|
||||
disabled={disabled}
|
||||
required={required}
|
||||
autoFocus={autoFocus}
|
||||
onChange={handleChange}
|
||||
onKeyDown={handleKeyDown}
|
||||
// Open on focus when there is something to say — suggestions, or the fact
|
||||
// that they are still being fetched. Not when the list would just read
|
||||
// "No results" at a caller that has not searched yet.
|
||||
onFocus={() => hasSuggestions && (flat.length > 0 || loading) && setOpen(true)}
|
||||
onBlur={onBlur}
|
||||
role={hasSuggestions ? 'combobox' : undefined}
|
||||
aria-expanded={hasSuggestions ? open : undefined}
|
||||
aria-controls={hasSuggestions ? listId : undefined}
|
||||
aria-autocomplete={hasSuggestions ? 'list' : undefined}
|
||||
aria-activedescendant={open && highlighted >= 0 ? `${listId}-${highlighted}` : undefined}
|
||||
/>
|
||||
{loading && <span className='wrolpi-searchbox-loading'><Loader size='xs'/></span>}
|
||||
{clearable && <IconButton
|
||||
icon='close'
|
||||
label='Clear search'
|
||||
className='wrolpi-searchbox-clear'
|
||||
onClick={handleClear}
|
||||
disabled={clearDisabled !== null ? clearDisabled : !text}
|
||||
/>}
|
||||
</div>
|
||||
|
||||
{open && hasSuggestions && <div className='wrolpi-searchbox-results' id={listId} role='listbox'>
|
||||
{loading && !flat.length && <div className='wrolpi-searchbox-empty'>Searching…</div>}
|
||||
{!loading && !flat.length && <div className='wrolpi-searchbox-empty'>No results</div>}
|
||||
{Object.entries(results ?? {}).map(([key, group]) => {
|
||||
if (!group?.results?.length) return null;
|
||||
return <div key={key} className='wrolpi-searchbox-group'>
|
||||
<div className='wrolpi-searchbox-group-name'>{group.name}</div>
|
||||
{group.results.map((result, resultIndex) => {
|
||||
index += 1;
|
||||
const optionIndex = index;
|
||||
return <div
|
||||
key={`${key}-${resultIndex}`}
|
||||
id={`${listId}-${optionIndex}`}
|
||||
role='option'
|
||||
aria-selected={optionIndex === highlighted}
|
||||
className={`wrolpi-searchbox-result${
|
||||
optionIndex === highlighted ? ' wrolpi-searchbox-result-active' : ''}`}
|
||||
// mousedown, not click: the input must not blur first.
|
||||
onMouseDown={event => {
|
||||
event.preventDefault();
|
||||
choose(result);
|
||||
}}
|
||||
onMouseEnter={() => setHighlighted(optionIndex)}
|
||||
>
|
||||
<div className='wrolpi-searchbox-result-title'>{result.title}</div>
|
||||
{result.description && <div className='wrolpi-searchbox-result-description'>
|
||||
{result.description}
|
||||
</div>}
|
||||
</div>
|
||||
})}
|
||||
</div>
|
||||
})}
|
||||
</div>}
|
||||
</div>
|
||||
}
|
||||
|
|
@ -16,6 +16,8 @@ export * from './Button';
|
|||
export * from './Feedback';
|
||||
export * from './Surfaces';
|
||||
export * from './DataTable';
|
||||
export * from './Navigation';
|
||||
export * from './SearchBox';
|
||||
export * from './Inputs';
|
||||
export * from './Overlays';
|
||||
export * from './ThemePicker';
|
||||
|
|
|
|||
|
|
@ -26,6 +26,26 @@
|
|||
}
|
||||
}
|
||||
|
||||
.wrolpi-icon-stack {
|
||||
position: relative;
|
||||
display: inline-flex;
|
||||
line-height: 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* The corner glyph sits on the main one, so it carries the surface color behind
|
||||
* it — without that the two sets of strokes cross and neither reads.
|
||||
*/
|
||||
.wrolpi-icon-stack-corner {
|
||||
position: absolute;
|
||||
right: -3px;
|
||||
bottom: -3px;
|
||||
display: flex;
|
||||
background: var(--bg);
|
||||
border-radius: 50%;
|
||||
padding: 1px;
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------- Buttons */
|
||||
/*
|
||||
* Dashed borders mean "destructive or failed", and nothing else may use them.
|
||||
|
|
@ -117,6 +137,188 @@ html[data-theme="amber"] .wrolpi-theme-option-active {
|
|||
border-top: 1px solid var(--border);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ Pagination */
|
||||
|
||||
.wrolpi-pagination button {
|
||||
background: var(--panel);
|
||||
color: var(--text);
|
||||
border: 1px solid var(--border);
|
||||
}
|
||||
|
||||
.wrolpi-pagination button[data-active] {
|
||||
background: var(--blue);
|
||||
color: var(--btn-text);
|
||||
border-color: var(--blue);
|
||||
}
|
||||
|
||||
/* Night and amber have no second hue to spend, so the current page is marked by a
|
||||
heavier border and the panel surface instead of a filled block of color. */
|
||||
html[data-theme="night"] .wrolpi-pagination button[data-active],
|
||||
html[data-theme="amber"] .wrolpi-pagination button[data-active] {
|
||||
background: var(--panel);
|
||||
color: var(--text);
|
||||
border: 2px solid var(--text);
|
||||
}
|
||||
|
||||
.wrolpi-pagination button:disabled {
|
||||
color: var(--muted);
|
||||
background: var(--bg);
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------- Tab bar */
|
||||
|
||||
.wrolpi-tabs {
|
||||
display: flex;
|
||||
align-items: flex-end;
|
||||
justify-content: space-between;
|
||||
gap: 12px;
|
||||
border-bottom: 1px solid var(--border);
|
||||
margin-bottom: 14px;
|
||||
}
|
||||
|
||||
.wrolpi-tabs-links {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
|
||||
.wrolpi-tabs-right {
|
||||
padding-bottom: 6px;
|
||||
}
|
||||
|
||||
.wrolpi-tab {
|
||||
display: inline-block;
|
||||
padding: 8px 14px;
|
||||
color: var(--muted);
|
||||
text-decoration: none;
|
||||
font-size: 13px;
|
||||
border: 1px solid transparent;
|
||||
/* Sits on the bar's own rule, so the active tab can erase it and read as
|
||||
connected to the panel below. */
|
||||
margin-bottom: -1px;
|
||||
}
|
||||
|
||||
.wrolpi-tab:hover {
|
||||
color: var(--text);
|
||||
}
|
||||
|
||||
.wrolpi-tab-active {
|
||||
color: var(--text);
|
||||
background: var(--panel);
|
||||
border-color: var(--border);
|
||||
border-bottom-color: var(--panel);
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.wrolpi-tab:focus-visible {
|
||||
outline: 2px solid var(--blue);
|
||||
outline-offset: -2px;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------- Search box */
|
||||
|
||||
.wrolpi-searchbox {
|
||||
position: relative;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.wrolpi-searchbox-control {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
background: var(--panel);
|
||||
border: 1px solid var(--border);
|
||||
padding: 0 8px;
|
||||
}
|
||||
|
||||
.wrolpi-searchbox-control:focus-within {
|
||||
border-color: var(--blue);
|
||||
}
|
||||
|
||||
html[data-theme="night"] .wrolpi-searchbox-control:focus-within,
|
||||
html[data-theme="amber"] .wrolpi-searchbox-control:focus-within {
|
||||
border-color: var(--text);
|
||||
}
|
||||
|
||||
.wrolpi-searchbox-icon {
|
||||
display: flex;
|
||||
color: var(--muted);
|
||||
}
|
||||
|
||||
.wrolpi-searchbox-input {
|
||||
flex: 1;
|
||||
min-width: 0;
|
||||
padding: 8px 0;
|
||||
background: none;
|
||||
border: none;
|
||||
outline: none;
|
||||
color: var(--text);
|
||||
font: inherit;
|
||||
font-size: 13px;
|
||||
}
|
||||
|
||||
.wrolpi-searchbox-input::placeholder {
|
||||
color: var(--muted);
|
||||
}
|
||||
|
||||
.wrolpi-searchbox-loading,
|
||||
.wrolpi-searchbox-clear {
|
||||
flex: none;
|
||||
}
|
||||
|
||||
/*
|
||||
* The suggestion list overlays the page rather than pushing it down — a search
|
||||
* that reflows the results underneath it is unusable while typing.
|
||||
*/
|
||||
.wrolpi-searchbox-results {
|
||||
position: absolute;
|
||||
z-index: 20;
|
||||
top: 100%;
|
||||
left: 0;
|
||||
right: 0;
|
||||
max-height: 320px;
|
||||
overflow-y: auto;
|
||||
background: var(--panel);
|
||||
border: 1px solid var(--border);
|
||||
border-top: none;
|
||||
}
|
||||
|
||||
.wrolpi-searchbox-group-name {
|
||||
padding: 6px 10px;
|
||||
font-size: 11px;
|
||||
font-weight: 600;
|
||||
letter-spacing: 0.08em;
|
||||
text-transform: uppercase;
|
||||
color: var(--muted);
|
||||
background: var(--head);
|
||||
}
|
||||
|
||||
.wrolpi-searchbox-result {
|
||||
padding: 7px 10px;
|
||||
cursor: pointer;
|
||||
border-top: 1px solid var(--border);
|
||||
}
|
||||
|
||||
.wrolpi-searchbox-result-active {
|
||||
background: var(--head);
|
||||
}
|
||||
|
||||
.wrolpi-searchbox-result-title {
|
||||
font-size: 13px;
|
||||
color: var(--text);
|
||||
overflow-wrap: anywhere;
|
||||
}
|
||||
|
||||
.wrolpi-searchbox-result-description {
|
||||
font-size: 11px;
|
||||
color: var(--muted);
|
||||
}
|
||||
|
||||
.wrolpi-searchbox-empty {
|
||||
padding: 10px;
|
||||
font-size: 12px;
|
||||
color: var(--muted);
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------- Modals */
|
||||
/*
|
||||
* Actions sit below the body, separated by a hairline rather than a raised bar.
|
||||
|
|
@ -460,6 +662,41 @@ html[data-theme="night"] .wrolpi-status-failed {
|
|||
|
||||
/* ----------------------------------------------------------------- Tables */
|
||||
|
||||
/* A sortable header: a real button, so the column is reachable by keyboard. */
|
||||
.wrolpi-th-sort {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
gap: 5px;
|
||||
background: none;
|
||||
border: none;
|
||||
padding: 0;
|
||||
margin: 0;
|
||||
font: inherit;
|
||||
color: inherit;
|
||||
cursor: pointer;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
/* The unsorted columns show a faint arrow: the affordance is visible, but it does
|
||||
not compete with the column actually doing the sorting. */
|
||||
.wrolpi-th-sort-idle {
|
||||
opacity: 0.35;
|
||||
}
|
||||
|
||||
.wrolpi-th-sort:hover .wrolpi-th-sort-idle {
|
||||
opacity: 0.7;
|
||||
}
|
||||
|
||||
.wrolpi-th-sort:focus-visible {
|
||||
outline: 2px solid var(--blue);
|
||||
outline-offset: 2px;
|
||||
}
|
||||
|
||||
html[data-theme="night"] .wrolpi-th-sort:focus-visible,
|
||||
html[data-theme="amber"] .wrolpi-th-sort:focus-visible {
|
||||
outline-color: var(--text);
|
||||
}
|
||||
|
||||
/* A failed row carries the danger border, matching failed messages. */
|
||||
.wrolpi-row-failed > td {
|
||||
border-top: 1.5px dashed var(--red);
|
||||
|
|
|
|||
|
|
@ -12,6 +12,11 @@ import {
|
|||
Confirm,
|
||||
Header,
|
||||
Icon,
|
||||
IconStack,
|
||||
Pagination,
|
||||
SearchBox,
|
||||
TabBar,
|
||||
tabClassName,
|
||||
IconButton,
|
||||
Label,
|
||||
Message,
|
||||
|
|
@ -280,6 +285,30 @@ describe('Table', () => {
|
|||
expect(container.querySelector('.wrolpi-row-failed')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('makes a sortable column a keyboard-reachable button that announces its state', async () => {
|
||||
// A click handler on the <th> alone is unreachable by keyboard, and the arrow
|
||||
// glyph says nothing to a screen reader.
|
||||
const onSort = jest.fn();
|
||||
renderUI(<Table>
|
||||
<Table.Header>
|
||||
<Table.Row>
|
||||
<Table.HeaderCell sorted='descending' onSort={onSort}>Size</Table.HeaderCell>
|
||||
<Table.HeaderCell onSort={jest.fn()}>Name</Table.HeaderCell>
|
||||
<Table.HeaderCell>Actions</Table.HeaderCell>
|
||||
</Table.Row>
|
||||
</Table.Header>
|
||||
</Table>);
|
||||
|
||||
expect(screen.getByRole('columnheader', {name: /Size/})).toHaveAttribute('aria-sort', 'descending');
|
||||
// Sortable but not currently sorted.
|
||||
expect(screen.getByRole('columnheader', {name: /Name/})).toHaveAttribute('aria-sort', 'none');
|
||||
// Not sortable at all.
|
||||
expect(screen.getByRole('columnheader', {name: 'Actions'})).not.toHaveAttribute('aria-sort');
|
||||
|
||||
await userEvent.click(screen.getByRole('button', {name: /Size/}));
|
||||
expect(onSort).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('keeps wide tables from scrolling the page sideways', () => {
|
||||
const {container} = renderUI(<Table><Table.Body>
|
||||
<Table.Row><Table.Cell>wide</Table.Cell></Table.Row>
|
||||
|
|
@ -409,6 +438,160 @@ describe('ThemePicker', () => {
|
|||
});
|
||||
});
|
||||
|
||||
describe('Pagination', () => {
|
||||
it('reports the current page and moves when one is clicked', async () => {
|
||||
const onPageChange = jest.fn();
|
||||
renderUI(<Pagination activePage={3} totalPages={12} onPageChange={onPageChange}/>);
|
||||
|
||||
await userEvent.click(screen.getByRole('button', {name: 'Page 5'}));
|
||||
|
||||
expect(onPageChange).toHaveBeenCalledWith(5);
|
||||
});
|
||||
|
||||
it('still renders one page when the total is unknown', () => {
|
||||
// Downloads render the paginator before the first response arrives; collapsing to
|
||||
// nothing and back shifts the whole page.
|
||||
renderUI(<Pagination activePage={1} totalPages={undefined} onPageChange={jest.fn()}/>);
|
||||
|
||||
expect(screen.getByRole('button', {name: 'Page 1'})).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
describe('TabBar', () => {
|
||||
it('marks the active tab with a class the themes key on', () => {
|
||||
const {container} = renderUI(<TabBar>
|
||||
<button className={tabClassName(true)}>Videos</button>
|
||||
<button className={tabClassName(false)}>Channels</button>
|
||||
</TabBar>);
|
||||
|
||||
expect(container.querySelectorAll('.wrolpi-tab')).toHaveLength(2);
|
||||
expect(container.querySelectorAll('.wrolpi-tab-active')).toHaveLength(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe('SearchBox', () => {
|
||||
const results = {
|
||||
directories: {name: 'Directories', results: [{title: 'videos/'}]},
|
||||
channels: {name: 'Channels', results: [
|
||||
{title: 'videos/Wranglerstar', description: 'Wranglerstar'},
|
||||
{title: 'videos/RoseRed', description: 'RoseRed Homestead'},
|
||||
]},
|
||||
};
|
||||
|
||||
it('submits what was typed', async () => {
|
||||
const onSubmit = jest.fn();
|
||||
renderUI(<SearchBox value='axe' onChange={jest.fn()} onSubmit={onSubmit}/>);
|
||||
|
||||
await userEvent.type(screen.getByRole('textbox'), '{Enter}');
|
||||
|
||||
expect(onSubmit).toHaveBeenCalledWith('axe');
|
||||
});
|
||||
|
||||
it('shows suggestions under their group headings', async () => {
|
||||
renderUI(<SearchBox value='vid' onChange={jest.fn()} results={results}
|
||||
onResultSelect={jest.fn()}/>);
|
||||
|
||||
await userEvent.click(screen.getByRole('combobox'));
|
||||
|
||||
expect(screen.getByText('Directories')).toBeInTheDocument();
|
||||
expect(screen.getAllByRole('option')).toHaveLength(3);
|
||||
});
|
||||
|
||||
it('selects a suggestion by click', async () => {
|
||||
const onResultSelect = jest.fn();
|
||||
renderUI(<SearchBox value='vid' onChange={jest.fn()} results={results}
|
||||
onResultSelect={onResultSelect}/>);
|
||||
await userEvent.click(screen.getByRole('combobox'));
|
||||
|
||||
await userEvent.click(screen.getByText('videos/Wranglerstar'));
|
||||
|
||||
expect(onResultSelect).toHaveBeenCalledWith(
|
||||
expect.objectContaining({title: 'videos/Wranglerstar'}));
|
||||
});
|
||||
|
||||
it('moves through suggestions with the arrow keys and takes one with Enter', async () => {
|
||||
const onResultSelect = jest.fn();
|
||||
const onSubmit = jest.fn();
|
||||
renderUI(<SearchBox value='vid' onChange={jest.fn()} results={results}
|
||||
onResultSelect={onResultSelect} onSubmit={onSubmit}/>);
|
||||
|
||||
const input = screen.getByRole('combobox');
|
||||
await userEvent.click(input);
|
||||
await userEvent.keyboard('{ArrowDown}{ArrowDown}{Enter}');
|
||||
|
||||
expect(onResultSelect).toHaveBeenCalledWith(
|
||||
expect.objectContaining({title: 'videos/Wranglerstar'}));
|
||||
// Enter took the highlighted suggestion instead of submitting the raw text.
|
||||
expect(onSubmit).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('wraps around rather than stranding the user at the end of the list', async () => {
|
||||
const onResultSelect = jest.fn();
|
||||
renderUI(<SearchBox value='vid' onChange={jest.fn()} results={results}
|
||||
onResultSelect={onResultSelect}/>);
|
||||
|
||||
await userEvent.click(screen.getByRole('combobox'));
|
||||
await userEvent.keyboard('{ArrowUp}{Enter}');
|
||||
|
||||
expect(onResultSelect).toHaveBeenCalledWith(
|
||||
expect.objectContaining({title: 'videos/RoseRed'}));
|
||||
});
|
||||
|
||||
it('closes on Escape without clearing what was typed', async () => {
|
||||
const onChange = jest.fn();
|
||||
renderUI(<SearchBox value='vid' onChange={onChange} results={results}
|
||||
onResultSelect={jest.fn()}/>);
|
||||
await userEvent.click(screen.getByRole('combobox'));
|
||||
|
||||
await userEvent.keyboard('{Escape}');
|
||||
|
||||
expect(screen.queryByRole('option')).not.toBeInTheDocument();
|
||||
expect(screen.getByRole('combobox')).toHaveValue('vid');
|
||||
expect(onChange).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('says it is searching rather than "no results" while suggestions are pending', async () => {
|
||||
renderUI(<SearchBox value='vid' onChange={jest.fn()} results={{}} loading
|
||||
onResultSelect={jest.fn()}/>);
|
||||
|
||||
await userEvent.click(screen.getByRole('combobox'));
|
||||
|
||||
expect(screen.getByText(/searching/i)).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('clears the input and submits the empty search', async () => {
|
||||
const onChange = jest.fn();
|
||||
const onSubmit = jest.fn();
|
||||
renderUI(<SearchBox value='axe' onChange={onChange} onSubmit={onSubmit} clearable/>);
|
||||
|
||||
await userEvent.click(screen.getByRole('button', {name: 'Clear search'}));
|
||||
|
||||
expect(onChange).toHaveBeenCalledWith('');
|
||||
expect(onSubmit).toHaveBeenCalledWith('');
|
||||
});
|
||||
|
||||
it('is a plain search field when no suggestions are wired up', () => {
|
||||
// Without this, a box that can never show options still claims to be a combobox.
|
||||
renderUI(<SearchBox value='' onChange={jest.fn()} onSubmit={jest.fn()}/>);
|
||||
|
||||
expect(screen.queryByRole('combobox')).not.toBeInTheDocument();
|
||||
expect(screen.getByRole('textbox')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
describe('IconStack', () => {
|
||||
it('names the pair once, and hides the corner glyph from assistive tech', () => {
|
||||
const {container} = renderUI(
|
||||
<IconStack corner={<Icon name='question'/>} label='WiFi status unknown'>
|
||||
<Icon name='wifi'/>
|
||||
</IconStack>
|
||||
);
|
||||
|
||||
expect(screen.getByRole('img', {name: 'WiFi status unknown'})).toBeInTheDocument();
|
||||
expect(container.querySelector('.wrolpi-icon-stack-corner')).toHaveAttribute('aria-hidden', 'true');
|
||||
});
|
||||
});
|
||||
|
||||
describe('Confirm', () => {
|
||||
it('names the action on its confirming button', async () => {
|
||||
const onConfirm = jest.fn();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue