nonChromiumPlatformUtils-45c9ed51.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. /*!
  2. * All material copyright ESRI, All Rights Reserved, unless otherwise specified.
  3. * See https://github.com/Esri/calcite-components/blob/master/LICENSE.md for details.
  4. * v1.0.0-beta.97
  5. */
  6. import { e as rectToClientRect } from './floating-ui-5c49748f.js';
  7. import './dom-8f0a9ff2.js';
  8. import './resources-9c476cb6.js';
  9. import './guid-9f15e57a.js';
  10. import './debounce-d85a6654.js';
  11. /**
  12. * This module provides utils to fix positioning across shadow DOM in non-Chromium browsers
  13. *
  14. * It is based on floating-ui's distributable
  15. */
  16. /**
  17. * 👇 the following are needed to fix shadow DOM positioning 👇️
  18. *
  19. * @param element
  20. */
  21. function getTrueOffsetParent(element) {
  22. if (!isHTMLElement(element) || getComputedStyle(element).position === "fixed") {
  23. return null;
  24. }
  25. return composedOffsetParent(element);
  26. }
  27. /**
  28. * Polyfills the old offsetParent behavior from before the spec was changed:
  29. * https://github.com/w3c/csswg-drafts/issues/159
  30. *
  31. * @param element
  32. */
  33. function composedOffsetParent(element) {
  34. let { offsetParent } = element;
  35. let ancestor = element;
  36. let foundInsideSlot = false;
  37. while (ancestor && ancestor !== offsetParent) {
  38. const { assignedSlot } = ancestor;
  39. if (assignedSlot) {
  40. let newOffsetParent = assignedSlot.offsetParent;
  41. if (getComputedStyle(assignedSlot).display === "contents") {
  42. const hadStyleAttribute = assignedSlot.hasAttribute("style");
  43. const oldDisplay = assignedSlot.style.display;
  44. assignedSlot.style.display = getComputedStyle(ancestor).display;
  45. newOffsetParent = assignedSlot.offsetParent;
  46. assignedSlot.style.display = oldDisplay;
  47. if (!hadStyleAttribute) {
  48. assignedSlot.removeAttribute("style");
  49. }
  50. }
  51. ancestor = assignedSlot;
  52. if (offsetParent !== newOffsetParent) {
  53. offsetParent = newOffsetParent;
  54. foundInsideSlot = true;
  55. }
  56. }
  57. else if (isShadowRoot(ancestor) && ancestor.host && foundInsideSlot) {
  58. break;
  59. }
  60. ancestor = (isShadowRoot(ancestor) && ancestor.host) || ancestor.parentNode;
  61. }
  62. return offsetParent;
  63. }
  64. function getElementRects(_ref) {
  65. const { reference, floating, strategy } = _ref;
  66. return {
  67. reference: getRectRelativeToOffsetParent(reference, getOffsetParent(floating), strategy),
  68. floating: { ...getDimensions(floating), x: 0, y: 0 }
  69. };
  70. }
  71. /**
  72. * ☝️ the following are needed to fix shadow DOM positioning ☝️
  73. */
  74. /**
  75. * 👇 the following are taken directly from floating-ui's ESM distributable to support the exports above 👇️
  76. *
  77. * **Notes**:
  78. * unused functions are removed
  79. * ESLint is disabled
  80. * TS-warnings are suppressed
  81. */
  82. /* eslint-disable */
  83. function isWindow(value) {
  84. return value && value.document && value.location && value.alert && value.setInterval;
  85. }
  86. function getWindow(node) {
  87. if (node == null) {
  88. return window;
  89. }
  90. if (!isWindow(node)) {
  91. const ownerDocument = node.ownerDocument;
  92. return ownerDocument ? ownerDocument.defaultView || window : window;
  93. }
  94. return node;
  95. }
  96. function getComputedStyle(element) {
  97. return getWindow(element).getComputedStyle(element);
  98. }
  99. function getNodeName(node) {
  100. return isWindow(node) ? "" : node ? (node.nodeName || "").toLowerCase() : "";
  101. }
  102. function getUAString() {
  103. // @ts-ignore
  104. const uaData = navigator.userAgentData;
  105. if (uaData != null && uaData.brands) {
  106. return uaData.brands.map((item) => item.brand + "/" + item.version).join(" ");
  107. }
  108. return navigator.userAgent;
  109. }
  110. function isHTMLElement(value) {
  111. return value instanceof getWindow(value).HTMLElement;
  112. }
  113. function isElement(value) {
  114. return value instanceof getWindow(value).Element;
  115. }
  116. function isNode(value) {
  117. return value instanceof getWindow(value).Node;
  118. }
  119. function isShadowRoot(node) {
  120. // Browsers without `ShadowRoot` support
  121. if (typeof ShadowRoot === "undefined") {
  122. return false;
  123. }
  124. const OwnElement = getWindow(node).ShadowRoot;
  125. return node instanceof OwnElement || node instanceof ShadowRoot;
  126. }
  127. function isOverflowElement(element) {
  128. // Firefox wants us to check `-x` and `-y` variations as well
  129. const { overflow, overflowX, overflowY, display } = getComputedStyle(element);
  130. return (/auto|scroll|overlay|hidden/.test(overflow + overflowY + overflowX) && !["inline", "contents"].includes(display));
  131. }
  132. function isTableElement(element) {
  133. return ["table", "td", "th"].includes(getNodeName(element));
  134. }
  135. function isContainingBlock(element) {
  136. // TODO: Try and use feature detection here instead
  137. const isFirefox = /firefox/i.test(getUAString());
  138. const css = getComputedStyle(element); // This is non-exhaustive but covers the most common CSS properties that
  139. // create a containing block.
  140. // https://developer.mozilla.org/en-US/docs/Web/CSS/Containing_block#identifying_the_containing_block
  141. return (css.transform !== "none" ||
  142. css.perspective !== "none" ||
  143. (isFirefox && css.willChange === "filter") ||
  144. (isFirefox && (css.filter ? css.filter !== "none" : false)) ||
  145. ["transform", "perspective"].some((value) => css.willChange.includes(value)) ||
  146. ["paint", "layout", "strict", "content"].some(
  147. // TS 4.1 compat
  148. (value) => {
  149. const contain = css.contain;
  150. return contain != null ? contain.includes(value) : false;
  151. }));
  152. }
  153. function isLayoutViewport() {
  154. // Not Safari
  155. return !/^((?!chrome|android).)*safari/i.test(getUAString()); // Feature detection for this fails in various ways
  156. // • Always-visible scrollbar or not
  157. // • Width of <html>, etc.
  158. // const vV = win.visualViewport;
  159. // return vV ? Math.abs(win.innerWidth / vV.scale - vV.width) < 0.5 : true;
  160. }
  161. function isLastTraversableNode(node) {
  162. return ["html", "body", "#document"].includes(getNodeName(node));
  163. }
  164. const min = Math.min;
  165. const max = Math.max;
  166. const round = Math.round;
  167. function getBoundingClientRect(element, includeScale, isFixedStrategy) {
  168. var _win$visualViewport$o, _win$visualViewport, _win$visualViewport$o2, _win$visualViewport2;
  169. if (includeScale === void 0) {
  170. includeScale = false;
  171. }
  172. if (isFixedStrategy === void 0) {
  173. isFixedStrategy = false;
  174. }
  175. const clientRect = element.getBoundingClientRect();
  176. let scaleX = 1;
  177. let scaleY = 1;
  178. if (includeScale && isHTMLElement(element)) {
  179. scaleX = element.offsetWidth > 0 ? round(clientRect.width) / element.offsetWidth || 1 : 1;
  180. scaleY = element.offsetHeight > 0 ? round(clientRect.height) / element.offsetHeight || 1 : 1;
  181. }
  182. const win = isElement(element) ? getWindow(element) : window;
  183. const addVisualOffsets = !isLayoutViewport() && isFixedStrategy;
  184. const x = (clientRect.left +
  185. (addVisualOffsets
  186. ? (_win$visualViewport$o =
  187. (_win$visualViewport = win.visualViewport) == null ? void 0 : _win$visualViewport.offsetLeft) != null
  188. ? _win$visualViewport$o
  189. : 0
  190. : 0)) /
  191. scaleX;
  192. const y = (clientRect.top +
  193. (addVisualOffsets
  194. ? (_win$visualViewport$o2 =
  195. (_win$visualViewport2 = win.visualViewport) == null ? void 0 : _win$visualViewport2.offsetTop) != null
  196. ? _win$visualViewport$o2
  197. : 0
  198. : 0)) /
  199. scaleY;
  200. const width = clientRect.width / scaleX;
  201. const height = clientRect.height / scaleY;
  202. return {
  203. width,
  204. height,
  205. top: y,
  206. right: x + width,
  207. bottom: y + height,
  208. left: x,
  209. x,
  210. y
  211. };
  212. }
  213. function getDocumentElement(node) {
  214. return ((isNode(node) ? node.ownerDocument : node.document) || window.document).documentElement;
  215. }
  216. function getNodeScroll(element) {
  217. if (isElement(element)) {
  218. return {
  219. scrollLeft: element.scrollLeft,
  220. scrollTop: element.scrollTop
  221. };
  222. }
  223. return {
  224. scrollLeft: element.pageXOffset,
  225. scrollTop: element.pageYOffset
  226. };
  227. }
  228. function getWindowScrollBarX(element) {
  229. // If <html> has a CSS width greater than the viewport, then this will be
  230. // incorrect for RTL.
  231. // @ts-ignore
  232. return getBoundingClientRect(getDocumentElement(element)).left + getNodeScroll(element).scrollLeft;
  233. }
  234. function isScaled(element) {
  235. // @ts-ignore
  236. const rect = getBoundingClientRect(element);
  237. return round(rect.width) !== element.offsetWidth || round(rect.height) !== element.offsetHeight;
  238. }
  239. function getRectRelativeToOffsetParent(element, offsetParent, strategy) {
  240. const isOffsetParentAnElement = isHTMLElement(offsetParent);
  241. const documentElement = getDocumentElement(offsetParent);
  242. const rect = getBoundingClientRect(element, // @ts-ignore - checked above (TS 4.1 compat)
  243. isOffsetParentAnElement && isScaled(offsetParent), strategy === "fixed");
  244. let scroll = {
  245. scrollLeft: 0,
  246. scrollTop: 0
  247. };
  248. const offsets = {
  249. x: 0,
  250. y: 0
  251. };
  252. if (isOffsetParentAnElement || (!isOffsetParentAnElement && strategy !== "fixed")) {
  253. if (getNodeName(offsetParent) !== "body" || isOverflowElement(documentElement)) {
  254. scroll = getNodeScroll(offsetParent);
  255. }
  256. if (isHTMLElement(offsetParent)) {
  257. // @ts-ignore
  258. const offsetRect = getBoundingClientRect(offsetParent, true);
  259. offsets.x = offsetRect.x + offsetParent.clientLeft;
  260. offsets.y = offsetRect.y + offsetParent.clientTop;
  261. }
  262. else if (documentElement) {
  263. offsets.x = getWindowScrollBarX(documentElement);
  264. }
  265. }
  266. return {
  267. x: rect.left + scroll.scrollLeft - offsets.x,
  268. y: rect.top + scroll.scrollTop - offsets.y,
  269. width: rect.width,
  270. height: rect.height
  271. };
  272. }
  273. function getParentNode(node) {
  274. if (getNodeName(node) === "html") {
  275. return node;
  276. }
  277. return (
  278. // this is a quicker (but less type safe) way to save quite some bytes from the bundle
  279. // @ts-ignore
  280. node.assignedSlot || // step into the shadow DOM of the parent of a slotted node
  281. node.parentNode || // DOM Element detected
  282. (isShadowRoot(node) ? node.host : null) || // ShadowRoot detected
  283. getDocumentElement(node) // fallback
  284. );
  285. }
  286. function getContainingBlock(element) {
  287. let currentNode = getParentNode(element);
  288. if (isShadowRoot(currentNode)) {
  289. currentNode = currentNode.host;
  290. }
  291. while (isHTMLElement(currentNode) && !isLastTraversableNode(currentNode)) {
  292. if (isContainingBlock(currentNode)) {
  293. return currentNode;
  294. }
  295. else {
  296. const parent = currentNode.parentNode;
  297. currentNode = isShadowRoot(parent) ? parent.host : parent;
  298. }
  299. }
  300. return null;
  301. } // Gets the closest ancestor positioned element. Handles some edge cases,
  302. // such as table ancestors and cross browser bugs.
  303. function getOffsetParent(element) {
  304. const window = getWindow(element);
  305. let offsetParent = getTrueOffsetParent(element);
  306. while (offsetParent && isTableElement(offsetParent) && getComputedStyle(offsetParent).position === "static") {
  307. offsetParent = getTrueOffsetParent(offsetParent);
  308. }
  309. if (offsetParent &&
  310. (getNodeName(offsetParent) === "html" ||
  311. (getNodeName(offsetParent) === "body" &&
  312. getComputedStyle(offsetParent).position === "static" &&
  313. !isContainingBlock(offsetParent)))) {
  314. return window;
  315. }
  316. return offsetParent || getContainingBlock(element) || window;
  317. }
  318. function getDimensions(element) {
  319. if (isHTMLElement(element)) {
  320. return {
  321. width: element.offsetWidth,
  322. height: element.offsetHeight
  323. };
  324. }
  325. // @ts-ignore
  326. const rect = getBoundingClientRect(element);
  327. return {
  328. width: rect.width,
  329. height: rect.height
  330. };
  331. }
  332. function getViewportRect(element, strategy) {
  333. const win = getWindow(element);
  334. const html = getDocumentElement(element);
  335. const visualViewport = win.visualViewport;
  336. let width = html.clientWidth;
  337. let height = html.clientHeight;
  338. let x = 0;
  339. let y = 0;
  340. if (visualViewport) {
  341. width = visualViewport.width;
  342. height = visualViewport.height;
  343. const layoutViewport = isLayoutViewport();
  344. if (layoutViewport || (!layoutViewport && strategy === "fixed")) {
  345. x = visualViewport.offsetLeft;
  346. y = visualViewport.offsetTop;
  347. }
  348. }
  349. return {
  350. width,
  351. height,
  352. x,
  353. y
  354. };
  355. }
  356. // of the `<html>` and `<body>` rect bounds if horizontally scrollable
  357. function getDocumentRect(element) {
  358. var _element$ownerDocumen;
  359. const html = getDocumentElement(element);
  360. const scroll = getNodeScroll(element);
  361. const body = (_element$ownerDocumen = element.ownerDocument) == null ? void 0 : _element$ownerDocumen.body;
  362. const width = max(html.scrollWidth, html.clientWidth, body ? body.scrollWidth : 0, body ? body.clientWidth : 0);
  363. const height = max(html.scrollHeight, html.clientHeight, body ? body.scrollHeight : 0, body ? body.clientHeight : 0);
  364. let x = -scroll.scrollLeft + getWindowScrollBarX(element);
  365. const y = -scroll.scrollTop;
  366. if (getComputedStyle(body || html).direction === "rtl") {
  367. x += max(html.clientWidth, body ? body.clientWidth : 0) - width;
  368. }
  369. return {
  370. width,
  371. height,
  372. x,
  373. y
  374. };
  375. }
  376. function getNearestOverflowAncestor(node) {
  377. const parentNode = getParentNode(node);
  378. if (isLastTraversableNode(parentNode)) {
  379. // @ts-ignore assume body is always available
  380. return node.ownerDocument.body;
  381. }
  382. if (isHTMLElement(parentNode) && isOverflowElement(parentNode)) {
  383. return parentNode;
  384. }
  385. return getNearestOverflowAncestor(parentNode);
  386. }
  387. function getOverflowAncestors(node, list) {
  388. var _node$ownerDocument;
  389. if (list === void 0) {
  390. list = [];
  391. }
  392. const scrollableAncestor = getNearestOverflowAncestor(node);
  393. const isBody = scrollableAncestor === ((_node$ownerDocument = node.ownerDocument) == null ? void 0 : _node$ownerDocument.body);
  394. const win = getWindow(scrollableAncestor);
  395. const target = isBody
  396. ? [win].concat(win.visualViewport || [], isOverflowElement(scrollableAncestor) ? scrollableAncestor : [])
  397. : scrollableAncestor;
  398. const updatedList = list.concat(target);
  399. return isBody
  400. ? updatedList // @ts-ignore: isBody tells us target will be an HTMLElement here
  401. : updatedList.concat(getOverflowAncestors(target));
  402. }
  403. function contains(parent, child) {
  404. const rootNode = child.getRootNode == null ? void 0 : child.getRootNode(); // First, attempt with faster native method
  405. if (parent.contains(child)) {
  406. return true;
  407. } // then fallback to custom implementation with Shadow DOM support
  408. else if (rootNode && isShadowRoot(rootNode)) {
  409. let next = child;
  410. do {
  411. // use `===` replace node.isSameNode()
  412. if (next && parent === next) {
  413. return true;
  414. } // @ts-ignore: need a better way to handle this...
  415. next = next.parentNode || next.host;
  416. } while (next);
  417. }
  418. return false;
  419. }
  420. function getNearestParentCapableOfEscapingClipping(element, clippingAncestors) {
  421. let currentNode = element;
  422. while (currentNode && !isLastTraversableNode(currentNode) && !clippingAncestors.includes(currentNode)) {
  423. if (isElement(currentNode) && ["absolute", "fixed"].includes(getComputedStyle(currentNode).position)) {
  424. break;
  425. }
  426. const parentNode = getParentNode(currentNode);
  427. currentNode = isShadowRoot(parentNode) ? parentNode.host : parentNode;
  428. }
  429. return currentNode;
  430. }
  431. function getInnerBoundingClientRect(element, strategy) {
  432. const clientRect = getBoundingClientRect(element, false, strategy === "fixed");
  433. const top = clientRect.top + element.clientTop;
  434. const left = clientRect.left + element.clientLeft;
  435. return {
  436. top,
  437. left,
  438. x: left,
  439. y: top,
  440. right: left + element.clientWidth,
  441. bottom: top + element.clientHeight,
  442. width: element.clientWidth,
  443. height: element.clientHeight
  444. };
  445. }
  446. function getClientRectFromClippingAncestor(element, clippingParent, strategy) {
  447. if (clippingParent === "viewport") {
  448. return rectToClientRect(getViewportRect(element, strategy));
  449. }
  450. if (isElement(clippingParent)) {
  451. return getInnerBoundingClientRect(clippingParent, strategy);
  452. }
  453. return rectToClientRect(getDocumentRect(getDocumentElement(element)));
  454. } // A "clipping ancestor" is an overflowable container with the characteristic of
  455. // clipping (or hiding) overflowing elements with a position different from
  456. // `initial`
  457. function getClippingAncestors(element) {
  458. // @ts-ignore
  459. const clippingAncestors = getOverflowAncestors(element);
  460. const nearestEscapableParent = getNearestParentCapableOfEscapingClipping(element, clippingAncestors);
  461. let clipperElement = null;
  462. if (nearestEscapableParent && isHTMLElement(nearestEscapableParent)) {
  463. const offsetParent = getOffsetParent(nearestEscapableParent);
  464. if (isOverflowElement(nearestEscapableParent)) {
  465. clipperElement = nearestEscapableParent;
  466. }
  467. else if (isHTMLElement(offsetParent)) {
  468. clipperElement = offsetParent;
  469. }
  470. }
  471. if (!isElement(clipperElement)) {
  472. return [];
  473. } // @ts-ignore isElement check ensures we return Array<Element>
  474. return clippingAncestors.filter((clippingAncestors) => clipperElement &&
  475. isElement(clippingAncestors) &&
  476. contains(clippingAncestors, clipperElement) &&
  477. getNodeName(clippingAncestors) !== "body");
  478. } // Gets the maximum area that the element is visible in due to any number of
  479. // clipping ancestors
  480. function getClippingRect(_ref) {
  481. let { element, boundary, rootBoundary, strategy } = _ref;
  482. const mainClippingAncestors = boundary === "clippingAncestors" ? getClippingAncestors(element) : [].concat(boundary);
  483. const clippingAncestors = [...mainClippingAncestors, rootBoundary];
  484. const firstClippingAncestor = clippingAncestors[0];
  485. const clippingRect = clippingAncestors.reduce((accRect, clippingAncestor) => {
  486. const rect = getClientRectFromClippingAncestor(element, clippingAncestor, strategy);
  487. accRect.top = max(rect.top, accRect.top);
  488. accRect.right = min(rect.right, accRect.right);
  489. accRect.bottom = min(rect.bottom, accRect.bottom);
  490. accRect.left = max(rect.left, accRect.left);
  491. return accRect;
  492. }, getClientRectFromClippingAncestor(element, firstClippingAncestor, strategy));
  493. return {
  494. width: clippingRect.right - clippingRect.left,
  495. height: clippingRect.bottom - clippingRect.top,
  496. x: clippingRect.left,
  497. y: clippingRect.top
  498. };
  499. }
  500. export { getClippingRect, getElementRects, getOffsetParent };