Click-To-Edit API
Use data-tina-field to wire HTML elements to the TinaCMS editor sidebar
Tina's "click to edit" feature allows editors to select the HTML element they want to click on the page in order to see the corresponding field in the sidebar.
We can implement this with the data-tina-field API.
Note
[data-tina-field] must be applied to HTML elements, not React components like TinaMarkdown.
Basic Usage
import { tinaField } from 'tinacms/dist/react';
export const HeroComponent = (props) => {
return (
<div>
<h4 data-tina-field={tinaField(props, 'heading')}>{props.heading}</h4>
<p data-tina-field={tinaField(props, 'message')}>{props.message}</p>
<ul data-tina-field={tinaField(props, 'links')}>
{props.links.map((link) => (
<li>
<a data-tina-field={tinaField(link)} href={link.url}>
{link.label}
</a>
</li>
))}
</ul>
</div>
);
};Custom Components
React components can't take arbitrary HTML attributes like data-tina-field. Pass it as a prop down to an HTML element inside the component:
const Section = ({ children, tinaField }) => {
return <section data-tina-field={tinaField}>{children}</section>;
};
<Section tinaField={tinaField(data, 'body')}>
<TinaMarkdown content={data.body} />
</Section>TinaMarkdown and tinaField
When using TinaMarkdown with custom components, the props object already includes _content_source metadata injected in edit mode. Pass props directly to tinaField:
<div data-tina-field={tinaField(props)}>
<MyComponent {...props} />
</div>To target a specific field inside the component:
<div data-tina-field={tinaField(props, 'format')}>
Format: {props.format}
</div>Full Example with Rich Text
import { useTina, tinaField } from 'tinacms/dist/react';
import { TinaMarkdown } from 'tinacms/dist/rich-text';
export default function Post(props) {
const { data } = useTina({
query: props.query,
variables: props.variables,
data: props.data,
});
return (
<div data-tina-field={tinaField(data.post, 'body')}>
<TinaMarkdown
content={data.post.body}
components={{
DateTime: (props) => {
return (
<div data-tina-field={tinaField(props)}>
<div data-tina-field={tinaField(props, 'format')}>
Date Time Format: {props.format}
</div>
</div>
);
},
}}
/>
</div>
);
}Styling
When edit mode is active, Tina automatically applies outlines to any [data-tina-field] element.
Override the default styles if needed:
.__tina-quick-editing-enabled [data-tina-field] {
outline: 2px dashed rgba(254, 34, 56, 0.5);
}
.__tina-quick-editing-enabled [data-tina-field]:hover {
outline: 2px dashed rgba(254, 34, 56, 1);
}