index.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. const pickrContainer = document.querySelector('.pickr-container');
  2. const themeContainer = document.querySelector('.theme-container');
  3. const themes = [
  4. [
  5. 'classic',
  6. {
  7. swatches: [
  8. 'rgba(244, 67, 54, 1)',
  9. 'rgba(233, 30, 99, 0.95)',
  10. 'rgba(156, 39, 176, 0.9)',
  11. 'rgba(103, 58, 183, 0.85)',
  12. 'rgba(63, 81, 181, 0.8)',
  13. 'rgba(33, 150, 243, 0.75)',
  14. 'rgba(3, 169, 244, 0.7)',
  15. 'rgba(0, 188, 212, 0.7)',
  16. 'rgba(0, 150, 136, 0.75)',
  17. 'rgba(76, 175, 80, 0.8)',
  18. 'rgba(139, 195, 74, 0.85)',
  19. 'rgba(205, 220, 57, 0.9)',
  20. 'rgba(255, 235, 59, 0.95)',
  21. 'rgba(255, 193, 7, 1)'
  22. ],
  23. components: {
  24. preview: true,
  25. opacity: true,
  26. hue: true,
  27. interaction: {
  28. hex: true,
  29. rgba: true,
  30. hsva: true,
  31. input: true,
  32. clear: true,
  33. save: true
  34. }
  35. }
  36. }
  37. ],
  38. [
  39. 'monolith',
  40. {
  41. swatches: [
  42. 'rgba(244, 67, 54, 1)',
  43. 'rgba(233, 30, 99, 0.95)',
  44. 'rgba(156, 39, 176, 0.9)',
  45. 'rgba(103, 58, 183, 0.85)',
  46. 'rgba(63, 81, 181, 0.8)',
  47. 'rgba(33, 150, 243, 0.75)',
  48. 'rgba(3, 169, 244, 0.7)'
  49. ],
  50. defaultRepresentation: 'HEXA',
  51. components: {
  52. preview: true,
  53. opacity: true,
  54. hue: true,
  55. interaction: {
  56. hex: false,
  57. rgba: false,
  58. hsva: false,
  59. input: true,
  60. clear: true,
  61. save: true
  62. }
  63. }
  64. }
  65. ],
  66. [
  67. 'nano',
  68. {
  69. swatches: [
  70. 'rgba(244, 67, 54, 1)',
  71. 'rgba(233, 30, 99, 0.95)',
  72. 'rgba(156, 39, 176, 0.9)',
  73. 'rgba(103, 58, 183, 0.85)',
  74. 'rgba(63, 81, 181, 0.8)',
  75. 'rgba(33, 150, 243, 0.75)',
  76. 'rgba(3, 169, 244, 0.7)'
  77. ],
  78. defaultRepresentation: 'HEXA',
  79. components: {
  80. preview: true,
  81. opacity: true,
  82. hue: true,
  83. interaction: {
  84. hex: false,
  85. rgba: false,
  86. hsva: false,
  87. input: true,
  88. clear: true,
  89. save: true
  90. }
  91. }
  92. }
  93. ]
  94. ];
  95. const buttons = [];
  96. let pickr = null;
  97. for (const [theme, config] of themes) {
  98. const button = document.createElement('button');
  99. button.innerHTML = theme;
  100. buttons.push(button);
  101. button.addEventListener('click', () => {
  102. const el = document.createElement('p');
  103. pickrContainer.appendChild(el);
  104. // Delete previous instance
  105. if (pickr) {
  106. pickr.destroyAndRemove();
  107. }
  108. // Apply active class
  109. for (const btn of buttons) {
  110. btn.classList[btn === button ? 'add' : 'remove']('active');
  111. }
  112. // Create fresh instance
  113. pickr = new Pickr(Object.assign({
  114. el, theme,
  115. default: '#42445a'
  116. }, config));
  117. // Set events
  118. pickr.on('init', instance => {
  119. console.log('Event: "init"', instance);
  120. }).on('hide', instance => {
  121. console.log('Event: "hide"', instance);
  122. }).on('show', (color, instance) => {
  123. console.log('Event: "show"', color, instance);
  124. }).on('save', (color, instance) => {
  125. console.log('Event: "save"', color, instance);
  126. }).on('clear', instance => {
  127. console.log('Event: "clear"', instance);
  128. }).on('change', (color, source, instance) => {
  129. console.log('Event: "change"', color, source, instance);
  130. }).on('changestop', (source, instance) => {
  131. console.log('Event: "changestop"', source, instance);
  132. }).on('cancel', instance => {
  133. console.log('cancel', pickr.getColor().toRGBA().toString(0));
  134. }).on('swatchselect', (color, instance) => {
  135. console.log('Event: "swatchselect"', color, instance);
  136. });
  137. });
  138. themeContainer.appendChild(button);
  139. }
  140. buttons[0].click();