|
| 1 | +import type { |
| 2 | + FunctionComponent, |
| 3 | + FunctionalComponent, |
| 4 | + ComponentClass, |
| 5 | +} from 'preact'; |
| 6 | + |
| 7 | +export type RegisterOptions = |
| 8 | + | { |
| 9 | + /** |
| 10 | + * Default: do not use shadow-dom for rendering |
| 11 | + */ |
| 12 | + shadow: false; |
| 13 | + } |
| 14 | + | { |
| 15 | + /** |
| 16 | + * Use shadow-dom for rendering |
| 17 | + */ |
| 18 | + shadow: true; |
| 19 | + /** |
| 20 | + * A string specifying the encapsulation mode for the shadow DOM tree. |
| 21 | + * Default mode is `"open"` |
| 22 | + * @see https://developer.mozilla.org/en-US/docs/Web/API/Element/attachShadow#mode |
| 23 | + */ |
| 24 | + mode?: 'open' | 'closed'; |
| 25 | + }; |
| 26 | + |
| 27 | +/** |
| 28 | + * Register a preact component as web-component. |
| 29 | + * @param componentDefinition The preact component to register |
| 30 | + * @param tagName The HTML element tag-name (must contain a hyphen and be lowercase) |
| 31 | + * @param observedAttributes HTML element attributes to observe |
| 32 | + * @param options Additional element options |
| 33 | + * @example |
| 34 | + * ```ts |
| 35 | + * // use custom web-component class |
| 36 | + * class PreactWebComponent extends Component { |
| 37 | + * static tagName = 'my-web-component'; |
| 38 | + * render() { |
| 39 | + * return <p>Hello world!</p> |
| 40 | + * } |
| 41 | + * } |
| 42 | + * |
| 43 | + * register(PreactComponent); |
| 44 | + * |
| 45 | + * // use a preact component |
| 46 | + * function PreactComponent({ prop }) { |
| 47 | + * return <p>Hello {prop}!</p> |
| 48 | + * } |
| 49 | + * |
| 50 | + * register(PreactComponent, 'my-component'); |
| 51 | + * register(PreactComponent, 'my-component', ['prop']); |
| 52 | + * register(PreactComponent, 'my-component', ['prop'], { |
| 53 | + * shadow: true, |
| 54 | + * mode: 'closed' |
| 55 | + * }); |
| 56 | + * ``` |
| 57 | + */ |
| 58 | +declare function register( |
| 59 | + componentDefinition: |
| 60 | + | FunctionComponent<any> |
| 61 | + | ComponentClass<any> |
| 62 | + | FunctionalComponent<any>, |
| 63 | + tagName?: string, |
| 64 | + observedAttributes?: string[], |
| 65 | + options?: RegisterOptions |
| 66 | +): void; |
| 67 | + |
| 68 | +export default register; |
0 commit comments