UNPKG

2.02 kBJavaScriptView Raw
1import getVariation from "./getVariation.js";
2import { variationPlacements, basePlacements, placements as allPlacements } from "../enums.js";
3import detectOverflow from "./detectOverflow.js";
4import getBasePlacement from "./getBasePlacement.js";
5export default function computeAutoPlacement(state, options) {
6 if (options === void 0) {
7 options = {};
8 }
9
10 var _options = options,
11 placement = _options.placement,
12 boundary = _options.boundary,
13 rootBoundary = _options.rootBoundary,
14 padding = _options.padding,
15 flipVariations = _options.flipVariations,
16 _options$allowedAutoP = _options.allowedAutoPlacements,
17 allowedAutoPlacements = _options$allowedAutoP === void 0 ? allPlacements : _options$allowedAutoP;
18 var variation = getVariation(placement);
19 var placements = variation ? flipVariations ? variationPlacements : variationPlacements.filter(function (placement) {
20 return getVariation(placement) === variation;
21 }) : basePlacements;
22 var allowedPlacements = placements.filter(function (placement) {
23 return allowedAutoPlacements.indexOf(placement) >= 0;
24 });
25
26 if (allowedPlacements.length === 0) {
27 allowedPlacements = placements;
28
29 if (process.env.NODE_ENV !== "production") {
30 console.error(['Popper: The `allowedAutoPlacements` option did not allow any', 'placements. Ensure the `placement` option matches the variation', 'of the allowed placements.', 'For example, "auto" cannot be used to allow "bottom-start".', 'Use "auto-start" instead.'].join(' '));
31 }
32 } // $FlowFixMe[incompatible-type]: Flow seems to have problems with two array unions...
33
34
35 var overflows = allowedPlacements.reduce(function (acc, placement) {
36 acc[placement] = detectOverflow(state, {
37 placement: placement,
38 boundary: boundary,
39 rootBoundary: rootBoundary,
40 padding: padding
41 })[getBasePlacement(placement)];
42 return acc;
43 }, {});
44 return Object.keys(overflows).sort(function (a, b) {
45 return overflows[a] - overflows[b];
46 });
47}
\No newline at end of file