Skip to content

Commit 5e2ed8d

Browse files
authored
Merge pull request #193 from KevinBatdorf/allow-id-editable
2 parents 8c944a0 + 3cf8849 commit 5e2ed8d

4 files changed

Lines changed: 51 additions & 11 deletions

File tree

pattern-css.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
constant('PATTERN_CSS_SELECTOR_OVERRIDE') : null,
4343
'globalCss' => get_option('pcss_global_css', ''),
4444
'globalCssCompiled' => get_option('pcss_global_css_compiled', ''),
45+
'allowManualOverride' => defined('PATTERN_CSS_ALLOW_MANUAL_OVERRIDE'),
4546
]) . ';',
4647
'before'
4748
);

readme.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,14 @@ Add this to functions.php:
132132

133133
`define('PATTERN_CSS_SELECTOR_OVERRIDE', ['name' => 'foo', 'type' => 'type']);`
134134

135+
= Can I manually set the class ID? =
136+
137+
By default, class IDs are auto-generated. If you need to manually set or change a block's class ID (e.g. to match an existing class), you can enable this by adding the following constant to wp-config.php or functions.php:
138+
139+
`define('PATTERN_CSS_ALLOW_MANUAL_OVERRIDE', true);`
140+
141+
Once enabled, the ID field in the Advanced panel becomes editable. Type your desired class name and press "Apply" to save it. The value will be slugified automatically.
142+
135143
== Screenshots ==
136144

137145
1. Add styles not available in the editor (supoprts nesting)
@@ -142,6 +150,7 @@ Add this to functions.php:
142150
== Changelog ==
143151

144152
= 1.5.5 - 2026-03-24 =
153+
- Added manual class ID override (opt-in via constant)
145154
- Warns when duplicate class IDs are detected in the editor
146155
- Prevents duplicate inline styles on the frontend
147156
- Switched to Biome for linting and formatting

src/components/BlockControl.tsx

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { useSelect } from '@wordpress/data';
1616
import { store as coreStore } from '@wordpress/editor';
1717
import { useCallback, useEffect, useRef, useState } from '@wordpress/element';
1818
import { __, sprintf } from '@wordpress/i18n';
19+
import { cleanForSlug } from '@wordpress/url';
1920
import type { Warning as CssWarning } from 'lightningcss-wasm';
2021
import { addToClassList } from '../lib/classes';
2122
import { focusAtEndOfLine2 } from '../lib/dom';
@@ -73,14 +74,16 @@ export const BlockControl = (
7374
const className = [
7475
...new Set(
7576
[
76-
...existing.filter((c: string) => !c.startsWith('pcss-')),
77+
...existing.filter((c: string) => c !== pcssClassId && !c.startsWith('pcss-')),
7778
newId,
7879
].filter(Boolean),
7980
),
8081
].join(' ');
8182
setAttributes({ pcssClassId: newId, className });
83+
setManualClassId(newId);
8284
}, [existingClasses, setAttributes]);
8385

86+
const [manualClassId, setManualClassId] = useState(pcssClassId ?? '');
8487
const [css, setCss] = useState(initialCss);
8588
const [transformed, setTransformed] = useState<Uint8Array>();
8689
const [compiled, setCompiled] = useState(compiledCss || '');
@@ -312,17 +315,43 @@ export const BlockControl = (
312315
__('%s ID', 'pattern-css'),
313316
'Pattern CSS',
314317
)}
315-
disabled
316-
onChange={() => undefined}
317-
value={pcssClassId ?? ''}
318+
disabled={!window.patternCss?.allowManualOverride}
319+
onChange={(value: string) => {
320+
setManualClassId(value);
321+
}}
322+
value={manualClassId}
318323
/>
319-
<Button
320-
variant="secondary"
321-
className="-mt-2"
322-
onClick={generateNewId}
323-
>
324-
{__('Generate New ID', 'pattern-css')}
325-
</Button>
324+
<div className="-mt-2 flex gap-2">
325+
{window.patternCss?.allowManualOverride && manualClassId !== pcssClassId && (
326+
<Button
327+
variant="primary"
328+
size="small"
329+
onClick={() => {
330+
const slug = cleanForSlug(manualClassId);
331+
setManualClassId(slug);
332+
const existing = existingClasses?.split(' ') || [];
333+
const className = [
334+
...new Set(
335+
[
336+
...existing.filter((c: string) => c !== pcssClassId),
337+
slug,
338+
].filter(Boolean),
339+
),
340+
].join(' ');
341+
setAttributes({ pcssClassId: slug, className });
342+
}}
343+
>
344+
{__('Apply', 'pattern-css')}
345+
</Button>
346+
)}
347+
<Button
348+
variant="secondary"
349+
size="small"
350+
onClick={generateNewId}
351+
>
352+
{__('Generate New ID', 'pattern-css')}
353+
</Button>
354+
</div>
326355
<p className="text-md mt-2 text-gray-600">
327356
{__(
328357
"If there's a styling conflict with another block you can generate a new ID.",

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ declare global {
99
type: string;
1010
name: string;
1111
};
12+
allowManualOverride?: boolean;
1213
globalCss: string;
1314
globalCssCompiled: string;
1415
};

0 commit comments

Comments
 (0)