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 4 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 CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## [`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 for `EuiLink` when it opens in a new tab ([#3659](https://github.com/elastic/eui/pull/3659))
myasonik marked this conversation as resolved.
Show resolved Hide resolved

**Bug fixes**

Expand Down
10 changes: 6 additions & 4 deletions src-docs/src/views/link/link.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ 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>.
myasonik marked this conversation as resolved.
Show resolved Hide resolved
</p>
<p>
This{' '}
<EuiLink href="http://www.elastic.co" external target="_blank">
<EuiLink href="http://www.elastic.co" external={false} target="_blank">
myasonik marked this conversation as resolved.
Show resolved Hide resolved
link
</EuiLink>{' '}
has the <EuiCode>external</EuiCode> prop set to true.
has the <EuiCode>external</EuiCode> prop set to false.
myasonik marked this conversation as resolved.
Show resolved Hide resolved
</p>
<p>
This link is actually a <EuiLink onClick={() => {}}>button</EuiLink> with
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
42 changes: 27 additions & 15 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 { 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".
myasonik marked this conversation as resolved.
Show resolved Hide resolved
*/
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"
/>
)}
</EuiI18n>
) : undefined;
const externalLinkIcon = (
<EuiIcon
aria-label={useEuiI18n('euiLink.external.ariaLabel', 'External link')}
size="s"
className="euiLink__externalIcon"
type="popout"
/>
);

const newTargetScreenreaderText = (
<EuiScreenReaderOnly>
<span>
{useEuiI18n(
'euiLink.newTarget.screenReaderOnlyText',
'(opens in a new tab or window)'
)}
myasonik marked this conversation as resolved.
Show resolved Hide resolved
</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