Skip to content

Commit

Permalink
[sparkle] - refactor: implement cva for className variations in `Ho…
Browse files Browse the repository at this point in the history
…verable`

 - Introduced `cva` for more scalable handling of className variations based on component variants
 - Replaced manual className construction with `cva` utility in `Hoverable` component for maintainability and readability improvements
  • Loading branch information
JulesBelveze committed Nov 13, 2024
1 parent d8d427e commit a482edd
Showing 1 changed file with 24 additions and 15 deletions.
39 changes: 24 additions & 15 deletions sparkle/src/components/Hoverable.tsx
Original file line number Diff line number Diff line change
@@ -1,33 +1,42 @@
import { cva, type VariantProps } from "class-variance-authority";
import React, { SyntheticEvent } from "react";
import { ReactNode } from "react";

import { classNames } from "@sparkle/lib/utils";
import { cn } from "@sparkle/lib/utils";

interface HoverableProps {
const hoverableVariants = cva(
"s-cursor-pointer s-duration-200 hover:s-underline hover:s-underline-offset-2",
{
variants: {
variant: {
invisible: "hover:s-text-highlight-light active:s-text-highlight-dark",
primary:
"s-font-medium s-text-foreground hover:s-text-highlight-light active:s-text-highlight-dark",
highlight:
"s-font-medium s-text-highlight hover:s-text-highlight-light active:s-text-highlight-dark",
},
},
defaultVariants: {
variant: "invisible",
},
}
);

interface HoverableProps extends VariantProps<typeof hoverableVariants> {
children: ReactNode;
className?: string;
onClick: (e: SyntheticEvent) => void;
variant?: "primary" | "highlight" | "invisible";
}

export function Hoverable({
children,
className = "",
className,
onClick,
variant = "invisible",
variant,
}: HoverableProps) {
const baseClasses =
"s-cursor-pointer s-duration-200 hover:s-underline hover:s-text-highlight-light hover:s-underline-offset-2 active:s-text-highlight-dark";

const variantClasses = {
invisible: "",
primary: "s-font-medium s-text-foreground",
highlight: "s-font-medium s-text-highlight",
};

return (
<span
className={classNames(baseClasses, variantClasses[variant], className)}
className={cn(hoverableVariants({ variant }), className)}
onClick={onClick}
>
{children}
Expand Down

0 comments on commit a482edd

Please sign in to comment.