useTouchPresentation@astryxdesign/core v0.7.5 · useTouchPresentation

Usage

Experimental: this hook may change or be removed in a patch release. The one answer to "is this a touch presentation?" that Astryx's adaptive components share, so a menu, a selector and a sheet never disagree on one device. It is SSR-safe: it renders the server default, then updates after hydration and when the conditions change (a window resized, a mouse attached). Touch behavior is one model on every touch device; nothing here chooses a platform. A phone in landscape is compact by its height. readTouchPresentation() reads the same answer once, for a decision taken at one moment, such as how a menu presents as it opens. For styles, write the same queries as media keys (@media (pointer: coarse)), so the first paint is already right.

ts
import {useTouchPresentation} from '@astryxdesign/core'

Best practices

GuidancePractices
Do

Choose touch behaviors on isTouch, layout on isCompact, and sheet-versus-popover on isCompactTouch.

Do

Pass a serverDefault when the server knows the device, to avoid a layout change after hydration.

Don't

Write your own phone media query; components would disagree about the same device.

Parameters

ParamTypeDescription
serverDefault
{isTouch?: boolean; isCompact?: boolean}

What to render before hydration, where no media query can be read (for example from a user-agent hint). Both default to false.

Returns

FieldTypeDescription
isTouchboolean

The primary pointer is coarse, a finger: (pointer: coarse). Key touch behaviors on it.

isCompactboolean

The viewport is compact: narrower than the theme's md point (768px by default) or shorter than 500px, the same numbers on every platform. Key layout on it.

isCompactTouchboolean

Both, for presentations that need both, such as a bottom sheet in place of a popover.

Use with shadcn

Already using the shadcn registry workflow? Install the real Astryx package and a local public re-export. Component implementation source stays in Astryx.

This install URL expires with the draft preview.
bash
npx shadcn@4.19.0 add https://astryx-h9q313l6c-fbopensource.vercel.app/shadcn/hooks/use-touch-presentation.json