https://github.com/uid11/auto-set-focus
Browser script for auto-setting focus on some element on page.
https://github.com/uid11/auto-set-focus
auto focus
Last synced: about 2 months ago
JSON representation
Browser script for auto-setting focus on some element on page.
- Host: GitHub
- URL: https://github.com/uid11/auto-set-focus
- Owner: uid11
- License: mit
- Created: 2021-01-18T07:02:36.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2021-01-26T10:24:07.000Z (over 5 years ago)
- Last Synced: 2025-02-23T02:20:04.578Z (over 1 year ago)
- Topics: auto, focus
- Language: JavaScript
- Homepage:
- Size: 8.79 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# auto-set-focus
[![Contributor Covenant][contributor-covenant-image]](CODE_OF_CONDUCT.md)
[![code style: prettier][prettier-image]](https://github.com/prettier/prettier)
[![Conventional Commits][conventional-commits-image]](https://conventionalcommits.org)
[![No dependencies][dependencies-none-image]](package.json)
[![License MIT][license-image]](LICENSE)
Browser script for auto-setting focus on some element on page.
The index file exports the `autoSetFocus` function as a default value.
When called, the `autoSetFocus(element)` function adds a `keydown` event handler
and a `visibilitychange` event handler.
Then, each time a key is pressed on the page, this `keydown` handler checks
which element on the page has focus.
If the focus is on some control for entering text, the handler does nothing.
Otherwise (when neither text input element has focus),
the handler clears the text in the element field, and places the focus on element.
The `visibilitychange` handler place the focus on element, when user return on
the page from other windows or tabs.
Finally, `autoSetFocus(element)` returns a `dispose` function that, when called,
removes the `keypdown` and `visibilitychange` handlers:
```js
const dispose = autoSetFocus(someInputElement);
// ...
// later, when you need to dispose auto-set-focus
dispose();
```
## License
[MIT](LICENSE)
[contributor-covenant-image]: https://img.shields.io/badge/Contributor%20Covenant-v2.0%20adopted-ff69b4.svg 'Contributor Covenant'
[conventional-commits-image]: https://img.shields.io/badge/Conventional%20Commits-1.0.0-yellow.svg 'Conventional Commits'
[dependencies-none-image]: https://img.shields.io/badge/dependencies-none-brightgreen 'No dependencies'
[license-image]: https://img.shields.io/badge/license-MIT-blue.svg 'The MIT License'
[prettier-image]: https://img.shields.io/badge/code_style-prettier-ff69b4.svg 'Prettier code style'