deterministic + unique ids in 6 components (node-derived scope)

Migrates Menu, ColumnLayout, Countdown, Gallery, Tabs, InputField, and
TextareaField from Math.random()/content-hash scope ids to the threaded
Craft node id (via scopeId()), keeping every emitted element id,
aria-controls/aria-labelledby/for, and inline <script> function
name/getElementById() call consistently scoped per component.

Resolves the two Important id-collision review findings:
- Tabs: tabId was djb2(anchorId||labels) -- two default Tabs instances
  produced identical aria-controls/aria-labelledby ids, so one instance's
  arrow-key script clobbered the other's tab/panel wiring.
- InputField/TextareaField: fieldId was `field-${name}` -- two fields
  sharing a (often default) name produced duplicate <label for>/<input
  id> pairs, breaking the for/id association for one of them.

Also eliminates the remaining Math.random() scope ids in Menu (hover CSS
class scope) and ColumnLayout (nth-child width CSS class scope), so no
export component is non-deterministic anymore.

All fall back to a stable content hash (never Math.random) for legacy
2-arg toHtml() call sites without a node id.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-07-12 14:35:12 -07:00
parent c2aac870e7
commit 8029126ab7
14 changed files with 291 additions and 53 deletions
+7 -3
View File
@@ -1,7 +1,7 @@
import React, { CSSProperties } from 'react';
import { useNode, UserComponent } from '@craftjs/core';
import { cssPropsToString } from '../../utils/style-helpers';
import { escapeHtml, escapeAttr, safeUrl } from '../../utils/escape';
import { escapeHtml, escapeAttr, safeUrl, scopeId } from '../../utils/escape';
interface GalleryImage {
src: string;
@@ -124,7 +124,7 @@ Gallery.craft = {
/* ---------- HTML export ---------- */
(Gallery as any).toHtml = (props: GalleryProps, _childrenHtml: string) => {
(Gallery as any).toHtml = (props: GalleryProps, _childrenHtml: string, nodeId?: string) => {
const sectionStyle = cssPropsToString({
padding: '60px 20px',
...props.style,
@@ -134,7 +134,11 @@ Gallery.craft = {
const gap = props.gap || '16px';
const lightbox = props.lightbox || false;
const galleryId = 'gallery_' + Math.random().toString(36).slice(2, 8);
// Deterministic AND unique id, scoped on the Craft node id, for this
// gallery's overlay/grid element ids and inline-script function names --
// so two Gallery instances (e.g. both left at default images) don't
// collide and end up sharing/clobbering one lightbox overlay.
const galleryId = scopeId(nodeId, JSON.stringify(images) + columns + gap, 'gallery');
const items = images.map((img) => {
const caption = img.caption