From 6d4e39833655da20881dd73c609cfa69bece46b4 Mon Sep 17 00:00:00 2001 From: DarkWiiPlayer Date: Thu, 29 Feb 2024 15:29:36 +0100 Subject: [PATCH] Add Ref class to follow reactive elements --- skooma.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/skooma.js b/skooma.js index 702d983..5be4037 100644 --- a/skooma.js +++ b/skooma.js @@ -152,6 +152,25 @@ export const reactiveElement = observable => { return element } +/** A reference to a reactive element that follows it around through changes */ +export class Ref { + #current + /** @param {Element|Text} target A reactive element to follow */ + constructor(target) { + this.#current = target + this.follow(target) + } + + follow(target) { + target.addEventListener("replaced", ({next}) => { + this.#current = next + this.follow(next) + }) + } + + deref() { return this.#current } +} + /** Set an attribute on an element * @param {Element} element * @param {string} attribute