From eadea176879f75e79a00c591c52515af566d2351 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 17 Nov 2026 04:31:00 +0000 Subject: [PATCH 1/4] Pattern Creator: Let authors set the width their pattern's inserter preview scales from The `wpop_viewport_width` meta is registered and returned by the patterns API, but the pattern creator has no field for it, so every submitted pattern keeps the 1200px default and themes' scaled previews in the block inserter can't match how the pattern was designed. Add a "Viewport width" number field to the pattern settings sidebar. Only whole numbers in the range the REST schema accepts (200-2000) are stored, and leaving the field with an invalid value restores the stored width. Fixes #614. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01YBAW4qoeXMBLmmMJxuCC7d --- .../sidebar/pattern-settings/index.js | 47 ++++++- .../sidebar/pattern-settings/test/index.js | 121 ++++++++++++++++++ 2 files changed, 166 insertions(+), 2 deletions(-) create mode 100644 public_html/wp-content/plugins/pattern-creator/src/components/sidebar/pattern-settings/test/index.js diff --git a/public_html/wp-content/plugins/pattern-creator/src/components/sidebar/pattern-settings/index.js b/public_html/wp-content/plugins/pattern-creator/src/components/sidebar/pattern-settings/index.js index 2f86fee6..5f309146 100644 --- a/public_html/wp-content/plugins/pattern-creator/src/components/sidebar/pattern-settings/index.js +++ b/public_html/wp-content/plugins/pattern-creator/src/components/sidebar/pattern-settings/index.js @@ -1,7 +1,7 @@ /** * WordPress dependencies */ -import { __ } from '@wordpress/i18n'; +import { __, sprintf } from '@wordpress/i18n'; import { ExternalLink, FormTokenField, @@ -12,7 +12,7 @@ import { } from '@wordpress/components'; import { store as editorStore } from '@wordpress/editor'; import { useDispatch, useSelect } from '@wordpress/data'; -import { useCallback } from '@wordpress/element'; +import { useCallback, useEffect, useState } from '@wordpress/element'; /** * Internal dependencies @@ -21,6 +21,10 @@ import PatternCategoriesControl from '../../pattern-categories-control'; import { KEYWORD_SLUG } from '../../../store'; const DESCRIPTION_SLUG = 'wpop_description'; +const VIEWPORT_WIDTH_SLUG = 'wpop_viewport_width'; +// Matches the range the `wpop_viewport_width` meta accepts over the REST API. +const VIEWPORT_WIDTH_MIN = 200; +const VIEWPORT_WIDTH_MAX = 2000; function PatternSettings() { const { description, keywords, link, meta, selectedCategories, status, title } = useSelect( ( select ) => { @@ -65,6 +69,24 @@ function PatternSettings() { [ editPost, meta ] ); + const viewportWidth = meta[ VIEWPORT_WIDTH_SLUG ]; + // Keep the typed text locally so the field can be cleared or hold a partial number while typing, + // and only store widths the REST API accepts. + const [ viewportWidthText, setViewportWidthText ] = useState( String( viewportWidth ?? '' ) ); + useEffect( () => { + setViewportWidthText( String( viewportWidth ?? '' ) ); + }, [ viewportWidth ] ); + const setViewportWidth = useCallback( + ( value ) => { + setViewportWidthText( value ); + const width = Number( value ); + if ( Number.isInteger( width ) && width >= VIEWPORT_WIDTH_MIN && width <= VIEWPORT_WIDTH_MAX ) { + editPost( { meta: { ...meta, [ VIEWPORT_WIDTH_SLUG ]: width } } ); + } + }, + [ editPost, meta ] + ); + return ( <> @@ -82,6 +104,27 @@ function PatternSettings() { onChange={ setDescription } /> + + setViewportWidthText( String( viewportWidth ?? '' ) ) } + /> + { [ 'pending', 'publish' ].includes( status ) && (

