# Component Usage Examples This document provides practical examples of how to use the common components, hooks, and utilities in the WebGUI application. ## Table of Contents - [Common Components](#common-components) - [Button](#button) - [IconButton](#iconbutton) - [Card](#card) - [Modal](#modal) - [Input](#input) - [Select](#select) - [Custom Hooks](#custom-hooks) - [useKeyboard](#usekeyboard) - [useDebounce](#usedebounce) - [useLocalStorage](#uselocalstorage) - [useClickOutside](#useclickoutside) - [Utilities](#utilities) - [classNames (cn)](#classnames-cn) - [Formatting](#formatting) - [Validation](#validation) - [Path Utilities](#path-utilities) --- ## Common Components ### Button The `Button` component provides a flexible button with multiple variants and sizes. #### Basic Usage ```tsx import { Button } from "@/components/common" function MyComponent() { return } ``` #### Variants ```tsx ``` #### Sizes ```tsx ``` #### Loading State ```tsx function SaveButton() { const [isSaving, setIsSaving] = useState(false) const handleSave = async () => { setIsSaving(true) try { await saveData() } finally { setIsSaving(false) } } return ( ) } ``` #### Disabled State ```tsx ``` --- ### IconButton The `IconButton` component is designed for icon-only buttons with consistent sizing. #### Basic Usage ```tsx import { IconButton } from "@/components/common" function CloseButton({ onClose }: { onClose: () => void }) { return } aria-label="Close dialog" onClick={onClose} /> } ``` #### Different Sizes ```tsx } aria-label="Delete" onClick={handleDelete} /> } aria-label="Edit" onClick={handleEdit} /> } aria-label="Settings" onClick={handleSettings} /> ``` --- ### Card The `Card` component provides a container with optional header, body, and footer sections. #### Basic Usage ```tsx import { Card, CardHeader, CardBody, CardFooter } from "@/components/common" function UserProfile() { return (

User Profile

Name: John Doe

Email: john@example.com

) } ``` #### Hoverable Card ```tsx

Click me!

``` #### Custom Padding ```tsx
Custom content with manual padding

Large padding card

``` --- ### Modal The `Modal` component provides a dialog with backdrop and keyboard support. #### Basic Confirmation Modal ```tsx import { Modal, ModalHeader, ModalBody, ModalFooter, Button } from "@/components/common" function DeleteConfirmation({ isOpen, onClose, onConfirm }: Props) { return (

Confirm Deletion

Are you sure you want to delete this item? This action cannot be undone.

) } ``` #### Modal with Form ```tsx function EditUserModal({ isOpen, onClose, user }: Props) { const [name, setName] = useState(user.name) const [email, setEmail] = useState(user.email) const [isSaving, setIsSaving] = useState(false) const handleSubmit = async () => { setIsSaving(true) try { await updateUser({ name, email }) onClose() } finally { setIsSaving(false) } } return (

Edit User

setName(e.target.value)} /> setEmail(e.target.value)} />
) } ``` #### Custom Modal Behavior ```tsx // Don't close on backdrop click {/* Modal content */} ``` --- ### Input The `Input` component provides a text input with label, error, and icon support. #### Basic Input ```tsx import { Input } from "@/components/common" function LoginForm() { const [email, setEmail] = useState("") return ( setEmail(e.target.value)} placeholder="Enter your email" /> ) } ``` #### Input with Error ```tsx function Form() { const [email, setEmail] = useState("") const [error, setError] = useState("") const handleBlur = () => { if (!email.includes("@")) { setError("Please enter a valid email address") } else { setError("") } } return ( setEmail(e.target.value)} onBlur={handleBlur} error={error} helperText="We'll never share your email with anyone" /> ) } ``` #### Input with Icons ```tsx } /> } /> ``` #### Input Sizes ```tsx ``` --- ### Select The `Select` component provides a dropdown with label and error support. #### Basic Select ```tsx import { Select } from "@/components/common" function CountrySelector() { const [country, setCountry] = useState("") const countries = [ { value: "us", label: "United States" }, { value: "uk", label: "United Kingdom" }, { value: "ca", label: "Canada" }, ] return ( setRole(e.target.value)} error={error} helperText="Select the user's role" /> ) } ``` --- ## Custom Hooks ### useKeyboard The `useKeyboard` hook manages keyboard shortcuts. #### Basic Keyboard Shortcuts ```tsx import { useKeyboard } from "@/hooks" function Editor() { useKeyboard({ handlers: [ { key: "s", modKey: true, // Cmd/Ctrl handler: () => save(), }, { key: "Escape", handler: () => closeModal(), }, ], }) return
Editor content
} ``` #### Simple Single Shortcut ```tsx import { useKeyboardShortcut } from "@/hooks" function Component() { useKeyboardShortcut("s", () => save(), { modKey: true }) } ``` #### Multiple Modifiers ```tsx useKeyboard({ handlers: [ { key: "s", modKey: true, shiftKey: true, handler: () => saveAs(), }, ], }) ``` --- ### useDebounce The `useDebounce` hook delays value updates. #### Debounced Search ```tsx import { useDebounce } from "@/hooks" import { useState, useEffect } from "react" function SearchComponent() { const [searchQuery, setSearchQuery] = useState("") const debouncedQuery = useDebounce(searchQuery, 500) useEffect(() => { if (debouncedQuery) { performSearch(debouncedQuery) } }, [debouncedQuery]) return setSearchQuery(e.target.value)} /> } ``` #### Debounced Callback ```tsx import { useDebouncedCallback } from "@/hooks" function AutoSave() { const handleSave = useDebouncedCallback((content: string) => { saveToServer(content) }, 1000) return