Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Experiment with typing CustomEvent #5

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 6 additions & 0 deletions jsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"compilerOptions": {
"target": "es2020",
"types": ["vitest/importMeta"]
}
}
25 changes: 25 additions & 0 deletions src/example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// @ts-check
/// <reference path="./index.d.ts" />
import { circle } from "leaflet";
const addTo = "addTo";

class Bar extends HTMLElement {
constructor() {
super();
this.addEventListener(addTo, (ev) => {
ev.detail.layer;
});
}

documentConnected() {
/** @type AddToEvent */
const event = new CustomEvent(addTo, {
detail: {
layer: circle([1, 1], { radius: 5 }),
},
});
this.dispatchEvent(event);
}
}

export default Bar;
18 changes: 18 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import type { Icon, Layer } from "leaflet";
import type { LIcon } from "l-icon";

declare global {
type AddToEvent = CustomEvent<{ layer: Layer }>;
type BindPopupEvent = CustomEvent<{ content: string }>;
type SetIconEvent = CustomEvent<{ icon: Icon }>;

interface HTMLElementEventMap {
addTo: AddToEvent;
bindPopup: BindPopupEvent;
setIcon: SetIconEvent;
}

interface HTMLElementTagNameMap {
"l-icon": LIcon;
}
}
16 changes: 11 additions & 5 deletions src/l-icon.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// @ts-check
/// <reference path="./index.d.ts" />
// @vitest-environment happy-dom
import * as L from "leaflet";
import { kebabToCamel } from "./util.js";

import { kebabToCamel } from "./util.js";

class LIcon extends HTMLElement {
constructor() {
Expand All @@ -10,6 +11,7 @@ class LIcon extends HTMLElement {
}

connectedCallback() {
/** @type {import("leaflet").IconOptions} */
const options = {};

// Strings
Expand Down Expand Up @@ -42,11 +44,13 @@ class LIcon extends HTMLElement {
});

if (this.hasAttribute("cross-origin")) {
options.crossOrigin = this.getAttribute("cross-origin") === "true";
let k = "crossOrigin";
options[k] = this.getAttribute("cross-origin") === "true";
}
this.icon = L.icon(options);

const event = new CustomEvent("icon:add", {
/** @type SetIconEvent */
const event = new CustomEvent("setIcon", {
cancelable: true,
bubbles: true,
detail: {
Expand Down Expand Up @@ -75,8 +79,10 @@ if (import.meta.vitest) {

it("emits icon:add event", async () => {
const el = document.createElement("l-icon");
/** @type {keyof HTMLElementEventMap} */
const eventName = "setIcon";
let promise = new Promise((resolve) => {
el.addEventListener("icon:add", (ev) => {
el.addEventListener(eventName, (ev) => {
resolve(ev.detail.icon);
});
});
Expand Down
15 changes: 11 additions & 4 deletions src/l-marker.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// @ts-check
/// <reference path="./index.d.ts" />
// @vitest-environment happy-dom
import * as L from "leaflet";
import { mapAddTo, popupAdd } from "./events.js";
import { mapAddTo } from "./events.js";
import LLayer from "./l-layer.js";

class LMarker extends LLayer {
Expand All @@ -9,7 +11,7 @@ class LMarker extends LLayer {
constructor() {
super();
this.layer = null;
this.addEventListener("icon:add", (ev) => {
this.addEventListener("setIcon", (ev) => {
ev.stopPropagation();
this.layer.setIcon(ev.detail.icon);
});
Expand All @@ -24,9 +26,9 @@ class LMarker extends LLayer {
this.layer.setIcon(icon);
}

this.setAttribute("leaflet-id", L.stamp(this.layer));
this.setAttribute("leaflet-id", L.stamp(this.layer).toString());

this.addEventListener(popupAdd, (ev) => {
this.addEventListener("bindPopup", (ev) => {
const { content } = ev.detail;
this.layer.bindPopup(content);
});
Expand All @@ -41,6 +43,11 @@ class LMarker extends LLayer {
this.dispatchEvent(event);
}

/**
* @param {string} name
* @param {string} _oldValue
* @param {string} newValue
*/
attributeChangedCallback(name, _oldValue, newValue) {
if (this.layer !== null) {
if (name === "lat-lng") {
Expand Down
5 changes: 3 additions & 2 deletions src/l-popup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// @ts-check
import { popupAdd } from "./events.js";
/// <reference path="./index.d.ts" />

class LPopup extends HTMLElement {
constructor() {
Expand All @@ -8,7 +8,8 @@ class LPopup extends HTMLElement {

connectedCallback() {
const content = this.getAttribute("content");
const event = new CustomEvent(popupAdd, {
/** @type {BindPopupEvent} */
const event = new CustomEvent("bindPopup", {
cancelable: true,
bubbles: true,
detail: {
Expand Down