Unkey
ComponentsForm inputs

FormCheckbox

A form checkbox component with built-in label, description, and error handling capabilities for creating accessible, user-friendly selection inputs.

Overview

The FormCheckbox component provides a comprehensive checkbox system that combines labels, descriptions, and validation states. It's designed to create accessible, user-friendly forms that require selection inputs with proper labeling, helpful context, and clear validation feedback while maintaining consistency with the design system.

Usage

import { FormCheckbox } from "@unkey/ui";
 
export default function MyComponent() {
  return (
    <FormCheckbox
      label="I agree to the terms and conditions"
      description="By checking this box, you agree to our terms of service."
      required
    />
  );
}

Examples

Default FormCheckbox

The default FormCheckbox includes a label and optional description text, providing clear context for users.

We'll send you occasional updates about our products

Required Field

Use the required prop to indicate mandatory fields. This automatically adds a "Required" tag to the label.

You must accept our terms to continue

Required Field with Error

When a required field has an error, the "Required" tag changes to error styling to draw more attention.

Optional Field

Use the optional prop to explicitly indicate non-mandatory fields. This adds an "Optional" badge to the label.

Get the latest updates directly to your inbox

Success State

Indicates successful validation or acceptance of selection. The success icon and text provide positive feedback.

Your account is now more secure

Warning State

Used for potentially impactful selections that should be made with caution. Includes a warning icon and explanatory text.

This includes anonymous activity information

Error State

Shows validation errors or other issues that need user attention. Features prominent error styling and message.

Disabled State

Apply when the field should be non-interactive, such as during form submission or based on other field values.

Only available to authorized personnel

Checked State

Pre-selected checkbox with an initial checked state that users can toggle.

Save your settings for future visits

Outline Variant

A subtler alternative to the primary variant with transparent background when checked.

A summary of your account activity

Ghost Variant

The most minimal styling for checkboxes that need less visual emphasis.

Display additional configuration options

Different Sizes

Checkboxes can be sized appropriately for different UI needs.

Small

Please verify before submission

Medium

Please verify before submission

Large

Please verify before submission

XLarge

Please verify before submission

Complex Usage

Example of FormCheckboxes with multiple props configured for a specific use case, such as a cookie consent form.

Enable all cookie types for the best experience
Required for the website to function
Help us improve site performance and usability
Enable advanced features and personalization
Allow us to provide relevant advertisements

Features

  • Built-in Labeling: Automatic label association with proper accessibility
  • Description Support: Optional helper text for additional context
  • Validation States: Multiple states (success, warning, error) with appropriate styling
  • Required/Optional Indicators: Clear visual indicators for field requirements
  • Multiple Variants: Different visual styles (default, outline, ghost) for various contexts
  • Size Options: Different sizes for different UI needs
  • Accessibility: Full accessibility support with proper ARIA attributes
  • Responsive Design: Adapts to different screen sizes
  • Dark Mode Support: Consistent appearance in light and dark themes
  • Customizable: Extensive styling options through className props

Props

The FormCheckbox component extends the standard Checkbox component props with additional form-specific properties:

PropTypeDefaultDescription
labelstring-The label text to display next to the checkbox
descriptionstring | React.ReactNode-Optional helper text displayed below the checkbox
requiredboolean-Whether the field is required. Adds a "Required" indicator
optionalboolean-Whether the field is optional. Adds an "Optional" indicator
errorstring-Error message to display. Overrides description and applies error styling
variant"primary" | "outline" | "ghost""primary"The variant style to use for the checkbox
color"default" | "success" | "warning" | "danger" | "info""default"The color scheme to use for the checkbox
size"sm" | "md" | "lg" | "xlg""md"The size of the checkbox
checkedbooleanfalseWhether the checkbox is checked
defaultCheckedboolean-Default checked state when uncontrolled
disabledboolean-Whether the checkbox is disabled
namestring-Name of the checkbox for form submission
valuestring-Value of the checkbox for form submission
onCheckedChange(checked: boolean) => void-Callback triggered when checkbox state changes
classNamestring-Additional CSS classes to apply
idstring-Unique identifier for the checkbox. Auto-generated if not provided

Variants

Default

  • Standard checkbox with label and optional description
  • Use for most checkbox inputs
  • Balanced visual weight and accessibility

Required

  • Includes "Required" indicator for mandatory fields
  • Use for fields that must be completed
  • Clear visual indication of importance

Optional

  • Includes "Optional" indicator for non-mandatory fields
  • Use for fields that can be left empty
  • Provides clarity in forms with mixed requirements

Success

  • Green styling for positive validation states
  • Use for successfully validated selections
  • Encourages positive user feedback

Warning

  • Yellow/orange styling for cautionary states
  • Use for selections that need attention but aren't errors
  • Indicates potential issues

Error

  • Red styling for validation errors
  • Use for failed validation or critical issues
  • Clearly indicates problems that need resolution

Outline

  • Subtler styling with transparent background when checked
  • Use for less prominent selections
  • Good for secondary options

Ghost

  • Minimal styling for checkboxes that need less visual emphasis
  • Use for tertiary or subtle selections
  • Perfect for dense UIs

Structure

The FormCheckbox component is composed of:

  1. Checkbox Element - The actual checkbox input with validation states
  2. Label Container - Contains the label text and required/optional indicators
  3. Description Text - Optional helper text below the checkbox
  4. Error Message - Validation error display when applicable
  5. Status Icons - Visual indicators for different states

Styling

The component includes default styling with:

  • Consistent spacing and typography from design system
  • Color-coded validation states for semantic meaning
  • Proper label positioning and association
  • Multiple visual variants for different contexts
  • Dark mode support with appropriate color schemes
  • Focus states for accessibility
  • Customizable through className props

Custom Styling

You can customize the appearance using className props:

<FormCheckbox
  label="Custom Checkbox"
  description="Custom description"
  className="custom-form-checkbox-class"
  variant="outline"
/>

Accessibility

The FormCheckbox component is built with accessibility in mind:

  • Label Association: Labels are properly associated with checkboxes using htmlFor/id
  • Error Announcements: Error messages are announced to screen readers using role="alert"
  • Required Indicators: Required fields are marked both visually and via aria-required
  • Helper Text: Helper text is linked to checkboxes using aria-describedby
  • Validation States: Error states are indicated using aria-invalid
  • Focus Management: Checkboxes maintain proper focus states for keyboard navigation
  • Screen Reader Support: Announces validation states and requirements appropriately
  • Keyboard Navigation: Full keyboard support for all interactions