popper-base.js 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103
  1. /**
  2. * @popperjs/core v2.11.6 - MIT License
  3. */
  4. (function (global, factory) {
  5. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
  6. typeof define === 'function' && define.amd ? define(['exports'], factory) :
  7. (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.Popper = {}));
  8. }(this, (function (exports) { 'use strict';
  9. function getWindow(node) {
  10. if (node == null) {
  11. return window;
  12. }
  13. if (node.toString() !== '[object Window]') {
  14. var ownerDocument = node.ownerDocument;
  15. return ownerDocument ? ownerDocument.defaultView || window : window;
  16. }
  17. return node;
  18. }
  19. function isElement(node) {
  20. var OwnElement = getWindow(node).Element;
  21. return node instanceof OwnElement || node instanceof Element;
  22. }
  23. function isHTMLElement(node) {
  24. var OwnElement = getWindow(node).HTMLElement;
  25. return node instanceof OwnElement || node instanceof HTMLElement;
  26. }
  27. function isShadowRoot(node) {
  28. // IE 11 has no ShadowRoot
  29. if (typeof ShadowRoot === 'undefined') {
  30. return false;
  31. }
  32. var OwnElement = getWindow(node).ShadowRoot;
  33. return node instanceof OwnElement || node instanceof ShadowRoot;
  34. }
  35. var max = Math.max;
  36. var min = Math.min;
  37. var round = Math.round;
  38. function getUAString() {
  39. var uaData = navigator.userAgentData;
  40. if (uaData != null && uaData.brands) {
  41. return uaData.brands.map(function (item) {
  42. return item.brand + "/" + item.version;
  43. }).join(' ');
  44. }
  45. return navigator.userAgent;
  46. }
  47. function isLayoutViewport() {
  48. return !/^((?!chrome|android).)*safari/i.test(getUAString());
  49. }
  50. function getBoundingClientRect(element, includeScale, isFixedStrategy) {
  51. if (includeScale === void 0) {
  52. includeScale = false;
  53. }
  54. if (isFixedStrategy === void 0) {
  55. isFixedStrategy = false;
  56. }
  57. var clientRect = element.getBoundingClientRect();
  58. var scaleX = 1;
  59. var scaleY = 1;
  60. if (includeScale && isHTMLElement(element)) {
  61. scaleX = element.offsetWidth > 0 ? round(clientRect.width) / element.offsetWidth || 1 : 1;
  62. scaleY = element.offsetHeight > 0 ? round(clientRect.height) / element.offsetHeight || 1 : 1;
  63. }
  64. var _ref = isElement(element) ? getWindow(element) : window,
  65. visualViewport = _ref.visualViewport;
  66. var addVisualOffsets = !isLayoutViewport() && isFixedStrategy;
  67. var x = (clientRect.left + (addVisualOffsets && visualViewport ? visualViewport.offsetLeft : 0)) / scaleX;
  68. var y = (clientRect.top + (addVisualOffsets && visualViewport ? visualViewport.offsetTop : 0)) / scaleY;
  69. var width = clientRect.width / scaleX;
  70. var height = clientRect.height / scaleY;
  71. return {
  72. width: width,
  73. height: height,
  74. top: y,
  75. right: x + width,
  76. bottom: y + height,
  77. left: x,
  78. x: x,
  79. y: y
  80. };
  81. }
  82. function getWindowScroll(node) {
  83. var win = getWindow(node);
  84. var scrollLeft = win.pageXOffset;
  85. var scrollTop = win.pageYOffset;
  86. return {
  87. scrollLeft: scrollLeft,
  88. scrollTop: scrollTop
  89. };
  90. }
  91. function getHTMLElementScroll(element) {
  92. return {
  93. scrollLeft: element.scrollLeft,
  94. scrollTop: element.scrollTop
  95. };
  96. }
  97. function getNodeScroll(node) {
  98. if (node === getWindow(node) || !isHTMLElement(node)) {
  99. return getWindowScroll(node);
  100. } else {
  101. return getHTMLElementScroll(node);
  102. }
  103. }
  104. function getNodeName(element) {
  105. return element ? (element.nodeName || '').toLowerCase() : null;
  106. }
  107. function getDocumentElement(element) {
  108. // $FlowFixMe[incompatible-return]: assume body is always available
  109. return ((isElement(element) ? element.ownerDocument : // $FlowFixMe[prop-missing]
  110. element.document) || window.document).documentElement;
  111. }
  112. function getWindowScrollBarX(element) {
  113. // If <html> has a CSS width greater than the viewport, then this will be
  114. // incorrect for RTL.
  115. // Popper 1 is broken in this case and never had a bug report so let's assume
  116. // it's not an issue. I don't think anyone ever specifies width on <html>
  117. // anyway.
  118. // Browsers where the left scrollbar doesn't cause an issue report `0` for
  119. // this (e.g. Edge 2019, IE11, Safari)
  120. return getBoundingClientRect(getDocumentElement(element)).left + getWindowScroll(element).scrollLeft;
  121. }
  122. function getComputedStyle(element) {
  123. return getWindow(element).getComputedStyle(element);
  124. }
  125. function isScrollParent(element) {
  126. // Firefox wants us to check `-x` and `-y` variations as well
  127. var _getComputedStyle = getComputedStyle(element),
  128. overflow = _getComputedStyle.overflow,
  129. overflowX = _getComputedStyle.overflowX,
  130. overflowY = _getComputedStyle.overflowY;
  131. return /auto|scroll|overlay|hidden/.test(overflow + overflowY + overflowX);
  132. }
  133. function isElementScaled(element) {
  134. var rect = element.getBoundingClientRect();
  135. var scaleX = round(rect.width) / element.offsetWidth || 1;
  136. var scaleY = round(rect.height) / element.offsetHeight || 1;
  137. return scaleX !== 1 || scaleY !== 1;
  138. } // Returns the composite rect of an element relative to its offsetParent.
  139. // Composite means it takes into account transforms as well as layout.
  140. function getCompositeRect(elementOrVirtualElement, offsetParent, isFixed) {
  141. if (isFixed === void 0) {
  142. isFixed = false;
  143. }
  144. var isOffsetParentAnElement = isHTMLElement(offsetParent);
  145. var offsetParentIsScaled = isHTMLElement(offsetParent) && isElementScaled(offsetParent);
  146. var documentElement = getDocumentElement(offsetParent);
  147. var rect = getBoundingClientRect(elementOrVirtualElement, offsetParentIsScaled, isFixed);
  148. var scroll = {
  149. scrollLeft: 0,
  150. scrollTop: 0
  151. };
  152. var offsets = {
  153. x: 0,
  154. y: 0
  155. };
  156. if (isOffsetParentAnElement || !isOffsetParentAnElement && !isFixed) {
  157. if (getNodeName(offsetParent) !== 'body' || // https://github.com/popperjs/popper-core/issues/1078
  158. isScrollParent(documentElement)) {
  159. scroll = getNodeScroll(offsetParent);
  160. }
  161. if (isHTMLElement(offsetParent)) {
  162. offsets = getBoundingClientRect(offsetParent, true);
  163. offsets.x += offsetParent.clientLeft;
  164. offsets.y += offsetParent.clientTop;
  165. } else if (documentElement) {
  166. offsets.x = getWindowScrollBarX(documentElement);
  167. }
  168. }
  169. return {
  170. x: rect.left + scroll.scrollLeft - offsets.x,
  171. y: rect.top + scroll.scrollTop - offsets.y,
  172. width: rect.width,
  173. height: rect.height
  174. };
  175. }
  176. // means it doesn't take into account transforms.
  177. function getLayoutRect(element) {
  178. var clientRect = getBoundingClientRect(element); // Use the clientRect sizes if it's not been transformed.
  179. // Fixes https://github.com/popperjs/popper-core/issues/1223
  180. var width = element.offsetWidth;
  181. var height = element.offsetHeight;
  182. if (Math.abs(clientRect.width - width) <= 1) {
  183. width = clientRect.width;
  184. }
  185. if (Math.abs(clientRect.height - height) <= 1) {
  186. height = clientRect.height;
  187. }
  188. return {
  189. x: element.offsetLeft,
  190. y: element.offsetTop,
  191. width: width,
  192. height: height
  193. };
  194. }
  195. function getParentNode(element) {
  196. if (getNodeName(element) === 'html') {
  197. return element;
  198. }
  199. return (// this is a quicker (but less type safe) way to save quite some bytes from the bundle
  200. // $FlowFixMe[incompatible-return]
  201. // $FlowFixMe[prop-missing]
  202. element.assignedSlot || // step into the shadow DOM of the parent of a slotted node
  203. element.parentNode || ( // DOM Element detected
  204. isShadowRoot(element) ? element.host : null) || // ShadowRoot detected
  205. // $FlowFixMe[incompatible-call]: HTMLElement is a Node
  206. getDocumentElement(element) // fallback
  207. );
  208. }
  209. function getScrollParent(node) {
  210. if (['html', 'body', '#document'].indexOf(getNodeName(node)) >= 0) {
  211. // $FlowFixMe[incompatible-return]: assume body is always available
  212. return node.ownerDocument.body;
  213. }
  214. if (isHTMLElement(node) && isScrollParent(node)) {
  215. return node;
  216. }
  217. return getScrollParent(getParentNode(node));
  218. }
  219. /*
  220. given a DOM element, return the list of all scroll parents, up the list of ancesors
  221. until we get to the top window object. This list is what we attach scroll listeners
  222. to, because if any of these parent elements scroll, we'll need to re-calculate the
  223. reference element's position.
  224. */
  225. function listScrollParents(element, list) {
  226. var _element$ownerDocumen;
  227. if (list === void 0) {
  228. list = [];
  229. }
  230. var scrollParent = getScrollParent(element);
  231. var isBody = scrollParent === ((_element$ownerDocumen = element.ownerDocument) == null ? void 0 : _element$ownerDocumen.body);
  232. var win = getWindow(scrollParent);
  233. var target = isBody ? [win].concat(win.visualViewport || [], isScrollParent(scrollParent) ? scrollParent : []) : scrollParent;
  234. var updatedList = list.concat(target);
  235. return isBody ? updatedList : // $FlowFixMe[incompatible-call]: isBody tells us target will be an HTMLElement here
  236. updatedList.concat(listScrollParents(getParentNode(target)));
  237. }
  238. function isTableElement(element) {
  239. return ['table', 'td', 'th'].indexOf(getNodeName(element)) >= 0;
  240. }
  241. function getTrueOffsetParent(element) {
  242. if (!isHTMLElement(element) || // https://github.com/popperjs/popper-core/issues/837
  243. getComputedStyle(element).position === 'fixed') {
  244. return null;
  245. }
  246. return element.offsetParent;
  247. } // `.offsetParent` reports `null` for fixed elements, while absolute elements
  248. // return the containing block
  249. function getContainingBlock(element) {
  250. var isFirefox = /firefox/i.test(getUAString());
  251. var isIE = /Trident/i.test(getUAString());
  252. if (isIE && isHTMLElement(element)) {
  253. // In IE 9, 10 and 11 fixed elements containing block is always established by the viewport
  254. var elementCss = getComputedStyle(element);
  255. if (elementCss.position === 'fixed') {
  256. return null;
  257. }
  258. }
  259. var currentNode = getParentNode(element);
  260. if (isShadowRoot(currentNode)) {
  261. currentNode = currentNode.host;
  262. }
  263. while (isHTMLElement(currentNode) && ['html', 'body'].indexOf(getNodeName(currentNode)) < 0) {
  264. var css = getComputedStyle(currentNode); // This is non-exhaustive but covers the most common CSS properties that
  265. // create a containing block.
  266. // https://developer.mozilla.org/en-US/docs/Web/CSS/Containing_block#identifying_the_containing_block
  267. if (css.transform !== 'none' || css.perspective !== 'none' || css.contain === 'paint' || ['transform', 'perspective'].indexOf(css.willChange) !== -1 || isFirefox && css.willChange === 'filter' || isFirefox && css.filter && css.filter !== 'none') {
  268. return currentNode;
  269. } else {
  270. currentNode = currentNode.parentNode;
  271. }
  272. }
  273. return null;
  274. } // Gets the closest ancestor positioned element. Handles some edge cases,
  275. // such as table ancestors and cross browser bugs.
  276. function getOffsetParent(element) {
  277. var window = getWindow(element);
  278. var offsetParent = getTrueOffsetParent(element);
  279. while (offsetParent && isTableElement(offsetParent) && getComputedStyle(offsetParent).position === 'static') {
  280. offsetParent = getTrueOffsetParent(offsetParent);
  281. }
  282. if (offsetParent && (getNodeName(offsetParent) === 'html' || getNodeName(offsetParent) === 'body' && getComputedStyle(offsetParent).position === 'static')) {
  283. return window;
  284. }
  285. return offsetParent || getContainingBlock(element) || window;
  286. }
  287. var top = 'top';
  288. var bottom = 'bottom';
  289. var right = 'right';
  290. var left = 'left';
  291. var auto = 'auto';
  292. var basePlacements = [top, bottom, right, left];
  293. var start = 'start';
  294. var end = 'end';
  295. var clippingParents = 'clippingParents';
  296. var viewport = 'viewport';
  297. var popper = 'popper';
  298. var reference = 'reference';
  299. var beforeRead = 'beforeRead';
  300. var read = 'read';
  301. var afterRead = 'afterRead'; // pure-logic modifiers
  302. var beforeMain = 'beforeMain';
  303. var main = 'main';
  304. var afterMain = 'afterMain'; // modifier with the purpose to write to the DOM (or write into a framework state)
  305. var beforeWrite = 'beforeWrite';
  306. var write = 'write';
  307. var afterWrite = 'afterWrite';
  308. var modifierPhases = [beforeRead, read, afterRead, beforeMain, main, afterMain, beforeWrite, write, afterWrite];
  309. function order(modifiers) {
  310. var map = new Map();
  311. var visited = new Set();
  312. var result = [];
  313. modifiers.forEach(function (modifier) {
  314. map.set(modifier.name, modifier);
  315. }); // On visiting object, check for its dependencies and visit them recursively
  316. function sort(modifier) {
  317. visited.add(modifier.name);
  318. var requires = [].concat(modifier.requires || [], modifier.requiresIfExists || []);
  319. requires.forEach(function (dep) {
  320. if (!visited.has(dep)) {
  321. var depModifier = map.get(dep);
  322. if (depModifier) {
  323. sort(depModifier);
  324. }
  325. }
  326. });
  327. result.push(modifier);
  328. }
  329. modifiers.forEach(function (modifier) {
  330. if (!visited.has(modifier.name)) {
  331. // check for visited object
  332. sort(modifier);
  333. }
  334. });
  335. return result;
  336. }
  337. function orderModifiers(modifiers) {
  338. // order based on dependencies
  339. var orderedModifiers = order(modifiers); // order based on phase
  340. return modifierPhases.reduce(function (acc, phase) {
  341. return acc.concat(orderedModifiers.filter(function (modifier) {
  342. return modifier.phase === phase;
  343. }));
  344. }, []);
  345. }
  346. function debounce(fn) {
  347. var pending;
  348. return function () {
  349. if (!pending) {
  350. pending = new Promise(function (resolve) {
  351. Promise.resolve().then(function () {
  352. pending = undefined;
  353. resolve(fn());
  354. });
  355. });
  356. }
  357. return pending;
  358. };
  359. }
  360. function format(str) {
  361. for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
  362. args[_key - 1] = arguments[_key];
  363. }
  364. return [].concat(args).reduce(function (p, c) {
  365. return p.replace(/%s/, c);
  366. }, str);
  367. }
  368. var INVALID_MODIFIER_ERROR = 'Popper: modifier "%s" provided an invalid %s property, expected %s but got %s';
  369. var MISSING_DEPENDENCY_ERROR = 'Popper: modifier "%s" requires "%s", but "%s" modifier is not available';
  370. var VALID_PROPERTIES = ['name', 'enabled', 'phase', 'fn', 'effect', 'requires', 'options'];
  371. function validateModifiers(modifiers) {
  372. modifiers.forEach(function (modifier) {
  373. [].concat(Object.keys(modifier), VALID_PROPERTIES) // IE11-compatible replacement for `new Set(iterable)`
  374. .filter(function (value, index, self) {
  375. return self.indexOf(value) === index;
  376. }).forEach(function (key) {
  377. switch (key) {
  378. case 'name':
  379. if (typeof modifier.name !== 'string') {
  380. console.error(format(INVALID_MODIFIER_ERROR, String(modifier.name), '"name"', '"string"', "\"" + String(modifier.name) + "\""));
  381. }
  382. break;
  383. case 'enabled':
  384. if (typeof modifier.enabled !== 'boolean') {
  385. console.error(format(INVALID_MODIFIER_ERROR, modifier.name, '"enabled"', '"boolean"', "\"" + String(modifier.enabled) + "\""));
  386. }
  387. break;
  388. case 'phase':
  389. if (modifierPhases.indexOf(modifier.phase) < 0) {
  390. console.error(format(INVALID_MODIFIER_ERROR, modifier.name, '"phase"', "either " + modifierPhases.join(', '), "\"" + String(modifier.phase) + "\""));
  391. }
  392. break;
  393. case 'fn':
  394. if (typeof modifier.fn !== 'function') {
  395. console.error(format(INVALID_MODIFIER_ERROR, modifier.name, '"fn"', '"function"', "\"" + String(modifier.fn) + "\""));
  396. }
  397. break;
  398. case 'effect':
  399. if (modifier.effect != null && typeof modifier.effect !== 'function') {
  400. console.error(format(INVALID_MODIFIER_ERROR, modifier.name, '"effect"', '"function"', "\"" + String(modifier.fn) + "\""));
  401. }
  402. break;
  403. case 'requires':
  404. if (modifier.requires != null && !Array.isArray(modifier.requires)) {
  405. console.error(format(INVALID_MODIFIER_ERROR, modifier.name, '"requires"', '"array"', "\"" + String(modifier.requires) + "\""));
  406. }
  407. break;
  408. case 'requiresIfExists':
  409. if (!Array.isArray(modifier.requiresIfExists)) {
  410. console.error(format(INVALID_MODIFIER_ERROR, modifier.name, '"requiresIfExists"', '"array"', "\"" + String(modifier.requiresIfExists) + "\""));
  411. }
  412. break;
  413. case 'options':
  414. case 'data':
  415. break;
  416. default:
  417. console.error("PopperJS: an invalid property has been provided to the \"" + modifier.name + "\" modifier, valid properties are " + VALID_PROPERTIES.map(function (s) {
  418. return "\"" + s + "\"";
  419. }).join(', ') + "; but \"" + key + "\" was provided.");
  420. }
  421. modifier.requires && modifier.requires.forEach(function (requirement) {
  422. if (modifiers.find(function (mod) {
  423. return mod.name === requirement;
  424. }) == null) {
  425. console.error(format(MISSING_DEPENDENCY_ERROR, String(modifier.name), requirement, requirement));
  426. }
  427. });
  428. });
  429. });
  430. }
  431. function uniqueBy(arr, fn) {
  432. var identifiers = new Set();
  433. return arr.filter(function (item) {
  434. var identifier = fn(item);
  435. if (!identifiers.has(identifier)) {
  436. identifiers.add(identifier);
  437. return true;
  438. }
  439. });
  440. }
  441. function getBasePlacement(placement) {
  442. return placement.split('-')[0];
  443. }
  444. function mergeByName(modifiers) {
  445. var merged = modifiers.reduce(function (merged, current) {
  446. var existing = merged[current.name];
  447. merged[current.name] = existing ? Object.assign({}, existing, current, {
  448. options: Object.assign({}, existing.options, current.options),
  449. data: Object.assign({}, existing.data, current.data)
  450. }) : current;
  451. return merged;
  452. }, {}); // IE11 does not support Object.values
  453. return Object.keys(merged).map(function (key) {
  454. return merged[key];
  455. });
  456. }
  457. function getViewportRect(element, strategy) {
  458. var win = getWindow(element);
  459. var html = getDocumentElement(element);
  460. var visualViewport = win.visualViewport;
  461. var width = html.clientWidth;
  462. var height = html.clientHeight;
  463. var x = 0;
  464. var y = 0;
  465. if (visualViewport) {
  466. width = visualViewport.width;
  467. height = visualViewport.height;
  468. var layoutViewport = isLayoutViewport();
  469. if (layoutViewport || !layoutViewport && strategy === 'fixed') {
  470. x = visualViewport.offsetLeft;
  471. y = visualViewport.offsetTop;
  472. }
  473. }
  474. return {
  475. width: width,
  476. height: height,
  477. x: x + getWindowScrollBarX(element),
  478. y: y
  479. };
  480. }
  481. // of the `<html>` and `<body>` rect bounds if horizontally scrollable
  482. function getDocumentRect(element) {
  483. var _element$ownerDocumen;
  484. var html = getDocumentElement(element);
  485. var winScroll = getWindowScroll(element);
  486. var body = (_element$ownerDocumen = element.ownerDocument) == null ? void 0 : _element$ownerDocumen.body;
  487. var width = max(html.scrollWidth, html.clientWidth, body ? body.scrollWidth : 0, body ? body.clientWidth : 0);
  488. var height = max(html.scrollHeight, html.clientHeight, body ? body.scrollHeight : 0, body ? body.clientHeight : 0);
  489. var x = -winScroll.scrollLeft + getWindowScrollBarX(element);
  490. var y = -winScroll.scrollTop;
  491. if (getComputedStyle(body || html).direction === 'rtl') {
  492. x += max(html.clientWidth, body ? body.clientWidth : 0) - width;
  493. }
  494. return {
  495. width: width,
  496. height: height,
  497. x: x,
  498. y: y
  499. };
  500. }
  501. function contains(parent, child) {
  502. var rootNode = child.getRootNode && child.getRootNode(); // First, attempt with faster native method
  503. if (parent.contains(child)) {
  504. return true;
  505. } // then fallback to custom implementation with Shadow DOM support
  506. else if (rootNode && isShadowRoot(rootNode)) {
  507. var next = child;
  508. do {
  509. if (next && parent.isSameNode(next)) {
  510. return true;
  511. } // $FlowFixMe[prop-missing]: need a better way to handle this...
  512. next = next.parentNode || next.host;
  513. } while (next);
  514. } // Give up, the result is false
  515. return false;
  516. }
  517. function rectToClientRect(rect) {
  518. return Object.assign({}, rect, {
  519. left: rect.x,
  520. top: rect.y,
  521. right: rect.x + rect.width,
  522. bottom: rect.y + rect.height
  523. });
  524. }
  525. function getInnerBoundingClientRect(element, strategy) {
  526. var rect = getBoundingClientRect(element, false, strategy === 'fixed');
  527. rect.top = rect.top + element.clientTop;
  528. rect.left = rect.left + element.clientLeft;
  529. rect.bottom = rect.top + element.clientHeight;
  530. rect.right = rect.left + element.clientWidth;
  531. rect.width = element.clientWidth;
  532. rect.height = element.clientHeight;
  533. rect.x = rect.left;
  534. rect.y = rect.top;
  535. return rect;
  536. }
  537. function getClientRectFromMixedType(element, clippingParent, strategy) {
  538. return clippingParent === viewport ? rectToClientRect(getViewportRect(element, strategy)) : isElement(clippingParent) ? getInnerBoundingClientRect(clippingParent, strategy) : rectToClientRect(getDocumentRect(getDocumentElement(element)));
  539. } // A "clipping parent" is an overflowable container with the characteristic of
  540. // clipping (or hiding) overflowing elements with a position different from
  541. // `initial`
  542. function getClippingParents(element) {
  543. var clippingParents = listScrollParents(getParentNode(element));
  544. var canEscapeClipping = ['absolute', 'fixed'].indexOf(getComputedStyle(element).position) >= 0;
  545. var clipperElement = canEscapeClipping && isHTMLElement(element) ? getOffsetParent(element) : element;
  546. if (!isElement(clipperElement)) {
  547. return [];
  548. } // $FlowFixMe[incompatible-return]: https://github.com/facebook/flow/issues/1414
  549. return clippingParents.filter(function (clippingParent) {
  550. return isElement(clippingParent) && contains(clippingParent, clipperElement) && getNodeName(clippingParent) !== 'body';
  551. });
  552. } // Gets the maximum area that the element is visible in due to any number of
  553. // clipping parents
  554. function getClippingRect(element, boundary, rootBoundary, strategy) {
  555. var mainClippingParents = boundary === 'clippingParents' ? getClippingParents(element) : [].concat(boundary);
  556. var clippingParents = [].concat(mainClippingParents, [rootBoundary]);
  557. var firstClippingParent = clippingParents[0];
  558. var clippingRect = clippingParents.reduce(function (accRect, clippingParent) {
  559. var rect = getClientRectFromMixedType(element, clippingParent, strategy);
  560. accRect.top = max(rect.top, accRect.top);
  561. accRect.right = min(rect.right, accRect.right);
  562. accRect.bottom = min(rect.bottom, accRect.bottom);
  563. accRect.left = max(rect.left, accRect.left);
  564. return accRect;
  565. }, getClientRectFromMixedType(element, firstClippingParent, strategy));
  566. clippingRect.width = clippingRect.right - clippingRect.left;
  567. clippingRect.height = clippingRect.bottom - clippingRect.top;
  568. clippingRect.x = clippingRect.left;
  569. clippingRect.y = clippingRect.top;
  570. return clippingRect;
  571. }
  572. function getVariation(placement) {
  573. return placement.split('-')[1];
  574. }
  575. function getMainAxisFromPlacement(placement) {
  576. return ['top', 'bottom'].indexOf(placement) >= 0 ? 'x' : 'y';
  577. }
  578. function computeOffsets(_ref) {
  579. var reference = _ref.reference,
  580. element = _ref.element,
  581. placement = _ref.placement;
  582. var basePlacement = placement ? getBasePlacement(placement) : null;
  583. var variation = placement ? getVariation(placement) : null;
  584. var commonX = reference.x + reference.width / 2 - element.width / 2;
  585. var commonY = reference.y + reference.height / 2 - element.height / 2;
  586. var offsets;
  587. switch (basePlacement) {
  588. case top:
  589. offsets = {
  590. x: commonX,
  591. y: reference.y - element.height
  592. };
  593. break;
  594. case bottom:
  595. offsets = {
  596. x: commonX,
  597. y: reference.y + reference.height
  598. };
  599. break;
  600. case right:
  601. offsets = {
  602. x: reference.x + reference.width,
  603. y: commonY
  604. };
  605. break;
  606. case left:
  607. offsets = {
  608. x: reference.x - element.width,
  609. y: commonY
  610. };
  611. break;
  612. default:
  613. offsets = {
  614. x: reference.x,
  615. y: reference.y
  616. };
  617. }
  618. var mainAxis = basePlacement ? getMainAxisFromPlacement(basePlacement) : null;
  619. if (mainAxis != null) {
  620. var len = mainAxis === 'y' ? 'height' : 'width';
  621. switch (variation) {
  622. case start:
  623. offsets[mainAxis] = offsets[mainAxis] - (reference[len] / 2 - element[len] / 2);
  624. break;
  625. case end:
  626. offsets[mainAxis] = offsets[mainAxis] + (reference[len] / 2 - element[len] / 2);
  627. break;
  628. }
  629. }
  630. return offsets;
  631. }
  632. function getFreshSideObject() {
  633. return {
  634. top: 0,
  635. right: 0,
  636. bottom: 0,
  637. left: 0
  638. };
  639. }
  640. function mergePaddingObject(paddingObject) {
  641. return Object.assign({}, getFreshSideObject(), paddingObject);
  642. }
  643. function expandToHashMap(value, keys) {
  644. return keys.reduce(function (hashMap, key) {
  645. hashMap[key] = value;
  646. return hashMap;
  647. }, {});
  648. }
  649. function detectOverflow(state, options) {
  650. if (options === void 0) {
  651. options = {};
  652. }
  653. var _options = options,
  654. _options$placement = _options.placement,
  655. placement = _options$placement === void 0 ? state.placement : _options$placement,
  656. _options$strategy = _options.strategy,
  657. strategy = _options$strategy === void 0 ? state.strategy : _options$strategy,
  658. _options$boundary = _options.boundary,
  659. boundary = _options$boundary === void 0 ? clippingParents : _options$boundary,
  660. _options$rootBoundary = _options.rootBoundary,
  661. rootBoundary = _options$rootBoundary === void 0 ? viewport : _options$rootBoundary,
  662. _options$elementConte = _options.elementContext,
  663. elementContext = _options$elementConte === void 0 ? popper : _options$elementConte,
  664. _options$altBoundary = _options.altBoundary,
  665. altBoundary = _options$altBoundary === void 0 ? false : _options$altBoundary,
  666. _options$padding = _options.padding,
  667. padding = _options$padding === void 0 ? 0 : _options$padding;
  668. var paddingObject = mergePaddingObject(typeof padding !== 'number' ? padding : expandToHashMap(padding, basePlacements));
  669. var altContext = elementContext === popper ? reference : popper;
  670. var popperRect = state.rects.popper;
  671. var element = state.elements[altBoundary ? altContext : elementContext];
  672. var clippingClientRect = getClippingRect(isElement(element) ? element : element.contextElement || getDocumentElement(state.elements.popper), boundary, rootBoundary, strategy);
  673. var referenceClientRect = getBoundingClientRect(state.elements.reference);
  674. var popperOffsets = computeOffsets({
  675. reference: referenceClientRect,
  676. element: popperRect,
  677. strategy: 'absolute',
  678. placement: placement
  679. });
  680. var popperClientRect = rectToClientRect(Object.assign({}, popperRect, popperOffsets));
  681. var elementClientRect = elementContext === popper ? popperClientRect : referenceClientRect; // positive = overflowing the clipping rect
  682. // 0 or negative = within the clipping rect
  683. var overflowOffsets = {
  684. top: clippingClientRect.top - elementClientRect.top + paddingObject.top,
  685. bottom: elementClientRect.bottom - clippingClientRect.bottom + paddingObject.bottom,
  686. left: clippingClientRect.left - elementClientRect.left + paddingObject.left,
  687. right: elementClientRect.right - clippingClientRect.right + paddingObject.right
  688. };
  689. var offsetData = state.modifiersData.offset; // Offsets can be applied only to the popper element
  690. if (elementContext === popper && offsetData) {
  691. var offset = offsetData[placement];
  692. Object.keys(overflowOffsets).forEach(function (key) {
  693. var multiply = [right, bottom].indexOf(key) >= 0 ? 1 : -1;
  694. var axis = [top, bottom].indexOf(key) >= 0 ? 'y' : 'x';
  695. overflowOffsets[key] += offset[axis] * multiply;
  696. });
  697. }
  698. return overflowOffsets;
  699. }
  700. var INVALID_ELEMENT_ERROR = 'Popper: Invalid reference or popper argument provided. They must be either a DOM element or virtual element.';
  701. var INFINITE_LOOP_ERROR = 'Popper: An infinite loop in the modifiers cycle has been detected! The cycle has been interrupted to prevent a browser crash.';
  702. var DEFAULT_OPTIONS = {
  703. placement: 'bottom',
  704. modifiers: [],
  705. strategy: 'absolute'
  706. };
  707. function areValidElements() {
  708. for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
  709. args[_key] = arguments[_key];
  710. }
  711. return !args.some(function (element) {
  712. return !(element && typeof element.getBoundingClientRect === 'function');
  713. });
  714. }
  715. function popperGenerator(generatorOptions) {
  716. if (generatorOptions === void 0) {
  717. generatorOptions = {};
  718. }
  719. var _generatorOptions = generatorOptions,
  720. _generatorOptions$def = _generatorOptions.defaultModifiers,
  721. defaultModifiers = _generatorOptions$def === void 0 ? [] : _generatorOptions$def,
  722. _generatorOptions$def2 = _generatorOptions.defaultOptions,
  723. defaultOptions = _generatorOptions$def2 === void 0 ? DEFAULT_OPTIONS : _generatorOptions$def2;
  724. return function createPopper(reference, popper, options) {
  725. if (options === void 0) {
  726. options = defaultOptions;
  727. }
  728. var state = {
  729. placement: 'bottom',
  730. orderedModifiers: [],
  731. options: Object.assign({}, DEFAULT_OPTIONS, defaultOptions),
  732. modifiersData: {},
  733. elements: {
  734. reference: reference,
  735. popper: popper
  736. },
  737. attributes: {},
  738. styles: {}
  739. };
  740. var effectCleanupFns = [];
  741. var isDestroyed = false;
  742. var instance = {
  743. state: state,
  744. setOptions: function setOptions(setOptionsAction) {
  745. var options = typeof setOptionsAction === 'function' ? setOptionsAction(state.options) : setOptionsAction;
  746. cleanupModifierEffects();
  747. state.options = Object.assign({}, defaultOptions, state.options, options);
  748. state.scrollParents = {
  749. reference: isElement(reference) ? listScrollParents(reference) : reference.contextElement ? listScrollParents(reference.contextElement) : [],
  750. popper: listScrollParents(popper)
  751. }; // Orders the modifiers based on their dependencies and `phase`
  752. // properties
  753. var orderedModifiers = orderModifiers(mergeByName([].concat(defaultModifiers, state.options.modifiers))); // Strip out disabled modifiers
  754. state.orderedModifiers = orderedModifiers.filter(function (m) {
  755. return m.enabled;
  756. }); // Validate the provided modifiers so that the consumer will get warned
  757. // if one of the modifiers is invalid for any reason
  758. {
  759. var modifiers = uniqueBy([].concat(orderedModifiers, state.options.modifiers), function (_ref) {
  760. var name = _ref.name;
  761. return name;
  762. });
  763. validateModifiers(modifiers);
  764. if (getBasePlacement(state.options.placement) === auto) {
  765. var flipModifier = state.orderedModifiers.find(function (_ref2) {
  766. var name = _ref2.name;
  767. return name === 'flip';
  768. });
  769. if (!flipModifier) {
  770. console.error(['Popper: "auto" placements require the "flip" modifier be', 'present and enabled to work.'].join(' '));
  771. }
  772. }
  773. var _getComputedStyle = getComputedStyle(popper),
  774. marginTop = _getComputedStyle.marginTop,
  775. marginRight = _getComputedStyle.marginRight,
  776. marginBottom = _getComputedStyle.marginBottom,
  777. marginLeft = _getComputedStyle.marginLeft; // We no longer take into account `margins` on the popper, and it can
  778. // cause bugs with positioning, so we'll warn the consumer
  779. if ([marginTop, marginRight, marginBottom, marginLeft].some(function (margin) {
  780. return parseFloat(margin);
  781. })) {
  782. console.warn(['Popper: CSS "margin" styles cannot be used to apply padding', 'between the popper and its reference element or boundary.', 'To replicate margin, use the `offset` modifier, as well as', 'the `padding` option in the `preventOverflow` and `flip`', 'modifiers.'].join(' '));
  783. }
  784. }
  785. runModifierEffects();
  786. return instance.update();
  787. },
  788. // Sync update – it will always be executed, even if not necessary. This
  789. // is useful for low frequency updates where sync behavior simplifies the
  790. // logic.
  791. // For high frequency updates (e.g. `resize` and `scroll` events), always
  792. // prefer the async Popper#update method
  793. forceUpdate: function forceUpdate() {
  794. if (isDestroyed) {
  795. return;
  796. }
  797. var _state$elements = state.elements,
  798. reference = _state$elements.reference,
  799. popper = _state$elements.popper; // Don't proceed if `reference` or `popper` are not valid elements
  800. // anymore
  801. if (!areValidElements(reference, popper)) {
  802. {
  803. console.error(INVALID_ELEMENT_ERROR);
  804. }
  805. return;
  806. } // Store the reference and popper rects to be read by modifiers
  807. state.rects = {
  808. reference: getCompositeRect(reference, getOffsetParent(popper), state.options.strategy === 'fixed'),
  809. popper: getLayoutRect(popper)
  810. }; // Modifiers have the ability to reset the current update cycle. The
  811. // most common use case for this is the `flip` modifier changing the
  812. // placement, which then needs to re-run all the modifiers, because the
  813. // logic was previously ran for the previous placement and is therefore
  814. // stale/incorrect
  815. state.reset = false;
  816. state.placement = state.options.placement; // On each update cycle, the `modifiersData` property for each modifier
  817. // is filled with the initial data specified by the modifier. This means
  818. // it doesn't persist and is fresh on each update.
  819. // To ensure persistent data, use `${name}#persistent`
  820. state.orderedModifiers.forEach(function (modifier) {
  821. return state.modifiersData[modifier.name] = Object.assign({}, modifier.data);
  822. });
  823. var __debug_loops__ = 0;
  824. for (var index = 0; index < state.orderedModifiers.length; index++) {
  825. {
  826. __debug_loops__ += 1;
  827. if (__debug_loops__ > 100) {
  828. console.error(INFINITE_LOOP_ERROR);
  829. break;
  830. }
  831. }
  832. if (state.reset === true) {
  833. state.reset = false;
  834. index = -1;
  835. continue;
  836. }
  837. var _state$orderedModifie = state.orderedModifiers[index],
  838. fn = _state$orderedModifie.fn,
  839. _state$orderedModifie2 = _state$orderedModifie.options,
  840. _options = _state$orderedModifie2 === void 0 ? {} : _state$orderedModifie2,
  841. name = _state$orderedModifie.name;
  842. if (typeof fn === 'function') {
  843. state = fn({
  844. state: state,
  845. options: _options,
  846. name: name,
  847. instance: instance
  848. }) || state;
  849. }
  850. }
  851. },
  852. // Async and optimistically optimized update – it will not be executed if
  853. // not necessary (debounced to run at most once-per-tick)
  854. update: debounce(function () {
  855. return new Promise(function (resolve) {
  856. instance.forceUpdate();
  857. resolve(state);
  858. });
  859. }),
  860. destroy: function destroy() {
  861. cleanupModifierEffects();
  862. isDestroyed = true;
  863. }
  864. };
  865. if (!areValidElements(reference, popper)) {
  866. {
  867. console.error(INVALID_ELEMENT_ERROR);
  868. }
  869. return instance;
  870. }
  871. instance.setOptions(options).then(function (state) {
  872. if (!isDestroyed && options.onFirstUpdate) {
  873. options.onFirstUpdate(state);
  874. }
  875. }); // Modifiers have the ability to execute arbitrary code before the first
  876. // update cycle runs. They will be executed in the same order as the update
  877. // cycle. This is useful when a modifier adds some persistent data that
  878. // other modifiers need to use, but the modifier is run after the dependent
  879. // one.
  880. function runModifierEffects() {
  881. state.orderedModifiers.forEach(function (_ref3) {
  882. var name = _ref3.name,
  883. _ref3$options = _ref3.options,
  884. options = _ref3$options === void 0 ? {} : _ref3$options,
  885. effect = _ref3.effect;
  886. if (typeof effect === 'function') {
  887. var cleanupFn = effect({
  888. state: state,
  889. name: name,
  890. instance: instance,
  891. options: options
  892. });
  893. var noopFn = function noopFn() {};
  894. effectCleanupFns.push(cleanupFn || noopFn);
  895. }
  896. });
  897. }
  898. function cleanupModifierEffects() {
  899. effectCleanupFns.forEach(function (fn) {
  900. return fn();
  901. });
  902. effectCleanupFns = [];
  903. }
  904. return instance;
  905. };
  906. }
  907. var createPopper = /*#__PURE__*/popperGenerator(); // eslint-disable-next-line import/no-unused-modules
  908. exports.createPopper = createPopper;
  909. exports.detectOverflow = detectOverflow;
  910. exports.popperGenerator = popperGenerator;
  911. Object.defineProperty(exports, '__esModule', { value: true });
  912. })));
  913. //# sourceMappingURL=popper-base.js.map