-
Notifications
You must be signed in to change notification settings - Fork 53
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
Allow creating a CustomElement without defining the tagName #55
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -69,6 +69,23 @@ FullName.propTypes = { | |||||
register(FullName, 'full-name'); | ||||||
``` | ||||||
|
||||||
### Creating a CustomElements without registering a tag name | ||||||
|
||||||
If authoring a custom-elements library, you might want to expose your CustomElements without registering their tag name, to let your users register their own tags. | ||||||
|
||||||
This can be achieved by calling the `toCustomElements` function on the default export: | ||||||
|
||||||
```js | ||||||
import register from 'preact-custom-element' | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
function MyComponent({ name = "World" }) { | ||||||
return <span>Hello {name}!</span> | ||||||
} | ||||||
|
||||||
export const MyElement = register.toCustomElement(MyComponent, ['name']) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
``` | ||||||
|
||||||
`toCustomElement` has the same signature as `register`, omitting the second parameter (tag name). | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
## Related | ||||||
|
||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,6 +1,6 @@ | ||||||
import { h, cloneElement, render, hydrate } from 'preact'; | ||||||
|
||||||
export default function register(Component, tagName, propNames, options) { | ||||||
function toCustomElement(Component, propNames, options) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
function PreactElement() { | ||||||
const inst = Reflect.construct(HTMLElement, [], PreactElement); | ||||||
inst._vdomComponent = Component; | ||||||
|
@@ -49,12 +49,20 @@ export default function register(Component, tagName, propNames, options) { | |||||
}); | ||||||
}); | ||||||
|
||||||
return PreactElement; | ||||||
} | ||||||
|
||||||
export default function register(Component, tagName, propNames, options) { | ||||||
const PreactElement = toCustomElement(Component, propNames, options); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
return customElements.define( | ||||||
tagName || Component.tagName || Component.displayName || Component.name, | ||||||
PreactElement | ||||||
); | ||||||
} | ||||||
|
||||||
register.toCustomElement = toCustomElement; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
function ContextProvider(props) { | ||||||
this.getChildContext = () => props.context; | ||||||
// eslint-disable-next-line no-unused-vars | ||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -245,4 +245,32 @@ describe('web components', () => { | |||||
}); | ||||||
assert.equal(getShadowHTML(), '<p>Active theme: sunny</p>'); | ||||||
}); | ||||||
|
||||||
function ManualRegistration() { | ||||||
return <div>I'm manually registered</div>; | ||||||
} | ||||||
|
||||||
it('allows manual deferred registration by just exposing the element', async () => { | ||||||
const CustomElement = registerElement.toCustomElement( | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
will need to add an import up top obv |
||||||
ManualRegistration, | ||||||
[], | ||||||
{ shadow: false } | ||||||
); | ||||||
|
||||||
const el = document.createElement('x-manually-registered'); | ||||||
|
||||||
root.appendChild(el); | ||||||
assert.equal( | ||||||
root.innerHTML, | ||||||
'<x-manually-registered></x-manually-registered>' | ||||||
); | ||||||
|
||||||
window.customElements.define('x-manually-registered', CustomElement); | ||||||
|
||||||
root.appendChild(el); | ||||||
assert.equal( | ||||||
root.innerHTML, | ||||||
"<x-manually-registered><div>I'm manually registered</div></x-manually-registered>" | ||||||
); | ||||||
}); | ||||||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.