# ui/uiscrollbar (vanilla.js 0.4.0)

> https://vanillapunch.com/docs/vanilla.js/0.4.0/api/ui/uiscrollbar/

Scroll bar.

## UIScrollBar extends WorldNode

Scroll bar.
- Displays the scroll state of the target UIScrollView as a track (background) + thumb (drag handle).
- When the user drags the thumb or track, directly updates the target UIScrollView's scrollOffset.
- Every tick, reads the target's offset / contentSize / visible area and updates the thumb position/size.

Placement note:
- Never place this node as a child of the node with UIScrollView attached.
  (TouchRecognizer finds the ancestor scrollview and hands the drag over to it.)
  Instead place it as a sibling so the raycast result is the ScrollBar itself.

Example: Put a vertical scroll bar next to a scroll view

```js
import { Engine, EngineConfiguration, UIScene, UIScrollView, UIScrollBar, ScrollBarAxis, WorldNode, Paint, Color, Pivot, Vector2 } from "./build/vanilla.js";

class ScrollBarScene extends UIScene {
	create() {
		super.create();
		const root = this.getRoot();
		const viewportNode = new WorldNode();
		viewportNode.setPivot(Pivot.topLeft.clone());
		viewportNode.setAnchor(Pivot.topLeft.clone());
		viewportNode.setContentSize(Vector2.create(300, 240));
		viewportNode.setLocalPosition(Vector2.create(160, 60));
		const scrollView = viewportNode.addComponent(UIScrollView);
		scrollView.setHorizontal(false);
		const rowCount = 24;
		const rowHeight = 40;
		const content = scrollView.getContent();
		for (let rowIndex = 0; rowIndex < rowCount; ++rowIndex) {
			const rowNode = new WorldNode();
			rowNode.setPivot(Pivot.topLeft.clone());
			rowNode.setAnchor(Pivot.topLeft.clone());
			rowNode.setContentSize(Vector2.create(300, rowHeight - 4));
			rowNode.setLocalPosition(Vector2.create(0, rowIndex * rowHeight));
			const rowPaint = rowNode.addComponent(Paint);
			rowPaint.setColor(new Color(0.2, 0.3 + (rowIndex % 2) * 0.1, 0.5, 1));
			content.addChild(rowNode);
		}
		scrollView.setScrollContentSize(Vector2.create(300, rowCount * rowHeight));
		root.addChild(viewportNode);

		const scrollBar = new UIScrollBar();
		scrollBar.setScrollView(scrollView);
		scrollBar.setAxis(ScrollBarAxis.vertical);
		scrollBar.setContentSize(Vector2.create(10, 240));
		scrollBar.setLocalPosition(Vector2.create(466, 60));
		scrollBar.setTrackColor(new Color(1, 1, 1, 0.1));
		scrollBar.setThumbColor(new Color(1, 1, 1, 0.7));
		scrollBar.setMinThumbSize(32);
		root.addChild(scrollBar);
	}
}

const engineConfiguration = new EngineConfiguration();
const engine = new Engine(engineConfiguration);
const scene = new ScrollBarScene();
scene.setLoadingMinDurationMs(0);
engine.run(scene);
```

- `constructor()` (constructor, since 0.2.1): Constructor.
- `setScrollView(scrollView)` (method, since 0.2.1): Sets the target UIScrollView.
  - `scrollView` UIScrollView | null: The scroll view whose state is shown and whose offset is changed by dragging. (null turns the thumb off and presses do nothing)
- `getScrollView()` (method, since 0.2.1): Returns the target UIScrollView. (Used by TouchRecognizer to block the ancestor scroll view
   handover behavior for its own scroll bar.)
  - returns UIScrollView | null: The scroll view given to setScrollView. (null if none was given)
- `setAxis(axis)` (method, since 0.2.1): Sets the axis. (ScrollBarAxis.vertical / horizontal)
  - `axis` string: Scroll bar axis. (A ScrollBarAxis value, default vertical)
- `getAxis()` (method, since 0.4.0): Returns the axis.
  - returns string: Scroll bar axis. (A ScrollBarAxis value)
- `getThumbRect()` (method, since 0.4.0): The current thumb rectangle. (Within the scroll bar node area, null if the thumb is off)
  - returns Rect | null: Thumb rectangle. (Local coordinates of the scroll bar node, null when the thumb is off)

  Example: The thumb moves down the track with the offset

  ```js
  import { UIScrollView, UIScrollBar, WorldNode, Vector2 } from "./build/vanilla.js";
  
  const viewportNode = new WorldNode();
  viewportNode.setContentSize(Vector2.create(200, 100));
  const scrollView = viewportNode.addComponent(UIScrollView);
  scrollView.setScrollContentSize(Vector2.create(200, 400));
  const scrollBar = new UIScrollBar();
  scrollBar.setScrollView(scrollView);
  scrollBar.setContentSize(Vector2.create(8, 100));
  scrollBar.tick(0);
  const topThumbRect = scrollBar.getThumbRect();
  scrollView.setScrollOffset(Vector2.create(0, -300));
  scrollBar.tick(0);
  const bottomThumbRect = scrollBar.getThumbRect();
  console.log(topThumbRect.size.y, bottomThumbRect.position.y);
  ```

