Skip to content

Commit

Permalink
Details block: remove auto-close behaviour (#67766)
Browse files Browse the repository at this point in the history
Co-authored-by: ellatrix <[email protected]>
Co-authored-by: Mamaduka <[email protected]>
Co-authored-by: annezazu <[email protected]>
  • Loading branch information
4 people authored Jan 13, 2025
1 parent cd8e655 commit aa38b22
Showing 1 changed file with 10 additions and 22 deletions.
32 changes: 10 additions & 22 deletions packages/block-library/src/details/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@ import {
RichText,
useBlockProps,
useInnerBlocksProps,
store as blockEditorStore,
InspectorControls,
} from '@wordpress/block-editor';
import { useSelect } from '@wordpress/data';
import {
ToggleControl,
__experimentalToolsPanel as ToolsPanel,
__experimentalToolsPanelItem as ToolsPanelItem,
} from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { useState } from '@wordpress/element';

/**
* Internal dependencies
Expand All @@ -30,30 +29,17 @@ const TEMPLATE = [
],
];

function DetailsEdit( { attributes, setAttributes, clientId } ) {
function DetailsEdit( { attributes, setAttributes } ) {
const { showContent, summary, allowedBlocks } = attributes;
const blockProps = useBlockProps();
const innerBlocksProps = useInnerBlocksProps( blockProps, {
template: TEMPLATE,
__experimentalCaptureToolbars: true,
allowedBlocks,
} );
const [ isOpen, setIsOpen ] = useState( showContent );
const dropdownMenuProps = useToolsPanelDropdownMenuProps();

// Check if either the block or the inner blocks are selected.
const hasSelection = useSelect(
( select ) => {
const { isBlockSelected, hasSelectedInnerBlock } =
select( blockEditorStore );
/* Sets deep to true to also find blocks inside the details content block. */
return (
hasSelectedInnerBlock( clientId, true ) ||
isBlockSelected( clientId )
);
},
[ clientId ]
);

return (
<>
<InspectorControls>
Expand Down Expand Up @@ -89,11 +75,13 @@ function DetailsEdit( { attributes, setAttributes, clientId } ) {
</ToolsPanelItem>
</ToolsPanel>
</InspectorControls>
<details
{ ...innerBlocksProps }
open={ hasSelection || showContent }
>
<summary onClick={ ( event ) => event.preventDefault() }>
<details { ...innerBlocksProps } open={ isOpen }>
<summary
onClick={ ( event ) => {
event.preventDefault();
setIsOpen( ! isOpen );
} }
>
<RichText
identifier="summary"
aria-label={ __( 'Write summary' ) }
Expand Down

0 comments on commit aa38b22

Please sign in to comment.