leaflet-components/src/elements/leaflet-geojson.ts
Buddy d753fd82b5 refactor: split element classes from customElements.define
Every component is now two modules sharing a basename:

- src/elements/leaflet-foo.ts -- `export default class LeafletFooElement
  extends WithProps(...)`, the class only, no customElements.define, no
  side effects. src/elements/index.ts is an order-free barrel.
- src/components/leaflet-foo.ts -- three lines: import the class, define
  the tag, re-export. Importing this (or src/index.ts) registers the tag.

Plugin authors can now import a class without triggering the built-in
define, to subclass it or register it under a different tag name. Package
subpath exports `leaflet-components/elements`,
`leaflet-components/elements/leaflet-foo.js`, and
`leaflet-components/components/leaflet-foo.js` map onto dist/; jsr.json
gains an `./elements` entry.

The load-bearing ordering moves from the export statements in src/index.ts
to its `./components/*` side-effect imports (the elements barrel carries no
define, so its order is free). Classes are renamed LeafletFoo ->
LeafletFooElement, including in the HTMLElementTagNameMap augmentation.

Docs (CLAUDE.md, docs/, README.md) updated for the new layout.
2026-09-08 20:59:45 -07:00

33 lines
1.1 KiB
TypeScript

import { GeoJSON, type PathOptions } from 'leaflet';
import type { GeoJsonObject } from 'geojson';
import { json, positional } from '../core/props.ts';
import { pathProps } from '../core/shared-props.ts';
import {
WithProps,
type LeafletAddEventListener,
type LeafletRemoveEventListener,
} from '../core/with-props.ts';
import type { GroupEvents } from '../core/event-types.ts';
export default class LeafletGeoJSONElement extends WithProps({
data: positional(
json<GeoJsonObject | null>(null, {
set(obj: GeoJSON, value) {
obj.clearLayers();
if (value) obj.addData(value);
},
}),
),
...pathProps,
}) {
declare readonly leafletObject?: GeoJSON;
declare addEventListener: LeafletAddEventListener<GroupEvents>;
declare removeEventListener: LeafletRemoveEventListener<GroupEvents>;
// Every prop but `data` is a style option, and GeoJSON takes those nested
// under `style` so they apply to each feature it builds.
createLeafletObject(options: PathOptions): GeoJSON {
return new GeoJSON(this.data, { style: options });
}
}