| 1 |
--- |
| 2 |
id: Button |
| 3 |
section: components |
| 4 |
cssPrefix: pf-v6-c-button |
| 5 |
propComponents: ['Button', 'BadgeCountObject'] |
| 6 |
ouia: true |
| 7 |
--- |
| 8 |
|
| 9 |
import { Fragment, useState } from 'react'; |
| 10 |
import RhMicronsCloseIcon from '@patternfly/react-icons/dist/esm/icons/rh-microns-close-icon'; |
| 11 |
import RhUiExternalLinkFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-external-link-fill-icon'; |
| 12 |
import RhUiAddCircleFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-add-circle-fill-icon'; |
| 13 |
import RhUiCopyFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-copy-fill-icon'; |
| 14 |
import RhMicronsCaretRightIcon from '@patternfly/react-icons/dist/esm/icons/rh-microns-caret-right-icon'; |
| 15 |
import UploadIcon from '@patternfly/react-icons/dist/esm/icons/upload-icon'; |
| 16 |
import RhUiNotificationFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-notification-fill-icon'; |
| 17 |
import RhUiQuestionMarkCircleFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-question-mark-circle-fill-icon'; |
| 18 |
|
| 19 |
## Examples |
| 20 |
|
| 21 |
### Variant examples |
| 22 |
|
| 23 |
PatternFly supports several button styling variants to be used in different scenarios as needed. The following button variants can be assigned using the `variant` property. |
| 24 |
|
| 25 |
```ts file="./ButtonVariations.tsx" |
| 26 |
|
| 27 |
``` |
| 28 |
|
| 29 |
| Variant | Description | |
| 30 |
| --------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | |
| 31 |
| Primary | Primary buttons are the most visually prominent variant. Use for the most important call to action. | |
| 32 |
| Secondary | Secondary buttons are less visually prominant than primary buttons. Use for general actions on a page that require less emphasis than primary buttons. | |
| 33 |
| Tertiary | Tertiary buttons are the least visually prominent variant. Use to retain a classic button format with less emphasis than primary or secondary buttons. | |
| 34 |
| Danger | Danger buttons may be used for actions that are potentially destructive or difficult/impossible to undo. Danger is an independent variant, but all button variants may use the the `isDanger` property to apply similar styling. | |
| 35 |
| Warning | Warning buttons may be used for actions that change an important setting or behavior, but not in a destructive or irreversible way. | |
| 36 |
| Link | Links are labeled, but have no background or border. Use for actions that require less emphasis, actions that navigate users to another page within the same window, and/or actions that navigate to external pages in a new window. Links may be placed inline with text using the `isInline` property. | |
| 37 |
| Plain | Plain buttons have no styling and are intended to be labeled with icons. | |
| 38 |
| Control | Control buttons can be labeled with text or icons. Primarily intended to be paired with other controls in an [input group](/components/input-group). | |
| 39 |
| Stateful | Stateful buttons are ideal for displaying the state of notifications. They have 3 states: `read`, `unread` and `attention`. | |
| 40 |
|
| 41 |
### Disabled buttons |
| 42 |
|
| 43 |
To indicate that an action is currently unavailable, all button variations can be disabled using the `isDisabled` property. |
| 44 |
|
| 45 |
```ts file="./ButtonDisabled.tsx" |
| 46 |
|
| 47 |
``` |
| 48 |
|
| 49 |
### Small buttons |
| 50 |
|
| 51 |
To fit into tight spaces, primary, secondary, tertiary, danger, and warning button variations can be made smaller using the `"sm"` value for the `size` property. |
| 52 |
|
| 53 |
```ts file="./ButtonSmall.tsx" |
| 54 |
|
| 55 |
``` |
| 56 |
|
| 57 |
### Call to action (CTA) buttons |
| 58 |
|
| 59 |
CTA buttons and links direct users to complete an action. Primary, secondary, tertiary, and link button variants can be styled as CTAs using the `"lg"` value for the `size` property. |
| 60 |
|
| 61 |
```ts file="./ButtonCallToAction.tsx" |
| 62 |
|
| 63 |
``` |
| 64 |
|
| 65 |
### Block level buttons |
| 66 |
|
| 67 |
Block level buttons span the full width of the parent element and can be enabled using the `isBlock` property. |
| 68 |
|
| 69 |
```ts file="./ButtonBlock.tsx" |
| 70 |
|
| 71 |
``` |
| 72 |
|
| 73 |
### Progress indicators |
| 74 |
|
| 75 |
Progress indicators can be added to buttons to identify that an action is in progress after a click. |
| 76 |
|
| 77 |
```ts file="./ButtonProgress.tsx" |
| 78 |
|
| 79 |
``` |
| 80 |
|
| 81 |
### Links as buttons |
| 82 |
|
| 83 |
Buttons that link to another resource may take the form of primary, secondary, tertiary, or link variants. Use `component="a"` and an `href` property to designate the button's target link. |
| 84 |
|
| 85 |
```ts file="./ButtonLinks.tsx" |
| 86 |
|
| 87 |
``` |
| 88 |
|
| 89 |
### Inline link as span |
| 90 |
|
| 91 |
Inline links should use `component="span"` and the `isInline` property to wrap inline with surrounding text. |
| 92 |
|
| 93 |
```ts file="./ButtonInlineSpanLink.tsx" |
| 94 |
|
| 95 |
``` |
| 96 |
|
| 97 |
### Custom component |
| 98 |
|
| 99 |
In addition to being able to pass a string to the `component` property, you can provide more fine-tuned customization by passing a callback that returns a component. |
| 100 |
|
| 101 |
```ts file="./ButtonCustomComponent.tsx" |
| 102 |
|
| 103 |
``` |
| 104 |
|
| 105 |
### Aria-disabled examples |
| 106 |
|
| 107 |
[Accessible Rich Internet Applications (ARIA)](https://www.w3.org/WAI/standards-guidelines/aria/) is a set of roles and attributes specified by the World Wide Web Consortium. ARIA defines ways to make web content and web applications more accessible to people with disabilities. |
| 108 |
|
| 109 |
Buttons that are aria-disabled are similar to normal disabled buttons, except they can receive focus. Every button variant can be aria-disabled using the `isAriaDisabled` property. |
| 110 |
|
| 111 |
Unlike normal disabled buttons, aria-disabled buttons can support tooltips. Furthermore, aria-disabled buttons can operate as links, which also support tooltips. |
| 112 |
|
| 113 |
```ts file="./ButtonAriaDisabled.tsx" |
| 114 |
|
| 115 |
``` |
| 116 |
|
| 117 |
### Button with count |
| 118 |
|
| 119 |
Buttons can display a `count` in the form of a badge to indicate some value or number by passing in the `countOptions` prop as a `BadgeCountObject` object. The `BadgeCountObject` object will handle `count`, `isRead`, and `className` props for the badge count. |
| 120 |
|
| 121 |
```ts file="./ButtonWithCount.tsx" |
| 122 |
|
| 123 |
``` |
| 124 |
|
| 125 |
### Plain with no padding |
| 126 |
|
| 127 |
To display a plain/icon button inline with text, use `noPadding` prop in addition to `variant="plain"`. |
| 128 |
|
| 129 |
```ts file="./ButtonPlainHasNoPadding.tsx" |
| 130 |
|
| 131 |
``` |
| 132 |
|
| 133 |
### Stateful |
| 134 |
|
| 135 |
Stateful buttons are ideal for displaying the state of notifications. Use `variant="stateful"` alongside with the `state` property, which can have these 3 values: `read`, `unread` (used as default) and `attention` (which means unread and requires attention). |
| 136 |
|
| 137 |
```ts file="./ButtonStateful.tsx" |
| 138 |
|
| 139 |
``` |
| 140 |
|
| 141 |
### Circle buttons |
| 142 |
|
| 143 |
Pass `isCircle` to a button to modify its styling from simply rounded corners to complete circles. Circle buttons are intended for buttons that only contain an icon and adequate accessible naming. |
| 144 |
|
| 145 |
```ts file="./ButtonCircle.tsx" |
| 146 |
|
| 147 |
``` |
| 148 |
|
| 149 |
## Animated examples |
| 150 |
|
| 151 |
The following `<Button>` implementations have animations built into them. When using one of the following implementations, the `icon` property will be overridden due to the animations needing specific icons internally. |
| 152 |
|
| 153 |
### Favorite |
| 154 |
|
| 155 |
You can pass both the `isFavorite` and `variant="plain"` properties into the `<Button>` to create a favorite button. Passing the `isFavorited` property will determine the current favorited state and update styling accordingly. |
| 156 |
|
| 157 |
```ts file = "./ButtonFavorite.tsx" |
| 158 |
|
| 159 |
``` |
| 160 |
|
| 161 |
### Settings |
| 162 |
|
| 163 |
For a "settings" button with animations on hover and focus, you can pass the `isSettings` property into either a `variant="plain"` or `variant="control"` button. |
| 164 |
|
| 165 |
```ts file="./ButtonSettings.tsx" |
| 166 |
|
| 167 |
``` |
| 168 |
|
| 169 |
### Hamburger |
| 170 |
|
| 171 |
For a "hamburger" button (`fa-bars`) that will animate based on the `hamburgerVariant` property, you can pass the `isHamburger` property. This will render the default icon for a hamburger button, and updating the `hamburgerVariant` property with a value of either "expand" or "collapse" will change the icon accordingly with an animation on hover and focus. |
| 172 |
|
| 173 |
When used within a PatternFly `<Masthead>`, the animations and icon updates will occur automatically. See one of our [full screen page demos](/components/page/react-demos) to see this in action. |
| 174 |
|
| 175 |
```ts file="./ButtonHamburger.tsx" |
| 176 |
|
| 177 |
``` |
| 178 |
|
| 179 |
## Using router links |
| 180 |
|
| 181 |
Router links can be used for in-app linking in React environments to prevent page reloading. To use a `Link` component from a router package, you can follow our [custom component example](#custom-component) and pass a callback to the `component` property of the `Button`: |
| 182 |
|
| 183 |
```nolive |
| 184 |
<Button variant="link" component={(props: any) => <Link {...props} to="#" />}> |
| 185 |
Router link |
| 186 |
</Button> |
| 187 |
``` |