Skip to content

Commit

Permalink
Merge pull request #52 from happyprime/fix/improvements
Browse files Browse the repository at this point in the history
Assorted improvements
  • Loading branch information
jeremyfelt authored Feb 23, 2023
2 parents 3be441a + 91cef11 commit 08c8d27
Show file tree
Hide file tree
Showing 24 changed files with 7,220 additions and 9,503 deletions.
2 changes: 0 additions & 2 deletions .distignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ tests/
vendor/
.distignore
.gitignore
.stylelintrc
composer.json
composer.lock
phpcs.xml
postcss.config.js
README.md
webpack.config.js
3 changes: 0 additions & 3 deletions .stylelintrc

This file was deleted.

14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
# Show/Hide Section Block
# Show / Hide Section Block

Creates accessible summaries that can be toggled to show or hide additional details.
Display an accessible show/hide interface with details and summary elements.

## About
## Description

This is a WordPress plugin that adds a Show/Hide Group block to the Block Editor.
TBD

The block allows users to add sections with summaries and associated content. The display of the associated content can be displayed or hidden by interacting with the summary or the Open/Close All button.
## Changelog

### 2.0.0

Initial public release.
13 changes: 5 additions & 8 deletions blocks/show-hide-group/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,13 @@
"description": "Creates accessible summaries that can be toggled to show or hide additional details.",
"icon": "hidden",
"attributes": {
"allIds": {
"type": "string"
},
"blockCount": {
"type": "number"
"hasToggle": {
"type": "boolean",
"default": false
}
},
"supports": {
"html": false
},
"editorScript": "file:../../build/show-hide-group.js",
"style": "file:./style.css"
}
"editorScript": "file:../../build/show-hide-group.js"
}
77 changes: 34 additions & 43 deletions blocks/show-hide-group/front-end.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,35 @@
function docReady( fn ) {
// see if DOM is already available
if (
document.readyState === 'complete' ||
document.readyState === 'interactive'
) {
// call on next available tick
setTimeout( fn, 1 );
} else {
document.addEventListener( 'DOMContentLoaded', fn );
}
}

