Create a component
Add a new block that editors can place on pages, from the CLI command to the first render in Live Preview.
This guide builds a Logo Wall: a headline and a row of customer logos that editors can add to any page. It takes about ten minutes and covers four parts:
- the CLI command
- the files it creates
- the fields in Contentful
- the code that renders them
You need: a project created with create-aces-app, and NEXT_CF_MANAGEMENT_TOKEN in .env.local. aces setup writes the token there.
1. Run the CLI
pnpm aces component add "Logo Wall" \
--description "A headline and a row of customer logos."
▸ Scaffolding Logo Wall (src/libs/cf/lib/cf-logo-wall)
✓ src/libs/cf/lib/cf-logo-wall/index.tsx
✓ src/libs/cf/lib/cf-logo-wall/client.tsx
✓ src/libs/cf/lib/cf-logo-wall/render.tsx
✓ src/libs/cf/lib/cf-logo-wall/services.ts
✓ src/libs/cf/lib/cf-logo-wall/skeleton.tsx
✓ aces.config.ts: components.LogoWall, added to pageBodies.default
✓ Updated src/libs/cf/registry/server.ts
✓ Updated src/libs/cf/registry/client.ts
▸ Contentful model in <space>/<environment>
The name you pass becomes every identifier, so pick it carefully:
From "Logo Wall" |
Value | Used for |
|---|---|---|
| Folder | cf-logo-wall |
src/libs/cf/lib/cf-logo-wall/ |
| Config key and GraphQL type | LogoWall |
aces.config.ts and __typename |
| Content type ID | logoWall |
Contentful, and the GraphQL query field |
| Components | CfLogoWall, CfLogoWallServer, CfLogoWallClient |
Rendering |
What the command changed
-
Five files in
src/libs/cf/lib/cf-logo-wall/. They’re described below. -
Exports appended to
src/libs/cf/index.ts. -
Config: a new entry in
aces.config.ts, andLogoWalladded to thedefaultpage body:LogoWall: { title: "Logo Wall", description: "A headline and a row of customer logos.", contentType: "logoWall", dir: "cf-logo-wall", }, -
The registry, regenerated by
aces sync, so the page body can renderLogoWallblocks. -
Contentful:
- a new 🔷 Logo Wall content type with one field,
internalTitle, and your description - the Page Body field now allows Logo Wall entries
- a Live Preview URL for the new type
- a new 🔷 Logo Wall content type with one field,
Useful flags:
| Flag | Effect |
|---|---|
--body caseStudy |
Allow it in a different page body (repeat the flag, or comma-separate the keys) |
--no-model |
Only write code, and don’t touch Contentful. Create the content type yourself, then run pnpm aces sync --contentful. |
--optimization |
Make it personalizable from the start. See Optimization. |
2. Know the scaffold
Every cf component has the same five files. The page body picks the server or the client version, and both render the same render.tsx.
cf-logo-wall/
services.ts the query: what to fetch
render.tsx the markup (CfLogoWall)
index.tsx live site: fetch, then render
client.tsx Draft Mode: fetch + live updates
skeleton.tsx loading / missing data
You edit two files: services.ts (what to fetch) and render.tsx (the markup). The look goes in a skin file (step 6). index.tsx and client.tsx stay as they are, because they spread whatever the query returns into the render.
Here is the generated services.ts:
export const LogoWallFragment = gql`
fragment LogoWall on LogoWall {
internalTitle
# TODO: add fields
sys {
id
}
}
`;
export const LogoWallQuery = gql`
${LogoWallFragment}
query ($id: String!, $preview: Boolean!, $locale: String) {
logoWall(id: $id, preview: $preview, locale: $locale) {
...LogoWall
}
}
`;
export const fetchLogoWallData = async (id: string, preview = false, locale: string = defaultLocale) => {
const client = preview ? cfPreviewClient : cfClient;
const { data } = await client.query({ query: LogoWallQuery, variables: { id, preview, locale } });
return data?.logoWall ?? null;
};
And here is index.tsx, the server component. You don’t need to change it:
export const CfLogoWallServer = async ({ id, preview, lang }: CfFetchById) => {
let data;
try {
data = await fetchLogoWallData(id, preview, lang);
} catch (error) {
console.error("Failed to fetch Logo Wall:", error);
return <LogoWallSkeleton />;
}
if (!data) return <LogoWallSkeleton />;
return <CfLogoWall {...data} id={id} lang={lang} preview={preview} />;
};
client.tsx is the same, except that it fetches in a useEffect and passes the data through useContentfulLiveUpdates. That’s what makes edits appear in Live Preview as the editor types.
3. Add the fields in Contentful
Open Content model → 🔷 Logo Wall and add two fields:
| Field | ID | Type | Settings |
|---|---|---|---|
| Headline | headline |
Short text | Localized |
| Logos | logos |
References, many | Accept only 🌐 Image entries |
Save the content type. Then save it to the repo, so contentful/models.json matches the space for the next project or environment:
pnpm aces models export
4. Fetch the fields
In services.ts, add the fields to the fragment. Images reuse the shared ImageFragment:
import { cfClient, cfPreviewClient, gql, ImageFragment } from "@aces/contentful";
export const LogoWallFragment = gql`
${ImageFragment}
fragment LogoWall on LogoWall {
internalTitle
headline
logosCollection(limit: 12) {
items {
...Image
}
}
sys {
id
}
}
`;
A multiple-reference field logos is logosCollection { items } in GraphQL. Always set a limit, because Contentful caps query complexity.
5. Render them
Replace the scaffolded render.tsx body. Three things matter here:
- Live Preview props:
ContentfulLivePreview.getPropson each editable element lets editors click it in the preview to jump to the field. - Structure only: layout classes (grid, gap, spacing) and
data-slothooks; no colors, borders or fonts. Those go in the skin (next step). - Imports: UI comes from
@aces/ui, and other cf components are imported by path.
"use client";
import { ContentfulLivePreview } from "@contentful/live-preview";
import type { CfBaseComponent, CfImageProps } from "@aces/types";
import { Container, H2 } from "@aces/ui";
import { generateId } from "@aces/utils";
import { CfImage } from "../cf-image/render";
export interface CfLogoWallProps extends CfBaseComponent {
headline?: string;
logosCollection?: { items: CfImageProps[] };
}
export const CfLogoWall = ({ internalTitle, headline, logosCollection, __typename, id, lang, preview }: CfLogoWallProps) => (
<section id={generateId(internalTitle)} data-component={__typename} data-slot="logo-wall" className="my-4 md:my-8">
<Container>
{headline && (
<H2 data-slot="logo-wall-title" className="mb-6" {...ContentfulLivePreview.getProps({ entryId: id, fieldId: "headline", locale: lang })}>
{headline}
</H2>
)}
<ul
data-slot="logo-wall-list"
className="grid grid-cols-2 gap-2 md:grid-cols-4"
{...ContentfulLivePreview.getProps({ entryId: id, fieldId: "logos", locale: lang })}
>
{logosCollection?.items.filter(Boolean).map((logo) => (
<li key={logo.sys?.id} data-slot="logo-wall-item">
<CfImage {...logo} id={logo.sys?.id ?? ""} nested lang={lang} preview={preview} />
</li>
))}
</ul>
</Container>
</section>
);
6. Give it a look
On the Base skin, add src/styles/skin/logo-wall.css and import it from src/styles/skin/index.css. On an Unstyled project, put the file in src/styles/custom/ and import it from src/app/styles.css with layer(aces-skin). Use tokens, so it follows a rebrand and the section’s data-surface:
[data-slot="logo-wall-item"] {
padding: calc(var(--spacing) * 6);
border: 1px solid var(--color-border);
border-radius: var(--radius-card);
background: var(--color-subtle);
}
If the block needs its own background, set data-surface="alt" (or accent, inverse) on the <section> instead of a color. See Styling.
7. See it in Live Preview
pnpm dev
- In Contentful, open a Page and add a Logo Wall entry to its Page Body.
- Give it a headline and link a few Image entries.
- Watch the Live Preview pane. The block appears, and headline edits show as you type.
Then check it the way CI will:
pnpm typecheck && pnpm lint && pnpm build
pnpm aces doctor
Change it later
- More fields: add the field in Contentful, then add it to the fragment and to
render.tsx. Runpnpm aces models exportto keepmodels.jsonin sync. - Another page type: add
LogoWallto that page body’scomponentsinaces.config.ts, then runpnpm aces sync --contentful. - Personalize it:
pnpm aces component add-optimization LogoWall. - Remove it:
pnpm aces component remove "Logo Wall". Add--delete-content-typeto remove it from the space too.
Something wrong or missing? Open an issue.