mdc.dom.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. /**
  2. * @license
  3. * Copyright Google LLC All Rights Reserved.
  4. *
  5. * Use of this source code is governed by an MIT-style license that can be
  6. * found in the LICENSE file at https://github.com/material-components/material-components-web/blob/master/LICENSE
  7. */
  8. (function webpackUniversalModuleDefinition(root, factory) {
  9. if(typeof exports === 'object' && typeof module === 'object')
  10. module.exports = factory();
  11. else if(typeof define === 'function' && define.amd)
  12. define([], factory);
  13. else if(typeof exports === 'object')
  14. exports["dom"] = factory();
  15. else
  16. root["mdc"] = root["mdc"] || {}, root["mdc"]["dom"] = factory();
  17. })(this, function() {
  18. return /******/ (function(modules) { // webpackBootstrap
  19. /******/ // The module cache
  20. /******/ var installedModules = {};
  21. /******/
  22. /******/ // The require function
  23. /******/ function __webpack_require__(moduleId) {
  24. /******/
  25. /******/ // Check if module is in cache
  26. /******/ if(installedModules[moduleId]) {
  27. /******/ return installedModules[moduleId].exports;
  28. /******/ }
  29. /******/ // Create a new module (and put it into the cache)
  30. /******/ var module = installedModules[moduleId] = {
  31. /******/ i: moduleId,
  32. /******/ l: false,
  33. /******/ exports: {}
  34. /******/ };
  35. /******/
  36. /******/ // Execute the module function
  37. /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
  38. /******/
  39. /******/ // Flag the module as loaded
  40. /******/ module.l = true;
  41. /******/
  42. /******/ // Return the exports of the module
  43. /******/ return module.exports;
  44. /******/ }
  45. /******/
  46. /******/
  47. /******/ // expose the modules object (__webpack_modules__)
  48. /******/ __webpack_require__.m = modules;
  49. /******/
  50. /******/ // expose the module cache
  51. /******/ __webpack_require__.c = installedModules;
  52. /******/
  53. /******/ // define getter function for harmony exports
  54. /******/ __webpack_require__.d = function(exports, name, getter) {
  55. /******/ if(!__webpack_require__.o(exports, name)) {
  56. /******/ Object.defineProperty(exports, name, { enumerable: true, get: getter });
  57. /******/ }
  58. /******/ };
  59. /******/
  60. /******/ // define __esModule on exports
  61. /******/ __webpack_require__.r = function(exports) {
  62. /******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
  63. /******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
  64. /******/ }
  65. /******/ Object.defineProperty(exports, '__esModule', { value: true });
  66. /******/ };
  67. /******/
  68. /******/ // create a fake namespace object
  69. /******/ // mode & 1: value is a module id, require it
  70. /******/ // mode & 2: merge all properties of value into the ns
  71. /******/ // mode & 4: return value when already ns object
  72. /******/ // mode & 8|1: behave like require
  73. /******/ __webpack_require__.t = function(value, mode) {
  74. /******/ if(mode & 1) value = __webpack_require__(value);
  75. /******/ if(mode & 8) return value;
  76. /******/ if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value;
  77. /******/ var ns = Object.create(null);
  78. /******/ __webpack_require__.r(ns);
  79. /******/ Object.defineProperty(ns, 'default', { enumerable: true, value: value });
  80. /******/ if(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key));
  81. /******/ return ns;
  82. /******/ };
  83. /******/
  84. /******/ // getDefaultExport function for compatibility with non-harmony modules
  85. /******/ __webpack_require__.n = function(module) {
  86. /******/ var getter = module && module.__esModule ?
  87. /******/ function getDefault() { return module['default']; } :
  88. /******/ function getModuleExports() { return module; };
  89. /******/ __webpack_require__.d(getter, 'a', getter);
  90. /******/ return getter;
  91. /******/ };
  92. /******/
  93. /******/ // Object.prototype.hasOwnProperty.call
  94. /******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
  95. /******/
  96. /******/ // __webpack_public_path__
  97. /******/ __webpack_require__.p = "";
  98. /******/
  99. /******/
  100. /******/ // Load entry module and return exports
  101. /******/ return __webpack_require__(__webpack_require__.s = "./packages/mdc-dom/index.ts");
  102. /******/ })
  103. /************************************************************************/
  104. /******/ ({
  105. /***/ "./packages/mdc-dom/events.ts":
  106. /*!************************************!*\
  107. !*** ./packages/mdc-dom/events.ts ***!
  108. \************************************/
  109. /*! no static exports found */
  110. /***/ (function(module, exports, __webpack_require__) {
  111. "use strict";
  112. /**
  113. * @license
  114. * Copyright 2019 Google Inc.
  115. *
  116. * Permission is hereby granted, free of charge, to any person obtaining a copy
  117. * of this software and associated documentation files (the "Software"), to deal
  118. * in the Software without restriction, including without limitation the rights
  119. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  120. * copies of the Software, and to permit persons to whom the Software is
  121. * furnished to do so, subject to the following conditions:
  122. *
  123. * The above copyright notice and this permission notice shall be included in
  124. * all copies or substantial portions of the Software.
  125. *
  126. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  127. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  128. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  129. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  130. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  131. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  132. * THE SOFTWARE.
  133. */
  134. Object.defineProperty(exports, "__esModule", { value: true });
  135. /**
  136. * Determine whether the current browser supports passive event listeners, and
  137. * if so, use them.
  138. */
  139. function applyPassive(globalObj) {
  140. if (globalObj === void 0) {
  141. globalObj = window;
  142. }
  143. return supportsPassiveOption(globalObj) ? { passive: true } : false;
  144. }
  145. exports.applyPassive = applyPassive;
  146. function supportsPassiveOption(globalObj) {
  147. if (globalObj === void 0) {
  148. globalObj = window;
  149. }
  150. // See
  151. // https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener
  152. var passiveSupported = false;
  153. try {
  154. var options = {
  155. // This function will be called when the browser
  156. // attempts to access the passive property.
  157. get passive() {
  158. passiveSupported = true;
  159. return false;
  160. }
  161. };
  162. var handler = function handler() {};
  163. globalObj.document.addEventListener('test', handler, options);
  164. globalObj.document.removeEventListener('test', handler, options);
  165. } catch (err) {
  166. passiveSupported = false;
  167. }
  168. return passiveSupported;
  169. }
  170. /***/ }),
  171. /***/ "./packages/mdc-dom/focus-trap.ts":
  172. /*!****************************************!*\
  173. !*** ./packages/mdc-dom/focus-trap.ts ***!
  174. \****************************************/
  175. /*! no static exports found */
  176. /***/ (function(module, exports, __webpack_require__) {
  177. "use strict";
  178. /**
  179. * @license
  180. * Copyright 2020 Google Inc.
  181. *
  182. * Permission is hereby granted, free of charge, to any person obtaining a copy
  183. * of this software and associated documentation files (the "Software"), to deal
  184. * in the Software without restriction, including without limitation the rights
  185. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  186. * copies of the Software, and to permit persons to whom the Software is
  187. * furnished to do so, subject to the following conditions:
  188. *
  189. * The above copyright notice and this permission notice shall be included in
  190. * all copies or substantial portions of the Software.
  191. *
  192. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  193. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  194. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  195. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  196. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  197. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  198. * THE SOFTWARE.
  199. */
  200. Object.defineProperty(exports, "__esModule", { value: true });
  201. var FOCUS_SENTINEL_CLASS = 'mdc-dom-focus-sentinel';
  202. /**
  203. * Utility to trap focus in a given root element, e.g. for modal components such
  204. * as dialogs. The root should have at least one focusable child element,
  205. * for setting initial focus when trapping focus.
  206. * Also tracks the previously focused element, and restores focus to that
  207. * element when releasing focus.
  208. */
  209. var FocusTrap = /** @class */function () {
  210. function FocusTrap(root, options) {
  211. if (options === void 0) {
  212. options = {};
  213. }
  214. this.root = root;
  215. this.options = options;
  216. // Previously focused element before trapping focus.
  217. this.elFocusedBeforeTrapFocus = null;
  218. }
  219. /**
  220. * Traps focus in `root`. Also focuses on either `initialFocusEl` if set;
  221. * otherwises sets initial focus to the first focusable child element.
  222. */
  223. FocusTrap.prototype.trapFocus = function () {
  224. var focusableEls = this.getFocusableElements(this.root);
  225. if (focusableEls.length === 0) {
  226. throw new Error('FocusTrap: Element must have at least one focusable child.');
  227. }
  228. this.elFocusedBeforeTrapFocus = document.activeElement instanceof HTMLElement ? document.activeElement : null;
  229. this.wrapTabFocus(this.root, focusableEls);
  230. if (!this.options.skipInitialFocus) {
  231. this.focusInitialElement(focusableEls, this.options.initialFocusEl);
  232. }
  233. };
  234. /**
  235. * Releases focus from `root`. Also restores focus to the previously focused
  236. * element.
  237. */
  238. FocusTrap.prototype.releaseFocus = function () {
  239. [].slice.call(this.root.querySelectorAll("." + FOCUS_SENTINEL_CLASS)).forEach(function (sentinelEl) {
  240. sentinelEl.parentElement.removeChild(sentinelEl);
  241. });
  242. if (this.elFocusedBeforeTrapFocus) {
  243. this.elFocusedBeforeTrapFocus.focus();
  244. }
  245. };
  246. /**
  247. * Wraps tab focus within `el` by adding two hidden sentinel divs which are
  248. * used to mark the beginning and the end of the tabbable region. When
  249. * focused, these sentinel elements redirect focus to the first/last
  250. * children elements of the tabbable region, ensuring that focus is trapped
  251. * within that region.
  252. */
  253. FocusTrap.prototype.wrapTabFocus = function (el, focusableEls) {
  254. var sentinelStart = this.createSentinel();
  255. var sentinelEnd = this.createSentinel();
  256. sentinelStart.addEventListener('focus', function () {
  257. if (focusableEls.length > 0) {
  258. focusableEls[focusableEls.length - 1].focus();
  259. }
  260. });
  261. sentinelEnd.addEventListener('focus', function () {
  262. if (focusableEls.length > 0) {
  263. focusableEls[0].focus();
  264. }
  265. });
  266. el.insertBefore(sentinelStart, el.children[0]);
  267. el.appendChild(sentinelEnd);
  268. };
  269. /**
  270. * Focuses on `initialFocusEl` if defined and a child of the root element.
  271. * Otherwise, focuses on the first focusable child element of the root.
  272. */
  273. FocusTrap.prototype.focusInitialElement = function (focusableEls, initialFocusEl) {
  274. var focusIndex = 0;
  275. if (initialFocusEl) {
  276. focusIndex = Math.max(focusableEls.indexOf(initialFocusEl), 0);
  277. }
  278. focusableEls[focusIndex].focus();
  279. };
  280. FocusTrap.prototype.getFocusableElements = function (root) {
  281. var focusableEls = [].slice.call(root.querySelectorAll('[autofocus], [tabindex], a, input, textarea, select, button'));
  282. return focusableEls.filter(function (el) {
  283. var isDisabledOrHidden = el.getAttribute('aria-disabled') === 'true' || el.getAttribute('disabled') != null || el.getAttribute('hidden') != null || el.getAttribute('aria-hidden') === 'true';
  284. var isTabbableAndVisible = el.tabIndex >= 0 && el.getBoundingClientRect().width > 0 && !el.classList.contains(FOCUS_SENTINEL_CLASS) && !isDisabledOrHidden;
  285. var isProgrammaticallyHidden = false;
  286. if (isTabbableAndVisible) {
  287. var style = getComputedStyle(el);
  288. isProgrammaticallyHidden = style.display === 'none' || style.visibility === 'hidden';
  289. }
  290. return isTabbableAndVisible && !isProgrammaticallyHidden;
  291. });
  292. };
  293. FocusTrap.prototype.createSentinel = function () {
  294. var sentinel = document.createElement('div');
  295. sentinel.setAttribute('tabindex', '0');
  296. // Don't announce in screen readers.
  297. sentinel.setAttribute('aria-hidden', 'true');
  298. sentinel.classList.add(FOCUS_SENTINEL_CLASS);
  299. return sentinel;
  300. };
  301. return FocusTrap;
  302. }();
  303. exports.FocusTrap = FocusTrap;
  304. /***/ }),
  305. /***/ "./packages/mdc-dom/index.ts":
  306. /*!***********************************!*\
  307. !*** ./packages/mdc-dom/index.ts ***!
  308. \***********************************/
  309. /*! no static exports found */
  310. /***/ (function(module, exports, __webpack_require__) {
  311. "use strict";
  312. /**
  313. * @license
  314. * Copyright 2018 Google Inc.
  315. *
  316. * Permission is hereby granted, free of charge, to any person obtaining a copy
  317. * of this software and associated documentation files (the "Software"), to deal
  318. * in the Software without restriction, including without limitation the rights
  319. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  320. * copies of the Software, and to permit persons to whom the Software is
  321. * furnished to do so, subject to the following conditions:
  322. *
  323. * The above copyright notice and this permission notice shall be included in
  324. * all copies or substantial portions of the Software.
  325. *
  326. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  327. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  328. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  329. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  330. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  331. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  332. * THE SOFTWARE.
  333. */
  334. var __importStar = this && this.__importStar || function (mod) {
  335. if (mod && mod.__esModule) return mod;
  336. var result = {};
  337. if (mod != null) for (var k in mod) {
  338. if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
  339. }result["default"] = mod;
  340. return result;
  341. };
  342. Object.defineProperty(exports, "__esModule", { value: true });
  343. var events = __importStar(__webpack_require__(/*! ./events */ "./packages/mdc-dom/events.ts"));
  344. exports.events = events;
  345. var focusTrap = __importStar(__webpack_require__(/*! ./focus-trap */ "./packages/mdc-dom/focus-trap.ts"));
  346. exports.focusTrap = focusTrap;
  347. var ponyfill = __importStar(__webpack_require__(/*! ./ponyfill */ "./packages/mdc-dom/ponyfill.ts"));
  348. exports.ponyfill = ponyfill;
  349. /***/ }),
  350. /***/ "./packages/mdc-dom/ponyfill.ts":
  351. /*!**************************************!*\
  352. !*** ./packages/mdc-dom/ponyfill.ts ***!
  353. \**************************************/
  354. /*! no static exports found */
  355. /***/ (function(module, exports, __webpack_require__) {
  356. "use strict";
  357. /**
  358. * @license
  359. * Copyright 2018 Google Inc.
  360. *
  361. * Permission is hereby granted, free of charge, to any person obtaining a copy
  362. * of this software and associated documentation files (the "Software"), to deal
  363. * in the Software without restriction, including without limitation the rights
  364. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  365. * copies of the Software, and to permit persons to whom the Software is
  366. * furnished to do so, subject to the following conditions:
  367. *
  368. * The above copyright notice and this permission notice shall be included in
  369. * all copies or substantial portions of the Software.
  370. *
  371. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  372. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  373. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  374. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  375. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  376. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  377. * THE SOFTWARE.
  378. */
  379. Object.defineProperty(exports, "__esModule", { value: true });
  380. /**
  381. * @fileoverview A "ponyfill" is a polyfill that doesn't modify the global prototype chain.
  382. * This makes ponyfills safer than traditional polyfills, especially for libraries like MDC.
  383. */
  384. function closest(element, selector) {
  385. if (element.closest) {
  386. return element.closest(selector);
  387. }
  388. var el = element;
  389. while (el) {
  390. if (matches(el, selector)) {
  391. return el;
  392. }
  393. el = el.parentElement;
  394. }
  395. return null;
  396. }
  397. exports.closest = closest;
  398. function matches(element, selector) {
  399. var nativeMatches = element.matches || element.webkitMatchesSelector || element.msMatchesSelector;
  400. return nativeMatches.call(element, selector);
  401. }
  402. exports.matches = matches;
  403. /**
  404. * Used to compute the estimated scroll width of elements. When an element is
  405. * hidden due to display: none; being applied to a parent element, the width is
  406. * returned as 0. However, the element will have a true width once no longer
  407. * inside a display: none context. This method computes an estimated width when
  408. * the element is hidden or returns the true width when the element is visble.
  409. * @param {Element} element the element whose width to estimate
  410. */
  411. function estimateScrollWidth(element) {
  412. // Check the offsetParent. If the element inherits display: none from any
  413. // parent, the offsetParent property will be null (see
  414. // https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/offsetParent).
  415. // This check ensures we only clone the node when necessary.
  416. var htmlEl = element;
  417. if (htmlEl.offsetParent !== null) {
  418. return htmlEl.scrollWidth;
  419. }
  420. var clone = htmlEl.cloneNode(true);
  421. clone.style.setProperty('position', 'absolute');
  422. clone.style.setProperty('transform', 'translate(-9999px, -9999px)');
  423. document.documentElement.appendChild(clone);
  424. var scrollWidth = clone.scrollWidth;
  425. document.documentElement.removeChild(clone);
  426. return scrollWidth;
  427. }
  428. exports.estimateScrollWidth = estimateScrollWidth;
  429. /***/ })
  430. /******/ });
  431. });
  432. //# sourceMappingURL=mdc.dom.js.map