Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exposing public properties? #1

Open
bestguy opened this issue Sep 4, 2016 · 7 comments
Open

Exposing public properties? #1

bestguy opened this issue Sep 4, 2016 · 7 comments
Assignees

Comments

@bestguy
Copy link

bestguy commented Sep 4, 2016

Hi, great library. I have a question on how to expose properties of a component to the DOM. It looks like you are iterating through the attributes to expose as the CustomElement prototype:

https://github.com/bspaulding/preact-custom-element/blob/master/src/index.js#L6

but am a little confused on what these would actually be. I mistakenly assumed I'd be able to define properties on the Preact component and automatically export them:

class Example extends Component {
  report() {
    console.log('SOUND OFF!')
  }
  render() {
    ...
}

However this does not work:

var example = $('x-example')[0];
example.report();

What would be the best way to do this?

@developit
Copy link
Member

Hi @bestguy - I believe you might be able to do that using the ._component property:

var example = $('x-example')[0]._component;
example.report();

@bestguy
Copy link
Author

bestguy commented Oct 10, 2016

Thanks @developit - I'm a little weak on web components, but was looking at Polymer docs (https://www.polymer-project.org/1.0/docs/devguide/instance-methods) and it allows this syntax:

Polymer({
    ...
    speak: function() {
      console.log(this._says);
    }
});

And call like so:

var cat1 = document.querySelector('cat-element');
cat1.speak();

without the ._component

Any tips on how I might modify this to support?

@developit
Copy link
Member

I think we'd just iterate over the methods on the component's prototype and hoist them (skipping the builtins like render() and constructor(). Actually shouldn't be too hard at all.

@bspaulding bspaulding self-assigned this Mar 16, 2018
@bspaulding
Copy link
Contributor

I've opened a PR to possibly address this. Initially I was using Object.getOwnPropertyNames to introspect as @developit mentioned, but I backed that out to an explicit list the user would pass in.

Just trying to allow the user retain some control over privacy, even though this is JS. :)

Thoughts / comments welcome there, I could go either way. Would be easy enough to introspect on your own and pass that into the explicit api, so I'm leaning that way. /shrug

@rburgst
Copy link

rburgst commented Aug 3, 2021

I have created a sample repo with react hooks where accessing the exposed methods does not work unfortunately. I would love to get a proposal on how to fix this.

https://github.com/rburgst/preact-web-component-method

@rburgst
Copy link

rburgst commented Jan 25, 2022

@developit could you share some insight here?

@floriankuc
Copy link

I came across a similar problem as @rburgst. The preact-custom-element documentation refers to its register function but there is still no way to call a method on the web component from outside. Has there ever been a solution to this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants