Skip to content

Commit

Permalink
Details block: add name attribute for exclusive accordion (#56971)
Browse files Browse the repository at this point in the history
* Fix conflict

* Don't save empty string.

* Update help text

* Use selector field

---------

Unlinked contributors: thomas-price, sntran, dogee, sfadschm.

Co-authored-by: t-hamano <[email protected]>
Co-authored-by: Soean <[email protected]>
Co-authored-by: fabiankaegy <[email protected]>
Co-authored-by: carolinan <[email protected]>
Co-authored-by: Mamaduka <[email protected]>
Co-authored-by: richtabor <[email protected]>
Co-authored-by: hanneslsm <[email protected]>
Co-authored-by: dabowman <[email protected]>
  • Loading branch information
9 people authored Jan 17, 2025
1 parent 12709bd commit ac316ee
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 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 @@ -255,7 +255,7 @@ Hide and show additional content. ([Source](https://github.com/WordPress/gutenbe
- **Name:** core/details
- **Category:** text
- **Supports:** align (full, wide), anchor, color (background, gradients, link, text), interactivity (clientNavigation), layout (~~allowEditing~~), spacing (blockGap, margin, padding), typography (fontSize, lineHeight), ~~html~~
- **Attributes:** allowedBlocks, showContent, summary
- **Attributes:** allowedBlocks, name, showContent, summary

## Embed

Expand Down
6 changes: 6 additions & 0 deletions packages/block-library/src/details/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
"source": "rich-text",
"selector": "summary"
},
"name": {
"type": "string",
"source": "attribute",
"attribute": "name",
"selector": ".wp-block-details"
},
"allowedBlocks": {
"type": "array"
}
Expand Down
17 changes: 16 additions & 1 deletion packages/block-library/src/details/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
InspectorControls,
} from '@wordpress/block-editor';
import {
TextControl,
ToggleControl,
__experimentalToolsPanel as ToolsPanel,
__experimentalToolsPanelItem as ToolsPanelItem,
Expand All @@ -30,7 +31,7 @@ const TEMPLATE = [
];

function DetailsEdit( { attributes, setAttributes } ) {
const { showContent, summary, allowedBlocks } = attributes;
const { name, showContent, summary, allowedBlocks } = attributes;
const blockProps = useBlockProps();
const innerBlocksProps = useInnerBlocksProps( blockProps, {
template: TEMPLATE,
Expand Down Expand Up @@ -75,6 +76,20 @@ function DetailsEdit( { attributes, setAttributes } ) {
</ToolsPanelItem>
</ToolsPanel>
</InspectorControls>
<InspectorControls group="advanced">
<TextControl
__next40pxDefaultSize
__nextHasNoMarginBottom
label={ __( 'Name attribute' ) }
value={ name || '' }
onChange={ ( newName ) =>
setAttributes( { name: newName } )
}
help={ __(
'Enables multiple Details blocks with the same name attribute to be connected, with only one open at a time.'
) }
/>
</InspectorControls>
<details { ...innerBlocksProps } open={ isOpen }>
<summary
onClick={ ( event ) => {
Expand Down
8 changes: 6 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,16 @@
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 || undefined }
open={ showContent }
>
<summary>
<RichText.Content value={ summary } />
</summary>
Expand Down

1 comment on commit ac316ee

@github-actions
Copy link

Choose a reason for hiding this comment

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

Flaky tests detected in ac316ee.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/12829757439
📝 Reported issues:

Please sign in to comment.