Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Update: Accordion heading level synchronization.
  • Loading branch information
levinbaria committed Oct 1, 2025
commit cd0fd710f39b584962c2c01e5e87d267219913d8
2 changes: 1 addition & 1 deletion docs/reference-guides/core-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Displays a group of accordion headers and associated expandable content. ([Sourc
- **Category:** design
- **Allowed Blocks:** core/accordion-content
- **Supports:** align (full, wide), background (backgroundImage, backgroundSize), color (background, gradients, text), interactivity, layout, shadow, spacing (blockGap, margin, padding), typography (fontSize, lineHeight), ~~html~~
- **Attributes:** autoclose, iconPosition, showIcon
- **Attributes:** autoclose, headingLevel, iconPosition, showIcon

## Accordion Content

Expand Down
6 changes: 2 additions & 4 deletions packages/block-library/src/accordion-header/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"parent": [ "core/accordion-content" ],
"usesContext": [
"core/accordion-icon-position",
"core/accordion-show-icon"
"core/accordion-show-icon",
"core/accordion-heading-level"
],
"supports": {
"anchor": true,
Expand Down Expand Up @@ -68,9 +69,6 @@
"type": "number",
"default": 3
},
"levelOptions": {
"type": "array"
},
"iconPosition": {
"type": "string",
"enum": [ "left", "right" ],
Expand Down
101 changes: 47 additions & 54 deletions packages/block-library/src/accordion-header/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,17 @@ import { useEffect } from '@wordpress/element';
import {
useBlockProps,
__experimentalGetSpacingClassesAndStyles as useSpacingProps,
BlockControls,
HeadingLevelDropdown,
RichText,
} from '@wordpress/block-editor';
import { ToolbarGroup } from '@wordpress/components';

export default function Edit( { attributes, setAttributes, context } ) {
const { level, title, levelOptions } = attributes;
const { title } = attributes;
const {
'core/accordion-icon-position': iconPosition,
'core/accordion-show-icon': showIcon,
'core/accordion-heading-level': headingLevel,
} = context;
const TagName = 'h' + level;
const TagName = 'h' + headingLevel;

// Set icon attributes.
useEffect( () => {
Expand All @@ -30,58 +28,53 @@ export default function Edit( { attributes, setAttributes, context } ) {
}
}, [ iconPosition, showIcon, setAttributes ] );

// Sync heading level from context.
useEffect( () => {
if ( headingLevel !== undefined && headingLevel !== attributes.level ) {
setAttributes( { level: headingLevel } );
}
}, [ headingLevel, attributes.level, setAttributes ] )

const blockProps = useBlockProps();
const spacingProps = useSpacingProps( attributes );

return (
<>
<BlockControls>
<ToolbarGroup>
<HeadingLevelDropdown
value={ level }
options={ levelOptions }
onChange={ ( newLevel ) =>
setAttributes( { level: newLevel } )
}
/>
</ToolbarGroup>
</BlockControls>
<TagName { ...blockProps }>
<button
className="wp-block-accordion-header__toggle"
style={ {
...spacingProps.style,
} }
>
{ showIcon && iconPosition === 'left' && (
<span
className="wp-block-accordion-header__toggle-icon"
aria-hidden="true"
>
+
</span>
) }
<RichText
withoutInteractiveFormatting
disableLineBreaks
tagName="span"
value={ title }
onChange={ ( newTitle ) =>
setAttributes( { title: newTitle } )
}
placeholder={ __( 'Accordion title' ) }
className="wp-block-accordion-header__toggle-title"
/>
{ showIcon && iconPosition === 'right' && (
<span
className="wp-block-accordion-header__toggle-icon"
aria-hidden="true"
>
+
</span>
) }
</button>
</TagName>
</>

<TagName { ...blockProps }>
<button
className="wp-block-accordion-header__toggle"
style={ {
...spacingProps.style,
} }
>
{ showIcon && iconPosition === 'left' && (
<span
className="wp-block-accordion-header__toggle-icon"
aria-hidden="true"
>
+
</span>
) }
<RichText
withoutInteractiveFormatting
disableLineBreaks
tagName="span"
value={ title }
onChange={ ( newTitle ) =>
setAttributes( { title: newTitle } )
}
placeholder={ __( 'Accordion title' ) }
className="wp-block-accordion-header__toggle-title"
/>
{ showIcon && iconPosition === 'right' && (
<span
className="wp-block-accordion-header__toggle-icon"
aria-hidden="true"
>
+
</span>
) }
</button>
</TagName>
);
}
2 changes: 1 addition & 1 deletion packages/block-library/src/accordion-header/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {

export default function save( { attributes } ) {
const { level, title, iconPosition, showIcon } = attributes;
const TagName = 'h' + level;
const TagName = 'h' + ( level || 3 );

const blockProps = useBlockProps.save();
const spacingProps = getSpacingClassesAndStyles( attributes );
Expand Down
10 changes: 9 additions & 1 deletion packages/block-library/src/accordion/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,19 @@
"autoclose": {
"type": "boolean",
"default": false
},
"headingLevel": {
"type": "number",
"default": 3
},
"levelOptions": {
"type": "array"
}
},
"providesContext": {
"core/accordion-icon-position": "iconPosition",
"core/accordion-show-icon": "showIcon"
"core/accordion-show-icon": "showIcon",
"core/accordion-heading-level": "headingLevel"
},
"allowedBlocks": [ "core/accordion-content" ],
"textdomain": "default",
Expand Down
28 changes: 22 additions & 6 deletions packages/block-library/src/accordion/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
BlockControls,
useBlockEditingMode,
store as blockEditorStore,
HeadingLevelDropdown,
} from '@wordpress/block-editor';
import { __ } from '@wordpress/i18n';
import {
Expand All @@ -17,6 +18,7 @@ import {
__experimentalToolsPanel as ToolsPanel,
__experimentalToolsPanelItem as ToolsPanelItem,
ToolbarButton,
ToolbarGroup,
} from '@wordpress/components';
import { useDispatch } from '@wordpress/data';
import { createBlock } from '@wordpress/blocks';
Expand All @@ -32,7 +34,7 @@ const ACCORDION_BLOCK = {
};

export default function Edit( {
attributes: { autoclose, iconPosition, showIcon },
attributes: { autoclose, iconPosition, showIcon, headingLevel, levelOptions },
clientId,
setAttributes,
isSelected: isSingleSelected,
Expand All @@ -58,11 +60,24 @@ export default function Edit( {
return (
<>
{ isSingleSelected && ! isContentOnlyMode && (
<BlockControls group="other">
<ToolbarButton onClick={ addAccordionContentBlock }>
{ __( 'Add' ) }
</ToolbarButton>
</BlockControls>
<>
<BlockControls>
<ToolbarGroup>
<HeadingLevelDropdown
value={ headingLevel }
options={ levelOptions }
onChange={ ( newLevel ) =>
setAttributes( {headingLevel: newLevel } )
}
/>
</ToolbarGroup>
</BlockControls>
<BlockControls group="other">
<ToolbarButton onClick={ addAccordionContentBlock }>
{ __( 'Add' ) }
</ToolbarButton>
</BlockControls>
</>
) }
<InspectorControls key="setting">
<ToolsPanel
Expand All @@ -72,6 +87,7 @@ export default function Edit( {
autoclose: false,
showIcon: true,
iconPosition: 'right',
headingLevel: 3,
} );
} }
dropdownMenuProps={ dropdownMenuProps }
Expand Down