Skip to main content

UiScrollbar

A vertical scrollbar component that enables scrolling within a parent container when its content exceeds the visible area.

Automatically calculates thumb size and position based on the content height, and supports mouse dragging and scroll wheel interaction.

Constructor

UiScrollbar(style = {}, props = {})
ParameterTypeDescription
stylestructOptional style overrides for layout, dimensions, or position.
propsstructOptional properties (see below).

Properties

PropertyTypeDefaultDescription
thumbColorintglobal.UI_COL_BOXColor used for the scrollbar thumb.

Internal State Variables

VariableTypeDescription
draggedboolWhether the thumb is currently being dragged.
dragStartYrealMouse Y position at the start of dragging.
dragStartScrollToprealScroll offset at the start of dragging.
maxScrollrealMaximum vertical scroll amount for the parent.
pointerEventsboolAlways true, to capture mouse input.

Behavior Summary

BehaviorDescription
Dynamic sizingAutomatically computes thumb height based on the visible area vs total content height.
Wheel scrollingThe parent container listens to onWheelUp and onWheelDown events and adjusts scrollTop accordingly.
DraggingClick-and-drag the thumb to scroll the parent content proportionally.
Auto-hide behaviorIf the content fits within the visible area (__maxScroll <= 0), the thumb resets and scrolling is disabled.
Continuous syncThumb position updates automatically each frame to reflect the parent’s scrollTop value.

Scroll calculation logic

StepComputation
1Compute total content height by summing child node heights (excluding the scrollbar itself).
2Calculate thumb height = layoutHeight * (layoutHeight / __contentHeight).
3Clamp minimum thumb height to 10px.
4Compute __maxThumbPosition = layoutHeight - thumbHeight.
5Compute __maxScroll = __contentHeight - parent.layout.height.
6Convert scroll offset ↔ thumb position using proportional mapping:
thumbY = (scrollTop / __maxScroll) * __maxThumbPosition.

Methods

MethodDescription
onStep(layoutUpdated)Updates the scrollbar each frame. Handles layout recalculation, dragging, and thumb synchronization.
onMount()Registers mouse wheel listeners on the parent to handle scrolling. (Called automatically when mounted)
createThumb()Creates and attaches the internal thumb node. (Executed in constructor)
__computeThumbHeight()Internal helper (inlined) to calculate thumb size and position.

Mouse interactions

EventDescription
Wheel UpDecreases parent scrollTop by 30 units (clamped at 0).
Wheel DownIncreases parent scrollTop by 30 units (clamped to __maxScroll).
Drag Start (Thumb)Activates dragging mode, recording initial mouse and scroll positions.
DraggingUpdates scroll position proportionally to mouse movement.
Mouse ReleaseEnds dragging, resets thumb width and offset.