项目作者: undecaf

项目描述 :
A smart Vue autofocus directive
高级语言: JavaScript
项目地址: git://github.com/undecaf/vue-autofocus.git
创建时间: 2020-08-20T20:52:00Z
项目社区:https://github.com/undecaf/vue-autofocus

开源协议:MIT License

下载


``` Focusing on the first focusable descendant that matches a selector: ```html
``` Auto-focusing on the input inside a [Vue Material Datepicker](https://vuematerial.io/components/datepicker): ```html ``` Setting the focus on the first input of a [Vue Material Dialog](https://vuematerial.io/components/dialog) whenever the dialog is (re-)opened (a selector is required since the dialog container is focusable): ```html ... ``` This will have no effect whatsoever: ```html
``` ## License Software: [MIT](http://opensource.org/licenses/MIT) Documentation: [CC-BY-SA 4.0](http://creativecommons.org/licenses/by-sa/4.0/)

A smart Vue autofocus directive

@undecaf/vue-autofocus" alt="Minified size">
Open issues
@undecaf/vue-autofocus/badge.svg" alt="Vulnerabilities">
@undecaf/vue-autofocus" alt="Total downloads">
License

This directive, v-autofocus, tries to be smart in the following ways:

A simple online example is available here
(example source code).

Please note: in this context, an element is considered “focusable” if it can become the
document.activeElement.
This includes contenteditable elements.

Focusable elements become non-focusable only if hidden or having attribute disabled.
Elements with any integer tabindex are at least click focusable.

Installation

As a module:

```shell script
$ npm install @undecaf/vue-autofocus
or
$ yarn add @undecaf/vue-autofocus

  1. Included as `<script>`:
  2. ```html
  3. <script src="https://cdn.jsdelivr.net/npm/@undecaf/vue-autofocus/dist/directives.min.js"></script>

Usage

Registering the directive

```javascript 1.8
import autofocus from ‘vue-autofocus’

Vue.use(autofocus)

  1. ### Configuration
  2. `v-autofocus` expects a configuration object, a primitive value as a single option (see below), or no
  3. value at all. Unspecified options get default values.
  4. The configuration object supports the following properties:
  5. | Name | Type | Description | Default |
  6. |------|------|-------------|---------|
  7. | `enabled` | `Boolean` | Enables the directive if truthy. | `true` |
  8. | `selector` | `String` | Only an element matching this selector can receive the focus, starting with the element on which this directive is placed. | `'*'` |
  9. | `on` | `String` or `Array<String>` | Child event(s) that re-trigger auto-focusing. | `[]` |
  10. | `delay` | `Number` | Delay (in ms) until the focus is set.<br>A value of `0` sets the focus synchronously with the trigger event. | `50` |
  11. If a value is specified that is not an object then its type determines which option it applies to:
  12. `Boolean` `enabled`, `String` `selector`, `Array` `on`, `Number` `delay`.
  13. The configuration can be modified after binding; changes to `on` take effect immediately, all other changes become noticeable only after a child event
  14. (e.g. [`'hook:updated'`](https://twitter.com/DamianDulisz/status/981549658571968512) or
  15. [`'md-opened'`](https://vuematerial.io/components/dialog)).
  16. ### Examples
  17. A simple use case:
  18. ```html
  19. <input type="text" v-autofocus>

Conditional autofocus:

  1. <input type="text" v-autofocus="{ enabled: active }"> <!-- or: autofocus="Boolean(active)" -->

Focusing on the first focusable descendant:

```html



Not focusable