Developer Docs
Quick reference guides with props tables, usage examples, and best practices for rapid development
RadioButton
Radio button with custom styling and optional ping effect
✨ Quick Examples
Radio buttons allow users to select a single option from a group of choices. Built with CSS variables (--vwf-color, --vwf-size) for consistent sizing and theming, with optional animated ping effect and icon replacement.
Live Example
const selected = van.state('option1');
RadioButton({
name: 'demo',
value: 'option1',
label: 'Option 1',
checked: () => selected.val === 'option1',
onchange: (e) => selected.val = e.target.value
})🔧 Props
| Prop | Type | Default | Description |
|---|---|---|---|
| id | string | auto-generated | Unique identifier for the radio button |
| name | string | required | Radio group name (all radios in a group must share the same name) |
| value | string | required | Unique value for this radio button |
| label | string | '' | Display text for the radio button |
| checked | State<boolean> | boolean | false | Checked state (reactive or static) |
| disabled | boolean | false | Disables the radio button |
| required | boolean | false | Shows red asterisk for required fields |
| onchange | Function | undefined | Change event handler - receives native event |
| class | string | '' | Radio input classes (overrides defaults via filterClassName) |
| containerClass | string | '' | Container wrapper classes - use vwfsize-* and vwfcolor-* here |
| labelClass | string | '' | Label text classes |
| iconClass | string | '' | Icon slot classes when icon is provided |
| icon | Element | undefined | Optional icon element to replace default dot |
| ping | boolean | false | Show animated ping ring effect on selection |
| children | any | [] | Additional label content |
💻 Usage
const selected = van.state('opt2');
RadioButton({
name: 'basics',
value: 'opt1',
label: 'First Option',
checked: () => selected.val === 'opt1',
onchange: (e) => selected.val = e.target.value
})📏 Size Variants
Control size using vwfsize-* utilities on containerClass. Radio button size and spacing scale proportionally with --vwf-size CSS variable.
// Size control via containerClass
RadioButton({
containerClass: 'vwfsize-lg', // Large size
name: 'sizes',
value: 'lg',
label: 'Large Radio'
})🎨 Color Customization
Use vwfcolor-* utilities on containerClass or class prop to set theme colors. The radio button and dot automatically use the --vwf-color variable.
// Custom color via vwfcolor utility
RadioButton({
containerClass: 'vwfsize-lg',
class: 'vwfcolor-sunset border-sunset border-2',
name: 'colors',
value: 'sunset',
label: 'Sunset'
})✨ Animated Ping Effect
Add ping={true} prop to show an animated ring effect when the radio button is selected. Great for drawing attention to important selections.
RadioButton({
ping: true, // Animated ping ring
containerClass: 'vwfsize-lg',
name: 'priority',
value: 'high',
label: 'High Priority'
})🔗 RadioGroup Helper
Use RadioGroup to manage multiple radio buttons with shared props. Simplifies creating radio button groups with consistent styling.
const selected = van.state('pro');
RadioGroup({
name: 'plan',
options: [
{ value: 'basic', text: 'Basic - $9/mo' },
{ value: 'pro', text: 'Pro - $19/mo' },
{ value: 'enterprise', text: 'Enterprise' }
],
value: selected, // Auto-managed state
radioProps: {
containerClass: 'vwfsize-sm xs:vwfsize-md',
class: 'vwfcolor-sky'
}
})🔘 Button-Style Radios
Hide the radio input with class="hidden" and style the label to create button-like selections. Use group-has-[:checked] for checked state styling.
RadioGroup({
name: 'time',
options: [
{ value: 'morning', text: '🌅 Morning' },
{ value: 'afternoon', text: '☀️ Afternoon' }
],
value: selectedTime,
radioProps: {
// Style the label like a button
labelClass: 'px-6 py-3 rounded-xl border-2 border-sky ' +
'group-has-[:checked]:bg-sky group-has-[:checked]:text-white',
class: 'hidden' // Hide the radio circle
}
})⭐ Custom Icon Replacement
Replace the default dot with a custom icon using the icon prop. The icon appears at the same position with opacity transition.
RadioButton({
name: 'custom',
value: 'check',
label: 'With Check Icon',
containerClass: 'vwfsize-xl',
class: 'vwfcolor-success border-success border-2 before:bg-transparent',
icon: span({ class: 'oj-ux-ico-check text-success text-2xl' })
})🚫 Disabled State
Disabled radio buttons show reduced opacity and change cursor to not-allowed. Both checked and unchecked states can be disabled.
// Disabled radio button
RadioButton({
name: 'group',
value: 'disabled',
label: 'Disabled Option',
disabled: true, // Reduces opacity, disables interaction
checked: true // Can be checked and disabled
})⚡️ Best Practices
✓ Do This
✓Always use the same name for radio buttons in a group
- This ensures only one radio button can be selected at a time within the group.
✓Use RadioGroup for managing multiple radios
- Simplifies state management and ensures consistent styling across all radio buttons.
✓Provide clear, distinct labels
- Users should be able to easily differentiate between options at a glance.
✓Use containerClass for size/color control
- Apply vwfsize-* and vwfcolor-* utilities to containerClass for consistent theming.
✓Consider button-style radios for 2-4 options
- Button-style radios (hidden input + styled label) work well for compact option sets.
✗ Avoid This
✗Don't use different names for related options
- All radio buttons in a group must share the same name attribute to work correctly.
✗Don't use radios for checkable lists
- If users should be able to select multiple options, use Checkbox instead.
✗Don't forget to handle the onchange event
- Always update your state when a radio button is selected.
✗Don't use too many radio buttons (>7)
- For more than 7 options, consider using a Select dropdown instead.
✨ Key Features
⭑CSS Variable Sizing & Color
- Uses --vwf-size and --vwf-color for consistent, scalable theming across all radio buttons.
⭑Animated Ping Effect
- Optional animated ring on selection for visual emphasis (ping prop).
⭑Custom Icon Replacement
- Replace the default dot with any icon element for unique radio button designs.
⭑Button-Style Radios
- Hide the radio input and style the label to create button-like selections.
⭑RadioGroup Helper
- Simplifies creating groups of radio buttons with consistent styling and state management.
⭑Full Tailwind Override
- filterClassName allows complete style override with any Tailwind utility classes.
⭑Disabled State Styling
- Automatically handles disabled state with reduced opacity and cursor styling.
⭑Auto-Generated IDs
- Prevents console errors with automatic unique ID generation if not provided.
⭑Group-Based Selection
- Uses group-has-[:checked] selector for checked state styling on labels and containers.
Explore More Components
Browse other component categories