You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
2.0 KiB

  1. import getVariation from "./getVariation.js";
  2. import { variationPlacements, basePlacements, placements as allPlacements } from "../enums.js";
  3. import detectOverflow from "./detectOverflow.js";
  4. import getBasePlacement from "./getBasePlacement.js";
  5. export default function computeAutoPlacement(state, options) {
  6. if (options === void 0) {
  7. options = {};
  8. }
  9. var _options = options,
  10. placement = _options.placement,
  11. boundary = _options.boundary,
  12. rootBoundary = _options.rootBoundary,
  13. padding = _options.padding,
  14. flipVariations = _options.flipVariations,
  15. _options$allowedAutoP = _options.allowedAutoPlacements,
  16. allowedAutoPlacements = _options$allowedAutoP === void 0 ? allPlacements : _options$allowedAutoP;
  17. var variation = getVariation(placement);
  18. var placements = variation ? flipVariations ? variationPlacements : variationPlacements.filter(function (placement) {
  19. return getVariation(placement) === variation;
  20. }) : basePlacements;
  21. var allowedPlacements = placements.filter(function (placement) {
  22. return allowedAutoPlacements.indexOf(placement) >= 0;
  23. });
  24. if (allowedPlacements.length === 0) {
  25. allowedPlacements = placements;
  26. if (process.env.NODE_ENV !== "production") {
  27. 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(' '));
  28. }
  29. } // $FlowFixMe[incompatible-type]: Flow seems to have problems with two array unions...
  30. var overflows = allowedPlacements.reduce(function (acc, placement) {
  31. acc[placement] = detectOverflow(state, {
  32. placement: placement,
  33. boundary: boundary,
  34. rootBoundary: rootBoundary,
  35. padding: padding
  36. })[getBasePlacement(placement)];
  37. return acc;
  38. }, {});
  39. return Object.keys(overflows).sort(function (a, b) {
  40. return overflows[a] - overflows[b];
  41. });
  42. }