5.8.6
Look and text
Match the search dialog and results page to your site with custom properties, parts and strings, or build your own dialog.
The search dialog draws in its own shadow root, so your site’s CSS does not change it by accident. You change its look in two ways: custom properties for colors, font and size, and ::part() for any single piece. You change its text with strings.
Three layers
Search comes in three layers. Most sites use the top one as it is.
| Layer | Import | Use it when |
|---|---|---|
| Default dialog | @astronomic-dev/search/ui (loaded for you by SearchLink and on EmDash) |
You want search working today, themed with custom properties and parts. |
| Browser behavior | @astronomic-dev/search/dom |
You render your own markup and want the dialog, keyboard, tabs, shortcut and screen reader updates done right. |
| Controller | @astronomic-dev/search/controller |
You want only the state: the query, the results, the tabs, the filters and recent searches. No DOM. |
@astronomic-dev/search/core holds the engine, for code that searches an index with no UI at all.
Build your own dialog
On plain Astro, a dialog of your own still loads only when a visitor reaches for search. Call startBoot({ selector, keys, load }) from @astronomic-dev/search/boot in your button’s script. It waits for hover, focus, a click or the shortcut, then calls load() (your app, through import()).
Give your buttons data-search-ui="yours" and pass selector: '[data-search-trigger][data-search-ui="yours"]'. The default dialog skips any button with a data-search-ui, so the two never take each other’s buttons. Use one kind of dialog per page.
On a phone, a dialog that opens after the tap gets no keyboard. Pass touch: touchOpens(view) too, from the same entry. Your view shows a stand-in box that looks like your dialog’s field, and your app’s openTouch(trigger, draft) moves its text and caret into the dialog. The README has the details.
For a worked example, see the Search family in Astronomic UI. The package README has the full API.
The dialog element
The default dialog is an <astronomic-search> element. SearchLink adds it to the page on first use. You can also place it yourself.
| Attribute | What it does |
|---|---|
index |
The index to search. Default: the default setting. |
types |
A comma list of the types this dialog shows. Default: all of the index’s types. |
group |
type, a facet name, or false. Replaces the index’s group.dialog for this dialog. |
theme |
light or dark. Default: the theme setting, else the visitor’s device setting. On an element you place yourself, only this attribute counts. |
SearchLink takes index, types and group as props, plus main (the button the shortcut belongs to; default the first on the page), href and class. Its slot is the label.
href is where the link goes with no JavaScript, and where “See all results” goes, with ?q= added. The default is /search under your base:
<SearchLink href="/docs">Search the docs</SearchLink>
On EmDash, the plugin adds the element for you. The admin page’s Theme setting puts theme on it. See Settings.
Open on a search
A button can open the dialog with a search already typed. Point its href at the same search on your search page, so the link still works with no JavaScript.
| Attribute | What it does |
|---|---|
data-search-query |
The words in the box. |
data-search-type |
The type tab to select. Default: all types. |
data-search-filters |
Filter picks, as in the search page’s URL: theme=lodestar, or theme=lodestar&theme=vega for two. |
<SearchLink href="/search?q=dark+mode&type=docs" data-search-query="dark mode" data-search-type="docs">
Dark mode in the docs
</SearchLink>
A button with none of these opens on the last search.
Light, dark and toggles
The dialog follows the visitor’s device. To keep it light or dark for everyone, set theme:
search({
sources: [collections({ docs: {} })],
theme: "light", // or "dark"
});
It applies to every dialog SearchLink adds, on every page. You can still change the theme attribute from a script afterwards. Your change stays, through view transitions too. An <astronomic-search> you place yourself takes its own theme attribute, not this setting. The search page takes your page’s colors, so theme does not change it.
A site with its own light and dark toggle. Search cannot know what your toggle changes, so leave theme out and set the dialog’s theme attribute with a small script. This one reads <html data-theme="dark"> or <html class="dark">. Change the dark line if yours differs:
// src/scripts/search-theme.js
// Keeps the search dialog's theme the same as the site's toggle.
const root = document.documentElement;
function sync() {
const dark = root.dataset.theme === "dark" || root.classList.contains("dark");
for (const el of document.querySelectorAll("astronomic-search, [data-search-proxy]")) el.setAttribute("theme", dark ? "dark" : "light");
}
const observer = new MutationObserver(sync);
// The toggle.
observer.observe(root, { attributes: true, attributeFilter: ["data-theme", "class"] });
// The dialog, which joins the page on first use, and its stand-in on a phone.
observer.observe(document, { childList: true, subtree: true });
// A dialog already on the page.
sync();
Load it from your layout, so every page has it:
<script>
import "../scripts/search-theme.js";
</script>
It also sets a dialog that is made later, and it keeps working with Astro’s <ClientRouter />.
If your colors are already custom properties that your toggle changes, you can point the dialog’s custom properties at them instead, with no script. Use your own names for the ones on the right:
astronomic-search,
[data-search-proxy] {
--search-bg: var(--page);
--search-fg: var(--ink);
--search-muted: var(--ink-soft);
--search-border: var(--line);
--search-accent: var(--brand);
--search-accent-fg: var(--on-brand);
--search-focus: var(--brand);
--search-mark-bg: color-mix(in srgb, var(--brand) 25%, transparent);
--search-mark-fg: var(--ink);
color-scheme: inherit;
}
color-scheme: inherit takes the page’s own color-scheme, so the scrollbar matches. Any color you leave out still follows theme, or the device.
On EmDash, the admin page has a Theme setting in place of the option. See Settings.
Custom properties
Set these on astronomic-search or on any ancestor, such as :root. Each has a light default and a dark one, so set both if you set colors:
astronomic-search {
--search-font: "Inter", sans-serif;
--search-accent: #0f766e;
--search-radius: 0.5rem;
}
@media (prefers-color-scheme: dark) {
astronomic-search {
--search-accent: #5eead4;
}
}
| Property | Light default | Dark default | Use |
|---|---|---|---|
--search-font |
system UI font | same | Every text in the dialog |
--search-bg |
#ffffff |
#17191e |
Dialog background |
--search-fg |
#16181d |
#eceef2 |
Text |
--search-muted |
#5c6370 |
#a3a9b5 |
Snippets, labels, hints |
--search-border |
#dfe2e7 |
#31353d |
Lines and outlines |
--search-line |
the border, 60% toward the background | same | The rail beside heading results |
--search-accent |
#2c5fd6 |
#8fb0ff |
Links, picked filters, the active result’s tint |
--search-accent-fg |
#ffffff |
#0d1117 |
Text on the accent |
--search-mark-bg |
#fdeaa6 |
#5b4a12 |
Matched words |
--search-mark-fg |
inherit |
#fff6d6 |
Matched words’ text |
--search-radius |
0.75rem |
same | Dialog corners (inner corners follow) |
--search-shadow |
soft drop shadow | darker shadow | Dialog shadow |
--search-width |
40rem |
same | Dialog width on wide screens |
--search-backdrop |
dark, 45% | black, 60% | The backdrop behind the dialog |
--search-focus |
#2c5fd6 |
#8fb0ff |
Focus rings |
On screens up to 40rem wide, the dialog fills the screen and keeps clear of the notch and the home bar. With reduced motion on, it opens with no animation.
On a phone, the first tap shows a small box that looks like the dialog’s search box, so the keyboard opens at once. It uses the same custom properties.
Parts
Style any single piece with astronomic-search::part(name). These names are stable.
astronomic-search::part(input) {
font-size: 1.0625rem;
}
astronomic-search::part(mark) {
text-decoration: underline;
}
| Part | Element |
|---|---|
dialog |
The <dialog> |
panel |
The box inside it |
header |
The row with the search box |
input |
The search box |
close |
The close button |
tabs |
The type tabs row |
tab |
One type tab |
filters-toggle |
The button that shows and hides the filters on small screens |
facets |
The filter row |
facet |
One filter button |
results |
The results list |
group |
One group of results |
group-label |
A group’s heading: its name, then its count |
group-count |
A group’s count, inside its group-label |
option |
One result (data-kind says page or heading), or a suggested link (data-kind="suggested") |
option-title |
A page result’s title |
option-heading |
A heading result’s heading |
snippet |
The text under a result |
mark |
A matched word |
type-label |
A result’s type, when results are not grouped |
empty |
The no-results message |
error |
The “not available” message |
recent |
Recent searches |
suggested |
The suggested links in an empty box (also a group) |
footer |
The bottom row |
see-all |
The “See all results” link |
new-tab |
The notice that links open in a new tab, in a dialog whose links do |
kbd |
A key in the footer hints |
status |
The screen reader count (visually hidden) |
The results page
The results page on EmDash and on the embed draws in its own shadow root too. It has no colors of its own: font and text color come from your page, and every line, fill and matched word is your text color, mixed lighter. So it fits a light or a dark theme with no change.
It reads the same --search-* properties, set on the results mount or an ancestor. A few are its own:
| Property | Default | Use |
|---|---|---|
--search-rule |
text color, 9% | The lines between results |
--search-band |
text color, 4% | The soft band behind each group’s heading |
--search-selected |
text color, 12% | A picked tab or filter |
--search-active |
the accent at 10% on the background | The active option’s fill (the one the arrow keys picked) |
--search-active-edge |
none (transparent) |
A 2px bar on the active option’s start side, in this color: for a fill too faint to see on its own |
The Search button
The results page’s Search button is an outline in your text color. To fill it with your brand color, set --search-accent:
[data-search-results] {
--search-accent: #0f766e;
}
The button’s text is then white or black, whichever reads better on the accent. To pick it yourself, set --search-accent-fg. On EmDash, the mount is <SearchResults>. See The results page.
Strings
Every label has a US English default. On plain Astro, replace any of them with the strings setting:
search({
sources: [collections({ docs: {} })],
strings: {
placeholder: "Search the docs",
noResults: "Nothing found for “{query}”",
results: { one: "1 page", other: "{count} pages" },
},
});
| Key | Default |
|---|---|
dialogLabel |
Search |
placeholder |
Search |
close |
Close |
allTypes |
All |
seeAll |
See all results |
newTab |
Links open in a new tab (the notice in a dialog whose links do) |
loading |
Loading… |
unavailable |
Search is not available right now. |
retry |
Try again |
noResults |
No results for “{query}” |
searchAllTypes |
Search all types |
recent |
Recent searches |
suggested |
Suggested |
browse |
Browse |
facetsLabel |
Filters |
typesLabel |
Content types |
other |
Other |
results |
{ zero: "No results", one: "1 result", other: "{count} results" } |
keys |
{ move: "to move", open: "to open", close: "to close" } |
{query} and {count} are filled in for you. results and keys merge by key, so you can replace just one of them.
The results page has a few words of its own, beside these: label (Search), submit (Search), showMore (Show more results), moreShown (Result {position} of {total}: {title}), headingMark (#), crumbMark (›) and typeText (Type). On the embed, set them in the config tag’s strings.
On EmDash, the admin page sets one string: Search box text, which is placeholder. See Settings.
Keyboard
mod+k(Cmd+K on Apple devices, Ctrl+K elsewhere) opens or closes search. In a text field or another open dialog, it only closes an open search, so it never opens a second search box from inside another./opens search, except while typing in a text field, a textarea or an editable area.- Up and Down move through the results. Focus stays in the box.
- Enter opens the result. Cmd+Enter or Ctrl+Enter opens it in a new tab.
- Escape clears the box, then closes the dialog. Focus goes back to the button that opened it.
- Tab moves to the type tabs (Left and Right switch them), the filters and “See all results”.
- No key acts while an input method is still composing text.
To change the shortcut keys, see Settings.