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:
onPressafter a press;onPressInandonPressOutaround it;onHoverInandonHoverOutas the pointer comes and goes. - Long presses: with
onLongPress, holding itdelayLongPressms (500 by default) without moving away is a long press, and not also a press. - Disabled:
disabledmakes 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
buttonunless given another role, labelled by its text. It offersactivate(itsonPress) and, withonLongPress,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.