Skip to content

Commit

Permalink
Details block: remove auto-close behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix committed Dec 10, 2024
1 parent 24c0076 commit 66ede25
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,12 +5,11 @@ import {
RichText,
useBlockProps,
useInnerBlocksProps,
store as blockEditorStore,
InspectorControls,
} from '@wordpress/block-editor';
import { useSelect } from '@wordpress/data';
import { PanelBody, ToggleControl } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { useState } from '@wordpress/element';

const TEMPLATE = [
[
Expand All @@ -21,27 +20,14 @@ const TEMPLATE = [
],
];

function DetailsEdit( { attributes, setAttributes, clientId } ) {
function DetailsEdit( { attributes, setAttributes } ) {
const { showContent, summary } = attributes;
const blockProps = useBlockProps();
const innerBlocksProps = useInnerBlocksProps( blockProps, {
template: TEMPLATE,
__experimentalCaptureToolbars: true,
} );

// 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 ]
);
const [ isOpen, setIsOpen ] = useState( showContent );

return (
<>
Expand All @@ -59,11 +45,13 @@ function DetailsEdit( { attributes, setAttributes, clientId } ) {
/>
</PanelBody>
</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 66ede25

Please sign in to comment.