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

Colorpicker wraps all input fields on page load #131

Closed leonardfischer closed 7 months ago

leonardfischer commented 8 months ago

Hello @mdbassit

I'm having the problem that all my colorpicker fields are "wrapped" even though I configure it like this:

Coloris({ wrap: false });

This seems to happen because "on load" the init method gets called with the default options which leads to wrapFields beeing called. Even if I configure Coloris afterwards, the fields are already wrapped :(

You can check that behaviour here: https://jsfiddle.net/xn67trqg/

Is there a workaround I can use? Maybe by using a different selector for el and then handling colorpickers manually?

MattOpen commented 7 months ago

I am also not a fan of "altering" existing dom elements. For a quick solution, I have rewritten the wrapFields function: (sure this is not the real solution, but it worked for me and I am willing to contribute a final solution)

`function wrapFields(selector) { document.querySelectorAll(selector).forEach(function (field) { var parentNode = field.parentNode;

  if (!parentNode.classList.contains('clr-field')) {
      var wrapper = field,
          classes = 'clr-field';
      wrapper.insertAdjacentHTML('beforebegin', "<button type=\"button\" aria-labelledby=\"clr-open-label\"></button>");
      parentNode.classList.add(classes);
      parentNode.style.color = field.value;

  //var wrapper = document.createElement('div');
  //var classes = 'clr-field';

  //if (settings.rtl || field.classList.contains('clr-rtl')) {
  //  classes += ' clr-rtl';
  //}

  //wrapper.innerHTML = "<button type=\"button\" aria-labelledby=\"clr-open-label\"></button>";
  //parentNode.insertBefore(wrapper, field);
  //wrapper.setAttribute('class', classes);
  //wrapper.style.color = field.value;
  //wrapper.appendChild(field);
}

}); }`

This is how the generated code looks like after: image

I am working with bootstrap and floating label and this the result image

mdbassit commented 7 months ago

@leonardfischer @MattOpen If you don't want to have your fields wrapped, you need to use a custom CSS class to initialize the color fields instead of the default [data-coloris] selector. As the doc states, the wrap option is ineffective on the default selector.

For my own uses, I need the color picker to keep this behavior, so I won't be accepting alterations to it.

MattOpen commented 7 months ago

Dear @mdbassit ,

I really love your work and you did a great job so far.

I have tried the solution with wrap: "false" and el: "#myid" The color picker is rendered but the additional button showing the color is not rendered. image

"For my own uses, I need the color picker to keep this behavior, so I won't be accepting alterations to it." thats also not my intention. I would like to enhance it, that you can use it with the additional div wrapped and others can use it without the additional div wrapped, but also showing the color in input.

it is unnecessary to fork the repo, only for this little enhancement. What do you think?

mdbassit commented 7 months ago

Thank you for your kind words !

I made this color picker mainly for my own use and decided to share it, modifying it to make it less convenient for me is not very reasonable. I'm afraid I have to insist on leaving it as is.

leonardfischer commented 7 months ago

Hey @mdbassit that's fine by me - I already use the workaround :)

MattOpen commented 2 months ago

Dear @mdbassit , ok fine. I have forked your repo and made my necessary changes. ColorisColorpicker