Category Filter
Selection de categorie filtrable pour les listes de contenu. Installez Category Filter depuis le registre VLLNT UI avec la CLI shadcn.
Selection de categorie filtrable pour les listes de contenu. Fait partie de la famille form dans VLLNT UI, il est publie comme entree de registre lisible par machine — copiez la source directement dans votre application avec la CLI shadcn et possedez-la, sans dependance d'execution a une bibliotheque de composants.
Apercu
Basculez entre clair et sombre pour inspecter l'apercu Storybook integre.
Installation
Ajoutez Category Filter a votre projet avec la CLI shadcn. La source arrive dans votre code, prete a etre adaptee :
pnpm dlx shadcn@latest add https://ui.vllnt.ai/r/category-filter.jsonSource
"use client";
import Link from "next/link";
import { usePathname } from "next/navigation";
import { Badge } from "../badge/badge";
type CategoryFilterProps = {
categories: string[];
lang: string;
};
function slugify(text: string): string {
return text
.toLowerCase()
.normalize("NFD")
.replaceAll(/[\u0300-\u036F]/g, "") // Remove diacritics
.replaceAll(/[^\s\w-]/g, "") // Remove special characters except spaces and hyphens
.trim()
.replaceAll(/\s+/g, "-") // Replace spaces with hyphens
.replaceAll(/-+/g, "-") // Collapse consecutive hyphens
.replaceAll(/^-+|-+$/g, ""); // Remove leading/trailing hyphens
}
export function CategoryFilter({ categories, lang }: CategoryFilterProps) {
const pathname = usePathname();
// Get all unique categories and sort them
// eslint-disable-next-line unicorn/prefer-spread
const allCategories: string[] = Array.from(new Set(categories)).sort();
if (allCategories.length === 0) {
return null;
}
return (
<div className="flex flex-wrap gap-2">
{allCategories.map((category) => {
const categorySlug = slugify(category);
const isSelected = pathname.includes(`/${categorySlug}`);
const href = `/${lang}/${categorySlug}`;
if (isSelected) {
return (
<Badge className="cursor-default" key={category} variant="default">
{category.charAt(0).toUpperCase() + category.slice(1)}
</Badge>
);
}
return (
<Link href={href} key={category}>
<Badge
className="cursor-pointer hover:bg-primary hover:text-primary-foreground transition-colors"
variant="secondary"
>
{category.charAt(0).toUpperCase() + category.slice(1)}
</Badge>
</Link>
);
})}
</div>
);
}
Stories
Explorez chaque variante et etat dans le Storybook interactif :
Apercu
Basculez entre clair et sombre pour inspecter l'apercu Storybook integre.