-
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.
- Loading branch information
1 parent
2686a47
commit 6d047cc
Showing
13 changed files
with
233 additions
and
152 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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,31 @@ | ||
.root { | ||
position: relative; | ||
width: fit-content; | ||
} | ||
|
||
.trigger { | ||
display: inline-block; | ||
} | ||
|
||
.content { | ||
position: absolute; | ||
z-index: 1; | ||
|
||
&.enter { | ||
opacity: 0; | ||
} | ||
|
||
&.enter-active { | ||
opacity: 1; | ||
transition: all 200ms ease-in-out; | ||
} | ||
|
||
&.exit { | ||
opacity: 1; | ||
} | ||
|
||
&.exit-active { | ||
opacity: 0; | ||
transition: all 200ms ease-in-out; | ||
} | ||
} |
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,124 @@ | ||
import { cloneElement, useEffect, useMemo, useRef, useState } from "react"; | ||
import styles from "./Popover.module.scss"; | ||
import { CSSTransition } from "react-transition-group"; | ||
|
||
export interface PopoverProps { | ||
/** | ||
* The content of the popover. | ||
*/ | ||
children: React.ReactElement; | ||
/** | ||
* The offset of the popover. | ||
*/ | ||
offsetY?: number; | ||
/** | ||
* The offset of the popover. | ||
*/ | ||
offsetX?: number; | ||
/** | ||
* The trigger of the popover. | ||
*/ | ||
content: React.ReactElement; | ||
/** | ||
* Whether the popover is opened or not. | ||
*/ | ||
opened: boolean; | ||
/** | ||
* The callback function when the popover is opened. | ||
*/ | ||
onChange: (opened: boolean) => void; | ||
} | ||
|
||
export function Popover(props: PopoverProps) { | ||
const { | ||
children, | ||
offsetY = 10, | ||
offsetX = 0, | ||
content, | ||
opened, | ||
onChange, | ||
} = props; | ||
|
||
const contentRef = useRef<HTMLDivElement>(null); | ||
const triggerRef = useRef<HTMLElement>(null); | ||
|
||
const [position, setPosition] = useState<"top" | "bottom">("bottom"); | ||
|
||
useEffect(() => { | ||
if (triggerRef.current && opened) { | ||
const triggerRect = triggerRef.current.getBoundingClientRect(); | ||
const viewportHeight = window.innerHeight; | ||
|
||
const spaceBelow = viewportHeight - triggerRect.bottom - offsetY; | ||
const spaceAbove = triggerRect.top - offsetY; | ||
|
||
if ( | ||
spaceBelow < (contentRef?.current?.offsetHeight || 0) && | ||
spaceAbove > (contentRef?.current?.offsetHeight || 0) | ||
) { | ||
setPosition("top"); | ||
} else { | ||
setPosition("bottom"); | ||
} | ||
} | ||
}, [opened, offsetY]); | ||
|
||
const positionStyle = useMemo(() => { | ||
switch (position) { | ||
case "top": | ||
return { | ||
bottom: `calc(100% + ${offsetY}px)`, | ||
right: `${offsetX}px`, | ||
}; | ||
case "bottom": | ||
default: | ||
return { | ||
top: `calc(100% + ${offsetY}px)`, | ||
right: `${offsetX}px`, | ||
}; | ||
} | ||
}, [position, offsetY, offsetX]); | ||
|
||
useEffect(() => { | ||
const handleClickOutside = (event: MouseEvent) => { | ||
if ( | ||
contentRef.current && | ||
!contentRef.current.contains(event.target as Node) && | ||
triggerRef && | ||
!triggerRef.current?.contains(event.target as Node) | ||
) { | ||
onChange(false); | ||
} | ||
}; | ||
document.addEventListener("mousedown", handleClickOutside); | ||
return () => { | ||
document.removeEventListener("mousedown", handleClickOutside); | ||
}; | ||
}, []); | ||
|
||
return ( | ||
<div className={styles["root"]}> | ||
{cloneElement(children, { ref: triggerRef })} | ||
<CSSTransition | ||
in={opened} | ||
timeout={300} | ||
unmountOnExit | ||
nodeRef={contentRef} | ||
classNames={{ | ||
enter: styles["enter"], | ||
enterActive: styles["enter-active"], | ||
exit: styles["exit"], | ||
exitActive: styles["exit-active"], | ||
}} | ||
> | ||
<div | ||
className={styles["content"]} | ||
style={positionStyle} | ||
ref={contentRef} | ||
> | ||
{content} | ||
</div> | ||
</CSSTransition> | ||
</div> | ||
); | ||
} |
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 @@ | ||
export { Popover, type PopoverProps } from "./Popover"; |
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
Oops, something went wrong.