{"id":16791938,"url":"https://github.com/metonym/svelte-focus-key","last_synced_at":"2025-03-23T15:31:19.032Z","repository":{"id":37008847,"uuid":"415678521","full_name":"metonym/svelte-focus-key","owner":"metonym","description":"Svelte component and action to focus an element when pressing a key","archived":false,"fork":false,"pushed_at":"2023-06-24T19:01:23.000Z","size":620,"stargazers_count":15,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-18T12:04:38.640Z","etag":null,"topics":["focus-input","keydown","slash-command","svelte","svelte-action","svelte-component","typescript"],"latest_commit_sha":null,"homepage":"https://metonym.github.io/svelte-focus-key","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/metonym.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-10-10T19:16:33.000Z","updated_at":"2024-07-04T21:23:14.000Z","dependencies_parsed_at":"2024-10-28T16:37:32.756Z","dependency_job_id":"df239a0e-b4c8-466f-9d66-99187142a9d2","html_url":"https://github.com/metonym/svelte-focus-key","commit_stats":null,"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/metonym%2Fsvelte-focus-key","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/metonym%2Fsvelte-focus-key/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/metonym%2Fsvelte-focus-key/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/metonym%2Fsvelte-focus-key/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/metonym","download_url":"https://codeload.github.com/metonym/svelte-focus-key/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245122759,"owners_count":20564367,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["focus-input","keydown","slash-command","svelte","svelte-action","svelte-component","typescript"],"created_at":"2024-10-13T08:43:35.237Z","updated_at":"2025-03-23T15:31:18.707Z","avatar_url":"https://github.com/metonym.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# svelte-focus-key\n\n[![NPM][npm]][npm-url]\n\n\u003e Svelte component and action to focus an element when pressing a key\n\nThe primary use case is to focus a search input when pressing the forward slash key (\"/\").\n\n## Installation\n\n```bash\n# Yarn\nyarn add -D svelte-focus-key\n\n# NPM\nnpm i -D svelte-focus-key\n\n# pnpm\npnpm i -D svelte-focus-key\n```\n\n## Usage\n\n### `FocusKey` component\n\nUse the [bind:this](https://svelte.dev/docs#bind_element) directive to pass the element to focus to the `FocusKey` component.\n\n\u003c!-- example-start demo/Basic.svelte --\u003e\n\n```svelte\n\u003cscript\u003e\n  import FocusKey from \"svelte-focus-key\";\n\n  let element;\n\u003c/script\u003e\n\n\u003cinput bind:this={element} placeholder={'Press \"/\" to focus'} /\u003e\n\n\u003cFocusKey {element} /\u003e\n```\n\n\u003c!-- example-end --\u003e\n\n### Custom focus key\n\nThe default focus key is the forward slash (`/`). Customize the key using the `key` prop.\n\n\u003c!-- example-start demo/CustomFocusKey.svelte --\u003e\n\n```svelte\n\u003cscript\u003e\n  import FocusKey from \"svelte-focus-key\";\n\n  let textarea;\n\u003c/script\u003e\n\n\u003ctextarea bind:this={textarea} placeholder={'Press \"s\" to focus'} /\u003e\n\n\u003cFocusKey element={textarea} key=\"s\" /\u003e\n```\n\n\u003c!-- example-end --\u003e\n\n### Multiple focus keys\n\nThe `key` prop can also accept an array of keys.\n\n\u003c!-- example-start demo/MultipleFocusKeys.svelte --\u003e\n\n```svelte\n\u003cscript\u003e\n  import FocusKey from \"svelte-focus-key\";\n\n  let node;\n\u003c/script\u003e\n\n\u003cinput bind:this={node} placeholder={'Press \"a\" or \"b\"'} /\u003e\n\n\u003cFocusKey element={node} key={[\"a\", \"b\"]} /\u003e\n```\n\n\u003c!-- example-end --\u003e\n\n### Combination of keys\n\nA combination of keys should be separated by a `+`.\n\n\u003c!-- example-start demo/ComboKeys.svelte --\u003e\n\n```svelte\n\u003cscript\u003e\n  import FocusKey from \"svelte-focus-key\";\n\n  let element;\n\u003c/script\u003e\n\n\u003cinput bind:this={element} placeholder={'Press \"⌘+k\" to focus'} /\u003e\n\n\u003cFocusKey {element} key=\"Meta+k\" /\u003e\n```\n\n\u003c!-- example-end --\u003e\n\n### Select text on focus\n\nSet `selectText` to `true` to select the text in the element when focusing.\n\n\u003c!-- example-start demo/SelectTextOnFocus.svelte --\u003e\n\n```svelte\n\u003cscript\u003e\n  import FocusKey from \"svelte-focus-key\";\n\n  let input;\n\u003c/script\u003e\n\n\u003cinput\n  bind:this={input}\n  value={'Press \"e\" to focus'}\n  placeholder={'Press \"e\" to focus'}\n/\u003e\n\n\u003cFocusKey element={input} key=\"e\" selectText /\u003e\n```\n\n\u003c!-- example-end --\u003e\n\n### `focusKey` action\n\nThis utility also provides a [Svelte action](https://svelte.dev/docs#use_action).\n\n\u003c!-- example-start demo/FocusKeyAction.svelte --\u003e\n\n```svelte\n\u003cscript\u003e\n  import { focusKey } from \"svelte-focus-key\";\n\u003c/script\u003e\n\n\u003cinput use:focusKey={{ key: \"q\" }} placeholder={'Press \"q\" to focus'} /\u003e\n```\n\n\u003c!-- example-end --\u003e\n\n## API\n\n### Props\n\n| Name       | Description                       | Type                   | Default value |\n| :--------- | :-------------------------------- | :--------------------- | :------------ |\n| element    | HTML element to focus             | `HTMLElement`          | `null`        |\n| key        | Key to trigger focus when pressed | `string` or `string[]` | `\"/\"`         |\n| selectText | Select element text when focusing | `boolean`              | `false`       |\n\nThe `focusKey` action has the same props as `FocusKey` except for `element`.\n\n[npm]: https://img.shields.io/npm/v/svelte-focus-key.svg?style=for-the-badge\u0026color=%23ff3e00\n[npm-url]: https://npmjs.com/package/svelte-focus-key\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmetonym%2Fsvelte-focus-key","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmetonym%2Fsvelte-focus-key","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmetonym%2Fsvelte-focus-key/lists"}