Skip to content

Commit

Permalink
feat: add mounted
Browse files Browse the repository at this point in the history
  • Loading branch information
sebkolind committed Dec 7, 2023
1 parent 6fb708d commit 6fc93df
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions lib/one.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 })
}

/**
Expand Down

0 comments on commit 6fc93df

Please sign in to comment.