No-Code Component is the main building block of Easyblocks. Each selectable element added to the editor canvas is an instance of a No-Code Component.
// No-Code Component Definition
import { NoCodeComponentDefinition } from "@easyblocks/core";
export const simpleBannerDefinition: NoCodeComponentDefinition = {
id: "SimpleBanner",
label: "SimpleBanner",
type: "section",
schema: [
{
prop: "backgroundColor",
label: "Background Color",
type: "color"
},
{
prop: "hasBorder",
label: "Has Border?",
type: "boolean",
responsive: true
},
{
prop: "padding",
label: "Pading",
type: "space"
},
{
prop: "gap",
label: "Gap",
type: "space"
},
{
prop: "buttonsGap",
label: "Buttons gap",
type: "space"
},
{
prop: "Title",
type: "component",
required: true,
accepts: ["@easyblocks/text"]
},
{
prop: "Buttons",
type: "component-collection",
accepts: ["Button"],
placeholderAppearance: {
height: 36,
width: 100,
label: "Add button"
}
}
],
styles: ({ values }) => {
return {
styled: {
Root: {
backgroundColor: values.backgroundColor,
border: values.hasBorder ? "2px solid black" : "none",
padding: values.padding
},
Wrapper: {
maxWidth: 600,
display: "flex",
flexDirection: "column",
gap: values.gap
},
ButtonsWrapper: {
display: "flex",
flexDirection: "row",
flexWrap: "wrap",
gap: values.buttonsGap
}
}
}
},
editing: ({ values, editingInfo}) => {
return {
components: {
Buttons: values.Buttons.map(() => ({
direction: "horizontal",
})),
Title: {
fields: [
{
...editingInfo.fields.find(field => field.path === "gap")!,
label: "Bottom gap"
}
]
}
},
};
},
};
// Component code
import { ReactElement } from "react";
type SimpleBannerProps = {
Root: ReactElement;
Title: ReactElement;
Wrapper: ReactElement;
Buttons: ReactElement[];
ButtonsWrapper: ReactElement
}
export function SimpleBanner(props: SimpleBannerProps) {
const { Root, Title, Wrapper, Buttons, ButtonsWrapper } = props;
return (
<Root.type {...Root.props}>
<Wrapper.type {...Wrapper.props}>
<Title.type {...Title.props} />
<ButtonsWrapper.type {...ButtonsWrapper.props}>
{Buttons.map((Button, index) => <Button.type {...Button.props} key={index} />)}
</ButtonsWrapper.type>
</Wrapper.type>
</Root.type>
)
}
In order to use this component two things must happen. First, you must add the definition to the Config.components property:
// easyblocks.config.ts
export const easyblocksConfig = {
components: {
// ...other component definitions,
simpleSectionDefinition,
},
};
<EasyblocksEditor
config={easyblocksConfig}
components={{ ...otherComponents, SimpleSection }}
/>
Now the component can be used.