NUKnightLab / NUKnightLab/juxtapose

can't use in web component

Open
#188 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
882
Forks
127
Avg merge
1m
Merged PRs (30d)
2

Description

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

https://github.com/alifeee/blog/commit/393fd7837bec9b6a6a128fb3c6fb72ae7958049f

- 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);
+ }

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the JXSlider initialization and trace how its selector is resolved, focusing on the document.querySelector call mentioned in the issue. Reproduce the supplied web component example and verify that a slider can initialize inside its shadow root without breaking normal selector usage.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
frontend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.