- `setTrackColor(color)` (method, since 0.2.1): Sets the track color.
  - `color` Color | string: Track (background) color. (Set on the track Paint as is, default black with alpha 0.15)
- `setThumbColor(color)` (method, since 0.2.1): Sets the thumb color.
  - `color` Color | string: Thumb (drag handle) color. (Set on the thumb node Paint as is, default white with alpha 0.6)
- `getTrackColor()` (method, since 0.4.0): Returns the track color. (the color of the track Paint)
  - returns Color: Track color. (The color held by the track Paint, as is)
- `getThumbColor()` (method, since 0.4.0): Returns the thumb color. (the color of the thumb node's Paint)
  - returns Color: Thumb color. (The color held by the thumb node Paint, as is)
- `setMinThumbSize(size)` (method, since 0.2.1): Sets the minimum thumb length.
  - `size` number: Lower limit of the thumb length. (Pixels, default 24, when long content would make the thumb shorter it stays at this length)
- `setAutoHide(autoHide)` (method, since 0.2.1): Sets auto hide. (Hides even the track when the content fits within the visible area)
  - `autoHide` boolean: If true, hides the track as well when there is nothing to scroll. (default true; if false, only the thumb is turned off; the node stays active while hidden, so it shows again on the next update when the content grows or this is turned off)
- `isAutoHidden()` (method, since 0.4.0): Return whether it is hidden by auto hide.
  - returns boolean: True if the track is hidden by auto hide because there is nothing to scroll.
- `isVisible()` (method, since 0.4.0): Return the visibility. (false while hidden by auto hide)
  - returns boolean: True if active, the local opacity is greater than 0, and it is not hidden by auto hide.
- `contains(viewPosition)` (method, since 0.4.0): Whether a position is inside the node. (does not take presses while hidden by auto hide)
  - `viewPosition` Vector2: Position to test. (View coordinates)
  - returns boolean: True if inside the node. (always false while hidden by auto hide)
- `tick(timeDelta)` (method, since 0.2.1): Update.
  - `timeDelta` number: Time since the previous frame. (Seconds, passed on to the node update before the thumb position is fitted again)
- `updateThumb()` (method, since 0.2.1): Updates the thumb position and size from the track length / visible area / content size.
- `applyTrackHitPosition(viewInputPosition)` (method, since 0.2.1): Takes a position in track coordinates (view) and updates the content offset. Used on track click.
  - Computes the progress so the thumb center lands at the click position.
  - `viewInputPosition` Vector2: Pressed position. (View coordinates, the world position of the scroll bar is subtracted to get the position on the track)
- `isInsideThumb(viewInputPosition)` (method, since 0.2.1): Whether viewInputPosition is on the thumb.
  - `viewInputPosition` Vector2: Position to test. (View coordinates)
  - returns boolean: True if inside the thumb rectangle. (Edges included)
- `touchPress(viewInputPosition)` (method, since 0.2.1): Touch press. (If the thumb is hit, starts dragging from there, if the track is hit, jumps then drags)
  - `viewInputPosition` Vector2: Press position. (view coordinates)

  Example: Jump by pressing the end of the track, then drag the thumb back up

  ```js
  import { UIScrollView, UIScrollBar, WorldNode, Vector2 } from "./build/vanilla.js";
  
  const viewportNode = new WorldNode();
  viewportNode.setContentSize(Vector2.create(200, 100));
  const scrollView = viewportNode.addComponent(UIScrollView);
  scrollView.setScrollContentSize(Vector2.create(200, 400));
  const scrollBar = new UIScrollBar();
  scrollBar.setScrollView(scrollView);
  scrollBar.setContentSize(Vector2.create(8, 100));
  scrollBar.tick(0);
  scrollBar.touchPress(Vector2.create(4, 100));
  const jumpedOffset = scrollView.getScrollOffset();
  scrollBar.touchMove(Vector2.create(4, 62.5));
  scrollBar.touchRelease(Vector2.create(4, 62.5));
  const draggedOffset = scrollView.getScrollOffset();
  console.log(jumpedOffset.y, draggedOffset.y);
  ```

- `touchMove(viewInputPosition)` (method, since 0.2.1): Touch move.
  - `viewInputPosition` Vector2: Moved position. (View coordinates, the length dragged since the press is carried to the offset by the track to content ratio)
- `touchRelease(viewInputPosition)` (method, since 0.2.1): Touch release.
  - `viewInputPosition` Vector2: Released position. (View coordinates, unused)
- `touchCancel(viewInputPosition)` (method, since 0.2.1): Touch cancel.
  - `viewInputPosition` Vector2: Position at cancellation. (View coordinates, unused)

## ScrollBarAxis (enum)

Scroll bar axis.

- `horizontal` = `horizontal`: Horizontal scroll bar. (The thumb moves along the track width and changes the horizontal offset)
- `vertical` = `vertical`: Vertical scroll bar. (The thumb moves along the track height and changes the vertical offset, default)
