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: remove auto-close behaviour #67766

Merged
merged 1 commit into from
Jan 13, 2025
Merged
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
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
Loading