-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Components: Add VisuallyHidden component (#18022)
* Add ScreenReaderText component * Add new component readme to manifest * Remove CSS style loading within stories * Switch component name to VisuallyHidden - Rename directory and includes - Update README usage - Update Storybook usage * Switch classname to components-visually-hidden * Lint: newline * Add focus style * Switch to 'as' for specifying tag * Move renderAsRenderProps to utils.js * Move utils to inside component folder Waiting to refine the utils usage a little better before making it look available for other components to use. * Apply suggestions from code review Co-Authored-By: Grzegorz (Greg) Ziółkowski <grzegorz@gziolo.pl> * Lint: Move newline * Fix variable name * Use variable for stylesheet
- Loading branch information
Showing
8 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# VisuallyHidden | ||
|
||
A component used to render text intended to be visually hidden, but will show for alternate devices, for example a screen reader. | ||
|
||
### Usage | ||
|
||
```jsx | ||
<VisuallyHidden> Show text for screenreader. </VisuallyHidden> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { renderAsRenderProps } from './utils'; | ||
|
||
/** | ||
* VisuallyHidden component to render text out non-visually | ||
* for use in devices such as a screen reader. | ||
*/ | ||
function VisuallyHidden( { | ||
as = 'div', | ||
...props | ||
} ) { | ||
return renderAsRenderProps( { | ||
as, | ||
className: 'components-visually-hidden', | ||
...props, | ||
} ); | ||
} | ||
export default VisuallyHidden; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import VisuallyHidden from '../'; | ||
|
||
export default { title: 'VisuallyHidden', component: VisuallyHidden }; | ||
|
||
export const _default = () => ( | ||
<> | ||
<VisuallyHidden> | ||
This should not show. | ||
</VisuallyHidden> | ||
<div> | ||
This text will <VisuallyHidden as="span">but not inline </VisuallyHidden> always show. | ||
</div> | ||
</> | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
.components-visually-hidden { | ||
border: 0; | ||
clip: rect(1px, 1px, 1px, 1px); | ||
-webkit-clip-path: inset(50%); | ||
clip-path: inset(50%); | ||
height: 1px; | ||
margin: -1px; | ||
overflow: hidden; | ||
padding: 0; | ||
position: absolute; | ||
width: 1px; | ||
word-wrap: normal !important; | ||
} | ||
|
||
.components-visually-hidden:focus { | ||
background-color: $light-gray-500; | ||
clip: auto !important; | ||
clip-path: none; | ||
color: #444; | ||
display: block; | ||
font-size: 1em; | ||
height: auto; | ||
left: 5px; | ||
line-height: normal; | ||
padding: 15px 23px 14px; | ||
text-decoration: none; | ||
top: 5px; | ||
width: auto; | ||
z-index: 100000; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/** | ||
* Utility Functions | ||
*/ | ||
|
||
/** | ||
* renderAsRenderProps is used to wrap a component and convert | ||
* the passed property "as" either a string or component, to the | ||
* rendered tag if a string, or component. | ||
* | ||
* See VisuallyHidden hidden for example. | ||
* | ||
* @param {string|Component} as A tag or component to render. | ||
* @return {Component} The rendered component. | ||
*/ | ||
function renderAsRenderProps( { as: Component = 'div', ...props } ) { | ||
if ( typeof props.children === 'function' ) { | ||
return props.children( props ); | ||
} | ||
return <Component { ...props } />; | ||
} | ||
|
||
export { renderAsRenderProps }; |