Skip to content

SetupDragIn not working inside the web component #2275

Description

@arulselvan

GridStack.setupDragIn not working inside the web component

Tried Gridstack inside the Lit web component, and it's working fine, but the scenario like dragging in the element from the sidebar (https://gridstackjs.com/demo/static.html) does not work inside shadow root.

Environment

  • version of gridstack.js - ^7.2.3

  • which browser/OS - all

Steps to reproduce

GridStack.setupDragIn method need to be called inside the web component(ShadowRoot), The following link has a working demo to reproduce the same inside Lit.
https://stackblitz.com/edit/vitest-dev-vitest-bzx77k?file=src/designer.ts

Expected behavior

Currently, inside setupDragInMethod, we are calling Utils.getElements,

Utils.getElements(dragIn).forEach(el => {

Utils.getElements internally using document.querySelectorAll, I think to work inside the shadow root, we need to query inside the parent element or give an argument to the method where we should look in the shadow root scenario...

let list = document.querySelectorAll(els);

Activity

  1. adumesny commented on Apr 27, 2023

    @adumesny
    Member

    or we could make setupDragIn() take an array of elements | string selector (like other methods) and you can pass the items you need... that may be more flexible in the long run.

  2. added a commit that references this issue on Apr 29, 2023
    612f621
  3. adumesny commented on Apr 29, 2023

    @adumesny
    Member

    I didn't get to test it, but this should handle it in the next release.

  4. arulselvan commented on May 3, 2023

    @arulselvan
    Author

    I didn't get to test it, but this should handle it in the next release.

    thanks @adumesny! upgraded to V8.0.1, and tested, works like a charm :)

  5. adumesny commented on May 3, 2023

    @adumesny
    Member

    curious, are you passing a list of elements or the virtual DOM parent ?

  6. arulselvan commented on May 5, 2023

    @arulselvan
    Author

    tested both, and I'm passing a list of elements like the below snip...

    `const availableWidgets = this.querySelectorAll(".newWidget");

    const widgetElements: HTMLElement[] = Array.from(
      availableWidgets
    ) as HTMLElement[];
    
    GridStack.setupDragIn(widgetElements, {
      appendTo: "parent",
      helper: "clone",
    });`
    

    Just thought, for Passing the root element as a param I think we can define a better type instead of the Document type currently tested the below snip works fine, but found during debugging inside Util.getElement in some specific condition, we using root.getElementById that might not be available for HTMLElement...

    const root: Document = this.querySelector("#widgets-container") as unknown as Document; GridStack.setupDragIn(".newWidget", { appendTo: "parent", helper: "clone" }, root);

  7. adumesny commented on May 5, 2023

    @adumesny
    Member

    good point. Utils.getElements() is supposed to get all elements (by class selector) but is also called for single ID which only works on the document, so signature need to be HTMLElement | Document. I'll change that.

  8. added 2 commits that reference this issue on May 6, 2023
    15ebb52
    319b34a
  9. adumesny commented on May 6, 2023

    @adumesny
    Member

    fixed the types. don't forget to donate if you find this lib useful!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions