Re: [w3c/webcomponents] How to define APIs only for custom element authors (#758)

Hey you all, but this `attachInternals` and `disabledFeatures` business implies there is a set of features that is pre-defined by the browser vendors, and does not let library authors define them. Mixins are better in the sense that you can get them from anywhere (as builtins, or from libraries).

Can we try to keep the API space symmetric between builtin and userland?

---

On another note,

## protected-like methods from a mixin

Here's a simple idea on how to expose a feature from a mixin to a CE author, while letting the CE author decide to (or not to) expose the API to public space.

Suppose the mixin accepts a key. Then inside the module or closure where the CE class is defined, we can create the mixin:

```js
const key = Symbol() // key can be any reference, like WeakMap keys.
const WithSauce = makeWithSauce(key)
```

then extend from it (assume the mixin extends HTMLElement by default):

```js
const key = Symbol() // key can be any reference, like WeakMap keys.
const WithSauce = makeWithSauce(key)
export default class SaucyEl extends WithSauce() {}
```

Note we do not export the key.

Now to use a method `createSauce` provided by the mixin, we can pass the key in:

```js
const key = Symbol() // key can be any reference, like WeakMap keys.
const WithSauce = makeWithSauce(key)
export default class SaucyEl extends WithSauce() {
  connectedCallback() {
    this.createSauce(key) // it works!
  }
}
```

Now suppose a user of the element tries to call the method on an instance of the element. The user's code runs outside the module scope where `SaucyEl` was defined, so

```js
import SaucyEl from 'saucy-lib'
customElements.define('saucy-el', SaucyEl)
const saucyEl = document.querySelector('saucy-el')

saucyEl.createSauce() // throws an Error

const key = Symbol()
saucyEl.createSauce(key) // throws an Error
```


-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/w3c/webcomponents/issues/758#issuecomment-439775609

Received on Monday, 19 November 2018 05:27:09 UTC