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

Add summary prop to PanelBody, and use it to create more accessible pre-publish panel sections. #25170

Closed
wants to merge 5 commits into from
Closed
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
2 changes: 1 addition & 1 deletion packages/block-library/src/navigation/editor.scss
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ $color-control-label-height: 20px;
margin-top: 2px;
}

.components-panel__body-title {
.components-panel__body-header {
display: none;
}
}
Expand Down
79 changes: 50 additions & 29 deletions packages/components/src/panel/body.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import mergeRefs from 'react-merge-refs';
/**
* WordPress dependencies
*/
import { useReducedMotion } from '@wordpress/compose';
import { useInstanceId, useReducedMotion } from '@wordpress/compose';
import { forwardRef, useRef } from '@wordpress/element';
import { chevronUp, chevronDown } from '@wordpress/icons';

Expand All @@ -20,7 +20,16 @@ import Icon from '../icon';
import { useControlledState, useUpdateEffect } from '../utils';

export function PanelBody(
{ children, className, icon, initialOpen, onToggle = noop, opened, title },
{
children,
className,
icon,
initialOpen,
onToggle = noop,
opened,
summary,
title,
},
ref
) {
const [ isOpened, setIsOpened ] = useControlledState( opened, {
Expand Down Expand Up @@ -63,49 +72,61 @@ export function PanelBody(

return (
<div className={ classes } ref={ mergeRefs( [ nodeRef, ref ] ) }>
<PanelBodyTitle
<PanelBodyHeader
icon={ icon }
isOpened={ isOpened }
onClick={ handleOnToggle }
summary={ summary }
title={ title }
/>
{ isOpened && children }
</div>
);
}

const PanelBodyTitle = forwardRef(
( { isOpened, icon, title, ...props }, ref ) => {
const PanelBodyHeader = forwardRef(
( { isOpened, icon, summary, title, ...props }, ref ) => {
const summaryClassName = 'components-panel__body-summary';
const summaryId = useInstanceId( PanelBodyHeader, summaryClassName );

if ( ! title ) return null;

return (
<h2 className="components-panel__body-title">
<Button
className="components-panel__body-toggle"
aria-expanded={ isOpened }
ref={ ref }
{ ...props }
>
{ /*
<div className="components-panel__body-header">
<h2 className="components-panel__body-title">
<Button
className="components-panel__body-toggle"
aria-expanded={ isOpened }
ref={ ref }
{ ...props }
aria-describedby={ summary ? summaryId : undefined }
>
{ /*
Firefox + NVDA don't announce aria-expanded because the browser
repaints the whole element, so this wrapping span hides that.
*/ }
<span aria-hidden="true">
<Icon
className="components-panel__arrow"
icon={ isOpened ? chevronUp : chevronDown }
/>
</span>
{ title }
{ icon && (
<Icon
icon={ icon }
className="components-panel__icon"
size={ 20 }
/>
) }
</Button>
</h2>
<span aria-hidden="true">
<Icon
className="components-panel__arrow"
icon={ isOpened ? chevronUp : chevronDown }
/>
</span>
{ title }
{ icon && (
<Icon
icon={ icon }
className="components-panel__icon"
size={ 20 }
/>
) }
</Button>
</h2>
{ summary && (
<div className={ summaryClassName } id={ summaryId }>
{ summary }
</div>
) }
</div>
);
}
);
Expand Down
22 changes: 14 additions & 8 deletions packages/components/src/panel/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -53,27 +53,33 @@
margin-top: -1px;
}

.components-panel__body > .components-panel__body-title {
.components-panel__body.is-opened > .components-panel__body-header {
margin: -1 * $grid-unit-20;
margin-bottom: 5px;
}

.components-panel__body-header > .components-panel__body-title {
display: block;
padding: 0;
transition: 0.1s background ease-in-out;
@include reduce-motion("transition");
font-size: inherit;
margin-top: 0;
margin-bottom: 0;
transition: 0.1s background ease-in-out;
@include reduce-motion("transition");
}
.components-panel__body.is-opened > .components-panel__body-title {
margin: -1 * $grid-unit-20;
margin-bottom: 5px;
}

// Hover States
.components-panel__body > .components-panel__body-title:hover {
.components-panel__body-header > .components-panel__body-title:hover {
// Override the default button hover style
background: $gray-100;
border: none;
}

.components-panel__body-summary {
padding: $grid-unit-20;
padding-top: 0;
}

.components-panel__body-toggle.components-button {
position: relative;
padding: $grid-unit-20;
Expand Down
4 changes: 3 additions & 1 deletion packages/components/src/panel/test/body.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import { PanelBody } from '../body';
const getPanelBody = ( container ) =>
container.querySelector( '.components-panel__body' );
const getPanelBodyContent = ( container ) =>
container.querySelector( '.components-panel__body > div' );
container.querySelector(
'.components-panel__body > div:not(.components-panel__body-header)'
);
const getPanelToggle = ( container ) =>
container.querySelector( '.components-panel__body-toggle' );

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`PluginPostPublishPanel renders fill properly 1`] = `"<div class=\\"components-panel__body my-plugin-post-publish-panel is-opened\\"><h2 class=\\"components-panel__body-title\\"><button type=\\"button\\" aria-expanded=\\"true\\" class=\\"components-button components-panel__body-toggle\\"><span aria-hidden=\\"true\\"><svg width=\\"24\\" height=\\"24\\" viewBox=\\"0 0 24 24\\" xmlns=\\"http://www.w3.org/2000/svg\\" class=\\"components-panel__arrow\\" role=\\"img\\" aria-hidden=\\"true\\" focusable=\\"false\\"><path d=\\"M6.5 12.4L12 8l5.5 4.4-.9 1.2L12 10l-4.5 3.6-1-1.2z\\"></path></svg></span>My panel title</button></h2>My panel content</div>"`;
exports[`PluginPostPublishPanel renders fill properly 1`] = `"<div class=\\"components-panel__body my-plugin-post-publish-panel is-opened\\"><div class=\\"components-panel__body-header\\"><h2 class=\\"components-panel__body-title\\"><button type=\\"button\\" aria-expanded=\\"true\\" class=\\"components-button components-panel__body-toggle\\"><span aria-hidden=\\"true\\"><svg width=\\"24\\" height=\\"24\\" viewBox=\\"0 0 24 24\\" xmlns=\\"http://www.w3.org/2000/svg\\" class=\\"components-panel__arrow\\" role=\\"img\\" aria-hidden=\\"true\\" focusable=\\"false\\"><path d=\\"M6.5 12.4L12 8l5.5 4.4-.9 1.2L12 10l-4.5 3.6-1-1.2z\\"></path></svg></span>My panel title</button></h2></div>My panel content</div>"`;
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`PluginPrePublishPanel renders fill properly 1`] = `"<div class=\\"components-panel__body my-plugin-pre-publish-panel is-opened\\"><h2 class=\\"components-panel__body-title\\"><button type=\\"button\\" aria-expanded=\\"true\\" class=\\"components-button components-panel__body-toggle\\"><span aria-hidden=\\"true\\"><svg width=\\"24\\" height=\\"24\\" viewBox=\\"0 0 24 24\\" xmlns=\\"http://www.w3.org/2000/svg\\" class=\\"components-panel__arrow\\" role=\\"img\\" aria-hidden=\\"true\\" focusable=\\"false\\"><path d=\\"M6.5 12.4L12 8l5.5 4.4-.9 1.2L12 10l-4.5 3.6-1-1.2z\\"></path></svg></span>My panel title</button></h2>My panel content</div>"`;
exports[`PluginPrePublishPanel renders fill properly 1`] = `"<div class=\\"components-panel__body my-plugin-pre-publish-panel is-opened\\"><div class=\\"components-panel__body-header\\"><h2 class=\\"components-panel__body-title\\"><button type=\\"button\\" aria-expanded=\\"true\\" class=\\"components-button components-panel__body-toggle\\"><span aria-hidden=\\"true\\"><svg width=\\"24\\" height=\\"24\\" viewBox=\\"0 0 24 24\\" xmlns=\\"http://www.w3.org/2000/svg\\" class=\\"components-panel__arrow\\" role=\\"img\\" aria-hidden=\\"true\\" focusable=\\"false\\"><path d=\\"M6.5 12.4L12 8l5.5 4.4-.9 1.2L12 10l-4.5 3.6-1-1.2z\\"></path></svg></span>My panel title</button></h2></div>My panel content</div>"`;
q
34 changes: 18 additions & 16 deletions packages/editor/src/components/post-publish-panel/prepublish.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,29 +54,31 @@ function PostPublishPanelPrepublish( {
<>
<PanelBody
initialOpen={ false }
title={ [
__( 'Visibility:' ),
<span
className="editor-post-publish-panel__link"
key="label"
>
title={ __( 'Visibility' ) }
summary={
<>
{
/* translators: Describing the following as the current visibility. */
__( 'Set to:' )
}{ ' ' }
<PostVisibilityLabel />
</span>,
] }
</>
}
>
<PostVisibility />
</PanelBody>
<PanelBody
initialOpen={ false }
title={ [
__( 'Publish:' ),
<span
className="editor-post-publish-panel__link"
key="label"
>
title={ __( 'Publish date' ) }
summary={
<>
{
/* translators: Describing the following as the current publish date. */
__( 'Set to:' )
}{ ' ' }
<PostScheduleLabel />
</span>,
] }
</>
}
>
<PostSchedule />
</PanelBody>
Expand Down