JSFiddle - React, Tailwind, and code Playground

by jeanwl

JavaScript

import { reactive, html } from 'https://cdn.skypack.dev/@arrow-js/core';

class Dialog {
    id = 'dialog'

    data = reactive({
        render: false
    })

    get el() {
        return window[this.id]
    }

    open() {
        this.data.render = true
    }

    close() {
        this.el.close()
    }

    onClose() {
        this.data.render = false
    }

    onMount() {
        console.log('onMount')
        this.el.showModal()
    }

    onBeforeUnmount() {
        console.log('onBeforeUnmount')
    }

    render() {
        if (!this.data.render) return this.onBeforeUnmount()

        setTimeout(() => this.onMount())

        return html`
        
        <dialog id="${this.id}"
            @click="${() => this.close()}"
            @close="${() => this.onClose()}">
            
            <p>Dialog Content</p>
        </dialog>
        
        `
    }
}

const dialog = new Dialog()

html`

<button @click="${() => dialog.open()}">Open Dialog</button>

${() => dialog.render()}

`(document.body)