Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
61 commits
Select commit Hold shift + click to select a range
29bfc7f
maint(docs): Add ADR 0009 on migrating patterns to BasePattern.
thet Nov 29, 2026
8959352
tech(pat-clone): Migrate to a class based pattern.
thet Nov 29, 2026
4bf9ab0
tech(pat-modal): Migrate to a class based pattern.
thet Nov 29, 2026
d044560
tech(pat-close-panel): Migrate to a class based pattern.
thet Nov 29, 2026
a55da57
tech(pat-notification): Migrate to a class based pattern.
thet Nov 29, 2026
36496e2
tech(pat-calendar): Migrate to a class based pattern.
thet Nov 29, 2026
3589848
tech(pat-form-state): Migrate to a class based pattern.
thet Nov 29, 2026
b2be468
tech(pat-auto-submit): Migrate to a class based pattern.
thet Nov 29, 2026
13c1cee
tech(pat-display-time): Migrate to a class based pattern.
thet Nov 29, 2026
5e8a395
tech(pat-date-picker): Migrate to a class based pattern.
thet Nov 29, 2026
5f0d56d
tech(pat-datetime-picker): Migrate to a class based pattern.
thet Nov 29, 2026
975be93
tech(pat-auto-suggest): Migrate to a class based pattern.
thet Nov 29, 2026
12ff4e6
tech(pat-selectbox): Migrate to a class based pattern.
thet Nov 29, 2026
e0a7e49
tech(pat-colour-picker): Migrate to a class based pattern.
thet Nov 29, 2026
9aa3258
tech(pat-toggle): Migrate to a class based pattern.
thet Nov 29, 2026
99b2f3b
tech(pat-switch): Migrate to a class based pattern.
thet Nov 29, 2026
2e1ce08
tech(pat-tabs): Migrate to a class based pattern.
thet Nov 29, 2026
01520f2
tech(pat-checklist): Migrate to a class based pattern.
thet Nov 29, 2026
902d94a
tech(pat-push): Migrate to a class based pattern.
thet Nov 29, 2026
68e9bae
tech(pat-forward): Migrate to a class based pattern.
thet Nov 29, 2026
6f2d017
tech(pat-menu): Migrate to a class based pattern.
thet Nov 29, 2026
c71d8dc
tech(pat-masonry): Migrate to a class based pattern.
thet Nov 29, 2026
16b1864
tech(pat-gallery): Migrate to a class based pattern.
thet Nov 29, 2026
a761549
tech(pat-carousel): Migrate to a class based pattern.
thet Nov 29, 2026
99c1616
tech(pat-image-crop): Migrate to a class based pattern.
thet Nov 29, 2026
dd88591
tech(pat-slides): Migrate to a class based pattern.
thet Nov 29, 2026
2789538
tech(pat-zoom): Migrate to a class based pattern.
thet Nov 29, 2026
b5dd85e
tech(pat-fullscreen): Migrate to a class based pattern.
thet Nov 29, 2026
65db82e
tech(pat-scroll-box): Migrate to a class based pattern.
thet Nov 29, 2026
e7d95e6
tech(pat-auto-scale): Migrate to a class based pattern.
thet Nov 29, 2026
8e14ecd
tech(pat-subform): Migrate to a class based pattern.
thet Nov 29, 2026
9e73a09
tech(pat-sortable): Migrate to a class based pattern.
thet Nov 29, 2026
b1e1ad1
tech(pat-breadcrumbs): Migrate to a class based pattern.
thet Nov 29, 2026
6109891
tech(pat-expandable-tree): Migrate to a class based pattern.
thet Nov 29, 2026
ee16082
tech(pat-minimalpattern): Migrate to a class based pattern.
thet Nov 29, 2026
dc66ebf
maint(docs): Add ADR 0010 on lazily loaded pattern implementations.
thet Nov 29, 2026
b873bcd
feat(core events): Allow await_pattern_init with a Promise of a patte…
thet Nov 29, 2026
4f0706f
feat(core lazy-pattern): Add lazily loaded patterns.
thet Nov 29, 2026
fb32514
maint(pat-markdown, pat-tooltip): Support lazily loaded patterns from…
thet Nov 29, 2026
694a4fb
tech(pat-syntax-highlight): Load the pattern implementation lazily.
thet Nov 29, 2026
811ea3e
tech(pat-display-time): Load the pattern implementation lazily.
thet Nov 29, 2026
5d4569d
tech(pat-date-picker): Load the pattern implementation lazily.
thet Nov 29, 2026
7f29b3c
tech(pat-datetime-picker): Load the pattern implementation lazily.
thet Nov 29, 2026
8092bbb
tech(pat-scroll-marker): Load the pattern implementation lazily.
thet Nov 29, 2026
8ab08bb
tech(pat-navigation): Load the pattern implementation lazily.
thet Nov 29, 2026
ae246b3
tech(pat-modal): Load the pattern implementation lazily.
thet Nov 29, 2026
a29a84b
tech(pat-calendar): Load the pattern implementation lazily.
thet Nov 29, 2026
f35d58b
tech(pat-notification): Load the pattern implementation lazily.
thet Nov 29, 2026
c3ab0e9
tech(pat-auto-suggest): Load the pattern implementation lazily.
thet Nov 29, 2026
20cc75f
tech(pat-sortable): Load the pattern implementation lazily.
thet Nov 29, 2026
55cea82
tech(pat-bumper): Load the pattern implementation lazily.
thet Nov 29, 2026
3d933d6
tech(pat-image-crop): Load the pattern implementation lazily.
thet Nov 29, 2026
cfd524a
tech(pat-clone): Load the pattern implementation lazily.
thet Nov 29, 2026
73b4dc6
tech(pat-masonry): Load the pattern implementation lazily.
thet Nov 29, 2026
e77bd36
tech(pat-gallery): Load the pattern implementation lazily.
thet Nov 29, 2026
8b9c6b4
tech(pat-carousel): Load the pattern implementation lazily.
thet Nov 29, 2026
1271d0a
tech(pat-auto-submit): Load the pattern implementation lazily.
thet Nov 29, 2026
0654be5
fix(pat-form-state): Do not bundle the pat-modal implementation eagerly.
thet Nov 29, 2026
90dfcdb
tech(pat-markdown): Load the pattern implementation lazily.
thet Nov 29, 2026
99e7f20
fix(pat-toggle): Remove the non-functional Enter key handler.
thet Nov 29, 2026
7f71942
fix(pat-subform): Do not submit the surrounding form on Enter in auto…
thet Nov 29, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions docs/adr/0009-migrate-patterns-to-basepattern.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# 0009 Migrate patterns to BasePattern

