Switch Component Design Document
Overview
The Switch component is a form component in this component library used for binary state toggling. It continues the consistent design philosophy of our component library, adopting the principles of "separation of concerns" and "logic abstraction." It achieves powerful and flexible switch control functionality through core Hooks such as useClassComputed and useStyleComputed.
The Switch component focuses on handling users' binary state toggling operations, providing a complete and easy-to-use state switching solution that supports multiple sizes, disabled state, and text display.
Design Philosophy
Layered Architecture
The Switch component strictly follows a three-layer architecture design:
- Structure Layer switch.vue - Responsible for the component's DOM structure and basic layout
- Logic Layer switchComputed.ts - Handles complex reactive computations through Hooks
- Presentation Layer switch.scss - Defines the component's visual presentation and style rules
Logic Abstraction
The Switch component abstracts complex logic into multiple core Hooks:
- useClassComputed - Handles dynamic class name generation
- useStyleComputed - Handles dynamic style generation
This design allows the component itself to only declare the required logic, while the specific computations are handled by the Hooks.
Core Feature Design
Structured Props Design
The props design of the Switch component is concise and clear:
export const switchProps = {
modelValue: {
type: Boolean,
default: false
},
value: {
type: [Boolean, String, Number],
default: false
},
size: {
type: String,
default: undefined
},
text: {
type: String,
default: undefined
},
disabled: {
type: Boolean,
default: false
}
}This design provides sufficient flexibility and supports multiple usage scenarios.
State Management
The Switch component manages core states through computed properties:
// Basic state
const isChecked = computed(() => Boolean(props.modelValue))
const isDisabled = computed(() => Boolean(props.disabled))This design ensures state consistency and reactivity.
Form Integration
The Switch component supports integration with form components:
// Form context injection
const formItemContext = inject<Partial<FormItemContext> | null>('myFormItemContext', null)
// Listen for prop changes, only used to clear validation
watch(() => props.modelValue, () => {
formItemContext?.clearValidate?.()
})Style System Design
CSS Variable Control
The Switch component implements style control through CSS variables:
--my-switch-text- Text content control
Size System
The Switch component supports multiple sizes:
- mini - Minimum size
- supersmall - Extra small size
- small - Small size
- medium - Medium size (default)
- large - Large size
- biglarge - Extra large size
- superlarge - Extra extra large size
State Styles
The Switch component provides corresponding styles according to different states:
- Default State - Style when turned off
- Checked State - Style when turned on
- Disabled State - Semi-transparent effect when disabled
Component Structure Design
The Switch component adopts a classic switch structure:
<label class="my-switch" :class="[classSwitch, { 'is-checked': isChecked }]" :style="styleSwitch">
<input type="checkbox" hidden :checked="isChecked" :disabled="isDisabled" @change="onInput" />
<span class="slider">
<span v-if="props.text" class="slider-text">{{ props.text }}</span>
</span>
<span class="text">
<slot />
</span>
</label>This structure leverages the semantic features of the native checkbox while achieving a beautiful visual effect through CSS.
Interaction Design
State Toggling
The Switch component supports clicking to toggle state:
const onInput = () => {
if (isDisabled.value) return
const newValue = !isChecked.value
emit('update:modelValue', newValue)
emit('change', newValue)
}Disabled State
The Switch component supports disabled state:
input:disabled + .slider {
opacity: 0.5;
cursor: not-allowed !important;
}Keyboard Navigation
The Switch component supports keyboard navigation:
- Tab Key - Switch focus between components
- Space Key - Toggle switch state
Extensibility Design
The Switch component has excellent extensibility:
- Multiple Size Support: Supports multiple sizes from mini to superlarge
- Text Display: Supports displaying custom text on the switch
- Disabled State: Supports disabled state control
- Slot Support: Supports adding extra text through slots
- Theme Adaptation: Supports theme customization through CSS variables
- Form Integration: Seamlessly integrates with form components
Style Details
The Switch component has the following characteristics in style handling:
- Smooth Animation: Smooth transition animation when the switch toggles
- Responsive Design: Supports multiple size adaptations
- Accessibility Design: Supports keyboard navigation and screen readers
- Text Positioning: Text on the switch moves with state changes
- Consistency: Maintains style consistency with other form components in the component library
Performance Optimization
Performance is optimized through the following methods:
- Use computed properties to cache calculation results
- Move complex logic to independent hooks
- Avoid unnecessary re-renders
- Use Vue 3 Composition API to improve runtime efficiency
- Reasonably add and remove event listeners
Design Value
The Switch component continues the design philosophy of our component library:
- Separation of concerns: Structure, styles, and logic are completely decoupled
- High reusability: Abstract common logic through hooks
- Easy maintenance: Clear code structure and naming conventions
- Strong extensibility: Supports rich customization options
- Consistency: Maintains a consistent design language with other components such as Checkbox, Radio, and Input