Skip to content

Commit

Permalink
Merge pull request #514 from vanbroup/autofocus
Browse files Browse the repository at this point in the history
Add a configurable autofocus option
  • Loading branch information
bglw authored Nov 28, 2023
2 parents 971186c + 5338f93 commit e033147
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
13 changes: 13 additions & 0 deletions docs/content/docs/ui.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,16 @@ new PagefindUI({
A set of custom ui strings to use instead of the automatically detected language strings. See the [translations/en.json](https://github.com/CloudCannon/pagefind/blob/main/pagefind_ui/translations/en.json) file for all available keys and their initial values.

The items in square brackets such as `SEARCH_TERM` will be substituted dynamically when the text is used.

### Autofocus

{{< diffcode >}}
```javascript
new PagefindUI({
element: "#search",
+ autofocus: true
});
```
{{< /diffcode >}}

Enabling autofocus automatically directs attention to the search input field for enhanced user convenience, particularly beneficial when the UI is loaded within a modal dialog. However, exercise caution, as using autofocus indiscriminately may pose potential accessibility challenges.
2 changes: 2 additions & 0 deletions pagefind_ui/default/svelte/ui.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
export let merge_index = [];
export let trigger_search_term = "";
export let translations = {};
export let autofocus = false;
let val = "";
$: if (trigger_search_term) {
Expand Down Expand Up @@ -239,6 +240,7 @@
)}
autocapitalize="none"
enterkeyhint="search"
{autofocus}
/>
<button
Expand Down
3 changes: 3 additions & 0 deletions pagefind_ui/default/ui-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export class PagefindUI {
let debounceTimeoutMs = opts.debounceTimeoutMs ?? 300;
let mergeIndex = opts.mergeIndex ?? [];
let translations = opts.translations ?? [];
let autofocus = opts.autofocus ?? false;

// Remove the UI-specific config before passing it along to the Pagefind backend
delete opts["element"];
Expand All @@ -41,6 +42,7 @@ export class PagefindUI {
delete opts["debounceTimeoutMs"];
delete opts["mergeIndex"];
delete opts["translations"];
delete opts["autofocus"];

const dom =
selector instanceof HTMLElement
Expand All @@ -62,6 +64,7 @@ export class PagefindUI {
debounce_timeout_ms: debounceTimeoutMs,
merge_index: mergeIndex,
translations,
autofocus,
pagefind_options: opts,
},
});
Expand Down

0 comments on commit e033147

Please sign in to comment.