Haunted
React's Hooks API implemented for web components
README
Haunted 🦇 🎃
📚 Read the Docs 📖
- ```html
- <html lang="en">
- <my-counter></my-counter>
- <script type="module">
- import { html } from 'https://unpkg.com/lit?module';
- import { component, useState } from 'https://unpkg.com/haunted/haunted.js';
- function Counter() {
- const [count, setCount] = useState(0);
- return html`
- <div id="count">${count}</div>
- <button type="button" @click=${() => setCount(count + 1)}
- Increment
- </button>
- `;
- }
- customElements.define('my-counter', component(Counter));
- </script>
- </html>
- ```
More example integrations can be found in this gist.
Hooks
Haunted supports the same API as React Hooks. The hope is that by doing so you can reuse hooks available on npm simply by aliasing package names in your bundler's config.
Currently Haunted supports the following hooks:
- useMemo
- useRef
- useState
Function Signatures
- ```ts
- // Or another renderer, see Guides
- type Renderer = (element: Element) => TemplateResult;
- interface Options {
- baseElement: HTMLElement;
- observedAttributes: string[];
- useShadowDOM: boolean
- }
- declare function component(
- renderer: Renderer,
- options: Options
- ): Element;
- declare function component<BaseElement = HTMLElement>(
- renderer: Renderer,
- baseElement: BaseElement,
- options: Options
- ): Element
- declare function virtual(renderer: Renderer): Directive
- ```
License
BSD-2-Clause