Status: Accepted (2026-11-29)

## Context

Most patterns were still based on the legacy `Base.extend` or were plain
objects with an `init($el)` function. Only part of them used the class based
`BasePattern`, which provides per-element instances, options parsing via a
static parser, instance scoped event listeners and automatic destroy when the
element is removed from the document (ADR 0001, 0004).

Legacy patterns never cleaned up: listeners on `document`/`window`,
observers, timers and third party instances (Pikaday, Select2, Slick,
Masonry, ...) stayed alive after their element was removed.

A class based implementation is also a precondition for loading pattern
implementations lazily, because a placeholder can then instantiate the real
class after loading it.

## Decision

Migrate all patterns of Patternslib and of the add-ons pat-shopping-cart,
pat-upload, pat-doclock and pat-tinymce to `BasePattern`, one commit per
pattern, following the pat-equaliser migration (3b43307e0):

- Options come from the static parser.
- Global listeners are instance scoped, third party instances, observers and
timers are cleaned up in `destroy()`.
- `destroy()` does not undo DOM changes (deferred).
- jQuery listeners are kept where events are triggered via jQuery
(`patterns-injected`, `pat-update`, ...), because native listeners don't
receive them. They use a per-instance namespace and are removed on destroy.
- The HTML API stays the same. The JavaScript API changes: the default export
is the class, the jQuery plugins and static `init` methods are gone. In
Patternslib the commits are marked `tech`, because the pattern integration
contract for designers is unchanged; the JavaScript API change is described
in each commit message. In the add-ons they are marked `breaking`.

Not migrated:

- **pat-inject**: it is a shared service as well as a pattern (`execute`,
`submitSubform`, `registerTypeHandler`, `init` are called by other
patterns), has module level side effects which must run on page load
(history handling, the `patterns-injected` rescan listener) and relies on a
timing contract with pat-validation.
- **pat-ajax**: the transport service of pat-inject and pat-subform.
- **pat-focus, pat-legend**: transform-only patterns without per-element
instances.
- **pat-grid**: CSS only.

## Consequences