docReady( function () {
const toggleAll = document.querySelectorAll(
'.wp-block-happyprime-show-hide-group .toggle-all'
);
if ( toggleAll.length > 0 ) {
toggleAll.forEach( ( toggle ) =>
toggle.addEventListener( 'click', () => {
const details = toggle.parentElement.querySelectorAll(
'details.wp-block-happyprime-show-hide-section'
);
if ( 'open all' === toggle.innerText.toLowerCase() ) {
// Open all.
details.forEach( ( detail ) => {
detail.setAttribute( 'open', true );
} );
// Update button.
toggle.innerText = 'Close All';
toggle.ariaExpanded = true;
} else {
// Close all.
details.forEach( ( detail ) => {
detail.removeAttribute( 'open' );
} );
// Update button.
toggle.innerText = 'Open All';
toggle.ariaExpanded = false;
}
} )
{
const handleToggleButton = () => {
const toggleAll = document.querySelectorAll(
'.wp-block-happyprime-show-hide-group .toggle-all'
);
}
} );

if ( toggleAll.length > 0 ) {
toggleAll.forEach( ( toggle ) =>
toggle.addEventListener( 'click', () => {
const details = toggle.parentElement.querySelectorAll(
'details.wp-block-happyprime-show-hide-section'
);

if ( false === Boolean( toggle.ariaExpanded ) ) {
details.forEach( ( detail ) => {
detail.setAttribute( 'open', true );
} );

toggle.innerText = 'Close All';
toggle.ariaExpanded = true;
} else {
details.forEach( ( detail ) => {
detail.removeAttribute( 'open' );
} );

toggle.innerText = 'Open All';
toggle.ariaExpanded = false;
}
} )
);
}
};

document.addEventListener( 'DOMContentLoaded', handleToggleButton );
}
202 changes: 102 additions & 100 deletions blocks/show-hide-group/index.js
Original file line number Diff line number Diff line change
@@ -1,129 +1,131 @@
import { registerBlockType } from '@wordpress/blocks';
import { InnerBlocks, useBlockProps } from '@wordpress/block-editor';
import {
InnerBlocks,
InspectorControls,
useBlockProps,
} from '@wordpress/block-editor';
import { PanelBody, ToggleControl } from '@wordpress/components';
import { dispatch, useSelect } from '@wordpress/data';
import { __ } from '@wordpress/i18n';

// Internal dependencies.
import metadata from './block.json';
import './style.css';

// Register the block.
registerBlockType( metadata, {
edit: ( props ) => {
const blockProps = useBlockProps(); // eslint-disable-line react-hooks/rules-of-hooks
const {
attributes: { blockCount, allIds },
setAttributes,
} = props;
// eslint-disable-next-line react-hooks/rules-of-hooks
const innerBlocks = useSelect( ( select ) => {
const currentBlocks = select( 'core/block-editor' ).getBlocks(
props.clientId
);
return currentBlocks;
} );
const Edit = ( props ) => {
const {
attributes: { hasToggle },
setAttributes,
} = props;

// Update inner blocks' htmlId attributes when the inner blocks change.
let newId = '';
let currentIds = '';
for ( let i = 0; i < innerBlocks.length; i++ ) {
newId = 'show-hide-section-' + i + '-' + props.clientId;
currentIds += ' ' + newId;
dispatch( 'core/block-editor' ).updateBlockAttributes(
innerBlocks[ i ].clientId,
{ htmlId: newId }
);
}
const details = useSelect( ( select ) => {
const currentBlocks = select( 'core/block-editor' ).getBlocks(
props.clientId
);

// Save the number of inner blocks and the list of IDs the Toggle All button controls.
const currentCount = innerBlocks.length;
setAttributes( { blockCount: currentCount, allIds: currentIds } );
return currentBlocks;
} );

// Create an Open/Close All button which will only be shown if there is more than one inner block.
// eslint-disable-next-line react-hooks/rules-of-hooks
const details = useSelect( ( select ) => {
const currentBlocks = select( 'core/block-editor' ).getBlocks(
props.clientId
);
return currentBlocks;
} );
const toggleAllSections = ( evt ) => {
if ( false === evt.target.ariaExpanded ) {
details.forEach( ( detail ) => {
dispatch( 'core/block-editor' ).updateBlockAttributes(
detail.clientId,
{ isOpen: true }
);
} );

const toggleAllSections = ( evt ) => {
if ( 'open all' === evt.target.innerText.toLowerCase() ) {
// Open all.
details.forEach( ( detail ) => {
dispatch( 'core/block-editor' ).updateBlockAttributes(
detail.clientId,
{ isOpen: 'open' }
);
} );
// Update button.
evt.target.innerText = 'Close All';
evt.target.ariaExpanded = true;
} else {
// Close all.
details.forEach( ( detail ) => {
dispatch( 'core/block-editor' ).updateBlockAttributes(
detail.clientId,
{ isOpen: '' }
);
} );
// Update button.
evt.target.innerText = 'Open All';
evt.target.ariaExpanded = false;
}
};
evt.target.innerText = __( 'Close all', 'show-hide-section' );
evt.target.ariaExpanded = true;
} else {
details.forEach( ( detail ) => {
dispatch( 'core/block-editor' ).updateBlockAttributes(
detail.clientId,
{ isOpen: false }
);
} );

const toggleAll = (
<button
className="toggle-all"
aria-expanded="false"
aria-controls={ allIds }
onClick={ toggleAllSections }
>
Open All
</button>
);
evt.target.innerText = __( 'Open all', 'show-hide-section' );
evt.target.ariaExpanded = false;
}
};

return (
<div { ...blockProps }>
{ blockCount > 1 && toggleAll }
return (
<>
<InspectorControls>
<PanelBody>
<ToggleControl
label={ __(
'Has open/close all toggle',
'show-hide-section'
) }
help={
hasToggle
? __(
'Open/close all toggle will display.',
'show-hide-section'
)
: __(
'Open/close all toggle will not display.',
'show-hide-section'
)
}
checked={ hasToggle }
onChange={ ( value ) => {
setAttributes( { hasToggle: value } );
} }
/>
</PanelBody>
</InspectorControls>
<div { ...useBlockProps() }>
{ hasToggle && (
<button
className="toggle-all"
aria-expanded="false"
onClick={ toggleAllSections }
>
{ __( 'Open all', 'show-hide-section' ) }
</button>
) }
<InnerBlocks
allowedBlocks={ [ 'happyprime/show-hide-section' ] }
template={ [
[
'happyprime/show-hide-section',
{ htmlId: 'show-hide-section-0' },
{},
[ [ 'core/paragraph', {} ] ],
],
[
'happyprime/show-hide-section',
{ htmlId: 'show-hide-section-1' },
{},
[ [ 'core/paragraph', {} ] ],
],
] }
templateLock={ false }
/>
</div>
);
},
save: ( props ) => {
const blockProps = useBlockProps.save();
const { blockCount, allIds } = props.attributes;
const toggleAll = (
<button
className="toggle-all"
aria-expanded="false"
aria-controls={ allIds }
>
Open All
</button>
);
</>
);
};

return (
<div { ...blockProps }>
{ blockCount > 1 && toggleAll }
<InnerBlocks.Content />
</div>
);
},
const Save = ( props ) => {
const {
attributes: { hasToggle },
} = props;

return (
<div { ...useBlockProps.save() }>
{ hasToggle && (
<button className="toggle-all" aria-expanded="false">
{ __( 'Open all', 'show-hide-section' ) }
</button>
) }
<InnerBlocks.Content />
</div>
);
};

// Register the block.
registerBlockType( metadata, {
edit: Edit,
save: Save,
} );
Loading

0 comments on commit 08c8d27

Please sign in to comment.