--- url: /guide/autoloading.md --- # Autoloading `@studiometa/ui` ships a declarative autoloader entry. You import one side-effect module. The [`@studiometa/js-toolkit`](https://js-toolkit-v4.studiometa.dev) registry then finds components from their `data-component` attribute and loads each component's JavaScript when it is needed. The autoloader works the same way with a bundler and from a CDN. The same markup runs in a bundled application, a content site, a prototype, or a CMS template. The `@studiometa/ui/autoload` entry ships JavaScript only — no Twig templates and no stylesheets. Read [Limitations](#limitations) before you use it in an application. For a no-build setup, load the entry from an ESM CDN such as [esm.sh](https://esm.sh). esm.sh serves any npm package as native ES modules and resolves the `@studiometa/js-toolkit` peer dependency. The examples below use esm.sh. ## Quick start Add a module script. Import the `@studiometa/ui/autoload` entry. The import registers the `@studiometa/ui` manifest and starts discovery. There is nothing more to call. ```html ``` The runtime scans the document for `data-component` tokens, imports the matching modules, and registers them with the [js-toolkit](https://js-toolkit-v4.studiometa.dev) runtime. The declarative contract (`data-component`, `data-ref`, `data-option-*`) is the same one that [Declarative runtime](/guide/concepts/declarative-runtime) describes. Pin an exact version in production. See [Version pinning](#version-pinning). ## Activate a package Import one side-effect entry for each component package you use. Each import registers that package's manifest with the shared runtime. ```html ``` The same entries resolve from `node_modules` when you install the packages and let your bundler resolve them: ```js import '@studiometa/ui/autoload'; import '@studiometa/ui-mapbox/autoload'; import '@studiometa/ui-motion/autoload'; import '@studiometa/ui-gsap/autoload'; ``` Import the entries at the top of one module. The manifests register before the runtime starts, so they join into one loader over the composed set. Import `@studiometa/ui-mapbox/autoload` only when you use the Mapbox family — see [Mapbox integration](#mapbox-integration) — `@studiometa/ui-motion/autoload` only when you use [Motion](#motion-integration), and `@studiometa/ui-gsap/autoload` only when you use [GSAP](#gsap-integration). Every component on the page must resolve to one `@studiometa/js-toolkit` runtime. `@studiometa/ui`, `@studiometa/ui-mapbox`, `@studiometa/ui-motion` and `@studiometa/ui-gsap` share the same `@studiometa/js-toolkit` peer, so one pin on that peer resolves the whole set. ### Version pinning A version reference follows the package's npm tags and semver ranges: ```html ``` Pin an exact version in production. An exact-version URL is immutable and stays cached the longest, and it makes every entry on the page resolve to the same version. A versionless URL and `@latest` follow the latest stable release. ## Mount strategies Each manifest entry carries a default mount strategy. Override it per element with `data-mount`. There are six: | Strategy | The component mounts when… | | --------------- | ------------------------------------------------------------------------------------ | | `eager` | the element exists. No other condition. See [What `eager` means](#what-eager-means). | | `visible` | the element crosses into the viewport, once. | | `in-view` | the element is in the viewport, and unmounts when it leaves. Reversible. | | `idle` | the browser is idle. | | `interaction` | the first `pointerenter`, `pointerdown` or `focusin` on the element. | | `media:` | the media query matches, and unmounts when it stops matching. Reversible. | ```html
Mounts on hover, touch, or focus
``` `visible` and `in-view` take an optional `IntersectionObserver` root margin — `data-mount="in-view:50%"`. `interaction` takes an optional `page` scope — `data-mount="interaction:page"` — which waits for the first deliberate interaction anywhere in the document rather than on the element. The element's `data-mount` always wins over the manifest default. An invalid value reports a `component.invalid-mount-strategy` diagnostic and the entry's default is used. ### What `eager` means `eager` reads like "load at page load". It is not that. The runtime never walks the manifest: it reads the `data-component` tokens declared on the elements of the page, and consults a manifest entry only for a token it finds there. Nothing loads for a token absent from the markup. `eager` means the entry has no condition left to wait for beyond that element existing — no viewport crossing, no media query, no interaction. The import then runs on a background scheduler task, off the frame that discovered the element. Every element declaring the same token shares one import. Use it for a component that only needs to exist, and always for an element that carries `hidden`. A hidden element is never rendered, so it never intersects the viewport and never receives a pointer or focus event: `visible`, `in-view` and `interaction` wait on it for a signal that cannot arrive. ### Package defaults The `@studiometa/ui` manifest ships every component as `eager`. `@studiometa/ui-motion` ships every component as `eager`, so each one mounts because its element is on the page: * `Motion` applies its `initial` styles before the element is first seen, so the final state does not flash. It also exists inside a closed ``, before the dialog opens. Use its `inView` option to play when it enters the viewport. * `MotionScrollTimeline` links every `Motion` child when it mounts, including a child far below the viewport. * `MotionSequence` builds its sequence from every `Motion` child, including a child far below the viewport. * `MotionView` mounts even when its element starts out not rendered, so it can enter, and it already listens when a containing `Dialog` opens or a descendant announces a DOM update. It loads the Motion library only on its first transition. `Motion`, `MotionScrollTimeline` and `MotionSequence` load the Motion library when they mount. Nothing changes for code that registers these classes directly with `registerComponents`: they declare no strategy, so they were already `eager`. `@studiometa/ui-gsap` ships every component as `eager` too: * `Gsap` and `GsapTimeline` apply their starting state when they build, so the final state does not flash when the element is first seen. Use the `scrollTrigger` option to play when the element enters the viewport. * `GsapStep` is mounted when its timeline builds, including a step far below the viewport. `Gsap` and `GsapTimeline` load GSAP when they mount, and `ScrollTrigger` only when their `scrollTrigger` option is set. `@studiometa/ui-mapbox` splits: * `MapboxMap` and `StoreLocator` are `visible`, so the heavy `mapbox-gl` import waits for a map to approach the viewport. * `MapboxGeocoder` is `visible`, so `@mapbox/mapbox-gl-geocoder` waits for the geocoder to approach the viewport. Do not put `hidden` on its element: it holds the search input, or nothing when the control is added to the map, and a hidden element never loads. To load it without waiting for the viewport, set `data-mount="eager"` on it. * The other eleven map children are `eager`: clusters, cluster items, the navigation, geolocate and fullscreen controls, images, layers, markers, popups and sources. They only need to exist, because `MapboxMap` already gates `mapbox-gl` and their own modules are small. Many of them render nothing and carry `hidden`, which `visible` could never load. The classes of `MapboxMap`, `StoreLocator` and `MapboxGeocoder` declare `visible` too, so the strategy holds for every one of them on a page, including the ones inserted later. It also applies when you register these classes directly with `registerComponents`: they then mount when they near the viewport, not at once. ## Component discovery The runtime scans the document at startup and then observes it with a `MutationObserver`. It discovers: * **Initial markup** — every `[data-component]` present when the script runs. * **Added subtrees** — elements inserted after startup. * **Multiple tokens** — space-separated values such as `data-component="Action Timer"`. The runtime imports and registers each token once for the whole document. When a component registers, it also registers the child components it exposes. For example, `Carousel` brings in `CarouselWrapper`, `CarouselItem`, `CarouselBtn`, `CarouselDrag` and `CarouselPlay`, so nested families work without a list of every child. The runtime cleans up observers and listeners when an element leaves the DOM. Discovery follows node insertion and removal, so the runtime does not observe a change to the `data-component` attribute of an element already in the DOM. Add or replace the element instead. When `MutationObserver` is not available, the initial scan still runs, but the runtime does not discover added components. ## Programmatic API The manifest runtime lives in `@studiometa/js-toolkit`. The `@studiometa/ui/autoload` entry is only the side-effect wrapper that hands the `@studiometa/ui` manifest to `registerManifest()`; the programmatic API — `registerManifest`, `defineManifest`, and the glob adapters — is imported directly from `@studiometa/js-toolkit`. Use it to compose a subset of packages or to build your own entry. ```js import { registerManifest } from '@studiometa/js-toolkit'; import { manifest as uiManifest } from '@studiometa/ui/manifest'; registerManifest(uiManifest); ``` A manifest maps `data-component` tokens to a lazy importer and an optional mount strategy: ```js import { registerManifest } from '@studiometa/js-toolkit'; registerManifest({ Gallery: { mountStrategy: 'visible', load: () => import('./components/Gallery.js').then(({ Gallery }) => Gallery), }, }); ``` To autoload your own components from their files, build the manifest with `defineManifest` and one of the glob adapters — `fromMetaGlob` for Vite, `fromWebpackContext` for webpack — then register it the same way. See the [js-toolkit documentation](https://js-toolkit-v4.studiometa.dev) for every export. ## Manual imports You can also import components directly from the CDN, without the autoloader. This suits a script that builds components itself. ```js import { Action, Dialog } from 'https://esm.sh/@studiometa/ui@1.10.0'; // the whole surface import { Action } from 'https://esm.sh/@studiometa/ui@1.10.0/Action'; // one component ``` Manual imports and the autoloader do not conflict, while every import on the page resolves to one js-toolkit runtime. Pin the same version everywhere. ## Mapbox integration Mapbox components load like any other component, but the autoloader does not bundle `mapbox-gl`. You provide it. This keeps you in control of the Mapbox version and its Web Worker. Declare an [import map](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script/type/importmap) before the autoload script. Point the bare specifiers at a source of your choice. Add the geocoder entry only when you use `MapboxGeocoder`. ```html ``` Load the Mapbox stylesheet yourself, and the geocoder stylesheet only when you use `MapboxGeocoder`. Give each map a valid access token through its `data-option-access-token`, and any other Mapbox `Map` option through `data-option-map-options`. `MapboxMap`, `StoreLocator` and `MapboxGeocoder` default to the `visible` mount strategy, so the map code and the geocoder code load when they cross into the viewport. The other map children default to `eager`, so they load as soon as they are on the page, `hidden` or not — see [Package defaults](#package-defaults). You own the `mapbox-gl` module, so its Web Worker is same-origin and a strict Content Security Policy works. When you load `mapbox-gl` from a CDN that builds its worker from a `blob:` URL, allow it: `Content-Security-Policy: worker-src blob:;`. ## Motion integration The `Motion` component loads like any other component, but the autoloader does not bundle the [`motion`](https://motion.dev) library. You provide it through an import map, which keeps you in control of the Motion version: ```html ``` The component resolves `motion` lazily the first time an animation is built. In a bundled build you can inject a specific entry — such as the smaller `motion/mini` — with `provideMotion()`; see the [Motion JS API](/reference/items/Motion/js-api#providing-the-motion-dependency). ## GSAP integration The `Gsap` and `GsapTimeline` components load like any other component, but the autoloader does not bundle [GSAP](https://gsap.com). You provide it through an import map, which keeps you in control of the GSAP version: ```html ``` The components import `gsap` lazily when they build, and `gsap/ScrollTrigger` only for a `scrollTrigger` option. In a bundled build you can inject the modules with `provideGsap()`; see the [Gsap JS API](/reference/items/Gsap/js-api#providing-gsap). ## Shopify integration Most Shopify components autoload as they do in a bundled build. `FetchShopifyPartial` is the exception. The `@shopify/partial-rendering` adapter is not resolved in a no-build setup, so the component logs a diagnostic and falls back to the base [`Fetch`](/reference/items/Fetch/) behavior. Use a bundled build when you need partial rendering. ## Diagnostics Recoverable conditions — a duplicate manifest token, an invalid `data-mount` value, a registry conflict, a component that fails to load or mount — are reported on js-toolkit's diagnostic channel rather than written straight to the console. Each one is a bubbling `js-toolkit:diagnostic` `CustomEvent` whose `detail` carries a stable `code`, a `severity` of `warning` or `error`, a `message`, the `component` name, and, for an error, the original `error`: ```js import { DIAGNOSTICS, EVENTS } from '@studiometa/js-toolkit'; document.addEventListener(EVENTS.diagnostic, (event) => { const { code, severity, message, component, error } = event.detail; if (code === DIAGNOSTICS.component.loadFailed) { console.error(`Component ${component} failed to load: ${message}`, error); } }); ``` `DIAGNOSTICS` enumerates every code core reports. A component outside core mints its own in the same `namespace.detail` shape, which is what makes a listener able to filter. esm.sh serves a source map with every asset, so developer tools show the original sources. The autoloader targets ES2020 module browsers (Chrome 63+, Firefox 67+, Safari 11.1+, Edge 79+). ## Limitations A no-build install trades flexibility for a zero-build setup. Its constraints are deliberate: * **One runtime version per page.** Several imports of the entries are safe; they share one runtime. Two different `@studiometa/js-toolkit` autoload runtimes cannot coexist — the second activation warns and does nothing. Pin one version. * **One js-toolkit runtime.** Every component on the page must resolve to one `@studiometa/js-toolkit` runtime. Do not mix CDN components with a separate npm build that bundles its own copy. * **No `data-component` mutation.** The runtime observes inserted and removed nodes only. * **No Shadow DOM.** Components own standard light-DOM elements. * **No templates or stylesheets.** No Twig and no CSS — including no Mapbox CSS. * **ES2020 module browsers only.** * **Mapbox is not provided.** You supply `mapbox-gl` and its CSS through an import map. * **Motion is not provided.** You supply `motion` through an import map. * **GSAP is not provided.** You supply `gsap` and `gsap/ScrollTrigger` through an import map. * **Shopify partial rendering is excluded.** `FetchShopifyPartial` falls back to base `Fetch`. ## Next steps * [js-toolkit documentation](https://js-toolkit-v4.studiometa.dev) — the registry, manifests and mount strategies. * [Declarative runtime](/guide/concepts/declarative-runtime) — the `data-component` / `data-option-*` contract.