Skip to content

Commit

Permalink
Improve package experience
Browse files Browse the repository at this point in the history
  • Loading branch information
niksy committed Mar 12, 2019
1 parent 425dbae commit 8849fca
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 56 deletions.
4 changes: 3 additions & 1 deletion .babelrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{
"presets": [["env", { "modules": false }]]
"presets": [
["@babel/preset-env", { "modules": false }]
]
}
5 changes: 4 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
"extends": [
"eslint:recommended"
],
"parserOptions": {
"sourceType": "module"
},
"env": {
"browser": true,
"node": true,
"es6": true
}
}
}
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules/
/dist/
.DS_Store
.DS_Store
package-lock.json
55 changes: 33 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,65 +1,76 @@
# dom-focus-lock

It is a trap! We got your focus and will not let him out!

[![NPM](https://nodei.co/npm/dom-focus-lock.png?downloads=true&stars=true)](https://nodei.co/npm/dom-focus-lock/)

This is a small, but very useful for:

- Modal dialogs. You can not leave it with "Tab", ie tab-out.
- Focused tasks. It will aways brings you back.

You have to use it in _every_ modal dialog, or you `a11y` will be shitty.
You have to use it in _every_ modal dialog, or you accessibility will be shitty.

# How to use

Just include script

```html
<script src="http://unpkg.com/dom-focus-lock"></script>
```
Or require it from js
Or require it from JS

```js
import focusLock from 'dom-focus-lock'
import focusLock from 'dom-focus-lock'
```
Then - activate the lock on desired node
Then, activate the lock on desired node

```js
focusLock.on(domNode);
//.....
focusLock.off(domNode);
```

#WHY?
# Why?

From [MDN Article about accessible dialogs](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/ARIA_Techniques/Using_the_dialog_role):

- The dialog must be properly labeled
- Keyboard __focus must be managed__ correctly

This one is about managing the focus.

I'v got a good [article about focus management, dialogs and WAI-ARIA](https://medium.com/@antonkorzunov/its-a-focus-trap-699a04d66fb5).


# Behavior
0. It will always keep focus inside Lock.
1. It will cycle forward then you press Tab.
2. It will cycle in reverse direction on Shift+Tab.
3. It will do it using _browser_ tools, not emulation.
4. It will handle positive tabIndex inside form.
5. It will prevent any jump outside, returning focus to the last element.

0. It will always keep focus inside Lock.
1. It will cycle forward then you press Tab.
2. It will cycle in reverse direction on Shift+Tab.
3. It will do it using _browser_ tools, not emulation.
4. It will handle positive tabIndex inside form.
5. It will prevent any jump outside, returning focus to the last element.

You can use nested Locks or have more than one Lock on the page.
Only `last`, or `deepest` one will work. No fighting.

# API
FocusLock has only 3 props, 2 of them you will never use(I hope):
- `disabled`, to disable(enable) behavior without altering the tree.

FocusLock has only 3 props, 2 of them you will never use (I hope):

- `disabled`, to disable(enable) behavior without altering the tree.

# How it works
Everything thing is simple - vue-focus-lock just dont left focus left boundaries of component, and
do something only if escape attempt was succeeded.

Everything is simple - dom-focus-lock just dont left focus left boundaries of component, and
do something only if escape attempt was succeeded.

It is not altering tabbing behavior at all. We are good citizens.
It is not altering tabbing behavior at all. We are good citizens.

# Not using a vanilla js?
try [react-focus-lock](https://github.com/theKashey/react-focus-lock) or [vue-focus-lock](https://github.com/theKashey/vue-focus-lock)
# Not using a vanilla JS?

Try [react-focus-lock](https://github.com/theKashey/react-focus-lock) or [vue-focus-lock](https://github.com/theKashey/vue-focus-lock)

# Licence
MIT



MIT
58 changes: 27 additions & 31 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,40 @@
"name": "dom-focus-lock",
"version": "1.0.3",
"description": "It is a trap! (for a focus)",
"author": "theKashey <[email protected]>",
"license": "MIT",
"main": "dist/index.js",
"module": "dist/index.esm.js",
"unpkg": "umd/index.js",
"scripts": {
"build": "rollup --config rollup.config.js",
"prepublish": "npm run build",
"prepublishOnly": "npm run build",
"lint": "eslint src tests",
"lint:fix": "eslint src tests --fix"
},
"repository": {
"type": "git",
"url": "git+https://github.com/theKashey/dom-focus-lock.git"
"lint:fix": "eslint src tests --fix",
"test": "ws --static.index example/testForm.html --open"
},
"files": [
"src",
"dist",
"umd"
],
"dependencies": {
"focus-lock": "^0.5.3"
},
"devDependencies": {
"@babel/core": "^7.3.4",
"@babel/preset-env": "^7.3.4",
"eslint": "^4.6.0",
"local-web-server": "^2.6.1",
"rollup": "^1.5.0",
"rollup-plugin-babel": "^4.3.2",
"rollup-plugin-commonjs": "^9.2.1",
"rollup-plugin-node-globals": "^1.4.0",
"rollup-plugin-node-resolve": "^4.0.1"
},
"browserslist": [
"last 2 versions"
],
"keywords": [
"vanilla",
"dom",
Expand All @@ -29,32 +45,12 @@
"trap",
"tabbable"
],
"author": "theKashey <[email protected]>",
"license": "ISC",
"repository": {
"type": "git",
"url": "git+https://github.com/theKashey/dom-focus-lock.git"
},
"bugs": {
"url": "https://github.com/theKashey/dom-focus-lock/issues"
},
"homepage": "https://github.com/theKashey/dom-focus-lock#readme",
"devDependencies": {
"babel-cli": "6",
"babel-core": "^6.24.0",
"babel-eslint": "7.2.3",
"babel-loader": "^6.4.1",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-preset-env": "1.6.0",
"babel-preset-es2015": "^6.24.0",
"babel-preset-stage-2": "^6.22.0",
"babel-runtime": "^6.23.0",
"eslint": "^4.6.0",
"eslint-plugin-vue": "^3.12.0",
"rollup": "^1.5.0",
"rollup-plugin-babel": "^3.0.7",
"rollup-plugin-commonjs": "^9.2.1",
"rollup-plugin-node-globals": "^1.4.0",
"rollup-plugin-node-resolve": "^4.0.1",
"style-loader": "^0.15.0"
},
"dependencies": {
"focus-lock": "^0.5.3"
}
"homepage": "https://github.com/theKashey/dom-focus-lock#readme"
}
8 changes: 8 additions & 0 deletions umd/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -592,15 +592,19 @@

var activateTrap = function activateTrap() {
var result = false;

if (lastActiveTrap) {
var observed = lastActiveTrap;

if (!isFreeFocus()) {
if (observed && !focusInside(observed)) {
result = setFocus(observed, lastActiveFocus);
}

lastActiveFocus = document.activeElement;
}
}

return result;
};

Expand All @@ -612,6 +616,7 @@

var handleStateChangeOnClient = function handleStateChangeOnClient(trap) {
lastActiveTrap = trap;

if (trap) {
activateTrap();
}
Expand All @@ -624,6 +629,7 @@
event && event.preventDefault();
return true;
}

return false;
};

Expand All @@ -640,6 +646,7 @@
if (instances.length === 0) {
attachHandler();
}

if (instances.indexOf(domNode) < 0) {
instances.push(domNode);
emitChange();
Expand All @@ -650,6 +657,7 @@
return node !== domNode;
});
emitChange();

if (instances.length === 0) {
detachHandler();
}
Expand Down

0 comments on commit 8849fca

Please sign in to comment.