How silversonicaxel/maplibre-layer-factory works
1. What this repository is
- maplibre-layer-factory is a TypeScript UI plugin for MapLibre GL JS. It is not a standalone app or backend service; it is a control library that gets mounted into an existing MapLibre map via map.addControl(...).
2. Top-level shape of the repo
- The repository is very small and library-focused: one source entrypoint in src/index.ts, one stylesheet in src/index.css, and a Vite setup (vite.config.ts) for building/distributing the package.
- index.html is likely a local demo/dev entry used by Vite, not a production surface.
3. Primary runtime boundary
- The library lives entirely on the browser side and depends on the MapLibre GL JS runtime.
- Its main integration boundary is the MapLibre control API: it receives a map instance from the host app and renders a custom control into the map UI.
4. Core responsibility
- The library’s job is to inspect layer metadata and render a selector UI that toggles layer visibility.
- It abstracts the complexity of layer switching into a reusable “factory” control, rather than forcing the app to build a custom legend or layer switcher.
5. Main UI subsystems
- The README describes a control structure with:
- a container
- a toggle
- a panel
- panel sections for layers, label, and overlays
- This suggests the entrypoint builds a composite control with both a collapsed/expanded state and separate rendering paths for different layer types.
6. Layer classification flow
- Each layer is driven by a metadata object with fields like name, caption, placeholder, ignore, overlay, and group.
- The control likely partitions incoming layers into:
- base layers: single-select, rendered as squared buttons
- overlay layers: multi-select, rendered as checkboxes
- ignored layers: excluded from the UI entirely
- Overlays can also be grouped under collapsible accordion sections.
7. Configuration and styling flow
- The factory accepts a single options object with:
- orientation for layout
- panel and button styles
- label styles
- overlay styles
- a withLabel flag
- Styling is intentionally split into granular style slots, which implies the control assembles DOM elements and applies inline style objects or style mappings per region/state.
8. State and interaction model
- The control likely maintains local UI state for:
- panel open/closed state
- currently selected base layer
- overlay checkbox states
- accordion open/closed state for overlay groups
- Interaction is centered on showing/hiding MapLibre layers, so the control acts as the state bridge between the DOM and the map style layer visibility.
9. Asset and presentation layer
- src/index.css is a dedicated styling layer, meaning the package ships a base visual skin in addition to runtime-generated styles.
- Consumers are expected to import that CSS alongside the library, so the architecture separates default presentation from runtime customization.
10. Build/distribution architecture
- The presence of package.json, package-lock.json, tsconfig.json, and vite.config.ts indicates a bundled library distribution rather than raw source publishing.
- Vite is the build tool, likely producing a browser-ready package and associated stylesheet for npm consumption.
11. Consumer integration boundary
- The host application is responsible for:
- creating the MapLibre map
- supplying layer metadata/configuration
- importing the library and its CSS
- mounting the control into a chosen map corner
- The plugin does not appear to own map creation, layer loading, or data fetching.
12. Notable architectural characteristic
- This is a single-feature library with a shallow file tree, so most architecture is concentrated in one entrypoint rather than spread across services or modules.
- For a diagram, the best representation is: host app → MapLibre map instance → custom layer factory control → DOM UI + layer visibility updates.
Main entry points: index.ts Classifier · index.css CSS skin · vite.config.ts Vite build · index.html Demo page
Components 19
Runtime/UI
Browser-side control mounted into an existing MapLibre map.
- Host app · Creates the map and mounts the control.
- MapLibre map · The map instance that accepts custom controls.
- Layer factory · Main reusable control exported by the library.
src/index.ts - Control shell · Container that assembles the control regions.
src/index.ts - Toggle · Collapsed/expanded control trigger.
src/index.ts - Panel · Expanded layer selector panel.
src/index.ts - Layer metadata · Per-layer fields such as name, caption, ignore, overlay, and group.
- Classifier · Splits incoming layers into base, overlay, and ignored sets.
src/index.ts - Base layers · Mutually exclusive layers shown as primary choices.
src/index.ts - Overlays · Independently toggled layers shown as checkboxes.
src/index.ts - Groups · Optional collapsible grouping for overlay layers.
src/index.ts - Labels · Optional label area controlled by the withLabel option.
src/index.ts - UI state · Tracks open/closed panel state and current layer selections.
src/index.ts - Layer visibility · Applies visibility changes back to MapLibre style layers.
src/index.ts - CSS skin · Base presentation layer shipped with the package.
src/index.css - Entry module · Public TypeScript entrypoint that wires the control together.
src/index.ts
Build/Distribution
Library bundling, packaging, and local demo entrypoints.
- Vite build · Produces the distributable browser library.
vite.config.ts - Demo page · Local Vite HTML entry for development or preview.
index.html - Package · Declares library metadata and scripts.
package.json
Connections 24
- Host app → MapLibre map: creates · The application owns the map instance.
- Host app → Entry module: imports · The app consumes the library entrypoint.
- Entry module → Layer factory: exports · Public API centers on the custom control.
- Layer factory → Control shell: builds · The control composes the UI shell.
- Control shell → Toggle: contains · The shell renders the collapse/expand trigger.
- Control shell → Panel: contains · The shell renders the selector panel.
- Layer factory → MapLibre map: mounts on · The control is attached through MapLibre's control API.
- Layer metadata → Classifier: drives · Metadata determines how layers are routed.
- Classifier → Base layers: routes to · Non-overlay layers become base choices.
- Classifier → Overlays: routes to · Overlay layers are collected separately.
- Classifier → Groups: organizes · Grouped overlays become accordion sections.
- Classifier → Labels: optionally adds · Label rendering depends on configuration.
- Toggle → UI state: toggles · Open/closed panel state is local UI state.
- Panel → UI state: reads · Panel contents reflect current selection state.
- Base layers → Layer visibility: updates · Selecting a base layer changes map visibility.
- Overlays → Layer visibility: updates · Overlay checkbox state is pushed to the map.
- MapLibre map → Layer visibility: applies · Visibility changes are applied on the map style.
- CSS skin → Control shell: styles · Default skin covers the container and layout.
- CSS skin → Toggle: styles · The base stylesheet styles the control trigger.
- CSS skin → Panel: styles · The base stylesheet styles the panel surfaces.
- Vite build → Entry module: bundles · Vite compiles the TypeScript entry.
- Vite build → CSS skin: bundles · The stylesheet is part of the distributable package.
- Demo page → Entry module: loads · The local HTML entry references the library during dev.
- Package → Vite build: orchestrates · Package scripts and metadata drive the build.