# @hubspot/cms-components

Utilities and components for JS rendering on the CMS — the public NPM package customers import into their themes. Includes field components (`fields/`), data hooks (`hooks/`, `data/`), module/template helpers (`modules/`, `templates/`, `descriptors/`), and island hydration support (`islands/`).

See the [root README](../README.md#-packages) for how this fits with the rest of the platform, and [Deploying and Releasing](../docs/deploy.md#cms-components) for version-pinning/compatibility notes when consuming this package from elsewhere in the monorepo.

## Declaring module fields without JSX

`defineFields` (from `@hubspot/cms-components/fields`) declares a module's fields as an object instead of `<ModuleFields>` JSX. It returns the same `fields.json` entries, and `FieldValues` gives the module's prop types with no type-generation watcher:

```tsx
import type { ModuleProps } from '@hubspot/cms-components';
import { defineFields, field, type FieldValues } from '@hubspot/cms-components/fields';

export const fields = defineFields({
  headline: field.text({ label: 'Headline', default: 'Hello' }),
  align: field.choice({ label: 'Alignment', choices: [['left', 'Left'], ['center', 'Center']] }),
  cards: field.repeatedGroup({
    label: 'Cards',
    occurrence: { min: 1, max: 6 },
    children: { title: field.text({ label: 'Title', required: true }) },
  }),
});

export function Component({ fieldValues }: ModuleProps<FieldValues<typeof fields>>) {
  return <h1>{fieldValues.headline}</h1>;
}
```

- Each `field.<type>` takes the props of the matching JSX component, minus `name`: a field's name is its key. `field.group` and `field.repeatedGroup` take their fields as `children`.
- `field.choice` values are strings (`choices: [['left', 'Left'], ...]`, as `[value, label]` pairs), which is what the type generator emits; `<ChoiceField>`'s `choices` type is looser (`[any, string][]`).
- Every entry must come from a `field.*` builder.
- `FieldValues` is the type the dev server generates for the equivalent JSX fields. Groups and repeated groups are always required keys; any other field is required only with `required: true`. Choices are unions of their values: arrays for `multiple: true`, either for a `multiple` only known at runtime.
- Sets of fields are plain objects, so they can be shared and extended with spreads: `children: { ...cardFields, body: field.richtext({ label: 'Body' }) }`.
- `npx generate-fields <themeDir> [--module <Name>]` (from `@hubspot/cms-dev-server`) writes each module's `fields.json` for inspection, with no HubSpot account needed. `npx validate-fields` does the same and then validates the fields with HubSpot. Delete the written files before `hs project upload`.
- `FieldValues` leaves out `crmobjectproperty` and `variantmanagement` fields, as the generated types do: the type generator emits no type for them.

### Variations of a set of fields

For modules that share fields with variations, write a function that takes the extra fields (the same pattern as Zod's generic functions). Each module's `FieldValues` then has exactly the fields it was given: required, optional, or absent.

```ts
const moduleFields = <E extends FieldShape>(extra: E) =>
  defineFields({ title: field.text({ label: 'Title', required: true }), ...extra });

export const fields = moduleFields({ cta: field.cta({ label: 'CTA', required: true }) });
```

If the function takes a flag instead, choose between whole sets and give it an overload per flag value. TypeScript computes one return type per function, so without overloads every call gets the union of both sets.

```ts
const base = { title: field.text({ label: 'Title', required: true }) };
const withCta = { ...base, cta: field.cta({ label: 'CTA', required: true }) };

function moduleFields(showCta: true): DefinedFields<typeof withCta>;
function moduleFields(showCta: false): DefinedFields<typeof base>;
function moduleFields(showCta: boolean): DefinedFields<typeof withCta> | DefinedFields<typeof base>;
function moduleFields(showCta: boolean) {
  return showCta ? defineFields(withCta) : defineFields(base);
}

export const fields = moduleFields(false); // FieldValues: { title }
```

A flag only known at runtime gets the last overload: `FieldValues` is the union of the two sets' types.

Avoid a condition inside one set, such as `defineFields({ ...base, ...(showCta ? withCta : {}) })`. TypeScript gives such a set one type for every value of the condition, so `FieldValues` can't tell whether `cta` is there: it types `cta` as optional even when it has `required: true`, unlike the type the dev server generates for the build where it is there.

## Scripts

- `build` / `build:prod` / `build:watch` — builds the package
- `build-types` — generates TypeScript type declarations
- `docs:field-types` — generates field-type documentation
- `test` / `test:watch` — vitest
- `publint` — validates the published package shape before release
