Active form registry: preview the content being edited outside the form (inherited theme settings)

Open
#257 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 2 days

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
15/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Active
Tech stack
react, typescript
Domain
frontend

Research direction

Start with the form layer the issue builds on: FormProvider and useFormContext (#252), and the form store (#244), to see how per-field subscriptions work. The proposed ActiveFormProvider, useActiveForm and useInheritedSetting would live in @plone/helpers, and ContentForm would register itself. Resolve the four open questions before writing code; the issue names no existing file to edit.

Written by the indexing model from the issue text.

Description

04 type: enhancement

Summary

Some components outside a form need to preview what's being edited, before it's saved. The typical case is a theme whose header, logo, footer or colors come from a behavior inherited down the content tree: a subsite sets its own logo and colors, and its children inherit them. While an editor changes them, the header should show the new values live.

Since #244, each form has its own store, reached through React context (FormProvider / useFormContext, #252). That only reaches components inside the form's subtree, and a header slot rendered by the layout is outside it. This issue proposes a small active form registry: an app-level pointer to the form being edited, with its identity. It's not a global copy of the form's data.

Prior art: Volto Light Theme

VLT (kitconcept/volto-light-theme) stores these settings in behaviors (voltolighttheme.header, voltolighttheme.theme, kitconcept.footer). It reads them through plone.restapi's inherit expander: content['@components'].inherit[behavior] gives data and from (the object it's inherited from).

For the live preview, its useLiveData hook and Theming component read Volto's Redux form.global, a copy of the whole content form. The copy has no identity, so each consumer re-derives whether the form values should win:

  • add mode, detected from the URL (pathname.endsWith('/add'));
  • whether the edited type has the behavior, by comparing the inherited keys with the form schema;
  • whether you're adding directly inside the object that owns the inherited value, by comparing from['@id'] with the URL;
  • errorContext as a fallback.

It works, but it's fragile. And since form.global changes on every keystroke, every consumer re-renders on every keystroke.

The distinction

  • A global copy of the form data (Volto's form.global, Aurora's former formAtom) is the wrong tool: forms are per-instance, and a copy has no identity.
  • A global pointer to the active editing session is fine: there's one per app, like the toolbar.

Proposal

type ActiveForm = {
  /** The form's store (#252): subscribe to single fields. */
  form: FormApi<Content>;
  /** The edited object's `@id`, or `undefined` when adding. */
  id?: string;
  /** Where the object is being added, when adding. */
  parent?: string;
  /** Whether the edited type provides a behavior, from its schema
   * (plone.restapi sets `behavior` on each property). */
  provides(behavior: string): boolean;
};
  • An ActiveFormProvider at the app or layout level holds the current ActiveForm (or null). useActiveForm() reads it.

  • ContentForm registers itself when it mounts and unregisters when it unmounts. Other forms (block settings, control panels, the recurrence modal) don't register, so it's always clear which form is "the content being edited".

  • A resolver hook for inherited settings replaces VLT's heuristics with explicit checks:

    // Conceptually; the real hook can't call hooks conditionally.
    function useInheritedSetting<T>(content, behavior: string, field: string): T {
      const inherited = content['@components']?.inherit?.[behavior];
      const active = useActiveForm();
      const editingSource =
        active?.provides(behavior) &&
        (active.id === inherited?.from['@id'] || active.id === undefined);
      return editingSource ? useFieldValue(field) : inherited?.data[field];
    }
    
    Case Result
    Editing the object that owns the setting the form's value, live
    Adding a new object that provides the behavior (e.g. a new subsite) the form's value, live
    Editing a child that only inherits the setting the inherited value
    Server rendering, or no form open the inherited value
  • Re-renders: consumers subscribe per field through the form store, so a header re-renders only when a field it reads changes, not on every keystroke.

Both pieces live in @plone/helpers, next to the form layer. Themes (and VLT on Aurora) use the resolver hook instead of reading form data themselves.

Open questions

  1. Is the public header visible while editing? Today the edit route uses the CMS UI layout, so there's nothing to preview yet. This becomes relevant when editing happens inside the public layout. The blocks editor already renders inside .content-area, so themes already style its content.
  2. Colors are CSS custom properties: a theme component can set them on :root from the resolver, without restyling components.
  3. Data: the inherit expansion has to be on the content request. Add-ons can declare it through apiExpanders (#226).
  4. Is it worth it? Without live preview, header and colors already update right after saving, because forms remount per item and React Router revalidates the loaders. That may be enough for logos; colors are where editors want to see changes before saving.

Related: #244 (form engine), #252 (form layer), #226 (apiExpanders).

Dominant language
JavaScript
Stars
10
Forks
9
Avg merge
4d 7h
Merged PRs (30d)
54

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from plone/aurora

All issues in plone/aurora

Similar issues

More JavaScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.