- Patterns are destroyed when their element is removed and stop reacting to
global events. Several leaks and long standing bugs found during the
migration are fixed, as separate `fix` commits where they were separable.
- All patterns initialize asynchronously, one tick later than legacy
patterns did. JavaScript code must wait with
`events.await_pattern_init(instance)`.
- JavaScript users of the legacy APIs need to migrate to `new Pattern(el)`
and instance methods. pat-modal: closing is now `close()`, `destroy()` only
cleans up.
- The add-ons depend on @patternslib/patternslib 9.11.1, whose `BasePattern`
has neither instance scoped listeners nor automatic destroy. They use
`core/events` directly and benefit from automatic destroy once they update.
108 changes: 108 additions & 0 deletions docs/adr/0010-lazily-loaded-pattern-implementations.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
# 0010 Lazily loaded pattern implementations

Status: Accepted (2026-11-29)

## Context

All patterns imported in `src/patterns.js` end up in one eagerly loaded
patterns chunk, which every page loads. Before this change it was about
201 KB minified, of which about 148 KB was pattern code and about 10 KB were
lookup maps of dynamic import contexts (ADR 0007). Heavy third party
libraries were already imported dynamically, but the pattern code itself
was not, even on pages which don't use the pattern.

Mockup already splits patterns into a thin registration module and a lazily
imported `--implementation` module. The convention started there with the
controlpanel patterns (Mockup commit fe0380c46, 2021-11-25) and is used for
several patterns since (e.g. Mockup's `pat/modal/modal.js`). Mockup's
patterns are `Base` patterns though, while the patterns split here are class
based (ADR 0009). See "Alternatives considered".

Other code instantiates patterns from the registry directly, e.g.
`new registry.patterns.markdown(el)` in pat-tooltip or
`await new registry.patterns[name](el)` in Mockup. Registry code of other
Module Federation bundles also instantiates registry entries with `new`.

## Decision

- `core/lazy-pattern.js` provides `create_lazy_pattern({name, trigger,
order, load})`. It returns a small placeholder class which is registered
instead of the real class. Instantiating it loads the implementation and
returns a Promise of the real pattern instance. A class constructor may
return an object, so the registry and other code using `new` keep working
unchanged. Once loaded, the registry entry is replaced by the real class.
- `events.await_pattern_init` also accepts such a Promise, so
`await events.await_pattern_init(new Pattern(el))` keeps working.
- A split pattern `pat/NAME/NAME.js` only contains the registration and
module level code which must run on page load. The class lives in
`pat/NAME/NAME--implementation.js`, which still registers itself, so
importing it directly gives an eagerly registered pattern. Code which needs
the class or the parser imports the `--implementation` module.
- Split are patterns where it makes a difference (roughly 2 KB minified or
more, including dynamic import lookup maps): calendar, navigation,
scroll-marker, auto-suggest, date-picker, datetime-picker, display-time,
sortable, modal, notification, bumper, image-crop, clone, masonry,
gallery, carousel, auto-submit, markdown and syntax-highlight.
- Kept eager, because lazy loading would break them or change what users
see:
- pat-inject and pat-ajax: shared services, used by many patterns and on
almost every page anyway (ADR 0009).
- pat-validation: must register its submit handlers before pat-inject.
- pat-tooltip: imported as class and parser by Mockup and pat-tiptap.
- pat-depends, pat-collapsible, pat-stacks, pat-toggle: they hide or show
content on initialization. Loading them later would flash content in the
wrong state.
- pat-bind: renders bound values on initialization.
- pat-tabs, pat-auto-scale, pat-equaliser: they adjust the layout on
initialization.
- Transform-only patterns (pat-legend, pat-focus) and small patterns, for
which the registration module would eat most of the gain.

## Alternatives considered

- **Grafting the implementation's prototype onto the registration pattern**
(Mockup's current approach, e.g. `pat/modal/modal.js`): the registration
module is a `Base` pattern whose `init()` imports the implementation and
copies its prototype onto the running instance with `$.extend`. This
relies on `Base` specifics and does not work for `BasePattern` classes:
class methods are not enumerable and are not copied, class fields are
created by the implementation's constructor which never runs, private
fields throw when accessed on an object not constructed by their class,
and `BasePattern` parses the options with the implementation's static
parser before `init()` runs.
- **Wrapping a separate implementation instance** (Mockup's pat-tinymce
since 2022): the registration pattern creates an implementation object in
its `init()` and keeps it as `this.instance`. With a `BasePattern` class
as implementation, there are two pattern instances per element. The
registration pattern has already stored itself as `el["pattern-NAME"]`, so
the real pattern refuses to initialize as already initialized. Destroying,
lifecycle tracking and init events would have to be forwarded between the
two instances.
- **A placeholder which constructs the real pattern** (chosen): the real
class is instantiated with its own constructor, so fields, private fields,
the static parser, the initialization order and the re-initialization
guard work unchanged, and the implementation modules are ordinary
`BasePattern` classes. The logic lives in one helper, so each registration
module is a few lines of configuration.

## Consequences

- The eagerly loaded patterns chunk went from 206.9 KB to 129.1 KB minified
(60.7 KB to 38.8 KB gzipped), measured with production builds before and
after splitting. The split pattern implementations and their dynamic
import lookup maps are no longer part of it. The entry points grow by about
0.5 KB for the additional chunk map entries.
- Instantiating a registration module's class with `new` returns a Promise
of the pattern instance, not the instance. Code which uses the result
directly has to `await new Pattern(el)`.
`events.await_pattern_init(new Pattern(el))` accepts the Promise. Code
which only registers patterns or lets the registry instantiate them is not
affected.
- A split pattern initializes after its implementation chunk is loaded,
i.e. one network round trip later on first use. Patterns which already
load a library dynamically now load two chunks in sequence.
- Breaking change for JavaScript users of split patterns: the default export
of `NAME.js` is the placeholder class and `parser` is no longer exported
there. Import `NAME--implementation.js` instead.
- Bundles importing `NAME.js` for registration (e.g. Mockup's
`patterns.js`) get the lazy loading without changes.
16 changes: 15 additions & 1 deletion src/core/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -281,12 +281,26 @@ const await_event = (el, event_name) => {
* Usage:
* await events.await_pattern_init(PATTERN);
*
* @param {Pattern instance} pattern - The pattern instance.
* @param {Pattern instance} pattern - The pattern instance, or a Promise of
* it as returned when instantiating a lazily loaded pattern.
*
* @returns {Promise} - Returns a Promise which can be used for ``await`` and which will be resolved when the event is throwm.
*
*/
const await_pattern_init = (pattern) => {
if (typeof pattern?.then === "function") {
// Lazily loaded patterns (see core/lazy-pattern.js) return a Promise
// of the pattern instance when instantiated. The instance is created
// synchronously when the Promise resolves while BasePattern
// initializes in a later tick, so the listeners below are still
// registered in time.
return pattern.then((instance) => {
if (!instance) {
throw new Error("The pattern could not be loaded.");
}
return await_pattern_init(instance);
});
}
// See: https://stackoverflow.com/a/44746691/1337474
return new Promise((resolve, reject) => {
// Case initialized
Expand Down
83 changes: 83 additions & 0 deletions src/core/lazy-pattern.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/**
* Lazily loaded patterns.
*
* Create a lightweight placeholder pattern class, which is registered in the
* patterns registry instead of the real pattern class. The real pattern
* implementation is only loaded when the pattern is used for the first time.
* This keeps the pattern implementation out of the eagerly loaded bundle.
*
* Usage - in ``example.js``:
*
* import registry from "../../core/registry";
* import create_lazy_pattern from "../../core/lazy-pattern";
*
* const Pattern = create_lazy_pattern({
* name: "example",
* trigger: ".pat-example",
* load: () => import("./example--implementation"),
* });
* registry.register(Pattern);
* export default Pattern;
*
* The implementation module ``example--implementation.js`` contains the
* BasePattern based class as its default export.
*
* Instantiating the placeholder class returns a Promise, which resolves to the
* instance of the real pattern class. So the registry and code doing
* ``await new registry.patterns.example(el)`` keep working.
*
* After the implementation was loaded, the registry entry is replaced with
* the real pattern class.
*/
import logging from "./logging";
import registry from "./registry";

const log = logging.getLogger("lazy-pattern");

export function create_lazy_pattern({ name, trigger, order, load }) {
let loading = null;

class LazyPattern {
static name = name;
static trigger = trigger;
static order = order;
static lazy = true;

static async load() {
if (!loading) {
loading = load()
.then((module) => {
const Pattern = module.default;
if (registry.patterns[name] === LazyPattern) {
// Swap the placeholder for the real pattern, so
// that further instantiations are done directly.
registry.patterns[name] = Pattern;
}
return Pattern;
})
.catch((error) => {
// Allow to retry loading.
loading = null;
throw error;
});
}
return await loading;
}

constructor(el, options, trigger) {
// Return a Promise instead of the instance. This is allowed for
// classes which do not extend another class.
return LazyPattern.load().then(
(Pattern) => new Pattern(el, options, trigger),
(error) => {
log.error(`Could not load the pattern ${name}.`, error);
return null;
},
);
}
}

return LazyPattern;
}

export default create_lazy_pattern;
Loading