-
Notifications
You must be signed in to change notification settings - Fork 8
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
8899e81
commit 6f26de9
Showing
4 changed files
with
344 additions
and
78 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 clsx from 'clsx'; | ||
import { useRef, useMemo } from 'react'; | ||
import { useMouse } from 'react-use'; | ||
|
||
export function SpotlightCard({ | ||
as: Component = 'div', | ||
from = 'rgba(255,255,255,0.8)', | ||
via = null, | ||
to = 'transparent', | ||
size = 350, | ||
mode = 'before', | ||
hsl = false, | ||
hslMin = 0, | ||
hslMax = 360, | ||
children, | ||
className, | ||
...props | ||
}) { | ||
const container = useRef(null); | ||
|
||
const { elX, elY, elW, elH } = useMouse(container); | ||
|
||
const spotlightColorStops = useMemo(() => { | ||
if (hsl) { | ||
const margin = hslMax - hslMin; | ||
const rate = (elY + elX) / (elH + elW); | ||
const hue = Math.round(margin * rate + hslMin); | ||
|
||
return `hsl(${hue} 80% 70%),transparent`; | ||
} | ||
|
||
return [from, via, to].filter((value) => !!value).join(','); | ||
}, [hsl, hslMax, hslMin, from, via, to, elY, elX, elH, elW]); | ||
|
||
const classes = | ||
mode == 'before' | ||
? `before:absolute before:inset-0 before:bg-[radial-gradient(var(--spotlight-size)_circle_at_var(--x)_var(--y),var(--spotlight-color-stops))]` | ||
: `after:absolute after:inset-0 after:bg-[radial-gradient(var(--spotlight-size)_circle_at_var(--x)_var(--y),var(--spotlight-color-stops))]`; | ||
|
||
return ( | ||
<Component | ||
ref={container} | ||
className={clsx( | ||
'relative transform-gpu overflow-hidden', | ||
classes, | ||
className, | ||
)} | ||
{...props} | ||
style={{ | ||
'--x': `${elX}px`, | ||
'--y': `${elY}px`, | ||
'--spotlight-color-stops': spotlightColorStops, | ||
'--spotlight-size': `${size}px`, | ||
}} | ||
> | ||
{children} | ||
</Component> | ||
); | ||
} |
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.