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

[CSS-in-JS] Convert EuiScreenReaderOnly #5846

Merged
merged 20 commits into from
Jun 23, 2022
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: 0 additions & 1 deletion src/components/accessibility/_index.scss

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

exports[`EuiScreenReaderLive with a static configuration accepts \`aria-live\` 1`] = `
<div
class="euiScreenReaderOnly"
class="emotion-euiScreenReaderOnly"
>
<div
aria-atomic="true"
Expand All @@ -23,7 +23,7 @@ exports[`EuiScreenReaderLive with a static configuration accepts \`aria-live\` 1

exports[`EuiScreenReaderLive with a static configuration accepts \`role\` 1`] = `
<div
class="euiScreenReaderOnly"
class="emotion-euiScreenReaderOnly"
>
<div
aria-atomic="true"
Expand All @@ -44,7 +44,7 @@ exports[`EuiScreenReaderLive with a static configuration accepts \`role\` 1`] =

exports[`EuiScreenReaderLive with a static configuration does not render screen reader content when inactive 1`] = `
<div
class="euiScreenReaderOnly"
class="emotion-euiScreenReaderOnly"
>
<div
aria-atomic="true"
Expand All @@ -61,7 +61,7 @@ exports[`EuiScreenReaderLive with a static configuration does not render screen

exports[`EuiScreenReaderLive with a static configuration renders screen reader content when active 1`] = `
<div
class="euiScreenReaderOnly"
class="emotion-euiScreenReaderOnly"
>
<div
aria-atomic="true"
Expand All @@ -81,73 +81,57 @@ exports[`EuiScreenReaderLive with a static configuration renders screen reader c
`;

exports[`EuiScreenReaderLive with dynamic properties alternates rendering screen reader content into the second live region when changed/toggled 1`] = `
<Component>
<div>
<button
data-test-subj="increment"
onClick={[Function]}
<div>
<button
data-test-subj="increment"
>
Increment
</button>
<div
class="emotion-euiScreenReaderOnly"
>
<div
aria-atomic="true"
aria-live="polite"
role="status"
/>
<div
aria-atomic="true"
aria-live="polite"
role="status"
>
Increment
</button>
<EuiScreenReaderLive>
<EuiScreenReaderOnly>
<div
className="euiScreenReaderOnly"
>
<div
aria-atomic="true"
aria-live="polite"
role="status"
/>
<div
aria-atomic="true"
aria-live="polite"
role="status"
>
<p>
Number of active options:
1
</p>
</div>
</div>
</EuiScreenReaderOnly>
</EuiScreenReaderLive>
<p>
Number of active options: 1
</p>
</div>
</div>
</Component>
</div>
`;

exports[`EuiScreenReaderLive with dynamic properties initially renders screen reader content in the first live region 1`] = `
<Component>
<div>
<button
data-test-subj="increment"
onClick={[Function]}
<div>
<button
data-test-subj="increment"
>
Increment
</button>
<div
class="emotion-euiScreenReaderOnly"
>
<div
aria-atomic="true"
aria-live="polite"
role="status"
/>
<div
aria-atomic="true"
aria-live="polite"
role="status"
>
Increment
</button>
<EuiScreenReaderLive>
<EuiScreenReaderOnly>
<div
className="euiScreenReaderOnly"
>
<div
aria-atomic="true"
aria-live="polite"
role="status"
>
<p>
Number of active options:
0
</p>
</div>
<div
aria-atomic="true"
aria-live="polite"
role="status"
/>
</div>
</EuiScreenReaderOnly>
</EuiScreenReaderLive>
<p>
Number of active options: 0
</p>
</div>
</div>
</Component>
</div>
`;
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ describe('EuiScreenReaderLive', () => {
};

it('initially renders screen reader content in the first live region', () => {
const component = mount(<Component />);
const component = render(<Component />);

expect(component).toMatchSnapshot();
});
Expand All @@ -87,7 +87,7 @@ describe('EuiScreenReaderLive', () => {

findTestSubject(component, 'increment').simulate('click');

expect(component).toMatchSnapshot();
expect(component.render()).toMatchSnapshot();
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@

exports[`EuiScreenReaderOnly adds an accessibility class to a child element and combines other classNames (foo, bar) given as props on the child 1`] = `
<p
class="euiScreenReaderOnly foo bar"
class="foo bar emotion-euiScreenReaderOnly"
>
This paragraph is not visibile to sighted users but will be read by screenreaders.
</p>
`;

exports[`EuiScreenReaderOnly adds an accessibility class to a child element when used with no props 1`] = `
<p
class="euiScreenReaderOnly"
class="emotion-euiScreenReaderOnly"
>
This paragraph is not visibile to sighted users but will be read by screenreaders.
</p>
`;

exports[`EuiScreenReaderOnly will show on focus 1`] = `
<a
class="euiScreenReaderOnly--showOnFocus"
class="emotion-euiScreenReaderOnly"
href="#"
>
Link
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
* Side Public License, v 1.
*/

import { css } from '@emotion/react';

