Skip to content

Instantly share code, notes, and snippets.

@matthewp
Last active October 17, 2021 15:52
Show Gist options
  • Select an option

  • Save matthewp/92c4daa6588eaef484c6f389d20d5700 to your computer and use it in GitHub Desktop.

Select an option

Save matthewp/92c4daa6588eaef484c6f389d20d5700 to your computer and use it in GitHub Desktop.

Revisions

  1. matthewp revised this gist Aug 19, 2019. 2 changed files with 2 additions and 0 deletions.
    1 change: 1 addition & 0 deletions lit-haunted-element.js
    Original file line number Diff line number Diff line change
    @@ -10,6 +10,7 @@ export default class LitHauntedElement extends LitElement {

    update(changedProperties) {
    this.hauntedState.run(() => super.update(changedProperties));
    this.hauntedState.runEffects();
    }
    }

    1 change: 1 addition & 0 deletions skate-haunted-element.js
    Original file line number Diff line number Diff line change
    @@ -16,5 +16,6 @@ export default class extends Base {

    renderer(...args) {
    this.hauntedState.run(() => super.renderer(...args));
    this.hauntedState.runEffects();
    }
    }
  2. matthewp created this gist Aug 19, 2019.
    22 changes: 22 additions & 0 deletions lit-haunted-element.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    import { LitElement } from 'lit-element';
    import { State } from 'haunted';

    export default class LitHauntedElement extends LitElement {
    constructor() {
    super();

    this.hauntedState = new State(() => this.requestUpdate(), this);
    }

    update(changedProperties) {
    this.hauntedState.run(() => super.update(changedProperties));
    }
    }

    export const litHaunted = (renderer) => {
    return class extends LitHauntedElement {
    render() {
    return renderer.call(this, this);
    }
    }
    };
    52 changes: 52 additions & 0 deletions readme.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,52 @@
    These are a couple of examples of base class integrations with Haunted. These could be starting points for developing more full fledged mixins.

    # LitElement

    The LitElement version can be used either as a base class, or as a mixin that takes a render function.

    ```js
    import { html } from 'lit-element';
    import LitHauntedElement, { litHaunted } from './lit-haunted-element.js';
    import { useState } from 'haunted';

    class MyHooksElement extends LitHauntedElement {
    render() {
    const [count, setCount] = useState(0);

    return html`Count: ${count}`;
    }
    }

    // OR can be used like so:
    const MyHooksElement2 = litHaunted(() => {
    const [count, setCount] = useState(0);

    return html`Count: ${count}`;
    });
    ```

    # SkateJS

    Another integration, this time with SkateJS and Preact.

    ```js
    import { useState } from 'haunted';
    import SkateHauntedElement, { html } from './skate-haunted-element.js';

    class MyElement extends SkateHauntedElement {
    render() {
    const [count, setCount] = useState(0);

    return html`
    <div>
    <p>A paragraph</p>
    <button type="button" onClick=${() => setCount(count + 1)}>Increment</button>
    <p><strong>Count:</strong> ${count}</p>
    </div>
    `;
    }
    }

    customElements.define('my-element', MyElement);
    ```
    20 changes: 20 additions & 0 deletions skate-haunted-element.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    import { withComponent } from 'skatejs';
    import withPreact from '@skatejs/renderer-preact';
    import htm from 'htm';
    import { h } from 'preact';
    import { State } from 'haunted';

    export const html = htm.bind(h);

    const Base = withComponent(withPreact());

    export default class extends Base {
    constructor() {
    super();
    this.hauntedState = new State(() => this.triggerUpdate(), this);
    }

    renderer(...args) {
    this.hauntedState.run(() => super.renderer(...args));
    }
    }