-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Initial implementation of <details> block #23940
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
Closed
Closed
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
175515c
Initial implementation of <details> block
0ed201e
Add keyword and fix block.json
91e73de
Replaced <details> in the editor with an accessible facimile
aea3c7c
Use CSS to place Twistie icon, move open/close state entirely to focu…
6db0c25
Remove open attribute
aa7d3dc
Add open toggle to InspectorControls, plus small fixes
dd5158b
Fix initialOpen on save
193b35d
prettier
565c44a
Rotate twistie instead of using two glyphs
399d054
Add test fixture
f47fc4c
Rename content attr to summaryContent
b1198d4
Linter
da5b639
Using a summary element in the edit view
ac83185
Fixed space bar for RichText in Firefox
37aeb92
Add faq to keywords
6cbb404
Align open/close toggle
1077a2f
Improve description
623dbdd
Add icon.
jasmussen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,13 @@ | ||
| { | ||
| "name": "core/details", | ||
| "category": "layout", | ||
| "attributes": { | ||
| "initialOpen": { | ||
| "type": "boolean", | ||
| "default": false | ||
| }, | ||
| "summaryContent": { | ||
| "type": "string" | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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,91 @@ | ||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { | ||
| InnerBlocks, | ||
| RichText, | ||
| InspectorControls, | ||
| } from '@wordpress/block-editor'; | ||
| import { __ } from '@wordpress/i18n'; | ||
| import { useSelect } from '@wordpress/data'; | ||
| import { | ||
| ToggleControl, | ||
| Panel, | ||
| PanelBody, | ||
| PanelRow, | ||
| } from '@wordpress/components'; | ||
| import { useEffect, useRef } from '@wordpress/element'; | ||
| import { SPACE } from '@wordpress/keycodes'; | ||
|
|
||
| export default function DetalsEdit( { | ||
| attributes, | ||
| className, | ||
| clientId, | ||
| isSelected, | ||
| setAttributes, | ||
| } ) { | ||
| const summaryRef = useRef( null ); | ||
|
|
||
| const keyUpListener = ( e ) => { | ||
| if ( e.keyCode === SPACE ) { | ||
| e.preventDefault(); | ||
| } | ||
| }; | ||
|
|
||
| const clickListener = ( e ) => e.preventDefault(); | ||
|
|
||
| useEffect( () => { | ||
| if ( ! summaryRef.current ) { | ||
| return; | ||
| } | ||
|
|
||
| summaryRef.current.addEventListener( 'keyup', keyUpListener ); | ||
| summaryRef.current.addEventListener( 'click', clickListener ); | ||
| return () => { | ||
| summaryRef.current.removeEventListener( 'keyup', keyUpListener ); | ||
| summaryRef.current.removeEventListener( 'click', clickListener ); | ||
| }; | ||
| }, [ summaryRef.current ] ); | ||
|
|
||
| const isInnerBlockSelected = useSelect( | ||
| ( select ) => | ||
| select( 'core/block-editor' ).hasSelectedInnerBlock( clientId ), | ||
| [ clientId ] | ||
| ); | ||
|
|
||
| const showInnerBlocks = | ||
| attributes.initialOpen || isSelected || isInnerBlockSelected; | ||
|
|
||
| return ( | ||
| <> | ||
| <InspectorControls> | ||
| <Panel> | ||
| <PanelBody> | ||
| <PanelRow> | ||
| <ToggleControl | ||
| label={ __( 'Open by default' ) } | ||
| onChange={ ( initialOpen ) => | ||
| setAttributes( { initialOpen } ) | ||
| } | ||
| checked={ attributes.initialOpen } | ||
| /> | ||
| </PanelRow> | ||
| </PanelBody> | ||
| </Panel> | ||
| </InspectorControls> | ||
| <details className={ className } open={ showInnerBlocks }> | ||
| <RichText | ||
georgeh marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| tagName="summary" | ||
| value={ attributes.summaryContent } | ||
| onChange={ ( summaryContent ) => | ||
| setAttributes( { summaryContent } ) | ||
| } | ||
| ref={ summaryRef } | ||
| placeholder={ __( 'Write a summary…' ) } | ||
| aria-label={ __( 'Summary text' ) } | ||
| /> | ||
| <InnerBlocks /> | ||
| </details> | ||
| </> | ||
| ); | ||
| } | ||
This file contains hidden or 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,5 @@ | ||
| .wp-block-details { | ||
| summary .rich-text { | ||
| display: inline; | ||
| } | ||
| } |
This file contains hidden or 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 @@ | ||||||
| /** | ||||||
| * WordPress dependencies | ||||||
| */ | ||||||
| import { __ } from '@wordpress/i18n'; | ||||||
| import { details as icon } from '@wordpress/icons'; | ||||||
|
|
||||||
| /** | ||||||
| * Internal dependencies | ||||||
| */ | ||||||
| import edit from './edit'; | ||||||
| import metadata from './block.json'; | ||||||
| import save from './save'; | ||||||
|
|
||||||
| const { name, category, attributes } = metadata; | ||||||
|
|
||||||
| export { metadata, name }; | ||||||
|
|
||||||
| export const settings = { | ||||||
| title: __( 'Details' ), | ||||||
| description: __( 'Create a toggle to show and hide content.' ), | ||||||
| icon, | ||||||
| category, | ||||||
| edit, | ||||||
| save, | ||||||
| // details and summary are not translated because they are the HTML tags | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| keywords: [ 'details', 'summary', __( 'faq' ) ], | ||||||
| attributes, | ||||||
| }; | ||||||
This file contains hidden or 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,16 @@ | ||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { InnerBlocks, RichText } from '@wordpress/block-editor'; | ||
|
|
||
| export default ( { attributes } ) => { | ||
| return ( | ||
| <details open={ attributes.initialOpen }> | ||
| <RichText.Content | ||
| tagName="summary" | ||
| value={ attributes.summaryContent } | ||
| /> | ||
| <InnerBlocks.Content /> | ||
| </details> | ||
| ); | ||
| }; |
This file contains hidden or 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 hidden or 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 hidden or 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,12 @@ | ||
| <!-- wp:core/details {"summaryContent": "This is the summary ", "initialOpen": false} --> | ||
| <details class="wp-block-details"> | ||
| <summary>This is the summary </summary> | ||
| <!-- wp:heading --> | ||
| <h2>More here</h2> | ||
| <!-- /wp:heading --> | ||
|
|
||
| <!-- wp:paragraph --> | ||
| <p></p> | ||
| <!-- /wp:paragraph --> | ||
| </details> | ||
| <!-- /wp:core/details --> |
This file contains hidden or 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,36 @@ | ||
| [ | ||
| { | ||
| "clientId": "_clientId_0", | ||
| "name": "core/details", | ||
| "isValid": true, | ||
| "attributes": { | ||
| "initialOpen": false, | ||
| "summaryContent": "This is the summary " | ||
| }, | ||
| "innerBlocks": [ | ||
| { | ||
| "clientId": "_clientId_0", | ||
| "name": "core/heading", | ||
| "isValid": true, | ||
| "attributes": { | ||
| "content": "More here", | ||
| "level": 2 | ||
| }, | ||
| "innerBlocks": [], | ||
| "originalContent": "<h2>More here</h2>" | ||
| }, | ||
| { | ||
| "clientId": "_clientId_1", | ||
| "name": "core/paragraph", | ||
| "isValid": true, | ||
| "attributes": { | ||
| "content": "", | ||
| "dropCap": false | ||
| }, | ||
| "innerBlocks": [], | ||
| "originalContent": "<p></p>" | ||
| } | ||
| ], | ||
| "originalContent": "<details class=\"wp-block-details\">\n\t<summary>This is the summary </summary>\n\t\n\n\t\n</details>" | ||
| } | ||
| ] |
46 changes: 46 additions & 0 deletions
46
packages/e2e-tests/fixtures/blocks/core__details.parsed.json
This file contains hidden or 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,46 @@ | ||
| [ | ||
| { | ||
| "blockName": "core/details", | ||
| "attrs": { | ||
| "summaryContent": "This is the summary ", | ||
| "initialOpen": false | ||
| }, | ||
| "innerBlocks": [ | ||
| { | ||
| "blockName": "core/heading", | ||
| "attrs": {}, | ||
| "innerBlocks": [], | ||
| "innerHTML": "\n\t<h2>More here</h2>\n\t", | ||
| "innerContent": [ | ||
| "\n\t<h2>More here</h2>\n\t" | ||
| ] | ||
| }, | ||
| { | ||
| "blockName": "core/paragraph", | ||
| "attrs": {}, | ||
| "innerBlocks": [], | ||
| "innerHTML": "\n\t<p></p>\n\t", | ||
| "innerContent": [ | ||
| "\n\t<p></p>\n\t" | ||
| ] | ||
| } | ||
| ], | ||
| "innerHTML": "\n<details class=\"wp-block-details\">\n\t<summary>This is the summary </summary>\n\t\n\n\t\n</details>\n", | ||
| "innerContent": [ | ||
| "\n<details class=\"wp-block-details\">\n\t<summary>This is the summary </summary>\n\t", | ||
| null, | ||
| "\n\n\t", | ||
| null, | ||
| "\n</details>\n" | ||
| ] | ||
| }, | ||
| { | ||
| "blockName": null, | ||
| "attrs": {}, | ||
| "innerBlocks": [], | ||
| "innerHTML": "\n", | ||
| "innerContent": [ | ||
| "\n" | ||
| ] | ||
| } | ||
| ] |
9 changes: 9 additions & 0 deletions
9
packages/e2e-tests/fixtures/blocks/core__details.serialized.html
This file contains hidden or 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 @@ | ||
| <!-- wp:details {"summaryContent":"This is the summary "} --> | ||
| <details class="wp-block-details"><summary>This is the summary </summary><!-- wp:heading --> | ||
| <h2>More here</h2> | ||
| <!-- /wp:heading --> | ||
|
|
||
| <!-- wp:paragraph --> | ||
| <p></p> | ||
| <!-- /wp:paragraph --></details> | ||
| <!-- /wp:details --> |
This file contains hidden or 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 hidden or 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,12 @@ | ||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { SVG, Path } from '@wordpress/primitives'; | ||
|
|
||
| const details = ( | ||
| <SVG xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"> | ||
| <Path d="M4 20h9v-1.5H4V20zm0-5.5V16h16v-1.5H4zM4 4v8l7-4-7-4z" /> | ||
| </SVG> | ||
| ); | ||
|
|
||
| export default details; |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.