Skip to content

Commit ad1ea47

Browse files
feat(website): cycle navbar counters and count all pull requests
- Query all PRs (is:pr) instead of open PRs so count reflects project activity - Add RotatingGitHubCounter to cycle stars -> forks -> PRs -> stars every 3s - Pause cycling on hover and focus; fallback to static stars on prefers-reduced-motion - Fixed width across metrics to prevent navbar layout shifts - Add comprehensive unit tests covering cycle order, pause, reduced motion, and skip logic
1 parent 66b20f0 commit ad1ea47

5 files changed

Lines changed: 360 additions & 12 deletions

File tree

‎src/app/page.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export default async function Home() {
1717

1818
return (
1919
<div className="flex flex-1 flex-col">
20-
<Navbar starsCount={stats.stars} />
20+
<Navbar stats={stats} />
2121
<main>
2222
<Hero />
2323
<Features />

‎src/components/landing/github-counter.tsx‎

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as React from "react";
22
import { Star, GitFork, GitPullRequest } from "lucide-react";
33
import { cn } from "@/lib/utils";
4+
import type { GitHubStats } from "@/lib/github";
45

56
export type GitHubMetric = "stars" | "forks" | "prs";
67

@@ -125,3 +126,151 @@ export function GitHubCounter({
125126
</a>
126127
);
127128
}
129+
130+
export interface RotatingGitHubCounterProps extends Omit<
131+
React.AnchorHTMLAttributes<HTMLAnchorElement>,
132+
"children"
133+
> {
134+
stats?: GitHubStats;
135+
intervalMs?: number;
136+
className?: string;
137+
}
138+
139+
/**
140+
* Navbar GitHub counter that smoothly cycles between available traction metrics:
141+
* stars → forks → PRs → stars (every 3 seconds).
142+
*
143+
* Features:
144+
* - Client component; data fetched on server and passed via props.
145+
* - Short fade/slide transition between metrics.
146+
* - Link (href) and aria-label follow the currently visible metric.
147+
* - Pauses rotation on hover and focus.
148+
* - Respects prefers-reduced-motion: no rotation, stays on stars.
149+
* - No aria-live (avoids screen reader announcement spam every 3s).
150+
* - Fixed width across metrics to prevent navbar layout shifts.
151+
* - Skips metrics with no count (< 10 or null). If none have a count, renders static stars pill.
152+
*/
153+
export function RotatingGitHubCounter({
154+
stats,
155+
intervalMs = 3000,
156+
className,
157+
...props
158+
}: RotatingGitHubCounterProps) {
159+
const [currentIndex, setCurrentIndex] = React.useState(0);
160+
const [isPaused, setIsPaused] = React.useState(false);
161+
const [prefersReducedMotion, setPrefersReducedMotion] = React.useState(false);
162+
163+
React.useEffect(() => {
164+
if (typeof window === "undefined" || !window.matchMedia) return;
165+
const mediaQuery = window.matchMedia("(prefers-reduced-motion: reduce)");
166+
setPrefersReducedMotion(mediaQuery.matches);
167+
168+
const handleChange = (e: MediaQueryListEvent) => {
169+
setPrefersReducedMotion(e.matches);
170+
};
171+
172+
if (mediaQuery.addEventListener) {
173+
mediaQuery.addEventListener("change", handleChange);
174+
return () => mediaQuery.removeEventListener("change", handleChange);
175+
} else if (mediaQuery.addListener) {
176+
mediaQuery.addListener(handleChange);
177+
return () => mediaQuery.removeListener(handleChange);
178+
}
179+
}, []);
180+
181+
// Filter metrics that have a valid formatted count (>= 10)
182+
// Preserves strict cycle order: stars -> forks -> prs
183+
const availableMetrics = React.useMemo(() => {
184+
const list: GitHubMetric[] = [];
185+
if (formatGitHubCount(stats?.stars) !== null) list.push("stars");
186+
if (formatGitHubCount(stats?.forks) !== null) list.push("forks");
187+
if (formatGitHubCount(stats?.prs) !== null) list.push("prs");
188+
return list;
189+
}, [stats?.stars, stats?.forks, stats?.prs]);
190+
191+
const hasValidCounts = availableMetrics.length > 0;
192+
193+
// Rotation timer: cycle every 3s, pause on hover/focus, no rotation on reduced motion
194+
React.useEffect(() => {
195+
if (prefersReducedMotion || isPaused || availableMetrics.length <= 1) {
196+
return;
197+
}
198+
199+
const timer = setInterval(() => {
200+
setCurrentIndex((prev) => (prev + 1) % availableMetrics.length);
201+
}, intervalMs);
202+
203+
return () => clearInterval(timer);
204+
}, [prefersReducedMotion, isPaused, availableMetrics.length, intervalMs]);
205+
206+
// If none have a count, show the static stars pill
207+
if (!hasValidCounts) {
208+
return <GitHubCounter metric="stars" count={null} className={className} {...props} />;
209+
}
210+
211+
// Active metric: force stars if prefers-reduced-motion is true
212+
const activeMetric: GitHubMetric = prefersReducedMotion
213+
? "stars"
214+
: availableMetrics[currentIndex % availableMetrics.length];
215+
216+
const config = metricConfig[activeMetric] ?? metricConfig.stars;
217+
const rawCount = stats?.[activeMetric] ?? null;
218+
const formattedCount = formatGitHubCount(rawCount);
219+
const MetricIcon = config.icon;
220+
221+
const ariaLabel = formattedCount
222+
? `${config.label} (${formattedCount} ${config.noun})`
223+
: config.label;
224+
225+
return (
226+
<a
227+
href={props.href ?? config.href}
228+
target="_blank"
229+
rel="noopener noreferrer"
230+
aria-label={ariaLabel}
231+
onMouseEnter={() => setIsPaused(true)}
232+
onMouseLeave={() => setIsPaused(false)}
233+
onFocus={() => setIsPaused(true)}
234+
onBlur={() => setIsPaused(false)}
235+
className={cn(
236+
"group inline-flex h-9 w-[136px] items-center justify-between rounded-full border border-white/[0.1] bg-white/[0.03] text-sm text-white/80 transition-colors hover:bg-white/5 hover:text-white",
237+
className,
238+
)}
239+
{...props}
240+
>
241+
{/* Left: GitHub logo */}
242+
<span className="flex items-center justify-center pl-3 pr-2.5">
243+
<svg
244+
width="16"
245+
height="16"
246+
viewBox="0 0 24 24"
247+
fill="currentColor"
248+
aria-hidden="true"
249+
className="size-4 text-white/80 transition-colors group-hover:text-white"
250+
>
251+
<path d="M12 .297c-6.63 0-12 5.373-12 12 0 5.303 3.438 9.8 8.205 11.385.6.113.82-.258.82-.577 0-.285-.01-1.04-.015-2.04-3.338.724-4.042-1.61-4.042-1.61C4.422 18.07 3.633 17.7 3.633 17.7c-1.087-.744.084-.729.084-.729 1.205.084 1.838 1.236 1.838 1.236 1.07 1.835 2.809 1.305 3.495.998.108-.776.417-1.305.76-1.605-2.665-.3-5.466-1.332-5.466-5.93 0-1.31.465-2.38 1.235-3.22-.135-.303-.54-1.523.105-3.176 0 0 1.005-.322 3.3 1.23.96-.267 1.98-.399 3-.405 1.02.006 2.04.138 3 .405 2.28-1.552 3.285-1.23 3.285-1.23.645 1.653.24 2.873.12 3.176.765.84 1.23 1.91 1.23 3.22 0 4.61-2.805 5.625-5.475 5.92.42.36.81 1.096.81 2.22 0 1.606-.015 2.896-.015 3.286 0 .315.21.69.825.57C20.565 22.092 24 17.592 24 12.297c0-6.627-5.373-12-12-12" />
252+
</svg>
253+
</span>
254+
255+
{/* 1px vertical divider */}
256+
<span className="h-3.5 w-px bg-white/[0.1]" aria-hidden="true" />
257+
258+
{/* Right: Count and metric icon with fade/slide transition */}
259+
<span className="flex flex-1 items-center justify-center gap-1.5 pl-2 pr-3 overflow-hidden">
260+
<span
261+
key={activeMetric}
262+
className="inline-flex items-center gap-1.5 transition-all duration-300 motion-safe:animate-in motion-safe:fade-in motion-safe:slide-in-from-bottom-1"
263+
>
264+
{formattedCount && (
265+
<span className="text-xs font-medium sm:text-sm">{formattedCount}</span>
266+
)}
267+
<MetricIcon
268+
size={14}
269+
className="text-white/70 transition-colors group-hover:text-white"
270+
aria-hidden="true"
271+
/>
272+
</span>
273+
</span>
274+
</a>
275+
);
276+
}

‎src/components/landing/navbar.tsx‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import Link from "next/link";
44
import Image from "next/image";
55
import { useState } from "react";
66
import { Menu, X, CornerDownLeft } from "lucide-react";
7-
import { GitHubCounter } from "./github-counter";
7+
import { GitHubCounter, RotatingGitHubCounter } from "./github-counter";
8+
import type { GitHubStats } from "@/lib/github";
89

910
const navLinks = [
1011
{ label: "Home", href: "/" },
@@ -14,11 +15,14 @@ const navLinks = [
1415
];
1516

1617
export interface NavbarProps {
18+
stats?: GitHubStats;
1719
starsCount?: number | null;
1820
}
1921

20-
export function Navbar({ starsCount }: NavbarProps = {}) {
22+
export function Navbar({ stats, starsCount }: NavbarProps = {}) {
2123
const [isOpen, setIsOpen] = useState(false);
24+
const resolvedStats = stats ?? (starsCount !== undefined ? { stars: starsCount } : undefined);
25+
const resolvedStarsCount = stats?.stars ?? starsCount;
2226

2327
return (
2428
<nav className="fixed right-0 left-0 z-50 mx-auto flex max-w-7xl justify-center top-2 px-6 py-4 transition-all duration-300">
@@ -44,14 +48,14 @@ export function Navbar({ starsCount }: NavbarProps = {}) {
4448

4549
{/* Right side actions */}
4650
<div className="flex items-center gap-2 sm:gap-3">
47-
{/* GitHub counter - desktop */}
51+
{/* GitHub counter - desktop (cycles stars → forks → PRs) */}
4852
<div className="hidden sm:block">
49-
<GitHubCounter metric="stars" count={starsCount} />
53+
<RotatingGitHubCounter stats={resolvedStats} />
5054
</div>
5155

5256
{/* GitHub counter - mobile */}
5357
<div className="sm:hidden">
54-
<GitHubCounter metric="stars" count={starsCount} hideCountOnMobile />
58+
<GitHubCounter metric="stars" count={resolvedStarsCount} hideCountOnMobile />
5559
</div>
5660

5761
{/* Get Started button */}
@@ -99,7 +103,7 @@ export function Navbar({ starsCount }: NavbarProps = {}) {
99103
<div className="mt-1 flex justify-center">
100104
<GitHubCounter
101105
metric="stars"
102-
count={starsCount}
106+
count={resolvedStarsCount}
103107
className="w-full justify-center py-2.5 h-auto text-center"
104108
/>
105109
</div>

‎src/lib/github.ts‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ export interface GitHubStats {
55
}
66

77
const REPO_URL = "https://api.github.com/repos/babelize/babelize-elements";
8-
const PRS_URL =
9-
"https://api.github.com/search/issues?q=repo:babelize/babelize-elements+is:pr+is:open";
8+
const PRS_URL = "https://api.github.com/search/issues?q=repo:babelize/babelize-elements+is:pr";
109

1110
/**
1211
* Fetch GitHub metrics on the server with time-based revalidation (1 hour).

0 commit comments

Comments
 (0)