Back to React Spectrum

ActionButton

packages/@adobe/react-spectrum/docs/button/ActionButton.mdx

2022-12-163.8 KB
Original Source

{/* 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-spectrum/button'; import {HeaderInfo, PropTable, PageDescription} from '@react-spectrum/docs'; import packageData from '@react-spectrum/button/package.json';

jsx
import {ActionButton} from '@react-spectrum/button';
import {View} from '@react-spectrum/view';
import {Flex} from '@react-spectrum/layout';

category: Buttons keywords: [action button]

ActionButton

<PageDescription>{docs.exports.ActionButton.description}</PageDescription>

<HeaderInfo packageData={packageData} componentNames={['ActionButton']} sourceData={[ {type: 'Spectrum', url: 'https://spectrum.adobe.com/page/action-button/'} ]} since="3.0.0" />

Example

tsx
<ActionButton>Edit</ActionButton>

Content

ActionButtons can have a label, an icon, or both. An icon is provided by passing an icon component as a child to the ActionButton. A visible label can be provided by passing a string or a Text component as a child, depending on whether the ActionButton has an accompanying icon.

tsx
import {Text} from '@react-spectrum/text';
import Edit from '@spectrum-icons/workflow/Edit';

<ActionButton>
  <Edit />
  <Text>Icon + Label</Text>
</ActionButton>

Accessibility

If no visible label is provided (e.g. an icon only button), an alternative text label must be provided to identify the control for accessibility. This should be added using the aria-label prop.

tsx
<ActionButton aria-label="Icon only">
  <Edit />
</ActionButton>

Internationalization

In order to internationalize an ActionButton, a localized string should be passed to the children or aria-label prop.

Events

ActionButtons support user interactions via mouse, keyboard, and touch. You can handle all of these via the onPress prop.

The following example uses an onPress handler to update a counter stored in React state.

tsx
function Example() {
  let [count, setCount] = React.useState(0);

  return (
   <ActionButton onPress={() => setCount(c => c + 1)}>{count} Edits</ActionButton>
  );
}

Props

<PropTable component={docs.exports.ActionButton} links={docs.links} />

Visual options

Quiet

View guidelines

tsx
<ActionButton isQuiet>Action!</ActionButton>

Disabled

View guidelines

tsx
<ActionButton isDisabled>Action!</ActionButton>

Static color

The staticColor prop can be used when an ActionButton is displayed over a color background. You are responsible for choosing the static color variant that ensures the text meets an accessible contrast ratio with the background.

tsx
<Flex wrap gap="size-250">
  <View backgroundColor="static-blue-700" padding="size-500">
    <ActionButton staticColor="white">
      <Edit />
      <Text>Edit</Text>
    </ActionButton>
  </View>
  <View backgroundColor="static-yellow-400" padding="size-500">
    <ActionButton staticColor="black" isQuiet>
      <Edit />
      <Text>Edit</Text>
    </ActionButton>
  </View>
</Flex>