Soundorv0.13.0

Pressable

A view that responds to presses, with its state for styling.

Pressable is a view that responds to presses: a click, Enter or Space while it has focus, or a screen reader's "activate". Its style and children may be functions of its state (pressed, hovered, focused), so a button is drawn from the same state the runtime tracks.

import { Pressable, Text, View } from '@soundor/react';
import { useState } from 'react';

// A Pressable's style and children can follow its state: pressed, hovered
// and focused (Tab to it, then Enter or Space). Hold it for a long press.
export default function PressableStates() {
  const [presses, setPresses] = useState(0);
  const [longPresses, setLongPresses] = useState(0);

  return (
    <View style={{ alignItems: 'center', gap: 12 }}>
      <Pressable
        onPress={() => setPresses((count) => count + 1)}
        onLongPress={() => setLongPresses((count) => count + 1)}
        style={({ pressed, hovered, focused }) => ({
          width: 220,
          alignItems: 'center',
          padding: 16,
          borderRadius: 12,
          borderWidth: 2,
          borderColor: focused ? '#8fb3ff' : 'transparent',
          backgroundColor: pressed
            ? '#2a5fd0'
            : hovered
              ? '#4f8bff'
              : '#3a7bfd',
          transform: [{ scale: pressed ? 0.97 : 1 }],
        })}
      >
        {({ pressed, hovered, focused }) => (
          <Text style={{ color: 'white', fontSize: 16 }}>
            {pressed ? 'Pressed' : hovered ? 'Hovered' : 'Press me'}
            {focused ? ' · focused' : ''}
          </Text>
        )}
      </Pressable>
      <Text style={{ color: '#8b8f98' }}>
        {presses} presses · {longPresses} long presses
      </Text>
    </View>
  );
}
pressable.tsxSource
  • Presses: onPress after a press; onPressIn and onPressOut around it; onHoverIn and onHoverOut as the pointer comes and goes.
  • Long presses: with onLongPress, holding it delayLongPress ms (500 by default) without moving away is a long press, and not also a press.
  • Disabled: disabled makes it neither pressable nor focusable, and tells assistive technology so.
  • Focus: it is focusable, so Tab reaches it; Enter or Space press it.
  • To assistive technology it is one element, a button unless given another role, labelled by its text. It offers activate (its onPress) and, with onLongPress, longpress.

Props

Prop

Type

style and children take a function of its state:

Prop

Type

Pressable also takes the event props and the accessibility props.

On this page