Skip to content

Commit

Permalink
Rework for Vue 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
simplesmiler committed Sep 22, 2016
1 parent 2cada99 commit 093e796
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 173 deletions.
50 changes: 4 additions & 46 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,22 @@ Well not anymore. `vue-focus` lets you manage focus from the safety of your view

## Requirements

- vue: ^1.0.0
- vue: ^2.0.0

## Install

From npm:

``` sh
$ npm install vue-focus@^1.0 --save
$ npm install vue-focus --save
```

From CDN:

``` html
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue-focus/1.0.0/vue-focus.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue-focus/2.0.0/vue-focus.js"></script>
<!-- OR -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue-focus/1.0.0/vue-focus.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue-focus/2.0.0/vue-focus.min.js"></script>
```

## API
Expand All @@ -59,47 +59,6 @@ export default {
};
```

### `focus-model`

> NOTE: this directive is deprecated. Use `v-focus="expression" @focus="expression = true" @blur="expression = false"` instead.
A directive that binds focus to the expression in a two-way manner, so that the element recieves focus when you change the bound value to `true` and loses focus when you change the bound value to `false`, **and vice versa**, sets the bound value to `true` when element recieves focus and sets the bound value to `false` when element loses focus. It takes a single argument: an expression to be bound.

> NOTE: This is a two-way bound directive. If you need a one-way bound version, please refer to [focus](#focus).
> NOTE: The expression has to be settable, and has to evaluate to a boolean value.
``` js
import { focusModel } from 'vue-focus';

export default {
directives: { focusModel: focusModel },
template: '<input type="text" v-focus-model="focused">',
data: function() {
return {
focused: false,
};
},
};
```

### `focus-auto`

> NOTE: this directive is deprecated. Use `v-focus="true"` instead.
A directive that makes the element gain focus whenever it enters the DOM (either via initial costruction or by the means of `:is`, `v-if` or `v-for`). It takes no arguments.

> NOTE: This is a reactive replacement for the native html attribute `autofocus`, which does not work after page has finished loading.
``` js
import { focusAuto } from 'vue-focus';

export default {
directives: { focusAuto: focusAuto },
template: '<input type="text" v-focus-auto>',
};
```

### `mixin`

A mixin that makes the `v-focus` directive available to the component under the default name.
Expand All @@ -121,7 +80,6 @@ export default {
## Caveats

1. Although you can write an input that gains focus immediately after loosing it, this is not recommended, as two such inputs will fall into infinite recursion and freeze the browser.
2. Prior to `[email protected]` views were able to inherit assets from the parent views, which made it possible to define the directive on the root view and have it available across the whole view hierarchy. Since `[email protected]` this is not possible. If you still want to define the directive application-wide, you should `Vue.directive('<directive-name>', <directive>);` in your application entry point for every directive you need. But bear in mind that this introduces implicit dependency for your components, making them less reusable.

## Notes

Expand Down
14 changes: 7 additions & 7 deletions examples/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<meta charset="UTF-8">
<title>Examples for vue-focus</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/1.0.26/vue.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.0.0-rc.6/vue.js"></script>
<script src="../dist/vue-focus.js"></script>
</head>
<body>
Expand All @@ -19,7 +19,7 @@ <h3><code>Example 1: Autofocus</code></h3>

<h3><code>Example 2: Show a hint for the focused field</code></h3>
<div id="example2">
<div><pre>{{ $data | json }}</pre></div>
<div><pre>{{ $data }}</pre></div>
<p>
<input type="text" v-focus="focused" @focus="focused = true" @blur="focused = false">
<span v-if="focused">Focused!</span>
Expand All @@ -30,14 +30,14 @@ <h3><code>Example 2: Show a hint for the focused field</code></h3>

<h3><code>Example 3: Move focus</code></h3>
<div id="example3">
<div><pre>{{ { focused: focused } | json }}</pre></div>
<p v-for="item in items">
<div><pre>{{ { focused: focused } }}</pre></div>
<p v-for="(item, index) in items">
<input type="text"
v-model="item.value"
@keydown.down.prevent="moveDown"
@keydown.up.prevent="moveUp"
v-focus="$index === focused"
@focus="focused = $index"
v-focus="index === focused"
@focus="focused = index"
@blur="focused = null"
>
</p>
Expand All @@ -48,7 +48,7 @@ <h3><code>Example 3: Move focus</code></h3>

<h3><code>Example 4: Custom label</code></h3>
<div id="example4">
<div><pre>{{ $data | json }}</pre></div>
<div><pre>{{ $data }}</pre></div>
<p>
<span role="label" @mousedown.prevent="focused = true">Label</span>
<input type="text"
Expand Down
126 changes: 6 additions & 120 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,131 +1,17 @@
import Vue from 'vue';
import { util } from 'vue';

// @NOTE: We have to use Vue.nextTick because the element might not be
// present at the time model changes, but will be in the next batch.
// But because we use Vue.nextTick, the directive may already be unbound
// by the time the callback executes, so we have to make sure it was not.

export var focus = {
priority: 1000,

bind: function() {
var self = this;
this.bound = true;

this.focus = function() {
if (self.bound === true) {
self.el.focus();
}
};

this.blur = function() {
if (self.bound === true) {
self.el.blur();
}
};
},

update: function(value) {
if (value) {
Vue.nextTick(this.focus);
} else {
Vue.nextTick(this.blur);
}
inserted: function(el, binding) {
if (binding.value) el.focus();
else el.blur();
},

unbind: function() {
this.bound = false;
},
};

export var focusModel = {
twoWay: true,
priority: 1000,

bind: function() {
util.warn(
this.name + '="' +
this.expression + '" is deprecated, ' +
'use v-focus="' + this.expression + '" @focus="' + this.expression + ' = true" @blur="' + this.expression + ' = false" instead'
);

var self = this;
this.bound = true;

this.focus = function() {
if (self.bound === true) {
self.el.focus();
}
};

this.blur = function() {
if (self.bound === true) {
self.el.blur();
}
};

this.focusHandler = function() {
self.set(true);
};

this.blurHandler = function() {
self.set(false);
};

util.on(this.el, 'focus', this.focusHandler);
util.on(this.el, 'blur', this.blurHandler);
},

update: function(value) {
if (value === true) {
Vue.nextTick(this.focus);
} else if (value === false) {
Vue.nextTick(this.blur);
} else {
if (process.env.NODE_ENV !== 'production') {
util.warn(
this.name + '="' +
this.expression + '" expects a boolean value, ' +
'got ' + JSON.stringify(value)
);
}
}
},

unbind: function() {
util.off(this.el, 'focus', this.focusHandler);
util.off(this.el, 'blur', this.blurHandler);
this.bound = false;
},
};

export var focusAuto = {
priority: 100,
bind: function() {
util.warn(
this.name + ' is deprecated, ' +
'use v-focus="true" instead'
);

var self = this;
this.bound = true;

Vue.nextTick(function() {
if (self.bound === true) {
self.el.focus();
}
});
},
unbind: function(){
this.bound = false;
componentUpdated: function(el, binding) {
if (binding.value) el.focus();
else el.blur();
},
};

export var mixin = {
directives: {
focus: focus,
focusModel: focusModel,
focusAuto: focusAuto,
},
};

0 comments on commit 093e796

Please sign in to comment.