表格视图包含操作表格所需的各项功能:标题和主要操作、筛选和排序、选择、行操作及分页。各项功能由组件提供,区块负责把这些组件组织在同一块面板上。
表格视图
<section class="sheet container-inline flex flex-col overflow-hidden" data-example="table-view" aria-labelledby="tv-title"> <header class="flex flex-wrap items-center justify-between gap-sm p-md"> <div class="flex flex-col gap-3xs"> <h2 class="heading heading-sm" id="tv-title">Atlas Labs</h2> <p class="m(0) fg-text-muted" id="tv-summary">24 projects</p> </div> <div class="flex flex-wrap items-center gap-xs"> <div class="segmented segmented-sm" role="radiogroup" aria-label="Show"> <label class="segmented-item"> <input type="radio" class="segmented-input" name="tv-set" value="projects" checked>Projects</label> <label class="segmented-item"> <input type="radio" class="segmented-input" name="tv-set" value="people">People</label> <label class="segmented-item"> <input type="radio" class="segmented-input" name="tv-set" value="files">Files</label> </div> <button type="button" class="btn btn-primary btn-sm" id="tv-action"> <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <path d="M3 8L13 8" /> <path d="M8 3L8 13" /> </svg> <span>New project</span> </button> </div> </header> <div class="flex flex-wrap items-center gap-xs px-md pb-md"> <label class="input-group input-group-sm grow(1) basis(200) max-w(320)"> <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <circle cx="7" cy="7" r="4.5" /> <path d="M10.182 10.182L14 14" /> </svg> <input class="input-group-field" type="search" id="tv-search" placeholder="Search projects" aria-label="Search projects" autocomplete="off"> </label> <button type="button" class="btn btn-sm" popovertarget="tv-filters" data-filter="open"> <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <path d="M4.5365 3L11.4635 3A1 1 0 0 1 12.2548 4.6114L9.4587 8.2299A1 1 0 0 0 9.25 8.8413L9.25 11.323A1 1 0 0 1 8.6214 12.2514L7.875 12.55A0.8203 0.8203 0 0 1 6.75 11.7883L6.75 8.8413A1 1 0 0 0 6.5413 8.2299L3.7452 4.6114A1 1 0 0 1 4.5365 3Z" /> </svg>Filters <span data-filter="count" aria-hidden="true" hidden> <span class="badge badge-sm badge-ink badge-pill">0</span> </span> </button> <select class="select select-raised select-sm ml-auto" id="tv-sort" aria-label="Sort by"></select> <div class="w-full" data-filter="applied" hidden> <div class="flex flex-wrap items-center gap-xs"> <ul class="flex flex-wrap gap-xs m(0) p(0)" aria-label="Applied filters" data-filter="chips"></ul> <button type="button" class="btn btn-quiet btn-sm" data-filter="clear-all">Clear all</button> </div> </div> </div> <div class="popover p(0) w(560)" id="tv-filters" popover role="dialog" aria-labelledby="tv-filters-title" style="--popover-max-width: min(36rem, calc(100vw - 2rem))" data-filter="panel"> <form class="flex flex-col" data-filter="form" novalidate> <header class="flex flex-col gap-3xs px-md pt-md pb-sm"> <h3 class="heading heading-xs" id="tv-filters-title">Filter projects</h3> <p class="m(0) fg-text-muted" id="tv-filters-hint">A project shows when it meets every condition.</p> </header> <ul class="flex flex-col gap-xs m(0) px-md py-xs" data-filter="conditions"></ul> <p class="m(0) px-md py-xs fg-text-muted" data-filter="empty">No conditions yet: every row shows.</p> <p class="m(0) px-md py-xs fg-danger" id="tv-filters-error" role="alert" data-filter="error" hidden></p> <div class="px-md pb-md"> <button type="button" class="btn btn-quiet btn-sm" data-filter="add"> <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <path d="M3 8L13 8" /> <path d="M8 3L8 13" /> </svg>Add condition</button> </div> <footer class="flex flex-wrap items-center justify-between gap-xs p-md bt(1) bt-solid b-line-hair"> <button type="button" class="btn btn-quiet btn-sm" data-filter="clear">Clear</button> <div class="flex gap-xs"> <button type="button" class="btn btn-sm" data-filter="cancel">Cancel</button> <button type="submit" class="btn btn-primary btn-sm">Apply</button> </div> </footer> </form> </div> <div class="scroll-area scroll-area-inline bt(1) bt-solid b-line-hair"> <table class="table table-interactive" id="tv-table" aria-labelledby="tv-title"> <thead id="tv-head"></thead> <tbody id="tv-body"></tbody> </table> </div> <footer class="flex flex-wrap items-center justify-between gap-sm p-md bt(1) bt-solid b-line-hair"> <div class="flex flex-wrap items-center gap-xs min-h(32)"> <p class="m(0) fg-text-muted" id="tv-count">No projects selected</p> <div id="tv-selected" hidden> <div class="flex items-center gap-3xs"> <button type="button" class="btn btn-quiet btn-sm btn-danger" id="tv-delete">Delete</button> <button type="button" class="btn btn-quiet btn-sm" id="tv-deselect">Clear selection</button> </div> </div> </div> <div class="flex flex-wrap items-center gap-sm"> <p class="m(0) fg-text-muted whitespace-nowrap" id="tv-range">1–8 of 24</p> <nav aria-label="Pagination"> <ol class="pagination" id="tv-pages"></ol> </nav> </div> </footer> <p class="sr-only" role="status" id="tv-status"></p> <template data-filter="row"> <li class="flex flex-wrap items-center gap-xs"> <span class="w(44) shrink(0) fg-text-muted text-end" data-part="joiner">Where</span> <select class="select select-sm" aria-label="Field" data-part="field"></select> <select class="select select-sm" aria-label="Comparison" data-part="operator"></select> <input class="input input-sm grow(1) basis(128) min-w(0)" aria-label="Value" data-part="value" required> <button type="button" class="btn btn-quiet btn-square btn-sm" aria-label="Remove condition" data-part="remove"> <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <path d="M4 4L12 12" /> <path d="M4 12L12 4" /> </svg> </button> </li> </template> <template data-filter="choice"> <select class="select select-sm grow(1) basis(128)" aria-label="Value" data-part="value"></select> </template> <template data-filter="chip"> <li> <span class="chip"> <span class="chip-label"></span> <button type="button" class="chip-remove" aria-label="Remove the filter"> <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <path d="M4 4L12 12" /> <path d="M4 12L12 4" /> </svg> </button> </span> </li> </template> <template id="tv-head-projects"> <table class="table"> <thead> <tr class="table-row"> <th class="table-heading" scope="col"> <input type="checkbox" class="checkbox" data-part="all" aria-label="Select all projects"> </th> <th class="table-heading" scope="col" data-sort="name">Project</th> <th class="table-heading" scope="col">Status</th> <th class="table-heading" scope="col">Region</th> <th class="table-heading table-numeric" scope="col" data-sort="deployments">Deployments</th> <th class="table-heading" scope="col" data-sort="deployed">Last deployed</th> <th class="table-heading" scope="col"> <span class="sr-only">Actions</span> </th> </tr> </thead> </table> </template> <template id="tv-row-projects"> <table class="table"> <tbody> <tr class="table-row"> <td class="table-cell"> <input type="checkbox" class="checkbox" data-part="select" aria-label="Select"> </td> <th class="table-cell table-primary" scope="row"> <span data-part="name"></span> <span class="block fg-text-muted font-regular" data-part="domain"></span> </th> <td class="table-cell"> <span class="badge badge-dot" data-part="status"></span> </td> <td class="table-cell" data-part="region"></td> <td class="table-cell table-numeric" data-part="deployments"></td> <td class="table-cell whitespace-nowrap" data-part="deployed"></td> <td class="table-cell text-end" data-part="actions"></td> </tr> </tbody> </table> </template> <template id="tv-head-people"> <table class="table"> <thead> <tr class="table-row"> <th class="table-heading" scope="col"> <input type="checkbox" class="checkbox" data-part="all" aria-label="Select all people"> </th> <th class="table-heading" scope="col" data-sort="name">Person</th> <th class="table-heading" scope="col">Role</th> <th class="table-heading" scope="col">Status</th> <th class="table-heading table-numeric" scope="col" data-sort="projects">Projects</th> <th class="table-heading" scope="col" data-sort="active">Last active</th> <th class="table-heading" scope="col"> <span class="sr-only">Actions</span> </th> </tr> </thead> </table> </template> <template id="tv-row-people"> <table class="table"> <tbody> <tr class="table-row"> <td class="table-cell"> <input type="checkbox" class="checkbox" data-part="select" aria-label="Select"> </td> <th class="table-cell table-primary" scope="row"> <span class="flex items-center gap-sm"> <span class="avatar avatar-sm" aria-hidden="true" data-part="initials"></span> <span class="min-w(0)"> <span class="block" data-part="name"></span> <span class="block fg-text-muted font-regular" data-part="email"></span> </span> </span> </th> <td class="table-cell" data-part="role"></td> <td class="table-cell"> <span class="badge badge-dot" data-part="status"></span> </td> <td class="table-cell table-numeric" data-part="projects"></td> <td class="table-cell whitespace-nowrap" data-part="active"></td> <td class="table-cell text-end" data-part="actions"></td> </tr> </tbody> </table> </template> <template id="tv-head-files"> <table class="table"> <thead> <tr class="table-row"> <th class="table-heading" scope="col"> <input type="checkbox" class="checkbox" data-part="all" aria-label="Select all files"> </th> <th class="table-heading" scope="col" data-sort="name">File</th> <th class="table-heading" scope="col">Type</th> <th class="table-heading table-numeric" scope="col" data-sort="size">Size</th> <th class="table-heading" scope="col" data-sort="modified">Modified</th> <th class="table-heading" scope="col">Owner</th> <th class="table-heading" scope="col"> <span class="sr-only">Actions</span> </th> </tr> </thead> </table> </template> <template id="tv-row-files"> <table class="table"> <tbody> <tr class="table-row"> <td class="table-cell"> <input type="checkbox" class="checkbox" data-part="select" aria-label="Select"> </td> <th class="table-cell table-primary" scope="row"> <span class="flex items-center gap-sm"> <span class="icon-frame icon-frame-xs" aria-hidden="true"> <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round"> <path d="M4.5 2L9.0858 2A1 1 0 0 1 9.7929 2.2929L12.2071 4.7071A1 1 0 0 1 12.5 5.4142L12.5 13A1 1 0 0 1 11.5 14L4.5 14A1 1 0 0 1 3.5 13L3.5 3A1 1 0 0 1 4.5 2Z" /> <path d="M10 2.5V4.4A0.1 0.1 0 0 0 10.1 4.5H12L12.2409 4.7427A0.9993 0.9993 0 0 1 12.5 5.4142V5.5H10.1Q9.6444 5.5 9.3222 5.1778Q9 4.8556 9 4.4V2A1.1002 1.1002 0 0 1 9.3761 2.0431A1.0001 1.0001 0 0 1 9.7929 2.2929L10 2.5Z" fill="currentColor" stroke="none" fill-rule="evenodd" /> </svg> </span> <span class="min-w(0) truncate" data-part="name"></span> </span> </th> <td class="table-cell" data-part="type"></td> <td class="table-cell table-numeric" data-part="size"></td> <td class="table-cell whitespace-nowrap" data-part="modified"></td> <td class="table-cell whitespace-nowrap" data-part="owner"></td> <td class="table-cell text-end" data-part="actions"></td> </tr> </tbody> </table> </template> <template id="tv-actions"> <div> <button type="button" class="btn btn-quiet btn-square btn-sm" aria-label="Actions"> <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <circle cx="3.5" cy="8" r="1.05" fill="currentColor" stroke="none" /> <circle cx="8" cy="8" r="1.05" fill="currentColor" stroke="none" /> <circle cx="12.5" cy="8" r="1.05" fill="currentColor" stroke="none" /> </svg> </button> <div class="menu menu-end" popover> <a class="menu-item" href="#" data-part="open">Open</a> <hr class="menu-separator"> <button type="button" class="menu-item menu-item-danger" data-part="delete">Delete</button> </div> </div> </template> <template id="tv-empty"> <table class="table"> <tbody> <tr class="table-row"> <td class="table-cell" colspan="7"> <div class="empty"> <p class="empty-title" data-part="title">No projects match</p> <p class="empty-description">Try another search, or remove a filter.</p> <div class="empty-actions"> <button type="button" class="btn btn-sm" data-part="reset">Clear the search and the filters</button> </div> </div> </td> </tr> </tbody> </table> </template> <template id="tv-page"> <ol class="pagination"> <li> <button type="button" class="btn btn-quiet btn-square btn-sm" aria-label="Page"></button> </li> </ol> </template> <template id="tv-gap"> <ol class="pagination"> <li class="pagination-gap" aria-hidden="true">…</li> </ol> </template> <template id="tv-previous"> <ol class="pagination"> <li> <button type="button" class="btn btn-quiet btn-square btn-sm" aria-label="Previous page"> <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <path d="M6 3.5L10.3586 7.8586A0.2 0.2 0 0 1 10.3586 8.1414L6 12.5" transform="rotate(180 8 8)" /> </svg> </button> </li> </ol> </template> <template id="tv-next"> <ol class="pagination"> <li> <button type="button" class="btn btn-quiet btn-square btn-sm" aria-label="Next page"> <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <path d="M6 3.5L10.3586 7.8586A0.2 0.2 0 0 1 10.3586 8.1414L6 12.5" /> </svg> </button> </li> </ol> </template></section>import { filters } from '../filter-bar/filters.js'import { filter, formatDate, paginate, search, sort } from '../filter-bar/query.js'import { sets } from './data.js'const size = 8const tones = { ready: 'badge-success', building: 'badge-info', queued: 'badge-warning', failed: 'badge-danger', active: 'badge-success', invited: 'badge-info', away: '',}const numbers = new Intl.NumberFormat('en-US')const bytes = (megabytes) => megabytes < 1 ? `${numbers.format(Math.round(megabytes * 1000))} KB` : `${numbers.format(megabytes)} MB`/** * The table view on local data. Search, the filter bar, the sort and the page decide which rows show; the * selection is kept to the rows that match, so the count in the footer, the pages and the rows always agree. * Projects, people and files are three sets with their own fields and rows. Returns the function that stops it. */export function mount(root) { const $ = (selector) => root.querySelector(selector) const copy = (id, selector) => $(id).content.querySelector(selector).cloneNode(true) const controller = new AbortController() const { signal } = controller // The rows are the page's to change: deleting one changes the set it is in. const data = Object.fromEntries(Object.entries(sets).map(([name, set]) => [name, { ...set, rows: [...set.rows] }])) const state = { set: 'projects', search: '', page: 1, sort: data.projects.sorts[0], applied: [], selected: new Map(Object.keys(data).map((name) => [name, new Set()])) } const set = () => data[state.set] const plural = (count) => set().noun[count === 1 ? 0 : 1] const say = (words) => { $('#tv-status').textContent = words } /** The rows that match, in order: what every count and every page is read from. */ function matching() { return sort(filter(search(set().rows, state.search, set().search), state.applied, set().fields), state.sort[0], state.sort[1]) } function fill(row, record) { const part = (name) => row.querySelector(`[data-part="${name}"]`) const field = (key) => set().fields.find((each) => each.key === key) const label = (key) => field(key).options.find(([value]) => value === record[key])[1] if (state.set === 'projects') { part('name').textContent = record.name part('domain').textContent = record.domain part('status').classList.add(tones[record.status]) part('status').textContent = label('status') part('region').textContent = label('region') part('deployments').textContent = numbers.format(record.deployments) part('deployed').textContent = formatDate(record.deployed) } else if (state.set === 'people') { part('initials').textContent = record.name.split(' ').map((word) => word[0]).slice(0, 2).join('') part('name').textContent = record.name part('email').textContent = record.email part('role').textContent = label('role') if (tones[record.status]) part('status').classList.add(tones[record.status]) part('status').textContent = label('status') part('projects').textContent = record.projects part('active').textContent = record.status === 'invited' ? 'Not yet' : formatDate(record.active) } else { part('name').textContent = record.name part('type').textContent = label('type') part('size').textContent = bytes(record.size) part('modified').textContent = formatDate(record.modified) part('owner').textContent = record.owner } const select = part('select') select.checked = state.selected.get(state.set).has(record.id) select.value = record.id select.setAttribute('aria-label', `Select ${record.name}`) if (select.checked) row.setAttribute('aria-selected', 'true') // Each row's actions are a menu named for its row, with an id of its own. const actions = copy('#tv-actions', 'div') const menu = actions.querySelector('.menu') menu.id = `tv-menu-${record.id}` const button = actions.querySelector('button') button.setAttribute('popovertarget', menu.id) button.setAttribute('aria-label', `Actions for ${record.name}`) menu.querySelector('[data-part="delete"]').value = record.id part('actions').replaceChildren(...actions.children) } function head() { const row = copy(`#tv-head-${state.set}`, 'tr') for (const heading of row.querySelectorAll('[data-sort]')) { if (heading.dataset.sort === state.sort[0]) heading.setAttribute('aria-sort', state.sort[1]) } $('#tv-head').replaceChildren(row) } function pages(result) { const list = [] const add = (id, setup) => { const item = copy(id, 'li'); setup(item.querySelector('button')); list.push(item) } add('#tv-previous', (button) => { button.value = String(result.page - 1); button.disabled = result.page === 1 }) // Every page when there are few; the first, the last and the neighbors of the current one when there are many. const shown = [...new Set([1, result.page - 1, result.page, result.page + 1, result.pages])].filter((page) => page >= 1 && page <= result.pages).sort((a, b) => a - b) for (const [index, page] of shown.entries()) { if (index && page - shown[index - 1] > 1) list.push(copy('#tv-gap', 'li')) add('#tv-page', (button) => { button.value = String(page) button.textContent = String(page) button.setAttribute('aria-label', `Page ${page}`) if (page === result.page) button.setAttribute('aria-current', 'page') }) } add('#tv-next', (button) => { button.value = String(result.page + 1); button.disabled = result.page === result.pages }) $('#tv-pages').replaceChildren(...list) } /** Write the table, its counts and its pages from the state. */ function draw() { const rows = matching() const selected = state.selected.get(state.set) // A row that no longer matches leaves the selection, so the count never includes what cannot be seen. for (const id of selected) if (!rows.some((row) => row.id === id)) selected.delete(id) const result = paginate(rows, state.page, size) state.page = result.page const many = set().noun[1] $('#tv-summary').textContent = rows.length === set().rows.length ? `${set().rows.length} ${plural(set().rows.length)}` : `${rows.length} of ${set().rows.length} ${many} match` head() const all = $('#tv-head [data-part="all"]') all.checked = rows.length > 0 && selected.size === rows.length all.indeterminate = selected.size > 0 && selected.size < rows.length all.disabled = rows.length === 0 all.setAttribute('aria-label', `Select all ${rows.length} ${plural(rows.length)}`) if (result.rows.length) { $('#tv-body').replaceChildren(...result.rows.map((record) => { const row = copy(`#tv-row-${state.set}`, 'tr') fill(row, record) return row })) } else { const empty = copy('#tv-empty', 'tr') empty.querySelector('[data-part="title"]').textContent = `No ${many} match` $('#tv-body').replaceChildren(empty) } $('#tv-count').textContent = selected.size ? `${selected.size} of ${rows.length} selected` : `No ${many} selected` $('#tv-selected').hidden = selected.size === 0 $('#tv-range').textContent = rows.length ? `${result.from}–${result.to} of ${rows.length}` : `0 of ${set().rows.length}` pages(result) } // The filter bar of whichever set is shown; a new set brings its own fields and starts with none applied. const bar = filters(root, { fields: set().fields, onChange: (applied) => { state.applied = applied; state.page = 1; draw() } }) function show(name) { state.set = name state.search = '' state.page = 1 state.applied = [] state.sort = set().sorts[0] const [one, many] = set().noun $('#tv-search').value = '' $('#tv-search').placeholder = `Search ${many}` $('#tv-search').setAttribute('aria-label', `Search ${many}`) $('#tv-action span').textContent = set().action $('#tv-filters-title').textContent = `Filter ${many}` $('#tv-filters-hint').textContent = `A${/^[aeiou]/.test(one) ? 'n' : ''} ${one} shows when it meets every condition.` $('#tv-sort').replaceChildren(...set().sorts.map(([key, direction, words]) => { const option = new Option(words, `${key} ${direction}`) option.className = 'select-option' return option })) bar.setFields(set().fields) draw() } root.querySelector('[name="tv-set"]').closest('.segmented').addEventListener('change', (event) => show(event.target.value), { signal }) $('#tv-search').addEventListener('input', (event) => { state.search = event.target.value state.page = 1 draw() }, { signal }) $('#tv-sort').addEventListener('change', (event) => { state.sort = event.target.value.split(' ') state.page = 1 draw() }, { signal }) $('#tv-pages').addEventListener('click', (event) => { const button = event.target.closest('button') if (!button || button.disabled) return state.page = Number(button.value) const pressed = button.getAttribute('aria-label') draw() // Focus stays on what was pressed, drawn again; at an end, where Previous or Next is disabled, on the current page. const again = [...$('#tv-pages').querySelectorAll('button:not(:disabled)')].find(each => each.getAttribute('aria-label') === pressed) ;(again ?? $(`#tv-pages [aria-current="page"]`))?.focus() say(`Page ${state.page} of ${Math.max(1, Math.ceil(matching().length / size))}`) }, { signal }) root.addEventListener('change', (event) => { const input = event.target const selected = state.selected.get(state.set) if (input.matches('#tv-body [data-part="select"]')) { if (input.checked) selected.add(input.value) else selected.delete(input.value) draw() $(`#tv-body [data-part="select"][value="${CSS.escape(input.value)}"]`)?.focus() } else if (input.matches('#tv-head [data-part="all"]')) { // All: every row that matches, on every page. None: clear them. if (input.checked) for (const row of matching()) selected.add(row.id) else selected.clear() draw() $('#tv-head [data-part="all"]').focus() } }, { signal }) function remove(ids) { const before = set().rows.length set().rows = set().rows.filter((row) => !ids.includes(row.id)) for (const id of ids) state.selected.get(state.set).delete(id) const removed = before - set().rows.length draw() say(`${removed} ${plural(removed)} deleted`) ;($('#tv-head [data-part="all"]:not(:disabled)') ?? $('#tv-search')).focus() } $('#tv-delete').addEventListener('click', () => remove([...state.selected.get(state.set)]), { signal }) $('#tv-deselect').addEventListener('click', () => { state.selected.get(state.set).clear() draw() $('#tv-head [data-part="all"]').focus() }, { signal }) $('#tv-body').addEventListener('click', (event) => { const target = event.target if (target.closest('[data-part="open"]')) event.preventDefault() const deleting = target.closest('[data-part="delete"]') if (deleting) { deleting.closest('[popover]').hidePopover() remove([deleting.value]) } if (target.closest('[data-part="reset"]')) { state.search = '' $('#tv-search').value = '' bar.setFields(set().fields) state.applied = [] draw() $('#tv-search').focus() } }, { signal }) show('projects') return () => { controller.abort() bar.destroy() }}/** * Atlas Labs' workspace as local data: its projects, its people and its files. Each set says which fields a * reader can filter by, search in and sort by; the table view writes its rows from them. Dates are YYYY-MM-DD * and the data was read on `today`. */export const today = '2026-11-14'export const sets = { projects: { noun: ['project', 'projects'], action: 'New project', search: ['name', 'domain'], sorts: [['deployed', 'descending', 'Last deployed'], ['name', 'ascending', 'Name'], ['deployments', 'descending', 'Most deployments']], fields: [ { key: 'name', label: 'Name', type: 'text' }, { key: 'status', label: 'Status', type: 'enum', options: [['ready', 'Ready'], ['building', 'Building'], ['queued', 'Queued'], ['failed', 'Failed']] }, { key: 'region', label: 'Region', type: 'enum', options: [['tokyo', 'Tokyo'], ['frankfurt', 'Frankfurt'], ['virginia', 'Virginia'], ['sydney', 'Sydney'], ['sao-paulo', 'São Paulo'], ['london', 'London'], ['singapore', 'Singapore'], ['oregon', 'Oregon']] }, { key: 'deployments', label: 'Deployments', type: 'number' }, { key: 'deployed', label: 'Last deployed', type: 'date' }, ], rows: [ { id: 'prj_01', name: 'Atlas', domain: 'atlas.dev', status: 'queued', region: 'oregon', deployments: 234, deployed: '2026-10-10' }, { id: 'prj_02', name: 'Borealis', domain: 'borealis.app', status: 'ready', region: 'oregon', deployments: 325, deployed: '2026-11-03' }, { id: 'prj_03', name: 'Cinder', domain: 'cinder.io', status: 'ready', region: 'oregon', deployments: 158, deployed: '2026-11-08' }, { id: 'prj_04', name: 'Drift', domain: 'drift.so', status: 'ready', region: 'tokyo', deployments: 307, deployed: '2026-11-09' }, { id: 'prj_05', name: 'Ember', domain: 'ember.dev', status: 'failed', region: 'virginia', deployments: 322, deployed: '2026-10-17' }, { id: 'prj_06', name: 'Fathom', domain: 'fathom.app', status: 'ready', region: 'frankfurt', deployments: 33, deployed: '2026-10-12' }, { id: 'prj_07', name: 'Gale', domain: 'gale.dev', status: 'ready', region: 'sydney', deployments: 310, deployed: '2026-11-02' }, { id: 'prj_08', name: 'Harbor', domain: 'harbor.app', status: 'ready', region: 'london', deployments: 228, deployed: '2026-10-16' }, { id: 'prj_09', name: 'Isle', domain: 'isle.io', status: 'ready', region: 'sydney', deployments: 330, deployed: '2026-10-12' }, { id: 'prj_10', name: 'Juniper', domain: 'juniper.so', status: 'ready', region: 'tokyo', deployments: 342, deployed: '2026-10-14' }, { id: 'prj_11', name: 'Kestrel', domain: 'kestrel.dev', status: 'ready', region: 'sao-paulo', deployments: 211, deployed: '2026-10-16' }, { id: 'prj_12', name: 'Lumen', domain: 'lumen.app', status: 'ready', region: 'london', deployments: 391, deployed: '2026-10-29' }, { id: 'prj_13', name: 'Meridian', domain: 'meridian.dev', status: 'ready', region: 'sao-paulo', deployments: 18, deployed: '2026-10-13' }, { id: 'prj_14', name: 'Nimbus', domain: 'nimbus.app', status: 'ready', region: 'frankfurt', deployments: 208, deployed: '2026-10-09' }, { id: 'prj_15', name: 'Orchard', domain: 'orchard.io', status: 'ready', region: 'singapore', deployments: 37, deployed: '2026-10-27' }, { id: 'prj_16', name: 'Pinecone', domain: 'pinecone.so', status: 'ready', region: 'sydney', deployments: 110, deployed: '2026-11-14' }, { id: 'prj_17', name: 'Quarry', domain: 'quarry.dev', status: 'ready', region: 'singapore', deployments: 365, deployed: '2026-10-15' }, { id: 'prj_18', name: 'Ridge', domain: 'ridge.app', status: 'failed', region: 'frankfurt', deployments: 292, deployed: '2026-10-19' }, { id: 'prj_19', name: 'Saltmarsh', domain: 'saltmarsh.dev', status: 'ready', region: 'london', deployments: 47, deployed: '2026-10-28' }, { id: 'prj_20', name: 'Tundra', domain: 'tundra.app', status: 'ready', region: 'tokyo', deployments: 212, deployed: '2026-10-24' }, { id: 'prj_21', name: 'Umbra', domain: 'umbra.io', status: 'ready', region: 'sydney', deployments: 364, deployed: '2026-11-06' }, { id: 'prj_22', name: 'Vale', domain: 'vale.so', status: 'ready', region: 'tokyo', deployments: 241, deployed: '2026-11-14' }, { id: 'prj_23', name: 'Willow', domain: 'willow.dev', status: 'queued', region: 'sydney', deployments: 232, deployed: '2026-11-03' }, { id: 'prj_24', name: 'Zephyr', domain: 'zephyr.app', status: 'ready', region: 'singapore', deployments: 332, deployed: '2026-11-06' }, ], }, people: { noun: ['person', 'people'], action: 'Invite people', search: ['name', 'email'], sorts: [['name', 'ascending', 'Name'], ['active', 'descending', 'Last active'], ['projects', 'descending', 'Most projects']], fields: [ { key: 'name', label: 'Name', type: 'text' }, { key: 'email', label: 'Email', type: 'text' }, { key: 'role', label: 'Role', type: 'enum', options: [['owner', 'Owner'], ['admin', 'Admin'], ['member', 'Member'], ['viewer', 'Viewer']] }, { key: 'status', label: 'Status', type: 'enum', options: [['active', 'Active'], ['away', 'Away'], ['invited', 'Invited']] }, { key: 'projects', label: 'Projects', type: 'number' }, { key: 'active', label: 'Last active', type: 'date' }, ], rows: [ { id: 'usr_01', name: 'Yi-ching Lin', email: 'yiching@atlaslabs.dev', role: 'owner', status: 'active', projects: 2, active: '2026-11-02' }, { id: 'usr_02', name: 'Po-han Chen', email: 'pohan@atlaslabs.dev', role: 'admin', status: 'active', projects: 7, active: '2026-11-02' }, { id: 'usr_03', name: 'Misaki Sato', email: 'misaki@atlaslabs.dev', role: 'admin', status: 'active', projects: 1, active: '2026-11-08' }, { id: 'usr_04', name: 'Seo-yeon Kim', email: 'seoyeon@atlaslabs.dev', role: 'member', status: 'active', projects: 14, active: '2026-11-06' }, { id: 'usr_05', name: 'Kenta Nakamura', email: 'kenta@atlaslabs.dev', role: 'member', status: 'active', projects: 10, active: '2026-10-20' }, { id: 'usr_06', name: 'Ji-hoon Park', email: 'jihoon@atlaslabs.dev', role: 'member', status: 'active', projects: 1, active: '2026-11-05' }, { id: 'usr_07', name: 'Mei-ling Wang', email: 'meiling@atlaslabs.dev', role: 'member', status: 'away', projects: 3, active: '2026-11-08' }, { id: 'usr_08', name: 'Haruto Takahashi', email: 'haruto@atlaslabs.dev', role: 'member', status: 'active', projects: 14, active: '2026-11-02' }, { id: 'usr_09', name: 'Ga-eun Lee', email: 'gaeun@atlaslabs.dev', role: 'viewer', status: 'active', projects: 11, active: '2026-10-26' }, { id: 'usr_10', name: 'Chia-hao Wu', email: 'chiahao@atlaslabs.dev', role: 'member', status: 'invited', projects: 0, active: '' }, { id: 'usr_11', name: 'Yuna Kobayashi', email: 'yuna@atlaslabs.dev', role: 'viewer', status: 'invited', projects: 0, active: '' }, { id: 'usr_12', name: 'Min-jun Choi', email: 'minjun@atlaslabs.dev', role: 'viewer', status: 'away', projects: 3, active: '2026-11-13' }, { id: 'usr_13', name: 'Shu-fen Huang', email: 'shufen@atlaslabs.dev', role: 'member', status: 'active', projects: 8, active: '2026-11-08' }, { id: 'usr_14', name: 'Ren Watanabe', email: 'ren@atlaslabs.dev', role: 'admin', status: 'active', projects: 1, active: '2026-11-06' }, { id: 'usr_15', name: 'Hye-jin Jung', email: 'hyejin@atlaslabs.dev', role: 'viewer', status: 'invited', projects: 0, active: '' }, { id: 'usr_16', name: 'Tzu-yu Tsai', email: 'tzuyu@atlaslabs.dev', role: 'member', status: 'active', projects: 6, active: '2026-10-26' }, ], }, files: { noun: ['file', 'files'], action: 'Upload', search: ['name', 'owner'], sorts: [['modified', 'descending', 'Last modified'], ['name', 'ascending', 'Name'], ['size', 'descending', 'Largest']], fields: [ { key: 'name', label: 'Name', type: 'text' }, { key: 'type', label: 'Type', type: 'enum', options: [['pdf', 'PDF'], ['image', 'Image'], ['sheet', 'Spreadsheet'], ['doc', 'Document'], ['archive', 'Archive']] }, { key: 'size', label: 'Size', type: 'number', unit: 'MB' }, { key: 'modified', label: 'Modified', type: 'date' }, { key: 'owner', label: 'Owner', type: 'text' }, ], rows: [ { id: 'fil_01', name: 'Q3 infrastructure report.pdf', type: 'pdf', size: 2.4, modified: '2026-10-19', owner: 'Kenta Nakamura' }, { id: 'fil_02', name: 'Launch checklist.pdf', type: 'pdf', size: 0.3, modified: '2026-11-02', owner: 'Po-han Chen' }, { id: 'fil_03', name: 'Brand guidelines.pdf', type: 'pdf', size: 18.6, modified: '2026-11-12', owner: 'Po-han Chen' }, { id: 'fil_04', name: 'Hero photograph.jpg', type: 'image', size: 4.1, modified: '2026-11-08', owner: 'Seo-yeon Kim' }, { id: 'fil_05', name: 'Dashboard screenshot.png', type: 'image', size: 1.2, modified: '2026-11-14', owner: 'Ji-hoon Park' }, { id: 'fil_06', name: 'Team offsite.jpg', type: 'image', size: 6.8, modified: '2026-11-03', owner: 'Haruto Takahashi' }, { id: 'fil_07', name: 'Usage by region.csv', type: 'sheet', size: 0.9, modified: '2026-11-10', owner: 'Haruto Takahashi' }, { id: 'fil_08', name: 'Invoices October.xlsx', type: 'sheet', size: 0.4, modified: '2026-10-19', owner: 'Misaki Sato' }, { id: 'fil_09', name: 'Capacity plan.xlsx', type: 'sheet', size: 1.7, modified: '2026-10-18', owner: 'Mei-ling Wang' }, { id: 'fil_10', name: 'Edge logs 2026-11-12.zip', type: 'archive', size: 214.0, modified: '2026-11-12', owner: 'Misaki Sato' }, { id: 'fil_11', name: 'Design tokens.zip', type: 'archive', size: 3.3, modified: '2026-11-05', owner: 'Seo-yeon Kim' }, { id: 'fil_12', name: 'Incident review.docx', type: 'doc', size: 0.2, modified: '2026-10-19', owner: 'Seo-yeon Kim' }, { id: 'fil_13', name: 'Onboarding guide.docx', type: 'doc', size: 1.1, modified: '2026-10-22', owner: 'Seo-yeon Kim' }, { id: 'fil_14', name: 'API reference.md', type: 'doc', size: 0.1, modified: '2026-11-09', owner: 'Ga-eun Lee' }, { id: 'fil_15', name: 'Release notes 0.9.md', type: 'doc', size: 0.05, modified: '2026-11-08', owner: 'Mei-ling Wang' }, { id: 'fil_16', name: 'Atlas logo.svg', type: 'image', size: 0.02, modified: '2026-10-17', owner: 'Haruto Takahashi' }, { id: 'fil_17', name: 'Migration script.zip', type: 'archive', size: 0.6, modified: '2026-10-26', owner: 'Po-han Chen' }, { id: 'fil_18', name: 'Pricing model.xlsx', type: 'sheet', size: 0.8, modified: '2026-11-01', owner: 'Yi-ching Lin' }, ], },}import { condition, describe, operatorsOf, problem } from './query.js'/** * A filter bar: conditions staged in a popover and applied together, then shown as chips that each remove one. * The page writes the bar's elements and marks them with `data-filter`, with a `<template>` for a condition's row, * a value to choose and an applied chip; this copies the templates, fills them in and says the applied conditions * to `onChange`. * * - The popover holds a draft. Opening it copies what is applied; Cancel, Escape or a click outside drop the draft. * - Apply checks every row first. A row that cannot be applied is marked `aria-invalid`, the message says why and * focus goes to it; nothing is applied until every row can be. * - Clear empties the draft, so Apply then shows every row. Clear all, beside the chips, applies at once. * * @param {HTMLElement} root The element that holds the bar, its chips and its popover. * @param {{ fields: object[], applied?: object[], onChange: (applied: object[]) => void }} options * @returns {{ readonly applied: object[], setFields(fields: object[], applied?: object[]): void, destroy(): void }} */export function filters(root, options) { const part = (name) => root.querySelector(`[data-filter="${name}"]`) const open = part('open') const count = part('count') const chips = part('chips') const clearAll = part('clear-all') const shown = part('applied') const panel = part('panel') const form = part('form') const list = part('conditions') const empty = part('empty') const error = part('error') const add = part('add') const controller = new AbortController() const { signal } = controller let fields = options.fields let applied = [...(options.applied ?? [])] let draft = [] const fieldOf = (key) => fields.find((field) => field.key === key) /** A copy of one of the page's templates: the markup is the page's, this only fills it in. */ const clone = (name) => part(name).content.firstElementChild.cloneNode(true) /** @param {[string, string][]} choices @param {string} chosen */ const fill = (select, choices, chosen) => select.replaceChildren(...choices.map(([value, words]) => { const option = new Option(words, value, false, value === chosen) option.className = 'select-option' return option })) /** The control for a condition's value: what its field holds. */ function valueControl(given, field, index) { const label = `Value of condition ${index + 1}` if (field.type === 'enum') { const select = clone('choice') fill(select, field.options, given.value) // Nothing is chosen for a new condition: a first option that cannot be chosen again says so. const prompt = new Option(`Choose a ${field.label.toLowerCase()}`, '', true, !given.value) prompt.disabled = true prompt.className = 'select-option' select.prepend(prompt) select.setAttribute('aria-label', label) return select } const input = clone('row').querySelector('[data-part="value"]') input.type = field.type === 'number' ? 'number' : field.type === 'date' ? 'date' : 'text' if (field.type === 'number') Object.assign(input, { step: 'any', min: '0', inputMode: 'decimal' }) if (field.type === 'text') input.placeholder = 'Value' input.setAttribute('aria-label', field.unit ? `${label}, in ${field.unit}` : label) input.value = given.value return input } /** Write the draft's rows. Focus goes back to the control it was on, by row and part. */ function drawDraft(focus) { list.replaceChildren(...draft.map((given, index) => { const field = fieldOf(given.field) const row = clone('row') row.dataset.index = String(index) row.querySelector('[data-part="joiner"]').textContent = index === 0 ? 'Where' : 'and' const fieldSelect = row.querySelector('[data-part="field"]') fill(fieldSelect, fields.map((each) => [each.key, each.label]), given.field) fieldSelect.setAttribute('aria-label', `Field of condition ${index + 1}`) const operator = row.querySelector('[data-part="operator"]') fill(operator, operatorsOf(field), given.operator) operator.setAttribute('aria-label', `Comparison of condition ${index + 1}`) row.querySelector('[data-part="value"]').replaceWith(valueControl(given, field, index)) row.querySelector('[data-part="remove"]').setAttribute('aria-label', `Remove condition ${index + 1}`) return row })) empty.hidden = draft.length > 0 if (focus) list.querySelector(`[data-index="${focus.index}"] [data-part="${focus.part}"]`)?.focus() } function say(message, control) { error.textContent = message ?? '' error.hidden = !message for (const invalid of list.querySelectorAll('[aria-invalid="true"]')) { invalid.removeAttribute('aria-invalid') invalid.removeAttribute('aria-describedby') } if (control) { control.setAttribute('aria-invalid', 'true') control.setAttribute('aria-describedby', error.id) } } /** Write the applied conditions: a chip each, the count on the button, and Clear all when there is any. */ function drawApplied() { chips.replaceChildren(...applied.map((given, index) => { const words = describe(given, fieldOf(given.field)) const chip = clone('chip') chip.querySelector('.chip-label').textContent = words const remove = chip.querySelector('.chip-remove') remove.setAttribute('aria-label', `Remove the filter ${words}`) remove.dataset.index = String(index) return chip })) // The wrappers have no display of their own, so `hidden` hides them whatever their content's classes say. shown.hidden = applied.length === 0 count.firstElementChild.textContent = String(applied.length) count.hidden = applied.length === 0 open.setAttribute('aria-label', applied.length ? `Filters, ${applied.length} applied` : 'Filters') } function commit(next, focusAfter) { applied = next drawApplied() options.onChange(applied) focusAfter?.focus() } // Opening copies what is applied into the draft; closing without Apply drops it. panel.addEventListener('toggle', (event) => { if (event.newState !== 'open') return draft = applied.map((given) => ({ ...given })) say(null) drawDraft() ;(list.querySelector('[data-part="field"]') ?? add).focus() }, { signal }) add.addEventListener('click', () => { draft.push(condition(fields[0])) say(null) drawDraft({ index: draft.length - 1, part: 'field' }) }, { signal }) list.addEventListener('click', (event) => { const remove = event.target.closest('[data-part="remove"]') if (!remove) return const index = Number(remove.closest('li').dataset.index) draft.splice(index, 1) say(null) if (draft.length) drawDraft({ index: Math.min(index, draft.length - 1), part: 'field' }) else { drawDraft(); add.focus() } }, { signal }) // A new field brings its own comparisons and its own kind of value. list.addEventListener('change', (event) => { const control = event.target.closest('[data-part]') const index = Number(control?.closest('li')?.dataset.index) if (!control || Number.isNaN(index)) return if (control.dataset.part === 'field') { draft[index] = condition(fieldOf(control.value)) // What was said about the condition this row held is no longer true of it. say(null) drawDraft({ index, part: 'field' }) } else if (control.dataset.part === 'operator') { draft[index].operator = control.value } else { draft[index].value = control.value } }, { signal }) list.addEventListener('input', (event) => { const control = event.target.closest('[data-part="value"]') if (!control) return draft[Number(control.closest('li').dataset.index)].value = control.value if (control.getAttribute('aria-invalid') === 'true') say(null) }, { signal }) form.addEventListener('submit', (event) => { event.preventDefault() for (const [index, given] of draft.entries()) { const control = list.querySelector(`[data-index="${index}"] [data-part="value"]`) const message = problem(given, fieldOf(given.field), control?.validity) if (message) { say(draft.length > 1 ? `Condition ${index + 1}: ${message}` : message, control) control?.focus() return } } panel.hidePopover() commit(draft.map((given) => ({ ...given, value: String(given.value).trim() })), open) }, { signal }) part('cancel').addEventListener('click', () => panel.hidePopover(), { signal }) part('clear').addEventListener('click', () => { draft = [] say(null) drawDraft() add.focus() }, { signal }) chips.addEventListener('click', (event) => { const remove = event.target.closest('.chip-remove') if (!remove) return const index = Number(remove.dataset.index) const next = applied.filter((_, at) => at !== index) commit(next) // Focus stays in the row of chips: on the next one, or the one before, or the button that opens the filters. ;(chips.querySelectorAll('.chip-remove')[Math.min(index, next.length - 1)] ?? open).focus() }, { signal }) clearAll.addEventListener('click', () => commit([], open), { signal }) drawApplied() return { get applied() { return applied.map((given) => ({ ...given })) }, setFields(next, start = []) { fields = next applied = start drawApplied() }, destroy() { controller.abort() if (panel.matches(':popover-open')) panel.hidePopover() }, }}/** * The demo's query logic, shared by the filter bar and the table view: conditions on a field, an operator and a * value, all of which a row must meet (one level of AND), plus search, sort and pages. It reads plain rows and * plain fields and touches no element. An application asks its own data source the same questions; this is * not a query engine to publish. * * A field is `{ key, label, type, options? }`: `type` is `text`, `enum`, `number` or `date`, and an enum lists * its `options` as `[value, label]`. A date is `YYYY-MM-DD`, so dates compare as text. *//** The operators each type of field offers, in the order a reader meets them: `[name, words]`. */export const operators = { text: [['contains', 'contains'], ['is', 'is'], ['is-not', 'is not'], ['starts', 'starts with']], enum: [['is', 'is'], ['is-not', 'is not']], number: [['is', 'is'], ['more', 'is more than'], ['less', 'is less than']], date: [['on', 'is on'], ['before', 'is before'], ['after', 'is after']],}/** @param {{ type: keyof typeof operators }} field */export const operatorsOf = (field) => operators[field.type]/** A new condition on a field: its first operator, and a value still to give, even a choice. */export function condition(field) { return { field: field.key, operator: operatorsOf(field)[0][0], value: '' }}/** * What is wrong with a condition, in words, or null when it can be applied. The input's own validity comes first, * so a number field that holds letters is caught as the browser sees it. * @param {{ field: string, operator: string, value: string }} given * @param {{ type: string, label: string, options?: string[][] }} field * @param {ValidityState} [validity] */export function problem(given, field, validity) { if (!field) return 'Choose a field.' if (!operatorsOf(field).some(([name]) => name === given.operator)) return `Choose how ${field.label.toLowerCase()} compares.` const value = String(given.value ?? '').trim() if (validity?.badInput) return field.type === 'date' ? `Enter a whole date for ${field.label.toLowerCase()}.` : `Enter a number for ${field.label.toLowerCase()}.` if (!value) return field.type === 'enum' ? `Choose a ${field.label.toLowerCase()}, or remove the condition.` : `Enter a value for ${field.label.toLowerCase()}, or remove the condition.` if (field.type === 'number' && !Number.isFinite(Number(value))) return `Enter a number for ${field.label.toLowerCase()}.` if (field.type === 'date' && !/^\d{4}-\d{2}-\d{2}$/.test(value)) return `Enter a whole date for ${field.label.toLowerCase()}.` if (field.type === 'enum' && !field.options.some(([option]) => option === value)) return `Choose a ${field.label.toLowerCase()}.` return null}/** Whether a row meets one condition. */function meets(row, given, field) { const have = row[field.key] const want = given.value switch (field.type) { case 'text': { const text = String(have).toLowerCase() const term = String(want).trim().toLowerCase() if (given.operator === 'contains') return text.includes(term) if (given.operator === 'starts') return text.startsWith(term) return given.operator === 'is' ? text === term : text !== term } case 'enum': return given.operator === 'is' ? have === want : have !== want case 'number': { const number = Number(want) if (given.operator === 'more') return have > number if (given.operator === 'less') return have < number return have === number } case 'date': if (given.operator === 'before') return have < want if (given.operator === 'after') return have > want return have === want } return true}/** The rows that meet every condition. */export function filter(rows, conditions, fields) { const keyed = new Map(fields.map((field) => [field.key, field])) return rows.filter((row) => conditions.every((given) => meets(row, given, keyed.get(given.field))))}/** The rows whose given text holds what was typed, without regard to case. */export function search(rows, text, keys) { const term = text.trim().toLowerCase() if (!term) return rows return rows.filter((row) => keys.some((key) => String(row[key]).toLowerCase().includes(term)))}/** The rows in the order of one field, ascending or descending; ties keep the order they had. */export function sort(rows, key, direction = 'ascending') { const sign = direction === 'descending' ? -1 : 1 const compare = (a, b) => typeof a === 'number' ? a - b : String(a).localeCompare(String(b), 'en', { numeric: true }) return [...rows].sort((a, b) => compare(a[key], b[key]) * sign)}/** One page of rows, and where it stands among them. A page past the end is the last one. */export function paginate(rows, page, size) { const pages = Math.max(1, Math.ceil(rows.length / size)) const current = Math.min(Math.max(1, page), pages) const start = (current - 1) * size return { rows: rows.slice(start, start + size), page: current, pages, from: rows.length ? start + 1 : 0, to: Math.min(start + size, rows.length), total: rows.length }}const dates = new Intl.DateTimeFormat('en-US', { month: 'short', day: 'numeric', year: 'numeric', timeZone: 'UTC' })/** A date as a reader reads it: `Nov 13, 2026`. */export const formatDate = (value) => dates.format(new Date(`${value}T00:00:00Z`))/** A condition in words, for its chip: `Status is Ready`. */export function describe(given, field) { const words = operatorsOf(field).find(([name]) => name === given.operator)?.[1] ?? given.operator const value = field.type === 'enum' ? field.options.find(([option]) => option === given.value)?.[1] ?? given.value : field.type === 'date' ? formatDate(given.value) : field.type === 'text' ? `“${String(given.value).trim()}”` : given.value return `${field.label} ${words} ${value}${field.unit ? ` ${field.unit}` : ''}`}搜索、筛选、排序和翻页浏览项目,或切换到成员和文件。选中行后可以删除。
区块提供可直接复制的标记,沿用所在页面的文字颜色和字号。预览在 body 上设置 fg-text-body text-sm。每个区块都是尺寸容器,会根据自身宽度重排,无论放在宽页面、侧边栏大小的空间还是对话框中。
#区块组成
- 一块面板。 各区域用细线
bt(1) bt-solid b-line-hair分隔,避免在卡片内再套卡片。 - 顶部标题区采用两端布局。 左侧是工作区名称和显示匹配行数的计数,右侧是在项目、成员和文件之间切换的分段控件以及主要操作。整体使用
flex flex-wrap items-center justify-between,面板较窄时,切换控件和主要操作移到标题下方。 - 搜索、筛选和排序位于同一可换行的行中。 其中有宽度在 200px 到 320px 之间伸展的搜索输入组、筛选栏的按钮,以及用于选择排序方式的凸起下拉选择框,后者用
ml-auto推到行尾。已应用的筛选条件在下方单独占一行,以胶囊标签显示,每个都可单独移除。 - 排序状态直接显示在表头。 已排序列的列标题带有
aria-sort,表格会加深标题文字颜色,并显示尖角箭头;更改排序则通过下拉选择框。 - 表格在自己的区域内滚动。 表格放在带有
scroll-area-inline的滚动区域中,面板变窄时,横向滚动限定在表格内,仍有列可继续滚动查看的边缘会渐隐。 - 每个数据集都有自己的首列单元格。 项目显示名称及其下方的域名,成员显示头像及旁边的姓名和邮箱,文件显示图标框及旁边的文件名。每个首列单元格都是
<th scope="row">,因此辅助技术读出单元格时会一并读出所在行。 - 选择只针对匹配的行。 在
table-row上设置aria-selected="true",使该行呈现选中色调,同时勾选行内的复选框。表头复选框会选中所有页中与搜索和筛选条件匹配的每一行,只选中其中一部分时则处于indeterminate状态。不再匹配的行会退出选择,因此“已选 3 项,共 15 项”只统计当前匹配结果中的行。 - 底部采用两端布局。 左侧是已选数量,有选中行时还会显示“删除”和“清除选择”;右侧是数据范围和由按钮组成的分页。到达首页或末页时,“上一页”或“下一页”会被禁用;页数较多时,保留第一页、最后一页以及当前页的相邻页,中间用省略号隔开。
- 行操作收在菜单中,由行尾一个低调的按钮打开。按钮名称包含该行所显示的项目、成员或文件的名称,例如“Atlas 的操作”。
- 没有匹配结果时会明确说明。 空状态填满表格主体,并提供返回的途径:清除搜索和筛选条件。
#哪些操作需要脚本
表格行是数据,因此示例中的 main.js 从 data.js 读取数据,写入标记中为每个数据集准备的模板:样式表和工具类来自标记,文字来自数据。它与筛选栏共用两个模块:query.js 对普通的行数据进行搜索、筛选、排序和分页,filters.js 负责运行筛选栏。在实际产品中,这些查询应交给你的数据源处理;区块只展示如何呈现每一种结果。没有脚本时,用 <form method="get"> 包住各区域:搜索框、下拉选择框和复选框会变成服务器可以处理的查询字符串,分页则变成链接。
#对选中行执行操作
示例只有一个针对选中行的操作,放在底部的已选数量旁边。如果有多个操作,就在浮动工具栏中显示已选数量、各项操作以及清除选择的入口。用 sticky bottom(16) 将工具栏放在面板底部,也可将其固定在视口底部;有选中行时显示工具栏。
对选中行执行操作
<div class="toolbar toolbar-floating flex-wrap justify-center" role="toolbar" aria-label="2 projects selected"> <span class="px-xs fg-text-strong">2 selected</span> <span class="toolbar-separator" role="separator" aria-orientation="vertical"></span> <button type="button" class="btn btn-quiet btn-sm"> <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <path d="M8 1.3333C4 4.6667 3.6667 8.6667 5.6667 12.3333H10.3333C12.3333 8.6667 12 4.6667 8 1.3333Z" /><circle cx="8" cy="6.6667" r="0.85" fill="currentColor" stroke="none" /><path d="M10.016 12.3333Q10.1079 12.4463 10.2446 12.5147L12.5342 13.6594A0.6999 0.6999 0 0 0 13.5377 13.1484L14.1443 9.5091Q14.1989 9.1824 13.9807 8.9328L11.9808 6.6473A0.7011 0.7011 0 0 0 11.3582 6.4146C11.5022 7.0678 11.5536 7.7269 11.5195 8.3865L11.635 7.7707L13.1346 9.4845L12.6181 12.5833L10.8942 11.7215L11.1203 10.5157C10.924 11.1275 10.6597 11.7349 10.3333 12.3333H10.016ZM4.6418 6.4146A0.6958 0.6958 0 0 0 4.0192 6.6473L2.0193 8.9328Q1.8012 9.1824 1.8557 9.5091L2.4623 13.1484A0.6999 0.6999 0 0 0 3.4658 13.6594L5.7554 12.5147Q5.8921 12.4463 5.984 12.3333H5.6667C5.3403 11.7349 5.076 11.1275 4.8797 10.5157L5.1058 11.7215L3.3819 12.5833L2.8654 9.4845L4.365 7.7707L4.4805 8.3865C4.4464 7.7269 4.4978 7.0678 4.6418 6.4146Z" fill="currentColor" stroke="none" fill-rule="evenodd" /><path d="M6.6667 15L9.3333 15" /> </svg>Redeploy </button> <button type="button" class="btn btn-quiet btn-sm"> <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <path d="M8.5 9L8.4994 1.9755A0.5 0.5 0 0 0 7.5054 1.9266L7.5 2L7.5014 9.0379A0.5007 0.5007 0 0 0 7.5892 9.285A0.7044 0.7044 0 0 0 8.4108 9.285A0.4996 0.4996 0 0 0 8.5 9Z" fill="currentColor" stroke="none" fill-rule="evenodd" /><path d="M5 6L7.8586 8.8586A0.2 0.2 0 0 0 8.1414 8.8586L11 6" /><path d="M2.5 10.5L2.5 12.5A1 1 0 0 0 3.5 13.5L12.5 13.5A1 1 0 0 0 13.5 12.5L13.5 10.5" /> </svg>Export </button> <button type="button" class="btn btn-quiet btn-sm btn-danger"> <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <path d="M2.5 4.5L13.5 4.5" /><path d="M4 4.5L4.4501 13.0526A1 1 0 0 0 5.4488 14L10.5512 14A1 1 0 0 0 11.5499 13.0526L12 4.5" /><path d="M6 4.5L6 3A1 1 0 0 1 7 2L9 2A1 1 0 0 1 10 3L10 4.5" /><path d="M8 7.5L8 11" /> </svg>Delete </button> <span class="toolbar-separator" role="separator" aria-orientation="vertical"></span> <button type="button" class="btn btn-quiet btn-square btn-sm" aria-label="Clear the selection"> <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <path d="M4 4L12 12" /><path d="M4 12L12 4" /> </svg> </button></div>import RocketIcon from '@master/icons-react/native/16/soft/rocket'import DownloadIcon from '@master/icons-react/native/16/soft/download'import TrashIcon from '@master/icons-react/native/16/soft/trash'import XIcon from '@master/icons-react/native/16/soft/x'<div className="toolbar toolbar-floating flex-wrap justify-center" role="toolbar" aria-label="2 projects selected"> <span className="px-xs fg-text-strong">2 selected</span> <span className="toolbar-separator" role="separator" aria-orientation="vertical"></span> <button type="button" className="btn btn-quiet btn-sm"> <RocketIcon className="icon" />Redeploy </button> <button type="button" className="btn btn-quiet btn-sm"> <DownloadIcon className="icon" />Export </button> <button type="button" className="btn btn-quiet btn-sm btn-danger"> <TrashIcon className="icon" />Delete </button> <span className="toolbar-separator" role="separator" aria-orientation="vertical"></span> <button type="button" className="btn btn-quiet btn-square btn-sm" aria-label="Clear the selection"> <XIcon className="icon" /> </button></div><script> import RocketIcon from '@master/icons-svelte/native/16/soft/rocket' import DownloadIcon from '@master/icons-svelte/native/16/soft/download' import TrashIcon from '@master/icons-svelte/native/16/soft/trash' import XIcon from '@master/icons-svelte/native/16/soft/x'</script><div class="toolbar toolbar-floating flex-wrap justify-center" role="toolbar" aria-label="2 projects selected"> <span class="px-xs fg-text-strong">2 selected</span> <span class="toolbar-separator" role="separator" aria-orientation="vertical"></span> <button type="button" class="btn btn-quiet btn-sm"><RocketIcon class="icon" />Redeploy</button> <button type="button" class="btn btn-quiet btn-sm"><DownloadIcon class="icon" />Export</button> <button type="button" class="btn btn-quiet btn-sm btn-danger"> <TrashIcon class="icon" />Delete </button> <span class="toolbar-separator" role="separator" aria-orientation="vertical"></span> <button type="button" class="btn btn-quiet btn-square btn-sm" aria-label="Clear the selection"> <XIcon class="icon" /> </button></div><script setup>import RocketIcon from '@master/icons-vue/native/16/soft/rocket'import DownloadIcon from '@master/icons-vue/native/16/soft/download'import TrashIcon from '@master/icons-vue/native/16/soft/trash'import XIcon from '@master/icons-vue/native/16/soft/x'</script><template> <div class="toolbar toolbar-floating flex-wrap justify-center" role="toolbar" aria-label="2 projects selected"> <span class="px-xs fg-text-strong">2 selected</span> <span class="toolbar-separator" role="separator" aria-orientation="vertical"></span> <button type="button" class="btn btn-quiet btn-sm"><RocketIcon class="icon" />Redeploy</button> <button type="button" class="btn btn-quiet btn-sm"><DownloadIcon class="icon" />Export</button> <button type="button" class="btn btn-quiet btn-sm btn-danger"> <TrashIcon class="icon" />Delete </button> <span class="toolbar-separator" role="separator" aria-orientation="vertical"></span> <button type="button" class="btn btn-quiet btn-square btn-sm" aria-label="Clear the selection"> <XIcon class="icon" /> </button> </div></template>#无障碍
- 面板是一个
<section>,以其标题作为无障碍名称,表格也以该标题命名。 - 每个行内复选框的无障碍名称都包含该行所代表的对象名称,例如“选择 Atlas”。表头复选框的名称说明它会选中什么,例如“选择全部 15 个项目”。
- 标题下方的计数是普通文本;另有一个
sr-only的状态区域,用于说明操作的结果,例如“第 2 页,共 3 页”“已删除 3 个项目”。焦点停留在刚按下的控件上;如果该控件所在的行已不存在,焦点会移到表头复选框。 - 操作列的列标题在视觉上隐藏,供辅助技术读取。
- 分页使用带标签的
<nav>;由于各页会就地更改视图,页码使用按钮,当前页带有aria-current="page"。 - 每行的第一个单元格是
<th scope="row">,因此辅助技术会在读出单元格时一并读出对应的项目、成员或文件。 - 筛选条件在名为“筛选项目”的弹出层中打开;其中各行和提示信息如何朗读,参见筛选栏。
- 表格在滚动区域内横向滚动。表格中包含复选框和按钮,因此键盘用户可以通过它们访问表格。如果表格内没有可聚焦的元素,需要为滚动区域设置
tabindex="0"、角色和无障碍名称。
#相关内容
- 表面托盘、凸起面板与凹槽构成所有组件共用的材质层级。
- 标题沿用页面文本色的标题,可搭配上方的引题和下方的导语。
- 分段控件多个选项共用一个凹槽,凸起面板滑向当前选项。
- 按钮凸起的控件,悬停时抬升,按下时下沉。
- 输入框嵌入表面的输入框,支持标签、提示、错误信息和输入框组。
- 下拉选择框原生下拉选择框,选项列表与控件使用同一套材质。
- 弹出层以触发按钮为锚点的浮动面板。
- 胶囊标签胶囊形控件,用作可切换的筛选条件或可移除的值。
- 徽标用于显示状态、分类或计数的小型胶囊标签,只有颜色本身有含义时才使用彩色。
- 滚动区域可滚动的区域。对应方向还有内容时,边缘渐隐。
- 表格用细线分隔数据行,支持可排序表头和数值对齐。
- 复选框小型原生凹槽,勾选后填充墨色。
- 头像以照片或姓名首字母代表个人或团队,可单独显示或叠放。
- 图标框标题旁的小型凹面图标框,让图标保持原有尺寸。
- 菜单在触发按钮旁打开弹出层,展示一组操作。
- 空状态视图暂无内容时呈现的状态。
- 分页用一排链接在长列表的各页间跳转,当前页呈按下的凹陷状态。
- 工具栏在小型凸起面板上排列一组低调的控件。