From 6fc93df4496ee6fcccf7881314637f0955ee91d1 Mon Sep 17 00:00:00 2001 From: "Sebastian L. K. Sorensen" Date: Thu, 7 Dec 2023 15:48:21 +0100 Subject: [PATCH] feat: add `mounted` --- lib/one.js | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/lib/one.js b/lib/one.js index ff202fd7..5167c20c 100644 --- a/lib/one.js +++ b/lib/one.js @@ -118,12 +118,18 @@ function watch(obj, key, fn) { } /** - * @param {{ el: HTMLElement, state: object, view: function }} context + * @param {object} context + * @param {HTMLElement} context.el The element to mount to + * @param {object} context.state The state object + * @param {function} context.view The view function + * @param {function} [context.mounted] The mounted function * @todo Add a way to hook into unmouting - * @todo Add a way to run operations after the view is mounted - * @todo Add a way to run operations before the view is unmounted + * @todo Add a way to run operations after the view is init + * @todo Add a way to run operations before the view is uninit */ -function mount({ el, state, view }) { +function mount(context) { + const { el, state, view, mounted } = context + const proxy = state ? new Proxy({ ...state }, { set(obj, prop, value) { if (obj[prop] == null) { @@ -150,11 +156,13 @@ function mount({ el, state, view }) { el.$one = { shadow: v } - if (el.children[0]) { + if (el.children?.[0]) { el.children[0].replaceWith(v) } else { el.append(v) } + + mounted?.({ el, state: proxy }) } /**