Customization

Custom Fields

Your own input for a field, in any UI library.

The built-in field types cover most content. When editors need a different input, like a color picker or a star rating, a custom field type adds one. You write the input with plain DOM, Vue, React or any other library, and Tenon stores the value like any other field. This page shows how to add one.

Add a custom field

Use the type

A field of type custom:<name> uses the input you register under <name>:

cms/collections/products.ts
export const products = defineCollection({
  fields: {
    title: { type: "text", required: true },
    color: { type: "custom:color", label: "Color" },
  },
});

Write the input

Write the input in plain DOM, or as a Vue or React component. It gets the field's context, with the value and onChange:

// src/admin/color.client.ts
import type { FieldContext, Mount } from "tenoncms/admin";

export const mountColor: Mount<FieldContext> = (el, { value, field, onChange }) => {
  const input = Object.assign(document.createElement("input"), {
    type: "color",
    value: value ?? "#000000",
    disabled: field.readOnly,
  });
  input.oninput = () => onChange(input.value);
  el.append(input);
  return () => input.remove();
};

In plain DOM, the input is a mount function: it gets an empty element and the context, and returns a cleanup. It lives in a module whose name ends in .client.ts, so it only runs in the browser. See Browser modules.

Register it

A component needs one more line: renderVue() or renderReact() turns it into the mount function, in a .client.ts module:

// src/admin/color.client.ts
import { renderVue } from "tenoncms/admin/vue";
import ColorInput from "./ColorInput.vue";

export const mountColor = renderVue(ColorInput);

The components run with your own vue or react-dom, so your framework compiles them. The admin brings its own Vue, and you install nothing extra. See Vue, React and your framework.

Then import the input in your config, and register it under the name:

tenon.config.ts
import { mountColor } from "./src/admin/color.client";

export default defineCms({
  // ...
  admin: {
    fields: { color: { mount: mountColor } },
  },
});

The admin now shows a color picker for color. The site reads the value as a string, like "#ff6600".

What the admin does

  • It renders the label and the description. Your input only renders the control.
  • It saves what you pass to onChange with the document.
  • A custom field stores a string. Without a registered input, the admin shows a text input.
  • The admin's .input and .btn classes style your markup like the built-in inputs. See Styles and UI libraries.

The input gets this context:

KeyValue
nameThe field name, like color
fieldtype, label, required and readOnly from the config
valueThe current value, or null / undefined when empty
onChangeSets the value
dataWhat the field's load returned. undefined without one.
tenonHelpers from the admin, like locale

The Vue and React components get new props on every change. The plain DOM input renders once. To follow a new value there, return { update, unmount }. See Build a custom field. Other libraries, like Svelte, work the same way: mount into the element, and unmount in the cleanup.