-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Add Details/summary block #45055
Changes from 28 commits
Commits
Show all changes
53 commits
Select commit
Hold shift + click to select a range
be21b7f
Add "details" block
carolinan 9686338
Add variation, fix stylesheet loading on front, change font size
carolinan ed02fa7
Remove the link option from the summary heading, update aria label
carolinan 340fe0e
Update edit.js
carolinan 7468272
Keep the details open in the editor
carolinan 04b8f85
Add toolbar control for collapsing and expanding the details, remove …
carolinan 80375c0
Update fixtures
carolinan 84ede00
Update style.scss
carolinan 843ab63
re-add the font size for the heading
carolinan 3c2397f
Remove font size limitations
carolinan 8810fb0
Update cursor to pointer.
carolinan 82ce6b0
Add aria label that announces the expanded or collapsed state
carolinan b56a2a7
Remove transcript variation, add keywords
carolinan 26b07c4
Open the details when the block is selected
carolinan 14d6974
Update edit.js
carolinan 125b246
Add a basic unwrap
carolinan 82643c4
Update edit.js
carolinan 10ffe31
Merge branch 'trunk' into add/details-summary-block
carolinan c75a4aa
Try using separate blocks
carolinan bc421cc
Update summary text, descriptions and fixtures
carolinan bd3474a
Update edit.js
carolinan 19cf689
remove transform and editor style.
carolinan a7c05f2
update icons.
carolinan c31508c
Limit the inner blocks to one inside the Details block
carolinan 4aef03e
Remove the alignments from the inner blocks, and prevent them from be…
carolinan 0297f7d
Update index.js
carolinan a1698ee
Merge branch 'trunk' into add/details-summary-block
carolinan 98ee1ee
Merge branch 'trunk' into add/details-summary-block
carolinan b740036
Merge branch 'trunk' into add/details-summary-block
carolinan d72a333
Add the icon
carolinan 95482f0
Try to apply font size to the heading inside summary
carolinan 3e2cd78
Update fixtures
carolinan d4b3923
Remove the allowed formatting from the RichText
carolinan 767c4a2
Merge branch 'trunk' into add/details-summary-block
carolinan 8146396
Merge branch 'trunk' into add/details-summary-block
carolinan 89a07cf
Merge branch 'trunk' into add/details-summary-block
carolinan cb2a3d8
Try the experimentalSelector and skipSerialization again
carolinan 7dc6665
Merge branch 'trunk' into add/details-summary-block
carolinan 460041a
Remove heading from Summary
carolinan c923448
Merge branch 'trunk' into add/details-summary-block
carolinan ff578c2
Rename the block, update CSS.
carolinan 52a404c
Update style.scss
carolinan c9382af
Change the category to text
carolinan 64be240
Merge branch 'trunk' into add/details-summary-block
carolinan 492ad92
Remove unsupported marker CSS
carolinan 90f5249
fix background clipping and update description
carolinan 731a8e8
Replace inner div with View primitive
carolinan eb47efc
Merge branch 'trunk' into add/details-summary-block
carolinan 597f6b6
Remove View from save.js
carolinan 772a334
Remove translation from save.js
carolinan 2a3b19b
Make the block opt-in, available from the Experiments page.
carolinan 995102f
Merge branch 'trunk' into add/details-summary-block
carolinan 9edd68e
remove fixtures
carolinan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.", | ||
"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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; |
69 changes: 69 additions & 0 deletions
69
packages/block-library/src/details-summary/heading-level-dropdown.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
48
packages/block-library/src/details-summary/heading-level-icon.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 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1.5.3-.8 0-1.6-.1-2.3-.4-.6-.2-1.1-.6-1.6-1-.1-.1 1-1.5 1-1.5zM9 15H7v-4H3v4H1V5h2v4h4V5h2v10z', | ||
6: 'M9 15H7v-4H3v4H1V5h2v4h4V5h2v10zm8.6-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> | ||
); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?