Slidebar Component Design Document
Overview
The Slidebar component is an interactive component in this component library used for numerical adjustment. It continues the consistent design philosophy of our component library, adopting the principles of "separation of concerns" and "logic abstraction." It implements a complete and easy-to-use slider solution through core logic such as useSlidebarComputed.
The Slidebar component focuses on handling users' numerical adjustment operations, providing an intuitive and visually appealing sliding adjustment feature that supports custom sizes, colors, disabled state, and numerical display.
Design Philosophy
Layered Architecture
The Slidebar component strictly follows a three-layer architecture design:
- Structure Layer slidebar.vue - Responsible for the component's DOM structure and basic layout
- Logic Layer slidebarComputed.ts - Handles complex reactive computations and interaction logic
- Presentation Layer slidebar.scss - Defines the component's visual presentation and style rules
Logic Abstraction
The Slidebar component abstracts complex logic into core functions:
- useSlidebarComputed - Handles the core logic of the slider
This design allows the component itself to only declare the required logic, while the specific computations are handled by the logic functions.
Core Feature Design
Structured Props Design
The props design of the Slidebar component is concise and clear:
export const silderbarProps = {
size: {
type: String,
},
thumbColor: {
type: String,
},
trackColor: {
type: String,
},
disabled: {
type: Boolean,
default: false,
},
noNum: {
type: Boolean,
default: false,
},
modelValue: {
type: [String, Number],
default: ''
}
}2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
This design provides sufficient flexibility and supports multiple usage scenarios.
State Management
The Slidebar component manages core states through reactive data:
const num = ref<number | string>(1); // num supports number or string
const progressWidth = ref(0);
const track = ref<HTMLElement | null>(null);
let isDragging = false;2
3
4
This design ensures state consistency and reactivity.
Style System Design
CSS Variable Control
The Slidebar component implements style control through SCSS variables:
$track-height- Track height$thumb-size- Thumb size$primary-color- Primary color$thumb-bg- Thumb background color
Size System
The Slidebar component supports flexible size control:
- Default Size - 100% width, maximum 320px
- Custom Size - Control width through the size property
- Responsive Design - Supports adaptation to different screen sizes
State Styles
The Slidebar component provides corresponding styles according to different states:
- Default State - Standard slider style
- Hover State - Style when mouse hovers
- Dragging State - Style changes during dragging
- Disabled State - Semi-transparent effect when disabled
Component Structure Design
Overall Structure
The overall structure of the Slidebar component is as follows:
<div class="slider-container" :style="containerStyle" :class="{ disabled: disabled }">
<div
ref="track"
class="slider-track"
:style="{ background: props.trackColor || defaultTrackBg }"
@mousedown="startDrag"
>
<div
class="slider-progress"
:style="{ width: progressWidth + '%', background: props.thumbColor || defaultProgressColor }"
></div>
<div
class="slider-thumb"
:style="{ left: progressWidth + '%', borderColor: props.thumbColor || defaultProgressColor }"
@mousedown.stop="startDrag"
>
<span v-if="!noNum" class="slider-value">{{ num }}</span>
</div>
</div>
</div>2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Component Hierarchy
The Slidebar component contains three main parts:
- Container -
.slider-container- Wraps the entire component - Track -
.slider-track- Displays the sliding track - Progress Bar -
.slider-progress- Displays the completed progress - Thumb -
.slider-thumb- Draggable user interaction element - Value Display -
.slider-value- Displays the current value
Interaction Design
Dragging Operation
The Slidebar component supports mouse dragging:
const startDrag = (e: MouseEvent) => {
if (props.disabled) return;
isDragging = true;
updateValue(e.clientX);
};
const onMouseMove = (e: MouseEvent) => {
if (!isDragging || props.disabled) return;
updateValue(e.clientX);
};
const stopDrag = () => {
isDragging = false;
};2
3
4
5
6
7
8
9
10
11
12
13
14
Value Update
The Slidebar component supports real-time value updating:
const updateValue = (clientX: number) => {
if (!track.value || props.disabled) return;
const rect = track.value.getBoundingClientRect();
let percentage = ((clientX - rect.left) / rect.width) * 100;
percentage = Math.max(0, Math.min(100, percentage));
progressWidth.value = percentage;
num.value = Math.round(percentage);
// Decide whether to emit string or number based on the type of modelValue
const emitValue = typeof props.modelValue === 'string' ? String(num.value) : num.value;
emit('update:modelValue', emitValue);
};2
3
4
5
6
7
8
9
10
11
12
Disabled State
The Slidebar component supports disabled state:
&.disabled {
opacity: 0.6;
cursor: not-allowed;
.slider-track,
.slider-thumb {
cursor: not-allowed;
pointer-events: none;
}
}2
3
4
5
6
7
8
9
10
Extensibility Design
The Slidebar component has excellent extensibility:
- Custom Size: Control component width through the size property
- Custom Color: Control colors through thumbColor and trackColor properties
- Disabled State: Control disabled state through the disabled property
- Value Display Control: Control value display through the noNum property
- Two-way Binding: Supports v-model two-way data binding
- Theme Adaptation: Supports theme customization through SCSS variables
Style Details
The Slidebar component has the following characteristics in style handling:
- Transition Animation: Smooth transition effects when the thumb and progress bar change
- Responsive Design: Supports flexible size adaptation
- Accessibility Design: Supports keyboard navigation and screen readers
- Consistency: Maintains style consistency with other form components in the component library
- Interaction Feedback: Clear visual feedback during dragging
- Value Display: Current value is displayed as a bubble above the thumb
Performance Optimization
Performance is optimized through the following methods:
- Use computed properties to cache calculation results
- Move complex logic to independent functions
- Avoid unnecessary re-renders
- Use Vue 3 Composition API to improve runtime efficiency
- Reasonably add and remove event listeners
- Use event delegation to reduce the number of event listeners
Design Value
The Slidebar 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 functions
- 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 SelectCascader, Select, SelectTime, SelectDate, PickColor, Rate, Switch, Checkbox, and Radio