Comparing version 0.2.0 to 0.3.0
/*! | ||
* ctrly v0.2.0 | ||
* ctrly v0.3.0 | ||
* Copyright (c) 2018 Jan Sorgalla | ||
@@ -296,3 +296,4 @@ * License: MIT | ||
allowMultiple: false, | ||
on: null | ||
on: null, | ||
autoInit: true | ||
}; | ||
@@ -423,4 +424,4 @@ function settings(opts) { | ||
if (options.closeOnBlur && !options.trapFocus) { | ||
removeFuncs.push(on(target, 'focusout', function (e) { | ||
if (!e.relatedTarget || !target.contains(e.relatedTarget)) { | ||
removeFuncs.push(on(document, 'focusin', function (e) { | ||
if (!target.contains(e.target)) { | ||
close(target, false); | ||
@@ -594,3 +595,5 @@ } | ||
} | ||
init(); | ||
if (options.autoInit) { | ||
init(); | ||
} | ||
return { | ||
@@ -597,0 +600,0 @@ init: init, |
/*! | ||
* ctrly v0.2.0 | ||
* ctrly v0.3.0 | ||
* Copyright (c) 2018 Jan Sorgalla | ||
* License: MIT | ||
*/ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):e.ctrly=t()}(this,function(){"use strict";function f(){try{var e=document.activeElement;return e&&e.nodeName?e:document.body}catch(e){return document.body}}function r(e){for(var t=[];e&&e.parentNode&&1===e.parentNode.nodeType;)e=e.parentNode,t.push(e);return t}function p(e){var t,n;(1<arguments.length&&void 0!==arguments[1]?arguments[1]:{}).restoreScrollPosition&&(n=r(e).map(function(e){return[e,e.scrollTop,e.scrollLeft]}),t=function(){n.forEach(function(e){e[0].scrollTop=e[1],e[0].scrollLeft=e[2]})});try{e.focus()}catch(e){}t&&t()}function v(e,t){var n=1<arguments.length?t:document;return n&&"function"==typeof n.querySelectorAll?[].slice.call(n.querySelectorAll(e)):[]}function n(e,t){if(!e)return!1;var n=e.matches||e.webkitMatchesSelector||e.msMatchesSelector;return"function"==typeof n&&n.call(e,t)}function b(e,t){if(!e)return null;if("function"==typeof e.closest)return e.closest(t);do{if(n(e,t))return e;e=e.parentNode}while(e&&1===e.nodeType);return null}var t;function a(){if(t)return t;t={capture:!1,once:!1,passive:!1};var e={get capture(){return!(t.capture=!0)},get once(){return!(t.once=!0)},get passive(){return!(t.passive=!0)}};return window.addEventListener("test",e,e),window.removeEventListener("test",e,e),t}function h(){var e=0<arguments.length&&void 0!==arguments[0]?arguments[0]:{},t=a(),n=t.once,r=t.passive,o=t.capture;return n||r||o?(n||delete e.once,r||delete e.passive,o||delete e.capture,e):Boolean(e.capture)}function m(t,e,n){var r=3<arguments.length&&void 0!==arguments[3]?arguments[3]:{capture:!1};if(!t||"function"!=typeof t.addEventListener)return function(){};var o=n,i=function(){!function(e,t,n){var r=3<arguments.length&&void 0!==arguments[3]?arguments[3]:{capture:!1};e&&"function"==typeof e.removeEventListener&&e.removeEventListener(t,n,h(r))}(t,e,o,r)};return r.once&&!a().once&&(o=function(e){i(),n.call(t,e)}),t.addEventListener(e,o,h(r)),i}var o=["a[href]","area[href]","input","select","textarea","button","iframe","object","audio[controls]","video[controls]","[contenteditable]","[tabindex]"].join(","),i=/^(input|select|textarea|button|object)$/;function c(e){var t=e.nodeName.toLowerCase();if("area"===t)return function(e){var t=e.parentNode,n=t.name;if(!e.href||!n||"map"!==t.nodeName.toLowerCase())return!1;var r=v('img[usemap="#'.concat(n,'"]'));return 0<r.length&&s(r[0])}(e);if(e.disabled)return!1;if(i.test(t)){var n=b(e,"fieldset");if(n&&n.disabled)return!1}return s(e)}function u(e){var t=d(e);return c(e)&&0<=t}function l(e,t){var n=d(e,!0),r=d(t,!0);return n===r?2&e.compareDocumentPosition(t)?1:-1:n-r}function s(e){var t=getComputedStyle(e);return"hidden"!==t.visibility&&"collapse"!==t.visibility&&"none"!==t.display&&r(e).every(function(e){return"none"!==getComputedStyle(e).display})}function d(e){var t=1<arguments.length&&void 0!==arguments[1]&&arguments[1],n=parseInt(e.getAttribute("tabindex"),10);return isNaN(n)?0:t&&n<0?0:n}function y(e){return v(o,0<arguments.length?e:document).filter(u).sort(l)}var g={selector:"[data-ctrly]",context:null,focusTarget:!0,closeOnBlur:!0,closeOnEsc:!0,closeOnOutsideClick:!0,closeOnScroll:!1,trapFocus:!1,allowMultiple:!1,on:null};function A(e){return"which"in e?e.which:e.keyCode}function E(e){return v('[aria-controls="'.concat(e.id,'"]'))}function w(e){return document.getElementById(e.getAttribute("aria-controls")||e.getAttribute("data-ctrly"))}function x(e){e.removeAttribute("aria-controls"),e.removeAttribute("aria-expanded")}var O=0;return function(){var n,r,e=0<arguments.length&&void 0!==arguments[0]?arguments[0]:{},c=(n={},[g,e].forEach(function(e){for(var t in e)Object.prototype.hasOwnProperty.call(e,t)&&(n[t]=e[t])}),n),u=c.selector,o=c.on||{},l={};function s(e,t){return("function"!=typeof o[t]||!1!==o[t](e))&&!1!==function(e,t){var n,r=2<arguments.length&&void 0!==arguments[2]?arguments[2]:{};if(!e||"function"!=typeof e.dispatchEvent)return!0;r.bubbles=r.bubbles||!1,r.cancelable=r.cancelable||!1,r.composed=r.composed||!1,r.detail=r.detail||null;try{n=new CustomEvent(t,r)}catch(e){(n=document.createEvent("CustomEvent")).initCustomEvent(t,r.bubbles,r.cancelable,r.detail)}return e.dispatchEvent(n)}(e,"ctrly:".concat(t),{bubbles:!0,cancelable:!0})}function d(e){var t=!(1<arguments.length&&void 0!==arguments[1])||arguments[1];if(!e)return!1;if(!e.hasAttribute("data-ctrly-opened"))return!1;if(!s(e,"close"))return!1;var n=f(),r=l[e.id]||{},o=r.lastActiveElement,i=r.destroy;return delete l[e.id],i&&i(),E(e).forEach(function(e){e.setAttribute("aria-expanded","false")}),e.removeAttribute("data-ctrly-opened"),e.setAttribute("aria-hidden","true"),e.removeAttribute("tabindex"),e.blur(),t&&o&&e.contains(n)&&p(o,{restoreScrollPosition:!0}),s(e,"closed"),e}function i(n){var e;v(u,(e=n,c.context?b(e,c.context):document)).forEach(function(e){var t=w(e);t&&t.id!==n.id&&d(t,!1)})}function a(e){var i,t,n,r,o,a=w(e);return a?!a.hasAttribute("data-ctrly-opened")&&!!s(a,"open")&&(l[a.id]={lastActiveElement:f(),destroy:(i=a,t=[],n=!1,r=function(){n=!0},o=function(){n=!1},(c.closeOnOutsideClick||c.closeOnScroll)&&(t.push(m(i,"mouseenter",r,{passive:!0})),t.push(m(i,"mouseleave",o,{passive:!0})),t.push(m(i,"touchstart",r,{passive:!0})),t.push(m(i,"touchend",o,{passive:!0}))),c.closeOnBlur&&!c.trapFocus&&t.push(m(i,"focusout",function(e){e.relatedTarget&&i.contains(e.relatedTarget)||d(i,!1)},{capture:!0,passive:!0})),c.closeOnEsc&&t.push(m(document,"keydown",function(e){27===A(e)&&d(i)&&e.preventDefault()})),c.closeOnOutsideClick&&t.push(m(document,"click",function(e){n||1!==A(e)||b(e.target,u)||d(i)},{passive:!0})),c.closeOnScroll&&t.push(m(window,"scroll",function(){n||d(i)},{passive:!0})),c.trapFocus&&t.push(m(document,"keydown",function(e){if(9===A(e)){var t=y(i);if(!t[0])return e.preventDefault(),void p(i);var n=f(),r=t[0],o=t[t.length-1];if(e.shiftKey&&n===r)return e.preventDefault(),void p(o);e.shiftKey||n!==o||(p(r),e.preventDefault())}})),function(){for(;t.length;)t.shift().call()})},E(a).forEach(function(e){e.setAttribute("aria-expanded","true")}),a.setAttribute("data-ctrly-opened",""),a.setAttribute("aria-hidden","false"),a.setAttribute("tabindex","-1"),s(a,"opened"),a):(x(e),!1)}function t(){var e,t;r||(r=function(e,t,n,r){var o=4<arguments.length&&void 0!==arguments[4]?arguments[4]:{capture:!1},i=!0===o.once;delete o.once;var a=m(e,t,function(e){var t=b(e.target,n);t&&(i&&a(),r.call(t,e,t))},o);return a}(document,"click",u,function(e,t){if(1===A(e)){var n=w(t);n?"true"!==t.getAttribute("aria-expanded")?(c.allowMultiple||i(n),a(t),n&&(e.preventDefault(),c.focusTarget&&p(y(n)[0]||n),n.scrollTop=0,n.scrollLeft=0)):d(n)&&e.preventDefault():d(function(e){for(var t=e;t;){if(t.id&&l[t.id])return t;t=t.parentElement}}(t))&&e.preventDefault()}})),e=function(){v(u).forEach(function(e){var t=w(e);if(t){e.setAttribute("aria-controls",t.id);var n=E(t).map(function(e){return e.id||e.setAttribute("id","ctrly-control-"+ ++O),e.id}),r=(t.getAttribute("aria-labelledby")||"").split(" ").concat(n).filter(function(e,t,n){return""!==e&&n.indexOf(e)===t});t.setAttribute("aria-labelledby",r.join(" ")),"true"===e.getAttribute("aria-expanded")||e.hasAttribute("data-ctrly-open")?a(e):(e.setAttribute("aria-expanded","false"),t.setAttribute("aria-hidden","true"),t.removeAttribute("tabindex"))}else x(e)})},"complete"!==(t=document.readyState)&&"interactive"!==t?document.addEventListener("DOMContentLoaded",function(){e()},h({capture:!0,once:!0,passive:!0})):setTimeout(e,0)}return t(),{init:t,destroy:function(){for(var e in r&&(r(),r=null),v(u).forEach(function(e){var t=w(e);t&&d(t,!1)}),l)Object.prototype.hasOwnProperty.call(l,e)&&(l[e].destroy(),delete l[e])}}}}); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):e.ctrly=t()}(this,function(){"use strict";function f(){try{var e=document.activeElement;return e&&e.nodeName?e:document.body}catch(e){return document.body}}function r(e){for(var t=[];e&&e.parentNode&&1===e.parentNode.nodeType;)e=e.parentNode,t.push(e);return t}function p(e){var t,n;(1<arguments.length&&void 0!==arguments[1]?arguments[1]:{}).restoreScrollPosition&&(n=r(e).map(function(e){return[e,e.scrollTop,e.scrollLeft]}),t=function(){n.forEach(function(e){e[0].scrollTop=e[1],e[0].scrollLeft=e[2]})});try{e.focus()}catch(e){}t&&t()}function v(e,t){var n=1<arguments.length?t:document;return n&&"function"==typeof n.querySelectorAll?[].slice.call(n.querySelectorAll(e)):[]}function n(e,t){if(!e)return!1;var n=e.matches||e.webkitMatchesSelector||e.msMatchesSelector;return"function"==typeof n&&n.call(e,t)}function b(e,t){if(!e)return null;if("function"==typeof e.closest)return e.closest(t);do{if(n(e,t))return e;e=e.parentNode}while(e&&1===e.nodeType);return null}var t;function a(){if(t)return t;t={capture:!1,once:!1,passive:!1};var e={get capture(){return!(t.capture=!0)},get once(){return!(t.once=!0)},get passive(){return!(t.passive=!0)}};return window.addEventListener("test",e,e),window.removeEventListener("test",e,e),t}function h(){var e=0<arguments.length&&void 0!==arguments[0]?arguments[0]:{},t=a(),n=t.once,r=t.passive,o=t.capture;return n||r||o?(n||delete e.once,r||delete e.passive,o||delete e.capture,e):Boolean(e.capture)}function m(t,e,n){var r=3<arguments.length&&void 0!==arguments[3]?arguments[3]:{capture:!1};if(!t||"function"!=typeof t.addEventListener)return function(){};var o=n,i=function(){!function(e,t,n){var r=3<arguments.length&&void 0!==arguments[3]?arguments[3]:{capture:!1};e&&"function"==typeof e.removeEventListener&&e.removeEventListener(t,n,h(r))}(t,e,o,r)};return r.once&&!a().once&&(o=function(e){i(),n.call(t,e)}),t.addEventListener(e,o,h(r)),i}var o=["a[href]","area[href]","input","select","textarea","button","iframe","object","audio[controls]","video[controls]","[contenteditable]","[tabindex]"].join(","),i=/^(input|select|textarea|button|object)$/;function u(e){var t=e.nodeName.toLowerCase();if("area"===t)return function(e){var t=e.parentNode,n=t.name;if(!e.href||!n||"map"!==t.nodeName.toLowerCase())return!1;var r=v('img[usemap="#'.concat(n,'"]'));return 0<r.length&&s(r[0])}(e);if(e.disabled)return!1;if(i.test(t)){var n=b(e,"fieldset");if(n&&n.disabled)return!1}return s(e)}function c(e){var t=d(e);return u(e)&&0<=t}function l(e,t){var n=d(e,!0),r=d(t,!0);return n===r?2&e.compareDocumentPosition(t)?1:-1:n-r}function s(e){var t=getComputedStyle(e);return"hidden"!==t.visibility&&"collapse"!==t.visibility&&"none"!==t.display&&r(e).every(function(e){return"none"!==getComputedStyle(e).display})}function d(e){var t=1<arguments.length&&void 0!==arguments[1]&&arguments[1],n=parseInt(e.getAttribute("tabindex"),10);return isNaN(n)?0:t&&n<0?0:n}function y(e){return v(o,0<arguments.length?e:document).filter(c).sort(l)}var g={selector:"[data-ctrly]",context:null,focusTarget:!0,closeOnBlur:!0,closeOnEsc:!0,closeOnOutsideClick:!0,closeOnScroll:!1,trapFocus:!1,allowMultiple:!1,on:null,autoInit:!0};function A(e){return"which"in e?e.which:e.keyCode}function E(e){return v('[aria-controls="'.concat(e.id,'"]'))}function w(e){return document.getElementById(e.getAttribute("aria-controls")||e.getAttribute("data-ctrly"))}function x(e){e.removeAttribute("aria-controls"),e.removeAttribute("aria-expanded")}var O=0;return function(){var n,r,e=0<arguments.length&&void 0!==arguments[0]?arguments[0]:{},u=(n={},[g,e].forEach(function(e){for(var t in e)Object.prototype.hasOwnProperty.call(e,t)&&(n[t]=e[t])}),n),c=u.selector,o=u.on||{},l={};function s(e,t){return("function"!=typeof o[t]||!1!==o[t](e))&&!1!==function(e,t){var n,r=2<arguments.length&&void 0!==arguments[2]?arguments[2]:{};if(!e||"function"!=typeof e.dispatchEvent)return!0;r.bubbles=r.bubbles||!1,r.cancelable=r.cancelable||!1,r.composed=r.composed||!1,r.detail=r.detail||null;try{n=new CustomEvent(t,r)}catch(e){(n=document.createEvent("CustomEvent")).initCustomEvent(t,r.bubbles,r.cancelable,r.detail)}return e.dispatchEvent(n)}(e,"ctrly:".concat(t),{bubbles:!0,cancelable:!0})}function d(e){var t=!(1<arguments.length&&void 0!==arguments[1])||arguments[1];if(!e)return!1;if(!e.hasAttribute("data-ctrly-opened"))return!1;if(!s(e,"close"))return!1;var n=f(),r=l[e.id]||{},o=r.lastActiveElement,i=r.destroy;return delete l[e.id],i&&i(),E(e).forEach(function(e){e.setAttribute("aria-expanded","false")}),e.removeAttribute("data-ctrly-opened"),e.setAttribute("aria-hidden","true"),e.removeAttribute("tabindex"),e.blur(),t&&o&&e.contains(n)&&p(o,{restoreScrollPosition:!0}),s(e,"closed"),e}function i(n){var e;v(c,(e=n,u.context?b(e,u.context):document)).forEach(function(e){var t=w(e);t&&t.id!==n.id&&d(t,!1)})}function a(e){var i,t,n,r,o,a=w(e);return a?!a.hasAttribute("data-ctrly-opened")&&!!s(a,"open")&&(l[a.id]={lastActiveElement:f(),destroy:(i=a,t=[],n=!1,r=function(){n=!0},o=function(){n=!1},(u.closeOnOutsideClick||u.closeOnScroll)&&(t.push(m(i,"mouseenter",r,{passive:!0})),t.push(m(i,"mouseleave",o,{passive:!0})),t.push(m(i,"touchstart",r,{passive:!0})),t.push(m(i,"touchend",o,{passive:!0}))),u.closeOnBlur&&!u.trapFocus&&t.push(m(document,"focusin",function(e){i.contains(e.target)||d(i,!1)},{capture:!0,passive:!0})),u.closeOnEsc&&t.push(m(document,"keydown",function(e){27===A(e)&&d(i)&&e.preventDefault()})),u.closeOnOutsideClick&&t.push(m(document,"click",function(e){n||1!==A(e)||b(e.target,c)||d(i)},{passive:!0})),u.closeOnScroll&&t.push(m(window,"scroll",function(){n||d(i)},{passive:!0})),u.trapFocus&&t.push(m(document,"keydown",function(e){if(9===A(e)){var t=y(i);if(!t[0])return e.preventDefault(),void p(i);var n=f(),r=t[0],o=t[t.length-1];if(e.shiftKey&&n===r)return e.preventDefault(),void p(o);e.shiftKey||n!==o||(p(r),e.preventDefault())}})),function(){for(;t.length;)t.shift().call()})},E(a).forEach(function(e){e.setAttribute("aria-expanded","true")}),a.setAttribute("data-ctrly-opened",""),a.setAttribute("aria-hidden","false"),a.setAttribute("tabindex","-1"),s(a,"opened"),a):(x(e),!1)}function t(){var e,t;r||(r=function(e,t,n,r){var o=4<arguments.length&&void 0!==arguments[4]?arguments[4]:{capture:!1},i=!0===o.once;delete o.once;var a=m(e,t,function(e){var t=b(e.target,n);t&&(i&&a(),r.call(t,e,t))},o);return a}(document,"click",c,function(e,t){if(1===A(e)){var n=w(t);n?"true"!==t.getAttribute("aria-expanded")?(u.allowMultiple||i(n),a(t),n&&(e.preventDefault(),u.focusTarget&&p(y(n)[0]||n),n.scrollTop=0,n.scrollLeft=0)):d(n)&&e.preventDefault():d(function(e){for(var t=e;t;){if(t.id&&l[t.id])return t;t=t.parentElement}}(t))&&e.preventDefault()}})),e=function(){v(c).forEach(function(e){var t=w(e);if(t){e.setAttribute("aria-controls",t.id);var n=E(t).map(function(e){return e.id||e.setAttribute("id","ctrly-control-"+ ++O),e.id}),r=(t.getAttribute("aria-labelledby")||"").split(" ").concat(n).filter(function(e,t,n){return""!==e&&n.indexOf(e)===t});t.setAttribute("aria-labelledby",r.join(" ")),"true"===e.getAttribute("aria-expanded")||e.hasAttribute("data-ctrly-open")?a(e):(e.setAttribute("aria-expanded","false"),t.setAttribute("aria-hidden","true"),t.removeAttribute("tabindex"))}else x(e)})},"complete"!==(t=document.readyState)&&"interactive"!==t?document.addEventListener("DOMContentLoaded",function(){e()},h({capture:!0,once:!0,passive:!0})):setTimeout(e,0)}return u.autoInit&&t(),{init:t,destroy:function(){for(var e in r&&(r(),r=null),v(c).forEach(function(e){var t=w(e);t&&d(t,!1)}),l)Object.prototype.hasOwnProperty.call(l,e)&&(l[e].destroy(),delete l[e])}}}}); |
{ | ||
"name": "ctrly", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"description": "Lightweight and dependency-free content toggling with a focus on accessibility.", | ||
@@ -22,4 +22,3 @@ "repository": "git@github.com:jsor/ctrly.git", | ||
"browser": "dist/ctrly.min.js", | ||
"module": "dist/ctrly-module.mjs", | ||
"es2015": "src/ctrly-es2015.js", | ||
"module": "dist/ctrly.esm.js", | ||
"files": [ | ||
@@ -30,9 +29,7 @@ "dist", | ||
"browserslist": [ | ||
"defaults", | ||
"IE >= 10", | ||
"Chrome >= 60", | ||
"Edge >= 15", | ||
"Firefox >= 54", | ||
"iOS >= 10.3", | ||
"Safari >= 10.1" | ||
"Chrome 49", | ||
"Edge 15", | ||
"Firefox 52", | ||
"IE 10", | ||
"Safari 10.1" | ||
], | ||
@@ -39,0 +36,0 @@ "xo": { |
@@ -38,4 +38,4 @@ ctrly | ||
The latest version can also be downloaded or included from | ||
[unpkg](https://unpkg.com/ctrly/dist/) or | ||
[jsDelivr](https://cdn.jsdelivr.net/npm/ctrly/dist/). | ||
[unpkg](https://unpkg.com/ctrly) or | ||
[jsDelivr](https://cdn.jsdelivr.net/npm/ctrly). | ||
@@ -190,2 +190,3 @@ ```html | ||
* [on](#on) | ||
* [autoInit](#autoinit) | ||
@@ -382,2 +383,20 @@ ### selector | ||
### autoInit | ||
*Default:* `true` | ||
By default, initialization is done when calling `ctrly()`. Passing `false` as | ||
an option disables this behavior and the [`init()`](#init) method must be called | ||
manually. | ||
#### Example | ||
```js | ||
const { init } = ctrly({ | ||
autoInit: false | ||
}); | ||
init(); | ||
``` | ||
More information about the event callbacks can be found in the | ||
@@ -384,0 +403,0 @@ [Events section](#events). |
@@ -23,3 +23,4 @@ import { | ||
allowMultiple: false, | ||
on: null | ||
on: null, | ||
autoInit: true | ||
}; | ||
@@ -194,7 +195,4 @@ | ||
removeFuncs.push( | ||
on(target, 'focusout', e => { | ||
if ( | ||
!e.relatedTarget || | ||
!target.contains(e.relatedTarget) | ||
) { | ||
on(document, 'focusin', e => { | ||
if (!target.contains(e.target)) { | ||
close(target, false); | ||
@@ -432,5 +430,7 @@ } | ||
init(); | ||
if (options.autoInit) { | ||
init(); | ||
} | ||
return {init, destroy}; | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
529
0
69102
7
1545