From 975a1f168b736e7c0f819a74cd8ab100392ba133 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Cchrisshank=E2=80=9D?= Date: Mon, 9 Dec 2024 12:48:36 -0800 Subject: [PATCH] make hull slots reactive --- demo/visualizing-set-relations.html | 9 ++----- src/folk-hull.ts | 37 ++++++++++++++++++++++++++++- 2 files changed, 38 insertions(+), 8 deletions(-) diff --git a/demo/visualizing-set-relations.html b/demo/visualizing-set-relations.html index 199c269..5f32549 100644 --- a/demo/visualizing-set-relations.html +++ b/demo/visualizing-set-relations.html @@ -10,6 +10,7 @@ } body { + z-index: -1; min-height: 100%; position: relative; margin: 0; @@ -23,12 +24,6 @@ display: block; position: absolute; inset: 0 0 0 0; - pointer-events: none; - background-color: #b4d8f669; - - * { - pointer-events: all; - } } h1 { @@ -46,7 +41,7 @@ - + diff --git a/src/folk-hull.ts b/src/folk-hull.ts index 5f3b4a3..c0c3d88 100644 --- a/src/folk-hull.ts +++ b/src/folk-hull.ts @@ -1,12 +1,27 @@ import { FolkBaseSet } from './folk-base-set'; import { verticesToPolygon } from './common/utils'; import type { Point } from './common/types'; +import { css, html } from './common/tags'; + declare global { interface HTMLElementTagNameMap { 'folk-hull': FolkHull; } } +const styles = css` + :host { + z-index: -1; + } + + #hull { + background-color: var(--folk-hull-bg, #b4d8f644); + height: 100%; + width: 100%; + pointer-events: none; + } +`; + export class FolkHull extends FolkBaseSet { static tagName = 'folk-hull'; @@ -16,6 +31,26 @@ export class FolkHull extends FolkBaseSet { return this.#hull; } + #shadow = this.attachShadow({ mode: 'open' }); + + #slot = document.createElement('slot'); + #hullEl = document.createElement('div'); + + constructor() { + super(); + + this.#hullEl.id = 'hull'; + + this.#shadow.adoptedStyleSheets.push(styles); + + this.#shadow.append(this.#hullEl, this.#slot); + + this.#slot.addEventListener('slotchange', this.#onSlotchange); + } + + // we might not need to react to the first slot change + #onSlotchange = () => this.observeSources(); + update() { if (this.sourcesMap.size === 0) { this.style.clipPath = ''; @@ -24,7 +59,7 @@ export class FolkHull extends FolkBaseSet { const rects = Array.from(this.sourcesMap.values()); this.#hull = makeHull(rects); - this.style.clipPath = verticesToPolygon(this.#hull); + this.#hullEl.style.clipPath = verticesToPolygon(this.#hull); } }