@nrk/core-toggle
Advanced tools
Comparing version 2.2.1 to 2.2.2
'use strict'; | ||
var name = "@nrk/core-toggle"; | ||
var version = "2.2.0"; | ||
var version = "2.2.1"; | ||
@@ -89,3 +89,2 @@ var IS_BROWSER = typeof window !== 'undefined'; | ||
var ARIA = IS_ANDROID ? 'data' : 'aria'; // Andriod has a bug and reads only label instead of content | ||
var OPEN = 'aria-expanded'; | ||
var KEYS = { ESC: 27 }; | ||
@@ -98,13 +97,13 @@ | ||
return queryAll(toggles).map(function (toggle) { | ||
var isOpen = toggle.getAttribute(OPEN) === 'true'; | ||
var content = getContentElement(toggle); | ||
var isOpen = toggle.hasAttribute('aria-expanded') ? toggle.getAttribute('aria-expanded') === 'true' : !content.hasAttribute('hidden'); | ||
var open = typeof options.open === 'boolean' ? options.open : (options.open === 'toggle' ? !isOpen : isOpen); | ||
var popup = String((options.hasOwnProperty('popup') ? options.popup : toggle.getAttribute(UUID)) || false); | ||
var popupAttr = popup.replace(/^true|false$/, '') ? 'setAttribute' : 'removeAttribute'; | ||
var content = getContentElement(toggle); | ||
if (options.value) { toggle.innerHTML = options.value; } // Set innerHTML before updating aria-label | ||
if (popup !== 'false' && popup !== 'true') { toggle.setAttribute('aria-label', ((toggle.textContent) + ", " + popup)); } // Only update aria-label if popup-mode | ||
toggle.setAttribute(UUID, popup); // aria-haspopup triggers forms mode in JAWS, therefore store in uuid | ||
toggle.setAttribute('aria-controls', content.id = content.id || getUUID()); | ||
content.setAttribute((ARIA + "-labelledby"), toggle.id = toggle.id || getUUID()); | ||
toggle[popupAttr]('aria-label', ((toggle.textContent) + ", " + popup)); | ||
setOpen(toggle, open); | ||
@@ -124,3 +123,3 @@ return toggle | ||
if (toggle.getAttribute(UUID) !== 'false' && toggle.getAttribute(OPEN) === 'true') { | ||
if (toggle.getAttribute(UUID) !== 'false' && toggle.getAttribute('aria-expanded') === 'true') { | ||
event.preventDefault(); // Prevent leaving maximized window in Safari | ||
@@ -153,3 +152,3 @@ toggle.focus(); | ||
queryAll(("[" + UUID + "]")).forEach(function (toggle) { | ||
var open = toggle.getAttribute(OPEN) === 'true'; | ||
var open = toggle.getAttribute('aria-expanded') === 'true'; | ||
var popup = toggle.getAttribute(UUID) !== 'false'; | ||
@@ -165,6 +164,6 @@ var content = getContentElement(toggle); | ||
var content = getContentElement(toggle); | ||
var isOpen = toggle.getAttribute(OPEN) === 'true'; | ||
var isOpen = toggle.getAttribute('aria-expanded') === 'true'; | ||
var willOpen = typeof open === 'boolean' ? open : (open === 'toggle' ? !isOpen : isOpen); | ||
var isUpdate = isOpen === willOpen || dispatchEvent(toggle, 'toggle', { relatedTarget: content, isOpen: isOpen, willOpen: willOpen }); | ||
var nextOpen = isUpdate ? willOpen : toggle.getAttribute(OPEN) === 'true'; // dispatchEvent can change attributes | ||
var nextOpen = isUpdate ? willOpen : toggle.getAttribute('aria-expanded') === 'true'; // dispatchEvent can change attributes | ||
var focus = !isOpen && nextOpen && content.querySelector('[autofocus]'); | ||
@@ -174,3 +173,3 @@ | ||
toggle.setAttribute(OPEN, nextOpen); | ||
toggle.setAttribute('aria-expanded', nextOpen); | ||
content[nextOpen ? 'removeAttribute' : 'setAttribute']('hidden', ''); | ||
@@ -177,0 +176,0 @@ } |
@@ -6,3 +6,2 @@ import { name, version } from './package.json' | ||
const ARIA = IS_ANDROID ? 'data' : 'aria' // Andriod has a bug and reads only label instead of content | ||
const OPEN = 'aria-expanded' | ||
const KEYS = { ESC: 27 } | ||
@@ -15,13 +14,13 @@ | ||
return queryAll(toggles).map((toggle) => { | ||
const isOpen = toggle.getAttribute(OPEN) === 'true' | ||
const content = getContentElement(toggle) | ||
const isOpen = toggle.hasAttribute('aria-expanded') ? toggle.getAttribute('aria-expanded') === 'true' : !content.hasAttribute('hidden') | ||
const open = typeof options.open === 'boolean' ? options.open : (options.open === 'toggle' ? !isOpen : isOpen) | ||
const popup = String((options.hasOwnProperty('popup') ? options.popup : toggle.getAttribute(UUID)) || false) | ||
const popupAttr = popup.replace(/^true|false$/, '') ? 'setAttribute' : 'removeAttribute' | ||
const content = getContentElement(toggle) | ||
if (options.value) toggle.innerHTML = options.value // Set innerHTML before updating aria-label | ||
if (popup !== 'false' && popup !== 'true') toggle.setAttribute('aria-label', `${toggle.textContent}, ${popup}`) // Only update aria-label if popup-mode | ||
toggle.setAttribute(UUID, popup) // aria-haspopup triggers forms mode in JAWS, therefore store in uuid | ||
toggle.setAttribute('aria-controls', content.id = content.id || getUUID()) | ||
content.setAttribute(`${ARIA}-labelledby`, toggle.id = toggle.id || getUUID()) | ||
toggle[popupAttr]('aria-label', `${toggle.textContent}, ${popup}`) | ||
setOpen(toggle, open) | ||
@@ -41,3 +40,3 @@ return toggle | ||
if (toggle.getAttribute(UUID) !== 'false' && toggle.getAttribute(OPEN) === 'true') { | ||
if (toggle.getAttribute(UUID) !== 'false' && toggle.getAttribute('aria-expanded') === 'true') { | ||
event.preventDefault() // Prevent leaving maximized window in Safari | ||
@@ -67,3 +66,3 @@ toggle.focus() | ||
queryAll(`[${UUID}]`).forEach((toggle) => { | ||
const open = toggle.getAttribute(OPEN) === 'true' | ||
const open = toggle.getAttribute('aria-expanded') === 'true' | ||
const popup = toggle.getAttribute(UUID) !== 'false' | ||
@@ -79,6 +78,6 @@ const content = getContentElement(toggle) | ||
const content = getContentElement(toggle) | ||
const isOpen = toggle.getAttribute(OPEN) === 'true' | ||
const isOpen = toggle.getAttribute('aria-expanded') === 'true' | ||
const willOpen = typeof open === 'boolean' ? open : (open === 'toggle' ? !isOpen : isOpen) | ||
const isUpdate = isOpen === willOpen || dispatchEvent(toggle, 'toggle', { relatedTarget: content, isOpen, willOpen }) | ||
const nextOpen = isUpdate ? willOpen : toggle.getAttribute(OPEN) === 'true' // dispatchEvent can change attributes | ||
const nextOpen = isUpdate ? willOpen : toggle.getAttribute('aria-expanded') === 'true' // dispatchEvent can change attributes | ||
const focus = !isOpen && nextOpen && content.querySelector('[autofocus]') | ||
@@ -88,4 +87,4 @@ | ||
toggle.setAttribute(OPEN, nextOpen) | ||
toggle.setAttribute('aria-expanded', nextOpen) | ||
content[nextOpen ? 'removeAttribute' : 'setAttribute']('hidden', '') | ||
} |
@@ -1,3 +0,3 @@ | ||
/*! @nrk/core-toggle v2.2.0 - Copyright (c) 2017-2019 NRK */ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t(require("react"),require("prop-types")):"function"==typeof define&&define.amd?define(["react","prop-types"],t):(e=e||self).CoreToggle=t(e.React,e.PropTypes)}(this,function(u,e){"use strict";u=u&&u.hasOwnProperty("default")?u.default:u,e=e&&e.hasOwnProperty("default")?e.default:e;var t="undefined"!=typeof window,o=t&&/(android)/i.test(navigator.userAgent),n=t&&/iPad|iPhone|iPod/.test(String(navigator.platform)),r=function(e){void 0===e&&(e=!1);try{window.addEventListener("test",null,{get passive(){e=!0}})}catch(e){}return e}();function i(e,t,o,n){(void 0===n&&(n=!1),"undefined"==typeof window||window[e=e+"-"+t])||(r||"object"!=typeof n||(n=Boolean(n.capture)),("resize"===t||"load"===t?window:document).addEventListener(window[e]=t,o,n))}var l="prevent_recursive_dispatch_maximum_callstack";function p(e,t,o){void 0===o&&(o={});var n,r=""+l+t;if(e[r])return!0;e[r]=!0,"function"==typeof window.CustomEvent?n=new window.CustomEvent(t,{bubbles:!0,cancelable:!0,detail:o}):(n=document.createEvent("CustomEvent")).initCustomEvent(t,!0,!0,o);var i=e.dispatchEvent(n);return e[r]=null,i}function a(e){return Date.now().toString(36)+Math.random().toString(36).slice(2,5)}function s(e,t){if(void 0===t&&(t=document),e){if(e.nodeType)return[e];if("string"==typeof e)return[].slice.call(t.querySelectorAll(e));if(e.length)return[].slice.call(e)}return[]}var c="data-@nrk/core-toggle-2.2.0".replace(/\W+/g,"-"),d=o?"data":"aria",g="aria-expanded",f=27;function v(e){return document.getElementById(e.getAttribute("aria-controls"))||e.nextElementSibling}function h(e,t){var o=v(e),n="true"===e.getAttribute(g),r="boolean"==typeof t?t:"toggle"===t?!n:n,i=n===r||p(e,"toggle",{relatedTarget:o,isOpen:n,willOpen:r})?r:"true"===e.getAttribute(g),u=!n&&i&&o.querySelector("[autofocus]");u&&setTimeout(function(){return u&&u.focus()}),e.setAttribute(g,i),o[i?"removeAttribute":"setAttribute"]("hidden","")}i(c,"keydown",function(e){if(e.keyCode===f)for(var t=e.target;t;t=t.parentElement){var o=t.id&&document.querySelector('[aria-controls="'+t.id+'"]')||t;if("false"!==o.getAttribute(c)&&"true"===o.getAttribute(g))return e.preventDefault(),o.focus(),h(o,!1)}},!0),i(c,"click",function(e){var r=e.target;if(e.defaultPrevented)return!1;for(var t=r,o=void 0;t;t=t.parentElement){var n=o&&t.id&&document.querySelector("["+c+'][aria-controls="'+t.id+'"]');if("BUTTON"!==t.nodeName&&"A"!==t.nodeName||t.hasAttribute(c)||(o=t),n){p(n,"toggle.select",{relatedTarget:v(n),currentTarget:o,value:o.textContent.trim()});break}}s("["+c+"]").forEach(function(e){var t="true"===e.getAttribute(g),o="false"!==e.getAttribute(c),n=v(e);e.contains(r)?h(e,!t):o&&t&&h(e,n.contains(r))})});var m=function(t){function i(e){t.call(this,e),this.onToggle=this.onToggle.bind(this),this.onToggleSelect=this.onToggleSelect.bind(this)}t&&(i.__proto__=t),(i.prototype=Object.create(t&&t.prototype)).constructor=i;var e={defaultProps:{configurable:!0}};return e.defaultProps.get=function(){return{open:null,popup:null,onToggle:null,onToggleSelect:null}},i.prototype.update=function(){var e,t,u;e=this.el.firstElementChild,t={popup:this.props.popup,open:this.props.open},u="object"==typeof t?t:{open:t},n&&(document.documentElement.style.cursor="pointer"),s(e).map(function(e){var t="true"===e.getAttribute(g),o="boolean"==typeof u.open?u.open:"toggle"===u.open?!t:t,n=String((u.hasOwnProperty("popup")?u.popup:e.getAttribute(c))||!1),r=n.replace(/^true|false$/,"")?"setAttribute":"removeAttribute",i=v(e);return u.value&&(e.innerHTML=u.value),e.setAttribute(c,n),e.setAttribute("aria-controls",i.id=i.id||a()),i.setAttribute(d+"-labelledby",e.id=e.id||a()),e[r]("aria-label",e.textContent+", "+n),h(e,o),e})},i.prototype.componentDidMount=function(){this.update(),this.el.addEventListener("toggle",this.onToggle),this.el.addEventListener("toggle.select",this.onToggleSelect)},i.prototype.componentDidUpdate=function(){this.update()},i.prototype.componentWillUnmount=function(){this.el.removeEventListener("toggle",this.onToggle),this.el.removeEventListener("toggle.select",this.onToggleSelect)},i.prototype.onToggle=function(e){this.props.onToggle&&this.props.onToggle(e)},i.prototype.onToggleSelect=function(e){this.props.onToggleSelect&&this.props.onToggleSelect(e)},i.prototype.render=function(){var o,n,e,r=this;return u.createElement("div",(o=this.props,n=i.defaultProps,void 0===(e={ref:function(e){return r.el=e}})&&(e={}),Object.keys(o).reduce(function(e,t){return n.hasOwnProperty(t)||(e[t]=o[t]),e},e)),u.Children.map(this.props.children,function(e,t){return 0===t?u.cloneElement(e,{"aria-expanded":String(Boolean(r.props.open))}):1===t?u.cloneElement(e,{hidden:!r.props.open}):e}))},Object.defineProperties(i,e),i}(u.Component);return m.propTypes={open:e.bool,popup:e.oneOfType([e.bool,e.string]),onToggle:e.func},m}); | ||
/*! @nrk/core-toggle v2.2.1 - Copyright (c) 2017-2019 NRK */ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t(require("react"),require("prop-types")):"function"==typeof define&&define.amd?define(["react","prop-types"],t):(e=e||self).CoreToggle=t(e.React,e.PropTypes)}(this,function(a,e){"use strict";a=a&&a.hasOwnProperty("default")?a.default:a,e=e&&e.hasOwnProperty("default")?e.default:e;var t="undefined"!=typeof window,n=t&&/(android)/i.test(navigator.userAgent),o=t&&/iPad|iPhone|iPod/.test(String(navigator.platform)),r=function(e){void 0===e&&(e=!1);try{window.addEventListener("test",null,{get passive(){e=!0}})}catch(e){}return e}();function i(e,t,n,o){(void 0===o&&(o=!1),"undefined"==typeof window||window[e=e+"-"+t])||(r||"object"!=typeof o||(o=Boolean(o.capture)),("resize"===t||"load"===t?window:document).addEventListener(window[e]=t,n,o))}var u="prevent_recursive_dispatch_maximum_callstack";function l(e,t,n){void 0===n&&(n={});var o,r=""+u+t;if(e[r])return!0;e[r]=!0,"function"==typeof window.CustomEvent?o=new window.CustomEvent(t,{bubbles:!0,cancelable:!0,detail:n}):(o=document.createEvent("CustomEvent")).initCustomEvent(t,!0,!0,n);var i=e.dispatchEvent(o);return e[r]=null,i}function p(e){return Date.now().toString(36)+Math.random().toString(36).slice(2,5)}function d(e,t){if(void 0===t&&(t=document),e){if(e.nodeType)return[e];if("string"==typeof e)return[].slice.call(t.querySelectorAll(e));if(e.length)return[].slice.call(e)}return[]}var s="data-@nrk/core-toggle-2.2.1".replace(/\W+/g,"-"),c=n?"data":"aria",g=27;function f(e){return document.getElementById(e.getAttribute("aria-controls"))||e.nextElementSibling}function v(e,t){var n=f(e),o="true"===e.getAttribute("aria-expanded"),r="boolean"==typeof t?t:"toggle"===t?!o:o,i=o===r||l(e,"toggle",{relatedTarget:n,isOpen:o,willOpen:r})?r:"true"===e.getAttribute("aria-expanded"),a=!o&&i&&n.querySelector("[autofocus]");a&&setTimeout(function(){return a&&a.focus()}),e.setAttribute("aria-expanded",i),n[i?"removeAttribute":"setAttribute"]("hidden","")}i(s,"keydown",function(e){if(e.keyCode===g)for(var t=e.target;t;t=t.parentElement){var n=t.id&&document.querySelector('[aria-controls="'+t.id+'"]')||t;if("false"!==n.getAttribute(s)&&"true"===n.getAttribute("aria-expanded"))return e.preventDefault(),n.focus(),v(n,!1)}},!0),i(s,"click",function(e){var r=e.target;if(e.defaultPrevented)return!1;for(var t=r,n=void 0;t;t=t.parentElement){var o=n&&t.id&&document.querySelector("["+s+'][aria-controls="'+t.id+'"]');if("BUTTON"!==t.nodeName&&"A"!==t.nodeName||t.hasAttribute(s)||(n=t),o){l(o,"toggle.select",{relatedTarget:f(o),currentTarget:n,value:n.textContent.trim()});break}}d("["+s+"]").forEach(function(e){var t="true"===e.getAttribute("aria-expanded"),n="false"!==e.getAttribute(s),o=f(e);e.contains(r)?v(e,!t):n&&t&&v(e,o.contains(r))})});var h=function(t){function i(e){t.call(this,e),this.onToggle=this.onToggle.bind(this),this.onToggleSelect=this.onToggleSelect.bind(this)}t&&(i.__proto__=t),(i.prototype=Object.create(t&&t.prototype)).constructor=i;var e={defaultProps:{configurable:!0}};return e.defaultProps.get=function(){return{open:null,popup:null,onToggle:null,onToggleSelect:null}},i.prototype.update=function(){var e,t,i;e=this.el.firstElementChild,t={popup:this.props.popup,open:this.props.open},i="object"==typeof t?t:{open:t},o&&(document.documentElement.style.cursor="pointer"),d(e).map(function(e){var t=f(e),n=e.hasAttribute("aria-expanded")?"true"===e.getAttribute("aria-expanded"):!t.hasAttribute("hidden"),o="boolean"==typeof i.open?i.open:"toggle"===i.open?!n:n,r=String((i.hasOwnProperty("popup")?i.popup:e.getAttribute(s))||!1);return i.value&&(e.innerHTML=i.value),"false"!==r&&"true"!==r&&e.setAttribute("aria-label",e.textContent+", "+r),e.setAttribute(s,r),e.setAttribute("aria-controls",t.id=t.id||p()),t.setAttribute(c+"-labelledby",e.id=e.id||p()),v(e,o),e})},i.prototype.componentDidMount=function(){this.update(),this.el.addEventListener("toggle",this.onToggle),this.el.addEventListener("toggle.select",this.onToggleSelect)},i.prototype.componentDidUpdate=function(){this.update()},i.prototype.componentWillUnmount=function(){this.el.removeEventListener("toggle",this.onToggle),this.el.removeEventListener("toggle.select",this.onToggleSelect)},i.prototype.onToggle=function(e){this.props.onToggle&&this.props.onToggle(e)},i.prototype.onToggleSelect=function(e){this.props.onToggleSelect&&this.props.onToggleSelect(e)},i.prototype.render=function(){var n,o,e,r=this;return a.createElement("div",(n=this.props,o=i.defaultProps,void 0===(e={ref:function(e){return r.el=e}})&&(e={}),Object.keys(n).reduce(function(e,t){return o.hasOwnProperty(t)||(e[t]=n[t]),e},e)),a.Children.map(this.props.children,function(e,t){return 0===t?a.cloneElement(e,{"aria-expanded":String(Boolean(r.props.open))}):1===t?a.cloneElement(e,{hidden:!r.props.open}):e}))},Object.defineProperties(i,e),i}(a.Component);return h.propTypes={open:e.bool,popup:e.oneOfType([e.bool,e.string]),onToggle:e.func},h}); | ||
//# sourceMappingURL=core-toggle.jsx.js.map |
@@ -1,3 +0,3 @@ | ||
/*! @nrk/core-toggle v2.2.0 - Copyright (c) 2017-2019 NRK */ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e=e||self).coreToggle=t()}(this,function(){"use strict";var e="undefined"!=typeof window,t=e&&/(android)/i.test(navigator.userAgent),n=e&&/iPad|iPhone|iPod/.test(String(navigator.platform)),o=function(e){void 0===e&&(e=!1);try{window.addEventListener("test",null,{get passive(){e=!0}})}catch(e){}return e}();function r(e,t,n,r){(void 0===r&&(r=!1),"undefined"==typeof window||window[e=e+"-"+t])||(o||"object"!=typeof r||(r=Boolean(r.capture)),("resize"===t||"load"===t?window:document).addEventListener(window[e]=t,n,r))}var u="prevent_recursive_dispatch_maximum_callstack";function a(e,t,n){void 0===n&&(n={});var r,o=""+u+t;if(e[o])return!0;e[o]=!0,"function"==typeof window.CustomEvent?r=new window.CustomEvent(t,{bubbles:!0,cancelable:!0,detail:n}):(r=document.createEvent("CustomEvent")).initCustomEvent(t,!0,!0,n);var i=e.dispatchEvent(r);return e[o]=null,i}function d(e){return Date.now().toString(36)+Math.random().toString(36).slice(2,5)}function i(e,t){if(void 0===t&&(t=document),e){if(e.nodeType)return[e];if("string"==typeof e)return[].slice.call(t.querySelectorAll(e));if(e.length)return[].slice.call(e)}return[]}var l="data-@nrk/core-toggle-2.2.0".replace(/\W+/g,"-"),c=t?"data":"aria",f="aria-expanded",s=27;function p(e){return document.getElementById(e.getAttribute("aria-controls"))||e.nextElementSibling}function g(e,t){var n=p(e),r="true"===e.getAttribute(f),o="boolean"==typeof t?t:"toggle"===t?!r:r,i=r===o||a(e,"toggle",{relatedTarget:n,isOpen:r,willOpen:o})?o:"true"===e.getAttribute(f),u=!r&&i&&n.querySelector("[autofocus]");u&&setTimeout(function(){return u&&u.focus()}),e.setAttribute(f,i),n[i?"removeAttribute":"setAttribute"]("hidden","")}return r(l,"keydown",function(e){if(e.keyCode===s)for(var t=e.target;t;t=t.parentElement){var n=t.id&&document.querySelector('[aria-controls="'+t.id+'"]')||t;if("false"!==n.getAttribute(l)&&"true"===n.getAttribute(f))return e.preventDefault(),n.focus(),g(n,!1)}},!0),r(l,"click",function(e){var o=e.target;if(e.defaultPrevented)return!1;for(var t=o,n=void 0;t;t=t.parentElement){var r=n&&t.id&&document.querySelector("["+l+'][aria-controls="'+t.id+'"]');if("BUTTON"!==t.nodeName&&"A"!==t.nodeName||t.hasAttribute(l)||(n=t),r){a(r,"toggle.select",{relatedTarget:p(r),currentTarget:n,value:n.textContent.trim()});break}}i("["+l+"]").forEach(function(e){var t="true"===e.getAttribute(f),n="false"!==e.getAttribute(l),r=p(e);e.contains(o)?g(e,!t):n&&t&&g(e,r.contains(o))})}),function(e,t){var u="object"==typeof t?t:{open:t};return n&&(document.documentElement.style.cursor="pointer"),i(e).map(function(e){var t="true"===e.getAttribute(f),n="boolean"==typeof u.open?u.open:"toggle"===u.open?!t:t,r=String((u.hasOwnProperty("popup")?u.popup:e.getAttribute(l))||!1),o=r.replace(/^true|false$/,"")?"setAttribute":"removeAttribute",i=p(e);return u.value&&(e.innerHTML=u.value),e.setAttribute(l,r),e.setAttribute("aria-controls",i.id=i.id||d()),i.setAttribute(c+"-labelledby",e.id=e.id||d()),e[o]("aria-label",e.textContent+", "+r),g(e,n),e})}}); | ||
/*! @nrk/core-toggle v2.2.1 - Copyright (c) 2017-2019 NRK */ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e=e||self).coreToggle=t()}(this,function(){"use strict";var e="undefined"!=typeof window,t=e&&/(android)/i.test(navigator.userAgent),n=e&&/iPad|iPhone|iPod/.test(String(navigator.platform)),i=function(e){void 0===e&&(e=!1);try{window.addEventListener("test",null,{get passive(){e=!0}})}catch(e){}return e}();function r(e,t,n,r){(void 0===r&&(r=!1),"undefined"==typeof window||window[e=e+"-"+t])||(i||"object"!=typeof r||(r=Boolean(r.capture)),("resize"===t||"load"===t?window:document).addEventListener(window[e]=t,n,r))}var a="prevent_recursive_dispatch_maximum_callstack";function u(e,t,n){void 0===n&&(n={});var r,i=""+a+t;if(e[i])return!0;e[i]=!0,"function"==typeof window.CustomEvent?r=new window.CustomEvent(t,{bubbles:!0,cancelable:!0,detail:n}):(r=document.createEvent("CustomEvent")).initCustomEvent(t,!0,!0,n);var o=e.dispatchEvent(r);return e[i]=null,o}function d(e){return Date.now().toString(36)+Math.random().toString(36).slice(2,5)}function l(e,t){if(void 0===t&&(t=document),e){if(e.nodeType)return[e];if("string"==typeof e)return[].slice.call(t.querySelectorAll(e));if(e.length)return[].slice.call(e)}return[]}var c="data-@nrk/core-toggle-2.2.1".replace(/\W+/g,"-"),f=t?"data":"aria",o=27;function s(e){return document.getElementById(e.getAttribute("aria-controls"))||e.nextElementSibling}function p(e,t){var n=s(e),r="true"===e.getAttribute("aria-expanded"),i="boolean"==typeof t?t:"toggle"===t?!r:r,o=r===i||u(e,"toggle",{relatedTarget:n,isOpen:r,willOpen:i})?i:"true"===e.getAttribute("aria-expanded"),a=!r&&o&&n.querySelector("[autofocus]");a&&setTimeout(function(){return a&&a.focus()}),e.setAttribute("aria-expanded",o),n[o?"removeAttribute":"setAttribute"]("hidden","")}return r(c,"keydown",function(e){if(e.keyCode===o)for(var t=e.target;t;t=t.parentElement){var n=t.id&&document.querySelector('[aria-controls="'+t.id+'"]')||t;if("false"!==n.getAttribute(c)&&"true"===n.getAttribute("aria-expanded"))return e.preventDefault(),n.focus(),p(n,!1)}},!0),r(c,"click",function(e){var i=e.target;if(e.defaultPrevented)return!1;for(var t=i,n=void 0;t;t=t.parentElement){var r=n&&t.id&&document.querySelector("["+c+'][aria-controls="'+t.id+'"]');if("BUTTON"!==t.nodeName&&"A"!==t.nodeName||t.hasAttribute(c)||(n=t),r){u(r,"toggle.select",{relatedTarget:s(r),currentTarget:n,value:n.textContent.trim()});break}}l("["+c+"]").forEach(function(e){var t="true"===e.getAttribute("aria-expanded"),n="false"!==e.getAttribute(c),r=s(e);e.contains(i)?p(e,!t):n&&t&&p(e,r.contains(i))})}),function(e,t){var o="object"==typeof t?t:{open:t};return n&&(document.documentElement.style.cursor="pointer"),l(e).map(function(e){var t=s(e),n=e.hasAttribute("aria-expanded")?"true"===e.getAttribute("aria-expanded"):!t.hasAttribute("hidden"),r="boolean"==typeof o.open?o.open:"toggle"===o.open?!n:n,i=String((o.hasOwnProperty("popup")?o.popup:e.getAttribute(c))||!1);return o.value&&(e.innerHTML=o.value),"false"!==i&&"true"!==i&&e.setAttribute("aria-label",e.textContent+", "+i),e.setAttribute(c,i),e.setAttribute("aria-controls",t.id=t.id||d()),t.setAttribute(f+"-labelledby",e.id=e.id||d()),p(e,r),e})}}); | ||
//# sourceMappingURL=core-toggle.min.js.map |
21
jsx.js
@@ -9,3 +9,3 @@ 'use strict'; | ||
var name = "@nrk/core-toggle"; | ||
var version = "2.2.0"; | ||
var version = "2.2.1"; | ||
@@ -110,3 +110,2 @@ var IS_BROWSER = typeof window !== 'undefined'; | ||
var ARIA = IS_ANDROID ? 'data' : 'aria'; // Andriod has a bug and reads only label instead of content | ||
var OPEN = 'aria-expanded'; | ||
var KEYS = { ESC: 27 }; | ||
@@ -119,13 +118,13 @@ | ||
return queryAll(toggles).map(function (toggle) { | ||
var isOpen = toggle.getAttribute(OPEN) === 'true'; | ||
var content = getContentElement(toggle); | ||
var isOpen = toggle.hasAttribute('aria-expanded') ? toggle.getAttribute('aria-expanded') === 'true' : !content.hasAttribute('hidden'); | ||
var open = typeof options.open === 'boolean' ? options.open : (options.open === 'toggle' ? !isOpen : isOpen); | ||
var popup = String((options.hasOwnProperty('popup') ? options.popup : toggle.getAttribute(UUID)) || false); | ||
var popupAttr = popup.replace(/^true|false$/, '') ? 'setAttribute' : 'removeAttribute'; | ||
var content = getContentElement(toggle); | ||
if (options.value) { toggle.innerHTML = options.value; } // Set innerHTML before updating aria-label | ||
if (popup !== 'false' && popup !== 'true') { toggle.setAttribute('aria-label', ((toggle.textContent) + ", " + popup)); } // Only update aria-label if popup-mode | ||
toggle.setAttribute(UUID, popup); // aria-haspopup triggers forms mode in JAWS, therefore store in uuid | ||
toggle.setAttribute('aria-controls', content.id = content.id || getUUID()); | ||
content.setAttribute((ARIA + "-labelledby"), toggle.id = toggle.id || getUUID()); | ||
toggle[popupAttr]('aria-label', ((toggle.textContent) + ", " + popup)); | ||
setOpen(toggle, open); | ||
@@ -145,3 +144,3 @@ return toggle | ||
if (toggle.getAttribute(UUID) !== 'false' && toggle.getAttribute(OPEN) === 'true') { | ||
if (toggle.getAttribute(UUID) !== 'false' && toggle.getAttribute('aria-expanded') === 'true') { | ||
event.preventDefault(); // Prevent leaving maximized window in Safari | ||
@@ -174,3 +173,3 @@ toggle.focus(); | ||
queryAll(("[" + UUID + "]")).forEach(function (toggle) { | ||
var open = toggle.getAttribute(OPEN) === 'true'; | ||
var open = toggle.getAttribute('aria-expanded') === 'true'; | ||
var popup = toggle.getAttribute(UUID) !== 'false'; | ||
@@ -186,6 +185,6 @@ var content = getContentElement(toggle); | ||
var content = getContentElement(toggle); | ||
var isOpen = toggle.getAttribute(OPEN) === 'true'; | ||
var isOpen = toggle.getAttribute('aria-expanded') === 'true'; | ||
var willOpen = typeof open === 'boolean' ? open : (open === 'toggle' ? !isOpen : isOpen); | ||
var isUpdate = isOpen === willOpen || dispatchEvent(toggle, 'toggle', { relatedTarget: content, isOpen: isOpen, willOpen: willOpen }); | ||
var nextOpen = isUpdate ? willOpen : toggle.getAttribute(OPEN) === 'true'; // dispatchEvent can change attributes | ||
var nextOpen = isUpdate ? willOpen : toggle.getAttribute('aria-expanded') === 'true'; // dispatchEvent can change attributes | ||
var focus = !isOpen && nextOpen && content.querySelector('[autofocus]'); | ||
@@ -195,3 +194,3 @@ | ||
toggle.setAttribute(OPEN, nextOpen); | ||
toggle.setAttribute('aria-expanded', nextOpen); | ||
content[nextOpen ? 'removeAttribute' : 'setAttribute']('hidden', ''); | ||
@@ -198,0 +197,0 @@ } |
@@ -5,3 +5,3 @@ { | ||
"author": "NRK <opensource@nrk.no> (https://www.nrk.no/)", | ||
"version": "2.2.1", | ||
"version": "2.2.2", | ||
"license": "MIT", | ||
@@ -8,0 +8,0 @@ "main": "core-toggle.cjs.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
78153
643