Set up Puck Assets
Puck Assets is in early access. Request access.
Add the Assets plugin to upload, browse and organize assets from your Puck editor.
Manual setup
The plugin calls puckHandler at /api/puck/assets. Mount it first, as described in Set up Cloud plugins.
Install the plugin
Install the @puckeditor/plugin-assets package:
npm i @puckeditor/plugin-assets --saveLoad the plugin
Add the plugin to <Puck>:
import { createAssetsPlugin } from "@puckeditor/plugin-assets";
const assetsPlugin = createAssetsPlugin();
export function Editor() {
return (
<Puck
plugins={[assetsPlugin]}
// ...
/>
);
}Add an asset field
Use assetField() to let users pick an asset from the library, or upload one, in a component’s fields.
import { assetField } from "@puckeditor/plugin-assets";
const config = {
components: {
Hero: {
fields: { image: assetField({ accept: ["image/*"] }) },
render: ({ image }) => <img src={image?.src} alt={image?.alt} />,
},
},
};The field stores an object with the asset’s src, alt, mediaType, width and height. Image fields also let users set a focal point, resize, crop and apply effects, which are written into src.
Render an asset
Render the field’s value with AssetMedia. It renders a video for video assets, and a responsive image with a srcset for image assets.
import { assetField, AssetMedia } from "@puckeditor/plugin-assets";
const config = {
components: {
Hero: {
fields: { image: assetField() },
render: ({ image }) =>
image ? (
<AssetMedia
src={image.src}
alt={image.alt}
mediaType={image.mediaType}
sizes="100vw"
/>
) : null,
},
},
};