Skip to content
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

can't use in web component #188

Open
alifeee opened this issue Nov 21, 2023 · 0 comments
Open

can't use in web component #188

alifeee opened this issue Nov 21, 2023 · 0 comments

Comments

@alifeee
Copy link

alifeee commented Nov 21, 2023

here is my web component

class ChairComparison extends HTMLElement {
  constructor() {
    super();
    this.root = this.attachShadow({ mode: "closed" });
  }
  connectedCallback() {
    this.render();
  }

  render() {
    const image1src = this.getAttribute("src1");
    const image2src = this.getAttribute("src2");

    const div = document.createElement("div");
    div.setAttribute("id", "slider");
    this.root.appendChild(div);

    let slider = new juxtapose.JXSlider(
      div,
      [
        {
          src: image1src,
        },
        {
          src: image2src,
        },
      ],
      {}
    );

    const link = document.createElement("link");
    link.setAttribute("rel", "stylesheet");
    link.setAttribute(
      "href",
      "https://cdn.knightlab.com/libs/juxtapose/latest/css/juxtapose.css"
    );
    this.root.appendChild(link);
  }
}

customElements.define("chair-comparison", ChairComparison);

Juxtapose uses document.querySelector which is not available on web components (as they are not found in the document, but their own shadow.

I fixed it with the following commit

alifeee/blog@393fd78

- this.wrapper = document.querySelector(this.selector);
+ // if selector is a DOM element, use it directly
+ if (this.selector instanceof Element) {
+   this.wrapper = this.selector;
+ } else {
+   this.wrapper = document.querySelector(this.selector);
+ }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant