zouhir / preact-habitat

Zero configuration Preact widgets renderer in any host DOM
MIT License
518 stars 43 forks source link

Ability for host page to update properties on the widget #21

Closed joelmalone closed 2 years ago

joelmalone commented 6 years ago

It'd be great if the host page could capture a reference to the Preact component, which it can then use to update the properties on the rendered component later.

An example use case might be:

gijo-varghese commented 5 years ago

any updates on this?

sirphoenix commented 5 years ago

I've made a pull request which enables passing the widget as reference to a local or global variable (#33). From there you could access any public methods, e.g. changing it's state.

Not sure about props though, that would mean completely re-rendering the widget from root.

zouhir commented 5 years ago

Would sharing a functional event emitter instance between page and widget be good?

import mitt from 'mitt'
window.emitter = mitt() //very bad lol

// widget can listen to an event from page
emitter.on('foo', e => console.log('foo', e) )

// page or widget can fire fire an event
emitter.emit('foo', { a: 'b' })

// working with handler references:
function onFoo() {}
emitter.on('foo', onFoo)   // listen
emitter.off('foo', onFoo)  // unlisten
sirphoenix commented 5 years ago

Why add the extra complexity of a global emitter when we're already talking about global/window namespace outside the widget?

Unless you really want habitat to accept messages from there and then change props on the widget?

@joelmalone do you have a code example of what you want to do exactly?