createButton
Provides the behavior and accessibility implementation for a button component. Use this hook when you need full control over the button's appearance while maintaining proper accessibility.
Import
import { createButton } from '@proyecto-viviana/solidaria';Usage
function CustomButton(props) {
const { buttonProps, isPressed } = createButton({
onPress: props.onPress,
isDisabled: props.isDisabled,
});
return (
<button
{...buttonProps}
class={isPressed() ? 'pressed' : ''}
>
{props.children}
</button>
);
}Custom Styled Button
Build a button with custom styling while maintaining accessibility.
Status: Not pressed yet
function GradientButton(props) {
const { buttonProps, isPressed } = createButton({
onPress: props.onPress,
});
return (
<button
{...buttonProps}
class={`gradient-btn ${isPressed() ? 'scale-95' : ''}`}
>
{props.children}
</button>
);
}Button as Link
Create an accessible link that looks like a button.
function ButtonLink(props) {
const { buttonProps } = createButton({
elementType: 'a',
});
return (
<a {...buttonProps} href={props.href}>
{props.children}
</a>
);
}Parameters
Props
| Prop | Type | Default | Description |
|---|---|---|---|
onPress | (e: PressEvent) => void | — | Handler called when the button is pressed |
onPressStart | (e: PressEvent) => void | — | Handler called when press starts |
onPressEnd | (e: PressEvent) => void | — | Handler called when press ends |
onPressChange | (isPressed: boolean) => void | — | Handler called when pressed state changes |
onPressUp | (e: PressEvent) => void | — | Handler called when pointer is released |
isDisabled | boolean | false | Whether the button is disabled |
elementType | 'button' | 'a' | 'div' | 'span' | 'button' | The HTML element to render as |
type | 'button' | 'submit' | 'reset' | 'button' | The button type (for form submission) |
href | string | — | URL for link buttons |
target | string | — | Link target (_blank, _self, etc.) |
excludeFromTabOrder | boolean | false | Remove from tab order |
Return Value
Props
| Prop | Type | Default | Description |
|---|---|---|---|
buttonProps | JSX.HTMLAttributes | — | Props to spread on the button element |
isPressed | Accessor<boolean> | — | Signal indicating if button is currently pressed |
Accessibility
- Handles
role="button"when using non-button elements - Manages
tabIndexfor keyboard accessibility - Handles Enter and Space key presses
- Sets
aria-disabledfor disabled state - Normalizes press events across mouse, touch, and keyboard
- Prevents default behavior for non-native buttons