SolidJSS
SolidJSโ€ข3y agoโ€ข
1 reply
eponymous

Problem creating web component with solid-element

Hi, I'm trying to create an image carousel web component by wrapping an existing JS library (called Glide) within a Solid component. As a regular solid component it works fine, but when I try to convert it to a web component I get an error from the Glide saying indicating that it cannot find the root element on which to mount the image carousel. It seems the initialization which I place within the onMount hook cannot access the elements of the shadow DOM (???) The code is below. Any insights would be appreciated.

import {Component, createEffect, createSignal, JSX, onMount} from "solid-js"
import Glide from "@glidejs/glide"
import {customElement} from "solid-element";


const VehicleGallery: Component = () => {

    onMount(() => {
        const glide = new Glide('.glide', {

              // GLIDE OPTIONS
        })
        glide.mount() // THROWS ERROR: "Root element must be a existing Html node"
    })

    return (
        <>
            <link rel="stylesheet" href="node_modules/@glidejs/glide/dist/css/glide.core.min.css" />
            <link rel="stylesheet" href="node_modules/@glidejs/glide/dist/css/glide.theme.min.css" />

            <div id="vehicle-gallery">
                <div class="glide">
                    <div class="glide__track" data-glide-el="track">
                        <ul class="glide__slides">
                            <li class="glide__slide">
                                <img src="https://dummyimage.com/1200x800/000000/fff.png"/>
                            </li>
                            <li class="glide__slide">
                                <img src="https://dummyimage.com/1200x800/FF0000/fff.png"/>
                            </li>
                        </ul>
                    </div>
                </div>
            </div>
        </>
    )
}

customElement('vehicle-gallery', {id: undefined}, (props, { element }) => {
    return <VehicleGallery id={props.id} />
})
Was this page helpful?