Directory
- Welcome
Checkbox Component
The Checkbox component creates interactive form controls that work for both checkboxes and radio buttons. It includes built-in label support, multiple styling variants, and comprehensive accessibility features.
Key Features
-
Dual Functionality: Works as both checkbox and radio button
-
Smart Styling: Multiple variants with hover and focus states
-
Label Integration: Built-in label support with required field indication
-
State Management: Supports various states including error handling
Variants
Choose from three distinct styling variants:
Accept terms and conditions
Delete my account
Variant Use Cases
- Default: Standard form controls and general usage
- Destructive: Actions requiring caution or confirmation
- Outline: When you need a more prominent selection area or want to emphasize the entire option
Component States
The component supports various interactive states:
Agree to privacy policy
*Receive marketing emails
Remember my preferences
Auto-save settings
Required field with error
*Implementation
Basic Checkbox
Create simple checkbox inputs with labels:
<Checkbox value="terms" name="terms" required> Accept terms and conditions</Checkbox>
<Checkbox variant="destructive" value="delete" name="delete"> Delete account</Checkbox>
Radio Button Groups
Use the component in radio button mode:
<RadioGroup title="Choose your favorite fruit" titleClass="text-lg mb-2"> <Checkbox type="radio" name="fruit" value="apple">Apple</Checkbox> <Checkbox type="radio" name="fruit" value="banana">Banana</Checkbox> <Checkbox type="radio" name="fruit" value="cherry">Cherry</Checkbox></RadioGroup>
Checkbox Component Props API
Prop | Type | Default | Description |
---|---|---|---|
checked | boolean | false | Checked state |
disabled | boolean | false | Disabled state |
inputProps | Omit<HTMLAttributes<input>, "type" | "name" | "value" | "required" | "disabled" | "checked"> | Input props | |
labelProps | HTMLAttributes<label> | Label props | |
name | string | Input name | |
required | boolean | false | Required field |
type | checkbox | radio | checkbox | Input type |
value | string | Input value | |
variant | default | destructive | outline | default | Visual style |
Custom Event Listeners
The checkbox component emits a custom event that you can listen to:
Click Me
Waiting for events… (Interact with the checkbox to see events)
const checkbox = document.querySelector('checkbox-wrapper');checkbox?.addEventListener(CheckboxEventNames.Change, (e) => { console.log(e.detail.checked);});
Accessibility Features
-
Automatic ID generation for label association
-
Proper focus states and indicators
-
Screen reader compatibility
-
Keyboard navigation support
-
Required field indication
-
Error state handling
-
ARIA attributes
Best Practices
-
Label Usage
- Use clear, concise labels
- Indicate required fields
- Maintain consistent terminology
- Consider label length
-
Grouping
- Group related checkboxes
- Use fieldsets for radio groups
- Provide clear group labels
- Maintain logical order
-
Visual Design
- Ensure adequate spacing
- Maintain clear hierarchy
- Use consistent sizing
- Consider mobile touch targets
Common Applications
Limited Time Launch Sale
Build user-friendly forms faster with our pre-built Checkbox component. Get the AstroJS starter template now and access a complete form controls toolkit.
GET 60% OFF!