From 175515ccbe9566a949efc882d34ef501913366f2 Mon Sep 17 00:00:00 2001 From: George Hotelling Date: Tue, 14 Jul 2020 16:16:13 -0400 Subject: [PATCH 01/18] Initial implementation of
block --- packages/block-library/src/details/block.json | 4 ++++ packages/block-library/src/details/edit.js | 22 +++++++++++++++++++ packages/block-library/src/details/index.js | 22 +++++++++++++++++++ packages/block-library/src/details/save.js | 13 +++++++++++ packages/block-library/src/index.js | 2 ++ 5 files changed, 63 insertions(+) create mode 100644 packages/block-library/src/details/block.json create mode 100644 packages/block-library/src/details/edit.js create mode 100644 packages/block-library/src/details/index.js create mode 100644 packages/block-library/src/details/save.js diff --git a/packages/block-library/src/details/block.json b/packages/block-library/src/details/block.json new file mode 100644 index 00000000000000..af1142cfcd0610 --- /dev/null +++ b/packages/block-library/src/details/block.json @@ -0,0 +1,4 @@ +{ + "name": "core/details", + "category": "layout" +} \ No newline at end of file diff --git a/packages/block-library/src/details/edit.js b/packages/block-library/src/details/edit.js new file mode 100644 index 00000000000000..dbe16cc178e72b --- /dev/null +++ b/packages/block-library/src/details/edit.js @@ -0,0 +1,22 @@ +/** + * WordPress dependencies + */ +import { InnerBlocks, RichText } from '@wordpress/block-editor'; +import { __ } from '@wordpress/i18n'; + +export default ( { attributes, setAttributes } ) => { + return ( + <> + { + setAttributes( { content } ); + } } + placeholder={ __( 'Visible Content' ) } + /> +
+ + + ); +}; diff --git a/packages/block-library/src/details/index.js b/packages/block-library/src/details/index.js new file mode 100644 index 00000000000000..c465e3d3eb627d --- /dev/null +++ b/packages/block-library/src/details/index.js @@ -0,0 +1,22 @@ +/** + * WordPress dependencies + */ +import { __ } from '@wordpress/i18n'; + +/** + * Internal dependencies + */ +import edit from './edit'; +import metadata from './block.json'; +import save from './save'; + +const { name, category } = metadata; + +export { metadata, name }; + +export const settings = { + title: __( 'Details' ), + category, + edit, + save, +}; diff --git a/packages/block-library/src/details/save.js b/packages/block-library/src/details/save.js new file mode 100644 index 00000000000000..eed7aac050257c --- /dev/null +++ b/packages/block-library/src/details/save.js @@ -0,0 +1,13 @@ +/** + * WordPress dependencies + */ +import { InnerBlocks, RichText } from '@wordpress/block-editor'; + +export default ( { attributes } ) => { + return ( +
+ + +
+ ); +}; diff --git a/packages/block-library/src/index.js b/packages/block-library/src/index.js index d92ab8f3a05429..1ce85cc737c643 100644 --- a/packages/block-library/src/index.js +++ b/packages/block-library/src/index.js @@ -31,6 +31,7 @@ import * as code from './code'; import * as columns from './columns'; import * as column from './column'; import * as cover from './cover'; +import * as details from './details'; import * as embed from './embed'; import * as file from './file'; import * as html from './html'; @@ -133,6 +134,7 @@ export const registerCoreBlocks = () => { columns, column, cover, + details, embed, file, group, From 0ed201ebbff0e99b317ef19509b0339cba6c7908 Mon Sep 17 00:00:00 2001 From: George Hotelling Date: Wed, 15 Jul 2020 16:43:09 -0400 Subject: [PATCH 02/18] Add keyword and fix block.json --- packages/block-library/src/details/block.json | 2 +- packages/block-library/src/details/index.js | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/block-library/src/details/block.json b/packages/block-library/src/details/block.json index af1142cfcd0610..41f1a80f6b8621 100644 --- a/packages/block-library/src/details/block.json +++ b/packages/block-library/src/details/block.json @@ -1,4 +1,4 @@ { "name": "core/details", "category": "layout" -} \ No newline at end of file +} diff --git a/packages/block-library/src/details/index.js b/packages/block-library/src/details/index.js index c465e3d3eb627d..7254110a7f75e6 100644 --- a/packages/block-library/src/details/index.js +++ b/packages/block-library/src/details/index.js @@ -19,4 +19,5 @@ export const settings = { category, edit, save, + keywords: [ __( 'accordion' ) ] }; From 91e73deccbcf9b6f533dcf857e39c72826b49a40 Mon Sep 17 00:00:00 2001 From: George Hotelling Date: Fri, 24 Jul 2020 16:06:24 -0400 Subject: [PATCH 03/18] Replaced
in the editor with an accessible facimile --- packages/block-library/src/details/block.json | 11 ++++++- packages/block-library/src/details/edit.js | 32 ++++++++++++++----- .../block-library/src/details/editor.scss | 6 ++++ packages/block-library/src/details/index.js | 5 +-- packages/block-library/src/details/save.js | 2 +- packages/block-library/src/editor.scss | 1 + 6 files changed, 45 insertions(+), 12 deletions(-) create mode 100644 packages/block-library/src/details/editor.scss diff --git a/packages/block-library/src/details/block.json b/packages/block-library/src/details/block.json index 41f1a80f6b8621..3d046ca86da547 100644 --- a/packages/block-library/src/details/block.json +++ b/packages/block-library/src/details/block.json @@ -1,4 +1,13 @@ { "name": "core/details", - "category": "layout" + "category": "layout", + "attributes": { + "open": { + "type": "boolean", + "defalut": false + }, + "content": { + "type": "string" + } + } } diff --git a/packages/block-library/src/details/edit.js b/packages/block-library/src/details/edit.js index dbe16cc178e72b..1202932274df39 100644 --- a/packages/block-library/src/details/edit.js +++ b/packages/block-library/src/details/edit.js @@ -2,21 +2,37 @@ * WordPress dependencies */ import { InnerBlocks, RichText } from '@wordpress/block-editor'; +import { Button } from '@wordpress/components'; import { __ } from '@wordpress/i18n'; -export default ( { attributes, setAttributes } ) => { +export default ( { attributes, className, setAttributes } ) => { + const toggleOpen = () => setAttributes( { open: ! attributes.open } ); + + /* You may be wondering why we don't just use a
element here. + * The problem we are trying to solve is that a is basically a + * button, and when it has focus, it eats the space key. + * + * That's a problem if you want to use a component inside your + * . Each time you press space, it toggles the rest of the + *
, and it doesn't even add a space to your . + * + * Then there's the fact that the space exists for a11y reasons. If you + * catch the event and preventDefault() then you've remove the way for + * keyboard users to toggle the
. + */ return ( - <> +
+ { - setAttributes( { content } ); - } } + onChange={ ( content ) => setAttributes( { content } ) } placeholder={ __( 'Visible Content' ) } /> -
- - + { attributes.open && } +
); }; diff --git a/packages/block-library/src/details/editor.scss b/packages/block-library/src/details/editor.scss new file mode 100644 index 00000000000000..2ba58eaca326cd --- /dev/null +++ b/packages/block-library/src/details/editor.scss @@ -0,0 +1,6 @@ +.wp-block-details { + .twistie, + .pseudo-summary { + display: inline; + } +} diff --git a/packages/block-library/src/details/index.js b/packages/block-library/src/details/index.js index 7254110a7f75e6..ceb7a5694e4819 100644 --- a/packages/block-library/src/details/index.js +++ b/packages/block-library/src/details/index.js @@ -10,7 +10,7 @@ import edit from './edit'; import metadata from './block.json'; import save from './save'; -const { name, category } = metadata; +const { name, category, attributes } = metadata; export { metadata, name }; @@ -19,5 +19,6 @@ export const settings = { category, edit, save, - keywords: [ __( 'accordion' ) ] + keywords: [ __( 'accordion' ) ], + attributes, }; diff --git a/packages/block-library/src/details/save.js b/packages/block-library/src/details/save.js index eed7aac050257c..52887b8983097b 100644 --- a/packages/block-library/src/details/save.js +++ b/packages/block-library/src/details/save.js @@ -5,7 +5,7 @@ import { InnerBlocks, RichText } from '@wordpress/block-editor'; export default ( { attributes } ) => { return ( -
+
diff --git a/packages/block-library/src/editor.scss b/packages/block-library/src/editor.scss index c7c23d390dca10..e01d42131994b4 100644 --- a/packages/block-library/src/editor.scss +++ b/packages/block-library/src/editor.scss @@ -12,6 +12,7 @@ @import "./code/editor.scss"; @import "./columns/editor.scss"; @import "./cover/editor.scss"; +@import "./details/editor.scss"; @import "./embed/editor.scss"; @import "./file/editor.scss"; @import "./classic/editor.scss"; From aea3c7cae378734616fde549c96b3cd776e69208 Mon Sep 17 00:00:00 2001 From: George Hotelling Date: Thu, 30 Jul 2020 12:00:02 -0400 Subject: [PATCH 04/18] Use CSS to place Twistie icon, move open/close state entirely to focus status --- packages/block-library/src/details/edit.js | 55 +++++++++++++------ .../block-library/src/details/editor.scss | 15 ++++- 2 files changed, 51 insertions(+), 19 deletions(-) diff --git a/packages/block-library/src/details/edit.js b/packages/block-library/src/details/edit.js index 1202932274df39..cfa6779f340109 100644 --- a/packages/block-library/src/details/edit.js +++ b/packages/block-library/src/details/edit.js @@ -1,12 +1,36 @@ +/** + * External dependencies + */ +import classnames from 'classnames'; + /** * WordPress dependencies */ import { InnerBlocks, RichText } from '@wordpress/block-editor'; -import { Button } from '@wordpress/components'; import { __ } from '@wordpress/i18n'; +import { useSelect } from '@wordpress/data'; -export default ( { attributes, className, setAttributes } ) => { - const toggleOpen = () => setAttributes( { open: ! attributes.open } ); +export default ( { + attributes, + className, + clientId, + isSelected, + setAttributes, +} ) => { + const innerBlockSelected = useSelect( + ( select ) => + select( 'core/block-editor' ).hasSelectedInnerBlock( clientId ), + [ clientId ] + ); + + const showInnerBlocks = isSelected || innerBlockSelected; + + const classes = classnames( + { + 'is-open': showInnerBlocks, + }, + className + ); /* You may be wondering why we don't just use a
element here. * The problem we are trying to solve is that a is basically a @@ -21,18 +45,17 @@ export default ( { attributes, className, setAttributes } ) => { * keyboard users to toggle the
. */ return ( -
- - setAttributes( { content } ) } - placeholder={ __( 'Visible Content' ) } - /> - { attributes.open && } -
+ <> +
+ setAttributes( { content } ) } + placeholder={ __( 'Visible Content' ) } + /> + { showInnerBlocks && } +
+ ); }; diff --git a/packages/block-library/src/details/editor.scss b/packages/block-library/src/details/editor.scss index 2ba58eaca326cd..9fc44207e9c945 100644 --- a/packages/block-library/src/details/editor.scss +++ b/packages/block-library/src/details/editor.scss @@ -1,6 +1,15 @@ .wp-block-details { - .twistie, - .pseudo-summary { - display: inline; + .block-library-details__pseudo-summary { + margin-left: -1.25em; + } + + .block-library-details__pseudo-summary::before { + content: "▶ "; + } +} + +.wp-block-details.is-open { + .block-library-details__pseudo-summary::before { + content: "▼ "; } } From 6db0c25b01b226a6191fc8cd57b51609eade3a89 Mon Sep 17 00:00:00 2001 From: George Hotelling Date: Thu, 30 Jul 2020 12:02:31 -0400 Subject: [PATCH 05/18] Remove open attribute --- packages/block-library/src/details/block.json | 4 ---- 1 file changed, 4 deletions(-) diff --git a/packages/block-library/src/details/block.json b/packages/block-library/src/details/block.json index 3d046ca86da547..8cf523ce3b9d1f 100644 --- a/packages/block-library/src/details/block.json +++ b/packages/block-library/src/details/block.json @@ -2,10 +2,6 @@ "name": "core/details", "category": "layout", "attributes": { - "open": { - "type": "boolean", - "defalut": false - }, "content": { "type": "string" } From aa7d3dcad9ce24c93d7ae3be0340d250cef445d9 Mon Sep 17 00:00:00 2001 From: George Hotelling Date: Fri, 31 Jul 2020 13:51:26 -0400 Subject: [PATCH 06/18] Add open toggle to InspectorControls, plus small fixes --- packages/block-library/src/details/block.json | 4 ++++ packages/block-library/src/details/edit.js | 19 ++++++++++++++++--- .../block-library/src/details/editor.scss | 7 +++---- packages/block-library/src/details/index.js | 4 +++- packages/block-library/src/details/save.js | 8 ++++++-- 5 files changed, 32 insertions(+), 10 deletions(-) diff --git a/packages/block-library/src/details/block.json b/packages/block-library/src/details/block.json index 8cf523ce3b9d1f..74c50d4e0724cd 100644 --- a/packages/block-library/src/details/block.json +++ b/packages/block-library/src/details/block.json @@ -2,6 +2,10 @@ "name": "core/details", "category": "layout", "attributes": { + "open": { + "type": "boolean", + "default": false + }, "content": { "type": "string" } diff --git a/packages/block-library/src/details/edit.js b/packages/block-library/src/details/edit.js index cfa6779f340109..bd7d76011ca07c 100644 --- a/packages/block-library/src/details/edit.js +++ b/packages/block-library/src/details/edit.js @@ -6,9 +6,14 @@ import classnames from 'classnames'; /** * WordPress dependencies */ -import { InnerBlocks, RichText } from '@wordpress/block-editor'; +import { + InnerBlocks, + RichText, + InspectorControls, +} from '@wordpress/block-editor'; import { __ } from '@wordpress/i18n'; import { useSelect } from '@wordpress/data'; +import { ToggleControl } from '@wordpress/components'; export default ( { attributes, @@ -23,7 +28,7 @@ export default ( { [ clientId ] ); - const showInnerBlocks = isSelected || innerBlockSelected; + const showInnerBlocks = attributes.open || isSelected || innerBlockSelected; const classes = classnames( { @@ -46,13 +51,21 @@ export default ( { */ return ( <> + + setAttributes( { open } ) } + checked={ attributes.open } + /> +
setAttributes( { content } ) } - placeholder={ __( 'Visible Content' ) } + placeholder={ __( 'Write a summary…' ) } + aria-label={ __( 'Summary text' ) } /> { showInnerBlocks && }
diff --git a/packages/block-library/src/details/editor.scss b/packages/block-library/src/details/editor.scss index 9fc44207e9c945..a7ee24b7bbec85 100644 --- a/packages/block-library/src/details/editor.scss +++ b/packages/block-library/src/details/editor.scss @@ -1,11 +1,10 @@ .wp-block-details { .block-library-details__pseudo-summary { - margin-left: -1.25em; + &::before { + content: "▶ "; + } } - .block-library-details__pseudo-summary::before { - content: "▶ "; - } } .wp-block-details.is-open { diff --git a/packages/block-library/src/details/index.js b/packages/block-library/src/details/index.js index ceb7a5694e4819..e364e9c834140c 100644 --- a/packages/block-library/src/details/index.js +++ b/packages/block-library/src/details/index.js @@ -16,9 +16,11 @@ export { metadata, name }; export const settings = { title: __( 'Details' ), + description: __( 'Create a toggle to show and hide blocks' ), category, edit, save, - keywords: [ __( 'accordion' ) ], + // details and summary are not translated because they are the HTML tags + keywords: [ 'details', 'summary' ], attributes, }; diff --git a/packages/block-library/src/details/save.js b/packages/block-library/src/details/save.js index 52887b8983097b..38a6283a373019 100644 --- a/packages/block-library/src/details/save.js +++ b/packages/block-library/src/details/save.js @@ -5,8 +5,12 @@ import { InnerBlocks, RichText } from '@wordpress/block-editor'; export default ( { attributes } ) => { return ( -
- +
+
); From dd5158b3ebf47851533cf790e01dc3d944c85e57 Mon Sep 17 00:00:00 2001 From: George Hotelling Date: Fri, 31 Jul 2020 13:55:28 -0400 Subject: [PATCH 07/18] Fix initialOpen on save --- packages/block-library/src/details/block.json | 2 +- packages/block-library/src/details/edit.js | 6 +++--- packages/block-library/src/details/save.js | 3 +-- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/packages/block-library/src/details/block.json b/packages/block-library/src/details/block.json index 74c50d4e0724cd..7838c7c6ad4c25 100644 --- a/packages/block-library/src/details/block.json +++ b/packages/block-library/src/details/block.json @@ -2,7 +2,7 @@ "name": "core/details", "category": "layout", "attributes": { - "open": { + "initialOpen": { "type": "boolean", "default": false }, diff --git a/packages/block-library/src/details/edit.js b/packages/block-library/src/details/edit.js index bd7d76011ca07c..68625e8b608bcc 100644 --- a/packages/block-library/src/details/edit.js +++ b/packages/block-library/src/details/edit.js @@ -28,7 +28,7 @@ export default ( { [ clientId ] ); - const showInnerBlocks = attributes.open || isSelected || innerBlockSelected; + const showInnerBlocks = attributes.initialOpen || isSelected || innerBlockSelected; const classes = classnames( { @@ -54,8 +54,8 @@ export default ( { setAttributes( { open } ) } - checked={ attributes.open } + onChange={ ( initialOpen ) => setAttributes( { initialOpen } ) } + checked={ attributes.initialOpen } />
diff --git a/packages/block-library/src/details/save.js b/packages/block-library/src/details/save.js index 38a6283a373019..d2c4bcf3781113 100644 --- a/packages/block-library/src/details/save.js +++ b/packages/block-library/src/details/save.js @@ -5,11 +5,10 @@ import { InnerBlocks, RichText } from '@wordpress/block-editor'; export default ( { attributes } ) => { return ( -
+
From 193b35d07dc5de070f9156f4e7c735e2d3a23fd2 Mon Sep 17 00:00:00 2001 From: George Hotelling Date: Fri, 31 Jul 2020 13:57:48 -0400 Subject: [PATCH 08/18] prettier --- packages/block-library/src/details/edit.js | 7 +++++-- packages/block-library/src/details/save.js | 5 +---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/block-library/src/details/edit.js b/packages/block-library/src/details/edit.js index 68625e8b608bcc..85a4789929322c 100644 --- a/packages/block-library/src/details/edit.js +++ b/packages/block-library/src/details/edit.js @@ -28,7 +28,8 @@ export default ( { [ clientId ] ); - const showInnerBlocks = attributes.initialOpen || isSelected || innerBlockSelected; + const showInnerBlocks = + attributes.initialOpen || isSelected || innerBlockSelected; const classes = classnames( { @@ -54,7 +55,9 @@ export default ( { setAttributes( { initialOpen } ) } + onChange={ ( initialOpen ) => + setAttributes( { initialOpen } ) + } checked={ attributes.initialOpen } /> diff --git a/packages/block-library/src/details/save.js b/packages/block-library/src/details/save.js index d2c4bcf3781113..0d88629f57b810 100644 --- a/packages/block-library/src/details/save.js +++ b/packages/block-library/src/details/save.js @@ -6,10 +6,7 @@ import { InnerBlocks, RichText } from '@wordpress/block-editor'; export default ( { attributes } ) => { return (
- +
); From 565c44a697d133d31926dca4a83326fac2a195aa Mon Sep 17 00:00:00 2001 From: George Hotelling Date: Fri, 31 Jul 2020 14:25:11 -0400 Subject: [PATCH 09/18] Rotate twistie instead of using two glyphs --- packages/block-library/src/details/editor.scss | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/block-library/src/details/editor.scss b/packages/block-library/src/details/editor.scss index a7ee24b7bbec85..9acb7134ac67de 100644 --- a/packages/block-library/src/details/editor.scss +++ b/packages/block-library/src/details/editor.scss @@ -1,6 +1,7 @@ .wp-block-details { .block-library-details__pseudo-summary { &::before { + display: inline-block; content: "▶ "; } } @@ -9,6 +10,6 @@ .wp-block-details.is-open { .block-library-details__pseudo-summary::before { - content: "▼ "; + transform: rotate(90deg); } } From 399d054ee5aef6ecf7be61207454703a37e1416d Mon Sep 17 00:00:00 2001 From: George Hotelling Date: Fri, 31 Jul 2020 15:13:48 -0400 Subject: [PATCH 10/18] Add test fixture --- .../fixtures/blocks/core__details.html | 12 +++++ .../fixtures/blocks/core__details.json | 36 +++++++++++++++ .../fixtures/blocks/core__details.parsed.json | 46 +++++++++++++++++++ .../blocks/core__details.serialized.html | 9 ++++ 4 files changed, 103 insertions(+) create mode 100644 packages/e2e-tests/fixtures/blocks/core__details.html create mode 100644 packages/e2e-tests/fixtures/blocks/core__details.json create mode 100644 packages/e2e-tests/fixtures/blocks/core__details.parsed.json create mode 100644 packages/e2e-tests/fixtures/blocks/core__details.serialized.html diff --git a/packages/e2e-tests/fixtures/blocks/core__details.html b/packages/e2e-tests/fixtures/blocks/core__details.html new file mode 100644 index 00000000000000..710f896b540401 --- /dev/null +++ b/packages/e2e-tests/fixtures/blocks/core__details.html @@ -0,0 +1,12 @@ + +
+ This is the summary + +

More here

+ + + +

+ +
+ diff --git a/packages/e2e-tests/fixtures/blocks/core__details.json b/packages/e2e-tests/fixtures/blocks/core__details.json new file mode 100644 index 00000000000000..7606b4e84ef596 --- /dev/null +++ b/packages/e2e-tests/fixtures/blocks/core__details.json @@ -0,0 +1,36 @@ +[ + { + "clientId": "_clientId_0", + "name": "core/details", + "isValid": true, + "attributes": { + "initialOpen": false, + "content": "This is the summary " + }, + "innerBlocks": [ + { + "clientId": "_clientId_0", + "name": "core/heading", + "isValid": true, + "attributes": { + "content": "More here", + "level": 2 + }, + "innerBlocks": [], + "originalContent": "

More here

" + }, + { + "clientId": "_clientId_1", + "name": "core/paragraph", + "isValid": true, + "attributes": { + "content": "", + "dropCap": false + }, + "innerBlocks": [], + "originalContent": "

" + } + ], + "originalContent": "
\n\tThis is the summary \n\t\n\n\t\n
" + } +] diff --git a/packages/e2e-tests/fixtures/blocks/core__details.parsed.json b/packages/e2e-tests/fixtures/blocks/core__details.parsed.json new file mode 100644 index 00000000000000..416e354db393f7 --- /dev/null +++ b/packages/e2e-tests/fixtures/blocks/core__details.parsed.json @@ -0,0 +1,46 @@ +[ + { + "blockName": "core/details", + "attrs": { + "content": "This is the summary ", + "initialOpen": false + }, + "innerBlocks": [ + { + "blockName": "core/heading", + "attrs": {}, + "innerBlocks": [], + "innerHTML": "\n\t

More here

\n\t", + "innerContent": [ + "\n\t

More here

\n\t" + ] + }, + { + "blockName": "core/paragraph", + "attrs": {}, + "innerBlocks": [], + "innerHTML": "\n\t

\n\t", + "innerContent": [ + "\n\t

\n\t" + ] + } + ], + "innerHTML": "\n
\n\tThis is the summary \n\t\n\n\t\n
\n", + "innerContent": [ + "\n
\n\tThis is the summary \n\t", + null, + "\n\n\t", + null, + "\n
\n" + ] + }, + { + "blockName": null, + "attrs": {}, + "innerBlocks": [], + "innerHTML": "\n", + "innerContent": [ + "\n" + ] + } +] diff --git a/packages/e2e-tests/fixtures/blocks/core__details.serialized.html b/packages/e2e-tests/fixtures/blocks/core__details.serialized.html new file mode 100644 index 00000000000000..2e8d97c8455364 --- /dev/null +++ b/packages/e2e-tests/fixtures/blocks/core__details.serialized.html @@ -0,0 +1,9 @@ + +
This is the summary +

More here

+ + + +

+
+ From f47fc4ca03e006bc70bec2175bb1ba24ebdc534c Mon Sep 17 00:00:00 2001 From: George Hotelling Date: Mon, 3 Aug 2020 11:41:16 -0400 Subject: [PATCH 11/18] Rename content attr to summaryContent --- packages/block-library/src/details/block.json | 2 +- packages/block-library/src/details/edit.js | 4 ++-- packages/block-library/src/details/save.js | 2 +- packages/e2e-tests/fixtures/blocks/core__details.html | 2 +- packages/e2e-tests/fixtures/blocks/core__details.json | 2 +- packages/e2e-tests/fixtures/blocks/core__details.parsed.json | 2 +- .../e2e-tests/fixtures/blocks/core__details.serialized.html | 2 +- 7 files changed, 8 insertions(+), 8 deletions(-) diff --git a/packages/block-library/src/details/block.json b/packages/block-library/src/details/block.json index 7838c7c6ad4c25..ee8cbd0ab8aac6 100644 --- a/packages/block-library/src/details/block.json +++ b/packages/block-library/src/details/block.json @@ -6,7 +6,7 @@ "type": "boolean", "default": false }, - "content": { + "summaryContent": { "type": "string" } } diff --git a/packages/block-library/src/details/edit.js b/packages/block-library/src/details/edit.js index 85a4789929322c..0c7b3a66e23c87 100644 --- a/packages/block-library/src/details/edit.js +++ b/packages/block-library/src/details/edit.js @@ -65,8 +65,8 @@ export default ( { setAttributes( { content } ) } + value={ attributes.summaryContent } + onChange={ ( summaryContent ) => setAttributes( { summaryContent } ) } placeholder={ __( 'Write a summary…' ) } aria-label={ __( 'Summary text' ) } /> diff --git a/packages/block-library/src/details/save.js b/packages/block-library/src/details/save.js index 0d88629f57b810..2d7339a1dd3510 100644 --- a/packages/block-library/src/details/save.js +++ b/packages/block-library/src/details/save.js @@ -6,7 +6,7 @@ import { InnerBlocks, RichText } from '@wordpress/block-editor'; export default ( { attributes } ) => { return (
- +
); diff --git a/packages/e2e-tests/fixtures/blocks/core__details.html b/packages/e2e-tests/fixtures/blocks/core__details.html index 710f896b540401..fa943245021d17 100644 --- a/packages/e2e-tests/fixtures/blocks/core__details.html +++ b/packages/e2e-tests/fixtures/blocks/core__details.html @@ -1,4 +1,4 @@ - +
This is the summary diff --git a/packages/e2e-tests/fixtures/blocks/core__details.json b/packages/e2e-tests/fixtures/blocks/core__details.json index 7606b4e84ef596..03beb29ac4a817 100644 --- a/packages/e2e-tests/fixtures/blocks/core__details.json +++ b/packages/e2e-tests/fixtures/blocks/core__details.json @@ -5,7 +5,7 @@ "isValid": true, "attributes": { "initialOpen": false, - "content": "This is the summary " + "summaryContent": "This is the summary " }, "innerBlocks": [ { diff --git a/packages/e2e-tests/fixtures/blocks/core__details.parsed.json b/packages/e2e-tests/fixtures/blocks/core__details.parsed.json index 416e354db393f7..4c8575353bb0d8 100644 --- a/packages/e2e-tests/fixtures/blocks/core__details.parsed.json +++ b/packages/e2e-tests/fixtures/blocks/core__details.parsed.json @@ -2,7 +2,7 @@ { "blockName": "core/details", "attrs": { - "content": "This is the summary ", + "summaryContent": "This is the summary ", "initialOpen": false }, "innerBlocks": [ diff --git a/packages/e2e-tests/fixtures/blocks/core__details.serialized.html b/packages/e2e-tests/fixtures/blocks/core__details.serialized.html index 2e8d97c8455364..8fa6ac9c91cf5e 100644 --- a/packages/e2e-tests/fixtures/blocks/core__details.serialized.html +++ b/packages/e2e-tests/fixtures/blocks/core__details.serialized.html @@ -1,4 +1,4 @@ - +
This is the summary

More here

From b1198d4f2ca4a07b3fd2e2c3c281ee699d49ffe9 Mon Sep 17 00:00:00 2001 From: George Hotelling Date: Mon, 3 Aug 2020 15:12:08 -0400 Subject: [PATCH 12/18] Linter --- packages/block-library/src/details/edit.js | 4 +++- packages/block-library/src/details/save.js | 5 ++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/packages/block-library/src/details/edit.js b/packages/block-library/src/details/edit.js index 0c7b3a66e23c87..c6ff26894113ea 100644 --- a/packages/block-library/src/details/edit.js +++ b/packages/block-library/src/details/edit.js @@ -66,7 +66,9 @@ export default ( { tagName="div" className="block-library-details__pseudo-summary" value={ attributes.summaryContent } - onChange={ ( summaryContent ) => setAttributes( { summaryContent } ) } + onChange={ ( summaryContent ) => + setAttributes( { summaryContent } ) + } placeholder={ __( 'Write a summary…' ) } aria-label={ __( 'Summary text' ) } /> diff --git a/packages/block-library/src/details/save.js b/packages/block-library/src/details/save.js index 2d7339a1dd3510..c3c1d2d03108d5 100644 --- a/packages/block-library/src/details/save.js +++ b/packages/block-library/src/details/save.js @@ -6,7 +6,10 @@ import { InnerBlocks, RichText } from '@wordpress/block-editor'; export default ( { attributes } ) => { return (
- +
); From da5b63980b78052ff27335ff0fc1183383c0ae5f Mon Sep 17 00:00:00 2001 From: George Hotelling Date: Tue, 4 Aug 2020 12:05:03 -0400 Subject: [PATCH 13/18] Using a summary element in the edit view --- packages/block-library/src/details/edit.js | 79 ++++++++++--------- .../block-library/src/details/editor.scss | 14 +--- 2 files changed, 42 insertions(+), 51 deletions(-) diff --git a/packages/block-library/src/details/edit.js b/packages/block-library/src/details/edit.js index c6ff26894113ea..269fb373015b43 100644 --- a/packages/block-library/src/details/edit.js +++ b/packages/block-library/src/details/edit.js @@ -1,8 +1,3 @@ -/** - * External dependencies - */ -import classnames from 'classnames'; - /** * WordPress dependencies */ @@ -14,6 +9,8 @@ import { import { __ } from '@wordpress/i18n'; import { useSelect } from '@wordpress/data'; import { ToggleControl } from '@wordpress/components'; +import { useEffect, useRef } from '@wordpress/element'; +import { SPACE } from '@wordpress/keycodes'; export default ( { attributes, @@ -22,34 +19,38 @@ export default ( { isSelected, setAttributes, } ) => { - const innerBlockSelected = useSelect( + const summaryRef = useRef( null ); + + useEffect( () => { + if ( ! summaryRef.current ) { + return; + } + + const keyDownListener = ( e ) => { + if ( e.keyCode === SPACE ) { + e.preventDefault(); + } + }; + + const clickListener = ( e ) => e.preventDefault(); + + summaryRef.current.addEventListener( 'keyup', keyDownListener ); + summaryRef.current.addEventListener( 'click', clickListener ); + return () => { + summaryRef.current.removeEventListener( 'keyup', keyDownListener ); + summaryRef.current.removeEventListener( 'click', clickListener ); + }; + }, [ summaryRef.current ] ); + + const isInnerBlockSelected = useSelect( ( select ) => select( 'core/block-editor' ).hasSelectedInnerBlock( clientId ), [ clientId ] ); const showInnerBlocks = - attributes.initialOpen || isSelected || innerBlockSelected; - - const classes = classnames( - { - 'is-open': showInnerBlocks, - }, - className - ); + attributes.initialOpen || isSelected || isInnerBlockSelected; - /* You may be wondering why we don't just use a
element here. - * The problem we are trying to solve is that a is basically a - * button, and when it has focus, it eats the space key. - * - * That's a problem if you want to use a component inside your - * . Each time you press space, it toggles the rest of the - *
, and it doesn't even add a space to your . - * - * Then there's the fact that the space exists for a11y reasons. If you - * catch the event and preventDefault() then you've remove the way for - * keyboard users to toggle the
. - */ return ( <> @@ -61,19 +62,19 @@ export default ( { checked={ attributes.initialOpen } /> -
- - setAttributes( { summaryContent } ) - } - placeholder={ __( 'Write a summary…' ) } - aria-label={ __( 'Summary text' ) } - /> - { showInnerBlocks && } -
+
+ + + setAttributes( { summaryContent } ) + } + placeholder={ __( 'Write a summary…' ) } + aria-label={ __( 'Summary text' ) } + /> + + +
); }; diff --git a/packages/block-library/src/details/editor.scss b/packages/block-library/src/details/editor.scss index 9acb7134ac67de..5c06909f94520f 100644 --- a/packages/block-library/src/details/editor.scss +++ b/packages/block-library/src/details/editor.scss @@ -1,15 +1,5 @@ .wp-block-details { - .block-library-details__pseudo-summary { - &::before { - display: inline-block; - content: "▶ "; - } - } - -} - -.wp-block-details.is-open { - .block-library-details__pseudo-summary::before { - transform: rotate(90deg); + summary .rich-text { + display: inline; } } From ac83185b2d4d80b437805bf812c170eb4bd3e9e6 Mon Sep 17 00:00:00 2001 From: George Hotelling Date: Fri, 7 Aug 2020 11:47:51 -0400 Subject: [PATCH 14/18] Fixed space bar for RichText in Firefox --- packages/block-library/src/details/edit.js | 46 +++++++++++----------- packages/rich-text/src/component/index.js | 5 +++ 2 files changed, 28 insertions(+), 23 deletions(-) diff --git a/packages/block-library/src/details/edit.js b/packages/block-library/src/details/edit.js index 269fb373015b43..2a7c230d38c869 100644 --- a/packages/block-library/src/details/edit.js +++ b/packages/block-library/src/details/edit.js @@ -12,32 +12,32 @@ import { ToggleControl } from '@wordpress/components'; import { useEffect, useRef } from '@wordpress/element'; import { SPACE } from '@wordpress/keycodes'; -export default ( { +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; } - const keyDownListener = ( e ) => { - if ( e.keyCode === SPACE ) { - e.preventDefault(); - } - }; - - const clickListener = ( e ) => e.preventDefault(); - - summaryRef.current.addEventListener( 'keyup', keyDownListener ); + summaryRef.current.addEventListener( 'keyup', keyUpListener ); summaryRef.current.addEventListener( 'click', clickListener ); return () => { - summaryRef.current.removeEventListener( 'keyup', keyDownListener ); + summaryRef.current.removeEventListener( 'keyup', keyUpListener ); summaryRef.current.removeEventListener( 'click', clickListener ); }; }, [ summaryRef.current ] ); @@ -63,18 +63,18 @@ export default ( { />
- - - setAttributes( { summaryContent } ) - } - placeholder={ __( 'Write a summary…' ) } - aria-label={ __( 'Summary text' ) } - /> - + + setAttributes( { summaryContent } ) + } + ref={ summaryRef } + placeholder={ __( 'Write a summary…' ) } + aria-label={ __( 'Summary text' ) } + />
); -}; +} diff --git a/packages/rich-text/src/component/index.js b/packages/rich-text/src/component/index.js index 76d8fc64d13268..ad70861aa0c549 100644 --- a/packages/rich-text/src/component/index.js +++ b/packages/rich-text/src/component/index.js @@ -527,6 +527,11 @@ function RichText( { function handleSpace( event ) { const { keyCode, shiftKey, altKey, metaKey, ctrlKey } = event; + if ( keyCode === SPACE && TagName === 'summary' ) { + handleChange( insert( createRecord(), ' ' ) ); + return; + } + if ( // Only override when no modifiers are pressed. shiftKey || From 37aeb928a12c2a2e79f46754ddd83a3e25e53fa7 Mon Sep 17 00:00:00 2001 From: George Hotelling Date: Wed, 12 Aug 2020 11:26:50 -0400 Subject: [PATCH 15/18] Add faq to keywords --- packages/block-library/src/details/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/block-library/src/details/index.js b/packages/block-library/src/details/index.js index e364e9c834140c..f652fb13659e66 100644 --- a/packages/block-library/src/details/index.js +++ b/packages/block-library/src/details/index.js @@ -21,6 +21,6 @@ export const settings = { edit, save, // details and summary are not translated because they are the HTML tags - keywords: [ 'details', 'summary' ], + keywords: [ 'details', 'summary', __( 'faq' ) ], attributes, }; From 6cbb4042fd4ac8e98769ff2bb4696d27c2107a3c Mon Sep 17 00:00:00 2001 From: George Hotelling Date: Wed, 12 Aug 2020 11:27:23 -0400 Subject: [PATCH 16/18] Align open/close toggle --- packages/block-library/src/details/edit.js | 27 +++++++++++++++------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/packages/block-library/src/details/edit.js b/packages/block-library/src/details/edit.js index 2a7c230d38c869..962045ee397eb6 100644 --- a/packages/block-library/src/details/edit.js +++ b/packages/block-library/src/details/edit.js @@ -8,7 +8,12 @@ import { } from '@wordpress/block-editor'; import { __ } from '@wordpress/i18n'; import { useSelect } from '@wordpress/data'; -import { ToggleControl } from '@wordpress/components'; +import { + ToggleControl, + Panel, + PanelBody, + PanelRow, +} from '@wordpress/components'; import { useEffect, useRef } from '@wordpress/element'; import { SPACE } from '@wordpress/keycodes'; @@ -54,13 +59,19 @@ export default function DetalsEdit( { return ( <> - - setAttributes( { initialOpen } ) - } - checked={ attributes.initialOpen } - /> + + + + + setAttributes( { initialOpen } ) + } + checked={ attributes.initialOpen } + /> + + +
Date: Wed, 12 Aug 2020 11:30:06 -0400 Subject: [PATCH 17/18] Improve description --- packages/block-library/src/details/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/block-library/src/details/index.js b/packages/block-library/src/details/index.js index f652fb13659e66..9c40b98b8bd7de 100644 --- a/packages/block-library/src/details/index.js +++ b/packages/block-library/src/details/index.js @@ -16,7 +16,7 @@ export { metadata, name }; export const settings = { title: __( 'Details' ), - description: __( 'Create a toggle to show and hide blocks' ), + description: __( 'Create a toggle to show and hide content.' ), category, edit, save, From 623dbdddf230918c45a119852f215758de518f9a Mon Sep 17 00:00:00 2001 From: jasmussen Date: Wed, 12 Aug 2020 17:43:38 +0200 Subject: [PATCH 18/18] Add icon. --- packages/block-library/src/details/index.js | 2 ++ packages/icons/src/index.js | 1 + packages/icons/src/library/details.js | 12 ++++++++++++ 3 files changed, 15 insertions(+) create mode 100644 packages/icons/src/library/details.js diff --git a/packages/block-library/src/details/index.js b/packages/block-library/src/details/index.js index 9c40b98b8bd7de..a439035fd61880 100644 --- a/packages/block-library/src/details/index.js +++ b/packages/block-library/src/details/index.js @@ -2,6 +2,7 @@ * WordPress dependencies */ import { __ } from '@wordpress/i18n'; +import { details as icon } from '@wordpress/icons'; /** * Internal dependencies @@ -17,6 +18,7 @@ export { metadata, name }; export const settings = { title: __( 'Details' ), description: __( 'Create a toggle to show and hide content.' ), + icon, category, edit, save, diff --git a/packages/icons/src/index.js b/packages/icons/src/index.js index 9054f43a6c4286..f0fcca0acbdbb0 100644 --- a/packages/icons/src/index.js +++ b/packages/icons/src/index.js @@ -49,6 +49,7 @@ export { default as currencyDollar } from './library/currency-dollar'; export { default as currencyEuro } from './library/currency-euro'; export { default as currencyPound } from './library/currency-pound'; export { default as desktop } from './library/desktop'; +export { default as details } from './library/details'; export { default as edit } from './library/edit'; export { default as external } from './library/external'; export { default as file } from './library/file'; diff --git a/packages/icons/src/library/details.js b/packages/icons/src/library/details.js new file mode 100644 index 00000000000000..ef9d3c95051077 --- /dev/null +++ b/packages/icons/src/library/details.js @@ -0,0 +1,12 @@ +/** + * WordPress dependencies + */ +import { SVG, Path } from '@wordpress/primitives'; + +const details = ( + + + +); + +export default details;