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.

42 lines
1.6 KiB

2 months ago
  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. } // $FlowFixMe[incompatible-type]: Flow seems to have problems with two array unions...
  27. var overflows = allowedPlacements.reduce(function (acc, placement) {
  28. acc[placement] = detectOverflow(state, {
  29. placement: placement,
  30. boundary: boundary,
  31. rootBoundary: rootBoundary,
  32. padding: padding
  33. })[getBasePlacement(placement)];
  34. return acc;
  35. }, {});
  36. return Object.keys(overflows).sort(function (a, b) {
  37. return overflows[a] - overflows[b];
  38. });
  39. }