Skip to content
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

Details block: add name attribute for exclusive accordion #56971

Open
wants to merge 3 commits into
base: trunk
Choose a base branch
from
Open
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
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
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
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 @@ -10,6 +10,7 @@ import {
} from '@wordpress/block-editor';
import { useSelect } from '@wordpress/data';
import {
TextControl,
ToggleControl,
__experimentalToolsPanel as ToolsPanel,
__experimentalToolsPanelItem as ToolsPanelItem,
Expand All @@ -31,7 +32,7 @@ const TEMPLATE = [
];

function DetailsEdit( { attributes, setAttributes, clientId } ) {
const { showContent, summary, allowedBlocks } = attributes;
const { name, showContent, summary, allowedBlocks } = attributes;
const blockProps = useBlockProps();
const innerBlocksProps = useInnerBlocksProps( blockProps, {
template: TEMPLATE,
Expand Down Expand Up @@ -89,6 +90,20 @@ function DetailsEdit( { attributes, setAttributes, clientId } ) {
</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={ hasSelection || showContent }
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
Loading