Preview
Scrollbar Directive
Allows to add overlay scrollbars that will look the same for every browser. The directive sets up the scrollbars from the library OverlayScrollbars.
Usage
Import the directive and styling from dialtone
import { DtScrollbarDirective } from "@dialpad/dialtone/vue3";
// Import styling
import "overlayscrollbars/overlayscrollbars.css";
Install the directive into vue instance
app.use(DtScrollbarDirective);
To add a custom overlay scrollbar to a scrollable region, apply the v-dt-scrollbar directive to the parent element of the desired region.
This parent element should have one and only one child. In the case where there are siblings, the scrollable element should be wrapped inside a new <div> tag with the directive attached by adding <div v-dt-scrollbar></div> around the element.
There is no need to explicitly add an overflow property. If the section overflows the available vertical space, a vertical scrollbar will be present. Similarly, if it exceeds the horizontal space, a horizontal scrollbar will appear.
Characteristics
- Has an overlay style: it appears on top of the content rather than the scrollbar taking up space within the container.
- It grows when hovering the scrollbar handle for better accessibility.
- Appears when the mouse enters the scrollable area and disappears on mouse out after a certain time. This can be customized, see variants.
- The look and feel will be the same for every browser and OS.
- Emulates a browser's native scrollbar keyboard and mouse events.
Variants
To customize the behavior of the scrollbar, you can use different show modes with the directive. The allowed values are 'enter' (default), 'always', 'scroll', and 'move'.
Enter (Default)
Show the scrollbar when the mouse enters the scrollable area. This is the default option, so no configuration is needed.
Always
Always show the scrollbar if the region is overflowing the available space.
The object syntax is also supported:
Scroll
Show the scrollbar on scroll.
Move
Show the scrollbar when the mouse moves inside the scrollable area.
Configuration Object
In addition to using directive arguments for scrollbar visibility (:always, :scroll, :move), you can pass a configuration object to the directive that supports additional options like offsets, CSS classes, and explicit show behavior.
Basic Syntax
Configuration Properties
| Property | Type | Default | Description |
|---|---|---|---|
showScrollbar | 'enter' | 'always' | 'scroll' | 'move' | 'enter' | Scrollbar visibility mode |
offset | Object | null | Offset configuration for scrollbar positioning |
offset.blockStart | number | string | undefined | Insets vertical scrollbar from the block-start (aka top) edge |
offset.blockEnd | number | string | undefined | Insets horizontal scrollbar from the block-end (aka bottom) edge |
offset.inlineStart | number | string | undefined | Insets horizontal scrollbar from the inline-start (aka left) edge |
offset.inlineEnd | number | string | undefined | Insets vertical scrollbar from the inline-end (aka right) edge |
blockClasses | string | undefined | CSS classes to apply to the vertical scrollbar |
inlineClasses | string | undefined | CSS classes to apply to the horizontal scrollbar |
Offset Option
The offset option allows you to adjust the positioning of scrollbars to accommodate fixed headers, footers, or other UI elements that overlap with the scrollable region. This eliminates the need for manual CSS overrides.
Offset Properties
| Property | Type | Description |
|---|---|---|
blockStart | number | string | Insets vertical scrollbar from the block-start (aka top) edge |
blockEnd | number | string | Insets horizontal scrollbar from the block-end (aka bottom) edge |
inlineStart | number | string | Insets horizontal scrollbar from the inline-start (aka left) edge |
inlineEnd | number | string | Insets vertical scrollbar from the inline-end (aka right) edge |
Numeric Values (Auto-converted to px)
String Values (Supports any CSS unit)
CSS Variables
Calc() Expressions
CSS Classes
The directive supports applying custom CSS classes to scrollbar elements, allowing you to use utility classes or custom styles for scrollbar appearance.
Block Axis (Vertical) Scrollbar Classes
Inline Axis (Horizontal) Scrollbar Classes
Both Scrollbars
Combined with Offset
ShowScrollbar Validation
The showScrollbar property is validated to ensure only valid values are used. If an invalid value is provided, the directive will log an informational message and fall back to the default 'enter' mode.
Valid values: 'enter', 'always', 'scroll', 'move'
Common Use Cases
Fixed Header
When you have a fixed header that overlaps the scrollable region:
Fixed Header and Footer
When you have both fixed header and footer:
Dynamic Offsets (Reactive)
Offsets can be reactive and will update automatically:
Limitations
Adding this directive to a DOM element or a Vue component will alter the DOM structure, by adding four elements inside the one that the directive was attached to. If the scrollable region is a Vue component, it's recommended to wrap it in a <div v-dt-scrollbar></div>, to avoid altering the structure that the component needs.
The added elements are:
- One with the class
os-size-observer - The second one is the scrollable viewport
- The horizontal scrollbar
- The vertical scrollbar
This can make it challenging to use with components that rely on event listeners or may even render it unusable.