Skip to content

Commit

Permalink
Add initial accordion implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
jffng committed Jul 30, 2024
1 parent 602b5fa commit 5519d7e
Show file tree
Hide file tree
Showing 32 changed files with 1,493 additions and 7 deletions.
41 changes: 41 additions & 0 deletions docs/reference-guides/core-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,47 @@ This page lists the blocks included in the block-library package.

<!-- START TOKEN Autogenerated - DO NOT EDIT -->

## Content

Accordion item content ([Source](https://github.com/WordPress/gutenberg/tree/trunk/packages/block-library/src/accordion-content))

- **Name:** core/accordion-content
- **Category:** design
- **Parent:** core/accordion-item
- **Supports:** border, color (background, gradient, text), interactivity, layout, shadow, spacing (blockGap, margin, padding), typography (fontSize, lineHeight)
- **Attributes:** allowedBlocks, isSelected, openByDefault, templateLock

## Accordion Group

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
- **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~~
- **Attributes:** allowedBlocks, autoclose, iconPosition

## Accordion

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
- **Category:** design
- **Parent:** core/accordion-group
- **Allowed Blocks:** core/accordion-trigger, core/accordion-content
- **Supports:** align (full, wide), border, color (background, gradient, text), interactivity, layout, shadow, spacing (blockGap, margin)
- **Attributes:** openByDefault

## Trigger

Accordion item trigger. ([Source](https://github.com/WordPress/gutenberg/tree/trunk/packages/block-library/src/accordion-trigger))

- **Name:** core/accordion-trigger
- **Category:** design
- **Parent:** core/accordion-item
- **Supports:** anchor, border, color (background, gradient, text), interactivity, layout, shadow, spacing (margin, padding), typography (fontSize, textAlign), ~~align~~
- **Attributes:** icon, iconPosition, level, openByDefault, textAlignment, title

## Archives

Display a date archive of your posts. ([Source](https://github.com/WordPress/gutenberg/tree/trunk/packages/block-library/src/archives))
Expand Down
6 changes: 6 additions & 0 deletions lib/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ function gutenberg_reregister_core_block_types() {
$blocks_dirs = array(
__DIR__ . '/../build/block-library/blocks/' => array(
'block_folders' => array(
'accordion-content',
'accordion-group',
'accordion-item',
'accordion-trigger',
'audio',
'button',
'buttons',
Expand Down Expand Up @@ -45,6 +49,8 @@ function gutenberg_reregister_core_block_types() {
'embed',
),
'block_names' => array(
'accordion-group.php' => 'core/accordion-group',
'accordion-item.php' => 'core/accordion-item',
'archives.php' => 'core/archives',
'avatar.php' => 'core/avatar',
'block.php' => 'core/block',
Expand Down
24 changes: 17 additions & 7 deletions lib/experimental/editor-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,34 @@ function gutenberg_enable_experiments() {
if ( $gutenberg_experiments && array_key_exists( 'gutenberg-zoomed-out-patterns-tab', $gutenberg_experiments ) ) {
wp_add_inline_script( 'wp-block-editor', 'window.__experimentalEnableZoomedOutPatternsTab = true', 'before' );
}
if ( $gutenberg_experiments && array_key_exists( 'gutenberg-quick-edit-dataviews', $gutenberg_experiments ) ) {
wp_add_inline_script( 'wp-block-editor', 'window.__experimentalQuickEditDataViews = true', 'before' );
}
if ( $gutenberg_experiments && array_key_exists( 'gutenberg-block-bindings-ui', $gutenberg_experiments ) ) {
wp_add_inline_script( 'wp-block-editor', 'window.__experimentalBlockBindingsUI = true', 'before' );
}
}

add_action( 'admin_init', 'gutenberg_enable_experiments' );

/**
* Sets a global JS variable used to trigger the availability of form & input blocks.
*
* @deprecated 18.8.0 Use gutenberg_enable_block_experiments().
*/
function gutenberg_enable_form_input_blocks() {
_deprecated_function( __FUNCTION__, 'Gutenberg 18.8.0', 'gutenberg_enable_block_experiments' );
}

/**
* Sets global JS variables used to enable various block experiments.
*/
function gutenberg_enable_block_experiments() {
$gutenberg_experiments = get_option( 'gutenberg-experiments' );

// Experimental form blocks.
if ( $gutenberg_experiments && array_key_exists( 'gutenberg-form-blocks', $gutenberg_experiments ) ) {
wp_add_inline_script( 'wp-block-editor', 'window.__experimentalEnableFormBlocks = true', 'before' );
}

// General experimental blocks that are not in the default block library.
if ( $gutenberg_experiments && array_key_exists( 'gutenberg-block-experiments', $gutenberg_experiments ) ) {
wp_add_inline_script( 'wp-block-editor', 'window.__experimentalEnableBlockExperiments = true', 'before' );
}
}

add_action( 'admin_init', 'gutenberg_enable_form_input_blocks' );
add_action( 'admin_init', 'gutenberg_enable_block_experiments' );

Check failure on line 64 in lib/experimental/editor-settings.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Expected 1 newline at end of file; 0 found
12 changes: 12 additions & 0 deletions lib/experiments-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,18 @@ function gutenberg_initialize_experiments_settings() {
)
);

add_settings_field(
'gutenberg-block-experiments',
__( 'Experimental blocks', 'gutenberg' ),
'gutenberg_display_experiment_field',
'gutenberg-experiments',
'gutenberg_experiments_section',
array(
'label' => __( 'Enable experimental blocks.<p class="description">(Warning: these blocks may have significant changes during development that cause validation errors and display issues.)</p>', 'gutenberg' ),
'id' => 'gutenberg-block-experiments',
)
);

add_settings_field(
'gutenberg-form-blocks',
__( 'Form and input blocks ', 'gutenberg' ),
Expand Down
74 changes: 74 additions & 0 deletions packages/block-library/src/accordion-content/block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
{
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 3,
"name": "core/accordion-content",
"version": "0.1.0",
"title": "Content",
"category": "design",
"description": "Accordion item content",
"example": {},
"parent": [ "core/accordion-item" ],
"supports": {
"color": {
"background": true,
"gradient": true
},
"border": true,
"interactivity": true,
"spacing": {
"padding": true,
"margin": [ "top", "bottom" ],
"blockGap": true,
"__experimentalDefaultControls": {
"padding": true,
"blockGap": true
}
},
"__experimentalBorder": {
"color": true,
"radius": true,
"style": true,
"width": true,
"__experimentalDefaultControls": {
"color": true,
"radius": true,
"style": true,
"width": true
}
},
"typography": {
"fontSize": true,
"lineHeight": true,
"__experimentalFontFamily": true,
"__experimentalFontWeight": true,
"__experimentalFontStyle": true,
"__experimentalTextTransform": true,
"__experimentalTextDecoration": true,
"__experimentalLetterSpacing": true,
"__experimentalDefaultControls": {
"fontSize": true
}
},
"shadow": true,
"layout": true
},
"attributes": {
"allowedBlocks": {
"type": "array"
},
"templateLock": {
"type": [ "string", "boolean" ],
"enum": [ "all", "insert", "contentOnly", false ],
"default": false
},
"openByDefault": {
"type": "boolean",
"default": false
},
"isSelected": {
"type": "boolean",
"default": false
}
},
"textdomain": "default"
}
61 changes: 61 additions & 0 deletions packages/block-library/src/accordion-content/edit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* WordPress dependencies
*/
import {
useBlockProps,
useInnerBlocksProps,
__experimentalUseBorderProps as useBorderProps,
__experimentalUseColorProps as useColorProps,
__experimentalGetSpacingClassesAndStyles as useSpacingProps,
__experimentalGetShadowClassesAndStyles as useShadowProps,
} from '@wordpress/block-editor';
/**
* External dependencies
*/
import clsx from 'clsx';

export default function Edit( { attributes } ) {
const { allowedBlocks, templateLock, openByDefault, isSelected } =
attributes;
const borderProps = useBorderProps( attributes );
const colorProps = useColorProps( attributes );
const spacingProps = useSpacingProps( attributes );
const shadowProps = useShadowProps( attributes );

const blockProps = useBlockProps();
const innerBlocksProps = useInnerBlocksProps(
{
className: 'accordion-content__wrapper',
style: {
...spacingProps.style,
},
},
{
allowedBlocks,
template: [ [ 'core/paragraph', {} ] ],
templateLock,
}
);

return (
<div
{ ...blockProps }
className={ clsx(
blockProps.className,
colorProps.className,
borderProps.className,
{
[ `has-custom-font-size` ]: blockProps?.style?.fontSize,
}
) }
style={ {
...borderProps.style,
...colorProps.style,
...shadowProps.style,
} }
aria-hidden={ ! isSelected && ! openByDefault }
>
<div { ...innerBlocksProps } />
</div>
);
}
41 changes: 41 additions & 0 deletions packages/block-library/src/accordion-content/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* WordPress dependencies
*/
import { SVG, Path } from '@wordpress/components';
/**
* Internal dependencies
*/
import Edit from './edit';
import save from './save';
import metadata from './block.json';
import initBlock from '../utils/init-block';

const icon = (
<SVG
width="24"
height="24"
viewBox="0 0 24 24"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<Path
fillRule="evenodd"
clipRule="evenodd"
d="M8.10417 6.00024H6.5C5.39543 6.00024 4.5 6.89567 4.5 8.00024V10.3336H6V8.00024C6 7.7241 6.22386 7.50024 6.5 7.50024H8.10417V6.00024ZM4.5 13.6669V16.0002C4.5 17.1048 5.39543 18.0002 6.5 18.0002H8.10417V16.5002H6.5C6.22386 16.5002 6 16.2764 6 16.0002V13.6669H4.5ZM10.3958 6.00024V7.50024H13.6042V6.00024H10.3958ZM15.8958 6.00024V7.50024H17.5C17.7761 7.50024 18 7.7241 18 8.00024V10.3336H19.5V8.00024C19.5 6.89567 18.6046 6.00024 17.5 6.00024H15.8958ZM19.5 13.6669H18V16.0002C18 16.2764 17.7761 16.5002 17.5 16.5002H15.8958V18.0002H17.5C18.6046 18.0002 19.5 17.1048 19.5 16.0002V13.6669ZM13.6042 18.0002V16.5002H10.3958V18.0002H13.6042Z"
fill="currentColor"
/>
</SVG>
);

const { name } = metadata;

export { metadata, name };

export const settings = {
icon,
example: {},
Edit,
save,
};

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

export default init();
51 changes: 51 additions & 0 deletions packages/block-library/src/accordion-content/save.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* WordPress dependencies
*/
import {
InnerBlocks,
useBlockProps,
__experimentalGetBorderClassesAndStyles as getBorderClassesAndStyles,
__experimentalGetColorClassesAndStyles as getColorClassesAndStyles,
__experimentalGetSpacingClassesAndStyles as getSpacingClassesAndStyles,
__experimentalGetShadowClassesAndStyles as getShadowClassesAndStyles,
} from '@wordpress/block-editor';
/**
* External dependencies
*/
import clsx from 'clsx';

export default function save( { attributes } ) {
const blockProps = useBlockProps.save();
const borderProps = getBorderClassesAndStyles( attributes );
const colorProps = getColorClassesAndStyles( attributes );
const spacingProps = getSpacingClassesAndStyles( attributes );
const shadowProps = getShadowClassesAndStyles( attributes );

return (
<div
{ ...blockProps }
className={ clsx(
blockProps.className,
colorProps.className,
borderProps.className,
{
[ `has-custom-font-size` ]: blockProps?.style?.fontSize,
}
) }
style={ {
...borderProps.style,
...colorProps.style,
...shadowProps.style,
} }
>
<div
className="accordion-content__wrapper"
style={ {
...spacingProps.style,
} }
>
<InnerBlocks.Content />
</div>
</div>
);
}
Loading

0 comments on commit 5519d7e

Please sign in to comment.