index.esm.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. /*!
  2. * tabbable 5.3.3
  3. * @license MIT, https://github.com/focus-trap/tabbable/blob/master/LICENSE
  4. */
  5. var candidateSelectors = ['input', 'select', 'textarea', 'a[href]', 'button', '[tabindex]:not(slot)', 'audio[controls]', 'video[controls]', '[contenteditable]:not([contenteditable="false"])', 'details>summary:first-of-type', 'details'];
  6. var candidateSelector = /* #__PURE__ */candidateSelectors.join(',');
  7. var NoElement = typeof Element === 'undefined';
  8. var matches = NoElement ? function () {} : Element.prototype.matches || Element.prototype.msMatchesSelector || Element.prototype.webkitMatchesSelector;
  9. var getRootNode = !NoElement && Element.prototype.getRootNode ? function (element) {
  10. return element.getRootNode();
  11. } : function (element) {
  12. return element.ownerDocument;
  13. };
  14. /**
  15. * @param {Element} el container to check in
  16. * @param {boolean} includeContainer add container to check
  17. * @param {(node: Element) => boolean} filter filter candidates
  18. * @returns {Element[]}
  19. */
  20. var getCandidates = function getCandidates(el, includeContainer, filter) {
  21. var candidates = Array.prototype.slice.apply(el.querySelectorAll(candidateSelector));
  22. if (includeContainer && matches.call(el, candidateSelector)) {
  23. candidates.unshift(el);
  24. }
  25. candidates = candidates.filter(filter);
  26. return candidates;
  27. };
  28. /**
  29. * @callback GetShadowRoot
  30. * @param {Element} element to check for shadow root
  31. * @returns {ShadowRoot|boolean} ShadowRoot if available or boolean indicating if a shadowRoot is attached but not available.
  32. */
  33. /**
  34. * @callback ShadowRootFilter
  35. * @param {Element} shadowHostNode the element which contains shadow content
  36. * @returns {boolean} true if a shadow root could potentially contain valid candidates.
  37. */
  38. /**
  39. * @typedef {Object} CandidatesScope
  40. * @property {Element} scope contains inner candidates
  41. * @property {Element[]} candidates
  42. */
  43. /**
  44. * @typedef {Object} IterativeOptions
  45. * @property {GetShadowRoot|boolean} getShadowRoot true if shadow support is enabled; falsy if not;
  46. * if a function, implies shadow support is enabled and either returns the shadow root of an element
  47. * or a boolean stating if it has an undisclosed shadow root
  48. * @property {(node: Element) => boolean} filter filter candidates
  49. * @property {boolean} flatten if true then result will flatten any CandidatesScope into the returned list
  50. * @property {ShadowRootFilter} shadowRootFilter filter shadow roots;
  51. */
  52. /**
  53. * @param {Element[]} elements list of element containers to match candidates from
  54. * @param {boolean} includeContainer add container list to check
  55. * @param {IterativeOptions} options
  56. * @returns {Array.<Element|CandidatesScope>}
  57. */
  58. var getCandidatesIteratively = function getCandidatesIteratively(elements, includeContainer, options) {
  59. var candidates = [];
  60. var elementsToCheck = Array.from(elements);
  61. while (elementsToCheck.length) {
  62. var element = elementsToCheck.shift();
  63. if (element.tagName === 'SLOT') {
  64. // add shadow dom slot scope (slot itself cannot be focusable)
  65. var assigned = element.assignedElements();
  66. var content = assigned.length ? assigned : element.children;
  67. var nestedCandidates = getCandidatesIteratively(content, true, options);
  68. if (options.flatten) {
  69. candidates.push.apply(candidates, nestedCandidates);
  70. } else {
  71. candidates.push({
  72. scope: element,
  73. candidates: nestedCandidates
  74. });
  75. }
  76. } else {
  77. // check candidate element
  78. var validCandidate = matches.call(element, candidateSelector);
  79. if (validCandidate && options.filter(element) && (includeContainer || !elements.includes(element))) {
  80. candidates.push(element);
  81. } // iterate over shadow content if possible
  82. var shadowRoot = element.shadowRoot || // check for an undisclosed shadow
  83. typeof options.getShadowRoot === 'function' && options.getShadowRoot(element);
  84. var validShadowRoot = !options.shadowRootFilter || options.shadowRootFilter(element);
  85. if (shadowRoot && validShadowRoot) {
  86. // add shadow dom scope IIF a shadow root node was given; otherwise, an undisclosed
  87. // shadow exists, so look at light dom children as fallback BUT create a scope for any
  88. // child candidates found because they're likely slotted elements (elements that are
  89. // children of the web component element (which has the shadow), in the light dom, but
  90. // slotted somewhere _inside_ the undisclosed shadow) -- the scope is created below,
  91. // _after_ we return from this recursive call
  92. var _nestedCandidates = getCandidatesIteratively(shadowRoot === true ? element.children : shadowRoot.children, true, options);
  93. if (options.flatten) {
  94. candidates.push.apply(candidates, _nestedCandidates);
  95. } else {
  96. candidates.push({
  97. scope: element,
  98. candidates: _nestedCandidates
  99. });
  100. }
  101. } else {
  102. // there's not shadow so just dig into the element's (light dom) children
  103. // __without__ giving the element special scope treatment
  104. elementsToCheck.unshift.apply(elementsToCheck, element.children);
  105. }
  106. }
  107. }
  108. return candidates;
  109. };
  110. var getTabindex = function getTabindex(node, isScope) {
  111. if (node.tabIndex < 0) {
  112. // in Chrome, <details/>, <audio controls/> and <video controls/> elements get a default
  113. // `tabIndex` of -1 when the 'tabindex' attribute isn't specified in the DOM,
  114. // yet they are still part of the regular tab order; in FF, they get a default
  115. // `tabIndex` of 0; since Chrome still puts those elements in the regular tab
  116. // order, consider their tab index to be 0.
  117. // Also browsers do not return `tabIndex` correctly for contentEditable nodes;
  118. // so if they don't have a tabindex attribute specifically set, assume it's 0.
  119. //
  120. // isScope is positive for custom element with shadow root or slot that by default
  121. // have tabIndex -1, but need to be sorted by document order in order for their
  122. // content to be inserted in the correct position
  123. if ((isScope || /^(AUDIO|VIDEO|DETAILS)$/.test(node.tagName) || node.isContentEditable) && isNaN(parseInt(node.getAttribute('tabindex'), 10))) {
  124. return 0;
  125. }
  126. }
  127. return node.tabIndex;
  128. };
  129. var sortOrderedTabbables = function sortOrderedTabbables(a, b) {
  130. return a.tabIndex === b.tabIndex ? a.documentOrder - b.documentOrder : a.tabIndex - b.tabIndex;
  131. };
  132. var isInput = function isInput(node) {
  133. return node.tagName === 'INPUT';
  134. };
  135. var isHiddenInput = function isHiddenInput(node) {
  136. return isInput(node) && node.type === 'hidden';
  137. };
  138. var isDetailsWithSummary = function isDetailsWithSummary(node) {
  139. var r = node.tagName === 'DETAILS' && Array.prototype.slice.apply(node.children).some(function (child) {
  140. return child.tagName === 'SUMMARY';
  141. });
  142. return r;
  143. };
  144. var getCheckedRadio = function getCheckedRadio(nodes, form) {
  145. for (var i = 0; i < nodes.length; i++) {
  146. if (nodes[i].checked && nodes[i].form === form) {
  147. return nodes[i];
  148. }
  149. }
  150. };
  151. var isTabbableRadio = function isTabbableRadio(node) {
  152. if (!node.name) {
  153. return true;
  154. }
  155. var radioScope = node.form || getRootNode(node);
  156. var queryRadios = function queryRadios(name) {
  157. return radioScope.querySelectorAll('input[type="radio"][name="' + name + '"]');
  158. };
  159. var radioSet;
  160. if (typeof window !== 'undefined' && typeof window.CSS !== 'undefined' && typeof window.CSS.escape === 'function') {
  161. radioSet = queryRadios(window.CSS.escape(node.name));
  162. } else {
  163. try {
  164. radioSet = queryRadios(node.name);
  165. } catch (err) {
  166. // eslint-disable-next-line no-console
  167. console.error('Looks like you have a radio button with a name attribute containing invalid CSS selector characters and need the CSS.escape polyfill: %s', err.message);
  168. return false;
  169. }
  170. }
  171. var checked = getCheckedRadio(radioSet, node.form);
  172. return !checked || checked === node;
  173. };
  174. var isRadio = function isRadio(node) {
  175. return isInput(node) && node.type === 'radio';
  176. };
  177. var isNonTabbableRadio = function isNonTabbableRadio(node) {
  178. return isRadio(node) && !isTabbableRadio(node);
  179. };
  180. var isZeroArea = function isZeroArea(node) {
  181. var _node$getBoundingClie = node.getBoundingClientRect(),
  182. width = _node$getBoundingClie.width,
  183. height = _node$getBoundingClie.height;
  184. return width === 0 && height === 0;
  185. };
  186. var isHidden = function isHidden(node, _ref) {
  187. var displayCheck = _ref.displayCheck,
  188. getShadowRoot = _ref.getShadowRoot;
  189. // NOTE: visibility will be `undefined` if node is detached from the document
  190. // (see notes about this further down), which means we will consider it visible
  191. // (this is legacy behavior from a very long way back)
  192. // NOTE: we check this regardless of `displayCheck="none"` because this is a
  193. // _visibility_ check, not a _display_ check
  194. if (getComputedStyle(node).visibility === 'hidden') {
  195. return true;
  196. }
  197. var isDirectSummary = matches.call(node, 'details>summary:first-of-type');
  198. var nodeUnderDetails = isDirectSummary ? node.parentElement : node;
  199. if (matches.call(nodeUnderDetails, 'details:not([open]) *')) {
  200. return true;
  201. } // The root node is the shadow root if the node is in a shadow DOM; some document otherwise
  202. // (but NOT _the_ document; see second 'If' comment below for more).
  203. // If rootNode is shadow root, it'll have a host, which is the element to which the shadow
  204. // is attached, and the one we need to check if it's in the document or not (because the
  205. // shadow, and all nodes it contains, is never considered in the document since shadows
  206. // behave like self-contained DOMs; but if the shadow's HOST, which is part of the document,
  207. // is hidden, or is not in the document itself but is detached, it will affect the shadow's
  208. // visibility, including all the nodes it contains). The host could be any normal node,
  209. // or a custom element (i.e. web component). Either way, that's the one that is considered
  210. // part of the document, not the shadow root, nor any of its children (i.e. the node being
  211. // tested).
  212. // If rootNode is not a shadow root, it won't have a host, and so rootNode should be the
  213. // document (per the docs) and while it's a Document-type object, that document does not
  214. // appear to be the same as the node's `ownerDocument` for some reason, so it's safer
  215. // to ignore the rootNode at this point, and use `node.ownerDocument`. Otherwise,
  216. // using `rootNode.contains(node)` will _always_ be true we'll get false-positives when
  217. // node is actually detached.
  218. var nodeRootHost = getRootNode(node).host;
  219. var nodeIsAttached = (nodeRootHost === null || nodeRootHost === void 0 ? void 0 : nodeRootHost.ownerDocument.contains(nodeRootHost)) || node.ownerDocument.contains(node);
  220. if (!displayCheck || displayCheck === 'full') {
  221. if (typeof getShadowRoot === 'function') {
  222. // figure out if we should consider the node to be in an undisclosed shadow and use the
  223. // 'non-zero-area' fallback
  224. var originalNode = node;
  225. while (node) {
  226. var parentElement = node.parentElement;
  227. var rootNode = getRootNode(node);
  228. if (parentElement && !parentElement.shadowRoot && getShadowRoot(parentElement) === true // check if there's an undisclosed shadow
  229. ) {
  230. // node has an undisclosed shadow which means we can only treat it as a black box, so we
  231. // fall back to a non-zero-area test
  232. return isZeroArea(node);
  233. } else if (node.assignedSlot) {
  234. // iterate up slot
  235. node = node.assignedSlot;
  236. } else if (!parentElement && rootNode !== node.ownerDocument) {
  237. // cross shadow boundary
  238. node = rootNode.host;
  239. } else {
  240. // iterate up normal dom
  241. node = parentElement;
  242. }
  243. }
  244. node = originalNode;
  245. } // else, `getShadowRoot` might be true, but all that does is enable shadow DOM support
  246. // (i.e. it does not also presume that all nodes might have undisclosed shadows); or
  247. // it might be a falsy value, which means shadow DOM support is disabled
  248. // Since we didn't find it sitting in an undisclosed shadow (or shadows are disabled)
  249. // now we can just test to see if it would normally be visible or not, provided it's
  250. // attached to the main document.
  251. // NOTE: We must consider case where node is inside a shadow DOM and given directly to
  252. // `isTabbable()` or `isFocusable()` -- regardless of `getShadowRoot` option setting.
  253. if (nodeIsAttached) {
  254. // this works wherever the node is: if there's at least one client rect, it's
  255. // somehow displayed; it also covers the CSS 'display: contents' case where the
  256. // node itself is hidden in place of its contents; and there's no need to search
  257. // up the hierarchy either
  258. return !node.getClientRects().length;
  259. } // Else, the node isn't attached to the document, which means the `getClientRects()`
  260. // API will __always__ return zero rects (this can happen, for example, if React
  261. // is used to render nodes onto a detached tree, as confirmed in this thread:
  262. // https://github.com/facebook/react/issues/9117#issuecomment-284228870)
  263. //
  264. // It also means that even window.getComputedStyle(node).display will return `undefined`
  265. // because styles are only computed for nodes that are in the document.
  266. //
  267. // NOTE: THIS HAS BEEN THE CASE FOR YEARS. It is not new, nor is it caused by tabbable
  268. // somehow. Though it was never stated officially, anyone who has ever used tabbable
  269. // APIs on nodes in detached containers has actually implicitly used tabbable in what
  270. // was later (as of v5.2.0 on Apr 9, 2021) called `displayCheck="none"` mode -- essentially
  271. // considering __everything__ to be visible because of the innability to determine styles.
  272. } else if (displayCheck === 'non-zero-area') {
  273. // NOTE: Even though this tests that the node's client rect is non-zero to determine
  274. // whether it's displayed, and that a detached node will __always__ have a zero-area
  275. // client rect, we don't special-case for whether the node is attached or not. In
  276. // this mode, we do want to consider nodes that have a zero area to be hidden at all
  277. // times, and that includes attached or not.
  278. return isZeroArea(node);
  279. } // visible, as far as we can tell, or per current `displayCheck` mode
  280. return false;
  281. }; // form fields (nested) inside a disabled fieldset are not focusable/tabbable
  282. // unless they are in the _first_ <legend> element of the top-most disabled
  283. // fieldset
  284. var isDisabledFromFieldset = function isDisabledFromFieldset(node) {
  285. if (/^(INPUT|BUTTON|SELECT|TEXTAREA)$/.test(node.tagName)) {
  286. var parentNode = node.parentElement; // check if `node` is contained in a disabled <fieldset>
  287. while (parentNode) {
  288. if (parentNode.tagName === 'FIELDSET' && parentNode.disabled) {
  289. // look for the first <legend> among the children of the disabled <fieldset>
  290. for (var i = 0; i < parentNode.children.length; i++) {
  291. var child = parentNode.children.item(i); // when the first <legend> (in document order) is found
  292. if (child.tagName === 'LEGEND') {
  293. // if its parent <fieldset> is not nested in another disabled <fieldset>,
  294. // return whether `node` is a descendant of its first <legend>
  295. return matches.call(parentNode, 'fieldset[disabled] *') ? true : !child.contains(node);
  296. }
  297. } // the disabled <fieldset> containing `node` has no <legend>
  298. return true;
  299. }
  300. parentNode = parentNode.parentElement;
  301. }
  302. } // else, node's tabbable/focusable state should not be affected by a fieldset's
  303. // enabled/disabled state
  304. return false;
  305. };
  306. var isNodeMatchingSelectorFocusable = function isNodeMatchingSelectorFocusable(options, node) {
  307. if (node.disabled || isHiddenInput(node) || isHidden(node, options) || // For a details element with a summary, the summary element gets the focus
  308. isDetailsWithSummary(node) || isDisabledFromFieldset(node)) {
  309. return false;
  310. }
  311. return true;
  312. };
  313. var isNodeMatchingSelectorTabbable = function isNodeMatchingSelectorTabbable(options, node) {
  314. if (isNonTabbableRadio(node) || getTabindex(node) < 0 || !isNodeMatchingSelectorFocusable(options, node)) {
  315. return false;
  316. }
  317. return true;
  318. };
  319. var isValidShadowRootTabbable = function isValidShadowRootTabbable(shadowHostNode) {
  320. var tabIndex = parseInt(shadowHostNode.getAttribute('tabindex'), 10);
  321. if (isNaN(tabIndex) || tabIndex >= 0) {
  322. return true;
  323. } // If a custom element has an explicit negative tabindex,
  324. // browsers will not allow tab targeting said element's children.
  325. return false;
  326. };
  327. /**
  328. * @param {Array.<Element|CandidatesScope>} candidates
  329. * @returns Element[]
  330. */
  331. var sortByOrder = function sortByOrder(candidates) {
  332. var regularTabbables = [];
  333. var orderedTabbables = [];
  334. candidates.forEach(function (item, i) {
  335. var isScope = !!item.scope;
  336. var element = isScope ? item.scope : item;
  337. var candidateTabindex = getTabindex(element, isScope);
  338. var elements = isScope ? sortByOrder(item.candidates) : element;
  339. if (candidateTabindex === 0) {
  340. isScope ? regularTabbables.push.apply(regularTabbables, elements) : regularTabbables.push(element);
  341. } else {
  342. orderedTabbables.push({
  343. documentOrder: i,
  344. tabIndex: candidateTabindex,
  345. item: item,
  346. isScope: isScope,
  347. content: elements
  348. });
  349. }
  350. });
  351. return orderedTabbables.sort(sortOrderedTabbables).reduce(function (acc, sortable) {
  352. sortable.isScope ? acc.push.apply(acc, sortable.content) : acc.push(sortable.content);
  353. return acc;
  354. }, []).concat(regularTabbables);
  355. };
  356. var tabbable = function tabbable(el, options) {
  357. options = options || {};
  358. var candidates;
  359. if (options.getShadowRoot) {
  360. candidates = getCandidatesIteratively([el], options.includeContainer, {
  361. filter: isNodeMatchingSelectorTabbable.bind(null, options),
  362. flatten: false,
  363. getShadowRoot: options.getShadowRoot,
  364. shadowRootFilter: isValidShadowRootTabbable
  365. });
  366. } else {
  367. candidates = getCandidates(el, options.includeContainer, isNodeMatchingSelectorTabbable.bind(null, options));
  368. }
  369. return sortByOrder(candidates);
  370. };
  371. var focusable = function focusable(el, options) {
  372. options = options || {};
  373. var candidates;
  374. if (options.getShadowRoot) {
  375. candidates = getCandidatesIteratively([el], options.includeContainer, {
  376. filter: isNodeMatchingSelectorFocusable.bind(null, options),
  377. flatten: true,
  378. getShadowRoot: options.getShadowRoot
  379. });
  380. } else {
  381. candidates = getCandidates(el, options.includeContainer, isNodeMatchingSelectorFocusable.bind(null, options));
  382. }
  383. return candidates;
  384. };
  385. var isTabbable = function isTabbable(node, options) {
  386. options = options || {};
  387. if (!node) {
  388. throw new Error('No node provided');
  389. }
  390. if (matches.call(node, candidateSelector) === false) {
  391. return false;
  392. }
  393. return isNodeMatchingSelectorTabbable(options, node);
  394. };
  395. var focusableCandidateSelector = /* #__PURE__ */candidateSelectors.concat('iframe').join(',');
  396. var isFocusable = function isFocusable(node, options) {
  397. options = options || {};
  398. if (!node) {
  399. throw new Error('No node provided');
  400. }
  401. if (matches.call(node, focusableCandidateSelector) === false) {
  402. return false;
  403. }
  404. return isNodeMatchingSelectorFocusable(options, node);
  405. };
  406. export { focusable, isFocusable, isTabbable, tabbable };
  407. //# sourceMappingURL=index.esm.js.map