From b3e7b4e8990f8a33ec39b06c72df6ad7d6bf25d4 Mon Sep 17 00:00:00 2001 From: Faisal Ahammad Date: Fri, 13 Nov 2026 23:09:39 +0600 Subject: [PATCH] feat(icon-list): add nested sub-list support Items can now hold a nested Icon List. The sub-list is added from the item toolbar or the button under the item text, and it starts with the parent list defaults for icons, colors and sizes. - New Sub-list Indent control in Dimensions. Nested lists inherit the value live from the parent, and a sub-list can override it for deeper levels. - Lists and items get list/listitem roles. Items without a sub-list keep the exact previous markup, and old content loads through deprecations without warnings. - Divider and vertical layout rules are scoped per list level, in the editor and on the frontend. - Backspace merge moves the sub-list to the previous item instead of dropping it. - BlockAppender accepts seed attributes for the inserted block. Fixes #3132 --- inc/css/blocks/class-icon-list-css.php | 15 +++ src/blocks/blocks/icon-list/block.json | 3 + src/blocks/blocks/icon-list/deprecated.js | 34 ++++++ src/blocks/blocks/icon-list/edit.js | 1 + src/blocks/blocks/icon-list/editor.scss | 35 +++++- src/blocks/blocks/icon-list/index.js | 2 + src/blocks/blocks/icon-list/inspector.js | 16 +++ .../blocks/icon-list/item/deprecated.js | 52 ++++++++ src/blocks/blocks/icon-list/item/edit.js | 83 ++++++++++++- src/blocks/blocks/icon-list/item/save.js | 13 +- src/blocks/blocks/icon-list/save.js | 3 +- src/blocks/blocks/icon-list/style.scss | 114 +++++++++++------- src/blocks/blocks/icon-list/sublist.js | 39 ++++++ src/blocks/blocks/icon-list/types.d.ts | 1 + .../block-appender-button/index.tsx | 5 +- src/blocks/test/e2e/blocks/icon-list.spec.js | 97 +++++++++++++++ .../test/unit/icon-list-sublist.test.js | 50 ++++++++ tests/test-icon-list-css.php | 74 ++++++++++++ 18 files changed, 582 insertions(+), 55 deletions(-) create mode 100644 src/blocks/blocks/icon-list/deprecated.js create mode 100644 src/blocks/blocks/icon-list/sublist.js create mode 100644 src/blocks/test/unit/icon-list-sublist.test.js create mode 100644 tests/test-icon-list-css.php diff --git a/inc/css/blocks/class-icon-list-css.php b/inc/css/blocks/class-icon-list-css.php index daaf310c4..7021c8a44 100644 --- a/inc/css/blocks/class-icon-list-css.php +++ b/inc/css/blocks/class-icon-list-css.php @@ -68,6 +68,21 @@ public function render_css( $block ) { 'property' => '--gap-icon-label', 'value' => 'gapIconLabel', ), + array( + 'property' => '--sublist-indent', + 'value' => 'sublistIndent', + 'unit' => 'px', + 'condition' => function ( $attrs ) { + return isset( $attrs['sublistIndent'] ) && is_numeric( $attrs['sublistIndent'] ); + }, + ), + array( + 'property' => '--sublist-indent', + 'value' => 'sublistIndent', + 'condition' => function ( $attrs ) { + return isset( $attrs['sublistIndent'] ) && is_string( $attrs['sublistIndent'] ) && ! is_numeric( $attrs['sublistIndent'] ); + }, + ), array( 'property' => '--content-color', 'value' => 'defaultContentColor', diff --git a/src/blocks/blocks/icon-list/block.json b/src/blocks/blocks/icon-list/block.json index f3321b044..a869a5145 100644 --- a/src/blocks/blocks/icon-list/block.json +++ b/src/blocks/blocks/icon-list/block.json @@ -47,6 +47,9 @@ "gapIconLabel": { "type": ["string"] }, + "sublistIndent": { + "type": ["number", "string"] + }, "horizontalAlign": { "type": "string" }, diff --git a/src/blocks/blocks/icon-list/deprecated.js b/src/blocks/blocks/icon-list/deprecated.js new file mode 100644 index 000000000..49c664177 --- /dev/null +++ b/src/blocks/blocks/icon-list/deprecated.js @@ -0,0 +1,34 @@ +/** + * WordPress dependencies + */ +import { + InnerBlocks, + useBlockProps +} from '@wordpress/block-editor'; + +import classNames from 'classnames'; + +import metadata from './block.json'; + +const deprecated = [{ + // Markup before sub-list support, which added the list role. + attributes: { ...metadata.attributes }, + + save: ({ + attributes, + className + }) => { + const blockProps = useBlockProps.save({ + id: attributes.id, + className: classNames( className, { 'has-divider': Boolean( attributes.hasDivider ) }) + }); + + return ( +
+ +
+ ); + } +}]; + +export default deprecated; diff --git a/src/blocks/blocks/icon-list/edit.js b/src/blocks/blocks/icon-list/edit.js index 87fa7ea22..19044d0b6 100644 --- a/src/blocks/blocks/icon-list/edit.js +++ b/src/blocks/blocks/icon-list/edit.js @@ -49,6 +49,7 @@ const Edit = ({ '--icon-align-mobile': attributes.alignmentMobile, '--gap': _px( attributes.gap ), '--gap-icon-label': attributes.gapIconLabel, + '--sublist-indent': _px( attributes.sublistIndent ), '--font-size': _px( attributes.defaultSize ), '--icon-size': attributes.defaultIconSize, '--label-visibility': attributes.hideLabels ? 'none' : undefined, diff --git a/src/blocks/blocks/icon-list/editor.scss b/src/blocks/blocks/icon-list/editor.scss index fb3740c56..ac3a9c138 100644 --- a/src/blocks/blocks/icon-list/editor.scss +++ b/src/blocks/blocks/icon-list/editor.scss @@ -23,13 +23,27 @@ align-items: var( --icon-align-tablet ); justify-content: var( --icon-align-tablet ); } - + @media (max-width: 600px) { align-items: var( --icon-align-mobile ); justify-content: var( --icon-align-mobile ); } } + // The editor nests items inside the inner blocks wrappers, so mirror the + // direct-child rules from style.scss for the editor DOM. + &:not(.is-style-horizontal) > .block-editor-inner-blocks > .block-editor-block-list__layout > .wp-block-themeisle-blocks-icon-list-item { + @include icon-list-item-vertical; + } + + &.has-divider:not(.is-style-horizontal) > .block-editor-inner-blocks > .block-editor-block-list__layout > .wp-block-themeisle-blocks-icon-list-item:not(:last-child)::after { + @include icon-list-divider-vertical; + } + + &.is-style-horizontal.has-divider > .block-editor-inner-blocks > .block-editor-block-list__layout > .wp-block-themeisle-blocks-icon-list-item:not(:last-child)::after { + @include icon-list-divider-horizontal; + } + &.is-selected.has-divider { &.is-style-horizontal, &:not(.is-style-horizontal) { .wp-block-themeisle-blocks-icon-list-item:nth-last-child(2)::after { @@ -39,6 +53,25 @@ } } +// Sub-list area inside an item: hidden while empty, on its own line once filled. +.wp-block-themeisle-blocks-icon-list .wp-block-themeisle-blocks-icon-list-item { + flex-wrap: wrap; + + > .block-editor-inner-blocks { + display: none; + } + + &.has-sublist > .block-editor-inner-blocks { + display: block; + flex-basis: 100%; + width: 100%; + } + + > .o-block-appender-button { + flex-basis: 100%; + } +} + .editor-styles-wrapper .wp-block.wp-block-themeisle-blocks-icon-list-item, .editor-styles-wrapper .block-editor-block-list__layout.is-root-container p.wp-block-themeisle-blocks-icon-list-item-content { /* We add this to override Neve styles */ diff --git a/src/blocks/blocks/icon-list/index.js b/src/blocks/blocks/icon-list/index.js index d209be2a7..ea0242855 100644 --- a/src/blocks/blocks/icon-list/index.js +++ b/src/blocks/blocks/icon-list/index.js @@ -10,6 +10,7 @@ import { registerBlockType } from '@wordpress/blocks'; */ import metadata from './block.json'; import { iconListIcon as icon } from '../../helpers/icons.js'; +import deprecated from './deprecated.js'; import edit from './edit.js'; import save from './save.js'; import './item/index.js'; @@ -26,6 +27,7 @@ registerBlockType( name, { 'list', 'items' ], + deprecated, edit, save, example: { diff --git a/src/blocks/blocks/icon-list/inspector.js b/src/blocks/blocks/icon-list/inspector.js index 838627b06..d5d925aeb 100644 --- a/src/blocks/blocks/icon-list/inspector.js +++ b/src/blocks/blocks/icon-list/inspector.js @@ -331,6 +331,22 @@ const Inspector = ({ } ]} /> + +
+ + setAttributes({ sublistIndent }) } + units={[ + { + a11yLabel: 'Pixels (px)', + label: 'px', + step: 1, + value: 'px' + } + ]} + /> { + const iconClassName = `${ attributes.iconPrefix } fa-${ attributes.icon }`; + const Icon = themeIsleIcons.icons[ attributes.icon ]; + + const blockProps = useBlockProps.save({ + id: attributes.id + }); + + return ( +
+ { + 'image' === attributes.library && attributes.icon ? ( + { + ) : ( + 'themeisle-icons' === attributes.library && attributes.icon ? ( + + ) : ( + + ) + ) + } + + + +
+ ); + } +}, { attributes, migrate: oldAttributes => ({ diff --git a/src/blocks/blocks/icon-list/item/edit.js b/src/blocks/blocks/icon-list/item/edit.js index f0429aa31..58bec80f3 100644 --- a/src/blocks/blocks/icon-list/item/edit.js +++ b/src/blocks/blocks/icon-list/item/edit.js @@ -9,11 +9,17 @@ import classnames from 'classnames'; import { __, sprintf } from '@wordpress/i18n'; import { + BlockControls, + InnerBlocks, RichText, useBlockProps } from '@wordpress/block-editor'; -import { select, useSelect } from '@wordpress/data'; +import { ToolbarButton } from '@wordpress/components'; + +import { createBlock } from '@wordpress/blocks'; + +import { dispatch, select, useDispatch, useSelect } from '@wordpress/data'; import { Fragment, @@ -21,6 +27,8 @@ import { useState } from '@wordpress/element'; +import { formatListBullets } from '@wordpress/icons'; + /** * Internal dependencies */ @@ -28,6 +36,8 @@ import metadata from './block.json'; import Inspector from './inspector.js'; import themeIsleIcons from './../../../helpers/themeisle-icons.js'; import { blockInit } from '../../../helpers/block-utility.js'; +import BlockAppender from '../../../components/block-appender-button'; +import { getSubListAttributes } from '../sublist.js'; const { attributes: defaultAttributes } = metadata; @@ -40,6 +50,7 @@ const Edit = ({ attributes, setAttributes, clientId, + isSelected, onReplace, onRemove, mergeBlocks @@ -47,9 +58,12 @@ const Edit = ({ const [ isURL, setIsUrl ] = useState( false ); + const { insertBlock } = useDispatch( 'core/block-editor' ); + const { hasParent, - parentAttributes + parentAttributes, + innerBlockCount } = useSelect( select => { const { getBlock, @@ -61,7 +75,8 @@ const Edit = ({ return { hasParent: parentBlock ? true : false, - parentAttributes: parentBlock ? parentBlock.attributes : {} + parentAttributes: parentBlock ? parentBlock.attributes : {}, + innerBlockCount: getBlock( clientId )?.innerBlocks.length ?? 0 }; }, []); @@ -97,7 +112,8 @@ const Edit = ({ useEffect( () => { if ( attributes.content === undefined ) { - const parentClientId = select( 'core/block-editor' ).getBlockParents( clientId ).at( -1 ); + // The direct parent list, so items added inside a sub-list are numbered within it. + const parentClientId = select( 'core/block-editor' ).getBlockRootClientId( clientId ); const parentBlock = select( 'core/block-editor' ).getBlock( parentClientId ); // translators: %d: the number of List item blocks. @@ -109,13 +125,43 @@ const Edit = ({ const iconClassName = `${ attributes.iconPrefix || parentAttributes.defaultPrefix } fa-${ attributes.icon || parentAttributes.defaultIcon }`; + const hasSubList = 0 < innerBlockCount; + + const addSubList = () => { + insertBlock( createBlock( 'themeisle-blocks/icon-list', getSubListAttributes( parentAttributes ) ), innerBlockCount, clientId ); + }; + + // Backspace merging into the previous item would drop this item's sub-list, + // because core only merges attributes. Move it over beforehand instead. + const handleMerge = ( forward ) => { + if ( ! forward && hasSubList ) { + const { getBlock, getBlockRootClientId, getPreviousBlockClientId } = select( 'core/block-editor' ); + const previousClientId = getPreviousBlockClientId( clientId ); + + if ( previousClientId ) { + const innerClientIds = getBlock( clientId ).innerBlocks.map( ( { clientId: innerId } ) => innerId ); + dispatch( 'core/block-editor' ).moveBlocksToPosition( innerClientIds, clientId, previousClientId ); + + // Cancel the merge when a lock kept the sub-list from moving, so it is not deleted with the item. + const moved = innerClientIds.every( ( innerId ) => previousClientId === getBlockRootClientId( innerId ) ); + + if ( ! moved ) { + return; + } + } + } + + mergeBlocks( forward ); + }; + const inlineStyles = { '--content-color': attributes.contentColor ?? parentAttributes.defaultContentColor, '--icon-color': attributes.iconColor ?? parentAttributes.defaultIconColor }; const blockProps = useBlockProps({ - style: inlineStyles + style: inlineStyles, + className: hasSubList ? 'has-sublist' : undefined }); return ( @@ -125,6 +171,16 @@ const Edit = ({ setAttributes={ setAttributes } /> + + { ! hasSubList && ( + + ) } + +
{ 'image' === attributes.library && isURL ? ( { @@ -156,10 +212,25 @@ const Edit = ({ ) } value={ attributes.content } onChange={ ( content ) => setAttributes({ content }) } - onMerge={ mergeBlocks } + onMerge={ mergeBlocks ? handleMerge : undefined } onReplace={ onReplace } onRemove={ onRemove } /> + + + + { isSelected && ! hasSubList && ( + + ) }
); diff --git a/src/blocks/blocks/icon-list/item/save.js b/src/blocks/blocks/icon-list/item/save.js index 6dfa36d2a..6ecdf8c20 100644 --- a/src/blocks/blocks/icon-list/item/save.js +++ b/src/blocks/blocks/icon-list/item/save.js @@ -7,6 +7,7 @@ import classnames from 'classnames'; * WordPress dependencies */ import { + InnerBlocks, RichText, useBlockProps } from '@wordpress/block-editor'; @@ -17,13 +18,19 @@ import { import themeIsleIcons from './../../../helpers/themeisle-icons.js'; const Save = ({ - attributes + attributes, + innerBlocks }) => { const iconClassName = `${ attributes.iconPrefix } fa-${ attributes.icon }`; const Icon = themeIsleIcons.icons[ attributes.icon ]; + // The class is only present on items that hold a sub-list, so flat items keep serializing the same markup. + const hasSubList = Array.isArray( innerBlocks ) && 0 < innerBlocks.length; + const blockProps = useBlockProps.save({ - id: attributes.id + id: attributes.id, + className: hasSubList ? 'has-sublist' : undefined, + role: 'listitem' }); return ( @@ -60,6 +67,8 @@ const Save = ({ ) } value={ attributes.content } /> + + ); }; diff --git a/src/blocks/blocks/icon-list/save.js b/src/blocks/blocks/icon-list/save.js index 29a4db3d2..fe9358ea4 100644 --- a/src/blocks/blocks/icon-list/save.js +++ b/src/blocks/blocks/icon-list/save.js @@ -13,7 +13,8 @@ const Save = ({ }) => { const blockProps = useBlockProps.save({ id: attributes.id, - className: classNames( className, { 'has-divider': Boolean( attributes.hasDivider ) }) + className: classNames( className, { 'has-divider': Boolean( attributes.hasDivider ) }), + role: 'list' }); return ( diff --git a/src/blocks/blocks/icon-list/style.scss b/src/blocks/blocks/icon-list/style.scss index 8ec0ac0b5..f1409d0c5 100644 --- a/src/blocks/blocks/icon-list/style.scss +++ b/src/blocks/blocks/icon-list/style.scss @@ -1,12 +1,58 @@ +// Dividers and vertical item rules are scoped to direct children so a sub-list +// styled by its own settings, never by the list it sits in. The editor nests its +// items inside wrapper elements, so editor.scss mirrors these with its own selectors. +@mixin icon-list-divider-vertical { + border-bottom: var(--divider-width) solid var(--divider-color); + content: ""; + position: absolute; + height: 100%; + width: var(--divider-length); + box-sizing: content-box; + margin-left: var(--divider-margin-left); + margin-right: var(--divider-margin-right); + + @media (min-width: 600px) and (max-width: 960px) { + margin-left: var(--divider-margin-left-tablet); + margin-right: var(--divider-margin-right-tablet); + } + + @media (max-width: 600px) { + margin-left: var(--divider-margin-left-mobile); + margin-right: var(--divider-margin-right-mobile); + } +} + +@mixin icon-list-divider-horizontal { + border-right: var(--divider-width) solid var(--divider-color); + content: ""; + position: absolute; + height: var(--divider-length); + width: 100%; + box-sizing: content-box; +} + +@mixin icon-list-item-vertical { + width: 100%; + justify-content: var( --icon-align ); + + @media (min-width: 600px) and (max-width: 960px) { + justify-content: var( --icon-align-tablet ); + } + + @media (max-width: 600px) { + justify-content: var( --icon-align-mobile ); + } +} + .wp-block-themeisle-blocks-icon-list { --horizontal-align: unset; // legacy --icon-align: var(--horizontal-align); --icon-align-tablet: var(--icon-align-tablet); --icon-align-mobile: var(--icon-align-mobile); - --gap: 5px; - --gap-icon-label: 10px; - --font-size: inherit; + --gap: 5px; + --gap-icon-label: 10px; + --font-size: inherit; --icon-size: 12px; --content-color: inherit; --label-visibility: block; @@ -58,59 +104,39 @@ } } + .wp-block-themeisle-blocks-icon-list-item.has-sublist { + flex-wrap: wrap; + + > .wp-block-themeisle-blocks-icon-list { + flex-basis: 100%; + } + } + + // The indent is inherited from the parent list, so its control moves + // existing sub-lists live. A sub-list can override it for deeper levels. + // Declaring a default here would block that inheritance. + .wp-block-themeisle-blocks-icon-list { + padding-inline-start: var(--sublist-indent, 32px); + } + &.is-style-horizontal { flex-wrap: wrap; flex-direction: row; width: 100%; - &.has-divider .wp-block-themeisle-blocks-icon-list-item:not(:last-child)::after { - border-right: var(--divider-width) solid var(--divider-color); - content: ""; - position: absolute; - // bottom: 0; - // right: 0; - height: var(--divider-length); - width: 100%; - box-sizing: content-box; + &.has-divider > .wp-block-themeisle-blocks-icon-list-item:not(:last-child)::after { + @include icon-list-divider-horizontal; } } &:not(.is-style-horizontal) { - .wp-block-themeisle-blocks-icon-list-item { - width: 100%; - justify-content: var( --icon-align ); - - @media (min-width: 600px) and (max-width: 960px) { - justify-content: var( --icon-align-tablet ); - } - - @media (max-width: 600px) { - justify-content: var( --icon-align-mobile ); - } + > .wp-block-themeisle-blocks-icon-list-item { + @include icon-list-item-vertical; } - &.has-divider .wp-block-themeisle-blocks-icon-list-item:not(:last-child)::after { - border-bottom: var(--divider-width) solid var(--divider-color); - content: ""; - position: absolute; - // bottom: 0; - // left: 0; - height: 100%; - width: var(--divider-length); - box-sizing: content-box; - margin-left: var(--divider-margin-left); - margin-right: var(--divider-margin-right); - - @media (min-width: 600px) and (max-width: 960px) { - margin-left: var(--divider-margin-left-tablet); - margin-right: var(--divider-margin-right-tablet); - } - - @media (max-width: 600px) { - margin-left: var(--divider-margin-left-mobile); - margin-right: var(--divider-margin-right-mobile); - } + &.has-divider > .wp-block-themeisle-blocks-icon-list-item:not(:last-child)::after { + @include icon-list-divider-vertical; } } diff --git a/src/blocks/blocks/icon-list/sublist.js b/src/blocks/blocks/icon-list/sublist.js new file mode 100644 index 000000000..fe45d30e3 --- /dev/null +++ b/src/blocks/blocks/icon-list/sublist.js @@ -0,0 +1,39 @@ +/** + * Attributes copied from a parent Icon List onto a sub-list inserted in + * one of its items, so nested structures start out consistent. Values + * that are not set are skipped and the block defaults apply. + * + * The sub-list indent is deliberately not copied: it reaches nested lists + * live from the parent through CSS inheritance, and a frozen copy would + * detach the sub-list from the parent's control. + * + * @param {Object} [parentAttributes] Attributes of the parent Icon List. + * @return {Object} Attributes for the new Icon List block. + */ +export const getSubListAttributes = ( parentAttributes ) => { + if ( ! parentAttributes ) { + return {}; + } + + const keys = [ + 'defaultLibrary', + 'defaultPrefix', + 'defaultIcon', + 'defaultIconId', + 'defaultIconAlt', + 'defaultContentColor', + 'defaultIconColor', + 'defaultSize', + 'defaultIconSize' + ]; + + const attributes = {}; + + keys.forEach( key => { + if ( undefined !== parentAttributes[ key ] && null !== parentAttributes[ key ] ) { + attributes[ key ] = parentAttributes[ key ]; + } + } ); + + return attributes; +}; diff --git a/src/blocks/blocks/icon-list/types.d.ts b/src/blocks/blocks/icon-list/types.d.ts index 23dc47aef..46e9dd412 100644 --- a/src/blocks/blocks/icon-list/types.d.ts +++ b/src/blocks/blocks/icon-list/types.d.ts @@ -12,6 +12,7 @@ type Attributes= { defaultIconSize: number defaultSize: number | string gap: number | string + sublistIndent: number | string horizontalAlign: string alignmentTablet: string alignmentMobile: string diff --git a/src/blocks/components/block-appender-button/index.tsx b/src/blocks/components/block-appender-button/index.tsx index f72f81706..48db2cfd8 100644 --- a/src/blocks/components/block-appender-button/index.tsx +++ b/src/blocks/components/block-appender-button/index.tsx @@ -20,6 +20,7 @@ type BlockAppenderButtonProps = { buttonText?: string, clientId: string, allowedBlock: string, + attributes?: Record, } & Button.Props; /** @@ -29,12 +30,14 @@ type BlockAppenderButtonProps = { * @param {string} params.buttonText - The text to display in the button. * @param {string} params.clientId - Root client ID. * @param {string} params.allowedBlock - The allowed block type. + * @param {Object} params.attributes - Attributes to seed the new block with. * @param {Object} params.props - Additional button props. */ const BlockAppender = ({ buttonText = __( 'Add Item', 'otter-blocks' ), clientId, allowedBlock, + attributes, ...props }: BlockAppenderButtonProps ) => { const { insertBlock } = useDispatch( 'core/block-editor' ); @@ -44,7 +47,7 @@ const BlockAppender = ({ const onClick = () => { const { innerBlocks } = getBlock( clientId ); - const newBlock = createBlock( allowedBlock ); + const newBlock = createBlock( allowedBlock, attributes ); insertBlock( newBlock, innerBlocks.length, clientId ); }; diff --git a/src/blocks/test/e2e/blocks/icon-list.spec.js b/src/blocks/test/e2e/blocks/icon-list.spec.js index 277b80a81..04f44b2f9 100644 --- a/src/blocks/test/e2e/blocks/icon-list.spec.js +++ b/src/blocks/test/e2e/blocks/icon-list.spec.js @@ -50,4 +50,101 @@ test.describe( 'Icon List', () => { expect( after[ 0 ].attributes.content ).toBe( 'AAA' ); expect( after[ 1 ].attributes.content ).toBe( 'BBB' ); }); + + // Issue #3132: a sub-list inserted in an item must be a real Icon List that + // inherits the icon defaults of the list it sits in, so nesting keeps the + // look of the parent without extra configuration. + test( 'adds a sub-list to an item, inheriting the parent list defaults', async({ editor }) => { + await editor.insertBlock({ name: 'themeisle-blocks/icon-list', attributes: { defaultIcon: 'star' }}); + + await editor.canvas.getByText( 'List item 1' ).click(); + await editor.canvas.getByRole( 'button', { name: 'Add Sub-list' } ).click(); + + const blocks = await editor.getBlocks(); + const list = blocks.find( ( b ) => 'themeisle-blocks/icon-list' === b.name ); + const item = list.innerBlocks[ 0 ]; + + expect( item.innerBlocks.length ).toBe( 1 ); + + const subList = item.innerBlocks[ 0 ]; + expect( subList.name ).toBe( 'themeisle-blocks/icon-list' ); + expect( subList.attributes.defaultIcon ).toBe( 'star' ); + + // The sub-list behaves like any Icon List: its InnerBlocks template fills in. + expect( subList.innerBlocks.length ).toBe( 3 ); + }); + + // The indent reaches nested lists through CSS inheritance from the parent + // list, so it updates live and is not a copy frozen at creation time. + test( 'applies the parent sub-list indent to nested lists', async({ editor, page }) => { + await editor.insertBlock({ name: 'themeisle-blocks/icon-list', attributes: { sublistIndent: 64 }}); + + await editor.canvas.getByText( 'List item 1' ).click(); + await editor.canvas.getByRole( 'button', { name: 'Add Sub-list' } ).click(); + + const nestedList = editor.canvas.locator( '.wp-block-themeisle-blocks-icon-list .wp-block-themeisle-blocks-icon-list-item .wp-block-themeisle-blocks-icon-list' ).first(); + await expect( nestedList ).toHaveCSS( 'padding-inline-start', '64px' ); + + // Moving the parent's control must move the existing sub-list. + await page.evaluate( () => { + const { getBlocks } = window.wp.data.select( 'core/block-editor' ); + const { updateBlockAttributes } = window.wp.data.dispatch( 'core/block-editor' ); + const list = getBlocks().find( ( block ) => 'themeisle-blocks/icon-list' === block.name ); + updateBlockAttributes( list.clientId, { sublistIndent: 80 } ); + }); + + await expect( nestedList ).toHaveCSS( 'padding-inline-start', '80px' ); + }); + + test( 'keeps the default sub-list indent when unset', async({ editor }) => { + await editor.insertBlock({ name: 'themeisle-blocks/icon-list' }); + + await editor.canvas.getByText( 'List item 1' ).click(); + await editor.canvas.getByRole( 'button', { name: 'Add Sub-list' } ).click(); + + const nestedList = editor.canvas.locator( '.wp-block-themeisle-blocks-icon-list .wp-block-themeisle-blocks-icon-list-item .wp-block-themeisle-blocks-icon-list' ).first(); + await expect( nestedList ).toHaveCSS( 'padding-inline-start', '32px' ); + }); + + test( 'serializes sub-lists inside items with list roles', async({ editor }) => { + await editor.insertBlock({ name: 'themeisle-blocks/icon-list' }); + + await editor.canvas.getByText( 'List item 2' ).click(); + await editor.canvas.getByRole( 'button', { name: 'Add Sub-list' } ).click(); + + const content = await editor.getEditedPostContent(); + + expect( content ).toContain( 'has-sublist' ); + expect( content ).toMatch( /]+role="list"/ ); + expect( content ).toMatch( /]+role="listitem"/ ); + + // The nested block delimiter belongs to the sub-list. The trailing space + // keeps item delimiters out of the count. + expect( content.split( '', + '
', + '

Flat item

', + '', + '', + '

Image item

', + '
', + '' + ].join( '\n' ); + + await page.evaluate( content => window.wp.data.dispatch( 'core/editor' ).editPost( { content } ), markup ); + + const blocks = await editor.getBlocks(); + expect( blocks.map( ( b ) => b.name ) ).toContain( 'themeisle-blocks/icon-list' ); + + await expect( editor.canvas.locator( '.block-editor-warning' ) ).toHaveCount( 0); + await expect( editor.canvas.getByText( 'Flat item' ) ).toBeVisible(); + await expect( editor.canvas.getByText( 'Image item' ) ).toBeVisible(); + }); }); diff --git a/src/blocks/test/unit/icon-list-sublist.test.js b/src/blocks/test/unit/icon-list-sublist.test.js new file mode 100644 index 000000000..77db7e136 --- /dev/null +++ b/src/blocks/test/unit/icon-list-sublist.test.js @@ -0,0 +1,50 @@ +/** + * Internal dependencies + */ +import { getSubListAttributes } from '../../blocks/icon-list/sublist'; + +describe( 'Icon List sub-list seeding', () => { + test( 'copies the parent list defaults onto the new sub-list', () => { + const parentAttributes = { + id: 'abc', + defaultLibrary: 'themeisle-icons', + defaultPrefix: 'fas', + defaultIcon: 'star', + defaultIconId: 12, + defaultIconAlt: 'A star', + defaultContentColor: '#111', + defaultIconColor: '#222', + defaultSize: '20px', + defaultIconSize: '16px', + sublistIndent: '40px', + gap: 5 + }; + + expect( getSubListAttributes( parentAttributes ) ).toEqual( { + defaultLibrary: 'themeisle-icons', + defaultPrefix: 'fas', + defaultIcon: 'star', + defaultIconId: 12, + defaultIconAlt: 'A star', + defaultContentColor: '#111', + defaultIconColor: '#222', + defaultSize: '20px', + defaultIconSize: '16px' + } ); + + // The indent is not copied: a frozen copy would detach the sub-list + // from the parent's control. It reaches nested lists by inheritance. + expect( getSubListAttributes( parentAttributes ) ).not.toHaveProperty( 'sublistIndent' ); + } ); + + test( 'omits unset attributes so the block defaults apply', () => { + expect( getSubListAttributes( { defaultIcon: 'check' } ) ).toEqual( { + defaultIcon: 'check' + } ); + } ); + + test( 'returns an empty object when the parent has nothing to inherit', () => { + expect( getSubListAttributes( {} ) ).toEqual( {} ); + expect( getSubListAttributes( undefined ) ).toEqual( {} ); + } ); +} ); diff --git a/tests/test-icon-list-css.php b/tests/test-icon-list-css.php new file mode 100644 index 000000000..66b285059 --- /dev/null +++ b/tests/test-icon-list-css.php @@ -0,0 +1,74 @@ + $attrs Block attributes. + * @return string + */ + private function render( array $attrs ): string { + $block = array( + 'blockName' => 'themeisle-blocks/icon-list', + 'attrs' => array_merge( array( 'id' => 'unittest123' ), $attrs ), + 'innerHTML' => '', + 'innerContent' => array(), + 'innerBlocks' => array(), + ); + + return (string) ( new Icon_List_CSS() )->render_css( $block ); + } + + /** + * A numeric indent is emitted with the pixel unit. + */ + public function test_numeric_sublist_indent_is_emitted_in_px() { + $css = $this->render( array( 'sublistIndent' => 40 ) ); + + $this->assertStringContainsString( '--sublist-indent: 40px', $css ); + } + + /** + * A string indent keeps its own unit. + */ + public function test_string_sublist_indent_is_emitted_as_is() { + $css = $this->render( array( 'sublistIndent' => '2rem' ) ); + + $this->assertStringContainsString( '--sublist-indent: 2rem', $css ); + } + + /** + * A unitless numeric string is emitted once, in pixels. Both the numeric + * and the string mappings would otherwise fire and the unitless value, + * coming last, would win in the cascade. + */ + public function test_numeric_string_sublist_indent_is_emitted_once_in_px() { + $css = $this->render( array( 'sublistIndent' => '32' ) ); + + $this->assertSame( 1, substr_count( $css, '--sublist-indent' ) ); + $this->assertStringContainsString( '--sublist-indent: 32px', $css ); + } + + /** + * No indent variable when the attribute is not set. + */ + public function test_sublist_indent_is_skipped_without_value() { + $css = $this->render( array() ); + + $this->assertStringNotContainsString( '--sublist-indent', $css ); + } +}