Skip to content

Commit

Permalink
Use Rollup for building project
Browse files Browse the repository at this point in the history
Closes #3.
  • Loading branch information
niksy committed Mar 7, 2019
1 parent 24c7f28 commit 03d4fcf
Show file tree
Hide file tree
Showing 5 changed files with 673 additions and 775 deletions.
4 changes: 2 additions & 2 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"presets": ["env"]
}
"presets": [["env", { "modules": false }]]
}
11 changes: 7 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
"version": "1.0.3",
"description": "It is a trap! (for a focus)",
"main": "dist/index.js",
"module": "dist/index.esm.js",
"unpkg": "umd/index.js",
"scripts": {
"build": "npm run babel && npm run umd",
"babel": "babel src -d dist",
"umd": "browserify dist/index.js --standalone focusLock -o umd/index.js",
"build": "rollup --config rollup.config.js",
"prepublish": "npm run build",
"lint": "eslint src tests",
"lint:fix": "eslint src tests --fix"
Expand Down Expand Up @@ -46,9 +45,13 @@
"babel-preset-es2015": "^6.24.0",
"babel-preset-stage-2": "^6.22.0",
"babel-runtime": "^6.23.0",
"browserify": "14.4.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": {
Expand Down
45 changes: 45 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
'use strict';

const babel = require('rollup-plugin-babel');
const resolve = require('rollup-plugin-node-resolve');
const commonjs = require('rollup-plugin-commonjs');
const globals = require('rollup-plugin-node-globals');

module.exports = [{
input: 'src/index.js',
output: [
{
file: 'dist/index.js',
format: 'cjs'
},
{
file: 'dist/index.esm.js',
format: 'esm'
}
],
plugins: [
babel({
exclude: 'node_modules/**'
})
],
external: [
'focus-lock'
]
}, {
input: 'src/index.js',
output: [
{
file: 'umd/index.js',
format: 'umd',
name: 'focusLock'
}
],
plugins: [
babel({
exclude: 'node_modules/**'
}),
resolve(),
commonjs(),
globals()
]
}];
124 changes: 61 additions & 63 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,81 +1,79 @@
(function () {
const {default: moveFocusInside, focusInside, focusIsHidden} = require('focus-lock');
import moveFocusInside, {focusInside, focusIsHidden} from 'focus-lock';

let lastActiveTrap = 0;
let lastActiveFocus = null;
let lastActiveTrap = 0;
let lastActiveFocus = null;


const focusOnBody = () => (
document && document.activeElement === document.body
);
const focusOnBody = () => (
document && document.activeElement === document.body
);

const isFreeFocus = () => focusOnBody() || focusIsHidden();
const isFreeFocus = () => focusOnBody() || focusIsHidden();


const activateTrap = () => {
let result = false;
if (lastActiveTrap) {
const observed = lastActiveTrap;
if(!isFreeFocus()) {
if (observed && !focusInside(observed)) {
result = moveFocusInside(observed, lastActiveFocus);
}
lastActiveFocus = document.activeElement;
const activateTrap = () => {
let result = false;
if (lastActiveTrap) {
const observed = lastActiveTrap;
if(!isFreeFocus()) {
if (observed && !focusInside(observed)) {
result = moveFocusInside(observed, lastActiveFocus);
}
lastActiveFocus = document.activeElement;
}
return result;
};
}
return result;
};

const reducePropsToState = (propsList) => {
return propsList
.filter(node => node)
.slice(-1)[0];
};
const reducePropsToState = (propsList) => {
return propsList
.filter(node => node)
.slice(-1)[0];
};

const handleStateChangeOnClient = (trap) => {
lastActiveTrap = trap;
if (trap) {
activateTrap();
}
};

let instances = [];
const handleStateChangeOnClient = (trap) => {
lastActiveTrap = trap;
if (trap) {
activateTrap();
}
};

const emitChange = (event) => {
if (handleStateChangeOnClient(reducePropsToState(instances))) {
event && event.preventDefault();
return true;
}
return false;
};
let instances = [];

const attachHandler = () => {
document.addEventListener('focusin', emitChange);
};
const emitChange = (event) => {
if (handleStateChangeOnClient(reducePropsToState(instances))) {
event && event.preventDefault();
return true;
}
return false;
};

const detachHandler = () => {
document.removeEventListener('focusin', emitChange);
};
const attachHandler = () => {
document.addEventListener('focusin', emitChange);
};

const focusLock = {
on(domNode){
if (instances.length === 0) {
attachHandler();
}
if (instances.indexOf(domNode) < 0) {
instances.push(domNode);
emitChange();
}
},
const detachHandler = () => {
document.removeEventListener('focusin', emitChange);
};

off(domNode){
instances = instances.filter(node => node !== domNode);
const focusLock = {
on(domNode){
if (instances.length === 0) {
attachHandler();
}
if (instances.indexOf(domNode) < 0) {
instances.push(domNode);
emitChange();
if (instances.length === 0) {
detachHandler();
}
}
};
},

off(domNode){
instances = instances.filter(node => node !== domNode);
emitChange();
if (instances.length === 0) {
detachHandler();
}
}
};

module.exports = focusLock;
})();
export default focusLock;
Loading

0 comments on commit 03d4fcf

Please sign in to comment.