popper.js 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655
  1. /**
  2. * @popperjs/core v2.0.0 - 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 = global || self, factory(global.Popper = {}));
  8. }(this, (function (exports) { 'use strict';
  9. function getBoundingClientRect(element) {
  10. var rect = element.getBoundingClientRect();
  11. return {
  12. width: rect.width,
  13. height: rect.height,
  14. top: rect.top,
  15. right: rect.right,
  16. bottom: rect.bottom,
  17. left: rect.left,
  18. x: rect.left,
  19. y: rect.top
  20. };
  21. }
  22. function getWindow(node) {
  23. if ({}.toString.call(node) !== '[object Window]') {
  24. var ownerDocument = node.ownerDocument;
  25. return ownerDocument ? ownerDocument.defaultView : window;
  26. }
  27. return node;
  28. }
  29. function getWindowScroll(node) {
  30. var win = getWindow(node);
  31. var scrollLeft = win.pageXOffset;
  32. var scrollTop = win.pageYOffset;
  33. return {
  34. scrollLeft: scrollLeft,
  35. scrollTop: scrollTop
  36. };
  37. }
  38. /*:: declare function isElement(node: mixed): boolean %checks(node instanceof
  39. Element); */
  40. function isElement(node) {
  41. var OwnElement = getWindow(node).Element;
  42. return node instanceof OwnElement;
  43. }
  44. /*:: declare function isHTMLElement(node: mixed): boolean %checks(node instanceof
  45. HTMLElement); */
  46. function isHTMLElement(node) {
  47. var OwnElement = getWindow(node).HTMLElement;
  48. return node instanceof OwnElement;
  49. }
  50. function getHTMLElementScroll(element) {
  51. return {
  52. scrollLeft: element.scrollLeft,
  53. scrollTop: element.scrollTop
  54. };
  55. }
  56. function getNodeScroll(node) {
  57. if (node === getWindow(node) || !isHTMLElement(node)) {
  58. return getWindowScroll(node);
  59. } else {
  60. return getHTMLElementScroll(node);
  61. }
  62. }
  63. function getNodeName(element) {
  64. return element ? (element.nodeName || '').toLowerCase() : null;
  65. }
  66. function getComputedStyle(element) {
  67. return getWindow(element).getComputedStyle(element);
  68. }
  69. function toNumber(cssValue) {
  70. return parseFloat(cssValue) || 0;
  71. }
  72. function getBorders(element) {
  73. var computedStyle = isHTMLElement(element) ? getComputedStyle(element) : {};
  74. return {
  75. top: toNumber(computedStyle.borderTopWidth),
  76. right: toNumber(computedStyle.borderRightWidth),
  77. bottom: toNumber(computedStyle.borderBottomWidth),
  78. left: toNumber(computedStyle.borderLeftWidth)
  79. };
  80. }
  81. function getInnerOffsets(offsetParent) {
  82. var rect = getBoundingClientRect(offsetParent);
  83. var borders = getBorders(offsetParent);
  84. return {
  85. x: rect.x + borders.left,
  86. y: rect.y + borders.top
  87. };
  88. } // Returns the composite rect of an element relative to its offsetParent.
  89. // Composite means it takes into account transforms as well as layout.
  90. function getCompositeRect(elementOrVirtualElement, offsetParent, isFixed) {
  91. if (isFixed === void 0) {
  92. isFixed = false;
  93. }
  94. var rect = getBoundingClientRect(elementOrVirtualElement);
  95. var scroll = {
  96. scrollLeft: 0,
  97. scrollTop: 0
  98. };
  99. var offsets = {
  100. x: 0,
  101. y: 0
  102. };
  103. if (!isFixed) {
  104. if (getNodeName(offsetParent) !== 'body') {
  105. scroll = getNodeScroll(offsetParent);
  106. }
  107. if (isHTMLElement(offsetParent)) {
  108. offsets = getInnerOffsets(offsetParent);
  109. }
  110. }
  111. return {
  112. x: rect.left + scroll.scrollLeft - offsets.x,
  113. y: rect.top + scroll.scrollTop - offsets.y,
  114. width: rect.width,
  115. height: rect.height
  116. };
  117. }
  118. // Returns the layout rect of an element relative to its offsetParent. Layout
  119. // means it doesn't take into account transforms.
  120. function getLayoutRect(element) {
  121. return {
  122. x: element.offsetLeft,
  123. y: element.offsetTop,
  124. width: element.offsetWidth,
  125. height: element.offsetHeight
  126. };
  127. }
  128. function getParentNode(element) {
  129. if (getNodeName(element) === 'html') {
  130. return element;
  131. }
  132. return element.parentNode || // DOM Element detected
  133. // $FlowFixMe: need a better way to handle this...
  134. element.host || // ShadowRoot detected
  135. document.ownerDocument || // Fallback to ownerDocument if available
  136. document.documentElement // Or to documentElement if everything else fails
  137. ;
  138. }
  139. function getScrollParent(node) {
  140. if (['html', 'body', '#document'].includes(getNodeName(node))) {
  141. return node.ownerDocument.body;
  142. }
  143. if (isHTMLElement(node)) {
  144. // Firefox wants us to check `-x` and `-y` variations as well
  145. var _getComputedStyle = getComputedStyle(node),
  146. overflow = _getComputedStyle.overflow,
  147. overflowX = _getComputedStyle.overflowX,
  148. overflowY = _getComputedStyle.overflowY;
  149. if (/auto|scroll|overlay|hidden/.test(overflow + overflowY + overflowX)) {
  150. return node;
  151. }
  152. }
  153. return getScrollParent(getParentNode(node));
  154. }
  155. function listScrollParents(element, list) {
  156. if (list === void 0) {
  157. list = [];
  158. }
  159. var scrollParent = getScrollParent(element);
  160. var isBody = getNodeName(scrollParent) === 'body';
  161. var target = isBody ? getWindow(scrollParent) : scrollParent;
  162. var updatedList = list.concat(target);
  163. return isBody ? updatedList : updatedList.concat(listScrollParents(getParentNode(target)));
  164. }
  165. function isTableElement(element) {
  166. return ['table', 'td', 'th'].includes(getNodeName(element));
  167. }
  168. var isFirefox = function isFirefox() {
  169. return typeof window.InstallTrigger !== 'undefined';
  170. };
  171. function getTrueOffsetParent(element) {
  172. var offsetParent;
  173. if (!isHTMLElement(element) || !(offsetParent = element.offsetParent) || // https://github.com/popperjs/popper.js/issues/837
  174. isFirefox() && getComputedStyle(offsetParent).position === 'fixed') {
  175. return null;
  176. }
  177. return offsetParent;
  178. }
  179. function getOffsetParent(element) {
  180. var window = getWindow(element);
  181. var offsetParent = getTrueOffsetParent(element); // Find the nearest non-table offsetParent
  182. while (offsetParent && isTableElement(offsetParent)) {
  183. offsetParent = getTrueOffsetParent(offsetParent);
  184. }
  185. if (offsetParent && getNodeName(offsetParent) === 'body' && getComputedStyle(offsetParent).position === 'static') {
  186. return window;
  187. }
  188. return offsetParent || window;
  189. }
  190. var top = 'top';
  191. var bottom = 'bottom';
  192. var right = 'right';
  193. var left = 'left';
  194. var auto = 'auto';
  195. var basePlacements = [top, bottom, right, left];
  196. var start = 'start';
  197. var end = 'end';
  198. var clippingParents = 'clippingParents';
  199. var viewport = 'viewport';
  200. var popper = 'popper';
  201. var reference = 'reference';
  202. var variationPlacements =
  203. /*#__PURE__*/
  204. basePlacements.reduce(function (acc, placement) {
  205. return acc.concat([placement + "-" + start, placement + "-" + end]);
  206. }, []);
  207. var placements =
  208. /*#__PURE__*/
  209. [].concat(basePlacements, [auto]).reduce(function (acc, placement) {
  210. return acc.concat([placement, placement + "-" + start, placement + "-" + end]);
  211. }, []); // modifiers that need to read the DOM
  212. var beforeRead = 'beforeRead';
  213. var read = 'read';
  214. var afterRead = 'afterRead'; // pure-logic modifiers
  215. var beforeMain = 'beforeMain';
  216. var main = 'main';
  217. var afterMain = 'afterMain'; // modifier with the purpose to write to the DOM (or write into a framework state)
  218. var beforeWrite = 'beforeWrite';
  219. var write = 'write';
  220. var afterWrite = 'afterWrite';
  221. var modifierPhases = [beforeRead, read, afterRead, beforeMain, main, afterMain, beforeWrite, write, afterWrite];
  222. function order(modifiers) {
  223. var map = new Map();
  224. var visited = new Set();
  225. var result = [];
  226. modifiers.forEach(function (modifier) {
  227. map.set(modifier.name, modifier);
  228. }); // On visiting object, check for its dependencies and visit them recursively
  229. function sort(modifier) {
  230. visited.add(modifier.name);
  231. var requires = [].concat(modifier.requires || [], modifier.requiresIfExists || []);
  232. requires.forEach(function (dep) {
  233. if (!visited.has(dep)) {
  234. var depModifier = map.get(dep);
  235. if (depModifier) {
  236. sort(depModifier);
  237. }
  238. }
  239. });
  240. result.push(modifier);
  241. }
  242. modifiers.forEach(function (modifier) {
  243. if (!visited.has(modifier.name)) {
  244. // check for visited object
  245. sort(modifier);
  246. }
  247. });
  248. return result;
  249. }
  250. function orderModifiers(modifiers) {
  251. // order based on dependencies
  252. var orderedModifiers = order(modifiers); // order based on phase
  253. return modifierPhases.reduce(function (acc, phase) {
  254. return acc.concat(orderedModifiers.filter(function (modifier) {
  255. return modifier.phase === phase;
  256. }));
  257. }, []);
  258. }
  259. function debounce(fn) {
  260. var pending;
  261. return function () {
  262. if (!pending) {
  263. pending = new Promise(function (resolve) {
  264. Promise.resolve().then(function () {
  265. pending = undefined;
  266. resolve(fn());
  267. });
  268. });
  269. }
  270. return pending;
  271. };
  272. }
  273. function format(str) {
  274. for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
  275. args[_key - 1] = arguments[_key];
  276. }
  277. return [].concat(args).reduce(function (p, c) {
  278. return p.replace(/%s/, c);
  279. }, str);
  280. }
  281. var INVALID_MODIFIER_ERROR = 'Popper: modifier "%s" provided an invalid %s property, expected %s but got %s';
  282. var MISSING_DEPENDENCY_ERROR = 'Popper: modifier "%s" requires "%s", but "%s" modifier is not available';
  283. var VALID_PROPERTIES = ['name', 'enabled', 'phase', 'fn', 'effect', 'requires', 'options'];
  284. function validateModifiers(modifiers) {
  285. modifiers.forEach(function (modifier) {
  286. Object.keys(modifier).forEach(function (key) {
  287. switch (key) {
  288. case 'name':
  289. if (typeof modifier.name !== 'string') {
  290. console.error(format(INVALID_MODIFIER_ERROR, String(modifier.name), '"name"', '"string"', "\"" + String(modifier.name) + "\""));
  291. }
  292. break;
  293. case 'enabled':
  294. if (typeof modifier.enabled !== 'boolean') {
  295. console.error(format(INVALID_MODIFIER_ERROR, modifier.name, '"enabled"', '"boolean"', "\"" + String(modifier.enabled) + "\""));
  296. }
  297. case 'phase':
  298. if (!modifierPhases.includes(modifier.phase)) {
  299. console.error(format(INVALID_MODIFIER_ERROR, modifier.name, '"phase"', "either " + modifierPhases.join(', '), "\"" + String(modifier.phase) + "\""));
  300. }
  301. break;
  302. case 'fn':
  303. if (typeof modifier.fn !== 'function') {
  304. console.error(format(INVALID_MODIFIER_ERROR, modifier.name, '"fn"', '"function"', "\"" + String(modifier.fn) + "\""));
  305. }
  306. break;
  307. case 'effect':
  308. if (typeof modifier.effect !== 'function') {
  309. console.error(format(INVALID_MODIFIER_ERROR, modifier.name, '"effect"', '"function"', "\"" + String(modifier.fn) + "\""));
  310. }
  311. break;
  312. case 'requires':
  313. if (!Array.isArray(modifier.requires)) {
  314. console.error(format(INVALID_MODIFIER_ERROR, modifier.name, '"requires"', '"array"', "\"" + String(modifier.requires) + "\""));
  315. }
  316. break;
  317. case 'requiresIfExists':
  318. if (!Array.isArray(modifier.requiresIfExists)) {
  319. console.error(format(INVALID_MODIFIER_ERROR, modifier.name, '"requiresIfExists"', '"array"', "\"" + String(modifier.requiresIfExists) + "\""));
  320. }
  321. break;
  322. case 'options':
  323. case 'data':
  324. break;
  325. default:
  326. console.error("PopperJS: an invalid property has been provided to the \"" + modifier.name + "\" modifier, valid properties are " + VALID_PROPERTIES.map(function (s) {
  327. return "\"" + s + "\"";
  328. }).join(', ') + "; but \"" + key + "\" was provided.");
  329. }
  330. modifier.requires && modifier.requires.forEach(function (requirement) {
  331. if (modifiers.find(function (mod) {
  332. return mod.name === requirement;
  333. }) == null) {
  334. console.error(format(MISSING_DEPENDENCY_ERROR, String(modifier.name), requirement, requirement));
  335. }
  336. });
  337. });
  338. });
  339. }
  340. function uniqueBy(arr, fn) {
  341. var identifiers = new Set();
  342. return arr.filter(function (item) {
  343. var identifier = fn(item);
  344. if (!identifiers.has(identifier)) {
  345. identifiers.add(identifier);
  346. return true;
  347. }
  348. });
  349. }
  350. function getBasePlacement(placement) {
  351. return placement.split('-')[0];
  352. }
  353. var INVALID_ELEMENT_ERROR = 'Popper: Invalid reference or popper argument provided to Popper, they must be either a valid DOM element, virtual element, or a jQuery-wrapped DOM element.';
  354. 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.';
  355. var DEFAULT_OPTIONS = {
  356. placement: 'bottom',
  357. modifiers: [],
  358. strategy: 'absolute'
  359. };
  360. function areValidElements() {
  361. for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
  362. args[_key] = arguments[_key];
  363. }
  364. return !args.some(function (element) {
  365. return !(element && typeof element.getBoundingClientRect === 'function');
  366. });
  367. }
  368. function popperGenerator(generatorOptions) {
  369. if (generatorOptions === void 0) {
  370. generatorOptions = {};
  371. }
  372. var _generatorOptions = generatorOptions,
  373. _generatorOptions$def = _generatorOptions.defaultModifiers,
  374. defaultModifiers = _generatorOptions$def === void 0 ? [] : _generatorOptions$def,
  375. _generatorOptions$def2 = _generatorOptions.defaultOptions,
  376. defaultOptions = _generatorOptions$def2 === void 0 ? DEFAULT_OPTIONS : _generatorOptions$def2;
  377. return function createPopper(reference, popper, options) {
  378. if (options === void 0) {
  379. options = defaultOptions;
  380. }
  381. var state = {
  382. placement: 'bottom',
  383. orderedModifiers: [],
  384. options: Object.assign({}, DEFAULT_OPTIONS, {}, defaultOptions),
  385. modifiersData: {},
  386. elements: {
  387. reference: reference,
  388. popper: popper
  389. },
  390. attributes: {},
  391. styles: {}
  392. };
  393. var effectCleanupFns = [];
  394. var isDestroyed = false;
  395. var instance = {
  396. state: state,
  397. setOptions: function setOptions(options) {
  398. cleanupModifierEffects();
  399. state.options = Object.assign({}, defaultOptions, {}, state.options, {}, options);
  400. state.scrollParents = {
  401. reference: isElement(reference) ? listScrollParents(reference) : [],
  402. popper: listScrollParents(popper)
  403. }; // Orders the modifiers based on their dependencies and `phase`
  404. // properties
  405. var orderedModifiers = orderModifiers([].concat(state.options.modifiers.filter(function (modifier) {
  406. return !defaultModifiers.find(function (_ref) {
  407. var name = _ref.name;
  408. return name === modifier.name;
  409. });
  410. }), defaultModifiers.map(function (defaultModifier) {
  411. return Object.assign({}, defaultModifier, {}, state.options.modifiers.find(function (_ref2) {
  412. var name = _ref2.name;
  413. return name === defaultModifier.name;
  414. }));
  415. }))); // Validate the provided modifiers so that the consumer will get warned
  416. // if one of the modifiers is invalid for any reason
  417. {
  418. var modifiers = uniqueBy([].concat(orderedModifiers, state.options.modifiers), function (_ref3) {
  419. var name = _ref3.name;
  420. return name;
  421. });
  422. validateModifiers(modifiers);
  423. if (getBasePlacement(state.options.placement) === auto) {
  424. var flipModifier = orderedModifiers.find(function (_ref4) {
  425. var name = _ref4.name;
  426. return name === 'flip';
  427. });
  428. if (!flipModifier) {
  429. console.error(['Popper: "auto" placements require the "flip" modifier be', 'present and enabled to work.'].join(' '));
  430. }
  431. }
  432. } // Strip out disabled modifiers
  433. state.orderedModifiers = orderedModifiers.filter(function (m) {
  434. return m.enabled;
  435. });
  436. runModifierEffects();
  437. return instance.update();
  438. },
  439. // Sync update – it will always be executed, even if not necessary. This
  440. // is useful for low frequency updates where sync behavior simplifies the
  441. // logic.
  442. // For high frequency updates (e.g. `resize` and `scroll` events), always
  443. // prefer the async Popper#update method
  444. forceUpdate: function forceUpdate() {
  445. if (isDestroyed) {
  446. return;
  447. }
  448. var _state$elements = state.elements,
  449. reference = _state$elements.reference,
  450. popper = _state$elements.popper; // Don't proceed if `reference` or `popper` are not valid elements
  451. // anymore
  452. if (!areValidElements(reference, popper)) {
  453. {
  454. console.error(INVALID_ELEMENT_ERROR);
  455. }
  456. return;
  457. } // Store the reference and popper rects to be read by modifiers
  458. state.rects = {
  459. reference: getCompositeRect(reference, getOffsetParent(popper), state.options.strategy === 'fixed'),
  460. popper: getLayoutRect(popper)
  461. }; // Modifiers have the ability to reset the current update cycle. The
  462. // most common use case for this is the `flip` modifier changing the
  463. // placement, which then needs to re-run all the modifiers, because the
  464. // logic was previously ran for the previous placement and is therefore
  465. // stale/incorrect
  466. state.reset = false;
  467. state.placement = state.options.placement; // On each update cycle, the `modifiersData` property for each modifier
  468. // is filled with the initial data specified by the modifier. This means
  469. // it doesn't persist and is fresh on each update.
  470. // To ensure persistent data, use `${name}#persistent`
  471. state.orderedModifiers.forEach(function (modifier) {
  472. return state.modifiersData[modifier.name] = Object.assign({}, modifier.data);
  473. });
  474. var __debug_loops__ = 0;
  475. for (var index = 0; index < state.orderedModifiers.length; index++) {
  476. {
  477. __debug_loops__ += 1;
  478. if (__debug_loops__ > 100) {
  479. console.error(INFINITE_LOOP_ERROR);
  480. break;
  481. }
  482. }
  483. if (state.reset === true) {
  484. state.reset = false;
  485. index = -1;
  486. continue;
  487. }
  488. var _state$orderedModifie = state.orderedModifiers[index],
  489. fn = _state$orderedModifie.fn,
  490. _state$orderedModifie2 = _state$orderedModifie.options,
  491. _options = _state$orderedModifie2 === void 0 ? {} : _state$orderedModifie2,
  492. name = _state$orderedModifie.name;
  493. if (typeof fn === 'function') {
  494. state = fn({
  495. state: state,
  496. options: _options,
  497. name: name,
  498. instance: instance
  499. }) || state;
  500. }
  501. }
  502. },
  503. // Async and optimistically optimized update – it will not be executed if
  504. // not necessary (debounced to run at most once-per-tick)
  505. update: debounce(function () {
  506. return new Promise(function (resolve) {
  507. instance.forceUpdate();
  508. resolve(state);
  509. });
  510. }),
  511. destroy: function destroy() {
  512. cleanupModifierEffects();
  513. isDestroyed = true;
  514. }
  515. };
  516. if (!areValidElements(reference, popper)) {
  517. {
  518. console.error(INVALID_ELEMENT_ERROR);
  519. }
  520. return instance;
  521. }
  522. instance.setOptions(options).then(function (state) {
  523. if (!isDestroyed && options.onFirstUpdate) {
  524. options.onFirstUpdate(state);
  525. }
  526. }); // Modifiers have the ability to execute arbitrary code before the first
  527. // update cycle runs. They will be executed in the same order as the update
  528. // cycle. This is useful when a modifier adds some persistent data that
  529. // other modifiers need to use, but the modifier is run after the dependent
  530. // one.
  531. function runModifierEffects() {
  532. state.orderedModifiers.forEach(function (_ref5) {
  533. var name = _ref5.name,
  534. _ref5$options = _ref5.options,
  535. options = _ref5$options === void 0 ? {} : _ref5$options,
  536. effect = _ref5.effect;
  537. if (typeof effect === 'function') {
  538. var cleanupFn = effect({
  539. state: state,
  540. name: name,
  541. instance: instance,
  542. options: options
  543. });
  544. var noopFn = function noopFn() {};
  545. effectCleanupFns.push(cleanupFn || noopFn);
  546. }
  547. });
  548. }
  549. function cleanupModifierEffects() {
  550. effectCleanupFns.forEach(function (fn) {
  551. return fn();
  552. });
  553. effectCleanupFns = [];
  554. }
  555. return instance;
  556. };
  557. }
  558. var passive = {
  559. passive: true
  560. };
  561. function effect(_ref) {
  562. var state = _ref.state,
  563. instance = _ref.instance,
  564. options = _ref.options;
  565. var _options$scroll = options.scroll,
  566. scroll = _options$scroll === void 0 ? true : _options$scroll,
  567. _options$resize = options.resize,
  568. resize = _options$resize === void 0 ? true : _options$resize;
  569. var window = getWindow(state.elements.popper);
  570. var scrollParents = [].concat(state.scrollParents.reference, state.scrollParents.popper);
  571. if (scroll) {
  572. scrollParents.forEach(function (scrollParent) {
  573. scrollParent.addEventListener('scroll', instance.update, passive);
  574. });
  575. }
  576. if (resize) {
  577. window.addEventListener('resize', instance.update, passive);
  578. }
  579. return function () {
  580. if (scroll) {
  581. scrollParents.forEach(function (scrollParent) {
  582. scrollParent.removeEventListener('scroll', instance.update, passive);
  583. });
  584. }
  585. if (resize) {
  586. window.removeEventListener('resize', instance.update, passive);
  587. }
  588. };
  589. }
  590. var eventListeners = {
  591. name: 'eventListeners',
  592. enabled: true,
  593. phase: 'write',
  594. fn: function fn() {},
  595. effect: effect,
  596. data: {}
  597. };
  598. function getVariation(placement) {
  599. return placement.split('-')[1];
  600. }
  601. function getMainAxisFromPlacement(placement) {
  602. return ['top', 'bottom'].includes(placement) ? 'x' : 'y';
  603. }
  604. function computeOffsets(_ref) {
  605. var reference = _ref.reference,
  606. element = _ref.element,
  607. placement = _ref.placement;
  608. var basePlacement = placement ? getBasePlacement(placement) : null;
  609. var variation = placement ? getVariation(placement) : null;
  610. var commonX = reference.x + reference.width / 2 - element.width / 2;
  611. var commonY = reference.y + reference.height / 2 - element.height / 2;
  612. var offsets;
  613. switch (basePlacement) {
  614. case top:
  615. offsets = {
  616. x: commonX,
  617. y: reference.y - element.height
  618. };
  619. break;
  620. case bottom:
  621. offsets = {
  622. x: commonX,
  623. y: reference.y + reference.height
  624. };
  625. break;
  626. case right:
  627. offsets = {
  628. x: reference.x + reference.width,
  629. y: commonY
  630. };
  631. break;
  632. case left:
  633. offsets = {
  634. x: reference.x - element.width,
  635. y: commonY
  636. };
  637. break;
  638. default:
  639. offsets = {
  640. x: reference.x,
  641. y: reference.y
  642. };
  643. }
  644. var mainAxis = basePlacement ? getMainAxisFromPlacement(basePlacement) : null;
  645. if (mainAxis != null) {
  646. var len = mainAxis === 'y' ? 'height' : 'width';
  647. switch (variation) {
  648. case start:
  649. offsets[mainAxis] = Math.floor(offsets[mainAxis]) - Math.floor(reference[len] / 2 - element[len] / 2);
  650. break;
  651. case end:
  652. offsets[mainAxis] = Math.floor(offsets[mainAxis]) + Math.ceil(reference[len] / 2 - element[len] / 2);
  653. break;
  654. }
  655. }
  656. return offsets;
  657. }
  658. function popperOffsets(_ref) {
  659. var state = _ref.state,
  660. name = _ref.name;
  661. // Offsets are the actual position the popper needs to have to be
  662. // properly positioned near its reference element
  663. // This is the most basic placement, and will be adjusted by
  664. // the modifiers in the next step
  665. state.modifiersData[name] = computeOffsets({
  666. reference: state.rects.reference,
  667. element: state.rects.popper,
  668. strategy: 'absolute',
  669. placement: state.placement
  670. });
  671. }
  672. var popperOffsets$1 = {
  673. name: 'popperOffsets',
  674. enabled: true,
  675. phase: 'read',
  676. fn: popperOffsets,
  677. data: {}
  678. };
  679. function getDocumentElement(element) {
  680. return element.ownerDocument.documentElement;
  681. }
  682. var unsetSides = {
  683. top: 'auto',
  684. right: 'auto',
  685. bottom: 'auto',
  686. left: 'auto'
  687. }; // Round the offsets to the nearest suitable subpixel based on the DPR.
  688. // Zooming can change the DPR, but it seems to report a value that will
  689. // cleanly divide the values into the appropriate subpixels.
  690. function roundOffsets(_ref) {
  691. var x = _ref.x,
  692. y = _ref.y;
  693. var dpr = window.devicePixelRatio || 1;
  694. return {
  695. x: Math.round(x * dpr) / dpr || 0,
  696. y: Math.round(y * dpr) / dpr || 0
  697. };
  698. }
  699. function mapToStyles(_ref2) {
  700. var _Object$assign2;
  701. var popper = _ref2.popper,
  702. popperRect = _ref2.popperRect,
  703. placement = _ref2.placement,
  704. offsets = _ref2.offsets,
  705. position = _ref2.position,
  706. gpuAcceleration = _ref2.gpuAcceleration,
  707. adaptive = _ref2.adaptive;
  708. var _roundOffsets = roundOffsets(offsets),
  709. x = _roundOffsets.x,
  710. y = _roundOffsets.y;
  711. var hasX = offsets.hasOwnProperty('x');
  712. var hasY = offsets.hasOwnProperty('y');
  713. var sideX = left;
  714. var sideY = top;
  715. if (adaptive) {
  716. var offsetParent = getOffsetParent(popper);
  717. if (offsetParent === getWindow(popper)) {
  718. offsetParent = getDocumentElement(popper);
  719. }
  720. if (placement === top) {
  721. y = y - offsetParent.clientHeight + popperRect.height;
  722. sideY = bottom;
  723. }
  724. if (placement === left) {
  725. x = x - offsetParent.clientWidth + popperRect.width;
  726. sideX = right;
  727. }
  728. }
  729. var commonStyles = Object.assign({
  730. position: position
  731. }, adaptive && unsetSides);
  732. if (gpuAcceleration) {
  733. var _Object$assign;
  734. return Object.assign({}, commonStyles, (_Object$assign = {}, _Object$assign[sideY] = hasY ? '0' : '', _Object$assign[sideX] = hasX ? '0' : '', _Object$assign.transform = (window.devicePixelRatio || 1) < 2 ? "translate(" + x + "px, " + y + "px)" : "translate3d(" + x + "px, " + y + "px, 0)", _Object$assign));
  735. }
  736. return Object.assign({}, commonStyles, (_Object$assign2 = {}, _Object$assign2[sideY] = hasY ? y + "px" : '', _Object$assign2[sideX] = hasX ? x + "px" : '', _Object$assign2.transform = '', _Object$assign2));
  737. }
  738. function computeStyles(_ref3) {
  739. var state = _ref3.state,
  740. options = _ref3.options;
  741. var _options$gpuAccelerat = options.gpuAcceleration,
  742. gpuAcceleration = _options$gpuAccelerat === void 0 ? true : _options$gpuAccelerat,
  743. _options$adaptive = options.adaptive,
  744. adaptive = _options$adaptive === void 0 ? true : _options$adaptive;
  745. var commonStyles = {
  746. placement: getBasePlacement(state.placement),
  747. popper: state.elements.popper,
  748. popperRect: state.rects.popper,
  749. gpuAcceleration: gpuAcceleration
  750. }; // popper offsets are always available
  751. state.styles.popper = Object.assign({}, state.styles.popper, {}, mapToStyles(Object.assign({}, commonStyles, {
  752. offsets: state.modifiersData.popperOffsets,
  753. position: state.options.strategy,
  754. adaptive: adaptive
  755. }))); // arrow offsets may not be available
  756. if (state.modifiersData.arrow != null) {
  757. state.styles.arrow = Object.assign({}, state.styles.arrow, {}, mapToStyles(Object.assign({}, commonStyles, {
  758. offsets: state.modifiersData.arrow,
  759. position: 'absolute',
  760. adaptive: false
  761. })));
  762. }
  763. state.attributes.popper = Object.assign({}, state.attributes.popper, {
  764. 'data-popper-placement': state.placement
  765. });
  766. }
  767. var computeStyles$1 = {
  768. name: 'computeStyles',
  769. enabled: true,
  770. phase: 'beforeWrite',
  771. fn: computeStyles,
  772. data: {}
  773. };
  774. // and applies them to the HTMLElements such as popper and arrow
  775. function applyStyles(_ref) {
  776. var state = _ref.state;
  777. Object.keys(state.elements).forEach(function (name) {
  778. var style = state.styles[name] || {};
  779. var attributes = state.attributes[name] || {};
  780. var element = state.elements[name]; // arrow is optional + virtual elements
  781. if (!isHTMLElement(element) || !getNodeName(element)) {
  782. return;
  783. } // Flow doesn't support to extend this property, but it's the most
  784. // effective way to apply styles to an HTMLElement
  785. // $FlowFixMe
  786. Object.assign(element.style, style);
  787. Object.entries(attributes).forEach(function (_ref2) {
  788. var name = _ref2[0],
  789. value = _ref2[1];
  790. if (value === false) {
  791. element.removeAttribute(name);
  792. } else {
  793. element.setAttribute(name, value === true ? '' : value);
  794. }
  795. });
  796. });
  797. }
  798. function effect$1(_ref3) {
  799. var state = _ref3.state;
  800. var initialStyles = {
  801. position: 'absolute',
  802. left: '0',
  803. top: '0'
  804. };
  805. Object.assign(state.elements.popper.style, initialStyles);
  806. return function () {
  807. Object.keys(state.elements).forEach(function (name) {
  808. var element = state.elements[name];
  809. var styleProperties = Object.keys(state.styles.hasOwnProperty(name) ? Object.assign({}, state.styles[name]) : initialStyles);
  810. var attributes = state.attributes[name] || {}; // Set all values to an empty string to unset them
  811. var style = styleProperties.reduce(function (style, property) {
  812. var _Object$assign;
  813. return Object.assign({}, style, (_Object$assign = {}, _Object$assign[String(property)] = '', _Object$assign));
  814. }, {}); // arrow is optional + virtual elements
  815. if (!isHTMLElement(element) || !getNodeName(element)) {
  816. return;
  817. } // Flow doesn't support to extend this property, but it's the most
  818. // effective way to apply styles to an HTMLElement
  819. // $FlowFixMe
  820. Object.assign(element.style, style);
  821. Object.keys(attributes).forEach(function (attribute) {
  822. return element.removeAttribute(attribute);
  823. });
  824. });
  825. };
  826. }
  827. var applyStyles$1 = {
  828. name: 'applyStyles',
  829. enabled: true,
  830. phase: 'write',
  831. fn: applyStyles,
  832. effect: effect$1,
  833. requires: ['computeStyles']
  834. };
  835. function distanceAndSkiddingToXY(placement, rects, offset) {
  836. var basePlacement = getBasePlacement(placement);
  837. var invertDistance = [left, top].includes(basePlacement) ? -1 : 1;
  838. var _ref = typeof offset === 'function' ? offset(Object.assign({}, rects, {
  839. placement: placement
  840. })) : offset,
  841. skidding = _ref[0],
  842. distance = _ref[1];
  843. skidding = skidding || 0;
  844. distance = (distance || 0) * invertDistance;
  845. return [left, right].includes(basePlacement) ? {
  846. x: distance,
  847. y: skidding
  848. } : {
  849. x: skidding,
  850. y: distance
  851. };
  852. }
  853. function offset(_ref2) {
  854. var state = _ref2.state,
  855. options = _ref2.options,
  856. name = _ref2.name;
  857. var _options$offset = options.offset,
  858. offset = _options$offset === void 0 ? [0, 0] : _options$offset;
  859. var data = placements.reduce(function (acc, placement) {
  860. acc[placement] = distanceAndSkiddingToXY(placement, state.rects, offset);
  861. return acc;
  862. }, {});
  863. var _data$state$placement = data[state.placement],
  864. x = _data$state$placement.x,
  865. y = _data$state$placement.y;
  866. state.modifiersData.popperOffsets.x += x;
  867. state.modifiersData.popperOffsets.y += y;
  868. state.modifiersData[name] = data;
  869. }
  870. var offset$1 = {
  871. name: 'offset',
  872. enabled: true,
  873. phase: 'main',
  874. requires: ['popperOffsets'],
  875. fn: offset
  876. };
  877. var hash = {
  878. left: 'right',
  879. right: 'left',
  880. bottom: 'top',
  881. top: 'bottom'
  882. };
  883. function getOppositePlacement(placement) {
  884. return placement.replace(/left|right|bottom|top/g, function (matched) {
  885. return hash[matched];
  886. });
  887. }
  888. var hash$1 = {
  889. start: 'end',
  890. end: 'start'
  891. };
  892. function getOppositeVariationPlacement(placement) {
  893. return placement.replace(/start|end/g, function (matched) {
  894. return hash$1[matched];
  895. });
  896. }
  897. function getViewportRect(element) {
  898. var win = getWindow(element);
  899. return {
  900. width: win.innerWidth,
  901. height: win.innerHeight,
  902. x: 0,
  903. y: 0
  904. };
  905. }
  906. function getDocumentRect(element) {
  907. var win = getWindow(element);
  908. var winScroll = getWindowScroll(element);
  909. var documentRect = getCompositeRect(getDocumentElement(element), win);
  910. documentRect.height = Math.max(documentRect.height, win.innerHeight);
  911. documentRect.width = Math.max(documentRect.width, win.innerWidth);
  912. documentRect.x = -winScroll.scrollLeft;
  913. documentRect.y = -winScroll.scrollTop;
  914. return documentRect;
  915. }
  916. function getDecorations(element) {
  917. var borders = getBorders(element);
  918. return {
  919. top: borders.top,
  920. right: element.offsetWidth - (element.clientWidth + borders.right),
  921. bottom: element.offsetHeight - (element.clientHeight + borders.bottom),
  922. left: borders.left
  923. };
  924. }
  925. function contains(parent, child) {
  926. // $FlowFixMe: hasOwnProperty doesn't seem to work in tests
  927. var isShadow = Boolean(child.getRootNode && child.getRootNode().host); // First, attempt with faster native method
  928. if (parent.contains(child)) {
  929. return true;
  930. } // then fallback to custom implementation with Shadow DOM support
  931. else if (isShadow) {
  932. var next = child;
  933. do {
  934. if (next && next.isSameNode(parent)) {
  935. return true;
  936. } // $FlowFixMe: need a better way to handle this...
  937. next = next.parentNode || next.host;
  938. } while (next);
  939. } // Give up, the result is false
  940. return false;
  941. }
  942. function rectToClientRect(rect) {
  943. return Object.assign({}, rect, {
  944. left: rect.x,
  945. top: rect.y,
  946. right: rect.x + rect.width,
  947. bottom: rect.y + rect.height
  948. });
  949. }
  950. function getFreshSideObject() {
  951. return {
  952. top: 0,
  953. right: 0,
  954. bottom: 0,
  955. left: 0
  956. };
  957. }
  958. function getClientRectFromMixedType(element, clippingParent) {
  959. return clippingParent === viewport ? rectToClientRect(getViewportRect(element)) : isHTMLElement(clippingParent) ? getBoundingClientRect(clippingParent) : rectToClientRect(getDocumentRect(getDocumentElement(element)));
  960. } // A "clipping parent" is an overflowable container with the characteristic of
  961. // clipping (or hiding) overflowing elements with a position different from
  962. // `initial`
  963. function getClippingParents(element) {
  964. var clippingParents = listScrollParents(element);
  965. var canEscapeClipping = ['absolute', 'fixed'].includes(getComputedStyle(element).position);
  966. var clipperElement = canEscapeClipping && isHTMLElement(element) ? getOffsetParent(element) : element;
  967. if (!isElement(clipperElement)) {
  968. return [];
  969. }
  970. return clippingParents.filter(function (clippingParent) {
  971. return isElement(clippingParent) && contains(clippingParent, clipperElement);
  972. });
  973. } // Gets the maximum area that the element is visible in due to any number of
  974. // clipping parents
  975. function getClippingRect(element, boundary, rootBoundary) {
  976. var mainClippingParents = boundary === 'clippingParents' ? getClippingParents(element) : [].concat(boundary);
  977. var clippingParents = [].concat(mainClippingParents, [rootBoundary]);
  978. var firstClippingParent = clippingParents[0];
  979. var clippingRect = clippingParents.reduce(function (accRect, clippingParent) {
  980. var rect = getClientRectFromMixedType(element, clippingParent);
  981. var decorations = isHTMLElement(clippingParent) ? getDecorations(clippingParent) : getFreshSideObject();
  982. accRect.top = Math.max(rect.top + decorations.top, accRect.top);
  983. accRect.right = Math.min(rect.right - decorations.right, accRect.right);
  984. accRect.bottom = Math.min(rect.bottom - decorations.bottom, accRect.bottom);
  985. accRect.left = Math.max(rect.left + decorations.left, accRect.left);
  986. return accRect;
  987. }, getClientRectFromMixedType(element, firstClippingParent));
  988. clippingRect.width = clippingRect.right - clippingRect.left;
  989. clippingRect.height = clippingRect.bottom - clippingRect.top;
  990. clippingRect.x = clippingRect.left;
  991. clippingRect.y = clippingRect.top;
  992. return clippingRect;
  993. }
  994. function mergePaddingObject(paddingObject) {
  995. return Object.assign({}, getFreshSideObject(), {}, paddingObject);
  996. }
  997. function expandToHashMap(value, keys) {
  998. return keys.reduce(function (hashMap, key) {
  999. hashMap[key] = value;
  1000. return hashMap;
  1001. }, {});
  1002. }
  1003. function detectOverflow(state, options) {
  1004. if (options === void 0) {
  1005. options = {};
  1006. }
  1007. var _options = options,
  1008. _options$placement = _options.placement,
  1009. placement = _options$placement === void 0 ? state.placement : _options$placement,
  1010. _options$boundary = _options.boundary,
  1011. boundary = _options$boundary === void 0 ? clippingParents : _options$boundary,
  1012. _options$rootBoundary = _options.rootBoundary,
  1013. rootBoundary = _options$rootBoundary === void 0 ? viewport : _options$rootBoundary,
  1014. _options$elementConte = _options.elementContext,
  1015. elementContext = _options$elementConte === void 0 ? popper : _options$elementConte,
  1016. _options$altBoundary = _options.altBoundary,
  1017. altBoundary = _options$altBoundary === void 0 ? false : _options$altBoundary,
  1018. _options$padding = _options.padding,
  1019. padding = _options$padding === void 0 ? 0 : _options$padding;
  1020. var paddingObject = mergePaddingObject(typeof padding !== 'number' ? padding : expandToHashMap(padding, basePlacements));
  1021. var altContext = elementContext === popper ? reference : popper;
  1022. var referenceElement = state.elements.reference;
  1023. var popperRect = state.rects.popper;
  1024. var element = state.elements[altBoundary ? altContext : elementContext];
  1025. var clippingClientRect = getClippingRect(isElement(element) ? element : getDocumentElement(state.elements.popper), boundary, rootBoundary);
  1026. var referenceClientRect = getBoundingClientRect(referenceElement);
  1027. var popperOffsets = computeOffsets({
  1028. reference: referenceClientRect,
  1029. element: popperRect,
  1030. strategy: 'absolute',
  1031. placement: placement
  1032. });
  1033. var popperClientRect = rectToClientRect(Object.assign({}, popperRect, {}, popperOffsets));
  1034. var elementClientRect = elementContext === popper ? popperClientRect : referenceClientRect; // positive = overflowing the clipping rect
  1035. // 0 or negative = within the clipping rect
  1036. var overflowOffsets = {
  1037. top: clippingClientRect.top - elementClientRect.top + paddingObject.top,
  1038. bottom: elementClientRect.bottom - clippingClientRect.bottom + paddingObject.bottom,
  1039. left: clippingClientRect.left - elementClientRect.left + paddingObject.left,
  1040. right: elementClientRect.right - clippingClientRect.right + paddingObject.right
  1041. };
  1042. var offsetData = state.modifiersData.offset; // Offsets can be applied only to the popper element
  1043. if (elementContext === popper && offsetData) {
  1044. var offset = offsetData[placement];
  1045. Object.keys(overflowOffsets).forEach(function (key) {
  1046. var multiply = [right, bottom].includes(key) ? 1 : -1;
  1047. var axis = [top, bottom].includes(key) ? 'y' : 'x';
  1048. overflowOffsets[key] += offset[axis] * multiply;
  1049. });
  1050. }
  1051. return overflowOffsets;
  1052. }
  1053. function computeAutoPlacement(state, options) {
  1054. if (options === void 0) {
  1055. options = {};
  1056. }
  1057. var _options = options,
  1058. placement = _options.placement,
  1059. boundary = _options.boundary,
  1060. rootBoundary = _options.rootBoundary,
  1061. padding = _options.padding,
  1062. flipVariations = _options.flipVariations;
  1063. var variation = getVariation(placement);
  1064. var placements = variation ? flipVariations ? variationPlacements : variationPlacements.filter(function (placement) {
  1065. return placement.includes(variation);
  1066. }) : basePlacements; // $FlowFixMe: Flow seems to have problems with two array unions...
  1067. var overflows = placements.reduce(function (acc, placement) {
  1068. acc[placement] = detectOverflow(state, {
  1069. placement: placement,
  1070. boundary: boundary,
  1071. rootBoundary: rootBoundary,
  1072. padding: padding
  1073. })[getBasePlacement(placement)];
  1074. return acc;
  1075. }, {});
  1076. return Object.keys(overflows).sort(function (a, b) {
  1077. return overflows[a] - overflows[b];
  1078. });
  1079. }
  1080. function getExpandedFallbackPlacements(placement) {
  1081. if (getBasePlacement(placement) === auto) {
  1082. return [];
  1083. }
  1084. var oppositePlacement = getOppositePlacement(placement);
  1085. return [getOppositeVariationPlacement(placement), oppositePlacement, getOppositeVariationPlacement(oppositePlacement)];
  1086. }
  1087. function flip(_ref) {
  1088. var state = _ref.state,
  1089. options = _ref.options,
  1090. name = _ref.name;
  1091. if (state.modifiersData[name]._skip) {
  1092. return;
  1093. }
  1094. var specifiedFallbackPlacements = options.fallbackPlacements,
  1095. padding = options.padding,
  1096. boundary = options.boundary,
  1097. rootBoundary = options.rootBoundary,
  1098. _options$flipVariatio = options.flipVariations,
  1099. flipVariations = _options$flipVariatio === void 0 ? true : _options$flipVariatio;
  1100. var preferredPlacement = state.options.placement;
  1101. var basePlacement = getBasePlacement(preferredPlacement);
  1102. var isBasePlacement = basePlacement === preferredPlacement;
  1103. var fallbackPlacements = specifiedFallbackPlacements || (isBasePlacement ? [getOppositePlacement(preferredPlacement)] : getExpandedFallbackPlacements(preferredPlacement));
  1104. var placements = uniqueBy([preferredPlacement].concat(fallbackPlacements).reduce(function (acc, placement) {
  1105. return getBasePlacement(placement) === auto ? acc.concat(computeAutoPlacement(state, {
  1106. placement: placement,
  1107. boundary: boundary,
  1108. rootBoundary: rootBoundary,
  1109. padding: padding,
  1110. flipVariations: flipVariations
  1111. })) : acc.concat(placement);
  1112. }, []), function (placement) {
  1113. return placement;
  1114. });
  1115. var referenceRect = state.rects.reference;
  1116. var popperRect = state.rects.popper;
  1117. var checksMap = new Map();
  1118. var makeFallbackChecks = true;
  1119. var firstFittingPlacement = placements[0];
  1120. for (var i = 0; i < placements.length; i++) {
  1121. var placement = placements[i];
  1122. var _basePlacement = getBasePlacement(placement);
  1123. var isStartVariation = getVariation(placement) === start;
  1124. var isVertical = [top, bottom].includes(_basePlacement);
  1125. var len = isVertical ? 'width' : 'height';
  1126. var overflow = detectOverflow(state, {
  1127. placement: placement,
  1128. boundary: boundary,
  1129. rootBoundary: rootBoundary,
  1130. padding: padding
  1131. });
  1132. var mainVariationSide = isVertical ? isStartVariation ? right : left : isStartVariation ? bottom : top;
  1133. if (referenceRect[len] > popperRect[len]) {
  1134. mainVariationSide = getOppositePlacement(mainVariationSide);
  1135. }
  1136. var altVariationSide = getOppositePlacement(mainVariationSide);
  1137. var checks = [overflow[_basePlacement] <= 0, overflow[mainVariationSide] <= 0, overflow[altVariationSide] <= 0];
  1138. if (checks.every(function (check) {
  1139. return check;
  1140. })) {
  1141. firstFittingPlacement = placement;
  1142. makeFallbackChecks = false;
  1143. break;
  1144. }
  1145. checksMap.set(placement, checks);
  1146. }
  1147. if (makeFallbackChecks) {
  1148. // `2` may be desired in some cases – research later
  1149. var numberOfChecks = flipVariations ? 3 : 1;
  1150. var _loop = function _loop(_i) {
  1151. var fittingPlacement = placements.find(function (placement) {
  1152. var checks = checksMap.get(placement);
  1153. if (checks) {
  1154. return checks.slice(0, _i).every(function (check) {
  1155. return check;
  1156. });
  1157. }
  1158. });
  1159. if (fittingPlacement) {
  1160. firstFittingPlacement = fittingPlacement;
  1161. return "break";
  1162. }
  1163. };
  1164. for (var _i = numberOfChecks; _i > 0; _i--) {
  1165. var _ret = _loop(_i);
  1166. if (_ret === "break") break;
  1167. }
  1168. }
  1169. if (state.placement !== firstFittingPlacement) {
  1170. state.modifiersData[name]._skip = true;
  1171. state.placement = firstFittingPlacement;
  1172. state.reset = true;
  1173. }
  1174. }
  1175. var flip$1 = {
  1176. name: 'flip',
  1177. enabled: true,
  1178. phase: 'main',
  1179. fn: flip,
  1180. requiresIfExists: ['offset'],
  1181. data: {
  1182. _skip: false
  1183. }
  1184. };
  1185. function getAltAxis(axis) {
  1186. return axis === 'x' ? 'y' : 'x';
  1187. }
  1188. function within(min, value, max) {
  1189. return Math.max(min, Math.min(value, max));
  1190. }
  1191. function preventOverflow(_ref) {
  1192. var state = _ref.state,
  1193. options = _ref.options,
  1194. name = _ref.name;
  1195. var _options$mainAxis = options.mainAxis,
  1196. checkMainAxis = _options$mainAxis === void 0 ? true : _options$mainAxis,
  1197. _options$altAxis = options.altAxis,
  1198. checkAltAxis = _options$altAxis === void 0 ? false : _options$altAxis,
  1199. boundary = options.boundary,
  1200. rootBoundary = options.rootBoundary,
  1201. padding = options.padding,
  1202. _options$tether = options.tether,
  1203. tether = _options$tether === void 0 ? true : _options$tether,
  1204. _options$tetherOffset = options.tetherOffset,
  1205. tetherOffset = _options$tetherOffset === void 0 ? 0 : _options$tetherOffset;
  1206. var overflow = detectOverflow(state, {
  1207. boundary: boundary,
  1208. rootBoundary: rootBoundary,
  1209. padding: padding
  1210. });
  1211. var basePlacement = getBasePlacement(state.placement);
  1212. var variation = getVariation(state.placement);
  1213. var isBasePlacement = !variation;
  1214. var mainAxis = getMainAxisFromPlacement(basePlacement);
  1215. var altAxis = getAltAxis(mainAxis);
  1216. var popperOffsets = state.modifiersData.popperOffsets;
  1217. var referenceRect = state.rects.reference;
  1218. var popperRect = state.rects.popper;
  1219. var tetherOffsetValue = typeof tetherOffset === 'function' ? tetherOffset(Object.assign({}, state.rects, {
  1220. placement: state.placement
  1221. })) : tetherOffset;
  1222. var data = {
  1223. x: 0,
  1224. y: 0
  1225. };
  1226. if (checkMainAxis) {
  1227. var mainSide = mainAxis === 'y' ? top : left;
  1228. var altSide = mainAxis === 'y' ? bottom : right;
  1229. var len = mainAxis === 'y' ? 'height' : 'width';
  1230. var offset = popperOffsets[mainAxis];
  1231. var min = popperOffsets[mainAxis] + overflow[mainSide];
  1232. var max = popperOffsets[mainAxis] - overflow[altSide];
  1233. var additive = tether ? -popperRect[len] / 2 : 0;
  1234. var minLen = variation === start ? referenceRect[len] : popperRect[len];
  1235. var maxLen = variation === start ? -popperRect[len] : -referenceRect[len]; // We need to include the arrow in the calculation so the arrow doesn't go
  1236. // outside the reference bounds
  1237. var arrowElement = state.elements.arrow;
  1238. var arrowRect = tether && arrowElement ? getLayoutRect(arrowElement) : {
  1239. width: 0,
  1240. height: 0
  1241. };
  1242. var arrowPaddingObject = state.modifiersData['arrow#persistent'] ? state.modifiersData['arrow#persistent'].padding : getFreshSideObject();
  1243. var arrowPaddingMin = arrowPaddingObject[mainSide];
  1244. var arrowPaddingMax = arrowPaddingObject[altSide]; // If the reference length is smaller than the arrow length, we don't want
  1245. // to include its full size in the calculation. If the reference is small
  1246. // and near the edge of a boundary, the popper can overflow even if the
  1247. // reference is not overflowing as well (e.g. virtual elements with no
  1248. // width or height)
  1249. var arrowLen = within(0, Math.abs(referenceRect[len] - arrowRect[len]), arrowRect[len]);
  1250. var minOffset = isBasePlacement ? referenceRect[len] / 2 - additive - arrowLen - arrowPaddingMin - tetherOffsetValue : minLen - arrowLen - arrowPaddingMin - tetherOffsetValue;
  1251. var maxOffset = isBasePlacement ? -referenceRect[len] / 2 + additive + arrowLen + arrowPaddingMax + tetherOffsetValue : maxLen + arrowLen + arrowPaddingMax + tetherOffsetValue;
  1252. var offsetModifierValue = state.modifiersData.offset ? state.modifiersData.offset[state.placement][mainAxis] : 0;
  1253. var tetherMin = state.modifiersData.popperOffsets[mainAxis] + minOffset - offsetModifierValue;
  1254. var tetherMax = state.modifiersData.popperOffsets[mainAxis] + maxOffset - offsetModifierValue;
  1255. var preventedOffset = within(tether ? Math.min(min, tetherMin) : min, offset, tether ? Math.max(max, tetherMax) : max);
  1256. state.modifiersData.popperOffsets[mainAxis] = preventedOffset;
  1257. data[mainAxis] = preventedOffset - offset;
  1258. }
  1259. if (checkAltAxis) {
  1260. var _mainSide = mainAxis === 'x' ? top : left;
  1261. var _altSide = mainAxis === 'x' ? bottom : right;
  1262. var _offset = popperOffsets[altAxis];
  1263. var _min = _offset + overflow[_mainSide];
  1264. var _max = _offset - overflow[_altSide];
  1265. var _preventedOffset = within(_min, _offset, _max);
  1266. state.modifiersData.popperOffsets[altAxis] = _preventedOffset;
  1267. data[altAxis] = _preventedOffset - _offset;
  1268. }
  1269. state.modifiersData[name] = data;
  1270. }
  1271. var preventOverflow$1 = {
  1272. name: 'preventOverflow',
  1273. enabled: true,
  1274. phase: 'main',
  1275. fn: preventOverflow,
  1276. requiresIfExists: ['offset']
  1277. };
  1278. function arrow(_ref) {
  1279. var _state$modifiersData$;
  1280. var state = _ref.state,
  1281. name = _ref.name;
  1282. var arrowElement = state.elements.arrow;
  1283. var popperOffsets = state.modifiersData.popperOffsets;
  1284. var basePlacement = getBasePlacement(state.placement);
  1285. var axis = getMainAxisFromPlacement(basePlacement);
  1286. var isVertical = [left, right].includes(basePlacement);
  1287. var len = isVertical ? 'height' : 'width';
  1288. if (!arrowElement) {
  1289. return;
  1290. }
  1291. var paddingObject = state.modifiersData[name + "#persistent"].padding;
  1292. var arrowRect = getLayoutRect(arrowElement);
  1293. var minProp = axis === 'y' ? top : left;
  1294. var maxProp = axis === 'y' ? bottom : right;
  1295. var endDiff = state.rects.reference[len] + state.rects.reference[axis] - popperOffsets[axis] - state.rects.popper[len];
  1296. var startDiff = popperOffsets[axis] - state.rects.reference[axis];
  1297. var centerToReference = endDiff / 2 - startDiff / 2; // Make sure the arrow doesn't overflow the popper if the center point is
  1298. // outside of the popper bounds
  1299. var center = within(paddingObject[minProp], state.rects.popper[len] / 2 - arrowRect[len] / 2 + centerToReference, state.rects.popper[len] - arrowRect[len] - paddingObject[maxProp]); // Prevents breaking syntax highlighting...
  1300. var axisProp = axis;
  1301. state.modifiersData[name] = (_state$modifiersData$ = {}, _state$modifiersData$[axisProp] = center, _state$modifiersData$);
  1302. }
  1303. function effect$2(_ref2) {
  1304. var state = _ref2.state,
  1305. options = _ref2.options,
  1306. name = _ref2.name;
  1307. var _options$element = options.element,
  1308. arrowElement = _options$element === void 0 ? '[data-popper-arrow]' : _options$element,
  1309. _options$padding = options.padding,
  1310. padding = _options$padding === void 0 ? 0 : _options$padding; // CSS selector
  1311. if (typeof arrowElement === 'string') {
  1312. arrowElement = state.elements.popper.querySelector(arrowElement);
  1313. if (!arrowElement) {
  1314. return;
  1315. }
  1316. }
  1317. if (!contains(state.elements.popper, arrowElement)) {
  1318. {
  1319. console.error(['Popper: "arrow" modifier\'s `element` must be a child of the popper', 'element.'].join(' '));
  1320. }
  1321. return;
  1322. }
  1323. state.elements.arrow = arrowElement;
  1324. state.modifiersData[name + "#persistent"] = {
  1325. padding: mergePaddingObject(typeof padding !== 'number' ? padding : expandToHashMap(padding, basePlacements))
  1326. };
  1327. }
  1328. var arrow$1 = {
  1329. name: 'arrow',
  1330. enabled: true,
  1331. phase: 'main',
  1332. fn: arrow,
  1333. effect: effect$2,
  1334. requires: ['popperOffsets'],
  1335. requiresIfExists: ['preventOverflow']
  1336. };
  1337. function getSideOffsets(overflow, rect, preventedOffsets) {
  1338. if (preventedOffsets === void 0) {
  1339. preventedOffsets = {
  1340. x: 0,
  1341. y: 0
  1342. };
  1343. }
  1344. return {
  1345. top: overflow.top - rect.height - preventedOffsets.y,
  1346. right: overflow.right - rect.width + preventedOffsets.x,
  1347. bottom: overflow.bottom - rect.height + preventedOffsets.y,
  1348. left: overflow.left - rect.width - preventedOffsets.x
  1349. };
  1350. }
  1351. function isAnySideFullyClipped(overflow) {
  1352. return [top, right, bottom, left].some(function (side) {
  1353. return overflow[side] >= 0;
  1354. });
  1355. }
  1356. function hide(_ref) {
  1357. var state = _ref.state,
  1358. name = _ref.name;
  1359. var referenceRect = state.rects.reference;
  1360. var popperRect = state.rects.popper;
  1361. var preventedOffsets = state.modifiersData.preventOverflow;
  1362. var referenceOverflow = detectOverflow(state, {
  1363. elementContext: 'reference'
  1364. });
  1365. var popperAltOverflow = detectOverflow(state, {
  1366. altBoundary: true
  1367. });
  1368. var referenceClippingOffsets = getSideOffsets(referenceOverflow, referenceRect);
  1369. var popperEscapeOffsets = getSideOffsets(popperAltOverflow, popperRect, preventedOffsets);
  1370. var isReferenceHidden = isAnySideFullyClipped(referenceClippingOffsets);
  1371. var hasPopperEscaped = isAnySideFullyClipped(popperEscapeOffsets);
  1372. state.modifiersData[name] = {
  1373. referenceClippingOffsets: referenceClippingOffsets,
  1374. popperEscapeOffsets: popperEscapeOffsets,
  1375. isReferenceHidden: isReferenceHidden,
  1376. hasPopperEscaped: hasPopperEscaped
  1377. };
  1378. state.attributes.popper = Object.assign({}, state.attributes.popper, {
  1379. 'data-popper-reference-hidden': isReferenceHidden,
  1380. 'data-popper-escaped': hasPopperEscaped
  1381. });
  1382. }
  1383. var hide$1 = {
  1384. name: 'hide',
  1385. enabled: true,
  1386. phase: 'main',
  1387. requiresIfExists: ['preventOverflow'],
  1388. fn: hide
  1389. };
  1390. var defaultModifiers = [eventListeners, popperOffsets$1, computeStyles$1, applyStyles$1, offset$1, flip$1, preventOverflow$1, arrow$1, hide$1];
  1391. var createPopper =
  1392. /*#__PURE__*/
  1393. popperGenerator({
  1394. defaultModifiers: defaultModifiers
  1395. }); // eslint-disable-next-line import/no-unused-modules
  1396. exports.createPopper = createPopper;
  1397. exports.defaultModifiers = defaultModifiers;
  1398. exports.popperGenerator = popperGenerator;
  1399. Object.defineProperty(exports, '__esModule', { value: true });
  1400. })));
  1401. //# sourceMappingURL=popper.js.map