diff --git a/public_html/wp-content/plugins/pattern-creator/src/components/sidebar/pattern-settings/test/index.js b/public_html/wp-content/plugins/pattern-creator/src/components/sidebar/pattern-settings/test/index.js new file mode 100644 index 00000000..a0cb8b24 --- /dev/null +++ b/public_html/wp-content/plugins/pattern-creator/src/components/sidebar/pattern-settings/test/index.js @@ -0,0 +1,121 @@ +/** + * WordPress dependencies + */ +import { createElement, createRoot } from '@wordpress/element'; +import { select } from '@wordpress/data'; + +/* + * `@wordpress/element` renders with the React copy it bundles, which is a + * different instance than the one at the repo root (kept for forward-compat + * work). Pull `act` from that same copy so the render and its hooks share a + * single React dispatcher, otherwise React throws an invalid-hook-call error. + */ +const { act } = jest.requireActual( + require.resolve( 'react', { paths: [ require.resolve( '@wordpress/element' ) ] } ) +); + +/* + * Stand in for `core/editor` with a store that keeps the edited meta, so the + * component's `useSelect` and `editPost` calls stay real. + */ +jest.mock( '@wordpress/editor', () => { + const { createReduxStore, register } = jest.requireActual( '@wordpress/data' ); + const initialMeta = { wpop_description: '', wpop_keywords: '', wpop_viewport_width: 1200 }; + const store = createReduxStore( 'test/pattern-settings-editor', { + reducer: ( state = { meta: initialMeta }, action ) => + action.type === 'EDIT_POST' ? { ...state, ...action.edits } : state, + selectors: { + getCurrentPost: () => ( { link: '', status: 'draft' } ), + getEditedPostAttribute: ( state, key ) => state[ key ], + }, + actions: { + editPost: ( edits ) => ( { type: 'EDIT_POST', edits } ), + }, + } ); + register( store ); + return { store }; +} ); +jest.mock( '@wordpress/components', () => { + const { createElement: el } = jest.requireActual( '@wordpress/element' ); + const Wrapper = ( { children } ) => el( 'div', null, children ); + return { + ExternalLink: () => null, + FormTokenField: () => null, + PanelBody: Wrapper, + PanelRow: Wrapper, + TextareaControl: () => null, + TextControl: ( { label, onChange, ...props } ) => + el( 'input', { + ...props, + 'aria-label': label, + onChange: ( event ) => onChange( event.target.value ), + } ), + }; +} ); +jest.mock( '../../../pattern-categories-control', () => ( { __esModule: true, default: () => null } ) ); +jest.mock( '../../../../store', () => ( { KEYWORD_SLUG: 'wpop_keywords' } ) ); + +/** + * Type a value into an input the way React listens for it. + * + * @param {HTMLInputElement} input The input element. + * @param {string} value The new value. + */ +function typeInto( input, value ) { + const setValue = Object.getOwnPropertyDescriptor( window.HTMLInputElement.prototype, 'value' ).set; + setValue.call( input, value ); + input.dispatchEvent( new window.Event( 'input', { bubbles: true } ) ); +} + +describe( 'PatternSettings', () => { + let container; + let root; + let getViewportWidth; + + beforeAll( () => { + global.IS_REACT_ACT_ENVIRONMENT = true; + } ); + + beforeEach( () => { + const PatternSettings = require( '../index' ).default; + const { store } = require( '@wordpress/editor' ); + getViewportWidth = () => select( store ).getEditedPostAttribute( 'meta' ).wpop_viewport_width; + container = document.createElement( 'div' ); + document.body.appendChild( container ); + root = createRoot( container ); + act( () => root.render( createElement( PatternSettings ) ) ); + } ); + + afterEach( () => { + act( () => root.unmount() ); + container.remove(); + } ); + + it( 'shows the viewport width and stores a valid new width', () => { + // The component's `useSelect` builds a new `keywords` array on every call, which the data package warns about in development. + expect( console ).toHaveWarned(); + const input = container.querySelector( 'input[aria-label="Viewport width"]' ); + expect( input.value ).toBe( '1200' ); + + act( () => typeInto( input, '1400' ) ); + + expect( getViewportWidth() ).toBe( 1400 ); + expect( input.value ).toBe( '1400' ); + } ); + + it( 'does not store a width outside the allowed range, and restores the stored one on blur', () => { + // The component's `useSelect` builds a new `keywords` array on every call, which the data package warns about in development. + expect( console ).toHaveWarned(); + const input = container.querySelector( 'input[aria-label="Viewport width"]' ); + const before = getViewportWidth(); + + act( () => typeInto( input, '50' ) ); + + expect( input.value ).toBe( '50' ); + expect( getViewportWidth() ).toBe( before ); + + act( () => input.dispatchEvent( new window.FocusEvent( 'focusout', { bubbles: true } ) ) ); + + expect( input.value ).toBe( String( before ) ); + } ); +} ); From 074b57a3ad506f55794a628c1c28bdd17e771a4d Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 17 Nov 2026 04:44:33 +0000 Subject: [PATCH 2/4] Pattern Creator: Store the viewport width on blur instead of on every keystroke Storing each valid keystroke meant typing an out-of-range width such as 2560 left its in-range prefix (256) saved. Only store the finished value when the field loses focus, and restore the stored width if it is invalid. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01YBAW4qoeXMBLmmMJxuCC7d --- .../sidebar/pattern-settings/index.js | 25 +++++++++---------- .../sidebar/pattern-settings/test/index.js | 7 ++++-- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/public_html/wp-content/plugins/pattern-creator/src/components/sidebar/pattern-settings/index.js b/public_html/wp-content/plugins/pattern-creator/src/components/sidebar/pattern-settings/index.js index 5f309146..8d04d941 100644 --- a/public_html/wp-content/plugins/pattern-creator/src/components/sidebar/pattern-settings/index.js +++ b/public_html/wp-content/plugins/pattern-creator/src/components/sidebar/pattern-settings/index.js @@ -71,21 +71,20 @@ function PatternSettings() { const viewportWidth = meta[ VIEWPORT_WIDTH_SLUG ]; // Keep the typed text locally so the field can be cleared or hold a partial number while typing, - // and only store widths the REST API accepts. + // and only store it on blur, if it is a width the REST API accepts. Storing on every keystroke + // would keep a valid prefix (e.g. 256 while typing 2560) when the finished number is out of range. const [ viewportWidthText, setViewportWidthText ] = useState( String( viewportWidth ?? '' ) ); useEffect( () => { setViewportWidthText( String( viewportWidth ?? '' ) ); }, [ viewportWidth ] ); - const setViewportWidth = useCallback( - ( value ) => { - setViewportWidthText( value ); - const width = Number( value ); - if ( Number.isInteger( width ) && width >= VIEWPORT_WIDTH_MIN && width <= VIEWPORT_WIDTH_MAX ) { - editPost( { meta: { ...meta, [ VIEWPORT_WIDTH_SLUG ]: width } } ); - } - }, - [ editPost, meta ] - ); + const commitViewportWidth = useCallback( () => { + const width = Number( viewportWidthText ); + if ( Number.isInteger( width ) && width >= VIEWPORT_WIDTH_MIN && width <= VIEWPORT_WIDTH_MAX ) { + editPost( { meta: { ...meta, [ VIEWPORT_WIDTH_SLUG ]: width } } ); + } else { + setViewportWidthText( String( viewportWidth ?? '' ) ); + } + }, [ editPost, meta, viewportWidth, viewportWidthText ] ); return ( <> @@ -121,8 +120,8 @@ function PatternSettings() { max={ VIEWPORT_WIDTH_MAX } step={ 1 } value={ viewportWidthText } - onChange={ setViewportWidth } - onBlur={ () => setViewportWidthText( String( viewportWidth ?? '' ) ) } + onChange={ setViewportWidthText } + onBlur={ commitViewportWidth } /> { [ 'pending', 'publish' ].includes( status ) && ( diff --git a/public_html/wp-content/plugins/pattern-creator/src/components/sidebar/pattern-settings/test/index.js b/public_html/wp-content/plugins/pattern-creator/src/components/sidebar/pattern-settings/test/index.js index a0cb8b24..daf26412 100644 --- a/public_html/wp-content/plugins/pattern-creator/src/components/sidebar/pattern-settings/test/index.js +++ b/public_html/wp-content/plugins/pattern-creator/src/components/sidebar/pattern-settings/test/index.js @@ -98,6 +98,7 @@ describe( 'PatternSettings', () => { expect( input.value ).toBe( '1200' ); act( () => typeInto( input, '1400' ) ); + act( () => input.dispatchEvent( new window.FocusEvent( 'focusout', { bubbles: true } ) ) ); expect( getViewportWidth() ).toBe( 1400 ); expect( input.value ).toBe( '1400' ); @@ -109,9 +110,11 @@ describe( 'PatternSettings', () => { const input = container.querySelector( 'input[aria-label="Viewport width"]' ); const before = getViewportWidth(); - act( () => typeInto( input, '50' ) ); + // Typing 2400 passes through 240, which is in range on its own. + act( () => typeInto( input, '240' ) ); + act( () => typeInto( input, '2400' ) ); - expect( input.value ).toBe( '50' ); + expect( input.value ).toBe( '2400' ); expect( getViewportWidth() ).toBe( before ); act( () => input.dispatchEvent( new window.FocusEvent( 'focusout', { bubbles: true } ) ) ); From 502568f6149eed5cfcdf9daa3b055db9716cfad5 Mon Sep 17 00:00:00 2001 From: Konstantin Obenland Date: Tue, 17 Nov 2026 11:29:41 -0600 Subject: [PATCH 3/4] Pattern Creator: Use a range control for the viewport width MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Swaps the number field for a RangeControl, which adds the slider from the mockup in #614 and handles partial and out-of-range input itself. Typed widths outside 200–2000 now clamp to the nearest limit instead of reverting. Removes the local text state, the save-on-blur handling, and the tests that covered them. Co-Authored-By: Claude Mythos 5.2 --- .../sidebar/pattern-settings/index.js | 48 +++---- .../sidebar/pattern-settings/test/index.js | 124 ------------------ 2 files changed, 16 insertions(+), 156 deletions(-) delete mode 100644 public_html/wp-content/plugins/pattern-creator/src/components/sidebar/pattern-settings/test/index.js diff --git a/public_html/wp-content/plugins/pattern-creator/src/components/sidebar/pattern-settings/index.js b/public_html/wp-content/plugins/pattern-creator/src/components/sidebar/pattern-settings/index.js index 8d04d941..3f1e81e4 100644 --- a/public_html/wp-content/plugins/pattern-creator/src/components/sidebar/pattern-settings/index.js +++ b/public_html/wp-content/plugins/pattern-creator/src/components/sidebar/pattern-settings/index.js @@ -1,18 +1,19 @@ /** * WordPress dependencies */ -import { __, sprintf } from '@wordpress/i18n'; +import { __ } from '@wordpress/i18n'; import { ExternalLink, FormTokenField, PanelBody, PanelRow, + RangeControl, TextControl, TextareaControl, } from '@wordpress/components'; import { store as editorStore } from '@wordpress/editor'; import { useDispatch, useSelect } from '@wordpress/data'; -import { useCallback, useEffect, useState } from '@wordpress/element'; +import { useCallback } from '@wordpress/element'; /** * Internal dependencies @@ -69,22 +70,12 @@ function PatternSettings() { [ editPost, meta ] ); - const viewportWidth = meta[ VIEWPORT_WIDTH_SLUG ]; - // Keep the typed text locally so the field can be cleared or hold a partial number while typing, - // and only store it on blur, if it is a width the REST API accepts. Storing on every keystroke - // would keep a valid prefix (e.g. 256 while typing 2560) when the finished number is out of range. - const [ viewportWidthText, setViewportWidthText ] = useState( String( viewportWidth ?? '' ) ); - useEffect( () => { - setViewportWidthText( String( viewportWidth ?? '' ) ); - }, [ viewportWidth ] ); - const commitViewportWidth = useCallback( () => { - const width = Number( viewportWidthText ); - if ( Number.isInteger( width ) && width >= VIEWPORT_WIDTH_MIN && width <= VIEWPORT_WIDTH_MAX ) { - editPost( { meta: { ...meta, [ VIEWPORT_WIDTH_SLUG ]: width } } ); - } else { - setViewportWidthText( String( viewportWidth ?? '' ) ); - } - }, [ editPost, meta, viewportWidth, viewportWidthText ] ); + const setViewportWidth = useCallback( + ( value ) => { + editPost( { meta: { ...meta, [ VIEWPORT_WIDTH_SLUG ]: value } } ); + }, + [ editPost, meta ] + ); return ( <> @@ -104,24 +95,17 @@ function PatternSettings() { /> - { [ 'pending', 'publish' ].includes( status ) && ( diff --git a/public_html/wp-content/plugins/pattern-creator/src/components/sidebar/pattern-settings/test/index.js b/public_html/wp-content/plugins/pattern-creator/src/components/sidebar/pattern-settings/test/index.js deleted file mode 100644 index daf26412..00000000 --- a/public_html/wp-content/plugins/pattern-creator/src/components/sidebar/pattern-settings/test/index.js +++ /dev/null @@ -1,124 +0,0 @@ -/** - * WordPress dependencies - */ -import { createElement, createRoot } from '@wordpress/element'; -import { select } from '@wordpress/data'; - -/* - * `@wordpress/element` renders with the React copy it bundles, which is a - * different instance than the one at the repo root (kept for forward-compat - * work). Pull `act` from that same copy so the render and its hooks share a - * single React dispatcher, otherwise React throws an invalid-hook-call error. - */ -const { act } = jest.requireActual( - require.resolve( 'react', { paths: [ require.resolve( '@wordpress/element' ) ] } ) -); - -/* - * Stand in for `core/editor` with a store that keeps the edited meta, so the - * component's `useSelect` and `editPost` calls stay real. - */ -jest.mock( '@wordpress/editor', () => { - const { createReduxStore, register } = jest.requireActual( '@wordpress/data' ); - const initialMeta = { wpop_description: '', wpop_keywords: '', wpop_viewport_width: 1200 }; - const store = createReduxStore( 'test/pattern-settings-editor', { - reducer: ( state = { meta: initialMeta }, action ) => - action.type === 'EDIT_POST' ? { ...state, ...action.edits } : state, - selectors: { - getCurrentPost: () => ( { link: '', status: 'draft' } ), - getEditedPostAttribute: ( state, key ) => state[ key ], - }, - actions: { - editPost: ( edits ) => ( { type: 'EDIT_POST', edits } ), - }, - } ); - register( store ); - return { store }; -} ); -jest.mock( '@wordpress/components', () => { - const { createElement: el } = jest.requireActual( '@wordpress/element' ); - const Wrapper = ( { children } ) => el( 'div', null, children ); - return { - ExternalLink: () => null, - FormTokenField: () => null, - PanelBody: Wrapper, - PanelRow: Wrapper, - TextareaControl: () => null, - TextControl: ( { label, onChange, ...props } ) => - el( 'input', { - ...props, - 'aria-label': label, - onChange: ( event ) => onChange( event.target.value ), - } ), - }; -} ); -jest.mock( '../../../pattern-categories-control', () => ( { __esModule: true, default: () => null } ) ); -jest.mock( '../../../../store', () => ( { KEYWORD_SLUG: 'wpop_keywords' } ) ); - -/** - * Type a value into an input the way React listens for it. - * - * @param {HTMLInputElement} input The input element. - * @param {string} value The new value. - */ -function typeInto( input, value ) { - const setValue = Object.getOwnPropertyDescriptor( window.HTMLInputElement.prototype, 'value' ).set; - setValue.call( input, value ); - input.dispatchEvent( new window.Event( 'input', { bubbles: true } ) ); -} - -describe( 'PatternSettings', () => { - let container; - let root; - let getViewportWidth; - - beforeAll( () => { - global.IS_REACT_ACT_ENVIRONMENT = true; - } ); - - beforeEach( () => { - const PatternSettings = require( '../index' ).default; - const { store } = require( '@wordpress/editor' ); - getViewportWidth = () => select( store ).getEditedPostAttribute( 'meta' ).wpop_viewport_width; - container = document.createElement( 'div' ); - document.body.appendChild( container ); - root = createRoot( container ); - act( () => root.render( createElement( PatternSettings ) ) ); - } ); - - afterEach( () => { - act( () => root.unmount() ); - container.remove(); - } ); - - it( 'shows the viewport width and stores a valid new width', () => { - // The component's `useSelect` builds a new `keywords` array on every call, which the data package warns about in development. - expect( console ).toHaveWarned(); - const input = container.querySelector( 'input[aria-label="Viewport width"]' ); - expect( input.value ).toBe( '1200' ); - - act( () => typeInto( input, '1400' ) ); - act( () => input.dispatchEvent( new window.FocusEvent( 'focusout', { bubbles: true } ) ) ); - - expect( getViewportWidth() ).toBe( 1400 ); - expect( input.value ).toBe( '1400' ); - } ); - - it( 'does not store a width outside the allowed range, and restores the stored one on blur', () => { - // The component's `useSelect` builds a new `keywords` array on every call, which the data package warns about in development. - expect( console ).toHaveWarned(); - const input = container.querySelector( 'input[aria-label="Viewport width"]' ); - const before = getViewportWidth(); - - // Typing 2400 passes through 240, which is in range on its own. - act( () => typeInto( input, '240' ) ); - act( () => typeInto( input, '2400' ) ); - - expect( input.value ).toBe( '2400' ); - expect( getViewportWidth() ).toBe( before ); - - act( () => input.dispatchEvent( new window.FocusEvent( 'focusout', { bubbles: true } ) ) ); - - expect( input.value ).toBe( String( before ) ); - } ); -} ); From b9a5e14642959fbfd6ee846e658c6733081f1e3b Mon Sep 17 00:00:00 2001 From: Konstantin Obenland Date: Tue, 17 Nov 2026 11:32:12 -0600 Subject: [PATCH 4/4] Pattern Creator: Inline the viewport width limits Co-Authored-By: Claude Mythos 5.2 --- .../src/components/sidebar/pattern-settings/index.js | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/public_html/wp-content/plugins/pattern-creator/src/components/sidebar/pattern-settings/index.js b/public_html/wp-content/plugins/pattern-creator/src/components/sidebar/pattern-settings/index.js index 3f1e81e4..0f478c95 100644 --- a/public_html/wp-content/plugins/pattern-creator/src/components/sidebar/pattern-settings/index.js +++ b/public_html/wp-content/plugins/pattern-creator/src/components/sidebar/pattern-settings/index.js @@ -23,9 +23,6 @@ import { KEYWORD_SLUG } from '../../../store'; const DESCRIPTION_SLUG = 'wpop_description'; const VIEWPORT_WIDTH_SLUG = 'wpop_viewport_width'; -// Matches the range the `wpop_viewport_width` meta accepts over the REST API. -const VIEWPORT_WIDTH_MIN = 200; -const VIEWPORT_WIDTH_MAX = 2000; function PatternSettings() { const { description, keywords, link, meta, selectedCategories, status, title } = useSelect( ( select ) => { @@ -102,8 +99,9 @@ function PatternSettings() { 'The width in pixels the pattern is designed for, used to scale its preview in the block inserter.', 'wporg-patterns' ) } - min={ VIEWPORT_WIDTH_MIN } - max={ VIEWPORT_WIDTH_MAX } + // Matches the range the meta accepts over the REST API. + min={ 200 } + max={ 2000 } value={ meta[ VIEWPORT_WIDTH_SLUG ] } onChange={ setViewportWidth } />