-
-
Notifications
You must be signed in to change notification settings - Fork 755
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2680 from udecode/docs/star-on-github
Star on github
- Loading branch information
Showing
6 changed files
with
325 additions
and
169 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,81 @@ | ||
'use client'; | ||
|
||
import { useEffect, useMemo, useRef, useState } from 'react'; | ||
import { useInView } from 'framer-motion'; | ||
|
||
export const useCounting = ({ | ||
duration, | ||
end, | ||
interval, | ||
isInView, | ||
reverse, | ||
start, | ||
}: { | ||
start: number; | ||
end: number; | ||
interval: number; | ||
duration: number; | ||
reverse: boolean; | ||
isInView: boolean; | ||
}) => { | ||
const [number, setNumber] = useState(start); | ||
const increment = | ||
Math.floor(Math.abs(start - end) / (duration / interval)) || 1; | ||
|
||
useEffect(() => { | ||
let timer: NodeJS.Timeout; | ||
if (isInView) { | ||
timer = setInterval(() => { | ||
setNumber((prevNumber) => { | ||
const newNumber = reverse | ||
? prevNumber - increment | ||
: prevNumber + increment; | ||
const isCompleted = reverse ? newNumber <= end : newNumber >= end; | ||
|
||
if (isCompleted) { | ||
clearInterval(timer); | ||
return end; | ||
} | ||
|
||
return newNumber; | ||
}); | ||
}, interval); | ||
} | ||
|
||
return () => clearInterval(timer); // Cleanup timer | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [isInView]); | ||
|
||
return number; | ||
}; | ||
|
||
export function CountingNumbers({ | ||
value, | ||
className, | ||
reverse = false, | ||
start = reverse ? 1000 : 0, | ||
interval = 10, | ||
duration = 800, | ||
}) { | ||
const ref = useRef(null); | ||
const isInView = useInView(ref); | ||
const number = useCounting({ | ||
start, | ||
end: value, | ||
interval, | ||
duration, | ||
reverse, | ||
isInView, | ||
}); | ||
|
||
const formattedNumber = useMemo( | ||
() => Intl.NumberFormat().format(number), | ||
[number] | ||
); | ||
|
||
return ( | ||
<p className={className} ref={ref}> | ||
{formattedNumber} | ||
</p> | ||
); | ||
} |
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,54 @@ | ||
'use client'; | ||
|
||
import Link from 'next/link'; | ||
import { StarIcon } from 'lucide-react'; | ||
|
||
import { siteConfig } from '@/config/site'; | ||
import { cn } from '@/lib/utils'; | ||
import { buttonVariants } from '@/registry/default/plate-ui/button'; | ||
|
||
import { CountingNumbers } from './counting-numbers'; | ||
import { Icons } from './icons'; | ||
|
||
export function StarOnGithub({ count }: { count: number }) { | ||
return ( | ||
<div className={cn('mx-2 hidden md:block', count > 0 && 'min-w-[225px]')}> | ||
<Link | ||
className={cn( | ||
buttonVariants(), | ||
'group relative flex w-full justify-start gap-2 overflow-hidden whitespace-pre rounded-sm', | ||
'dark:bg-muted dark:text-foreground', | ||
'hover:ring-2 hover:ring-primary hover:ring-offset-2', | ||
'transition-all duration-300 ease-out' | ||
)} | ||
target="_blank" | ||
href={siteConfig.links.github} | ||
> | ||
<span | ||
className={cn( | ||
'absolute right-0 -mt-12 h-32 w-8 translate-x-12 rotate-12', | ||
'bg-white opacity-10', | ||
'transition-all duration-1000 ease-out ', | ||
cn( | ||
count > 0 | ||
? 'group-hover:translate-x-[-181px]' | ||
: 'group-hover:translate-x-[-135px]' | ||
) | ||
)} | ||
/> | ||
<Icons.gitHub className="h-4 w-4" /> | ||
Star on GitHub | ||
<div className="hidden items-center gap-1 text-sm text-muted-foreground md:flex"> | ||
<StarIcon className="h-4 w-4 transition-all duration-300 group-hover:text-[#e3b341]" /> | ||
|
||
{count > 0 && ( | ||
<CountingNumbers | ||
value={count} | ||
className="font-medium text-background dark:text-foreground" | ||
/> | ||
)} | ||
</div> | ||
</Link> | ||
</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
Oops, something went wrong.