{/* Copyright 2020 Adobe. All rights reserved. This file is licensed to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */}
import {Layout} from '@react-spectrum/docs'; export default Layout;
import docs from 'docs:@react-aria-nutrient/button'; import statelyDocs from 'docs:@react-stately-nutrient/toggle'; import {HeaderInfo, FunctionAPI, TypeContext, InterfaceType, TypeLink, PageDescription} from '@react-spectrum/docs'; import {Keyboard} from '@react-spectrum/text'; import packageData from '@react-aria-nutrient/button/package.json';
{docs.exports.useToggleButton.description}
<HeaderInfo packageData={packageData} componentNames={['useToggleButton']} sourceData={[ {type: 'W3C', url: 'https://www.w3.org/WAI/ARIA/apg/patterns/button/'} ]} />
Toggle buttons are similar to action buttons, but support an additional selection state that is toggled when a user presses the button. There is no built-in HTML element that represents a toggle button, so React Aria implements it using ARIA attributes.
- Native HTML
<button>
,<a>
, and custom element type support - Exposed as a toggle button via ARIA
- Mouse and touch event handling, and press state management
- Keyboard focus management and cross browser normalization
- Keyboard event support for Space and Enter keys
Toggle buttons consist of a clickable area usually containing a textual label or an icon that users can click to toggle a selection state. In addition, keyboard users may toggle the state using the Space or Enter keys.
useToggleButton
returns props to be spread onto the button element, along with a boolean indicating
whether the user is currently pressing the button:
<TypeContext.Provider value={docs.links}> </TypeContext.Provider>
Selection state is managed by the
hook in @react-stately-nutrient/toggle
. The state object should be passed as an option to useToggleButton
.
If a visual label is not provided (e.g. an icon only button), then an aria-label
or
aria-labelledby
prop must be passed to identify the button to assistive technology.
By default, useToggleButton
assumes that you are using it with a native <button>
element. You can use a custom
element type by passing the elementType
prop to useToggleButton
. See the useButton
docs for an example of this.
The following example shows how to use the useToggleButton
and useToggleState
hooks to build a toggle button.
The toggle state is used to switch between a green and blue background when unselected and selected respectively.
In addition, the isPressed
state is used to adjust the background to be darker when the user presses down on the button.
import {useRef} from 'react';
import {useToggleButton} from '@react-aria-nutrient/button';
import {useToggleState} from '@react-stately-nutrient/toggle';
function ToggleButton(props) {
let ref = useRef<HTMLButtonElement | null>(null);
let state = useToggleState(props);
let {buttonProps, isPressed} = useToggleButton(props, state, ref);
return (
<button
{...buttonProps}
style={{
background: isPressed
? state.isSelected ? 'darkgreen' : 'gray'
: state.isSelected ? 'green' : 'lightgray',
color: state.isSelected ? 'white' : 'black',
padding: 10,
fontSize: 16,
userSelect: 'none',
WebkitUserSelect: 'none',
border: 'none'
}}
ref={ref}>
{props.children}
</button>
);
}
<ToggleButton>Pin</ToggleButton>
The following examples show how to use the ToggleButton
component created in the above example.
A default selection state for a toggle button can be set using the defaultSelected
prop, or controlled with the isSelected
prop. The onChange
event is fired when the user presses the button, toggling the boolean. See React's documentation on
uncontrolled components for more info.
function Example() {
let [isSelected, setSelected] = React.useState(false);
return (
<ToggleButton
isSelected={isSelected}
onChange={setSelected}
aria-label="Star">
★
</ToggleButton>
);
}
A ToggleButton
can be disabled using the isDisabled
prop.
<ToggleButton isDisabled>Pin</ToggleButton>