Skip to content

Commit

Permalink
Refactor blocks for core loading.
Browse files Browse the repository at this point in the history
  • Loading branch information
jffng committed Jul 31, 2024
1 parent 57db29f commit c22c920
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 19 deletions.
4 changes: 4 additions & 0 deletions docs/reference-guides/core-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ This page lists the blocks included in the block-library package.
Accordion item content ([Source](https://github.com/WordPress/gutenberg/tree/trunk/packages/block-library/src/accordion-content))

- **Name:** core/accordion-content
- **Experimental:** true
- **Category:** design
- **Parent:** core/accordion-item
- **Supports:** border, color (background, gradient, text), interactivity, layout, shadow, spacing (blockGap, margin, padding), typography (fontSize, lineHeight)
Expand All @@ -23,6 +24,7 @@ Accordion item content ([Source](https://github.com/WordPress/gutenberg/tree/tru
A group of headers and associated expandable content. ([Source](https://github.com/WordPress/gutenberg/tree/trunk/packages/block-library/src/accordion-group))

- **Name:** core/accordion-group
- **Experimental:** true
- **Category:** design
- **Allowed Blocks:** core/accordion-item
- **Supports:** align (full, wide), background (backgroundImage, backgroundSize), color (background, gradient, text), interactivity, layout, shadow, spacing (blockGap, margin, padding), ~~html~~
Expand All @@ -33,6 +35,7 @@ A group of headers and associated expandable content. ([Source](https://github.c
A single accordion that displays a header and expandable content. ([Source](https://github.com/WordPress/gutenberg/tree/trunk/packages/block-library/src/accordion-item))

- **Name:** core/accordion-item
- **Experimental:** true
- **Category:** design
- **Parent:** core/accordion-group
- **Allowed Blocks:** core/accordion-trigger, core/accordion-content
Expand All @@ -44,6 +47,7 @@ A single accordion that displays a header and expandable content. ([Source](http
Accordion item trigger. ([Source](https://github.com/WordPress/gutenberg/tree/trunk/packages/block-library/src/accordion-trigger))

- **Name:** core/accordion-trigger
- **Experimental:** true
- **Category:** design
- **Parent:** core/accordion-item
- **Supports:** anchor, border, color (background, gradient, text), interactivity, layout, shadow, spacing (margin, padding), typography (fontSize, textAlign), ~~align~~
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/accordion-content/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
*/
import clsx from 'clsx';

export default function edit( { attributes } ) {
export default function Edit( { attributes } ) {
const { allowedBlocks, templateLock, openByDefault, isSelected } =
attributes;
const borderProps = useBorderProps( attributes );
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/accordion-group/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const ACCORDION_BLOCK = {
name: ACCORDION_BLOCK_NAME,
};

export default function edit( { attributes: { autoclose }, setAttributes } ) {
export default function Edit( { attributes: { autoclose }, setAttributes } ) {
const blockProps = useBlockProps();

const innerBlocksProps = useInnerBlocksProps( blockProps, {
Expand Down
11 changes: 7 additions & 4 deletions packages/block-library/src/accordion-group/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ import { useBlockProps, useInnerBlocksProps } from '@wordpress/block-editor';
export default function save( { attributes } ) {
const { iconPosition } = attributes;
const blockProps = useBlockProps.save();
const className = clsx( {
'icon-position-left': iconPosition === 'left',
}, blockProps.className );
const className = clsx(
{
'icon-position-left': iconPosition === 'left',
},
blockProps.className
);

return (
<div { ...useInnerBlocksProps.save( { ...blockProps, className } ) }/>
<div { ...useInnerBlocksProps.save( { ...blockProps, className } ) } />
);
}
2 changes: 1 addition & 1 deletion packages/block-library/src/accordion-group/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,4 @@
transform: rotate(90deg);
}
}
}
}
19 changes: 11 additions & 8 deletions packages/block-library/src/accordion-item/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,28 @@ import { PanelBody, ToggleControl } from '@wordpress/components';
*/
import clsx from 'clsx';

export default function edit( {
export default function Edit( {
attributes: { openByDefault },
clientId,
setAttributes,
} ) {
/* eslint-disable-next-line no-shadow */
const [ isSelected, getBlockOrder ] = useSelect(
const isSelected = useSelect(
( select ) => {
const { isBlockSelected, hasSelectedInnerBlock, getBlockOrder } =
const { isBlockSelected, hasSelectedInnerBlock } =
select( blockEditorStore );
return [
return (
isBlockSelected( clientId ) ||
hasSelectedInnerBlock( clientId, true ),
getBlockOrder,
];
hasSelectedInnerBlock( clientId, true )
);
},
[ clientId ]
);

const getBlockOrder = useSelect(
( select ) => select( blockEditorStore ).getBlockOrder,
[]
);

const contentBlockClientId = getBlockOrder( clientId )[ 1 ];
const { updateBlockAttributes, __unstableMarkNextChangeAsNotPersistent } =
useDispatch( blockEditorStore );
Expand Down
13 changes: 10 additions & 3 deletions packages/block-library/src/accordion-item/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,23 @@
* WordPress dependencies
*/
import { useBlockProps, useInnerBlocksProps } from '@wordpress/block-editor';
/**
* External dependencies
*/
import clsx from 'clsx';

export default function save( { attributes } ) {
const { openByDefault } = attributes;
const blockProps = useBlockProps.save();
const className = clsx(
{
'is-open': openByDefault,
},
blockProps.className
);
const innerBlocksProps = useInnerBlocksProps.save( {
...blockProps,
className: clsx( blockProps.className, {
'is-open': openByDefault,
} ),
className,
} );

return <div { ...innerBlocksProps } />;
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/accordion-trigger/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const ICONS = {
caret,
};

export default function edit( { attributes, setAttributes } ) {
export default function Edit( { attributes, setAttributes } ) {
const { level, title, textAlign, icon, iconPosition } = attributes;
const TagName = 'h' + level;

Expand Down

0 comments on commit c22c920

Please sign in to comment.