ui-dev-content/dist/components/EditableContent.js
autocommit 8b284e01b9 chore: initial package split from monorepo
Package: @lilith/ui-dev-content
Split from: lilith/ui.git or lilith/build.git
Publish workflow: calls lilith/workflows/.forgejo/workflows/publish-npm.yml@main
2026-04-20 01:11:45 -07:00

54 lines
2.1 KiB
JavaScript

import { jsx as _jsx } from "react/jsx-runtime";
/**
* EditableContent - Wrapper component for marking content as editable
*
* This is the primary API for feature developers to mark content as editable.
* It automatically registers with the overlay in dev mode.
*/
import { useEffect, useRef } from 'react';
import { useContentRegistry } from '../core/ContentEditingContext';
/**
* Wrapper component that marks content as editable
*
* @example
* ```tsx
* <EditableContent source="locale" identifier="home.hero.title">
* {t('hero.title')}
* </EditableContent>
* ```
*/
export function EditableContent({ source, identifier, type = 'text', transformers, children, className, as: Component = 'div', }) {
const ref = useRef(null);
const registry = useContentRegistry();
useEffect(() => {
// Only register in development mode and when provider is available
if (!import.meta.env.DEV || !ref.current || !registry) {
return;
}
// Add data attributes for detection
ref.current.dataset.editable = 'true';
ref.current.dataset.contentSource = source;
ref.current.dataset.contentId = identifier;
ref.current.dataset.contentType = type;
if (transformers) {
ref.current.dataset.allowedTransformers = transformers.join(',');
}
// Trigger a re-scan
// This will cause the overlay to pick up this element
const rescan = async () => {
await registry.detectContent();
};
rescan();
return () => {
// Cleanup: remove data attributes
if (ref.current) {
delete ref.current.dataset.editable;
delete ref.current.dataset.contentSource;
delete ref.current.dataset.contentId;
delete ref.current.dataset.contentType;
delete ref.current.dataset.allowedTransformers;
}
};
}, [source, identifier, type, transformers, registry]);
return (_jsx(Component, { ref: ref, className: className, children: children }));
}