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

Add Details/summary block #45055

Merged
merged 53 commits into from
Apr 6, 2023
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
be21b7f
Add "details" block
carolinan Oct 18, 2022
9686338
Add variation, fix stylesheet loading on front, change font size
carolinan Oct 18, 2022
ed02fa7
Remove the link option from the summary heading, update aria label
carolinan Oct 18, 2022
340fe0e
Update edit.js
carolinan Oct 18, 2022
7468272
Keep the details open in the editor
carolinan Oct 20, 2022
04b8f85
Add toolbar control for collapsing and expanding the details, remove …
carolinan Oct 21, 2022
80375c0
Update fixtures
carolinan Oct 21, 2022
84ede00
Update style.scss
carolinan Oct 21, 2022
843ab63
re-add the font size for the heading
carolinan Oct 21, 2022
3c2397f
Remove font size limitations
carolinan Oct 27, 2022
8810fb0
Update cursor to pointer.
carolinan Oct 27, 2022
82ce6b0
Add aria label that announces the expanded or collapsed state
carolinan Oct 27, 2022
b56a2a7
Remove transcript variation, add keywords
carolinan Oct 31, 2022
26b07c4
Open the details when the block is selected
carolinan Nov 4, 2022
14d6974
Update edit.js
carolinan Nov 4, 2022
125b246
Add a basic unwrap
carolinan Nov 4, 2022
82643c4
Update edit.js
carolinan Nov 5, 2022
10ffe31
Merge branch 'trunk' into add/details-summary-block
carolinan Nov 7, 2022
c75a4aa
Try using separate blocks
carolinan Nov 8, 2022
bc421cc
Update summary text, descriptions and fixtures
carolinan Nov 8, 2022
bd3474a
Update edit.js
carolinan Nov 9, 2022
19cf689
remove transform and editor style.
carolinan Nov 9, 2022
a7c05f2
update icons.
carolinan Nov 9, 2022
c31508c
Limit the inner blocks to one inside the Details block
carolinan Nov 10, 2022
4aef03e
Remove the alignments from the inner blocks, and prevent them from be…
carolinan Nov 10, 2022
0297f7d
Update index.js
carolinan Nov 11, 2022
a1698ee
Merge branch 'trunk' into add/details-summary-block
carolinan Dec 16, 2022
98ee1ee
Merge branch 'trunk' into add/details-summary-block
carolinan Dec 30, 2022
b740036
Merge branch 'trunk' into add/details-summary-block
carolinan Feb 8, 2023
d72a333
Add the icon
carolinan Feb 13, 2023
95482f0
Try to apply font size to the heading inside summary
carolinan Feb 13, 2023
3e2cd78
Update fixtures
carolinan Feb 13, 2023
d4b3923
Remove the allowed formatting from the RichText
carolinan Feb 17, 2023
767c4a2
Merge branch 'trunk' into add/details-summary-block
carolinan Feb 17, 2023
8146396
Merge branch 'trunk' into add/details-summary-block
carolinan Feb 21, 2023
89a07cf
Merge branch 'trunk' into add/details-summary-block
carolinan Feb 28, 2023
cb2a3d8
Try the experimentalSelector and skipSerialization again
carolinan Feb 28, 2023
7dc6665
Merge branch 'trunk' into add/details-summary-block
carolinan Mar 1, 2023
460041a
Remove heading from Summary
carolinan Mar 1, 2023
c923448
Merge branch 'trunk' into add/details-summary-block
carolinan Mar 2, 2023
ff578c2
Rename the block, update CSS.
carolinan Mar 3, 2023
52a404c
Update style.scss
carolinan Mar 3, 2023
c9382af
Change the category to text
carolinan Mar 3, 2023
64be240
Merge branch 'trunk' into add/details-summary-block
carolinan Mar 13, 2023
492ad92
Remove unsupported marker CSS
carolinan Mar 13, 2023
90f5249
fix background clipping and update description
carolinan Mar 13, 2023
731a8e8
Replace inner div with View primitive
carolinan Mar 13, 2023
eb47efc
Merge branch 'trunk' into add/details-summary-block
carolinan Mar 30, 2023
597f6b6
Remove View from save.js
carolinan Mar 31, 2023
772a334
Remove translation from save.js
carolinan Mar 31, 2023
2a3b19b
Make the block opt-in, available from the Experiments page.
carolinan Mar 31, 2023
995102f
Merge branch 'trunk' into add/details-summary-block
carolinan Apr 4, 2023
9edd68e
remove fixtures
carolinan Apr 4, 2023
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
27 changes: 27 additions & 0 deletions docs/reference-guides/core-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,33 @@ Add an image or video with a text overlay — great for headers. ([Source](https
- **Supports:** align, anchor, color (~~background~~, ~~text~~), spacing (margin, padding), typography (fontSize, lineHeight), ~~html~~
- **Attributes:** allowedBlocks, alt, backgroundType, contentPosition, customGradient, customOverlayColor, dimRatio, focalPoint, gradient, hasParallax, id, isDark, isRepeated, minHeight, minHeightUnit, overlayColor, templateLock, url, useFeaturedImage

## Details

A block that displays a summary and shows or hides additional content. ([Source](https://github.com/WordPress/gutenberg/tree/trunk/packages/block-library/src/details))

- **Name:** core/details
- **Category:** theme
- **Supports:** align, color (background, gradients, link, text), spacing (margin, padding), typography (fontSize, lineHeight), ~~html~~
- **Attributes:** showContent

## Details Content

The content inside the details is hidden until the details are expanded. ([Source](https://github.com/WordPress/gutenberg/tree/trunk/packages/block-library/src/details-content))

- **Name:** core/details-content
- **Category:** theme
- **Supports:** color (background, gradients, link, text), spacing (margin, padding), typography (fontSize, lineHeight), ~~align~~, ~~html~~, ~~lock~~, ~~multiple~~, ~~reusable~~
- **Attributes:**

## Details Summary

Clicking on the summary will show or hide the content inside the details. ([Source](https://github.com/WordPress/gutenberg/tree/trunk/packages/block-library/src/details-summary))

- **Name:** core/details-summary
- **Category:** theme
- **Supports:** color (background, gradients, text), spacing (margin, padding), typography (fontSize, lineHeight), ~~align~~, ~~html~~, ~~lock~~, ~~multiple~~, ~~reusable~~
- **Attributes:** level, summary

## Embed

Add a block that displays content pulled from other sites, like Twitter or YouTube. ([Source](https://github.com/WordPress/gutenberg/tree/trunk/packages/block-library/src/embed))
Expand Down
3 changes: 3 additions & 0 deletions lib/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ function gutenberg_reregister_core_block_types() {
'column',
'columns',
'comments',
'details',
'details-content',
'details-summary',
'group',
'html',
'list',
Expand Down
51 changes: 51 additions & 0 deletions packages/block-library/src/details-content/block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 2,
"__experimental": true,
"name": "core/details-content",
"title": "Details Content",
"category": "theme",
"parent": [ "core/details" ],
"description": "The content inside the details is hidden until the details are expanded.",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This description reads a little awkwardly to me without the context of the primary Details block description being before it as per docs/reference-guides/core-blocks.md. Perhaps we could tweak the descriptions for both the Details Content and Details Summary blocks to be more self sufficient?

"textdomain": "default",
"supports": {
"align": false,
"color": {
"gradients": true,
"link": true,
"__experimentalDefaultControls": {
"background": true,
"text": true,
"link": true
}
},
"__experimentalBorder": {
"radius": true,
"color": true,
"width": true,
"style": true
},
"html": false,
"lock": false,
"multiple": false,
"reusable": false,
"spacing": {
"margin": true,
"padding": true
},
"typography": {
"fontSize": true,
"lineHeight": true,
"__experimentalFontFamily": true,
"__experimentalFontWeight": true,
"__experimentalFontStyle": true,
"__experimentalTextTransform": true,
"__experimentalTextDecoration": true,
"__experimentalLetterSpacing": true,
"__experimentalDefaultControls": {
"fontSize": true
}
}
},
"style": "wp-block-details"
}
28 changes: 28 additions & 0 deletions packages/block-library/src/details-content/edit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* WordPress dependencies
*/
import { useBlockProps, useInnerBlocksProps } from '@wordpress/block-editor';
import { __ } from '@wordpress/i18n';

const TEMPLATE = [
[
'core/paragraph',
{
placeholder: __(
'Add text or blocks that will display when the details block is opened.'
),
},
],
];

function DetailsContentBlock() {
carolinan marked this conversation as resolved.
Show resolved Hide resolved
const blockProps = useBlockProps();
const innerBlocksProps = useInnerBlocksProps( blockProps, {
template: TEMPLATE,
templateLock: false,
} );

return <div { ...innerBlocksProps }></div>;
}

export default DetailsContentBlock;
23 changes: 23 additions & 0 deletions packages/block-library/src/details-content/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* WordPress dependencies
*/
import { postContent as icon } from '@wordpress/icons';

/**
* Internal dependencies
*/
import initBlock from '../utils/init-block';
import metadata from './block.json';
import edit from './edit';
import save from './save';

const { name } = metadata;
export { metadata, name };

export const settings = {
icon,
save,
edit,
};

export const init = () => initBlock( { name, metadata, settings } );
13 changes: 13 additions & 0 deletions packages/block-library/src/details-content/save.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* WordPress dependencies
*/
import { useBlockProps, InnerBlocks } from '@wordpress/block-editor';

export default function save() {
const blockProps = useBlockProps.save();
return (
<div { ...blockProps }>
carolinan marked this conversation as resolved.
Show resolved Hide resolved
<InnerBlocks.Content />
</div>
);
}
60 changes: 60 additions & 0 deletions packages/block-library/src/details-summary/block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
{
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 2,
"__experimental": true,
"name": "core/details-summary",
"title": "Details Summary",
"category": "theme",
"parent": [ "core/details" ],
"description": "Clicking on the summary will show or hide the content inside the details.",
"textdomain": "default",
"attributes": {
"level": {
"type": "number",
"default": 2
},
"summary": {
"type": "string",
"source": "html",
"selector": "h1,h2,h3,h4,h5,h6"
}
},
"supports": {
"align": false,
"color": {
"gradients": true,
"__experimentalDefaultControls": {
"background": true,
"text": true
}
},
"__experimentalBorder": {
"radius": true,
"color": true,
"width": true,
"style": true
},
"html": false,
"lock": false,
"multiple": false,
"reusable": false,
"spacing": {
"margin": true,
"padding": true
},
"typography": {
"fontSize": true,
"lineHeight": true,
"__experimentalFontFamily": true,
"__experimentalFontWeight": true,
"__experimentalFontStyle": true,
"__experimentalTextTransform": true,
"__experimentalTextDecoration": true,
"__experimentalLetterSpacing": true,
"__experimentalDefaultControls": {
"fontSize": true
}
}
},
"style": "wp-block-details"
}
48 changes: 48 additions & 0 deletions packages/block-library/src/details-summary/edit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* WordPress dependencies
*/
import {
RichText,
BlockControls,
useBlockProps,
} from '@wordpress/block-editor';
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import HeadingLevelDropdown from './heading-level-dropdown';
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can eventually be replaced by #46003


function DetailsSummaryBlock( { attributes, setAttributes } ) {
const { level, summary } = attributes;
const tagName = 'h' + level;

return (
<>
<BlockControls group="block">
<HeadingLevelDropdown
selectedLevel={ level }
onChange={ ( newLevel ) =>
setAttributes( { level: newLevel } )
}
/>
</BlockControls>
<summary
{ ...useBlockProps() }
onClick={ ( event ) => event.preventDefault() }
>
<RichText
tagName={ tagName }
aria-label={ __( 'Add summary' ) }
withoutInteractiveFormatting
value={ !! summary ? summary : __( 'Details' ) }
onChange={ ( newSummary ) =>
setAttributes( { summary: newSummary } )
}
/>
</summary>
</>
);
}

export default DetailsSummaryBlock;
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
* WordPress dependencies
*/
import { ToolbarDropdownMenu } from '@wordpress/components';
import { __, sprintf } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import HeadingLevelIcon from './heading-level-icon';

const HEADING_LEVELS = [ 1, 2, 3, 4, 5, 6 ];

const POPOVER_PROPS = {
className: 'block-library-heading-level-dropdown',
};

/** @typedef {import('@wordpress/element').WPComponent} WPComponent */

/**
* HeadingLevelDropdown props.
*
* @typedef WPHeadingLevelDropdownProps
*
* @property {number} selectedLevel The chosen heading level.
* @property {(newValue:number)=>any} onChange Callback to run when
* toolbar value is changed.
*/

/**
* Dropdown for selecting a heading level (1 through 6).
*
* @param {WPHeadingLevelDropdownProps} props Component props.
*
* @return {WPComponent} The toolbar.
*/
export default function HeadingLevelDropdown( { selectedLevel, onChange } ) {
return (
<ToolbarDropdownMenu
popoverProps={ POPOVER_PROPS }
icon={ <HeadingLevelIcon level={ selectedLevel } /> }
label={ __( 'Change heading level' ) }
controls={ HEADING_LEVELS.map( ( targetLevel ) => {
{
const isActive = targetLevel === selectedLevel;

return {
icon: (
<HeadingLevelIcon
level={ targetLevel }
isPressed={ isActive }
/>
),
label: sprintf(
// translators: %s: heading level e.g: "1", "2", "3"
__( 'Heading %d' ),
targetLevel
),
isActive,
onClick() {
onChange( targetLevel );
},
role: 'menuitemradio',
};
}
} ) }
/>
);
}
48 changes: 48 additions & 0 deletions packages/block-library/src/details-summary/heading-level-icon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* WordPress dependencies
*/
import { Path, SVG } from '@wordpress/components';

/** @typedef {import('@wordpress/element').WPComponent} WPComponent */

/**
* HeadingLevelIcon props.
*
* @typedef WPHeadingLevelIconProps
*
* @property {number} level The heading level to show an icon for.
* @property {?boolean} isPressed Whether or not the icon should appear pressed; default: false.
*/

/**
* Heading level icon.
*
* @param {WPHeadingLevelIconProps} props Component props.
*
* @return {?WPComponent} The icon.
*/
export default function HeadingLevelIcon( { level, isPressed = false } ) {
const levelToPath = {
1: 'M9 5h2v10H9v-4H5v4H3V5h2v4h4V5zm6.6 0c-.6.9-1.5 1.7-2.6 2v1h2v7h2V5h-1.4z',
2: 'M7 5h2v10H7v-4H3v4H1V5h2v4h4V5zm8 8c.5-.4.6-.6 1.1-1.1.4-.4.8-.8 1.2-1.3.3-.4.6-.8.9-1.3.2-.4.3-.8.3-1.3 0-.4-.1-.9-.3-1.3-.2-.4-.4-.7-.8-1-.3-.3-.7-.5-1.2-.6-.5-.2-1-.2-1.5-.2-.4 0-.7 0-1.1.1-.3.1-.7.2-1 .3-.3.1-.6.3-.9.5-.3.2-.6.4-.8.7l1.2 1.2c.3-.3.6-.5 1-.7.4-.2.7-.3 1.2-.3s.9.1 1.3.4c.3.3.5.7.5 1.1 0 .4-.1.8-.4 1.1-.3.5-.6.9-1 1.2-.4.4-1 .9-1.6 1.4-.6.5-1.4 1.1-2.2 1.6V15h8v-2H15z',
3: 'M12.1 12.2c.4.3.8.5 1.2.7.4.2.9.3 1.4.3.5 0 1-.1 1.4-.3.3-.1.5-.5.5-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7.5c-.2-.2-.5-.4-.8-.5-.6-.2-1.3-.2-1.9 0-.3.1-.6.3-.8.5l-.6.9c-.2.5-.2.9-.2 1.4.4-.3.8-.6 1.2-.8.4-.2.8-.3 1.3-.3.4 0 .8 0 1.2.2.4.1.7.3 1 .6.3.3.5.6.7.9.2.4.3.8.3 1.3s-.1.9-.3 1.4c-.2.4-.5.7-.8 1-.4.3-.8.5-1.2.6-1 .3-2 .3-3 0-.5-.2-1-.5-1.4-.9-.4-.4-.8-.9-1-1.5-.2-.6-.3-1.3-.3-2.1s.1-1.6.4-2.3c.2-.6.6-1.2 1-1.6.4-.4.9-.7 1.4-.9.6-.3 1.1-.4 1.7-.4.7 0 1.4.1 2 .3.5.2 1 .5 1.4.8 0 .1-1.3 1.4-1.3 1.4zm-2.4 5.8c.2 0 .4 0 .6-.1.2 0 .4-.1.5-.2.1-.1.3-.3.4-.5.1-.2.1-.5.1-.7 0-.4-.1-.8-.4-1.1-.3-.2-.7-.3-1.1-.3-.3 0-.7.1-1 .2-.4.2-.7.4-1 .7 0 .3.1.7.3 1 .1.2.3.4.4.6.2.1.3.3.5.3.2.1.5.2.7.1z',
};
if ( ! levelToPath.hasOwnProperty( level ) ) {
return null;
}

return (
<SVG
width="24"
height="24"
viewBox="0 0 20 20"
xmlns="http://www.w3.org/2000/svg"
isPressed={ isPressed }
>
<Path d={ levelToPath[ level ] } />
</SVG>
);
}
Loading