Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

Make icons animate on hover

Tim Wilsonc222900b343f
diff --git a/apps/web/ui/layout/sidebar/icons/cursor-rays.tsx b/apps/web/ui/layout/sidebar/icons/cursor-rays.tsx
index 46314e283f..d1bbd349ad 100644
--- a/apps/web/ui/layout/sidebar/icons/cursor-rays.tsx
+++ b/apps/web/ui/layout/sidebar/icons/cursor-rays.tsx
@@ -2,17 +2,17 @@ import { cn } from "@dub/utils";
import { SVGProps, useEffect, useRef } from "react";
export function CursorRays({
- isActive,
+ "data-hovered": hovered,
className,
...rest
-}: { isActive?: boolean } & SVGProps<SVGSVGElement>) {
+}: { "data-hovered"?: boolean } & SVGProps<SVGSVGElement>) {
const cursorRef = useRef<SVGGElement>(null);
const raysRef = useRef<SVGGElement>(null);
useEffect(() => {
if (!raysRef.current || !cursorRef.current) return;
- if (isActive) {
+ if (hovered) {
raysRef.current.animate(
[
{ transform: "scale(1)", opacity: 1 },
@@ -22,7 +22,7 @@ export function CursorRays({
{ transform: "scale(1)", opacity: 1 },
],
{
- duration: 500,
+ duration: 300,
},
);
@@ -33,11 +33,11 @@ export function CursorRays({
{ transform: "translate(0, 0)" },
],
{
- duration: 500,
+ duration: 300,
},
);
}
- }, [isActive]);
+ }, [hovered]);
return (
<svg
diff --git a/apps/web/ui/layout/sidebar/icons/gear.tsx b/apps/web/ui/layout/sidebar/icons/gear.tsx
index 85167e5815..17b71b3645 100644
--- a/apps/web/ui/layout/sidebar/icons/gear.tsx
+++ b/apps/web/ui/layout/sidebar/icons/gear.tsx
@@ -1,23 +1,23 @@
import { SVGProps, useEffect, useRef } from "react";
export function Gear({
- isActive,
+ "data-hovered": hovered,
...rest
-}: { isActive?: boolean } & SVGProps<SVGSVGElement>) {
+}: { "data-hovered"?: boolean } & SVGProps<SVGSVGElement>) {
const ref = useRef<SVGSVGElement>(null);
useEffect(() => {
if (!ref.current) return;
- if (isActive) {
+ if (hovered) {
ref.current.animate(
- [{ transform: "rotate(0deg)" }, { transform: "rotate(360deg)" }],
+ [{ transform: "rotate(0)" }, { transform: "rotate(180deg)" }],
{
duration: 300,
},
);
}
- }, [isActive]);
+ }, [hovered]);
return (
<svg
diff --git a/apps/web/ui/layout/sidebar/icons/hyperlink.tsx b/apps/web/ui/layout/sidebar/icons/hyperlink.tsx
index e7899b54ed..f6643a4e12 100644
--- a/apps/web/ui/layout/sidebar/icons/hyperlink.tsx
+++ b/apps/web/ui/layout/sidebar/icons/hyperlink.tsx
@@ -1,15 +1,15 @@
import { SVGProps, useEffect, useRef } from "react";
export function Hyperlink({
- isActive,
+ "data-hovered": hovered,
...rest
-}: { isActive?: boolean } & SVGProps<SVGSVGElement>) {
+}: { "data-hovered"?: boolean } & SVGProps<SVGSVGElement>) {
const ref = useRef<SVGSVGElement>(null);
useEffect(() => {
if (!ref.current) return;
- if (isActive) {
+ if (hovered) {
ref.current.animate(
[
{ transform: "rotate(0deg)" },
@@ -22,7 +22,7 @@ export function Hyperlink({
},
);
}
- }, [isActive]);
+ }, [hovered]);
return (
<svg
diff --git a/apps/web/ui/layout/sidebar/icons/lines-y.tsx b/apps/web/ui/layout/sidebar/icons/lines-y.tsx
index 770568c532..f3a938e821 100644
--- a/apps/web/ui/layout/sidebar/icons/lines-y.tsx
+++ b/apps/web/ui/layout/sidebar/icons/lines-y.tsx
@@ -4,17 +4,17 @@ import { SVGProps, useEffect, useRef } from "react";
const SCALES = [0.3, 1.5, 1.75, 0.75];
export function LinesY({
- isActive,
+ "data-hovered": hovered,
className,
...rest
-}: { isActive?: boolean } & SVGProps<SVGSVGElement>) {
+}: { "data-hovered"?: boolean } & SVGProps<SVGSVGElement>) {
const line1Ref = useRef<SVGLineElement>(null);
const line2Ref = useRef<SVGLineElement>(null);
const line3Ref = useRef<SVGLineElement>(null);
const line4Ref = useRef<SVGLineElement>(null);
useEffect(() => {
- if (!isActive) return;
+ if (!hovered) return;
[line1Ref, line2Ref, line3Ref, line4Ref].forEach((ref, idx) => {
if (!ref.current) return;
@@ -31,7 +31,7 @@ export function LinesY({
},
);
});
- }, [isActive]);
+ }, [hovered]);
return (
<svg
diff --git a/apps/web/ui/layout/sidebar/items.ts b/apps/web/ui/layout/sidebar/items.ts
index 50b564e272..c3017aef68 100644
--- a/apps/web/ui/layout/sidebar/items.ts
+++ b/apps/web/ui/layout/sidebar/items.ts
@@ -1,9 +1,9 @@
import { BetaFeatures } from "@/lib/types";
+import { Icon } from "@dub/ui";
import {
CircleInfo,
ConnectedDots,
CubeSettings,
- Gear,
Gear2,
Gift,
Globe,
@@ -14,14 +14,14 @@ import {
Users6,
} from "@dub/ui/src/icons";
import { Webhook } from "lucide-react";
-import { ComponentType, SVGProps } from "react";
import { CursorRays } from "./icons/cursor-rays";
+import { Gear } from "./icons/gear";
import { Hyperlink } from "./icons/hyperlink";
import { LinesY } from "./icons/lines-y";
-type NavItem = {
+export type NavItem = {
name: string;
- icon: ComponentType<SVGProps<SVGSVGElement> & { isActive?: boolean }>;
+ icon: Icon;
href: string;
exact?: boolean;
};
diff --git a/apps/web/ui/layout/sidebar/sidebar-nav.tsx b/apps/web/ui/layout/sidebar/sidebar-nav.tsx
index 56606607d8..9ea7c0d71f 100644
--- a/apps/web/ui/layout/sidebar/sidebar-nav.tsx
+++ b/apps/web/ui/layout/sidebar/sidebar-nav.tsx
@@ -5,8 +5,8 @@ import { AnimatePresence, motion } from "framer-motion";
import { ChevronLeft } from "lucide-react";
import Link from "next/link";
import { useParams, usePathname } from "next/navigation";
-import { ReactNode, Suspense, useMemo } from "react";
-import { ITEMS } from "./items";
+import { ReactNode, Suspense, useMemo, useState } from "react";
+import { ITEMS, type NavItem } from "./items";
import UserDropdown from "./user-dropdown";
import { WorkspaceDropdown } from "./workspace-dropdown";
@@ -85,32 +85,9 @@ export function SidebarNav({ toolContent }: { toolContent?: ReactNode }) {
{name}
</div>
)}
- {items({ slug: slug || "", flags }).map(
- ({ name, icon: Icon, href, exact }) => {
- const isActive = exact
- ? pathname === href
- : pathname.startsWith(href);
-
- return (
- <Link
- key={href}
- href={href}
- data-active={isActive}
- className={cn(
- "group flex items-center gap-2.5 rounded-md p-2 text-sm leading-none text-neutral-600 transition-[background-color,color,font-weight] duration-75 hover:bg-neutral-200/50 active:bg-neutral-200/80",
- isActive &&
- "bg-blue-100/50 font-medium text-blue-600 hover:bg-blue-100/80 active:bg-blue-100",
- )}
- >
- <Icon
- className="size-4 text-neutral-500 transition-colors duration-75 group-data-[active=true]:text-blue-600"
- isActive={isActive}
- />
- {name}
- </Link>
- );
- },
- )}
+ {items({ slug: slug || "", flags }).map((item) => (
+ <NavItem key={item.name} pathname={pathname} item={item} />
+ ))}
</div>
))}
</div>
@@ -120,3 +97,32 @@ export function SidebarNav({ toolContent }: { toolContent?: ReactNode }) {
</div>
);
}
+
+function NavItem({ pathname, item }: { pathname: string; item: NavItem }) {
+ const { name, icon: Icon, href, exact } = item;
+
+ const isActive = exact ? pathname === href : pathname.startsWith(href);
+
+ const [hovered, setHovered] = useState(false);
+
+ return (
+ <Link
+ key={href}
+ href={href}
+ data-active={isActive}
+ onPointerEnter={() => setHovered(true)}
+ onPointerLeave={() => setHovered(false)}
+ className={cn(
+ "group flex items-center gap-2.5 rounded-md p-2 text-sm leading-none text-neutral-600 transition-[background-color,color,font-weight] duration-75 hover:bg-neutral-200/50 active:bg-neutral-200/80",
+ isActive &&
+ "bg-blue-100/50 font-medium text-blue-600 hover:bg-blue-100/80 active:bg-blue-100",
+ )}
+ >
+ <Icon
+ className="size-4 text-neutral-500 transition-colors duration-75 group-data-[active=true]:text-blue-600"
+ data-hovered={hovered}
+ />
+ {name}
+ </Link>
+ );
+}