Skip to content

Commit

Permalink
feat: simplify logic
Browse files Browse the repository at this point in the history
  • Loading branch information
chenesan committed Oct 26, 2022
1 parent 1a42394 commit ff78d60
Showing 1 changed file with 16 additions and 50 deletions.
66 changes: 16 additions & 50 deletions packages/core/src/mixins/anchored/getPositionState.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,83 +32,49 @@ const horizontalPlacements = [LEFT, RIGHT];
*
* @returns {{ placement: Placement, remainingSpace: number }}
*/
export function getPlacementAndRemainingSpaceImpl({
possiblePlacements,
export function getPlacementAndRemainingSpace({
defaultPlacement,
anchorRect,
selfRect,
distanceFromAnchor,
}) {
const {
canPlace,
remainingSpace,
} = placementStrategies[defaultPlacement].canPlace({
const possiblePlacements = (
verticalPlacements.includes(defaultPlacement)
? verticalPlacements
: horizontalPlacements
);
const defaultPlacementResult = placementStrategies[defaultPlacement].canPlace({
anchorRect,
selfRect,
distanceFromAnchor,
});
if (canPlace) {
if (defaultPlacementResult.canPlace) {
return {
placement: defaultPlacement,
remainingSpace,
remainingSpace: defaultPlacementResult.remainingSpace,
};
}
const oppositePlacement = possiblePlacements.find(placement => placement !== defaultPlacement);
const {
canPlace: canPlaceInOpposite,
remainingSpace: remainingSpaceInOpposite,
} = placementStrategies[oppositePlacement].canPlace({
const oppositePlacementResult = placementStrategies[oppositePlacement].canPlace({
anchorRect,
selfRect,
distanceFromAnchor,
});
if (canPlaceInOpposite) {
return {
placement: oppositePlacement,
remainingSpace: remainingSpaceInOpposite,
};
}
const placementWithLargerRemaingSpace = (
remainingSpace >= remainingSpaceInOpposite
const placement = (
defaultPlacementResult.remainingSpace >= oppositePlacementResult.remainingSpace
? defaultPlacement
: oppositePlacement
);
return {
placement: placementWithLargerRemaingSpace,
placement,
remainingSpace: (
placementWithLargerRemaingSpace === defaultPlacement
? remainingSpace
: remainingSpaceInOpposite
placement === defaultPlacement
? defaultPlacementResult.remainingSpace
: oppositePlacementResult.remainingSpace
),
};
}

/**
* Determine whether *wrapped component* should be placed above or below
* its *anchor*.
*
* @returns {{ placement: Placement, remainingSpace: number }}
*/
export function getPlacementAndRemainingSpace({
defaultPlacement,
anchorRect,
selfRect,
distanceFromAnchor,
}) {
const possiblePlacements = (
verticalPlacements.includes(defaultPlacement)
? verticalPlacements
: horizontalPlacements
);
return getPlacementAndRemainingSpaceImpl({
possiblePlacements,
defaultPlacement,
anchorRect,
selfRect,
distanceFromAnchor,
});
}

/**
* Get position of *wrapped component* based on spacing around
* the *anchor element*.
Expand Down

0 comments on commit ff78d60

Please sign in to comment.