-
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 accessible Toolbar (#18534)
* Add accessible Toolbar * Remove ToolbarButton from Button * Remove ref for now * Pass className to ToolbarGroup on Toolbar * Update Toolbar stories * Remove withInstanceId from Toolbar stories
- Loading branch information
Showing
26 changed files
with
697 additions
and
224 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
26 changes: 26 additions & 0 deletions
26
packages/components/src/toolbar-button/accessible-toolbar-button-container.js
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,26 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { useToolbarItem } from 'reakit/Toolbar'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { Children, cloneElement, useContext } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import ToolbarContext from '../toolbar-context'; | ||
|
||
function AccessibleToolbarButtonContainer( props ) { | ||
const accessibleToolbarState = useContext( ToolbarContext ); | ||
const childButton = Children.only( props.children ); | ||
|
||
// https://reakit.io/docs/composition/#props-hooks | ||
const itemHTMLProps = useToolbarItem( accessibleToolbarState, childButton.props ); | ||
|
||
return <div { ...props }>{ cloneElement( childButton, itemHTMLProps ) }</div>; | ||
} | ||
|
||
export default AccessibleToolbarButtonContainer; |
6 changes: 6 additions & 0 deletions
6
packages/components/src/toolbar-button/accessible-toolbar-button-container.native.js
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,6 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import ToolbarButtonContainer from './toolbar-button-container'; | ||
|
||
export default ToolbarButtonContainer; |
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,8 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { createContext } from '@wordpress/element'; | ||
|
||
const ToolbarContext = createContext(); | ||
|
||
export default ToolbarContext; |
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,112 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import classnames from 'classnames'; | ||
import { flatMap } from 'lodash'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useContext } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import ToolbarButton from '../toolbar-button'; | ||
import ToolbarGroupContainer from './toolbar-group-container'; | ||
import ToolbarGroupCollapsed from './toolbar-group-collapsed'; | ||
import ToolbarContext from '../toolbar-context'; | ||
|
||
/** | ||
* Renders a collapsible group of controls | ||
* | ||
* The `controls` prop accepts an array of sets. A set is an array of controls. | ||
* Controls have the following shape: | ||
* | ||
* ``` | ||
* { | ||
* icon: string, | ||
* title: string, | ||
* subscript: string, | ||
* onClick: Function, | ||
* isActive: boolean, | ||
* isDisabled: boolean | ||
* } | ||
* ``` | ||
* | ||
* For convenience it is also possible to pass only an array of controls. It is | ||
* then assumed this is the only set. | ||
* | ||
* Either `controls` or `children` is required, otherwise this components | ||
* renders nothing. | ||
* | ||
* @param {Object} props Component props. | ||
* @param {Array} [props.controls] The controls to render in this toolbar. | ||
* @param {WPElement} [props.children] Any other things to render inside the toolbar besides the controls. | ||
* @param {string} [props.className] Class to set on the container div. | ||
* @param {boolean} [props.isCollapsed] Turns ToolbarGroup into a dropdown menu. | ||
* @param {WPBlockTypeIconRender} [props.icon] The [Dashicon](https://developer.wordpress.org/resource/dashicons/) icon slug string, or an SVG WP element. | ||
* @param {string} [props.label] The menu item text. | ||
*/ | ||
function ToolbarGroup( { | ||
controls = [], | ||
children, | ||
className, | ||
isCollapsed, | ||
icon, | ||
title, | ||
...otherProps | ||
} ) { | ||
// It'll contain state if `ToolbarGroup` is being used within | ||
// `<Toolbar accessibilityLabel="label" />` | ||
const accessibleToolbarState = useContext( ToolbarContext ); | ||
|
||
if ( ( ! controls || ! controls.length ) && ! children ) { | ||
return null; | ||
} | ||
|
||
const finalClassName = classnames( | ||
// Unfortunately, there's legacy code referencing to `.components-toolbar` | ||
// So we can't get rid of it | ||
accessibleToolbarState ? 'components-toolbar-group' : 'components-toolbar', | ||
className | ||
); | ||
|
||
// Normalize controls to nested array of objects (sets of controls) | ||
let controlSets = controls; | ||
if ( ! Array.isArray( controlSets[ 0 ] ) ) { | ||
controlSets = [ controlSets ]; | ||
} | ||
|
||
if ( isCollapsed ) { | ||
return ( | ||
<ToolbarGroupCollapsed | ||
icon={ icon } | ||
label={ title } | ||
controls={ controlSets } | ||
className={ finalClassName } | ||
children={ children } | ||
{ ...otherProps } | ||
/> | ||
); | ||
} | ||
|
||
return ( | ||
<ToolbarGroupContainer className={ finalClassName } { ...otherProps }> | ||
{ flatMap( controlSets, ( controlSet, indexOfSet ) => | ||
controlSet.map( ( control, indexOfControl ) => ( | ||
<ToolbarButton | ||
key={ [ indexOfSet, indexOfControl ].join() } | ||
containerClassName={ | ||
indexOfSet > 0 && indexOfControl === 0 ? 'has-left-divider' : null | ||
} | ||
{ ...control } | ||
/> | ||
) ) | ||
) } | ||
{ children } | ||
</ToolbarGroupContainer> | ||
); | ||
} | ||
|
||
export default ToolbarGroup; |
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,28 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import { ToolbarButton, ToolbarGroup } from '../../'; | ||
|
||
export default { title: 'Components|ToolbarGroup', component: ToolbarGroup }; | ||
|
||
export const _default = () => { | ||
return ( | ||
<ToolbarGroup> | ||
<ToolbarButton icon="editor-bold" title="Bold" isActive /> | ||
<ToolbarButton icon="editor-italic" title="Italic" /> | ||
<ToolbarButton icon="admin-links" title="Link" /> | ||
</ToolbarGroup> | ||
); | ||
}; | ||
|
||
export const withControlsProp = () => { | ||
return ( | ||
<ToolbarGroup | ||
controls={ [ | ||
{ icon: 'editor-bold', title: 'Bold', isActive: true }, | ||
{ icon: 'editor-italic', title: 'Italic' }, | ||
{ icon: 'admin-links', title: 'Link' }, | ||
] } | ||
/> | ||
); | ||
}; |
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,11 @@ | ||
.container { | ||
flex-direction: row; | ||
border-left-width: 1px; | ||
border-color: #e9eff3; | ||
padding-left: 5px; | ||
padding-right: 5px; | ||
} | ||
|
||
.containerDark { | ||
border-color: #525354; | ||
} |
Oops, something went wrong.