nonChromiumPlatformUtils.js 17 KB

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