measuredco / puck
- пятница, 8 сентября 2023 г. в 00:00:06
The self-hosted drag and drop editor for React.
The self-hosted, drag and drop editor for React.
Render the editor:
// Editor.jsx
import { Puck } from "@measured/puck";
// Create puck component config
const config = {
components: {
HeadingBlock: {
fields: {
children: {
type: "text",
},
},
render: ({ children }) => {
return <h1>{children}</h1>;
},
},
},
};
// Describe the initial data
const initialData = {};
// Save the data to your database
const save = (data) => {};
// Render Puck editor
export function Editor() {
return <Puck config={config} data={initialData} onPublish={save} />;
}Render the page:
// Page.jsx
import { Render } from "@measured/puck";
export function Page() {
return <Render config={config} data={data} />;
}Install the package
npm i @measured/puck --save
Or generate a puck application using a recipe
npx create-puck-app my-appPuck is a React component that can be easily integrated into your existing application. We also provide helpful recipes for common use cases:
Puck can be configured to work with plugins. Plugins can extend the functionality to support novel functionality.
heading-analyzer: Analyze the heading outline of your page and be warned when you're not respecting WCAG 2 accessiblity standards.The plugin API follows a React paradigm. Each plugin passed to the Puck editor can provide three functions:
renderRoot (Component): Render the root node of the preview contentrenderRootFields (Component): Render the root fieldsrenderFields (Component): Render the fields for the currently selected componentEach render function receives the children prop, which you must render, and the data prop, which can be used to read the data model.
Here's a basic plugin that renders a "My plugin" heading in the root field area:
const myPlugin = {
renderRootFields: (props) => (
<div>
{props.children}
<h2>My plugin</h2>
</div>
),
};<Puck>The <Puck> component renders the Puck editor.
Config): Puck component configurationData): Initial data to render(Data) => void [optional]): Callback that triggers when the user makes a change(Data) => void [optional]): Callback that triggers when the user hits the "Publish" buttonComponent [optional]): Render function for overriding the Puck header componentComponent [optional]): Render function for overriding the Puck header actions. Use a fragment.string [optional]): Set the title shown in the header titlestring [optional]): Set a path to show after the header titlePlugin[] [optional]): Array of plugins that can be used to enhance Puck<Render>The <Render> component renders user-facing UI using Puck data.
Config): Puck component configurationData): Data to renderConfigThe Config object describes which components Puck should render, how they should render and which inputs are available to them.
object)
object):
Field): Title of the content, typically used for the page title.Field): User defined fields, used to describe the input data stored in the root key.Component): Render a React component at the root of your component tree. Useful for defining context providers.object): Definitions for each of the components you want to show in the visual editor
object)
Field): The Field objects describing the input data stored against this component.Component): Render function for your React component. Receives props as defined in fields.object [optional]): Default props to pass to your component. Will show in fields.FieldA Field represents a user input field shown in the Puck interface.
text | textarea | number | select | radio | external | array): The input type to rendertext [optional]): A label for the input. Will use the key if not provided.object): Object describing sub-fields for items in an array input
Field): The Field objects describing the input data for each item(object, number) => string [optional]): Function to get the name of each item when using the array or external field typesobject [optional]): Default props to pass to each new item added, when using a array field typeobject[]): array of items to render for select or radio inputs
string)string | number | boolean)Adaptor): Content adaptor if using the external input typeobject): Paramaters passed to the adaptorDataThe Data object stores the puck state.
object):
root fieldsobject[]):
AdaptorAn Adaptor can be used to load content from an external content repository, like Strapi.js.
string): The human-readable name of the adaptor(adaptorParams: object) => object): Fetch a list of content and return an arrayNB Using an adaptor on the reserved field name
_datawill spread the resulting data over your object, and lock the overridden fields.
PluginPlugins that can be used to enhance Puck.
Component): Render the root node of the preview contentComponent): Render the root fieldsComponent): Render the fields for the currently selected componentPuck is developed and maintained by Measured, a small group of industry veterans with decades of experience helping companies solve hard UI problems. We offer consultancy and development services for scale-ups, SMEs and enterprises.
If you need support integrating Puck or creating a beautiful component library, please reach out via our website.
MIT © Measured Co.