5.8.3
Works with your theme
How search fits a theme's own search box and shortcut keys, and how to check your theme in a minute.
Most themes already have a search box, a search button or a shortcut key of their own. This page covers how Astronomic Search fits each one. Takeover and placements are for EmDash (and the embed). On plain Astro you place the search button yourself.
Shortcut keys
The shortcut keys work on any theme. By default, Cmd+K (Ctrl+K on Windows and Linux) and / open the search dialog. / does nothing while the visitor is typing in a field.
- On EmDash: set the keys on the Search admin page, under First and second shortcut. Click a field and press the keys. Backspace clears it.
- On plain Astro: set
shortcut: { keys: ["mod+k", "/"] }in the integration, orshortcut: falseto turn the keys off. See Settings.
Some themes bind Cmd+K to their own search box. EmDash’s blog template does. With takeover off, a press reaches both, so both searches may open. Turn takeover on, or pick other keys.
With takeover on, our shortcut keys win: a press that opens or closes search never reaches the theme’s listeners. Other keys stay the theme’s. One case still lets the theme’s listener run first: a listener the theme added in the capture phase before our script loaded.
Takeover
Takeover lets the theme’s own search box open our dialog. It is off by default. Turn it on with Take over the theme’s search form on the Search admin page.
It works by itself on two kinds of search box:
- A plain search form. A GET form whose
actionis your results page URL, with one text or search field namedq. Most themes use this form. - EmDash’s
LiveSearchbox (emdash/ui/search), which EmDash’s default templates use in their header. Its own results dropdown stays shut.
When takeover is on:
- A click in the theme’s field, or focus on it, opens our dialog with any text already in it.
- The theme’s field shows the Search box text setting, so it reads the same as the dialog.
- The theme’s field is read-only while our search owns it, so typing never changes it. It still takes focus, and it stays the way back in: a key, a paste, a click or Enter in the field opens our dialog with what you typed or pasted. When the dialog closes, focus goes back to the field. Tab and Escape do not open it.
- Known limit in Firefox: an input method, such as the ones for Chinese, Japanese and Korean, does not start in a read-only field. This can matter after the dialog closes and focus is back in the theme’s field. Click the field or press Enter to open the dialog, then type there.
- If our search cannot load (for example before the first index build), the dialog closes and the theme’s field is given back with its text. It stays the theme’s for the rest of the page. A form submit that cannot open search goes to the results page as usual.
A form is left alone when it does not match. Product filters, POST forms and forms that go to another page keep working.
If your theme’s field has another name, put that name in Search input name. The Results page URL setting must match the form’s action exactly, trailing slash included. To leave one form alone, give it, or an element around it, the class no-search-takeover.
On plain Astro there is no takeover. Put a SearchLink where the theme’s search box was. See Install on Astro.
Custom search
Some themes have a search of their own that is not a form: a button that opens the theme’s own popup, for example. Takeover cannot reach it. Mark it with one data-search-placement element in the theme’s header, and our dialog opens from it.
For a button, use a modal placement around a plain link to your results page:
<div data-search-placement data-search-open="modal">
<a href="/search">Search</a>
</div>
For a search field, use an inline placement around a real form. Typing opens the dialog with the text:
<div data-search-placement data-search-open="inline">
<form role="search" method="get" action="/search">
<label for="site-q">Search</label>
<input id="site-q" type="search" name="q">
<button>Search</button>
</form>
</div>
- Replace the theme’s own search button or popup with the placement, so the page has one search.
- The inline field’s
namemust match the Search input name setting (qby default). - Do not put
data-search-triggeron the modal link. - Both kinds work with no JavaScript: the link and the form go to the results page.
- Placements work with takeover on or off.
For every placement option, see Embed on any server.
Check your theme
It takes about a minute.
- Open the Search admin page and turn on Take over the theme’s search form. Save.
- Open a public page of your site in a new tab.
- Click the theme’s search box, or its search button.
- See which search opens.
- Our dialog opens: takeover works. You are done.
- The theme’s own search opens: check that Search input name matches the field’s
name, and Results page URL matches the form’saction. If the theme’s search is not a form, add a placement. - Nothing opens at all: press Cmd+K (Ctrl+K). If our dialog does not open either, the plugin’s tags may be missing from your layout. See Install on EmDash and Troubleshooting.
EmDash’s own search
EmDash has a search of its own: search() from emdash, a full text search in the database. Keep it. It is the backup for visitors without JavaScript.
On your results page, the theme’s form and its search() results go inside <SearchResults>. They show when our script does not run, and stop showing when our results page draws, so the page never shows two search fields. See The results page.