-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tooltips for options to explain configuration directly within app…
…lication.
- Loading branch information
Showing
6 changed files
with
260 additions
and
69 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,14 @@ | ||
.tooltip { | ||
position: fixed; | ||
width: 300px; | ||
background-color: rgba(100, 100, 100, 0.3); | ||
color: white; | ||
border-radius: 0.25rem; | ||
backdrop-filter: blur(10px); | ||
-webkit-backdrop-filter: blur(10px); | ||
border: 1px solid rgba(100, 100, 100, 0.35); | ||
box-sizing: border-box; | ||
font-size: 0.95rem; | ||
z-index: 999999; | ||
padding: 0.1rem 0.4rem; | ||
} |
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,66 @@ | ||
import { useEffect, useRef, useState } from 'react' | ||
import styles from './Tooltip.module.css' | ||
|
||
function Tooltip({ x, y, message }: { x: number; y: number; message: string }): JSX.Element { | ||
const ref = useRef<HTMLDivElement>(null) | ||
|
||
return ( | ||
<div | ||
className={`poppins-light ${styles.tooltip}`} | ||
style={{ top: `${y}px`, right: `${x}px`, transform: 'translateY(-50%)' }} | ||
ref={ref} | ||
> | ||
{message} | ||
</div> | ||
) | ||
} | ||
|
||
export default Tooltip | ||
|
||
export function useTooltip( | ||
ref: React.RefObject<HTMLElement>, | ||
message?: string, | ||
gap = 10 | ||
): JSX.Element { | ||
if (!message) return <></> | ||
|
||
const [point, setPoint] = useState({ x: 0, y: 0 }) | ||
const [show, setShow] = useState(false) | ||
|
||
useEffect(() => { | ||
const handleMouseOver = (): void => { | ||
if (ref.current) { | ||
const { innerWidth } = window | ||
|
||
// Determine bottom left corner of the element | ||
const rect = ref.current.getBoundingClientRect() | ||
const x = innerWidth - rect.left + gap | ||
const y = (rect.bottom - rect.top) / 2 + rect.top | ||
|
||
setPoint({ x, y }) | ||
setShow(true) | ||
|
||
// If the right side of the tooltip goes off screen, decrease the width to match | ||
if (x + ref.current.offsetWidth > innerWidth) { | ||
ref.current.style.width = `${innerWidth - x}px` | ||
} else { | ||
ref.current.style.width = '350px' | ||
} | ||
} | ||
} | ||
|
||
const handleMouseOut = (): void => { | ||
setShow(false) | ||
} | ||
|
||
ref.current?.addEventListener('mouseover', handleMouseOver) | ||
ref.current?.addEventListener('mouseout', handleMouseOut) | ||
|
||
return (): void => { | ||
ref.current?.removeEventListener('mouseover', handleMouseOver) | ||
ref.current?.removeEventListener('mouseout', handleMouseOut) | ||
} | ||
}, [ref]) | ||
|
||
return <>{show ? <Tooltip {...point} message={message} /> : null}</> | ||
} |
Oops, something went wrong.