Skip to content

Commit

Permalink
Add name attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
Soean committed Dec 12, 2023
1 parent 45d7bab commit 7a7d506
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 5 deletions.
2 changes: 1 addition & 1 deletion docs/reference-guides/core-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ Hide and show additional content. ([Source](https://github.com/WordPress/gutenbe
- **Name:** core/details
- **Category:** text
- **Supports:** align (full, wide), color (background, gradients, link, text), layout (~~allowEditing~~), spacing (blockGap, margin, padding), typography (fontSize, lineHeight), ~~html~~
- **Attributes:** showContent, summary
- **Attributes:** name, showContent, summary

## Embed

Expand Down
4 changes: 4 additions & 0 deletions packages/block-library/src/details/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
"keywords": [ "accordion", "summary", "toggle", "disclosure" ],
"textdomain": "default",
"attributes": {
"name": {
"type": "string",
"default": ""
},
"showContent": {
"type": "boolean",
"default": false
Expand Down
14 changes: 12 additions & 2 deletions packages/block-library/src/details/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
InspectorControls,
} from '@wordpress/block-editor';
import { useSelect } from '@wordpress/data';
import { PanelBody, ToggleControl } from '@wordpress/components';
import { PanelBody, TextControl, ToggleControl } from '@wordpress/components';
import { __ } from '@wordpress/i18n';

const TEMPLATE = [
Expand All @@ -22,7 +22,7 @@ const TEMPLATE = [
];

function DetailsEdit( { attributes, setAttributes, clientId } ) {
const { showContent, summary } = attributes;
const { name, showContent, summary } = attributes;
const blockProps = useBlockProps();
const innerBlocksProps = useInnerBlocksProps( blockProps, {
template: TEMPLATE,
Expand Down Expand Up @@ -56,6 +56,16 @@ function DetailsEdit( { attributes, setAttributes, clientId } ) {
} )
}
/>
<TextControl
label="Name"
value={ name }
onChange={ ( newName ) =>
setAttributes( { name: newName } )
}
help={ __(
'When using the name attribute to assign the same name to a group of detail elements, only one element in the set is opened at a time.'
) }
/>
</PanelBody>
</InspectorControls>
<details
Expand Down
4 changes: 2 additions & 2 deletions packages/block-library/src/details/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
import { RichText, useBlockProps, InnerBlocks } from '@wordpress/block-editor';

export default function save( { attributes } ) {
const { showContent } = attributes;
const { name, showContent } = attributes;
const summary = attributes.summary ? attributes.summary : 'Details';
const blockProps = useBlockProps.save();

return (
<details { ...blockProps } open={ showContent }>
<details { ...blockProps } name={ name } open={ showContent }>
<summary>
<RichText.Content value={ summary } />
</summary>
Expand Down

0 comments on commit 7a7d506

Please sign in to comment.