Scrollbar Component Design Documentation
Overview
The Scrollbar component is an advanced container component in this library used for customizing scrollbar styles. It adheres to the library's consistent design philosophy of "Separation of Concerns" and "Logic Abstraction." Through core Hooks like useClassComputed, useStyleComputed, and useScrollVariables, it achieves powerful and flexible scrollbar style control.
The Scrollbar component provides richer functionality than the Scroll component, including dimension control, behavior control, color customization, and other multi-dimensional scrollbar customization options.
Design Philosophy
Layered Architecture
The Scrollbar component is strictly designed with a three-layer architecture:
- Structure Layer (
scrollbar.vue) - Responsible for the component's DOM structure and basic layout. - Logic Layer (
scrollbarComputed.ts) - Handles complex reactive computations via Hooks. - Presentation Layer (
scrollbar.scss) - Defines the component's visual presentation and style rules.
Logic Abstraction
The Scrollbar component abstracts complex style logic into multiple core Hooks:
useClassComputed- Handles dynamic class name generation.useStyleComputed- Handles dynamic style generation.useScrollVariables- Handles scrollbar CSS variable generation.
This design allows the component itself to simply declare the required logic, while the specific computations are delegated to the Hooks.
Core Feature Design
Structured Props Design
The props of the Scrollbar component are grouped into logical categories based on functionality:
// Dimension-related properties
export const scrollbarDimensionProps = {
height: {
type: String,
default: '' as const
},
Maxheight: {
type: String,
default: '' as const
},
ScrollWidth: {
type: String,
default: ''
},
widthX: {
type: Boolean,
default: false
}
};
// Color-related properties
export const scrollbarColorProps = {
thumbColor: {
type: String,
default: ''
},
thumbHoverColor: {
type: String,
default: ''
},
trackColor: {
type: String,
default: ''
}
};
// Behavior-related properties
export const scrollbarBehaviorProps = {
disabledHeight: {
type: Boolean,
default: false
},
disabledWidth: {
type: Boolean,
default: false
},
disabledScroll: {
type: Boolean,
default: false
}
};2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
This grouping makes the code structure clearer and facilitates future feature extensions.
Application of useClassComputed
The Scrollbar component utilizes the useClassComputed Hook to handle dynamic class name generation:
const scrollbarClass = useClassComputed<ScrollbarProps>({
baseClass: 'scrollbar-container',
flagClasses: {
Maxheight: !!props.Maxheight,
widthX: props.widthX,
disabledHeight: props.disabledHeight,
disabledWidth: props.disabledWidth,
disabledScroll: props.disabledScroll,
corner: props.corner,
},
});2
3
4
5
6
7
8
9
10
11
This design ensures consistency and maintainability in class name generation.
Application of useStyleComputed and useScrollVariables
The Scrollbar component uses the useStyleComputed and useScrollVariables Hooks to handle style generation:
const scrollbarStyle = useStyleComputed<ScrollbarProps>(props, {
propToStyleMap: {
height: 'height',
Maxheight: 'maxHeight',
ScrollWidth: 'scrollWidth',
},
cssVariables: useScrollVariables({
thumbColor: props.thumbColor,
thumbHoverColor: props.thumbHoverColor,
trackColor: props.trackColor,
scrollWidth: props.ScrollWidth,
scrollHeight: props.ScrollWidth,
}),
});2
3
4
5
6
7
8
9
10
11
12
13
14
This design encapsulates complex styling logic within independent Hooks, simplifying the complexity of the component itself.
Style System Design
CSS Variable Control
The Scrollbar component achieves scrollbar style control through CSS variables:
--scrollbar-container-track-color- Scrollbar track color.--scrollbar-container-thumb-color- Scrollbar thumb color.--scrollbar-container-thumb-hover-color- Scrollbar thumb hover color.--scrollbar-container-scrollbar-width- Scrollbar width.--scrollbar-container-scrollbar-height- Scrollbar height.
WebKit Browser Support
The component supports scrollbar style customization for WebKit browsers via the following CSS rules:
&::-webkit-scrollbar {
width: var(--scrollbar-container-scrollbar-width, $scrollbar-default-size);
height: var(--scrollbar-container-scrollbar-height, $scrollbar-default-size);
}
&::-webkit-scrollbar-track {
background-color: var(--scrollbar-container-track-color);
}
&::-webkit-scrollbar-thumb {
background-color: var(--scrollbar-container-thumb-color);
border-radius: 10px;
&:hover {
background-color: var(--scrollbar-container-thumb-hover-color);
}
}2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Scrollbar Behavior Control
The component supports multiple scroll behavior controls:
- widthX - Controls horizontal scrollbar display.
- disabledHeight - Disables vertical scrolling.
- disabledWidth - Disables horizontal scrolling.
- disabledScroll - Completely disables scrolling.
- corner - Controls scrollbar corner styles.
Component Structure Design
The Scrollbar component adopts a flexible tag rendering mechanism:
<div :is="props.tag" :class="scrollbarClass" :style="scrollbarStyle">
<slot />
</div>2
3
This design allows the component to be rendered as different HTML elements, defaulting to <div> but customizable to other tags via the tag property.
Extensibility Design
The Scrollbar component offers excellent extensibility:
- Custom Tag Support: Can render as different HTML elements via the tag prop.
- Comprehensive Style Control: Supports full style settings for track color, thumb color, hover color, width, etc.
- Behavior Control: Supports multiple scroll behavior control options.
- Dimension Control: Supports height, max-height, scrollbar width, and other dimension controls.
- Theme Adaptation: Supports theme customization via CSS variables.
Style Details
The Scrollbar component features the following style handling characteristics:
- Default Styles: Provides a dark-themed default scrollbar style.
- Rounded Corners: Scrollbar thumb features rounded corner design.
- Hover Effect: Thumb color changes on hover.
- Multi-Dimensional Control: Supports separate control of vertical and horizontal scrollbars.
- Corner Styles: Supports customization of scrollbar corner styles.
Performance Optimization
Performance is optimized through:
- Caching computation results using computed properties.
- Moving complex logic into independent hooks.
- Avoiding unnecessary re-renders.
- Leveraging Vue 3 Composition API for runtime efficiency.
Design Value
The Scrollbar component embodies the design philosophy of our component library:
- Separation of Concerns: Complete decoupling of structure, style, and logic.
- High Reusability: Abstracting common logic via hooks.
- Ease of Maintenance: Clear code structure and naming conventions.
- Strong Extensibility: Support for rich customization options.
- Consistency: Maintains a consistent design language with other components like Button, Border, and Text.