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 }) } /**