-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: consider margins in Popper calculations (#5112)
* fix: consider margins in Popper calculations * WIP * fix entire problem by removing margins * remove extra popper stuff, fix for other elements * fix test
- Loading branch information
Showing
5 changed files
with
112 additions
and
200 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
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,57 @@ | ||
import { useCallback, useRef, useMemo } from 'react'; | ||
import hasClass from 'dom-helpers/hasClass'; | ||
|
||
function getMargins(element) { | ||
const styles = getComputedStyle(element); | ||
|
||
const top = parseFloat(styles.marginTop) || 0; | ||
const right = parseFloat(styles.marginRight) || 0; | ||
const bottom = parseFloat(styles.marginBottom) || 0; | ||
const left = parseFloat(styles.marginLeft) || 0; | ||
return { top, right, bottom, left }; | ||
} | ||
|
||
export default function usePopperMarginModifiers() { | ||
const overlayRef = useRef(null); | ||
const margins = useRef(null); | ||
|
||
return [ | ||
useCallback((overlay) => { | ||
if ( | ||
!overlay || | ||
!(hasClass(overlay, 'popover') || hasClass(overlay, 'dropdown-menu')) | ||
) | ||
return; | ||
|
||
margins.current = getMargins(overlay); | ||
overlay.style.margin = 0; | ||
overlayRef.current = overlay; | ||
}, []), | ||
[ | ||
useMemo(() => { | ||
return { | ||
name: 'offset', | ||
options: { | ||
offset: ({ placement }) => { | ||
if (!margins.current) return [0, 0]; | ||
const { top, left, bottom, right } = margins.current; | ||
|
||
switch (placement.split('-')[0]) { | ||
case 'top': | ||
return [0, bottom]; | ||
case 'left': | ||
return [0, right]; | ||
case 'bottom': | ||
return [0, top]; | ||
case 'right': | ||
return [0, left]; | ||
default: | ||
return [0, 0]; | ||
} | ||
}, | ||
}, | ||
}; | ||
}, [margins]), | ||
], | ||
]; | ||
} |
Oops, something went wrong.