hono-preact
Overview
Quick Start
The Route Table
Layouts & Nesting
Adding Pages
Active Links
Server Loaders
Loading States
Reloading Data
Prefetching
Streaming
Live Loaders
Realtime Channels
Server Actions
Validation
Optimistic UI
View Transitions
Middleware
CSRF Protection
CLI
Vite Config
Project Structure
Composing Hono Middleware
WebSockets
Rooms & Presence
renderPage
Link Prefetch
Build & Deploy
Overview
Dialog
Popover
Tooltip
Menu
Context Menu
Select
Combobox
Toast
renderElement
useControllableState
mergeRefs
useListNavigation
useTypeahead
useListboxSelection
usePosition
usePositioner
useDismiss
useFocusReturn
useSafeArea
usePresence

usePositioner#

usePositioner is the complete anchored-overlay positioning hook the library's own Popover, Tooltip, Menu, Select, and Combobox parts are built on. It composes usePosition (floating placement), usePresence (the open/close lifecycle), top-layer promotion, and the user-agent [popover] style neutralization into a single hook, so a custom anchored overlay does not have to rediscover the platform quirks the library already handles.

See also: usePosition, usePresence, useDismiss.

Demo#

Example#

import { usePositioner } from 'hono-preact-ui';
import { useRef } from 'preact/hooks';

function Anchored({ open }: { open: boolean }) {
  const anchorRef = useRef<HTMLButtonElement>(null);
  const floatingRef = useRef<HTMLElement>(null);

  const { isPresent, positionerProps, state } = usePositioner({
    open,
    anchorRef,
    floatingRef,
    side: 'bottom',
    align: 'center',
    offset: 8,
    mount: 'unmount',
  });

  return (
    <>
      <button ref={anchorRef}>anchor</button>
      {isPresent && (
        <div {...positionerProps} data-side={state.side}>
          overlay contents
        </div>
      )}
    </>
  );
}

Signature#

import { usePositioner } from 'hono-preact-ui';

function usePositioner(opts: UsePositionerOptions): UsePositionerResult;

Options#

OptionTypeNotes
openbooleanThe overlay's open state; drives the presence lifecycle.
anchorRefRefObject<HTMLElement>The element the overlay is positioned against.
floatingRefRefObject<HTMLElement>The positioned overlay element.
sideSidePreferred side: top, right, bottom, or left.
alignAlignAlignment along the side: start, center, or end.
offsetnumberGap in pixels between anchor and overlay.
getAnchorRectClientRectGetterOptional. Position against a point or virtual element instead of anchorRef (e.g. a pointer position).
mount'unmount' | 'hidden''unmount': branch on isPresent and return null while closed. 'hidden': keep the element mounted and hidden while closed.

usePositioner returns { isPresent, positionerProps, state, position, arrowRef }: spread positionerProps onto your positioner element (it already carries the merged ref including floatingRef, Popover-API promotion, the UA [popover] style resets, and data-side / data-align), branch on isPresent when mount is 'unmount', and read state.side / state.align for side-aware styling. For a custom arrow, attach the returned arrowRef to your arrow element and read position.arrowX / position.arrowY for its offset (this is what the built-in Arrow part does).