vael-ui v0.5.7

Used with a component · Dock

useDock

The magnification engine behind <Dock>. Items near the pointer grow along a smooth cosine falloff, and their neighbors spread apart to make room. It writes transform, or the real size in grow mode, straight onto each item every animation frame, so the effect costs no re-renders. A mouse magnifies on hover. A finger or pen magnifies once it slides 8 px along the dock, so a tap stays a tap. It stays at rest under prefers-reduced-motion: reduce. <Dock> adds the buttons, tooltips, roving keyboard focus and the tap bounce.

Install

import { useDock } from 'vael-ui'

Examples

<script setup lang="ts">
import { useTemplateRef } from 'vue'
import { useDock } from 'vael-ui'

const apps = ['Mail', 'Notes', 'Music', 'Photos']
const rootEl = useTemplateRef<HTMLElement>('root')
const { setItemEl, onPointerDown, onPointerMove, onPointerLeave } = useDock(
  rootEl,
  () => apps.length,
  { baseSize: 48, maxSize: 72 },
)
</script>

<template>
  <div
    ref="root"
    class="my-dock"
    @pointerdown="onPointerDown"
    @pointermove="onPointerMove"
    @pointerleave="onPointerLeave"
    @pointerup="onPointerLeave"
    @pointercancel="onPointerLeave"
  >
    <button v-for="(app, i) in apps" :key="app" :ref="setItemEl(i)" class="my-dock-item">
      {{ app }}
    </button>
  </div>
</template>

<style>
/* touch-action: none lets a finger slide along the dock without scrolling the page. */
.my-dock { display: flex; gap: 8px; touch-action: none; }
.my-dock-item { inline-size: 48px; block-size: 48px; }
</style>

Parameters

NameTypeDescription
rootElRef<HTMLElement | null>The dock (first argument). Item centers and its gap are measured against it.
itemCountMaybeRefOrGetter<number>The number of items (second argument). A change measures the items again after the next render.
orientationMaybeRefOrGetter<'horizontal' | 'vertical'>The axis items grow and spread along. Default 'horizontal'.
baseSizeMaybeRefOrGetter<number>The resting item size in px that maxSize and range are designed against. Default 48. Items that rest smaller, in a narrow container, magnify in proportion.
maxSizeMaybeRefOrGetter<number>The item size in px directly under the pointer. Default 76.
rangeMaybeRefOrGetter<number | undefined>The distance in px from the pointer at which items are back to baseSize. Default baseSize * 3.5.
magnifyMaybeRefOrGetter<boolean>Magnifies items near the pointer. false keeps every item at rest. Default true.
disabledMaybeRefOrGetter<boolean>Turns magnification off, the same as magnify: false. It doesn't block clicks; your markup does that. Default false.
growMaybeRefOrGetter<boolean>Sets each item's real inline-size and block-size, so the dock grows with them. Default false, which scales and shifts items with transform: the dock keeps its size and end items can reach past it.

Returns

NameTypeDescription
setItemEl(index: number) => (el: unknown) => voidBind it as :ref="setItemEl(index)" on each item. A component instance resolves to its root element.
onPointerMove(event: PointerEvent) => voidBind it on the root pointermove. It moves every item toward its new size.
onPointerLeave() => voidBind it on the root pointerleave, pointerup and pointercancel. Items ease back to rest.
onPointerDown(event: PointerEvent) => voidBind it on the root pointerdown. It records where a finger or pen touched down, for the 8 px slide check.
touchSlid() => booleantrue once the finger or pen that is down has slid far enough to magnify. false means the press is a tap, so you can tell a slide from a tap on release.
remeasure() => voidMeasures the resting item centers again. It already runs on mount, on root resize and when itemCount changes. Call it after a layout change none of those catch, such as a font swap.