index.js 37 KB

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