Repository navigation
UI: add LinkButton #78944
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
UI: add LinkButton #78944
Changes from all commits
97e9a5a
1cf917e
5310cb4
e901e2d
a35beac
cb145a8
9c4c419
35e0041
0fd3ebb
6f7ddc8
2bdf9d5
b7e3c9a
ab40fa1
de47f5d
08b339b
664c9a9
87ae26b
fa47678
4c70ad6
b9d0ba5
7d26ad8
14c6eee
f444f3d
9a11dfe
b142e5f
13a8f8a
9007ae8
c599184
63a1c50
acbf582
73727a3
1d89e01
5403316
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| import { Canvas, Markdown, Meta } from '@storybook/addon-docs/blocks'; | ||
| import * as UsageGuidelinesStories from './usage-guidelines.story'; | ||
|
|
||
| <Meta of={ UsageGuidelinesStories } /> | ||
|
|
||
| # Button, IconButton, Link, and LinkButton | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One nuance that comes up in modern web apps are actions that change the URL but don't push an item onto the history stack. A common example would be a button that opens some kind of dialog or panel, and that new view has a permalink that is It's perhaps a bit of an advanced concept to use as our basic guideline, but we might want to capture this nuance somewhere in this doc as well.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah; I also don't also want to make document too technical sounding since one audience should be designers who necessarily don't know what we mean by technical descriptions like you're suggesting. Agree that "Navigation" is vague, or the whole topic is vague;
Perhaps there's a way to think and frame "navigate" entirely as non non-technical term, but I'm not sure how to phrase it. Good as-is for now and we can always iterate? |
||
|
|
||
| Choose the component based on **what happens when the user activates it**, not only on how it should look. | ||
|
|
||
| ## Quick reference | ||
|
|
||
| <Markdown>{ ` | ||
| | Goal | Component | Element | Examples | | ||
| | --- | --- | --- | --- | | ||
| | Performs an action on the current page | [Button](?path=/docs/design-system-components-button--docs), [IconButton](?path=/docs/design-system-components-iconbutton--docs) | \`<button>\` | Save, Delete, Submit, toolbar icon actions | | ||
| | Navigate to another page or URL | [Link](?path=/docs/design-system-components-link--docs) | \`<a>\` | "Learn more", docs links, external references | | ||
| | Navigate with button styling | [LinkButton](?path=/docs/design-system-components-linkbutton--docs) | \`<a>\` | Standalone CTAs, card actions | | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "card actions" reads as a bit ambiguous/contradictory for an example for links. What specifically did we mean here?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This type of card with links seems pretty common:
Since they're primary CTAs, folks don't want to use Link for visual reasons; a button carries more "CTA feel" and is more noticeable. Standalone would be more like what could be in EmptyState in the middle of the page:
Word are hard, and visual examples help. :-) I want to revisit this guide in a follow-up with clearer do/don't illustrations and "Do/Don't do" sections (similar to what I did in Badge) , but I think it's better for a follow-up. |
||
| ` }</Markdown> | ||
|
|
||
| ## Use `Button` and `IconButton` for actions | ||
|
|
||
| Use `Button` or `IconButton` when activation should **not** navigate away as a link would: | ||
|
|
||
| - Submitting or resetting a form (`type="submit"` / `type="reset"`) | ||
| - Opening overlays ([Dialog](?path=/docs/design-system-components-dialog--docs), [Popover](?path=/docs/design-system-components-popover--docs), etc.) | ||
| - Running JavaScript on the current page (`onClick` without a meaningful `href`) | ||
| - Toggle controls (`aria-pressed` on neutral minimal buttons) | ||
| - Showing a **loading** state while an async action completes | ||
|
|
||
| Use [IconButton](?path=/docs/design-system-components-iconbutton--docs) for the same kinds of actions when the control is **icon-only** — for example, toolbar buttons. It inherits `Button` props and adds a built-in tooltip via the required `label` prop (used as the accessible name). | ||
|
|
||
| `Button` and `IconButton` use button semantics. Do not use them as stand-ins for navigation when a real URL is the primary outcome. | ||
|
|
||
| <Canvas of={ UsageGuidelinesStories.UseButtonForActions } /> | ||
|
|
||
| See [Button](?path=/docs/design-system-components-button--docs) and [IconButton](?path=/docs/design-system-components-iconbutton--docs) for API details and examples. | ||
|
|
||
| ## Use `Link` for navigation | ||
|
|
||
| Use `Link` when activation should take the user to another page or URL. This is the default choice for navigation — not just inline references in running text, but any case where the outcome is “go somewhere else.” | ||
|
|
||
| `Link` styling communicates navigation more clearly than a button-shaped control. Users can see at a glance that they are following a link, not triggering an action on the current page. | ||
|
|
||
| Typical uses: | ||
|
|
||
| - Inline references inside [Text](?path=/docs/design-system-components-text--docs) or paragraphs | ||
| - Standalone “Learn more”, “View documentation”, or “Open site” actions | ||
| - External destinations with `openInNewTab` (adds `target="_blank"` and an accessible new-tab indicator) | ||
|
|
||
| Compose `Text` as the host and `Link` via `render` when you need typography from `Text` on a standalone link. | ||
|
|
||
| <Canvas of={ UsageGuidelinesStories.UseLinkForInlineNavigation } /> | ||
|
|
||
| See [Link](?path=/docs/design-system-components-link--docs) for API details and examples. | ||
|
|
||
| ## Use `LinkButton` for navigation with button styling | ||
|
|
||
| Use `LinkButton` when the destination is a **URL** and the control should **look like a `Button`** — for example, standalone calls to action, card footers, or notice actions where button weight fits the layout. | ||
|
|
||
| `LinkButton` shares `Button` visual props (`variant`, `tone`, `size`) but renders an `<a>` like `Link`. Use `openInNewTab` for external destinations (adds `target="_blank"` and an accessible new-tab indicator). It does **not** support `disabled`, `loading`, `focusableWhenDisabled`, `nativeButton`, or toggle `aria-pressed`. | ||
|
|
||
| Do **not** use `LinkButton` for form submit, dialogs, or other non-navigation actions — use `Button` instead. | ||
|
|
||
| Do **not** use `Button` with `render={ <a href="…" /> }` and `nativeButton={ false }` for new code when `LinkButton` fits; `LinkButton` is the supported pattern for button-styled links. | ||
|
|
||
| <Canvas of={ UsageGuidelinesStories.UseLinkButtonForNavigation } /> | ||
|
|
||
| > **Note:** Prefer `Link` for navigation when possible. Its underline and link styling communicate where the user is going more clearly than a button-shaped control. Reach for `LinkButton` only when you have considered `Button` and `Link` and still need button prominence. | ||
|
|
||
| See [LinkButton](?path=/docs/design-system-components-linkbutton--docs) for API details and examples. | ||
|
|
||
| ## Common mistakes | ||
|
|
||
| - **`Button` or `IconButton` + `onClick` for navigation** — Prefer `Link` with a real `href` so keyboard, assistive technology, and browser link affordances stay correct. | ||
| - **`LinkButton` when `Link` would work** — Default to `Link` for navigation; button styling can obscure that the control goes elsewhere. | ||
| - **`LinkButton` for submit or toggle actions** — Use `Button` or `IconButton`, depending on whether the control needs a visible text label or is icon-only; `LinkButton` does not support loading or pressed toggle styling. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| import type { Meta, StoryObj } from '@storybook/react-vite'; | ||
| import { createInterpolateElement } from '@wordpress/element'; | ||
| import { cog } from '@wordpress/icons'; | ||
| import { Button } from '../index'; | ||
| import { IconButton } from '../../icon-button'; | ||
| import { Link } from '../../link'; | ||
| import { LinkButton } from '../../link-button'; | ||
| import { Stack } from '../../stack'; | ||
| import { Text } from '../../text'; | ||
| import * as Tooltip from '../../tooltip'; | ||
|
|
||
| const meta: Meta = { | ||
| title: 'Design System/Components/Button/Usage Guidelines', | ||
| parameters: { | ||
| controls: { disable: true }, | ||
| }, | ||
| tags: [ '!dev' ], | ||
| }; | ||
| export default meta; | ||
|
|
||
| type Story = StoryObj; | ||
|
|
||
| /** | ||
| * Use `Button` or `IconButton` for actions on the current view: submitting a | ||
| * form, opening a dialog, toggling UI, or running JavaScript. Both render a | ||
| * `<button>` and support loading and pressed states. | ||
| */ | ||
| export const UseButtonForActions: Story = { | ||
| render: () => ( | ||
| <Tooltip.Provider delay={ 0 }> | ||
| <Stack direction="row" gap="sm" wrap="wrap" align="center"> | ||
| <Button type="submit">Save changes</Button> | ||
| <Button variant="outline" onClick={ () => {} }> | ||
| Settings | ||
| </Button> | ||
| <IconButton | ||
| icon={ cog } | ||
| label="Settings" | ||
| variant="outline" | ||
| onClick={ () => {} } | ||
| /> | ||
| </Stack> | ||
| </Tooltip.Provider> | ||
| ), | ||
| }; | ||
|
|
||
| /** | ||
| * Use `Link` for navigation. Its underline and link styling communicate where | ||
| * the user is going more clearly than a button-shaped control. | ||
| */ | ||
| export const UseLinkForInlineNavigation: Story = { | ||
| render: () => ( | ||
| <Text variant="body-md" render={ <p /> }> | ||
| { createInterpolateElement( | ||
| 'Read the <DocumentationLink /> for more details, or <ExternalLink />.', | ||
| { | ||
| DocumentationLink: ( | ||
| <Link href="https://wordpress.org/documentation/"> | ||
| documentation | ||
| </Link> | ||
| ), | ||
| ExternalLink: ( | ||
| <Link href="https://make.wordpress.org/" openInNewTab> | ||
| open an external reference | ||
| </Link> | ||
| ), | ||
| } | ||
| ) } | ||
| </Text> | ||
| ), | ||
| }; | ||
|
|
||
| /** | ||
| * Use `LinkButton` when navigation should look like a `Button`, such as | ||
| * standalone calls to action that require an `href`. | ||
| * | ||
| * Note: Prefer `Link` for navigation when possible. Its underline and link | ||
| * styling set clearer expectations than a button-shaped control. | ||
| */ | ||
| export const UseLinkButtonForNavigation: Story = { | ||
| render: () => ( | ||
| <Stack direction="column" gap="md"> | ||
| <Text variant="body-md" render={ <p /> }> | ||
| Standalone navigation calls to action can use `LinkButton` when | ||
| button styling matches the surrounding UI. | ||
| </Text> | ||
| <div> | ||
| <LinkButton href="https://make.wordpress.org/" openInNewTab> | ||
| Get started | ||
| </LinkButton> | ||
| </div> | ||
| <Text variant="body-md" render={ <p /> }> | ||
| { createInterpolateElement( | ||
| 'Note: Prefer <LinkComponent /> for navigation when possible — its underline and link styling communicate where the user is going more clearly than a button-shaped control.', | ||
| { | ||
| LinkComponent: ( | ||
| <Link href="https://wordpress.org/documentation/"> | ||
| Link | ||
| </Link> | ||
| ), | ||
| } | ||
| ) } | ||
| </Text> | ||
| </Stack> | ||
| ), | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| import { forwardRef } from '@wordpress/element'; | ||
| import { ButtonIcon } from '../button/icon'; | ||
| import { type LinkButtonIconProps } from './types'; | ||
|
|
||
| export const LinkButtonIcon = forwardRef< SVGSVGElement, LinkButtonIconProps >( | ||
| function LinkButtonIcon( props, ref ) { | ||
| return <ButtonIcon ref={ ref } { ...props } />; | ||
| } | ||
| ); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import { LinkButton as _LinkButton } from './link-button'; | ||
| import { LinkButtonIcon } from './icon'; | ||
|
|
||
| export type { LinkButtonProps, LinkButtonIconProps } from './types'; | ||
|
|
||
| LinkButtonIcon.displayName = 'LinkButton.Icon'; | ||
|
|
||
| /** | ||
| * A link that looks like a `Button`. Prefer `Link` for navigation unless | ||
| * button prominence is intentional. | ||
|
simison marked this conversation as resolved.
Resolved
|
||
| * | ||
| * See the [Usage Guidelines](https://wordpress.github.io/gutenberg/?path=/docs/design-system-components-button-usage-guidelines--docs) | ||
| * for when to use `Button`, `IconButton`, `Link`, or `LinkButton`. | ||
| */ | ||
| export const LinkButton = Object.assign( _LinkButton, { | ||
| /** | ||
| * An icon component specifically designed to work well when rendered inside | ||
| * a `LinkButton` component. | ||
| */ | ||
| Icon: LinkButtonIcon, | ||
| } ); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| import clsx from 'clsx'; | ||
| import { forwardRef } from '@wordpress/element'; | ||
| import { Link } from '../link'; | ||
| import { type LinkButtonProps } from './types'; | ||
| import buttonStyles from '../button/style.module.css'; | ||
| import focusStyles from '../utils/css/focus.module.css'; | ||
| import styles from './style.module.css'; | ||
|
|
||
| /** | ||
| * A link that looks like a `Button`. Prefer `Link` for navigation unless | ||
| * button prominence is intentional. | ||
| * | ||
| * See the [Usage Guidelines](https://wordpress.github.io/gutenberg/?path=/docs/design-system-components-button-usage-guidelines--docs) | ||
| * for when to use `Button`, `IconButton`, `Link`, or `LinkButton`. | ||
| */ | ||
| export const LinkButton = forwardRef< HTMLAnchorElement, LinkButtonProps >( | ||
| function LinkButton( | ||
| { | ||
| tone = 'brand', | ||
| variant = 'solid', | ||
| size = 'default', | ||
| className, | ||
| children, | ||
| ...props | ||
| }, | ||
| ref | ||
| ) { | ||
| return ( | ||
| <Link | ||
| ref={ ref } | ||
| variant="unstyled" | ||
| className={ clsx( | ||
| styles[ 'link-button' ], | ||
| focusStyles[ 'outset-ring--focus-except-active' ], | ||
|
simison marked this conversation as resolved.
Resolved
|
||
| variant !== 'unstyled' && buttonStyles.button, | ||
| buttonStyles[ `is-${ tone }` ], | ||
| buttonStyles[ `is-${ variant }` ], | ||
| buttonStyles[ `is-${ size }` ], | ||
| className | ||
| ) } | ||
| { ...props } | ||
| > | ||
| { children } | ||
| </Link> | ||
| ); | ||
| } | ||
| ); | ||


Uh oh!
There was an error while loading. Please reload this page.