Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/ui/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
- `AlertDialog`, `Dialog`, `Drawer`, `Popover`, and `Tooltip`: Derive Trigger props from the corresponding Base UI components. ([#81824](https://github.com/WordPress/gutenberg/pull/81824))
- `Autocomplete.Status`, `Combobox.Status`: Add a `Status` subcomponent that announces async list status to screen readers. Item popups give Status its own collapsing grid row so a visible result count sits above the list without overlaying items ([#82195](https://github.com/WordPress/gutenberg/pull/82195)).
- `Autocomplete.Popup`, `Combobox.Popup`, `SearchableChipSelect`, `SearchableChipSelectControl`, `Select.Popup`, and `SelectControl`: Add `width` on Popups and `popupWidth` on composites, with preset width constraints (`anchor`, `content`, `sm`, `md`, `lg`, and `available`) for the item popup. `SelectControl` defaults to `content` to preserve prior popup sizing ([#82087](https://github.com/WordPress/gutenberg/pull/82087), [#82193](https://github.com/WordPress/gutenberg/pull/82193)).
- `Link`, `Menu.LinkItem`: Accept `target`, apply the new-tab indicator and accessible notice for `_blank`, and preserve explicit targets when `openInNewTab` is set. ([#82347](https://github.com/WordPress/gutenberg/pull/82347))
- `Menu`: Support multiple item descriptions. ([#81825](https://github.com/WordPress/gutenberg/pull/81825))
- `Menu`: Use `Text` for item labels and descriptions to share typography and apply `text-wrap: pretty`. ([#82237](https://github.com/WordPress/gutenberg/pull/82237))
- `Text`: Apply `text-wrap: pretty` by default to improve line wrapping. ([#82133](https://github.com/WordPress/gutenberg/pull/82133))
Expand Down
6 changes: 4 additions & 2 deletions packages/ui/src/link/link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ export const Link = forwardRef< HTMLAnchorElement, LinkProps >( function Link(
variant = 'default',
tone = 'brand',
openInNewTab = false,
target,
render,
className,
...props
},
ref
) {
const shouldShowNewTabIndicator = openInNewTab || target === '_blank';
const element = useRender( {
render,
defaultTagName: 'a',
Expand All @@ -41,11 +43,11 @@ export const Link = forwardRef< HTMLAnchorElement, LinkProps >( function Link(
variant === 'unstyled' && styles[ 'is-unstyled' ],
className
),
target: openInNewTab ? '_blank' : undefined,
target: target ?? ( openInNewTab ? '_blank' : undefined ),
children: (
<>
{ children }
{ openInNewTab && (
{ shouldShowNewTabIndicator && (
<span
className={ styles[ 'link-icon' ] }
role="img"
Expand Down
8 changes: 8 additions & 0 deletions packages/ui/src/link/stories/index.story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ export const Default: Story = {
},
};

export const OpenInNewTab: Story = {
args: {
children: 'Visit WordPress.org',
href: 'https://wordpress.org',
openInNewTab: true,
},
};

/**
* Note: `tone` has no effect on `unstyled` variant
*/
Expand Down
47 changes: 47 additions & 0 deletions packages/ui/src/link/test/index.jsdom.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,52 @@ describe( 'Link', () => {
} )
).toBeVisible();
} );

it( 'treats target="_blank" as opening in a new tab', () => {
render(
<Link href="https://example.com" target="_blank">
External
</Link>
);

expect(
screen.getByRole( 'link', {
name: 'External (opens in a new tab)',
} )
).toHaveAttribute( 'target', '_blank' );
} );

it( 'forwards a named target without adding a new tab notice', () => {
render(
<Link href="https://example.com" target="wp-preview-123">
Preview
</Link>
);

expect(
screen.getByRole( 'link', { name: 'Preview' } )
).toHaveAttribute( 'target', 'wp-preview-123' );
expect(
screen.queryByLabelText( '(opens in a new tab)' )
).not.toBeInTheDocument();
} );

it( 'preserves an explicit target when openInNewTab is true', () => {
render(
<Link
href="https://example.com"
target="wp-preview-123"
openInNewTab
>
Preview
</Link>
);

expect(
screen.getByRole( 'link', {
name: 'Preview (opens in a new tab)',
} )
).toHaveAttribute( 'target', 'wp-preview-123' );
} );
} );
} );
13 changes: 11 additions & 2 deletions packages/ui/src/link/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@ import { type ReactNode } from 'react';
import { type ComponentProps } from '../utils/types';

export interface LinkProps extends Omit< ComponentProps< 'a' >, 'target' > {
/**
* Where to open the linked document. `"_blank"` also adds the visual
* indicator and accessible new-tab notice.
*
* When both `target` and `openInNewTab` are set, `target` determines the
* browsing context.
*/
target?: ComponentProps< 'a' >[ 'target' ];

/**
* The visual treatment of the link.
*
Expand All @@ -21,8 +30,8 @@ export interface LinkProps extends Omit< ComponentProps< 'a' >, 'target' > {
tone?: 'brand' | 'neutral';

/**
* Whether to open the link in a new browser tab.
* When true, sets `target="_blank"` and appends a visual arrow indicator.
* Adds a visual indicator and accessible notice for opening in a new tab.
* Defaults `target` to `"_blank"` when no explicit target is set.
*
* @default false
*/
Expand Down
6 changes: 4 additions & 2 deletions packages/ui/src/menu/link-item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const LinkItem = forwardRef< Element, LinkItemProps >( function MenuLinkItem(
children,
className,
openInNewTab = false,
target,
prefix,
shortcut,
suffix,
Expand All @@ -29,7 +30,8 @@ const LinkItem = forwardRef< Element, LinkItemProps >( function MenuLinkItem(
},
ref
) {
const externalLinkIndicator = openInNewTab ? (
const shouldShowNewTabIndicator = openInNewTab || target === '_blank';
const externalLinkIndicator = shouldShowNewTabIndicator ? (
<span
className={ styles[ 'external-link-indicator' ] }
role="img"
Expand Down Expand Up @@ -59,7 +61,7 @@ const LinkItem = forwardRef< Element, LinkItemProps >( function MenuLinkItem(
{ ...props }
{ ...itemAriaProps }
rel={ rel }
target={ openInNewTab ? '_blank' : undefined }
target={ target ?? ( openInNewTab ? '_blank' : undefined ) }
className={ clsx(
defenseStyles.a,
resetStyles[ 'box-sizing' ],
Expand Down
1 change: 1 addition & 0 deletions packages/ui/src/menu/stories/index.story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ export const LinkItem: Story = {
<Menu.LinkItem
href="https://developer.wordpress.org"
openInNewTab
target="wordpress-developer-resources"
shortcut={ SHORTCUTS.developerResources }
suffix="Docs"
>
Expand Down
76 changes: 76 additions & 0 deletions packages/ui/src/menu/test/index.jsdom.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1363,6 +1363,82 @@ describe( 'Menu', () => {
).not.toHaveAttribute( 'aria-labelledby' );
} );

it( 'treats target="_blank" on link items as opening in a new tab', async () => {
const user = userEvent.setup();

render(
<Menu.Root>
<Menu.Trigger>Actions</Menu.Trigger>
<Menu.Popup>
<Menu.LinkItem href="https://wordpress.org" target="_blank">
<Menu.ItemLabel>WordPress.org</Menu.ItemLabel>
</Menu.LinkItem>
</Menu.Popup>
</Menu.Root>
);

await user.click( screen.getByRole( 'button', { name: 'Actions' } ) );

expect(
await screen.findByRole( 'menuitem', {
name: 'WordPress.org (opens in a new tab)',
} )
).toHaveAttribute( 'target', '_blank' );
} );

it( 'forwards a named target on link items without adding a new tab notice', async () => {
const user = userEvent.setup();

render(
<Menu.Root>
<Menu.Trigger>Actions</Menu.Trigger>
<Menu.Popup>
<Menu.LinkItem
href="https://wordpress.org"
target="wp-preview-123"
>
<Menu.ItemLabel>Preview</Menu.ItemLabel>
</Menu.LinkItem>
</Menu.Popup>
</Menu.Root>
);

await user.click( screen.getByRole( 'button', { name: 'Actions' } ) );

const item = await screen.findByRole( 'menuitem', { name: 'Preview' } );
expect( item ).toHaveAttribute( 'target', 'wp-preview-123' );
expect(
screen.queryByLabelText( '(opens in a new tab)' )
).not.toBeInTheDocument();
} );

it( 'preserves an explicit link item target when openInNewTab is true', async () => {
const user = userEvent.setup();

render(
<Menu.Root>
<Menu.Trigger>Actions</Menu.Trigger>
<Menu.Popup>
<Menu.LinkItem
href="https://wordpress.org"
target="wp-preview-123"
openInNewTab
>
<Menu.ItemLabel>Preview</Menu.ItemLabel>
</Menu.LinkItem>
</Menu.Popup>
</Menu.Root>
);

await user.click( screen.getByRole( 'button', { name: 'Actions' } ) );

expect(
await screen.findByRole( 'menuitem', {
name: 'Preview (opens in a new tab)',
} )
).toHaveAttribute( 'target', 'wp-preview-123' );
} );

it( 'uses custom item label and description ids for generated aria relationships', async () => {
const user = userEvent.setup();

Expand Down
13 changes: 11 additions & 2 deletions packages/ui/src/menu/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,17 @@ export type LinkItemProps = Omit<
> &
MenuItemLayoutProps & {
/**
* Whether to open the link in a new browser tab.
* When true, sets `target="_blank"` and appends a visual arrow indicator.
* Where to open the linked document. `"_blank"` also adds the visual
* indicator and accessible new-tab notice.
*
* When both `target` and `openInNewTab` are set, `target` determines the
* browsing context.
*/
target?: ComponentProps< 'a' >[ 'target' ];

/**
* Adds a visual indicator and accessible notice for opening in a new tab.
* Defaults `target` to `"_blank"` when no explicit target is set.
*
* @default false
*/
Expand Down
1 change: 1 addition & 0 deletions storybook/components-manifest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -792,6 +792,7 @@
- openInNewTab
- render
- style
- target
- tone
- variant
- name: MenuGroup
Expand Down
Loading