Getting started

Actions

Forms

Layout

Navigation

Overlays

Feedback

Data display

Controllers

Helpers

Recipes

Decisions

Swipe direction from a delta, the wrap target of a focus trap, a wrapped index, and where a floating surface goes.

Reach for them for custom gestures, focus loops and option lists. The helpers are pure: no DOM at import time or in the call path, so they run on the server and in tests. Each result below is computed when the page is built, by running the call shown.

Import

import {
	resolveSwipeDirection,
	DEFAULT_SWIPE_THRESHOLD_PX,
	resolveTrapMove,
	wrapCollectionIndex,
	computeFloatingCoords,
	computeFloatingPosition,
	type FloatingViewport,
	type SwipeDirection,
	type SwipeAxis,
	type SwipeOptions,
	type SwipeVelocity,
	type TrapMove,
} from '@ecopages/radiant-ui/helpers';

Swipe

resolveSwipeDirection(dx, dy, options) needs travel along the axis that exceeds dominance times the travel on the other axis (1 by default) and that is at least threshold, or, with minVelocity, at least MIN_FLICK_TRAVEL_PX with a release velocity ({ x, y } in px per ms, from a velocity tracker) of at least minVelocity along the axis, in the direction of the travel. It is the default of the resolveDirection hook of SwipeController.

CallResult
resolveSwipeDirection(-60, 5)"left"
resolveSwipeDirection(10, 5)null
resolveSwipeDirection(5, 60)null
resolveSwipeDirection(5, 60, { axis: 'both' })"down"
resolveSwipeDirection(-24, 2, { minVelocity: 0.5, velocity: { x: -0.6, y: 0 } })"left"
DEFAULT_SWIPE_THRESHOLD_PX48

Focus trap and collections

resolveTrapMove says where Tab goes inside a trapped surface, generic over the item so it needs no DOM. wrapCollectionIndex moves an active index by one and wraps.

CallResult
resolveTrapMove(['a', 'b', 'c'], 'c', false, false){"kind":"item","item":"a"}
resolveTrapMove(['a', 'b', 'c'], 'b', false, false){"kind":"native"}
resolveTrapMove([], 'a', false, false){"kind":"surface"}
wrapCollectionIndex(2, 1, 3)0
wrapCollectionIndex(0, -1, 3)2

Floating position

computeFloatingCoords(anchor, size, placement, gap, viewport, direction) returns the fixed coordinates of a surface beside an anchor rect, flipping to the opposite side when it overflows and clamping on the cross axis. computeFloatingPosition also returns the resolved placement, which names a physical side. direction ('ltr' by default, or 'rtl') is the anchor's writing direction, which an inline-start or inline-end side resolves against, and which swaps -start and -end on top and bottom when it is 'rtl', so a right-to-left bottom-start resolves to bottom-end. The caller passes the viewport, so nothing here reads window; applyFloatingPosition in @ecopages/radiant-ui/controllers does that for an element.

const anchor = button.getBoundingClientRect();
const viewport = { width: window.innerWidth, height: window.innerHeight, padding: 8 };

const { coords, placement } = computeFloatingPosition(anchor, { width: 120, height: 28 }, 'bottom-start', 8, viewport);