matthewp / haunted

React's Hooks API implemented for web components 👻
BSD 2-Clause "Simplified" License
2.59k stars 92 forks source link

add option to disable shadowDom for createContext #75

Open geocine opened 5 years ago

geocine commented 5 years ago

I know it is not possible right now, so I just copied it like so

import { useContext, component } from "haunted";

const contextEvent = "haunted.context";

export const createContext = defaultValue => {
  const Context = {};

  Context.Provider = class extends HTMLElement {
    constructor() {
      super();
      this.listeners = [];

      this.eventHandler = event => {
        const { detail } = event;

        if (detail.Context === Context) {
          detail.value = this.value;

          detail.unsubscribe = () => {
            const index = this.listeners.indexOf(detail.callback);

            if (index > -1) {
              this.listeners.splice(index, 1);
            }
          };

          this.listeners.push(detail.callback);

          event.stopPropagation();
        }
      };

      this.addEventListener(contextEvent, this.eventHandler);
    }

    disconnectedCallback() {
      this.removeEventListener(contextEvent, this.eventHandler);
    }

    set value(value) {
      this._value = value;
      this.listeners.forEach(callback => callback(value));
    }

    get value() {
      return this._value;
    }
  };

  Context.Consumer = component(
    function({ render }) {
      const context = useContext(Context);

      return render(context);
    },
    HTMLElement,
    { useShadowDOM: false }
  );

  Context.defaultValue = defaultValue;

  return Context;
};
matthewp commented 5 years ago

createContext should be able to take a second argument that takes the component options just like component() does.

askbeka commented 5 years ago

In my opinion Consumer component always has to render into light DOM. Just like react behaves. I left it that way because there was no option to render to light DOM before.

Users will always expect a template that they see at author time to be in light dom since they might want to style it etc. And shadow dom has to be hidden in component implementation.

On Thu, 14 Mar 2019, 18:35 Matthew Phillips, notifications@github.com wrote:

createContext should be able to take a second argument that takes the component options just like component() does.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/matthewp/haunted/issues/75#issuecomment-472979973, or mute the thread https://github.com/notifications/unsubscribe-auth/AC3Imrq0yktSJ2ZJms2x_RnSEaASL53aks5vWohtgaJpZM4b0apY .

askbeka commented 5 years ago

So in other words, your implantation has to replace current one

On Thu, 14 Mar 2019, 19:34 Beknar Askarov, beknaraskarov@gmail.com wrote:

In my opinion Consumer component always has to render into light DOM. Just like react behaves. I left it that way because there was no option to render to light DOM before.

Users will always expect a template that they see at author time to be in light dom since they might want to style it etc. And shadow dom has to be hidden in component implementation.

On Thu, 14 Mar 2019, 18:35 Matthew Phillips, notifications@github.com wrote:

createContext should be able to take a second argument that takes the component options just like component() does.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/matthewp/haunted/issues/75#issuecomment-472979973, or mute the thread https://github.com/notifications/unsubscribe-auth/AC3Imrq0yktSJ2ZJms2x_RnSEaASL53aks5vWohtgaJpZM4b0apY .

askbeka commented 5 years ago

Consumer and Provider have to be virtual components, but since there is now teardown functioning properly and other bugs with virtual components they are components right now

On Thu, 14 Mar 2019, 19:35 Beknar Askarov, beknaraskarov@gmail.com wrote:

So in other words, your implantation has to replace current one

On Thu, 14 Mar 2019, 19:34 Beknar Askarov, beknaraskarov@gmail.com wrote:

In my opinion Consumer component always has to render into light DOM. Just like react behaves. I left it that way because there was no option to render to light DOM before.

Users will always expect a template that they see at author time to be in light dom since they might want to style it etc. And shadow dom has to be hidden in component implementation.

On Thu, 14 Mar 2019, 18:35 Matthew Phillips, notifications@github.com wrote:

createContext should be able to take a second argument that takes the component options just like component() does.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/matthewp/haunted/issues/75#issuecomment-472979973, or mute the thread https://github.com/notifications/unsubscribe-auth/AC3Imrq0yktSJ2ZJms2x_RnSEaASL53aks5vWohtgaJpZM4b0apY .