components/floating/useAnchoredPosition
import { … } from 'zidane-ui'ui/src/components/floating/useAnchoredPosition.ts
Functions#
anchorNudgeFunction#
Where a surface placed on side starts its enter: px back toward its
anchor, so it slides out of it (a menu above its trigger rises, one to the
right moves right). Menus nudge 4px, tooltips 2px.
function anchorNudge(side: Side, px: number): { x: number; y: number }Parameters
| Name | Type | Description |
|---|---|---|
| side | Side | The side of the anchor the surface is placed on. |
| px | number | How far back toward the anchor the enter starts, in px. |
Returns
{ x: number; y: number }The x and y offset to start the enter from.
placeSurfaceFunction#
Computes where a surface goes next to an anchor. It tries side, flips to the opposite side
when side does not fit but the opposite does, then clamps the result 8px inside the viewport.
Parameters
| Name | Type | Description |
|---|---|---|
| a | Rect | The anchor's rectangle. |
| size | { height: number; width: number } | The surface's measured width and height. |
| side | Side | The preferred side of the anchor. |
| align | Align | How the surface lines up along the anchor's edge. |
| offset | number | Gap between anchor and surface, in px. |
| viewport? | { height: number; width: number } | The viewport size to fit in. Defaults to the window's inner size. |
Returns
PlacementThe position, the side used and the transform-origin that grows the surface out of the
anchor.
useAnchoredPositionFunction#
Measures where a surface goes next to its anchor and keeps it current. It re-measures on resize, on scroll outside the surface and when the surface's own size changes.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
| open | boolean | Whether the surface is shown. No placement is computed while it is closed. | |
| anchor | Anchor | The element or point to place against. | |
| surfaceRef | RefObject<HTMLElement | null> | Ref to the surface element, measured for its size. | |
| options? | { align?: Align; offset?: number; side?: Side } | {} | The preferred |
Returns
Placement | nullThe placement, or null while closed or before the first measurement.
Interfaces#
PlacementInterface#
Where useAnchoredPosition puts a surface, in viewport coordinates.
interface PlacementProperties
| Name | Type | Description |
|---|---|---|
| left | number | Distance from the viewport's left edge, in px. |
| origin | string | CSS transform-origin that grows the surface out of its anchor. |
| side | Side | The side actually used, which differs from the preferred one when the surface was flipped. |
| top | number | Distance from the viewport's top edge, in px. |
RectInterface#
A rectangle in viewport coordinates, like a DOMRect.
interface RectProperties
| Name | Type | Description |
|---|---|---|
| bottom | number | Bottom edge. |
| height | number | Height. |
| left | number | Left edge. |
| right | number | Right edge. |
| top | number | Top edge. |
| width | number | Width. |
Types#
AlignType#
How a surface lines up along the anchor's edge: with its start, its centre or its end.
type Align = 'start' | 'center' | 'end'AnchorType#
What a surface is placed against: an element, or a viewport point such as a right-click.
An element of null yields no placement.
type Anchor = { element: HTMLElement | null } | { point: { x: number; y: number } }SideType#
The side of the anchor a surface is placed on.
type Side = 'top' | 'bottom' | 'right' | 'left'