Button and ButtonLink
The Button Component supports different variants, sizes, loading and disabled states, can include icons or only be an icon button. By default it also has an animation enabled, that can be disabled if needed. The ButtonLink Component looks exactly like Button but renders an anchor tag.
Includes basic buttons in different variants and sizes, buttons with icons, loading state, disabled state, no animation, icon buttons.
Examples
<Button variant="primary" size="large">Primary large</Button>
<Button variant="secondary" size="large">Secondary large</Button>
<Button variant="tertiary" size="medium">Tertiary medium</Button>
<Button variant="link" size="medium">Link medium</Button>Buttons with icons
<Button icon={<RiArrowRightCircleLine />}>
Right icon (default)
</Button>
<Button
icon={<RiArrowLeftCircleLine />}
iconPosition="left"
>
Left icon
</Button>Loading state
A11Y Requirement
When showing a loading state, there must also be an aria-live region to announce the loading state to screen readers.
<Button loading={isLoading}>
Search
</Button>
<div className="sr-only" aria-live="polite" aria-atomic="false">
{isLoading ? 'Searching...' : 'Search ready'}
</div>Disabled state
<Button disabled>Click me</Button>No animation
<Button disableAnimation>Primary large</Button>Icon only buttons
A11Y Requirement
Icon only buttons must have an aria-label.
<Button
variant="primary"
size="large"
aria-label="Search"
icon={<RiCommandLine />}
/><ButtonLink />
ButtonLink looks exactly like Button but renders an anchor tag and accepts an href prop for navigation.
Here are some examples of the ButtonLink component in different variants, sizes, and states:
<ButtonLink
href="/account"
// all other button props
>
Go to account
</ButtonLink>Accessibility
- Icon only buttons must have an aria-label.
- When showing a loading state, there must also be an aria-live region to announce the loading state to screen readers.
- Do not use tertiary buttons as replacements for text links in inline text as they might not provide enough contrast to surrouding text.
- These buttons are not intended to be used as tab or accordion controls or any other control requiring an active state.
- Whe multiple buttons are rendered in a row or column, their focus and hover states must make them visually distintive from the rest of the buttons in the group.
Requirements
| 3rd Party Libraries | version |
|---|---|
class-variance-authority | ^0.7.1 |
The following folder needs to be copied to your project:
| Copy to project | Reason |
|---|---|
ui/components/button/ | Includes <Button />, <ButtonLink /> and <RotateOnGroupHover /> |
Components
<Button />
The <Button /> component renders a <button> element. Per default it has an animation on hover that rotates the text (<RotateOnGroupHover />).
<ButtonLink />
The <ButtonLink /> component renders an <a> element and accepts an href prop for navigation. It looks exactly like the <Button /> component and has the same props.
Shared Props
| Prop | Type | Description |
|---|---|---|
variant | 'primary | secondary | tertiary | link' | The variant of the button. The link variant only looks like a link, but does not render an anchor tag. |
size | 'small | medium | large' | The size of the button. The variant link can't be size="large". |
disabled | boolean | The disabled state of the button. Adds disabled:cursor-not-allowed. |
loading | boolean | The loading state of the button. Shows a spinner icon and adds pointer-events-none. |
iconPosition | 'left | right' | The position of the icon. Only applies if an icon is provided. |
disableAnimation | boolean | Disables the animation on hover that rotates the text. |
icon | ReactNode | The icon to be rendered inside the button. Can be used with or without children. If used without children, it renders an icon button and requires an aria-label for accessibility reasons. |
<RotateOnGroupHover />
The <RotateOnGroupHover /> component is a wrapper component that adds a rotation animation to its children when the parent group is hovered. It is used inside the <Button /> & <ButtonLink /> component to add the rotation animation on hover.
- Add the
rotateOnHoverGroupClassNameclass to the parent element who's hover state you want to use as the trigger for rotation. - Wrap the child element you want to rotate in the
<RotateOnGroupHover>component.
Shared Props
| Prop | Type | Description |
|---|---|---|
afterClass | string | Additional class names that are added to the element after the rotation. This can be used to add additional styles to the button when it is hovered. |