diff --git a/docs/design/headless-combobox.md b/docs/design/headless-combobox.md new file mode 100644 index 0000000000..d02da73a9c --- /dev/null +++ b/docs/design/headless-combobox.md @@ -0,0 +1,504 @@ +# Headless combobox + +Status: exploration, first target of [headless-widgets.md](./headless-widgets.md). Read that document first; this one only holds what is specific to `combobox-web`. + +## 1. Where the seam is today + +The combobox is closer to the concept than it looks. The `Selector` interface in `src/helpers/types.ts` is already a headless model: options, captions, selection, search term, lazy loading, validation, read-only state, Mendix binding. It knows nothing about HTML. + +The other half is tangled into the UI. `SingleSelection.tsx` and `MultiSelection.tsx` each do three jobs at once: wire downshift (keyboard, ARIA, open state), wire floating-ui (positioning), and render Atlas markup with `form-control` classes. So the split is three-way: + +| Today | Becomes | +| --------------------------------------------------------------- | -------------------------- | +| `helpers/` providers and selectors, `useGetSelector` | Core store and ViewModel | +| `useDownshiftSingleSelectProps`, `useDownshiftMultiSelectProps` | `useComboboxBehavior` | +| `useFloatingMenu` | `useComboboxPopover` | +| `components/`, `ui/Combobox.scss` | Atlas View, parts, classes | +| `Combobox.tsx` | Thin wrapper | + +`useGetSelector` already hand-rolls a store and forces re-renders with `setInput({})`. Replacing it with a MobX store and the snapshot hook is a small move. + +Two side channels in the current code disappear once the Core owns UI state, and are worth knowing about before touching the hooks: + +- `useDownshiftSingleSelectProps` and its multi counterpart assign `selector.onLeaveEvent = closeMenu`, so the model holds a callback into downshift. With `isOpen` in the Core, `leave()` closes the menu directly. +- `useGetSelector` registers `onAfterSearchTermChange` on the options provider purely to trigger a React re-render. With MobX, the search term is observable and no callback is needed. + +The selector families (association, database, static, enum/boolean) each have a `Preview/` variant used only by `Combobox.editorPreview.tsx`. Since the editor preview keeps its own rendering, the preview providers can stay in the widget package, or ship in the core package as a `preview` entry. Decide in migration step 1. + +The dropdown-filter plugin (`shared/widget-plugin-dropdown-filter`) is the in-repo precedent: controllers, stores, a `classes()` map in `picker-primitives.tsx`, and a `Combobox` control driven by a `useComboboxProps` factory. It was never designed as a public surface, but its shape is the right one. + +## 2. State ownership decisions + +All of `isOpen`, `inputValue`, and `highlightedIndex` live in the Core: + +- `inputValue` drives the datasource filter (`setSearchTerm`) and the `onChangeFilterInputEvent` with its debounce. +- `onLeaveEvent` closes the menu, so the Core must own `isOpen`. +- `highlightedIndex` is needed for the ARIA status message. A library View feeds it when its library reports the highlight (Material UI does) and leaves it at -1 when it does not (Ant Design); the status message then carries the option count only. + +Downshift stays as the Atlas View's keyboard handling and ARIA (section 4), fully controlled: `isOpen`, `inputValue`, `selectedItem`, and `highlightedIndex` are all passed in from the Core and every `onXChange` forwards to a Core command. The key handling and accumulated bug fixes in the two downshift hooks survive the move unchanged. No other View needs it: the hand-rolled Views of the Studio Pro trial (step 8) handle keys over the commands, the library Views bring their own. + +Chip order for multi-select (`selectedItemsSorting`) is a model concern, exposed as the order of `chips`, because it is a Studio Pro property with data meaning. It is not a hint. + +## 3. ViewModel + +```ts +interface ComboboxOptionVM { + id: string; + caption: string; // always present: the caption attribute or expression + content?: ReactNode; // present whenever a custom-content widget is configured, regardless of placement + selected: boolean; + highlighted: boolean; +} + +interface ComboboxHints { + readOnlyStyle: "control" | "text"; + selectedItemsStyle: "boxes" | "text"; + loadingType: "skeleton" | "spinner"; + customContent: "none" | "everywhere" | "menuOnly"; // Studio Pro "no" | "yes" | "listItem" +} + +interface ComboboxViewModel extends WidgetViewModel, InputViewModel { + mode: "single" | "multi"; + a11y: InputA11y; // the live-region text stays in the Atlas Behavior for the parity release, see below + + // state + options: ReadonlyArray; + value: string | ReadonlyArray | null; + selected: ComboboxOptionVM | null; // single mode, the selected option with its caption and content + chips: ReadonlyArray; // multi mode, selected items in configured order + selectedCaption: string; // the selection's caption: single caption, joined captions in the multi "text" style, else "" + placeholder: string; // what the input area shows while nothing is selected + inputValue: string; + isOpen: boolean; + highlightedIndex: number; + clearable: boolean; + filterable: boolean; + allSelected: "all" | "some" | "none"; // multi mode, select-all checkbox state + loading: { isLoading: boolean; hasMore: boolean }; + slots: { menuFooter?: ReactNode }; + + // commands, referentially stable + open(): void; + close(): void; + toggle(): void; + highlight(index: number): void; + select(id: string): void; + deselect(id: string): void; + toggleItem(id: string): void; + clear(): void; + selectAll(): void; + setInputValue(text: string): void; + loadMore(): void; + leave(): void; // from the input facet, narrowed: also closes the menu +} +``` + +Notes: + +- `caption` is always a string. The Core needs it anyway for filtering, downshift's `itemToString`, and the ARIA status message. +- `content` is the widget tree the app developer configured under "Custom content" (`optionsSourceAssociationCustomContent` and its database and static siblings). It is the one place React leaks into the contract. The Core builds it whenever the widget is configured and never decides where it is shown. Placement is the View's decision, guided by `hints.customContent`: the Atlas View renders `content` in the menu for `everywhere` and `menuOnly`, and in the input and chips for `everywhere` only. A custom View renders `content ?? caption` wherever it likes. This is safe because every use of the placement today is presentational: `CaptionsProvider.render`, the class and placeholder toggles in `SingleSelection`, and `getSelectedCaptionsPlaceholder`. +- `enter` and `leave` exist so a View that does not use our Behavior can still fire the Mendix enter and leave events. +- `placeholder` is separate from `selectedCaption` since 2026-11-04. One field had carried the empty option text, the selected caption, the joined captions and the empty string depending on mode and style, and three library Views got the placeholder wrong on the first try. The empty option text is not a key in the text map either: the Core resolves it, because a database combobox shows the attribute's display value while its value matches no option. +- All texts come through `vm.t`, see the generic document, section 7.1. The combobox text map: + +```ts +interface ComboboxTexts { + noOptions: []; + clearSelection: []; + removeSelection: []; + selectAll: []; + a11ySelectedValue: [caption: string]; + a11yOptionsAvailable: [count: number]; + a11yInstructions: []; + a11yNoOption: []; +} +``` + +The status message that downshift builds today by concatenating `a11ySelectedValue`, the count, `a11yOptionsAvailable`, and `a11yInstructions` becomes `a11y.statusMessage`, composed by the Core through `t`. A View using our Behavior gets it in the live region; a View without Behavior reads it directly. Parametrised keys replace today's string concatenation, so word order is no longer fixed to English. + +Status 2026-11-03: implemented in the package change `combobox-second-view`. The Core composes `a11y.statusMessage` and the Behavior hands it to downshift; the parametrised keys are still English-ordered templates until the system-text API exists. + +Status 2026-11-10: reverted for the parity release (package change `combobox-parity-release`). The Core-composed text differed from the released widget in three places, so the Behavior composes the message again with the released wording, including downshift's previous result count and highlighted item; `a11y.statusMessage` leaves the ViewModel and comes back with the first View that runs without the Behavior. + +### Structural sharing slices + +`inputSlice` (inputValue, isOpen, highlightedIndex), `selectionSlice` (value, selected, chips, allSelected), `optionsSlice` (array of per-id `optionSlice` entries), `loadingSlice`, `staticSlice` (hints, t, clearable, filterable, a11y). Typing a character rebuilds `inputSlice` only. Moving the highlight creates exactly two new option objects. + +## 4. Behavior + +The Atlas View's keyboard handling and ARIA: downshift in controlled mode behind prop getters. It ships beside the Atlas View and a raw-markup View may reuse it (level 3a); the contract does not require it, see the generic document, section 4, rule 6. + +```ts +interface ComboboxBehavior { + getRootProps(): { onFocus; onBlur }; // enter and leave detection via relatedTarget + getInputProps(): { + id; + role: "combobox"; + value; + onChange; + onKeyDown; + onFocus; + onBlur; + ref; + "aria-expanded"; + "aria-controls"; + "aria-activedescendant"; + "aria-autocomplete"; + "aria-invalid"; + "aria-describedby"; + "aria-required"; + "aria-label"; + "aria-labelledby"; + "data-readonly"; // filtering off or read-only; open and disabled are aria-expanded and disabled + }; + getToggleProps(): { tabIndex: -1; onClick; onMouseDown; "aria-label"; "data-open" }; + getMenuProps(): { + id; + role: "listbox"; + "aria-labelledby"; + "aria-multiselectable"; + onMouseDown; + onScroll; + ref; + "data-lazy"; + "data-highlighted"; + }; + getOptionProps(index: number): { + id; + role: "option"; + "aria-selected"; + onClick; + onMouseMove; + "data-highlighted"; + "data-selected"; + }; + getChipProps(index: number): { tabIndex; onKeyDown; ref; "data-active" }; + getChipRemoveProps(index: number): { onClick; "aria-label"; tabIndex: -1 }; + getClearProps(): { onClick; "aria-label"; tabIndex }; + getSelectAllProps(): { onClick; "aria-label"; checked: "all" | "some" | "none" }; +} + +function useComboboxBehavior(vm: ComboboxViewModel): ComboboxBehavior; +``` + +Behaviour that must survive, with its current home: + +- Backspace on an empty single-select input clears when clearable (`SingleSelection.onInputKeyDown`). +- Backspace and ArrowLeft redirect to chips only when the caret rule in `isChipNavigationPermitted` allows it (`MultiSelection`). +- Focus restoration after chip removal (`MultiSelection`, chipRefs effect). +- Space toggles the highlighted item in multi mode without closing. +- Menu `onMouseDown` prevents blur; header and footer stop propagation so clicks inside do not close. +- Lazy loading triggers from menu scroll (`useLazyLoading`). + +## 5. Popover + +```ts +function useComboboxPopover(vm: ComboboxViewModel, options?: { alwaysOpen?: boolean }): ComboboxPopover; +``` + +Wraps today's `useFloatingMenu`: fixed strategy, bottom-start, flip, size capped at 320px, width matched to the reference. `alwaysOpen` is the editor preview's `keepMenuOpen` and disables floating entirely. + +## 6. Classes + +```ts +type ComboboxClassKey = + | "root" + | "inputContainer" + | "selectedItems" // the input area: chips, input and placeholder + | "input" + | "placeholder" // what the input area shows while nothing is typed + | "caption" + | "customContent" + | "chip" + | "chipRemove" + | "clear" + | "clearIcon" + | "toggle" + | "toggleIcon" + | "icon" + | "menu" + | "menuHeader" + | "selectAll" + | "menuList" + | "menuItem" + | "noOptions" + | "menuFooter" + | "skeleton" + | "skeletonBar" + | "spinner" + | "spinnerIcon"; + +function comboboxClasses(prefix = "widget-combobox"): Record; +``` + +The values are the class names the released widget always had (`menuItem` is `widget-combobox-item`, `chip` is `widget-combobox-selected-item`, `toggle` is `widget-combobox-down-arrow`), so app CSS that targets an element keeps matching. Validation is `ValidationAlert` from component-kit with its Atlas classes, and the unavailable placeholder is rendered by the wrapper, not the View; neither is a key. + +State is a data attribute on the element, never a modifier class. The attributes that come from the Behavior getters are the state downshift and the Behavior own; the rest are set by the View from the ViewModel and the hints. The released modifier classes are still emitted next to the attributes for one release, from `legacyModifiers.ts` and deprecated in the CHANGELOG, so the step 4 DOM diff is additive and nothing breaks for apps. + +| Modifier class before step 4 | Becomes | +| ------------------------------------------------------------------------------- | -------------------------------------------------- | +| `widget-combobox-input-container-active` | `inputContainer` + `data-open` | +| `widget-combobox-input-container-disabled` | `inputContainer` + `data-disabled` | +| `widget-combobox-multiselect` | `inputContainer` + `data-has-selection` | +| `widget-combobox-boxes`, `widget-combobox-text` | `selectedItems` + `data-style="boxes" \| "text"` | +| `widget-combobox-custom-content` | `selectedItems` + `data-custom` | +| `widget-combobox-input-nofilter` | `input` + `data-readonly` (Behavior) | +| `widget-combobox-placeholder-custom` | `placeholder` + `data-custom` | +| `widget-combobox-placeholder-empty` | `placeholder` + `data-empty` | +| `active` on the toggle icon | `toggle` + `data-open` (Behavior) | +| `widget-combobox-menu-hidden` | `menu` without `data-open` | +| `widget-combobox-menu-lazy-scroll` | `menuList` + `data-lazy` (Behavior) | +| `widget-combobox-menu-highlighted` | `menuList` + `data-highlighted` (Behavior) | +| `widget-combobox-item-highlighted` | `menuItem` + `data-highlighted` (Behavior) | +| `widget-combobox-item-selected` | `menuItem` + `data-selected` (Behavior) | +| `widget-combobox-menu-header-select-all-button-disabled` | `selectAll` + `data-disabled` | +| none | `chip` + `data-active` (Behavior) | +| `widget-combobox-spinner-margin` | `spinner` + `data-margins` | +| `widget-combobox-skeleton-loader-small`, `widget-combobox-spinner-loader-small` | `skeletonBar`, `spinnerIcon` + `data-size="small"` | + +`form-control` and `form-control-static` are applied by the Atlas View only, from `hints.readOnlyStyle`. + +Status 2026-11-02: implemented in the package change `combobox-classes`; `src/components/classes.ts` is the map, and the table above is the one in `Combobox.scss`. + +## 7. Parts + +```ts +interface ComboboxParts { + InputContainer; // the form-control box; the input area, Clear and Toggle are its children + Placeholder; // what the input area shows while nothing is typed + Chip; // one selected item of a multi-select, with its remove control + Clear; + Toggle; + Validation; + Menu; // the popover: header, list and footer; the options, NoOptions and Loader are its children + SelectAll; // the menu header of a multi-select + Option; + NoOptions; + Loader; +} +``` + +Eleven parts, each something a View author would replace as a whole, which is fewer than the class keys on purpose. The root, the input area and the input element stay in the View: they carry the Behavior wiring and nothing worth redoing without redoing the View. The chip remove control, the menu header, list and footer, the icons and the loader rows are elements inside their part and reachable through `classes`. Each part receives the ViewModel where it reads from it, plus the Behavior props of its element (`Option` receives `option`, `index` and `optionProps`; `Chip` receives `chip`, `index`, `chipProps` and `removeProps`). Parts read the resolved class map through `useComboboxClasses()`, a context the View provides, so one `classes` override reaches every element including the captions and icons rendered inside a part. The Atlas View is the composition of these parts and nothing else: one tree for both modes, the chips and the select-all control rendered in multi mode only. + +Status 2026-11-02: implemented in the package change `combobox-parts`; `src/components/parts.ts` is the map, `src/components/parts/` the components, `src/components/ComboboxView.tsx` the composition. + +## 8. View contract + +```ts +interface ComboboxViewProps { + vm: ComboboxViewModel; + classes?: Partial; + parts?: Partial; +} +type ComboboxViewComponent = ComponentType; +``` + +`classes` overrides replace the Atlas class of the given keys and leave the others; `parts` replaces the given parts and leaves the others. The View takes the snapshot and the wrapper subscribes (generic document, section 9); the Atlas View additionally takes the editor preview's `keepMenuOpen` and `className`. Every View puts `vm.id` on the combobox input, which is where the Studio Pro label points, spreads the focus boundary on its root and lets that root fill its container. + +## 9. Examples per customisation level + +All of these are files in the app project, see the delivery section of the generic document. Each exports a `ComboboxViewComponent` and nothing else. + +### Level 0: CSS only, no JavaScript + +```css +.widget-combobox-menu-item[data-highlighted] { + background: var(--brand-50); +} +.widget-combobox-menu-item[data-selected] { + font-weight: 600; +} +``` + +### Level 1: a few class touches + +```tsx +import { ComboboxView, type ComboboxViewComponent } from "@mendix/combobox-kit"; + +export const MyCombobox: ComboboxViewComponent = props => ( + +); +``` + +Tailwind, same file shape: + +```tsx +export const MyCombobox: ComboboxViewComponent = props => ( + +); +``` + +### Level 2: replace one part + +```tsx +import { ComboboxView, type ComboboxParts, type ComboboxViewComponent } from "@mendix/combobox-kit"; + +const MyOption: ComboboxParts["Option"] = ({ option, optionProps }) => ( +
  • + + {option.content ?? option.caption} +
  • +); + +export const MyCombobox: ComboboxViewComponent = props => ; +``` + +Keyboard and ARIA keep working because `optionProps` still comes from our Behavior. A replaced part brings its own classes; `useComboboxClasses()` is there if it wants ours. + +### Level 3a: full View with our Behavior (Tailwind, raw markup) + +```tsx +import { useComboboxBehavior, useComboboxPopover, type ComboboxViewComponent } from "@mendix/combobox-kit/atlas"; + +export const MyCombobox: ComboboxViewComponent = ({ vm }) => { + const b = useComboboxBehavior(vm); + const pop = useComboboxPopover(vm); + return ( +
    + + + {vm.isOpen && ( +
      + {vm.options.map((o, i) => ( +
    • + {o.content ?? o.caption} +
    • + ))} +
    + )} + {vm.validation &&

    {vm.validation}

    } +
    + ); +}; +``` + +### Level 3b: full View with a component library (Material UI, no Behavior) + +```tsx +import Autocomplete from "@mui/material/Autocomplete"; +import TextField from "@mui/material/TextField"; +import type { ComboboxViewComponent } from "@mendix/combobox-kit"; + +export const MyCombobox: ComboboxViewComponent = ({ vm }) => { + const byId = (id: string | null) => vm.options.find(o => o.id === id) ?? null; + return ( + o.caption} + renderOption={(props, o) =>
  • {o.content ?? o.caption}
  • } + value={vm.mode === "single" ? vm.selected : vm.chips} + onChange={(_, next) => (next ? vm.select(next.id) : vm.clear())} + inputValue={vm.inputValue} + onInputChange={(_, text) => vm.setInputValue(text)} + disabled={vm.readOnly} + loading={vm.loading.isLoading} + renderInput={params => ( + + )} + onFocus={vm.enter} + onBlur={vm.leave} + /> + ); +}; +``` + +Material UI owns keyboard and ARIA here. The Core still owns data, Mendix binding, lazy loading, and events. `highlightedIndex` is ignored, which is harmless. `getOptionLabel` uses the plain `caption`; `renderOption` shows custom `content` when the app developer configured it, ignoring the `menuOnly` versus `everywhere` hint, which is the View's prerogative. + +## 10. Thin wrapper and package layout + +```tsx +// packages/pluggableWidgets/combobox-web/src/Combobox.tsx +export default function Combobox(props: ComboboxContainerProps) { + const { vm } = useComboboxCore(props); + const View = useWidgetView("Combobox", props.uiVariant); + return ; +} +``` + +The wrapper renders no element: the View's root is the View's. The combobox has the Label system property, so the client renders the form-group wrapper with the label, Class and Style, and the View links the label by putting `vm.id` on the input. + +``` +packages/ + kits/combobox-kit/ @mendix/combobox-kit + src/main.ts ".": ComboboxViewModel and its types, useComboboxCore, createComboboxContainer + src/atlas.ts "./atlas": ComboboxView, parts, classes, useComboboxBehavior, useComboboxPopover + src/conformance.ts "./conformance": describeComboboxConformance + src/preview.ts "./preview": createPreviewContainer, the placeholder text for the editor + src/core/ ComboboxCore, container, config, gate, services, ViewModel builder + src/helpers/ the selectors and providers per data source family + src/preview/ the editor preview selectors + src/hooks/, src/atlas/ the Atlas View and its hooks + src/typings/ComboboxProps.ts copy of the widget's generated typings, guarded by a spec + AGENTS.md + pluggableWidgets/combobox-web/ Combobox.tsx wrapper, XML, editorConfig, editorPreview, + icons, ui/Combobox.scss, the three widget specs with their snapshots +``` + +`Combobox.xml` gains one property: `uiVariant`, string, default `atlas`, in the Advanced group. `Combobox.editorPreview.tsx` stays as it is. + +## 11. Safety net + +The existing net is thinner than the migration needs. Status at 2026-10-28: + +| Layer | Coverage | +| -------------------- | -------------------------------------------------------------------------------------------------------------------------- | +| Component unit tests | 29 cases across `SingleSelection`, `MultiSelection`, `StaticSelection`, with 3 DOM snapshots | +| Helper unit tests | 20 cases, all on the database selector and its utils | +| E2E | 28 Playwright cases with 17 visual snapshots: every data source family, keyboard chip handling, onChange, menu positioning | + +Not covered directly at that date: association, static, enum, and boolean selectors; lazy loading; the filter-event debounce; enter and leave events; custom content; validation display. These are exactly what step 1 touches. + +Update, same day: the OpenSpec change `combobox-selector-safety-net` in the package added characterisation tests for rule 1 below. The unit suite went from 6 suites and 61 tests to 16 suites and 194 tests with the 5 snapshots untouched. Every selector family, `getSelector`, `BaseOptionsProvider` filtering, `LazyLoadProvider`, `datasourceFilter` and the `useGetSelector` seam are covered, and each pinned quirk is marked `// Quirk:` in the test and listed in that change's `design.md`. Enter and leave events, custom content rendering beyond `caption.render`, and validation display remain covered only through the component tests. + +Rules, in order of application: + +1. **Characterisation tests first, at the seam that does not move.** The `Selector` interface stays the Core's internal API in step 1. Before any change, write tests against it for every family using the builders in `widget-plugin-test-utils`: `currentId`, `setValue` writing to the attribute or association, `options.getAll` under a search term, `caption.get`, `readOnly`, `validation`, `clearable`, `loadMore`, and `updateProps` transitions from unavailable to available. They pass on the old code and must pass unchanged on the new code. This is what the package's tdd-refactor OpenSpec schema calls "existing behaviour preservation tests". +2. **Strangler, not rewrite.** Step 1 changes the inside of `useGetSelector` and the providers only. `components/` and its tests are untouched. A snapshot diff in steps 1 to 3 is a regression; `pnpm run test -u` is not allowed in those steps. +3. **Split the store change from the re-render change.** Convert the store to MobX while keeping the `setInput({})` trigger, verify, then replace the trigger with a reaction in a separate commit. Two small diffs bisect; one large one does not. +4. **MobX strict mode in tests.** `configure({ enforceActions: "always" })` in the Jest setup, so any mutation outside an action throws in CI instead of showing up as stale UI in Studio Pro. +5. **Never make Mendix values observable.** `ListValue`, `EditableValue`, `ListWidgetValue` go in as `observable.ref` through a props gate, the pattern `widget-plugin-mobx-kit` and the dropdown-filter stores already use. Wrapping one in `makeAutoObservable` breaks silently. +6. **E2E is the gate per step.** Visual snapshots stay identical through step 3. Step 4 (classes and data attributes) is the only step that changes the DOM on purpose and the only step where snapshots are updated, with the diff reviewed in the PR. +7. **Run the component suite against both paths in step 2.** Keep the Selector path alive behind a test-only switch while the ViewModel path is introduced, parametrise the component tests to run twice, delete the old path once both are green. +8. **Manual checklist in Studio Pro** on the `combobox-web-atlas4` test project: one page per data source family, plus read-only, validation, custom content, lazy loading, and the onChange flows. Automated tests do not exercise the editor preview, and the preview providers share code with the runtime ones. + +## 12. Migration plan + +Each step keeps the MPK, the XML, and the existing unit and E2E tests green. + +1. **Extract the store.** Precondition: section 11, rules 1 to 5 in place. Move `getSelector`, the providers, and the selectors into a MobX store under a Core. Replace the forced re-render in `useGetSelector` with the snapshot hook. No change to `components/`. + Status 2026-10-29: done in the package change `combobox-mobx-store` (stacked on the safety-net branch). The selectors and providers are observable, `useObservableSnapshot` replaces the `setState({})` trigger and subscribes to the search term only. The props gate was deferred to step 2 because the `Selector` seam still takes props by method call. +2. **Build the ViewModel.** Add the computed slices from section 3. Rewrite `SingleSelection` and `MultiSelection` to read from the ViewModel instead of the Selector. Whatever they cannot get from the ViewModel is a gap in the contract; fix the contract, not the View. + Status 2026-10-30: done in the package change `combobox-viewmodel`, on the same branch as step 1. `ComboboxCore` derives the ViewModel as cached slices, the two views render from the snapshot only, and `onAfterSearchTermChange` is gone. The Core is assembled by a brandi container (`src/core/Combobox.container.ts`), the composition root pattern of datagrid-web and gallery-web: a frozen config for the static properties, a props gate for the Mendix values, the selector family bound per instance, and a setup host for the debounced filter-input action. `getSelector` and `useGetSelector` are gone with it. Deferred to step 3: `isOpen`, `inputValue`, `highlightedIndex` and `highlighted` per option (downshift still owns them), and the selector reads inside the downshift and lazy-loading hooks. Deferred to step 4: option `content` is still rendered by the captions providers, so the Core does decide the caption markup until then. Additions to the contract found on the way: `selectedContent`, `canSelectAll`, `loading.lazy`, `hints.selectionMethod`, and `tabIndex` on the envelope. +3. **Extract Behavior and Popover.** Make downshift controlled by the Core. Move `useDownshift*` and `useFloatingMenu` into the two hooks with the getter shapes from sections 4 and 5. Verify every item in the "behaviour that must survive" list. + Status 2026-11-02: done in the package change `combobox-behavior`. The Core owns `isOpen` and `highlightedIndex`, `inputValue` is the search term, `enter` and `leave` run the Mendix actions and `leave` closes, the lazy first page is requested by `open()`. `useComboboxBehavior` runs downshift fully controlled and returns the section 4 getters without the data attributes (step 4); `useComboboxPopover` wraps the floating menu. The views take the ViewModel only. Deferred: `a11y.statusMessage` stays composed in the Behavior because the multi-mode text depends on downshift's previous result count; the selectors still take props through `updateProps`, split off as `combobox-selector-gate`. + Status 2026-11-02, later the same day: `combobox-selector-gate` done. The selectors are computeds over the props gate and the frozen config, the way the data grid and gallery models derive from theirs; the datasource limit, filter and selection writes are setup reactions; `container.setProps` is the gate alone. One deliberate fix on the way: a static combo box follows an external clear of its attribute. +4. **Introduce classes and data attributes.** Replace literal class names with the map and modifier classes with data attributes per the table in section 6. Update `Combobox.scss` and the E2E selectors. + Status 2026-11-02: done in the package change `combobox-classes`. `src/components/classes.ts` holds the map with the released class names as values; the Behavior getters carry the state attributes of their element and the View sets the hint-derived ones; `ComboboxOptionVM.content` is the custom content only, `OptionContent` places it per `hints.customContent`, and the captions providers lost `render`; `selectedContent` became `selectedCaption`, kept in the Core because the database selector's empty caption is the attribute's display value. The five DOM snapshots were updated once, on purpose, and the diff is additive: two data attributes next to the classes that were there. The modifier classes stay for one release, deprecated in the CHANGELOG, so app CSS keeps matching. Still deferred: `a11y.statusMessage`, until a View without our Behavior needs it (step 7). +5. **Split into parts.** Break the Atlas View into one component per class key. Add the `classes` and `parts` props. + Status 2026-11-02: done in the package change `combobox-parts`. Eleven parts, not one per class key: a part is something worth replacing as a whole, the rest stays inside its part and is reachable through `classes`. `ComboboxView` is one component for both modes with the `classes` and `parts` props; the parts read the resolved classes through a context hook; `SingleSelection`, `MultiSelection` and the three wrappers are gone. The five DOM snapshots are untouched. Still taking `vm` instead of the Core, until the wrapper of step 6. +6. **Move to a package.** Create `packages/kits/combobox-kit`, point the widget at it, reduce `Combobox.tsx` to the wrapper in section 10. + Status 2026-11-05: done in the package change `combobox-package-move`. Two private workspace packages: `@mendix/widget-kit` holds the envelope, the input facet, `focusBoundaryProps` and `useObservableSnapshot`; `@mendix/combobox-kit` holds the core, helpers, hooks, the Atlas View, the conformance suite, the preview selectors and the sandbox, with the four entries of section 10. The widget keeps the XML, the editor files, the icons, the stylesheet, the wrapper, the trial registry and its three specs, and depends on the package as `workspace:*`; the MPK bundles it and is byte-for-byte the same shape. The Behavior's root props and the bare View use the shared focus helper. 91 files moved with history; the widget's 51 tests and 5 snapshots pass unchanged against `dist`, the package runs 226 and the generic package 3. +7. **Write the second View.** A throwaway Material UI View in a sandbox, run against the conformance suite. This is the real test of the ViewModel shape. + Status 2026-11-03: done before step 6, in the package change `combobox-second-view`. `src/conformance/describeComboboxConformance.tsx` is the suite of section 11 of the generic design: sixteen role-based cases over a live Core, run by the Atlas View and by `src/sandbox/BareView.tsx`, a hand-rolled View with its own keyboard handling and ARIA over the ViewModel commands instead of the Material UI View sketched here (same ViewModel surface, no dependency, deletable). The suite found one Atlas gap (`aria-multiselectable` on the multi-select list, fixed) and the status message moved into the Core. The ViewModel needed no change; what a View without the Behavior does itself is recorded in the change's `design.md`. + Status 2026-11-10: `BareView` is removed for the parity release (package change `combobox-parity-release`); it was never built into the MPK and is recoverable from history. The conformance suite stays and runs the Atlas View. +8. **Prove the in-project route.** Add `uiVariant` and the hardcoded first-iteration registry. Put a View file into a Mendix test project with the package in `node_modules`, let the project bundler compile it, hardcode its registration, and confirm the widget renders it with `uiVariant` set. + Status 2026-11-04: proven ahead of step 6 in a scratch Mendix project, as a trial that is not committed. The widget's Name suffix stood in for `uiVariant` (Class cannot be the switch: with the Label system property the client keeps Class and Style on the form-group wrapper), and a registry of five `lazy(() => import("@combobox-ui/"))` entries, left external by the widget's rollup config, stood in for the registration. Studio Pro's bundler resolves each specifier from the project's `node_modules` into a lazy chunk, so no registration call exists; the price is that the specifiers are a fixed list (section 13). Five Views ran over the ViewModel without a change to it: two hand-rolled (plain CSS, and Tailwind compiled into the chunk), Material UI Autocomplete, Ant Design Select, and the Siemens iX dropdown (web components, token-only CSS). What they taught is folded into the sections above: `placeholder` split from `selectedCaption` (section 3); `className` and `style` dropped from the envelope, the focus boundary made a helper and the full-width root a contract rule (generic document, sections 7 and 8.5), because every View repeated the full-width root, the focus boundary and the menu `mousedown` guard; the highlight as optional input (section 2); downshift as the Atlas View's (section 4); per-View CSS, the conformance split and the chip-navigation gap (generic document, sections 11 and 14). + +Publishing to npm comes after step 8. + +Status 2026-11-10: the first release of the migrated widget is a parity release. It ships the Core, MobX and the Atlas View with no change for app developers or their users; steps 7 and 8 (a second View, `uiVariant` and the registry) continue after it. The trial registry was removed on 2026-11-06 because the CI test project has no `node_modules`. + +## 13. Open questions (combobox) + +- Step 8's extension point: the registry is a fixed list of import specifiers because the widget's bundler must know them. Named slots, or one conventional app package that exports the variants by name for `uiVariant` to select among? +- Chip keyboard navigation (ArrowLeft, ArrowRight, Backspace, Delete on the chips) is Atlas Behavior today and absent from the conformance suite; two hand-rolled Views shipped without it. Does the suite require it of every View? +- `ComboboxTexts` has no key for the toggle control; the Atlas toggle is decorative and the bare View hides its own. Add `toggleMenu` when the texts move to the system-text API, or keep the toggle out of the accessibility tree by contract? +- Does the existing combobox OpenSpec folder get one change per migration step, or one change for the whole split? diff --git a/docs/design/headless-widgets.md b/docs/design/headless-widgets.md new file mode 100644 index 0000000000..edf927519f --- /dev/null +++ b/docs/design/headless-widgets.md @@ -0,0 +1,412 @@ +# Headless widgets: replaceable UI for pluggable widgets + +Status: exploration, not yet scheduled. First target: combobox, see [headless-combobox.md](./headless-combobox.md). + +This document is the widget-agnostic concept. Per-widget documents hold the concrete contract, examples, migration plan, and questions for one widget. + +## 1. Problem + +Every widget in this repository is a monolith. One component owns the Mendix binding (EditableValue, ListValue, ActionValue), the lifecycle, the keyboard and ARIA behaviour, the popover positioning, and the HTML structure styled with Atlas UI classes. Atlas is coupled to an outdated Bootstrap, so the markup and class structure of every widget is dictated by it. + +App developers increasingly want widgets to render in their own design system: Material UI, Ant Design, Tailwind, or a corporate component library. Today the only option is to override Atlas CSS against undocumented class names. Changing the HTML structure is impossible. + +## 2. Goal + +Split each widget into a part we own, which keeps the lifecycle, the Mendix API binding, and the hard behaviour, and a part the app developer may replace, which renders the HTML. Ship our own Atlas rendering as the default so existing apps see no change. + +App developers never rebuild widgets. They write or adjust a View as a file in their own project, with our package in `node_modules`, and the widget picks it up. We stay in control of the Core and the Marketplace widget. + +Non-goals for the first iteration: + +- Data grid. Its surface is too large to prove the concept on. +- Studio Pro design-mode preview of custom UIs. The editor preview keeps its own simplified rendering. +- A visual theming tool. This is a code-level contract. + +## 3. Vocabulary + +| Term | Meaning | +| ------------- | ----------------------------------------------------------------------------------------------------------------- | +| **Core** | Headless object per widget. Owns all state, the Mendix binding, and the lifecycle. Implemented with MobX. | +| **ViewModel** | Plain, immutable object the Core exposes. State plus stable commands. The only required public contract. | +| **View** | React component that renders a ViewModel. We ship the Atlas View. App developers may ship their own. | +| **Behavior** | Optional hook that turns a ViewModel into DOM prop getters: keyboard handling and ARIA attributes. | +| **Popover** | Optional hook that positions a floating element. Wraps floating-ui. | +| **Classes** | Typed map of CSS hook names, one per rendered part. Overridable. | +| **Parts** | Components the Atlas View is assembled from, one per thing worth replacing as a whole. Individually replaceable. | +| **Host** | Whatever creates a Core and mounts a View. The pluggable widget is the host in apps; our test harness is another. | +| **Variant** | Free-string name of a View, set per widget instance in Studio Pro through the `uiVariant` property. | + +"Headless" follows the industry use of the word (Headless UI, Radix Primitives, React Aria, Downshift). + +## 4. Layering + +``` +Mendix props ──▶ Core (MobX store, Mendix binding, all state) + │ + ├── ViewModel plain data + stable commands REQUIRED + ├── useXBehavior(vm) keyboard + ARIA prop getters optional + ├── useXPopover(vm) floating refs + styles optional + ├── classes() CSS hook names optional + └── parts one component per replaceable thing optional + │ + ▼ + View (Atlas default, or app developer's) +``` + +Rules that keep the layering honest: + +1. The Core knows nothing about HTML. No class names, no refs, no DOM events. +2. The ViewModel contains plain data and functions. No MobX observables, no React nodes except where a slot explicitly carries one. +3. The Atlas View is built only from the ViewModel and the optional helpers. If the Atlas View needs something the ViewModel does not expose, the ViewModel is incomplete. +4. Behavior, Popover, Classes, and Parts are each a function of the ViewModel. None of them hold model state. +5. Studio Pro properties that only describe Atlas presentation are exposed as advisory `hints`. A custom View may ignore them. +6. Behavior and Popover belong to the Atlas View. They are its keyboard handling, ARIA and positioning, shipped beside it and reusable by a raw-markup View, but not part of the contract: a View built on a component library brings its own, and the conformance suite (section 11) pins what the widget promises either way. downshift and floating-ui are therefore dependencies of the Atlas View, never of the Core. + +## 5. State ownership + +All state lives in the Core, including UI-looking state such as open flags, input text, and highlighted index. Reasons: + +- Mendix often cares about it. Input text may drive a datasource filter and an event; a leave event may close a menu. +- A View built on a component library (Material UI, Ant) runs that library in controlled mode against the ViewModel. Such libraries accept open, input value, and value as controlled props. +- One source of truth makes conformance tests (section 11) meaningful regardless of the View. + +Third-party behaviour libraries such as downshift may stay, but in fully controlled mode: the Core is the source of truth and the library reduces DOM events into Core commands. + +## 6. Reactivity: MobX inside, plain objects outside + +The Core is a MobX store. MobX is never exposed. The Mendix client bundles its own MobX version and widgets bundle another. `observer` from one copy cannot see observables from another, so exposing a MobX object would force every app developer onto our exact MobX instance. + +### 6.1 Snapshot hook + +The Core exposes `viewModel` as a MobX `computed`. A computed caches, so two reads with no state change in between return the same reference. That is exactly the contract `useSyncExternalStore` needs. + +```ts +function useWidgetViewModel(core: { viewModel: VM }): VM { + const subscribe = useCallback((onChange: () => void) => reaction(() => core.viewModel, onChange), [core]); + const getSnapshot = useCallback(() => core.viewModel, [core]); + return useSyncExternalStore(subscribe, getSnapshot); +} +``` + +A selector variant is offered next to it, built on `useSyncExternalStoreWithSelector`, so sub-components subscribe to a slice only: + +```ts +const isOpen = useWidgetState(core, vm => vm.isOpen); +``` + +### 6.2 Structural sharing + +The ViewModel is a tree of computeds, not one big computed. Each slice is its own cached getter. Per-item slices are cached by id with `computedFn`. + +```ts +class SomeCore { + get headerSlice() { + return { value: this.store.inputValue, isOpen: this.store.isOpen }; + } + get itemsSlice() { + return this.store.visibleIds.map(id => this.itemSlice(id)); + } + itemSlice = computedFn((id: string) => ({ + id, + caption: this.store.caption(id), + selected: this.store.isSelected(id) + })); + get viewModel() { + return { ...this.headerSlice, items: this.itemsSlice, ...this.commands }; + } +} +``` + +A change in one slice rebuilds that slice and the top-level object. Every other slice and every unchanged item keep their reference, so `React.memo` in a View skips them. This is what keeps plain-object snapshots cheap enough for lists. + +### 6.3 Commands are stable + +Commands are bound once in the Core constructor. Their identity never changes, so `onClick={vm.open}` does not defeat memoisation. + +### 6.4 Our own Views + +Views inside this repository may use `observer` from our MobX copy and read the store directly for fine-grained updates. The Atlas View deliberately does not, for the first iteration. It consumes the ViewModel through the public snapshot hook, so it doubles as the proof that the contract is complete. + +### 6.5 Rejected alternative: live ViewModel + +A ViewModel whose properties are getters reading observables, with the Core supplying an `observer` wrapper (`core.observe(MyView)`), gives fine-grained tracking without exposing MobX. It is rejected as the default because of the classic footgun: passing the live object to a memoised child that reads a property the parent did not read means the child never updates. Plain snapshots cannot surprise anyone that way. It may return later as an opt-in for power users. + +## 7. The ViewModel envelope and the input facet + +Every widget's ViewModel starts with the same envelope: the fields every pluggable widget fills, and nothing else. A survey of the 55 widgets in this repository on 2026-11-05 found exactly five, and the Studio Pro core widgets (text box, date picker, action button, tab container, layout grid) fill the same five. + +```ts +interface WidgetViewModel { + id: string; // a View puts it on the element the Studio Pro label targets + status: "loading" | "ready" | "unavailable"; + hints: Hints; // advisory, Atlas-presentation properties from Studio Pro + t: Translate; // all user-facing and ARIA texts, see 7.1 + tabIndex?: number; // Studio Pro "Tab index" system property, generated for every widget +} +``` + +Input widgets add one facet. It is Studio Pro's own definition of an input widget, the bundle every core input and every pluggable input with the Editability property carries: read-only state, validation feedback, the required flag and label for accessibility, and the on-enter and on-leave events. + +```ts +interface InputViewModel { + readOnly: boolean; + validation?: string; + a11y: { label?: string; required: boolean; describedBy?: string }; + enter(): void; // the widget received focus from outside: runs the "On enter" action + leave(): void; // focus left the widget: runs the "On leave" action +} +``` + +A combobox is `WidgetViewModel & InputViewModel` plus its own body; a data grid, a gallery, a chart or a file uploader is `WidgetViewModel` plus its own body. An input whose XML has no enter or leave action exposes the two commands as commands that run nothing, so that every input View wires focus the same way. There are no further facets. A named facet is justified only when shared code consumes the bundle: the input facet has the focus helper (section 8.5) and the input conformance cases (section 11); a field that one widget fills, such as the combobox's composed live-region message, stays in that widget's ViewModel until a second widget fills it the same way and shared code wants it. Until 2026-11-05 the envelope carried the input fields; the survey showed that only 10 of 55 widgets fill them. + +Widget-specific state and commands extend the two. Commands are referentially stable. Nothing in a ViewModel type may mention a behaviour library, a positioning library, Atlas, or class names. + +The Studio Pro Class and Style system properties are in neither, because whether a widget receives them depends on the widget. A widget with the Label system property is rendered by the client inside a form-group wrapper that carries the label, the Class and the Style; the widget never sees them, and its one duty is to put `id` on the element the label targets, so that `