mdbassit / Coloris

A lightweight and elegant JavaScript color picker. Written in vanilla ES6, no dependencies. Accessible.
https://coloris.js.org/examples.html
MIT License
443 stars 58 forks source link

NaN values when inline color picker #119

Closed VelinGeorgiev closed 11 months ago

VelinGeorgiev commented 1 year ago

Here is my setup for inline use, however the selector in not working and alwayis returns #NaN. Selecting a color from pallette will result in #NaN....

Am I missing something in the seutp?

Coloris({
            parent: '#e-color-picker',
            alpha: true,
            theme: 'large',
            format: 'mixed',
            inline: true
        });

        document.querySelector('#clr-color-value').value = 'green';
        document.querySelector('#clr-color-value').dispatchEvent(new Event('change', { bubbles: true }));

        document.addEventListener('coloris:pick', event => {
            console.log('New color', event.detail.color);
       });

Screenshot 2023-09-10 at 07 24 58

mdbassit commented 12 months ago

I believe you are setting the value of #clr-color-value before the color picker is properly initialized. Try this :

Coloris({
    parent: '#e-color-picker',
    alpha: true,
    theme: 'large',
    format: 'mixed',
    inline: true
});

const setColorToGreen = () => {
    document.querySelector('#clr-color-value').value = 'green';
    document.querySelector('#clr-color-value').dispatchEvent(new Event('change', { bubbles: true }));
};

document.addEventListener('coloris:pick', event => {
    console.log('New color', event.detail.color);
});

// Wait for the document to load first
if (document.readyState !== 'loading') {
    setColorToGreen();
} else {
    document.addEventListener('DOMContentLoaded', function () {
        setColorToGreen();
    });
}
mdbassit commented 12 months ago

Also, if all you need is to set the default color of the inline picker, just use the defaultColor option:

Coloris({
    parent: '#e-color-picker',
    alpha: true,
    theme: 'large',
    format: 'mixed',
    inline: true,
    defaultColor: 'green'
});
VelinGeorgiev commented 12 months ago

@mdbassit thanks for your response. I don't believe the issue is with setting the defautl value. I get the same result even by just using the basic initializaiton and nothing elsle.

Coloris({
    parent: '#e-color-picker',
    alpha: true,
    theme: 'large',
    format: 'mixed',
    inline: true,
    defaultColor: 'green'
});

In my case when I try to select a color, the cursor position does not get updated. Awlays stays x=0, z=0, however 'coloris:pick' events still get fired.

mdbassit commented 12 months ago

That's quite strange! I'm unable to reproduce the issue, but it's likely that something is interfering with the calculation of the position of the mouse pointer. Can you maybe share the rest of your code or the link to a page where I can take a look at this in action?

VelinGeorgiev commented 12 months ago

Thanks @mdbassit ! I will try to debug the comming days and share a better example if I am unable to make it work.

VelinGeorgiev commented 11 months ago

Ok, I managed to make it work on a clear page.

Here are examples in case someone is interested.


<html>
    <body style="margin: 0;padding: 0;">

        <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/mdbassit/Coloris@latest/dist/coloris.min.css"/>
        <script src="https://cdn.jsdelivr.net/gh/mdbassit/Coloris@latest/dist/coloris.min.js"></script>

        <div style="display: flex; justify-content: center; align-items: center;height: 100%;">
            <div id="e-color-container" style="border: 1px solid #000;position: relative;height: 400px;width: 400px;"></div>
        </div>

        <script>
            document.addEventListener('DOMContentLoaded', () => {
                Coloris({
                    parent: '#e-color-container',
                    inline: true,
                    alpha: true,
                    theme: 'large',
                    format: 'mixed',
                    defaultColor: '#fff'
                });

                document.addEventListener('coloris:pick', event => {
                    console.log('New color', event.detail.color); 
                });
            });
        </script>
    </body>
</html>

and Preact

<html>
    <body style="margin: 0;padding: 0;">

        <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/mdbassit/Coloris@latest/dist/coloris.min.css"/>
        <script src="https://cdn.jsdelivr.net/gh/mdbassit/Coloris@latest/dist/coloris.min.js"></script>

        <script type="module">
            import { html, Component, render, useEffect } from 'https://unpkg.com/htm/preact/standalone.module.js';

            function App (props) {

                useEffect(() => {

                    Coloris({
                        parent: '#e-color-container',
                        inline: true,
                        alpha: true,
                        theme: 'large',
                        format: 'mixed',
                        defaultColor: '#fff'
                    });

                }, [])

              return html`
                <div style="display: flex; justify-content: center; align-items: center;height: 100%;">
                    <div id="e-color-container" style="border: 1px solid #000;position: relative;height: 400px;width: 400px;"></div>
                </div>
              `;
            }

            render(html`<${App} />`, document.body);
        </script>
    </body>
</html>

Thanks!

VelinGeorgiev commented 11 months ago

Will close the thread. Thanks!