Official React adapter for SightEdit visual editing system.
npm install @sightedit/core @sightedit/reactimport React from 'react';
import { SightEditProvider, Editable } from '@sightedit/react';
function App() {
return (
<SightEditProvider config={{
endpoint: '/api/sightedit',
debug: true
}}>
<div className="container">
<Editable sight="hero.title" type="text">
<h1>Welcome to SightEdit</h1>
</Editable>
<Editable sight="hero.description" type="richtext">
<p>Edit this content visually!</p>
</Editable>
</div>
</SightEditProvider>
);
}
export default App;Wraps your app and initializes SightEdit:
<SightEditProvider config={{
endpoint: '/api/sightedit',
apiKey: 'your-api-key',
theme: {
primaryColor: '#007bff'
},
onSave: (data) => {
console.log('Saved:', data);
}
}}>
{/* Your app */}
</SightEditProvider>Makes any content editable:
<Editable
sight="product.name"
type="text"
placeholder="Enter product name"
required
maxLength={100}
>
<h2>Product Name</h2>
</Editable>Props:
sight(required): Unique identifier for the contenttype: Editor type (text, richtext, image, link, etc.)placeholder: Placeholder textrequired: Make field requiredminLength/maxLength: Text length constraintsmin/max: Number constraintsoptions: Options for select typevalidation: Custom validation functiononChange: Callback when content changes
Button to toggle edit mode:
<EditModeToggle className="btn btn-primary">
{({ isEditMode }) => isEditMode ? 'Exit Edit' : 'Enter Edit'}
</EditModeToggle>Access the SightEdit instance and state:
function MyComponent() {
const { instance, isEditMode, toggleEditMode, save } = useSightEdit();
return (
<div>
<p>Edit mode: {isEditMode ? 'ON' : 'OFF'}</p>
<button onClick={toggleEditMode}>Toggle</button>
</div>
);
}Just get edit mode state:
function Header() {
const { isEditMode, toggleEditMode } = useEditMode();
return (
<header>
{isEditMode && <span>Editing...</span>}
</header>
);
}Get the save function:
function SaveButton() {
const save = useSightEditSave();
const handleSave = async () => {
await save('custom.data', { foo: 'bar' });
};
return <button onClick={handleSave}>Save Custom Data</button>;
}import { SightEditProvider, Editable, EditableProps } from '@sightedit/react';
import type { SightEditConfig } from '@sightedit/react';
const config: SightEditConfig = {
endpoint: '/api/sightedit',
auth: {
type: 'bearer',
token: 'your-token'
}
};
interface ProductProps extends EditableProps {
productId: string;
}
const ProductTitle: React.FC<ProductProps> = ({ productId, ...props }) => {
return (
<Editable
sight={`product.${productId}.title`}
type="text"
{...props}
>
<h1>Product Title</h1>
</Editable>
);
};<Editable
sight="settings.theme"
type="color"
onChange={(color) => {
document.body.style.backgroundColor = color;
}}
>
<div style={{ padding: '10px', border: '1px solid #ddd' }}>
Theme Color: #667eea
</div>
</Editable><Editable sight="features" type="collection">
<div className="features-grid">
<div data-sight-item="1">
<h3 data-sight="title">Feature 1</h3>
<p data-sight="description">Description</p>
</div>
<div data-sight-item="2">
<h3 data-sight="title">Feature 2</h3>
<p data-sight="description">Description</p>
</div>
</div>
</Editable><SightEditProvider config={{
endpoint: '/api/sightedit',
auth: {
type: 'bearer',
getToken: async () => {
const token = await fetchAuthToken();
return token;
}
}
}}>
{/* Your app */}
</SightEditProvider>Use the HOC for class components:
import { withSightEdit } from '@sightedit/react';
class MyComponent extends React.Component {
render() {
const { isEditMode, toggleEditMode } = this.props;
return (
<div>
<p>Edit mode: {isEditMode ? 'ON' : 'OFF'}</p>
<button onClick={toggleEditMode}>Toggle</button>
</div>
);
}
}
export default withSightEdit(MyComponent);// pages/_app.js
import { SightEditProvider } from '@sightedit/react';
function MyApp({ Component, pageProps }) {
return (
<SightEditProvider config={{
endpoint: '/api/sightedit',
mode: process.env.NODE_ENV === 'development' ? 'development' : 'production'
}}>
<Component {...pageProps} />
</SightEditProvider>
);
}
export default MyApp;<SightEditProvider config={{
endpoint: '/api/sightedit',
onError: (error) => {
console.error('SightEdit error:', error);
toast.error('Failed to save changes');
}
}}>
{/* Your app */}
</SightEditProvider>