Skip to content
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

Adds 'opens in new target' text for links for screen readers #4172

Merged
merged 7 commits into from
Oct 29, 2020
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
## [`master`](https://github.com/elastic/eui/tree/master)

- Added more exports for `EuiInMemoryTable` types ([#4179](https://github.com/elastic/eui/pull/4179))
- Added screen reader notification if `EuiLink` opens in a new window ([#4172](https://github.com/elastic/eui/pull/4172))
- Set `external` prop to true if `EuiLink` opens in a new window ([#4172](https://github.com/elastic/eui/pull/4172))

**Bug fixes**

Expand Down
8 changes: 5 additions & 3 deletions src-docs/src/views/link/link.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ export default () => (
<EuiLink href="http://www.elastic.co" target="_blank">
Elastic website
</EuiLink>{' '}
in a new tab.
in a new tab. Setting <EuiCode>target=&ldquo;_blank&rdquo;</EuiCode> also
defaults to <EuiCode>{'external={true}'}</EuiCode>.
</p>
<p>
This{' '}
<EuiLink href="http://www.elastic.co" external target="_blank">
<EuiLink href="http://www.elastic.co" external>
link
</EuiLink>{' '}
has the <EuiCode>external</EuiCode> prop set to true.
Expand All @@ -31,7 +32,8 @@ export default () => (
}}>
link
</EuiLink>{' '}
with both an href and an onClick handler.
with both an <EuiCode>href</EuiCode> and an <EuiCode>onClick</EuiCode>{' '}
handler.
</p>
<p>Links can be colored as well.</p>
<ul>
Expand Down
32 changes: 29 additions & 3 deletions src/components/link/__snapshots__/link.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,21 @@ exports[`EuiLink accent is rendered 1`] = `
/>
`;

exports[`EuiLink allows for target and external to be controlled independently 1`] = `
<a
class="euiLink euiLink--primary"
href="#"
rel="noopener noreferrer"
target="_blank"
>
<span
class="euiScreenReaderOnly"
>
(opens in a new tab or window)
</span>
</a>
`;

exports[`EuiLink button respects the type property 1`] = `
<button
class="euiLink euiLink--primary"
Expand Down Expand Up @@ -127,9 +142,20 @@ exports[`EuiLink supports target 1`] = `
<a
class="euiLink euiLink--primary"
href="#"
rel="noreferrer"
target="_parent"
/>
rel="noopener noreferrer"
target="_blank"
>
<span
aria-label="External link"
class="euiLink__externalIcon"
data-euiicon-type="popout"
/>
<span
class="euiScreenReaderOnly"
>
(opens in a new tab or window)
</span>
</a>
`;

exports[`EuiLink text is rendered 1`] = `
Expand Down
9 changes: 8 additions & 1 deletion src/components/link/link.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,14 @@ describe('EuiLink', () => {
});

test('supports target', () => {
const component = render(<EuiLink href="#" target="_parent" />);
const component = render(<EuiLink href="#" target="_blank" />);
expect(component).toMatchSnapshot();
});

test('allows for target and external to be controlled independently', () => {
const component = render(
<EuiLink href="#" target="_blank" external={false} />
);
expect(component).toMatchSnapshot();
});

Expand Down
40 changes: 26 additions & 14 deletions src/components/link/link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ import React, {
} from 'react';
import classNames from 'classnames';
import { EuiIcon } from '../icon';
import { EuiI18n } from '../i18n';
import { EuiI18n, useEuiI18n } from '../i18n';
import { CommonProps, ExclusiveUnion, keysOf } from '../common';
import { getSecureRelForTarget } from '../../services';
import { EuiScreenReaderOnly } from '../accessibility';

export type EuiLinkType = 'button' | 'reset' | 'submit';
export type EuiLinkColor =
Expand Down Expand Up @@ -68,7 +69,8 @@ export interface LinkAnchorProps {
type?: EuiLinkType;
color?: EuiLinkColor;
/**
* Set to true to show an icon indicating that it is an external link.
* Set to true to show an icon indicating that it is an external link;
* Defaults to true if `target="_blank"`
*/
external?: boolean;
}
Expand Down Expand Up @@ -102,18 +104,25 @@ const EuiLink = forwardRef<HTMLAnchorElement | HTMLButtonElement, EuiLinkProps>(
},
ref
) => {
const externalLinkIcon = external ? (
<EuiI18n token="euiLink.external.ariaLabel" default="External link">
{(ariaLabel: string) => (
<EuiIcon
aria-label={ariaLabel}
size="s"
className="euiLink__externalIcon"
type="popout"
const externalLinkIcon = (
<EuiIcon
aria-label={useEuiI18n('euiLink.external.ariaLabel', 'External link')}
size="s"
className="euiLink__externalIcon"
type="popout"
/>
);

const newTargetScreenreaderText = (
<EuiScreenReaderOnly>
<span>
<EuiI18n
token="euiLink.newTarget.screenReaderOnlyText"
default="(opens in a new tab or window)"
/>
)}
</EuiI18n>
) : undefined;
</span>
</EuiScreenReaderOnly>
);

if (href === undefined) {
const buttonProps = {
Expand Down Expand Up @@ -146,13 +155,16 @@ const EuiLink = forwardRef<HTMLAnchorElement | HTMLButtonElement, EuiLinkProps>(
onClick,
...rest,
};
const showExternalLinkIcon =
(target === '_blank' && external !== false) || external === true;

return (
<a
ref={ref as React.Ref<HTMLAnchorElement>}
{...(anchorProps as EuiLinkAnchorProps)}>
{children}
{externalLinkIcon}
{showExternalLinkIcon && externalLinkIcon}
{target === '_blank' && newTargetScreenreaderText}
</a>
);
}
Expand Down