/*
* Mixin that hides elements offscreen to only be read by screen reader
* See https://github.com/elastic/eui/pull/5130 and https://github.com/elastic/eui/pull/5152 for more info
Expand All @@ -28,3 +30,17 @@ export const euiScreenReaderOnly = () => `
// Chrome requires the negative margin to not cause overflows of parent containers
margin: -1px;
`;

/*
* Styles
*/
export const euiScreenReaderOnlyStyles = (showOnFocus?: boolean) => ({
euiScreenReaderOnly: showOnFocus
? css`
// The :active selector is necessary for Safari which removes :focus when a button is pressed
&:not(:focus):not(:active) {
${euiScreenReaderOnly()}
}
`
: css(euiScreenReaderOnly()),
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,39 @@
* Side Public License, v 1.
*/

import { cloneElement, ReactElement, FunctionComponent } from 'react';
import { ReactElement, FunctionComponent } from 'react';
import classNames from 'classnames';

import { cloneElementWithCss } from '../../../services/theme/clone_element';
import { euiScreenReaderOnlyStyles } from './screen_reader_only.styles';

export interface EuiScreenReaderOnlyProps {
/**
* ReactElement to render as this component's content
*/
children: ReactElement<any>;
children: ReactElement;

/**
* For keyboard navigation, force content to display visually upon focus.
*/
showOnFocus?: boolean;
className?: string;
}

export const EuiScreenReaderOnly: FunctionComponent<EuiScreenReaderOnlyProps> = ({
children,
className,
cee-chen marked this conversation as resolved.
Show resolved Hide resolved
showOnFocus,
}) => {
const classes = classNames(
{
euiScreenReaderOnly: !showOnFocus,
'euiScreenReaderOnly--showOnFocus': showOnFocus,
},
children.props.className
);
const classes = classNames(className, children.props.className);

const styles = euiScreenReaderOnlyStyles(showOnFocus);
const cssStyles = [styles.euiScreenReaderOnly];

const props = {
...children.props,
className: classes,
className: classes.length ? classes : undefined,
css: cssStyles,
};

return cloneElement(children, props);
return cloneElementWithCss(children, props);
};
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
exports[`EuiSkipLink is rendered 1`] = `
<a
aria-label="aria-label"
class="euiButton euiButton--primary euiButton--small euiButton--fill euiScreenReaderOnly--showOnFocus euiSkipLink testClass1 testClass2 emotion-euiSkipLink"
class="euiButton euiButton--primary euiButton--small euiButton--fill euiSkipLink testClass1 testClass2 emotion-euiSkipLink-euiScreenReaderOnly"
data-test-subj="test subject string"
href="#somewhere"
rel="noreferrer"
Expand All @@ -22,7 +22,7 @@ exports[`EuiSkipLink is rendered 1`] = `

exports[`EuiSkipLink props onClick is rendered 1`] = `
<a
class="euiButton euiButton--primary euiButton--small euiButton--fill euiScreenReaderOnly--showOnFocus euiSkipLink emotion-euiSkipLink"
class="euiButton euiButton--primary euiButton--small euiButton--fill euiSkipLink emotion-euiSkipLink-euiScreenReaderOnly"
href="#somewhere"
rel="noreferrer"
>
Expand All @@ -38,7 +38,7 @@ exports[`EuiSkipLink props onClick is rendered 1`] = `

exports[`EuiSkipLink props position absolute is rendered 1`] = `
<a
class="euiButton euiButton--primary euiButton--small euiButton--fill euiScreenReaderOnly--showOnFocus euiSkipLink emotion-euiSkipLink-absolute"
class="euiButton euiButton--primary euiButton--small euiButton--fill euiSkipLink emotion-euiSkipLink-absolute-euiScreenReaderOnly"
href="#somewhere"
rel="noreferrer"
>
Expand All @@ -54,7 +54,7 @@ exports[`EuiSkipLink props position absolute is rendered 1`] = `

exports[`EuiSkipLink props position fixed is rendered 1`] = `
<a
class="euiButton euiButton--primary euiButton--small euiButton--fill euiScreenReaderOnly--showOnFocus euiSkipLink emotion-euiSkipLink-fixed"
class="euiButton euiButton--primary euiButton--small euiButton--fill euiSkipLink emotion-euiSkipLink-fixed-euiScreenReaderOnly"
href="#somewhere"
rel="noreferrer"
tabindex="0"
Expand All @@ -71,7 +71,7 @@ exports[`EuiSkipLink props position fixed is rendered 1`] = `

exports[`EuiSkipLink props position static is rendered 1`] = `
<a
class="euiButton euiButton--primary euiButton--small euiButton--fill euiScreenReaderOnly--showOnFocus euiSkipLink emotion-euiSkipLink"
class="euiButton euiButton--primary euiButton--small euiButton--fill euiSkipLink emotion-euiSkipLink-euiScreenReaderOnly"
href="#somewhere"
rel="noreferrer"
>
Expand All @@ -87,7 +87,7 @@ exports[`EuiSkipLink props position static is rendered 1`] = `

exports[`EuiSkipLink props tabIndex is rendered 1`] = `
<a
class="euiButton euiButton--primary euiButton--small euiButton--fill euiScreenReaderOnly--showOnFocus euiSkipLink emotion-euiSkipLink"
class="euiButton euiButton--primary euiButton--small euiButton--fill euiSkipLink emotion-euiSkipLink-euiScreenReaderOnly"
href="#somewhere"
rel="noreferrer"
tabindex="-1"
Expand Down
Loading