mdbassit / Coloris

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

Update swatches runtime #69

Closed giacomarco closed 1 year ago

giacomarco commented 1 year ago

Good morning. Ty for your project. I update your code for supply a personal need.

case 'swatches': if (Array.isArray(options.swatches)) { updateSwatches( options.swatches) } break;

Then I add a new function :

function updateSwatches(swatchesList) { var swatches = []; swatchesList.forEach(function (swatch, i) { swatches.push("<button id=\"clr-swatch-" + i + "\" aria-labelledby=\"clr-swatch-label clr-swatch-" + i + "\" style=\"color: " + swatch + ";\">" + swatch + "</button>"); }); getEl('clr-swatches').innerHTML = swatches.length ? "<div>" + swatches.join('') + "</div>" : ''; }

inside function Coloris I add a visibility for this function : Coloris.updateSwatches = updateSwatches;

with this code I have the possibilities to update swatches runtime.

mdbassit commented 1 year ago

You actually don't need to do any of that. If you just want to update the swatches at runtime, all you need to do is call the Coloris() function with the new swatch as a parameter like this:

Coloris({
  swatches: [
    'red',
    'blue',
    'green',
    'yellow',
    '#48cae4'
  ]
});
giacomarco commented 1 year ago

Work correctly. Thank you. I Try this way before change source code, probably I made some mistake when I try. You can close this issue. Thank you again.