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 3 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
24 changes: 22 additions & 2 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,14 @@ 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"
>
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
45 changes: 22 additions & 23 deletions src/components/link/link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ 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';
Expand Down Expand Up @@ -69,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 @@ -103,29 +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 newTargetScreenreaderText = target ? (
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)"
/>
{useEuiI18n(
'euiLink.newTarget.screenReaderOnlyText',
'(opens in a new tab or window)'
)}
myasonik marked this conversation as resolved.
Show resolved Hide resolved
</span>
</EuiScreenReaderOnly>
) : undefined;
);

if (href === undefined) {
const buttonProps = {
Expand Down Expand Up @@ -158,14 +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}
{newTargetScreenreaderText}
{showExternalLinkIcon && externalLinkIcon}
{target === '_blank' && newTargetScreenreaderText}
</a>
);
}
Expand Down