1.19.0 • Published 5 months ago

@asphalt-react/button v1.19.0

Weekly downloads
-
License
UNLICENSED
Repository
-
Last release
5 months ago

Button

Buttons provide a way to trigger an action. The caption of a Button should hint what action it'll perform. The caption is typically text but Buttons can also have an icon with text or just an icon (which we call as Icon Button).

Buttons come in 4 variants to emphasize different levels:

  • Primary (default): For strong call to an action.
  • Secondary: For supporting primary buttons.
  • Tertiary: For actions less important than primary actions.
  • Nude: For least important actions.

There are 3 intents, which paired with the above variants address multiple supportive actions:

  • Brand (default): For most recommended action.
  • Danger: For actions that have some element of risk like deletion.
  • System: For less recommended actions.

Variants and an intents are mutually exclusive. Button falls back to the default value if a variant or intent occurs more than once. For example, using primary and nude together will fallback to a primary variant.

Button features the ability to render a semantic link styled as a button (which we call Link Button). Link Buttons and Icon Link Buttons — icon as caption — handle security and privacy risks if the link opens in a new tab.

Button package exports a named ToggleButton component as well. A ToggleButton is a digital switch to turn an option on or off.

Buttons support 4 sizes: - extra small (xs) - small (s) - medium (m) - large (l)

Buttons's caption decides its width by default, but it can also stretch to take its container's width. The rendered button element has a type attribute set to "button". You may override the type attribute if needed.

Buttons accept React ref to give access to the underlying element. They accept most of the button element's attributes such as disabled & onClick and supports data-* attributes.

Accessibility

  1. All buttons are focusable and keyboard navigable; tab (or shift+tab when tabbing backwards).
  2. space and enter keys trigger the buttons when in focus; toggles the state of ToggleButton.
  3. ToggleButton has a role of “switch”.
  4. The on/off state of ToggleButton determines the value of aria-checked attribute.
  5. enter key triggers the link buttons when in focus.
  6. All buttons accept the aria-* attributes button role.
  7. Link buttons accept the aria-* attributes for the link role.

Accessibility must-haves

  1. Add aria-label or aria-labelledby in Icon Button & icon-only variant of ToggleButton to help assitive technologies.

Usage

import { Button } from '@asphalt-react/button'

<Button>Click me</Button>

Props

children

React node for button's caption

typerequireddefault
nodetrueN/A

primary

Renders a primary variant

typerequireddefault
boolfalsefalse

secondary

Renders a secondary variant

typerequireddefault
boolfalsefalse

tertiary

Renders a tertiary variant

typerequireddefault
boolfalsefalse

nude

Renders a nude variant

typerequireddefault
boolfalsefalse

brand

Renders brand intent

typerequireddefault
boolfalsefalse

danger

Renders danger intent

typerequireddefault
boolfalsefalse

system

Renders system intent

typerequireddefault
boolfalsefalse

link

Renders a link button

typerequireddefault
boolfalsefalse

as

Link element to render. Accepts an HTML element or a React component.

typerequireddefault
elementTypefalse"a"

asProps

Accepts props & attributes for the link element.

typerequireddefault
objectfalse{ href: "." }

icon

Renders an icon button

typerequireddefault
boolfalsefalse

compact

Renders an icon button with less padding. Works only with a nude icon button. Does not support any intent.

typerequireddefault
boolfalsefalse

qualifier

Qualifiers are icons that enhance the caption. Button prepends the qualifier by default

Accepts SVG or SVG wrapped React component. Checkout @asphalt-react/iconpack for SVG wrapped React components.

⚠️ Do not use qualifier to render an icon button, use icon prop instead

typerequireddefault
elementfalsenull

qualifierEnd

Appends qualifier to the caption

typerequireddefault
boolfalsefalse

size

Controls size of button. Possible values are "xs", "s", "m", "l" for extra small, small, medium & large respectively

typerequireddefault
enumfalse"m"

stretch

Stretches Button to take it's container width

typerequireddefault
boolfalsefalse

ToggleButton

A ToggleButton is a digital switch to turn an option on or off. Switching between the states — as a result of direct user action or a programmatic event — should produce an immediate result. The caption should always reflect the current logical state of the button.

There are two variants:

  1. text-only
  2. icon-only

ToggleButton vs Checkbox

Similar to ToggleSwitches, ToggleButtons are not an alternative to Checkboxes. They are not form controls and should not appear inside a form. ToggleButtons should only manipulate local UI states, i.e. they should not directly or indirectly initiate a remote API call to change database states.

ToggleButton vs ToggleSwitch

Both of them provide options to toggle between states. Consider the following points to choose between them:

  1. Prefer ToggleSwitches to toggle states on mobile; they support swipe gestures
  2. ToggleButtons fit better in designs with space constraints
  3. Use ToggleButtons to programmatically toggle the state. For example, you can bind a keyboard shortcut to a mute button in a video player.

Usage

import  { ToggleButton } from '@asphalt-react/button'

<ToggleButton text="mute" on />

Props

text

Label for text-only variant

typerequireddefault
stringfalseN/A

icon

Icon for icon-only variant. Accepts SVG or SVG wrapped React component

Note: icon prop overrides text prop

typerequireddefault
nodefalseN/A

children

React node for caption

Note: Both icon and text props override children

typerequireddefault
nodefalseN/A

on

Sets the state of button

typerequireddefault
boolfalsefalse

size

Sets the size of button. Possible values are "s", "m", "l" for small, medium & large respectively

typerequireddefault
enumfalse"m"