This repository has been archived by the owner on Jun 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Fix default positioning for Arrow component
- Loading branch information
1 parent
114cfe3
commit b664597
Showing
4 changed files
with
97 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* Given a popper.js placement string, this function | ||
* returns a CSS style object containing sensible default | ||
* styles for the placement string. | ||
* | ||
* The values can be customised, but default to: | ||
* - 100% for the primary direction | ||
* - 8 pixels for the secondary direction | ||
* | ||
* When no secondary direction is defined, styles for centering | ||
* are returned which can be customised with an offset: | ||
* `calc(50% + ${centerOffset})` | ||
* | ||
* Examples: | ||
* getArrowPosition('bottom-start') // returns {bottom: '100%', left: 8} | ||
* getArrowPosition('bottom-start', {defaultSecondaryValue: 4}) // returns {bottom: '100%', left: 4} | ||
* getArrowPosition('left-end') // returns {left: '100%', bottom: 8} | ||
* getArrowPosition('right') // returns {right: '100%', top: 50%} | ||
* getArrowPosition('right', {centerOffset: '-4px'}) // returns {right: '100%', top: calc(50% - 4px)} | ||
*/ | ||
|
||
function getSecondaryDirection(primary, secondary) { | ||
if (primary === 'left' || primary === 'right') { | ||
return secondary === 'end' ? 'bottom' : 'top'; | ||
} else { | ||
return secondary === 'end' ? 'right' : 'left'; | ||
} | ||
} | ||
|
||
export function getPlacements(placement) { | ||
let primaryPlacement = placement; | ||
let secondaryPlacement; | ||
if (placement.indexOf('-') > -1) { | ||
[primaryPlacement, secondaryPlacement] = placement.split('-'); | ||
} | ||
return [primaryPlacement, secondaryPlacement]; | ||
} | ||
|
||
function getArrowPosition( | ||
placement, | ||
{defaultPrimaryValue = '100%', defaultSecondaryValue = 8, centerOffset} | ||
) { | ||
const [primaryPlacement, secondaryPlacement] = getPlacements(placement); | ||
|
||
return { | ||
[primaryPlacement]: defaultPrimaryValue, | ||
[getSecondaryDirection(secondaryPlacement)]: secondaryPlacement | ||
? defaultSecondaryValue | ||
: centerOffset | ||
? `calc(50% + ${centerOffset})` | ||
: '50%', | ||
}; | ||
} | ||
|
||
export default getArrowPosition; |
This file was deleted.
Oops, something went wrong.