Category Text A

Category selector. It can be placed inside a product item as a category, size or variant selector.

Preview

Code

import React, { useState } from "react"; import styles from "./category-text-a.module.css"; type CategoryType = { slug: string; title: React.ReactNode; }; type Props = { className?: string; categories?: CategoryType[]; initialCategorySlug?: string; size?: "small" | "medium" | "large"; rounded?: "none" | "small" | "medium" | "large" | "full"; // Put additional variants here, then define them in the CSS variant?: "default" | "black"; // Behavior when category is selected onSelectCategory?: (categorySlug: string) => void; }; export default function CategoryTextA({ categories, className, rounded = "medium", size = "medium", initialCategorySlug, variant, onSelectCategory, }: Props) { // Active slug state const [activeSlug, setActiveSlug] = useState<string>( initialCategorySlug || (categories ? categories[0]?.slug : "") ); return ( <> {categories && ( <div className={[ styles["wrapper"], styles[`s-${size}`], styles[`r-${rounded}`], styles[`variant-${variant}`], className, ].join(" ")} > {categories.map((category) => { return ( <div key={category.slug}> <button type="button" className={[ styles["category-item"], activeSlug === category.slug && styles["active"], ].join(" ")} onClick={() => { setActiveSlug(category.slug); onSelectCategory && onSelectCategory(category.slug); }} > {category.title} </button> </div> ); })} </div> )} </> ); }

Design

Figma design file:

Documentation

Properties

Props of the component:

  • className (string): Specifies the CSS class of the component.

  • categories (Array of CategoryType): Specifies the categories to select.

  • initialCategorySlug (string): Specifies the default selected item slug.

  • size ("small" | "medium" | "large"): Specifies the size of the component.

  • rounded ("none" | "small" | "medium" | "large" | "full"): Specifies the border radius of each item frame.

  • variant ( "default" | "black" or a customized value): Specifies the color or theme variant of the component. See the "Sample CSS customization" below for an example of usage.

  • onSelectCategory (function): Fires when an item is clicked.

CategoryType item arguments
  • slug (string): Specifies the reference slug text.
  • title (string or ReactNode): Specifies the item title text.
Sample CSS customization
.variant-black { --active-bg-color: #1d1d1d; }