index.js 37 KB

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