Skip to content

Commit

Permalink
[Framework/Accessibility] Add kuiScreenReaderOnly class (#13133)
Browse files Browse the repository at this point in the history
* [Framework/Accessibility] Add kuiScreenReaderOnly class

* fix typo

* use GuideLink

* add screen_reader react component and tests

* export KuiScreenReaderOnly

* --wip-- [skip ci]

* fix lint rule

* remove obsolete snapshots
  • Loading branch information
tsullivan authored Aug 1, 2017
1 parent 9057937 commit f37eab9
Show file tree
Hide file tree
Showing 11 changed files with 151 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

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

exports[`KuiScreenReaderOnly adds an accessibility class to a child element when used with no props 1`] = `
<p
class="kuiScreenReaderOnly"
>
This paragraph is not visibile to sighted users but will be read by screenreaders.
</p>
`;
1 change: 1 addition & 0 deletions ui_framework/components/accessibility/_index.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@import "./_screen_reader";
8 changes: 8 additions & 0 deletions ui_framework/components/accessibility/_screen_reader.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.kuiScreenReaderOnly {
position: absolute;
left: -10000px;
top: auto;
width: 1px;
height: 1px;
overflow: hidden;
}
1 change: 1 addition & 0 deletions ui_framework/components/accessibility/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { KuiKeyboardAccessible } from './keyboard_accessible';
export { KuiScreenReaderOnly } from './screen_reader';
20 changes: 20 additions & 0 deletions ui_framework/components/accessibility/screen_reader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import {
cloneElement,
PropTypes,
} from 'react';
import classNames from 'classnames';


export const KuiScreenReaderOnly = ({ children }) => {
const classes = classNames('kuiScreenReaderOnly', children.props.className);

const props = Object.assign({}, children.props, {
className: classes
});

return cloneElement(children, props);
};

KuiScreenReaderOnly.propTypes = {
children: PropTypes.node
};
35 changes: 35 additions & 0 deletions ui_framework/components/accessibility/screen_reader.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import React from 'react';
import { render } from 'enzyme';

import { KuiScreenReaderOnly } from './screen_reader';

describe('KuiScreenReaderOnly', () => {
describe('adds an accessibility class to a child element', () => {
test('when used with no props', () => {
const $paragraph = render(
<KuiScreenReaderOnly>
<p>
This paragraph is not visibile to sighted users but will be read by
screenreaders.
</p>
</KuiScreenReaderOnly>
);

expect($paragraph)
.toMatchSnapshot();
});
test('and combines other classNames (foo, bar) given as props on the child', () => {
const $paragraph = render(
<KuiScreenReaderOnly>
<p className='foo bar'>
This paragraph is not visibile to sighted users but will be read by
screenreaders.
</p>
</KuiScreenReaderOnly>
);

expect($paragraph)
.toMatchSnapshot();
});
});
});
1 change: 1 addition & 0 deletions ui_framework/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export { KuiActionItem } from './action_item';

export {
KuiKeyboardAccessible,
KuiScreenReaderOnly,
} from './accessibility';

export {
Expand Down
1 change: 1 addition & 0 deletions ui_framework/components/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
@import "common_styles";

// Components
@import "accessibility/index";
@import "action_item/index";
@import "badge/index";
@import "bar/index";
Expand Down
8 changes: 8 additions & 0 deletions ui_framework/dist/ui_framework.css
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ main {
display: block;
/* 1 */ }

.kuiScreenReaderOnly {
position: absolute;
left: -10000px;
top: auto;
width: 1px;
height: 1px;
overflow: hidden; }

.kuiActionItem {
display: -webkit-box;
display: -webkit-flex;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,22 @@ import { renderToHtml } from '../../services';
import {
GuideCode,
GuideDemo,
GuideLink,
GuidePage,
GuideSection,
GuideSectionTypes,
GuideText,
} from '../../components';

import KeyboardAccessible from './keyboard_accessible';
import ScreenReaderOnly from './screen_reader';

const keyboardAccessibleSource = require('!!raw!./keyboard_accessible');
const keyboardAccessibleHtml = renderToHtml(KeyboardAccessible);

const screenReaderOnlyHtml = renderToHtml(ScreenReaderOnly);
const screenReaderOnlySource = require('!!raw!./screen_reader');

export default props => (
<GuidePage title={props.route.name}>
<GuideSection
Expand All @@ -37,5 +43,35 @@ export default props => (
<KeyboardAccessible />
</GuideDemo>
</GuideSection>

<GuideSection
title="ScreenReaderOnly"
source={[{
type: GuideSectionTypes.JS,
code: screenReaderOnlySource,
}, {
type: GuideSectionTypes.HTML,
code: screenReaderOnlyHtml,
}]}
>
<GuideText>
This class can be useful to add accessibility to older designs that are
still in use, but it shouldn't be a permanent solution. See <GuideLink
href='http://webaim.org/techniques/css/invisiblecontent/'
>
http://webaim.org/techniques/css/invisiblecontent/
{
// eslint-disable-next-line react/jsx-closing-tag-location
}</GuideLink> for more information.
</GuideText>

<GuideText>
Use a screenreader to verify that there is a second paragraph in this example:
</GuideText>

<GuideDemo>
<ScreenReaderOnly />
</GuideDemo>
</GuideSection>
</GuidePage>
);
23 changes: 23 additions & 0 deletions ui_framework/doc_site/src/views/accessibility/screen_reader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import React from 'react';

import {
KuiScreenReaderOnly,
} from '../../../../components';


export default () => (
<div>
<p>
This is the first paragraph. It is visible to all.
</p>
<KuiScreenReaderOnly>
<p>
This is the second paragraph. It is hidden for sighted users but visible to screen readers.
</p>
</KuiScreenReaderOnly>
<p>
This is the third paragraph. It is visible to all.
</p>
</div>
);

0 comments on commit f37eab9

Please sign in to comment.