Skip to content

Commit 4a6a12b

Browse files
authored
[system] Introduce visuallyHidden style utility (mui#21413)
1 parent f1246e8 commit 4a6a12b

File tree

19 files changed

+135
-64
lines changed

19 files changed

+135
-64
lines changed

docs/pages/api-docs/rating.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ Any other props supplied will be provided to the root element (native element).
5959
| <span class="prop-name">readOnly</span> | <span class="prop-name">.MuiRating-readOnly</span> | Styles applied to the root element if `readOnly={true}`.
6060
| <span class="prop-name">disabled</span> | <span class="prop-name">.Mui-disabled</span> | Pseudo-class applied to the root element if `disabled={true}`.
6161
| <span class="prop-name">focusVisible</span> | <span class="prop-name">.Mui-focusVisible</span> | Pseudo-class applied to the root element if keyboard focused.
62-
| <span class="prop-name">visuallyhidden</span> | <span class="prop-name">.MuiRating-visuallyhidden</span> | Visually hide an element.
62+
| <span class="prop-name">visuallyHidden</span> | <span class="prop-name">.MuiRating-visuallyHidden</span> | Visually hide an element.
6363
| <span class="prop-name">pristine</span> | <span class="prop-name">.MuiRating-pristine</span> | Styles applied to the pristine label.
6464
| <span class="prop-name">label</span> | <span class="prop-name">.MuiRating-label</span> | Styles applied to the label elements.
6565
| <span class="prop-name">icon</span> | <span class="prop-name">.MuiRating-icon</span> | Styles applied to the icon wrapping elements.

docs/pages/api-docs/typography.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ The `MuiTypography` name can be used for providing [default props](/customizatio
3737
| <span class="prop-name">gutterBottom</span> | <span class="prop-type">bool</span> | <span class="prop-default">false</span> | If `true`, the text will have a bottom margin. |
3838
| <span class="prop-name">noWrap</span> | <span class="prop-type">bool</span> | <span class="prop-default">false</span> | If `true`, the text will not wrap, but instead will truncate with a text overflow ellipsis.<br>Note that text overflow can only happen with block or inline-block level elements (the element needs to have a width in order to overflow). |
3939
| <span class="prop-name">paragraph</span> | <span class="prop-type">bool</span> | <span class="prop-default">false</span> | If `true`, the text will have a bottom margin. |
40-
| <span class="prop-name">variant</span> | <span class="prop-type">'h1'<br>&#124;&nbsp;'h2'<br>&#124;&nbsp;'h3'<br>&#124;&nbsp;'h4'<br>&#124;&nbsp;'h5'<br>&#124;&nbsp;'h6'<br>&#124;&nbsp;'subtitle1'<br>&#124;&nbsp;'subtitle2'<br>&#124;&nbsp;'body1'<br>&#124;&nbsp;'body2'<br>&#124;&nbsp;'caption'<br>&#124;&nbsp;'button'<br>&#124;&nbsp;'overline'<br>&#124;&nbsp;'srOnly'<br>&#124;&nbsp;'inherit'</span> | <span class="prop-default">'body1'</span> | Applies the theme typography styles. |
40+
| <span class="prop-name">variant</span> | <span class="prop-type">'h1'<br>&#124;&nbsp;'h2'<br>&#124;&nbsp;'h3'<br>&#124;&nbsp;'h4'<br>&#124;&nbsp;'h5'<br>&#124;&nbsp;'h6'<br>&#124;&nbsp;'subtitle1'<br>&#124;&nbsp;'subtitle2'<br>&#124;&nbsp;'body1'<br>&#124;&nbsp;'body2'<br>&#124;&nbsp;'caption'<br>&#124;&nbsp;'button'<br>&#124;&nbsp;'overline'<br>&#124;&nbsp;'inherit'</span> | <span class="prop-default">'body1'</span> | Applies the theme typography styles. |
4141
| <span class="prop-name">variantMapping</span> | <span class="prop-type">object</span> | <span class="prop-default">{ h1: 'h1', h2: 'h2', h3: 'h3', h4: 'h4', h5: 'h5', h6: 'h6', subtitle1: 'h6', subtitle2: 'h6', body1: 'p', body2: 'p',}</span> | The component maps the variant prop to a range of different HTML element types. For instance, subtitle1 to `<h6>`. If you wish to change that mapping, you can provide your own. Alternatively, you can use the `component` prop. |
4242

4343
The `ref` is forwarded to the root element.
@@ -62,7 +62,6 @@ Any other props supplied will be provided to the root element (native element).
6262
| <span class="prop-name">subtitle1</span> | <span class="prop-name">.MuiTypography-subtitle1</span> | Styles applied to the root element if `variant="subtitle1"`.
6363
| <span class="prop-name">subtitle2</span> | <span class="prop-name">.MuiTypography-subtitle2</span> | Styles applied to the root element if `variant="subtitle2"`.
6464
| <span class="prop-name">overline</span> | <span class="prop-name">.MuiTypography-overline</span> | Styles applied to the root element if `variant="overline"`.
65-
| <span class="prop-name">srOnly</span> | <span class="prop-name">.MuiTypography-srOnly</span> | Styles applied to the root element if `variant="srOnly"`. Only accessible to screen readers.
6665
| <span class="prop-name">alignLeft</span> | <span class="prop-name">.MuiTypography-alignLeft</span> | Styles applied to the root element if `align="left"`.
6766
| <span class="prop-name">alignCenter</span> | <span class="prop-name">.MuiTypography-alignCenter</span> | Styles applied to the root element if `align="center"`.
6867
| <span class="prop-name">alignRight</span> | <span class="prop-name">.MuiTypography-alignRight</span> | Styles applied to the root element if `align="right"`.

docs/pages/system/screen-readers.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import React from 'react';
2+
import MarkdownDocs from 'docs/src/modules/components/MarkdownDocs';
3+
import { prepareMarkdown } from 'docs/src/modules/utils/parseMarkdown';
4+
5+
const pageFilename = 'system/screen-readers';
6+
const requireDemo = require.context('docs/src/pages/system/screen-readers', false, /\.(js|tsx)$/);
7+
const requireRaw = require.context(
8+
'!raw-loader!../../src/pages/system/screen-readers',
9+
false,
10+
/\.(js|md|tsx)$/,
11+
);
12+
13+
export default function Page({ demos, docs }) {
14+
return <MarkdownDocs demos={demos} docs={docs} requireDemo={requireDemo} />;
15+
}
16+
17+
Page.getInitialProps = () => {
18+
const { demos, docs } = prepareMarkdown({ pageFilename, requireRaw });
19+
return { demos, docs };
20+
};

docs/src/pages.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ const pages = [
150150
{ pathname: '/system/shadows' },
151151
{ pathname: '/system/sizing' },
152152
{ pathname: '/system/spacing' },
153+
{ pathname: '/system/screen-readers' },
153154
{ pathname: '/system/typography' },
154155
{ pathname: '/system/api', title: 'API' },
155156
],

docs/src/pages/components/icons/icons.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,12 +236,15 @@ If your icons have semantic meaning, you need to provide a text alternative only
236236

237237
```jsx
238238
import Icon from '@material-ui/core/Icon';
239-
import Typography from '@material-ui/core/Typography';
239+
import { visuallyHidden } from '@material-ui/system';
240+
import { makeStyles } from '@material-ui/core/styles';
241+
242+
const classes = makeStyles({ visuallyHidden })();
240243

241244
// ...
242245

243246
<Icon>add_circle</Icon>
244-
<Typography variant="srOnly">Create a user</Typography>
247+
<span className={classes.visuallyHidden}>Create a user</span>
245248
```
246249

247250
### Reference

docs/src/pages/components/tables/EnhancedTable.js

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import FormControlLabel from '@material-ui/core/FormControlLabel';
2020
import Switch from '@material-ui/core/Switch';
2121
import DeleteIcon from '@material-ui/icons/Delete';
2222
import FilterListIcon from '@material-ui/icons/FilterList';
23+
import { visuallyHidden } from '@material-ui/system';
2324

2425
function createData(name, calories, fat, carbs, protein) {
2526
return { name, calories, fat, carbs, protein };
@@ -106,7 +107,7 @@ function EnhancedTableHead(props) {
106107
>
107108
{headCell.label}
108109
{orderBy === headCell.id ? (
109-
<span className={classes.visuallyHidden}>
110+
<span className={classes.sortSpan}>
110111
{order === 'desc' ? 'sorted descending' : 'sorted ascending'}
111112
</span>
112113
) : null}
@@ -200,17 +201,8 @@ const useStyles = makeStyles((theme) => ({
200201
table: {
201202
minWidth: 750,
202203
},
203-
visuallyHidden: {
204-
border: 0,
205-
clip: 'rect(0 0 0 0)',
206-
height: 1,
207-
margin: -1,
208-
overflow: 'hidden',
209-
padding: 0,
210-
position: 'absolute',
211-
top: 20,
212-
width: 1,
213-
},
204+
// TODO fix #20379.
205+
sortSpan: visuallyHidden,
214206
}));
215207

216208
export default function EnhancedTable() {

docs/src/pages/components/tables/EnhancedTable.tsx

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import FormControlLabel from '@material-ui/core/FormControlLabel';
1919
import Switch from '@material-ui/core/Switch';
2020
import DeleteIcon from '@material-ui/icons/Delete';
2121
import FilterListIcon from '@material-ui/icons/FilterList';
22+
import { visuallyHidden } from '@material-ui/system';
23+
import { CSSProperties } from '@material-ui/styles';
2224

2325
interface Data {
2426
calories: number;
@@ -141,7 +143,7 @@ function EnhancedTableHead(props: EnhancedTableProps) {
141143
>
142144
{headCell.label}
143145
{orderBy === headCell.id ? (
144-
<span className={classes.visuallyHidden}>
146+
<span className={classes.sortSpan}>
145147
{order === 'desc' ? 'sorted descending' : 'sorted ascending'}
146148
</span>
147149
) : null}
@@ -227,17 +229,8 @@ const useStyles = makeStyles((theme: Theme) =>
227229
table: {
228230
minWidth: 750,
229231
},
230-
visuallyHidden: {
231-
border: 0,
232-
clip: 'rect(0 0 0 0)',
233-
height: 1,
234-
margin: -1,
235-
overflow: 'hidden',
236-
padding: 0,
237-
position: 'absolute',
238-
top: 20,
239-
width: 1,
240-
},
232+
// TODO fix #20379.
233+
sortSpan: visuallyHidden as CSSProperties,
241234
}),
242235
);
243236

docs/src/pages/guides/migration-v4/migration-v4.md

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# Migration from v3 to v4
1+
# Migration from v4 to v5
22

3-
<p class="description">Yeah, v4 has been released!</p>
3+
<p class="description">Yeah, v5 has been released!</p>
44

55
Looking for the v4 docs? [Find them here](https://material-ui.com/versions/).
66

@@ -54,4 +54,19 @@ yarn add @material-ui/core
5454
- background-color: #f00;
5555
+ border-color: #f00;
5656
}
57-
```
57+
```
58+
59+
### Typography
60+
61+
- [Typography] Replace `srOnly` prop support with a style util:
62+
63+
```diff
64+
-import Typography from '@material-ui/core/Typography';
65+
+import { visuallyHidden } from '@material-ui/system';
66+
+import styled from 'styled-component';
67+
68+
+const Span = styled('span')(visuallyHidden);
69+
70+
-<Typography variant="srOnly">Create a user</Typography>
71+
+<Span>Create a user</Span>
72+
```
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import React from 'react';
2+
import Link from '@material-ui/core/Link';
3+
import { visuallyHidden } from '@material-ui/system';
4+
import { makeStyles } from '@material-ui/core/styles';
5+
6+
const useStyles = makeStyles({
7+
// TODO fix #20379.
8+
span: visuallyHidden,
9+
});
10+
11+
export default function VisuallyHiddenUsage() {
12+
const classes = useStyles();
13+
14+
return (
15+
<Link href="#foo">
16+
Read more
17+
{/* always visually hidden because the parent is focusable element */}
18+
<span className={classes.span}>about how to visually hide elements</span>
19+
</Link>
20+
);
21+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import React from 'react';
2+
import Link from '@material-ui/core/Link';
3+
import { visuallyHidden } from '@material-ui/system';
4+
import { makeStyles } from '@material-ui/core/styles';
5+
import { CSSProperties } from '@material-ui/styles';
6+
7+
const useStyles = makeStyles({
8+
// TODO fix #20379.
9+
span: visuallyHidden as CSSProperties,
10+
});
11+
12+
export default function VisuallyHiddenUsage() {
13+
const classes = useStyles();
14+
15+
return (
16+
<Link href="#foo">
17+
Read more
18+
{/* always visually hidden because the parent is focusable element */}
19+
<span className={classes.span}>about how to visually hide elements</span>
20+
</Link>
21+
);
22+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Screen readers
2+
3+
<p class="description">Collection of utilities for improving accessibility with screen readers.</p>
4+
5+
## Visually hidden elements
6+
7+
The visually hidden style utility provides a common mechanism for hidings elements visually, but making them available for the assistive technology.
8+
9+
### Usage
10+
11+
The visually hidden style utility can help provide context to read more links.
12+
13+
{{"demo": "pages/system/screen-readers/VisuallyHiddenUsage.js"}}

framer/Material-UI.framerfx/code/Typography.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ interface Props {
2929
| 'caption'
3030
| 'button'
3131
| 'overline'
32-
| 'srOnly'
3332
| 'inherit';
3433
label?: string;
3534
width?: number;
@@ -93,7 +92,6 @@ addPropertyControls(Typography, {
9392
'caption',
9493
'button',
9594
'overline',
96-
'srOnly',
9795
'inherit',
9896
],
9997
},

packages/material-ui-lab/src/Rating/Rating.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ export type RatingClassKey =
9393
| 'readOnly'
9494
| 'disabled'
9595
| 'focusVisible'
96-
| 'visuallyhidden'
96+
| 'visuallyHidden'
9797
| 'pristine'
9898
| 'label'
9999
| 'icon'

packages/material-ui-lab/src/Rating/Rating.js

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
useControlled,
1111
unstable_useId as useId,
1212
} from '@material-ui/core/utils';
13+
import { visuallyHidden } from '@material-ui/system';
1314
import Star from '../internal/svg-icons/Star';
1415

1516
function clamp(value, min, max) {
@@ -71,18 +72,7 @@ export const styles = (theme) => ({
7172
/* Pseudo-class applied to the root element if keyboard focused. */
7273
focusVisible: {},
7374
/* Visually hide an element. */
74-
visuallyhidden: {
75-
border: 0,
76-
clip: 'rect(0 0 0 0)',
77-
height: 1,
78-
margin: -1,
79-
color: '#000',
80-
overflow: 'hidden',
81-
padding: 0,
82-
position: 'absolute',
83-
top: 20,
84-
width: 1,
85-
},
75+
visuallyHidden,
8676
/* Styles applied to the pristine label. */
8777
pristine: {
8878
'input:focus + &': {
@@ -342,7 +332,7 @@ const Rating = React.forwardRef(function Rating(props, ref) {
342332
<React.Fragment key={state.value}>
343333
<label className={classes.label} htmlFor={id} {...labelProps}>
344334
{container}
345-
<span className={classes.visuallyhidden}>{getLabelText(state.value)}</span>
335+
<span className={classes.visuallyHidden}>{getLabelText(state.value)}</span>
346336
</label>
347337
<input
348338
onFocus={handleFocus}
@@ -355,7 +345,7 @@ const Rating = React.forwardRef(function Rating(props, ref) {
355345
type="radio"
356346
name={name}
357347
checked={state.checked}
358-
className={classes.visuallyhidden}
348+
className={classes.visuallyHidden}
359349
/>
360350
</React.Fragment>
361351
);
@@ -444,10 +434,10 @@ const Rating = React.forwardRef(function Rating(props, ref) {
444434
type="radio"
445435
name={name}
446436
defaultChecked
447-
className={classes.visuallyhidden}
437+
className={classes.visuallyHidden}
448438
/>
449439
<label className={classes.pristine} htmlFor={`${name}-empty`}>
450-
<span className={classes.visuallyhidden}>{emptyLabelText}</span>
440+
<span className={classes.visuallyHidden}>{emptyLabelText}</span>
451441
</label>
452442
</React.Fragment>
453443
)}

packages/material-ui-system/src/index.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import * as CSS from 'csstype';
2-
32
// disable automatic export
43
export {};
54

@@ -202,5 +201,7 @@ export const typography: SimpleStyleFunction<
202201
>;
203202
export type TypographyProps = PropsFor<typeof typography>;
204203

204+
export const visuallyHidden: CSS.Properties;
205+
205206
// utils
206207
type Omit<T, K> = Pick<T, Exclude<keyof T, K>>;

packages/material-ui-system/src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ export * from './spacing';
2020
export { default as style } from './style';
2121
export { default as typography } from './typography';
2222
export * from './typography';
23+
export { default as visuallyHidden } from './visuallyHidden';
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const visuallyHidden = {
2+
border: 0,
3+
clip: 'rect(0 0 0 0)',
4+
height: 1,
5+
margin: -1,
6+
overflow: 'hidden',
7+
padding: 0,
8+
position: 'absolute',
9+
whiteSpace: 'nowrap',
10+
width: 1,
11+
};
12+
13+
export default visuallyHidden;

packages/material-ui/src/Typography/Typography.d.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import * as React from 'react';
22
import { StandardProps, PropTypes } from '..';
33
import { OverrideProps, OverridableTypeMap, OverridableComponent } from '../OverridableComponent';
4-
import { Variant as ThemeVariant } from '../styles/createTypography';
5-
6-
type Variant = ThemeVariant | 'srOnly';
4+
import { Variant } from '../styles/createTypography';
75

86
export interface TypographyTypeMap<P = {}, D extends React.ElementType = 'span'> {
97
props: P & {
@@ -64,7 +62,6 @@ export type TypographyClassKey =
6462
| 'caption'
6563
| 'button'
6664
| 'overline'
67-
| 'srOnly'
6865
| 'alignLeft'
6966
| 'alignCenter'
7067
| 'alignRight'

packages/material-ui/src/Typography/Typography.js

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,6 @@ export const styles = (theme) => ({
3535
subtitle2: theme.typography.subtitle2,
3636
/* Styles applied to the root element if `variant="overline"`. */
3737
overline: theme.typography.overline,
38-
/* Styles applied to the root element if `variant="srOnly"`. Only accessible to screen readers. */
39-
srOnly: {
40-
position: 'absolute',
41-
height: 1,
42-
width: 1,
43-
overflow: 'hidden',
44-
},
4538
/* Styles applied to the root element if `align="left"`. */
4639
alignLeft: {
4740
textAlign: 'left',
@@ -233,7 +226,6 @@ Typography.propTypes = {
233226
'caption',
234227
'button',
235228
'overline',
236-
'srOnly',
237229
'inherit',
238230
]),
239231
/**

0 commit comments

Comments
 (0)