Skip to content
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
13 changes: 13 additions & 0 deletions packages/block-library/src/details/block.json
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"
}
}
}
91 changes: 91 additions & 0 deletions packages/block-library/src/details/edit.js
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
tagName="summary"
value={ attributes.summaryContent }
onChange={ ( summaryContent ) =>
setAttributes( { summaryContent } )
}
ref={ summaryRef }
placeholder={ __( 'Write a summary…' ) }
aria-label={ __( 'Summary text' ) }
/>
<InnerBlocks />
</details>
</>
);
}
5 changes: 5 additions & 0 deletions packages/block-library/src/details/editor.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.wp-block-details {
summary .rich-text {
display: inline;
}
}
28 changes: 28 additions & 0 deletions packages/block-library/src/details/index.js
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// details and summary are not translated because they are the HTML tags
// "details" and "summary" are not translated because they are the HTML tags.

keywords: [ 'details', 'summary', __( 'faq' ) ],
attributes,
};
16 changes: 16 additions & 0 deletions packages/block-library/src/details/save.js
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>
);
};
1 change: 1 addition & 0 deletions packages/block-library/src/editor.scss
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
2 changes: 2 additions & 0 deletions packages/block-library/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -133,6 +134,7 @@ export const registerCoreBlocks = () => {
columns,
column,
cover,
details,
embed,
file,
group,
Expand Down
12 changes: 12 additions & 0 deletions packages/e2e-tests/fixtures/blocks/core__details.html
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 -->
36 changes: 36 additions & 0 deletions packages/e2e-tests/fixtures/blocks/core__details.json
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 packages/e2e-tests/fixtures/blocks/core__details.parsed.json
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"
]
}
]
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 -->
1 change: 1 addition & 0 deletions packages/icons/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
12 changes: 12 additions & 0 deletions packages/icons/src/library/details.js
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;
5 changes: 5 additions & 0 deletions packages/rich-text/src/component/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ||
Expand Down