-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(Popover): update offset to make sure the popover panel in window (#…
…1608) * fix(Popover): update offset to make sure the popover panel in window * fix(Popover): fix bottom and transform * fix(sonar): fix check issue * fix(eslint): fix eslint issue Co-authored-by: Zhang Rui <zhangrui@growingio.com>
- Loading branch information
Ryan Zhang
and
Zhang Rui
committed
Dec 3, 2021
1 parent
68ba6a9
commit 5557aca
Showing
2 changed files
with
66 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { PositioningStrategy } from '@popperjs/core'; | ||
import { Modifier, usePopper } from 'react-popper'; | ||
import { placements } from './interface'; | ||
|
||
export interface UsePopProps { | ||
referenceElement: any; | ||
popperElement: any; | ||
placement: string; | ||
modifiers: ReadonlyArray<Modifier<any>>; | ||
strategy: PositioningStrategy; | ||
} | ||
|
||
const clear = (text: string) => (replaces: string[]) => { | ||
let replaceText = text; | ||
replaces.forEach((replace) => { | ||
while (replaceText.indexOf(replace) > -1) { | ||
replaceText = replaceText.replace(replace, ''); | ||
} | ||
}); | ||
return replaceText; | ||
}; | ||
|
||
const clear3D = (transform: string): string | number[] => { | ||
if (!transform) { | ||
return transform; | ||
} | ||
if (transform.indexOf('translate3d') === -1) { | ||
return transform; | ||
} | ||
const text = clear(transform)(['translate3d(', 'px', ')']); | ||
return text.split(',').map((value) => Number(value)); | ||
}; | ||
|
||
const usePop = ({ referenceElement, popperElement, placement, modifiers, strategy }: UsePopProps) => { | ||
const { styles, attributes, ...popperProps } = usePopper(referenceElement, popperElement, { | ||
placement: placements[placement], | ||
modifiers, | ||
strategy, | ||
}); | ||
if (popperElement) { | ||
const three = clear3D(styles?.popper?.transform as string); | ||
if (Array.isArray(three)) { | ||
const [x, y, z] = three; | ||
const divHeight = popperElement.offsetHeight; | ||
const winHeight = window.innerHeight; | ||
if (styles?.popper?.bottom === 'auto') { | ||
let yField = y < 0 ? 0 : y; | ||
yField = yField + divHeight > winHeight ? winHeight - divHeight : yField; | ||
styles.popper.transform = `translate3d(${x}px, ${yField}px, ${z}px)`; | ||
} else if (styles?.popper?.bottom === '0') { | ||
const yField = y + divHeight > 0 ? 0 : y; | ||
styles.popper.transform = `translate3d(${x}px, ${yField}px, ${z}px)`; | ||
} | ||
} | ||
} | ||
return { styles, attributes, ...popperProps }; | ||
}; | ||
|
||
export default usePop; |
5557aca
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs: