From 943863b9205d3ea7966594e1643e5eabbb622364 Mon Sep 17 00:00:00 2001 From: David Fross Date: Thu, 3 Sep 2026 08:37:44 -0500 Subject: [PATCH] fix(gutenberg): prevent setAttributes on initial mount to avoid dirtying blocks --- packages/gutenberg/src/index.tsx | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/packages/gutenberg/src/index.tsx b/packages/gutenberg/src/index.tsx index 8f3cdb3..007a11d 100644 --- a/packages/gutenberg/src/index.tsx +++ b/packages/gutenberg/src/index.tsx @@ -1,4 +1,4 @@ -import React, { useEffect, useMemo, useState } from "react"; +import React, { useEffect, useMemo, useRef, useState } from "react"; import { InspectorControls } from "@wordpress/block-editor"; import { PanelBody, PanelRow, SearchControl, Spinner, TabPanel } from "@wordpress/components"; import { createHigherOrderComponent } from "@wordpress/compose"; @@ -210,6 +210,8 @@ const withInspectorControls = createHigherOrderComponent((BlockEdit: React.Compo const [selectedSearchResult, setSelectedSearchResult] = useState(-1); + const isInitialMount = useRef(true); + useEffect(() => { if (!props.attributes.className) { return; @@ -224,14 +226,16 @@ const withInspectorControls = createHigherOrderComponent((BlockEdit: React.Compo })); }, []); - useEffect( - () => { - props.setAttributes({ - className: [...classNames.active, ...classNames.dynamic].join(" "), - }); - }, - [classNames], - ); + useEffect(() => { + if (isInitialMount.current) { + isInitialMount.current = false; + return; + } + + props.setAttributes({ + className: [...classNames.active, ...classNames.dynamic].join(" "), + }); + }, [classNames]); useEffect(() => { setSearchValue("");