vael-ui v0.5.7

Used with a component · Resizable

useResizable

The headless drag and keyboard engine behind <Resizable>. It turns a handle's pointer drag into a size in px on your ref, written once a frame. Mid drag, the size stretches past min and max with rubber-band resistance, then settles back within them on release. Escape puts it back where the drag started. Arrow keys resize by 16 px, and Home and End jump to min and max. <Resizable> adds the panel, the separator role and its ARIA values. Use the composable when the handle and the panel live apart in your own layout. A window that pulls both ways takes a pair of sizes, useResizable({ horizontal: width, vertical: height }, { horizontal: { min: 240 }, vertical: { min: 140 } }), and each handle names where it sits: onHandlePointerdown(event, 'se'). A side pulls one axis and a corner pulls both, and onCommit fires once per release.

Install

import { useResizable } from 'vael-ui'

Examples

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

const width = ref(240)
const { isDragging, onHandlePointerdown, onHandleKeydown } = useResizable(width, {
  min: 160,
  max: 480,
})
</script>

<template>
  <aside class="sidebar" :style="{ inlineSize: `${width}px` }">
    <nav>...</nav>
    <div
      class="sidebar-handle"
      role="separator"
      aria-orientation="vertical"
      aria-label="Resize sidebar"
      tabindex="0"
      :aria-valuenow="Math.round(width)"
      aria-valuemin="160"
      aria-valuemax="480"
      :data-active="isDragging || undefined"
      @pointerdown="onHandlePointerdown"
      @keydown="onHandleKeydown"
    />
  </aside>
</template>

Parameters

NameTypeDescription
modelRef<number> | { horizontal: Ref<number>; vertical: Ref<number> }The size in px (first argument), written on every drag frame. A pair of sizes lets one handle pull both axes.
horizontal, verticalUseResizableAxisOptionsWith a pair of sizes: each axis takes its own min, max, step, offset, centered, flip, bounds and scrollEl, as one size does. disabled, step and onCommit at the top level apply to both.
minMaybeRefOrGetter<number>The smallest size. Default 0.
maxMaybeRefOrGetter<number>The largest size. End jumps the size to it, and does nothing without one. Default Infinity, no limit.
directionMaybeRefOrGetter<'horizontal' | 'vertical'>The drag axis. Default 'horizontal', which reads clientX and the Left and Right arrows. 'vertical' reads clientY and the Up and Down arrows.
edgeMaybeRefOrGetter<'start' | 'end'>Which edge the handle sits on. Default 'end', the trailing edge, so dragging right or down grows the panel. 'start' flips it, for a handle on a right-docked panel's left border.
disabledMaybeRefOrGetter<boolean>Ignores pointer and key input while true. Default false.
stepMaybeRefOrGetter<number | undefined>Snaps to a multiple of step on release, a day on a timeline say, while the drag itself stays smooth. An arrow key moves one step. Unset, a drag doesn't snap and an arrow key moves 16 px.
offsetRef<number>Where it starts, for a span such as a timeline bar. An edge: 'start' handle then moves the start and the size together, so the far end stays put, and onMovePointerdown moves the whole span. One instance makes a bar: each end's handle names its edge, onHandlePointerdown(event, 'start') and onHandlePointerdown(event, 'end'). A handle pulled past min flips the span over its other end.
centeredMaybeRefOrGetter<boolean>Resizes from the middle: the edge you pull follows the pointer and the opposite edge moves the same distance the other way, so the size changes twice as far and the centre stays put. With offset, the start moves to keep it there. Default false.
flipMaybeRefOrGetter<boolean>With offset: a handle pulled past min flips the span over its other end, so one press can pull a span out either way. Default true. Set false to stop at min instead, as a panel does.
boundsMaybeRefOrGetter<[number, number]>With offset: the span stays between these, start and end.
scrollElMaybeRefOrGetter<HTMLElement | null>A scrolling container. Near its edges it scrolls under the pointer during a drag, faster the closer the pointer gets, and the drag follows. Set its scroll-padding to what covers it, such as a sticky column, and the edge is measured from there.
onCommit() => voidFires once per settled interaction: on pointer release, or after each key press. Save the size here instead of on every drag frame.

Returns

NameTypeDescription
onMovePointerdown(event: PointerEvent) => voidWith offset: bind it on the span to move it whole. It starts after 4 px of travel, so a still press stays a click, and the click after a move, or after Escape, doesn't fire.
onMoveKeydown(event: KeyboardEvent) => voidWith offset: the arrow keys move the whole span a step.
onHandlePointerdown(event: PointerEvent, edge?: 'start' | 'end') => voidBind it on the handle. Give the handle touch-action: none, so a touch drag resizes instead of scrolling. A span has two handles: name the one you pressed. Left out, it's the edge option. With a pair of sizes, name where the handle sits instead: 'n', 's', 'e', 'w', 'ne', 'nw', 'se' or 'sw', as the screen shows it, also right to left.
onHandleKeydown(event: KeyboardEvent, edge?: 'start' | 'end') => voidBind it on the handle, and give the handle tabindex="0" so it can take focus. Name its edge as onHandlePointerdown does.
isDraggingRef<boolean>true from pointer down until release. A move counts from its first 4 px.