搜尋結果頁面回應一次搜尋:輸入關鍵字的欄位、縮小結果範圍的篩選條件、筆數、排序方式,以及分頁呈現的結果。它建立在頁面外框上,包含頁首、主要區域和頁尾,並由整份文件一起捲動。空間足夠時,篩選條件位於結果旁邊;空間不足時,則收在抽屜裡。
搜尋結果
<div class="page bg-surface-base fg-text-body text-sm" data-example="results"> <a class="skip" href="#rs-main">Skip to content</a> <header class="navbar" style="--navbar-padding-x: max(1.5rem, calc(50% - 33.5rem))"> <a class="navbar-brand" href="#"> <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="8" cy="8" r="6" /> <path d="M8 4.3333A3.6667 3.6667 0 0 1 8 11.6667Z" fill="currentColor" stroke="none" /> </svg>Atlas</a> <nav class="navbar-nav" aria-label="Main"> <a class="navbar-link" href="#">Product</a> <a class="navbar-link" href="#">Pricing</a> <a class="navbar-link" href="#">Docs</a> <a class="navbar-link" href="#" aria-current="page">Resources</a> </nav> <div class="navbar-end"> <a class="btn btn-sm hidden inline-flex@container(--xl)" href="#">Sign in</a> <button type="button" class="btn btn-quiet btn-square btn-sm navbar-toggle" popovertarget="rs-menu" aria-label="Open the menu"> <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 5.5L13 5.5" /> <path d="M3 10.5L13 10.5" /> </svg> </button> <div class="menu menu-end" id="rs-menu" popover> <a class="menu-item" href="#">Product</a> <a class="menu-item" href="#">Pricing</a> <a class="menu-item" href="#">Docs</a> <a class="menu-item" href="#" aria-current="page">Resources</a> <a class="menu-item" href="#">Sign in</a> </div> </div> </header> <main class="grow(1) w-full max-w(1120) mx-auto px-lg py-xl container-inline" id="rs-main" tabindex="-1"> <header class="flex flex-col gap-3xs"> <h1 class="heading heading-xl">Resources</h1> <p class="m(0) fg-text-muted">Guides, templates, videos and case studies for teams that deploy with Atlas.</p> </header> <form class="flex flex-wrap gap-xs max-w(560) mt-lg" id="rs-form" method="get" role="search" aria-label="Resources"> <label class="input-group grow(1) basis(200) min-w(0)"> <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" name="q" placeholder="Search resources" aria-label="Search resources" autocomplete="off"> </label> <button class="btn">Search</button> </form> <div class="flex flex-col gap-lg mt-xl flex-row@container(--4xl) gap-xl@container(--4xl)"> <div class="w(240)@container(--4xl) pt(6)@container(--4xl) shrink(0)" data-results="sidebar"> <fieldset class="fieldset" data-results="conditions"> <legend class="fieldset-legend">Type</legend> <label class="check flex"> <input type="checkbox" class="checkbox" name="type" value="guide" form="rs-form"> <span class="grow(1)">Guides</span> <span class="fg-text-muted" data-count="guide">4</span> </label> <label class="check flex"> <input type="checkbox" class="checkbox" name="type" value="template" form="rs-form"> <span class="grow(1)">Templates</span> <span class="fg-text-muted" data-count="template">3</span> </label> <label class="check flex"> <input type="checkbox" class="checkbox" name="type" value="video" form="rs-form"> <span class="grow(1)">Videos</span> <span class="fg-text-muted" data-count="video">3</span> </label> <label class="check flex"> <input type="checkbox" class="checkbox" name="type" value="case-study" form="rs-form"> <span class="grow(1)">Case studies</span> <span class="fg-text-muted" data-count="case-study">2</span> </label> </fieldset> </div> <section class="grow(1) basis(0) min-w(0) flex flex-col gap-md" aria-labelledby="rs-results-title"> <h2 class="sr-only" id="rs-results-title">Results</h2> <div class="flex flex-wrap items-center gap-xs"> <div class="hidden@container(--4xl)" data-results="open" hidden> <button type="button" class="btn btn-sm" commandfor="rs-filters" command="show-modal"> <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-results="applied" hidden> <span class="badge badge-sm badge-ink badge-pill" aria-hidden="true">0</span> <span class="sr-only">, 0 applied</span> </span> </button> </div> <p class="m(0) grow(1) fg-text-muted" id="rs-count" role="status">12 resources</p> <div data-results="clear" hidden> <button type="button" class="btn btn-quiet btn-sm">Clear all</button> </div> <div class="flex flex-wrap items-center justify-between gap-xs grow(1) grow(0)@container(--md)"> <label class="flex flex-wrap items-center gap-xs fg-text-muted">Sort <select class="select select-sm" name="sort" form="rs-form"> <option class="select-option" value="updated">Recently updated</option> <option class="select-option" value="name">Name</option> </select> </label> <div class="segmented segmented-sm" role="radiogroup" aria-label="Show results as"> <label class="segmented-item"> <input type="radio" class="segmented-input" name="view" value="cards" form="rs-form" checked>Cards</label> <label class="segmented-item"> <input type="radio" class="segmented-input" name="view" value="list" form="rs-form">List</label> </div> </div> </div> <div id="rs-results"> <ul class="auto-grid" style="--auto-grid-min: 15rem"> <li class="card"> <header class="card-header"> <h3 class="card-title"> <a class="card-link" href="#">Deploy your first project</a> </h3> <p class="card-description">Connect a repository, push to main and open the address Atlas gives it.</p> </header> <div class="card-body flex flex-wrap items-center justify-between gap-xs mt-auto fg-text-muted"> <span class="badge">Guide</span> <time datetime="2026-11-12">Nov 12, 2026</time> </div> </li> <li class="card"> <header class="card-header"> <h3 class="card-title"> <a class="card-link" href="#">A tour of the new dashboard</a> </h3> <p class="card-description">Four minutes through projects, domains and usage, now on one page.</p> </header> <div class="card-body flex flex-wrap items-center justify-between gap-xs mt-auto fg-text-muted"> <span class="badge">Video</span> <time datetime="2026-11-10">Nov 10, 2026</time> </div> </li> <li class="card"> <header class="card-header"> <h3 class="card-title"> <a class="card-link" href="#">How Borealis cut cold builds to under a minute</a> </h3> <p class="card-description">Restoring caches before the install step, measured across forty projects.</p> </header> <div class="card-body flex flex-wrap items-center justify-between gap-xs mt-auto fg-text-muted"> <span class="badge">Case study</span> <time datetime="2026-11-06">Nov 6, 2026</time> </div> </li> <li class="card"> <header class="card-header"> <h3 class="card-title"> <a class="card-link" href="#">Astro blog starter</a> </h3> <p class="card-description">Posts in Markdown, a feed and a preview deployment for every draft.</p> </header> <div class="card-body flex flex-wrap items-center justify-between gap-xs mt-auto fg-text-muted"> <span class="badge">Template</span> <time datetime="2026-11-03">Nov 3, 2026</time> </div> </li> <li class="card"> <header class="card-header"> <h3 class="card-title"> <a class="card-link" href="#">Roll back without a meeting</a> </h3> <p class="card-description">A deployment never changes, so going back is choosing an earlier one.</p> </header> <div class="card-body flex flex-wrap items-center justify-between gap-xs mt-auto fg-text-muted"> <span class="badge">Guide</span> <time datetime="2026-10-29">Oct 29, 2026</time> </div> </li> <li class="card"> <header class="card-header"> <h3 class="card-title"> <a class="card-link" href="#">Reading a build log</a> </h3> <p class="card-description">Where a failed build says what went wrong, and what the timings mean.</p> </header> <div class="card-body flex flex-wrap items-center justify-between gap-xs mt-auto fg-text-muted"> <span class="badge">Video</span> <time datetime="2026-10-24">Oct 24, 2026</time> </div> </li> </ul> </div> <div data-results="foot"> <div class="flex flex-wrap items-center justify-between gap-sm"> <p class="m(0) fg-text-muted" id="rs-range">1–6 of 12</p> <nav aria-label="Pagination"> <ol class="pagination" id="rs-pages"> <li> <a class="btn btn-quiet btn-square btn-sm" aria-label="Previous page" aria-disabled="true"> <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> </a> </li> <li> <a class="btn btn-quiet btn-square btn-sm" href="?page=1" aria-label="Page 1" aria-current="page">1</a> </li> <li> <a class="btn btn-quiet btn-square btn-sm" href="?page=2" aria-label="Page 2">2</a> </li> <li> <a class="btn btn-quiet btn-square btn-sm" href="?page=2" 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> </a> </li> </ol> </nav> </div> </div> <p class="sr-only" id="rs-status" role="status"></p> </section> </div> <dialog class="dialog dialog-drawer" id="rs-filters" closedby="any" aria-labelledby="rs-filters-title" style="--dialog-width: 20rem"> <header class="dialog-header"> <h2 class="dialog-title" id="rs-filters-title">Filters</h2> </header> <div class="dialog-body" data-results="drawer"></div> <footer class="dialog-footer"> <button type="button" class="btn" data-results="drawer-clear">Clear</button> <button type="button" class="btn btn-primary" commandfor="rs-filters" command="close" data-results="drawer-done" aria-live="polite">Show 12 results</button> </footer> <button type="button" class="btn btn-quiet btn-square btn-sm dialog-close" commandfor="rs-filters" command="close" aria-label="Close the filters"> <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> </dialog> </main> <footer class="bt(1) bt-solid b-line-hair"> <div class="flex flex-wrap items-center justify-between gap-sm w-full max-w(1120) mx-auto px-lg py-lg text-xs fg-text-muted"> <span>© 2026 Atlas Labs</span> <nav class="flex flex-wrap gap-lg" aria-label="Footer"> <a class="link link-quiet" href="#">Privacy</a> <a class="link link-quiet" href="#">Terms</a> <a class="link link-quiet" href="#">Status</a> </nav> </div> </footer> <template id="rs-cards"> <ul class="auto-grid" style="--auto-grid-min: 15rem"> <li class="card"> <header class="card-header"> <h3 class="card-title"> <a class="card-link" href="#"></a> </h3> <p class="card-description"></p> </header> <div class="card-body flex flex-wrap items-center justify-between gap-xs mt-auto fg-text-muted"> <span class="badge"></span> <time></time> </div> </li> </ul> </template> <template id="rs-list"> <ul class="list list-interactive mx(-10)"> <li> <a class="list-row" href="#"> <div class="list-body"> <h3 class="list-title"></h3> <p class="list-description"></p> </div> <span class="list-end flex items-center gap-sm fg-text-muted"> <time class="hidden inline@container(--md)"></time> <span class="badge"></span> </span> </a> </li> </ul> </template> <template id="rs-empty"> <div class="empty"> <span class="icon-frame empty-media" aria-hidden="true"> <svg class="icon" viewBox="0 0 20 20" fill="none" stroke="currentColor" stroke-width="1.25" stroke-linecap="round" stroke-linejoin="round"> <circle cx="8.75" cy="8.75" r="5.75" /> <path d="M12.8159 12.8159L17.5 17.5" /> </svg> </span> <h3 class="empty-title">No resources match</h3> <p class="empty-description">Try another word or fewer types, or clear the conditions to see all 12 resources.</p> <div class="empty-actions"> <button type="button" class="btn btn-sm" data-results="reset">Clear conditions</button> </div> </div> </template> <template id="rs-page"> <ol class="pagination"> <li> <a class="btn btn-quiet btn-square btn-sm" aria-label="Page"></a> </li> </ol> </template></div>import { formatDate, paginate, search, sort } from '../filter-bar/query.js'import { resources, types } from './data.js'const size = 6const labels = new Map(types)const orders = { updated: ['updated', 'descending'], name: ['title', 'ascending'] }/** * The results page on local data. The form is the state: every draw reads the search, the types, the sort and * the view from its named fields, as a server reads them from the URL the same form submits. The script adds * what a round trip would otherwise do (it answers as the reader types and keeps the page number) and moves * the one set of conditions between the sidebar and the drawer. Returns the function that stops it. */export function mount(root) { const $ = (selector) => root.querySelector(selector) const part = (name) => root.querySelector(`[data-results="${name}"]`) const controller = new AbortController() const { signal } = controller const form = $('#rs-form') const drawer = $('#rs-filters') const conditions = part('conditions') const [previous, next] = [$('#rs-pages').firstElementChild, $('#rs-pages').lastElementChild] let page = 1 /** What the form would send: `q`, every checked `type`, `sort` and `view`. */ const query = () => new URLSearchParams(new FormData(form)) /** The address of a page of the same query, for a pagination link. */ const address = (number) => { const given = query() if (!given.get('q')) given.delete('q') given.set('page', number) return `?${given}` } const say = (words) => { $('#rs-status').textContent = words } function results(rows, view) { if (!rows.length) return $('#rs-empty').content.firstElementChild.cloneNode(true) const list = $(`#rs-${view}`).content.firstElementChild.cloneNode(true) const item = list.firstElementChild list.replaceChildren(...rows.map((record) => { const copy = item.cloneNode(true) copy.querySelector('.card-link, .list-title').textContent = record.title copy.querySelector('.card-description, .list-description').textContent = record.summary copy.querySelector('.badge').textContent = labels.get(record.type) copy.querySelector('time').dateTime = record.updated copy.querySelector('time').textContent = formatDate(record.updated) return copy })) return list } function pages(result) { const aim = (link, number, possible) => { if (possible) link.setAttribute('href', address(number)) else link.removeAttribute('href') if (possible) link.removeAttribute('aria-disabled') else link.setAttribute('aria-disabled', 'true') } aim(previous.querySelector('a'), result.page - 1, result.page > 1) aim(next.querySelector('a'), result.page + 1, result.page < result.pages) const numbers = Array.from({ length: result.pages }, (_, index) => { const item = $('#rs-page').content.querySelector('li').cloneNode(true) const link = item.querySelector('a') link.setAttribute('href', address(index + 1)) link.setAttribute('aria-label', `Page ${index + 1}`) link.textContent = String(index + 1) if (index + 1 === result.page) link.setAttribute('aria-current', 'page') return item }) $('#rs-pages').replaceChildren(previous, ...numbers, next) } /** Write the results, the counts and the pages from the form. */ function draw() { const given = query() const chosen = given.getAll('type') const found = search(resources, given.get('q') ?? '', ['title', 'summary']) const rows = sort(found.filter((record) => !chosen.length || chosen.includes(record.type)), ...orders[given.get('sort')]) const result = paginate(rows, page, size) page = result.page const narrowed = chosen.length > 0 || Boolean(given.get('q')?.trim()) $('#rs-count').textContent = !rows.length ? 'No resources match' : rows.length < resources.length ? `${rows.length} of ${resources.length} resources` : `${resources.length} resources` part('clear').hidden = !narrowed // Beside each type, how many of what the search found are of it. for (const count of conditions.querySelectorAll('[data-count]')) count.textContent = String(found.filter((record) => record.type === count.dataset.count).length) part('applied').hidden = chosen.length === 0 part('applied').firstElementChild.textContent = String(chosen.length) part('applied').lastElementChild.textContent = `, ${chosen.length} applied` part('drawer-done').textContent = `Show ${rows.length} ${rows.length === 1 ? 'result' : 'results'}` $('#rs-results').replaceChildren(results(result.rows, given.get('view'))) part('foot').hidden = result.pages === 1 $('#rs-range').textContent = `${result.from}–${result.to} of ${result.total}` pages(result) return result } /** Empty the search and the types, or the types alone, and start from the first page. */ function clear({ text = true } = {}) { if (text) form.elements.q.value = '' for (const box of form.elements.type) box.checked = false page = 1 draw() } // One set of conditions stands where the width puts it: in the sidebar while the Filters button is not // displayed, which the stylesheet decides at 56rem of main region, and in the drawer otherwise. The same // checkboxes move, so nothing is copied and no choice can be lost; each names its form, so it is submitted // from either place. function place() { const wide = getComputedStyle(part('open')).display === 'none' const home = wide ? part('sidebar') : part('drawer') if (conditions.parentElement === home) return const active = root.ownerDocument.activeElement const held = conditions.contains(active) ? active : null // Focus on the drawer's own buttons would go back to the Filters button, which is no longer displayed. const stranded = wide && drawer.open && drawer.contains(active) && !held if (home.moveBefore) home.moveBefore(conditions, null) else home.append(conditions) part('sidebar').hidden = !wide if (wide && drawer.open) drawer.close() ;(held ?? (stranded ? conditions.querySelector('input') : null))?.focus() } part('open').hidden = false place() const observer = new ResizeObserver(place) observer.observe($('#rs-main')) // A field of the form changed, wherever it stands. Only the view keeps the page. root.addEventListener('input', (event) => { if (event.target.form !== form) return if (event.target.name !== 'view') page = 1 say('') draw() }, { signal }) form.addEventListener('submit', (event) => { event.preventDefault() page = 1 draw() }, { signal }) part('clear').addEventListener('click', () => { clear(); form.elements.q.focus() }, { signal }) part('drawer-clear').addEventListener('click', () => clear({ text: false }), { signal }) $('#rs-results').addEventListener('click', (event) => { // A resource's own page is not part of the example. if (event.target.closest('a')) event.preventDefault() if (event.target.closest('[data-results="reset"]')) { clear(); form.elements.q.focus() } }, { signal }) $('#rs-pages').addEventListener('click', (event) => { const link = event.target.closest('a') if (!link) return event.preventDefault() if (!link.hasAttribute('href')) return page = Number(new URLSearchParams(link.search).get('page')) const result = draw() // The link pressed may have lost its address: focus stays on the current page's, without pulling the view back // down to it, and the new page is shown from its first result. $('#rs-pages [aria-current="page"]').focus({ preventScroll: true }) $('#rs-results-title').parentElement.scrollIntoView({ block: 'start' }) say(`Page ${result.page} of ${result.pages}, resources ${result.from} to ${result.to}`) }, { signal }) draw() return () => { controller.abort() observer.disconnect() if (drawer.open) drawer.close() part('sidebar').append(conditions) part('sidebar').hidden = false part('open').hidden = true }}/** * Twelve resources of Atlas, a deployment platform: what the results page searches, narrows and sorts. A real * page asks its server for one page of these and for the count; the rows here stand in for that answer. * `updated` is `YYYY-MM-DD`, so dates sort as text. */export const types = [['guide', 'Guide'], ['template', 'Template'], ['video', 'Video'], ['case-study', 'Case study']]export const resources = [ { id: 'first-project', type: 'guide', title: 'Deploy your first project', summary: 'Connect a repository, push to main and open the address Atlas gives it.', updated: '2026-11-12' }, { id: 'dashboard-tour', type: 'video', title: 'A tour of the new dashboard', summary: 'Four minutes through projects, domains and usage, now on one page.', updated: '2026-11-10' }, { id: 'borealis-builds', type: 'case-study', title: 'How Borealis cut cold builds to under a minute', summary: 'Restoring caches before the install step, measured across forty projects.', updated: '2026-11-06' }, { id: 'astro-blog', type: 'template', title: 'Astro blog starter', summary: 'Posts in Markdown, a feed and a preview deployment for every draft.', updated: '2026-11-03' }, { id: 'rolling-back', type: 'guide', title: 'Roll back without a meeting', summary: 'A deployment never changes, so going back is choosing an earlier one.', updated: '2026-10-29' }, { id: 'build-log', type: 'video', title: 'Reading a build log', summary: 'Where a failed build says what went wrong, and what the timings mean.', updated: '2026-10-24' }, { id: 'incident-runbook', type: 'template', title: 'Incident runbook', summary: 'Who decides, who writes the update and when to roll back, on one page.', updated: '2026-10-20' }, { id: 'custom-domain', type: 'guide', title: 'Move a custom domain to Atlas without downtime, certificates included', summary: 'Lower the record’s lifetime, issue the certificate first, then switch.', updated: '2026-10-15' }, { id: 'cinder-previews', type: 'case-study', title: 'Cinder reviews forty previews a day with a team of six', summary: 'Comments pinned to the page replaced the screenshots in their reviews.', updated: '2026-10-09' }, { id: 'edge-caching', type: 'video', title: 'Edge caching in ten minutes', summary: 'What is cached in each region, for how long, and how a deployment clears it.', updated: '2026-10-02' }, { id: 'launch-checklist', type: 'template', title: 'Launch checklist', summary: 'Domains, redirects, analytics and a rollback plan, to tick off before a launch.', updated: '2026-09-25' }, { id: 'branch-previews', type: 'guide', title: 'Preview every branch', summary: 'Give each branch its own address and protect it with a password.', updated: '2026-09-18' },]/** * 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}` : ''}`}搜尋「build」,然後清除。依名稱排序,前往第二頁,再切換到「List」:排序方式和頁碼都會保留。選擇「平板」或「手機」,開啟「Filters」並勾選一種類型;再選擇「桌面」,側邊欄中的同一項也已勾選。搜尋不存在的內容,即可看到空狀態。
這十二筆資源是範例自身檔案中的資料列,範例的腳本就依這些資料回應。實際的頁面會向伺服器查詢;接上伺服器說明了該在哪裡處理。
#建構方式
- 外框套用
page,內部共用同一個寬度。 主要區域是grow(1) max-w(1120) mx-auto px-lg,頁尾的內容也採用相同的寬度。導覽列占滿整個寬度,其--navbar-padding-x為max(1.5rem, calc(50% - 33.5rem)),也就是內容本身的起點,因此在任何寬度下,品牌、標題、搜尋欄位和頁尾的第一個字都從同一條線開始。 - 搜尋是獨立的表單。
<form method="get" role="search">包含名為q的欄位和送出按鈕。欄位是input-group,會延展到表單的max-w(560);當欄位會窄於 200px 時,按鈕就換到欄位下方。 - 其他欄位指名所屬的表單。 類型核取方塊、排序選單和 Cards / List 切換器都位於
<form>之外,並帶有form="rs-form"。無論放在哪裡,它們都會隨表單一起送出,核取方塊因此才能移進抽屜。 - 從 56rem 起分成兩欄。 主要區域是尺寸容器
container-inline。主體是flex flex-col,主要區域達到 56rem 時改為flex-row@container(--4xl):篩選條件使用w(240)的欄,也就是 15rem;結果則使用彈性寬度的欄grow(1) basis(0) min-w(0)。 - 筆數、排序和檢視方式排在同一行。 筆數是帶有
role="status"的段落,占用剩餘的空間,因此排序選單和切換器位於行尾。這一行可以換行:在手機上,排序選單和切換器會換到第二列,分居兩端。 - 無論哪種檢視方式,結果都是清單。 以卡片呈現時,是內含卡片的
<ul class="auto-grid">,並設定--auto-grid-min: 15rem:在「桌面」每列三張、「平板」兩張、「手機」一張,由網格自行計算。以清單呈現時,是<ul class="list list-interactive">,每一列都是連結;mx(-10)讓各列的文字貼齊欄的邊緣。每頁六筆,下方是目前的範圍和以連結組成的分頁導覽。 - 找不到結果時顯示空狀態。 它會說明沒有結果、建議可以嘗試的做法,並提供清除搜尋和類型的按鈕。
#篩選條件的位置
核取方塊只有一組。它寫在側邊欄裡,切換時是搬移,不是複製。
- 主要區域達到 56rem 時,篩選條件是結果旁邊 15rem 的欄,「Filters」按鈕則為
hidden@container(--4xl)。 - 小於 56rem 時,這一欄是空的並且隱藏,「Filters」按鈕會顯示,並透過
command="show-modal"和commandfor開啟抽屜,也就是設定--dialog-width: 20rem的dialog dialog-drawer。腳本會把<fieldset>放進抽屜的主體。按鈕上的徽章會顯示已勾選的類型數量。 - 門檻由樣式表決定。 腳本不保存任何數字:主要區域的尺寸改變時,它會檢查「Filters」按鈕是否顯示,再依此把 fieldset 放進側邊欄或抽屜。修改標記中的
--4xl,腳本就會跟著調整。 - 切換寬度時不會遺失任何東西,因為沒有任何東西重複:在抽屜中勾選的核取方塊,就是側邊欄中的那個核取方塊。沒有第二個 id,也沒有需要複製的狀態。如果抽屜開啟時頁面變寬,抽屜會關閉,焦點則留在原本所在的核取方塊上。
- 在抽屜中,選擇會立即套用。 結果會在抽屜後方更新,確認按鈕會說明結果的筆數(例如「Show 4 results」)並關閉抽屜。「Clear」會取消勾選所有類型,但不會動到搜尋內容。
沒有腳本時,fieldset 會留在標記所寫的位置:小於 56rem 時在結果上方,達到 56rem 後在結果旁邊;「Filters」按鈕因為只會開啟空的抽屜,所以維持 hidden。
#接上伺服器
標記就是伺服器為第一頁送出的內容:六筆結果、筆數和分頁連結。讀者能設定的每一項,都是同一個 GET 表單中具名的欄位,因此 URL 就是狀態。
- 查詢。 送出表單會傳送
?q=build&type=guide&type=video&sort=name&view=list。type會重複,每個勾選的核取方塊各出現一次;sort為updated或name;view為cards或list。表單沒有action,因此會送到頁面本身的網址。 - 分頁。 每個分頁連結都是同一個查詢再加上
page,例如?sort=name&view=list&page=2。目前頁面的連結帶有aria-current="page";第一頁的「Previous」和最後一頁的「Next」沒有href,並帶有aria-disabled="true"。 - 伺服器渲染的內容。 帶有所讀取值的欄位(搜尋欄位的
value、核取方塊和檢視方式的checked、排序選單的selected)、該頁的結果、筆數,以及沒有符合項目時由伺服器自己輸出的空狀態。條件一旦變更,就是不含page的新查詢,也就是第一頁。 - 腳本增加的功能。 它會在讀者輸入或勾選時直接回應,不必往返伺服器;它會記住頁碼,把篩選條件移進抽屜,並顯示只有它才能運作的控制項:「Filters」按鈕和「Clear all」。它每次都透過
new FormData(form)從表單讀取條件,不保留任何複本;頁碼是它唯一保存的東西。保留這份腳本的專案,請讓它的draw()改為針對該查詢發出請求,並用history.replaceState把同一個查詢寫入網址,這樣重新載入或分享連結時,都會顯示相同的結果。
範例從篩選列的 query.js 匯入 search、sort 和 paginate,用來代替上述請求。它們屬於示範的一部分,不屬於 @master/ui:程式庫並未發布任何查詢引擎。
#使用時機
- 使用搜尋結果頁面:找到的項目要逐一閱讀,而且各有自己的頁面時,例如文章、指南、範本、刊登項目或人員。
- 使用表格檢視:需要跨欄比較資料列、選取並批次操作時。它是應用程式頁面中的一個區塊,自帶搜尋、排序和分頁。
- 使用篩選列:每個條件由欄位、比較方式和值組成,而且要組合多個條件時。側邊欄的核取方塊則適合少數幾組固定、且隨時值得顯示的選項。
- 使用儀表板:頁面的用途是摘要時,先呈現數據和圖表,清單只作為進一步檢視的入口。
#無障礙
- 地標包括帶有
Main導覽的頁首、主要區域、名為Resources的搜尋表單、Pagination導覽,以及帶有自己導覽的頁尾。第一個連結是通往主要區域的略過連結,主要區域設有tabindex="-1"。 - 在任何寬度下,原始碼順序都是閱讀順序:標題、搜尋、篩選條件、筆數與排序、結果、分頁。抽屜寫在主要區域的最後,開啟之前不會妨礙任何人。
- 類型是帶有
<legend>的<fieldset>;每個類型的筆數都寫在其核取方塊的標籤中。Cards / List 切換器是由原生單選按鈕組成的radiogroup,排序則是帶有標籤的原生<select>。 - 筆數帶有
role="status",因此條件變更時不必移動焦點就會朗讀,例如「4 of 12 resources」或「No resources match」。另一個在視覺上隱藏的狀態區域,會在按下分頁連結後說出頁碼和範圍,焦點則留在目前頁面的連結上。 - 結果位於視覺上隱藏的
<h2>「Results」之下,每個標題都是<h3>,讀者因此可以直接跳到結果,並從一筆結果跳到下一筆。卡片的標題就是它的連結card-link,整張卡片都是點按範圍。 - 抽屜是以其標題命名的原生模態
<dialog>。開啟時焦點會移到第一個核取方塊;Esc、在外部按下(closedby="any")、關閉按鈕和「Show results」都會關閉它,焦點則回到「Filters」按鈕。按鈕的名稱會說明已套用幾個篩選條件。 - 「Clear all」和空狀態的按鈕按下後會自行移除,因此焦點會移到搜尋欄位。
#相關項目
- 導覽列位於頁面頂端的導覽列,頁面內容捲到它下方時才會顯示背景。
- 略過連結頁面上的第一個連結,取得焦點時才顯示,可略過導覽並跳到內容。
- 標題以頁面的高對比文字色呈現標題,搭配上方的前導標籤與下方的引言。
- 輸入欄位採內凹樣式的文字欄位,支援標籤、提示、錯誤訊息與群組。
- 按鈕凸起的控制項,懸停時微微抬升,按下時向內凹陷。
- 核取方塊勾選後會填上顏色的原生小方框。
- 下拉選單原生下拉選單,選項清單採用與介面一致的樣式。
- 分段控制項在同一條軌道上排列數個選項,選取指示會滑到目前的選項。
- 徽章顯示狀態、類別或數量的小型膠囊標籤;只有在顏色具有意義時才使用色彩。
- 自動網格在可用空間內排入最多等寬欄,由網格自行計算欄數。
- 卡片帶有浮起效果的面板,各部分會依卡片可用空間調整。
- 清單將項目逐列排列,每列可放置媒體、文字和尾端內容。
- 分頁導覽以一列連結切換長清單的各個頁面,目前所在的頁面會呈現按下的凹陷狀態。
- 空狀態檢視畫面尚無內容可顯示時呈現的狀態。
- 圖示框在標題旁以指定大小呈現圖示的裝飾框。
- 對話框以覆疊面板呈現的原生模態對話框,無須腳本即可開啟和關閉。
- 選單在按鈕旁的彈出視窗中列出可執行的操作。
- 連結沿用句子的文字顏色,帶使用者前往其他位置。