-
Notifications
You must be signed in to change notification settings - Fork 157
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
Showing
4 changed files
with
132 additions
and
46 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
'use client' | ||
import { cn } from '@site/src/lib/utils' | ||
import { motion, useAnimationFrame, useMotionTemplate, useMotionValue, useTransform } from 'framer-motion' | ||
import React, { useRef } from 'react' | ||
|
||
export function MovingButton({ | ||
borderRadius = '1.75rem', | ||
children, | ||
as: Component = 'div', | ||
containerClassName, | ||
borderClassName, | ||
duration, | ||
className, | ||
...otherProps | ||
}: { | ||
borderRadius?: string | ||
children: React.ReactNode | ||
as?: any | ||
containerClassName?: string | ||
borderClassName?: string | ||
duration?: number | ||
className?: string | ||
[key: string]: any | ||
}) { | ||
return ( | ||
<Component | ||
className={cn('relative h-14 w-auto min-w-32 overflow-hidden bg-transparent p-[2px] text-xl', containerClassName)} | ||
style={{ | ||
borderRadius: borderRadius, | ||
}} | ||
{...otherProps} | ||
> | ||
<div className="absolute inset-0" style={{ borderRadius: `calc(${borderRadius} * 0.96)` }}> | ||
<MovingBorder duration={duration} rx="30%" ry="30%"> | ||
<div | ||
className={cn( | ||
'size-20 bg-[radial-gradient(var(--ifm-color-primary)_40%,transparent_60%)] opacity-[0.8]', | ||
borderClassName, | ||
)} | ||
/> | ||
</MovingBorder> | ||
</div> | ||
|
||
<div | ||
className={cn( | ||
'relative flex h-full w-full items-center justify-center bg-slate-900/[0.8] text-sm antialiased backdrop-blur-xl', | ||
className, | ||
)} | ||
style={{ | ||
borderRadius: `calc(${borderRadius} * 0.96)`, | ||
}} | ||
> | ||
{children} | ||
</div> | ||
</Component> | ||
) | ||
} | ||
|
||
export const MovingBorder = ({ | ||
children, | ||
duration = 5000, | ||
rx, | ||
ry, | ||
...otherProps | ||
}: { | ||
children: React.ReactNode | ||
duration?: number | ||
rx?: string | ||
ry?: string | ||
[key: string]: any | ||
}) => { | ||
const pathRef = useRef<any>() | ||
const progress = useMotionValue<number>(0) | ||
|
||
useAnimationFrame(time => { | ||
const length = pathRef.current?.getTotalLength() | ||
if (length) { | ||
const pxPerMillisecond = length / duration | ||
progress.set((time * pxPerMillisecond) % length) | ||
} | ||
}) | ||
|
||
const x = useTransform(progress, val => pathRef.current?.getPointAtLength(val).x) | ||
const y = useTransform(progress, val => pathRef.current?.getPointAtLength(val).y) | ||
|
||
const transform = useMotionTemplate`translateX(${x}px) translateY(${y}px) translateX(-50%) translateY(-50%)` | ||
|
||
return ( | ||
<> | ||
<svg | ||
xmlns="http://www.w3.org/2000/svg" | ||
preserveAspectRatio="none" | ||
className="absolute h-full w-full" | ||
width="100%" | ||
height="100%" | ||
{...otherProps} | ||
> | ||
<rect fill="none" width="100%" height="100%" rx={rx} ry={ry} ref={pathRef} /> | ||
</svg> | ||
<motion.div | ||
style={{ | ||
position: 'absolute', | ||
top: 0, | ||
left: 0, | ||
display: 'inline-block', | ||
transform, | ||
}} | ||
> | ||
{children} | ||
</motion.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