Skip to main content
PROYECTOVIVIANA

createPress

Handles press interactions across mouse, touch, keyboard, and virtual clicks. This is the foundation for createButton and other interactive components.

Import

import { createPress } from '@proyecto-viviana/solidaria';

Usage

const { pressProps, isPressed } = createPress({
  onPress: () => setStatus('pressed'),
  onPressStart: () => setStatus('press started'),
  onPressEnd: () => setStatus('press ended'),
});

return (
  <div {...pressProps} tabIndex={0} role="button">
    Press me
  </div>
);

Interactive Card

Make any element pressable while normalizing all input types.

Press count: 0

Last input: none

Try clicking, touching, or pressing Enter/Space

const { pressProps, isPressed } = createPress({
  onPress: (e) => setLastInput(e.pointerType),
});

<div
  {...pressProps}
  tabIndex={0}
  role="button"
  class={isPressed() ? 'scale-95' : ''}
>
  Click, touch, or press Enter/Space
</div>

Press State Tracking

Track press start and end separately for visual feedback.

Press and hold me

Watch the events log →

Event Log:

No events yet...

const { pressProps, isPressed } = createPress({
  onPressStart: (e) => setStatus('pressing...'),
  onPressEnd: (e) => setStatus('released'),
  onPress: (e) => setStatus('completed!'),
});

Parameters

Props

PropTypeDefaultDescription
onPress(e: PressEvent) => voidHandler called when a press is completed (pointer up within element)
onPressStart(e: PressEvent) => voidHandler called when a press starts
onPressEnd(e: PressEvent) => voidHandler called when a press ends (either completed or cancelled)
onPressChange(isPressed: boolean) => voidHandler called when the pressed state changes
onPressUp(e: PressEvent) => voidHandler called when pointer is released over the element
isDisabledbooleanfalseWhether press events are disabled
isPressedbooleanWhether the element is pressed (controlled)
preventFocusOnPressbooleanfalsePrevent focus when pressing with mouse/touch
shouldCancelOnPointerExitbooleanfalseCancel press when pointer leaves element
allowTextSelectionOnPressbooleanfalseAllow text selection during press

Return Value

Props

PropTypeDefaultDescription
pressPropsJSX.HTMLAttributesProps to spread on the target element
isPressedAccessor<boolean>Signal indicating if element is currently pressed

PressEvent Object

Props

PropTypeDefaultDescription
type'pressstart' | 'pressend' | 'pressup' | 'press'The type of press event
pointerType'mouse' | 'pen' | 'touch' | 'keyboard' | 'virtual'The type of pointer that triggered the event
targetHTMLElementThe element that was pressed
shiftKeybooleanWhether shift key was held
ctrlKeybooleanWhether ctrl key was held
metaKeybooleanWhether meta/cmd key was held
altKeybooleanWhether alt key was held

Pointer Type Handling

The hook normalizes interactions across different input methods:

  • mouse - Desktop mouse clicks
  • touch - Mobile/tablet touch events
  • pen - Stylus interactions
  • keyboard - Enter or Space key presses
  • virtual - Programmatic triggers (e.g., element.click())

Accessibility

  • Supports keyboard activation via Enter and Space keys
  • Works with screen reader virtual clicks
  • Remember to add role="button" and tabIndex=0 for non-button elements
  • Press is cancelled if pointer exits element (prevents accidental activation)
  • Prevents text selection during press for better UX