Skip to content

Early previewhamur is not on npm yet. The components are being built in the open, follow along on GitHub.

InputsReact

Toggle

A two-state button that can be on or off.

Rendered by React 19.3.1

Loading React…
import * as React from 'react'
import { Toggle } from '@hamur/react/toggle'

export default function ToggleExample() {
  const [pressed, setPressed] = React.useState(false)
  return (
    <div className="stack">
      <div className="row">
        <Toggle
          className="toggle"
          aria-label="Bold"
          onPressedChange={setPressed}
        >
          Bold
        </Toggle>
        <Toggle className="toggle" aria-label="Italic" disabled>
          Italic
        </Toggle>
      </div>
      <p className="muted">Bold is {pressed ? 'on' : 'off'}</p>
    </div>
  )
}
62Tests passing in React · 0 failing
5.6 KiBSize
0.60 msCommit
29.0 msFrame
11.0 msMutations
3.00 msMutatedElements
Not measured yetMemory
productdevbook and 13 upstream authors12 commits · last

Installation

npm i @hamur/react

Not on npm yet. The package goes out under the chosen name.

Anatomy

Import the component and assemble its parts:

<Toggle />

Examples

The demo at the top is the one example this component has so far.

API reference

6 props across 1 parts, in React syntax. className, style, render are listed once, under Shared props.

ToggleToggle

A two-state button that can be on or off. Renders a <button>.

Toggle props
PropTypeDefault
valueA unique string that identifies the toggle when usedTValue—
defaultPressedWhether the toggle button is currently pressed.booleanfalse
pressedWhether the toggle button is currently pressed.boolean—
onPressedChangeCallback fired when the pressed state is changed.((pressed: boolean, eventDetails: { reason: "none"; event: Event; cancel: () => void; allowPropagation: () => void; isCanceled: boolean; isPropagationAllowed: boolean; trigger: Element | undefined; }) => void)—
nativeButtonWhether the component renders a native <button> element when replacing itbooleantrue
disabledWhether the component should ignore user interaction.booleanfalse
Toggle data attributes
Data attributeDescription
data-pressedPresent when the toggle button is pressed.
data-disabledPresent when the toggle button is disabled.

Shared props

Every part that renders an element takes these.

Shared props
PropType
classNameCSS class applied to the element, or a function that returns a class based on the component's state.string | ((state: State) => string | undefined)
styleStyle applied to the element, or a function that returns a style object based on the component's state.React.CSSProperties | ((state: State) => React.CSSProperties | undefined)
renderAllows you to replace the component's HTML element with a different tag, or compose it with another component.React.ReactElement<unknown, string | React.JSXElementConstructor<any>> | ComponentRenderFn<HTMLProps, State>

Keyboard and accessibility

Keyboard

Toggle has no keys of its own; as a native <button> it takes Enter and Space from the browser.

Accessibility

What the parts set
PartSetsSo that
Togglearia-pressed from presseda screen reader says whether it is on
Togglerenders a <button> while nativeButton is onit is reached and activated like any button

Focus order

  1. Toggle is one tab stop; inside a ToggleGroup the group decides which toggle takes it.