@designbasekorea/ui-wc 0.5.6 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (202) hide show
  1. package/dist/chunks/lottie-0e23d630.js +1 -0
  2. package/dist/esm/components/animation-background/canvas-controller.js +1 -121
  3. package/dist/esm/components/animation-background/controls.js +1 -180
  4. package/dist/esm/components/animation-background/graphite-controller.js +1 -196
  5. package/dist/esm/components/animation-background/graphite-presets.js +1 -42
  6. package/dist/esm/components/animation-background/graphite-shader.js +1 -111
  7. package/dist/esm/components/animation-background/layers.js +1 -369
  8. package/dist/esm/components/animation-background/lumina-presets.js +1 -53
  9. package/dist/esm/components/animation-background/lumina-shaders.js +1 -181
  10. package/dist/esm/components/animation-background/lumina-utils.js +1 -60
  11. package/dist/esm/components/animation-background/particle-defaults.js +1 -36
  12. package/dist/esm/components/animation-background/renderers.js +1 -167
  13. package/dist/esm/components/animation-background/shader-controller.js +1 -154
  14. package/dist/esm/components/color-picker-utils.js +1 -134
  15. package/dist/esm/components/date-picker-utils.js +1 -140
  16. package/dist/esm/components/db-accordion.js +1 -304
  17. package/dist/esm/components/db-ad-banner.js +1 -455
  18. package/dist/esm/components/db-ai-chat-list.d.ts +43 -0
  19. package/dist/esm/components/db-ai-chat-list.js +1 -0
  20. package/dist/esm/components/db-ai-chat.d.ts +159 -0
  21. package/dist/esm/components/db-ai-chat.js +1 -0
  22. package/dist/esm/components/db-alert.js +1 -149
  23. package/dist/esm/components/db-animation-background.js +1 -486
  24. package/dist/esm/components/db-animation-text.js +1 -601
  25. package/dist/esm/components/db-app-badge.js +1 -88
  26. package/dist/esm/components/db-audio-player.js +1 -752
  27. package/dist/esm/components/db-avatar.js +1 -188
  28. package/dist/esm/components/db-backdrop.js +1 -139
  29. package/dist/esm/components/db-badge.js +1 -91
  30. package/dist/esm/components/db-banner.js +1 -188
  31. package/dist/esm/components/db-bottom-navigation.js +1 -146
  32. package/dist/esm/components/db-bottom-sheet.js +1 -481
  33. package/dist/esm/components/db-breadcrumbs.js +1 -329
  34. package/dist/esm/components/db-button.js +1 -316
  35. package/dist/esm/components/db-calendar.js +1 -675
  36. package/dist/esm/components/db-card.js +1 -265
  37. package/dist/esm/components/db-carousel.js +1 -264
  38. package/dist/esm/components/db-chat-composer.d.ts +134 -0
  39. package/dist/esm/components/db-chat-composer.js +1 -0
  40. package/dist/esm/components/db-chat-list.d.ts +47 -0
  41. package/dist/esm/components/db-chat-list.js +1 -0
  42. package/dist/esm/components/db-chat-message.d.ts +39 -0
  43. package/dist/esm/components/db-chat-message.js +1 -0
  44. package/dist/esm/components/db-chat-orb.d.ts +99 -0
  45. package/dist/esm/components/db-chat-orb.js +1 -0
  46. package/dist/esm/components/db-chat-room.d.ts +71 -0
  47. package/dist/esm/components/db-chat-room.js +1 -0
  48. package/dist/esm/components/db-chat-suggestions.d.ts +44 -0
  49. package/dist/esm/components/db-chat-suggestions.js +1 -0
  50. package/dist/esm/components/db-chat-typing.d.ts +22 -0
  51. package/dist/esm/components/db-chat-typing.js +1 -0
  52. package/dist/esm/components/db-chat-welcome.d.ts +32 -0
  53. package/dist/esm/components/db-chat-welcome.js +1 -0
  54. package/dist/esm/components/db-chat-widget.d.ts +146 -0
  55. package/dist/esm/components/db-chat-widget.js +1 -0
  56. package/dist/esm/components/db-checkbox.js +1 -202
  57. package/dist/esm/components/db-chip.js +1 -104
  58. package/dist/esm/components/db-code-block.js +1 -304
  59. package/dist/esm/components/db-color-picker.js +1 -746
  60. package/dist/esm/components/db-confirm.js +1 -191
  61. package/dist/esm/components/db-container.js +1 -39
  62. package/dist/esm/components/db-context-menu.js +1 -265
  63. package/dist/esm/components/db-countdown.js +1 -146
  64. package/dist/esm/components/db-cursor-follower.js +1 -173
  65. package/dist/esm/components/db-date-picker.js +1 -837
  66. package/dist/esm/components/db-divider.js +1 -66
  67. package/dist/esm/components/db-download-progress.d.ts +89 -0
  68. package/dist/esm/components/db-download-progress.js +1 -0
  69. package/dist/esm/components/db-drawer.js +1 -245
  70. package/dist/esm/components/db-dropdown.js +1 -493
  71. package/dist/esm/components/db-dropzone.js +1 -266
  72. package/dist/esm/components/db-email-verify.d.ts +62 -0
  73. package/dist/esm/components/db-email-verify.js +1 -0
  74. package/dist/esm/components/db-empty-state.js +1 -97
  75. package/dist/esm/components/db-file-uploader.js +1 -256
  76. package/dist/esm/components/db-floating-action-button.js +1 -139
  77. package/dist/esm/components/db-footer.js +1 -385
  78. package/dist/esm/components/db-form.js +1 -566
  79. package/dist/esm/components/db-gantt.js +1 -882
  80. package/dist/esm/components/db-gradient.js +1 -133
  81. package/dist/esm/components/db-grid-background.js +1 -909
  82. package/dist/esm/components/db-grid.js +1 -73
  83. package/dist/esm/components/db-icons.d.ts +3 -0
  84. package/dist/esm/components/db-icons.js +1 -82
  85. package/dist/esm/components/db-image-list.js +1 -214
  86. package/dist/esm/components/db-image-placeholder.js +1 -96
  87. package/dist/esm/components/db-image.js +1 -289
  88. package/dist/esm/components/db-indicator.js +1 -517
  89. package/dist/esm/components/db-input.js +1 -420
  90. package/dist/esm/components/db-kanban.js +1 -538
  91. package/dist/esm/components/db-label.js +1 -66
  92. package/dist/esm/components/db-lightbox.js +1 -372
  93. package/dist/esm/components/db-list.js +1 -340
  94. package/dist/esm/components/db-logo-svg.js +1 -72
  95. package/dist/esm/components/db-logo.js +1 -106
  96. package/dist/esm/components/db-lottie.js +1 -109
  97. package/dist/esm/components/db-markdown-editor.js +1 -458
  98. package/dist/esm/components/db-marquee.js +1 -402
  99. package/dist/esm/components/db-masonry.js +1 -94
  100. package/dist/esm/components/db-menu-item.js +1 -211
  101. package/dist/esm/components/db-modal.js +1 -522
  102. package/dist/esm/components/db-navbar.js +1 -680
  103. package/dist/esm/components/db-onboarding-modal.js +1 -322
  104. package/dist/esm/components/db-otp-input.d.ts +53 -0
  105. package/dist/esm/components/db-otp-input.js +1 -0
  106. package/dist/esm/components/db-page-header.js +1 -312
  107. package/dist/esm/components/db-pagination.js +1 -271
  108. package/dist/esm/components/db-phone-verify.d.ts +61 -0
  109. package/dist/esm/components/db-phone-verify.js +1 -0
  110. package/dist/esm/components/db-popover.js +1 -580
  111. package/dist/esm/components/db-progress-step.js +1 -166
  112. package/dist/esm/components/db-progress.js +1 -171
  113. package/dist/esm/components/db-progressbar.js +1 -72
  114. package/dist/esm/components/db-radio.js +1 -172
  115. package/dist/esm/components/db-random-gradient.js +1 -121
  116. package/dist/esm/components/db-range-slider.js +1 -811
  117. package/dist/esm/components/db-rating.js +1 -325
  118. package/dist/esm/components/db-reorder.js +1 -286
  119. package/dist/esm/components/db-resizable-panels.js +1 -282
  120. package/dist/esm/components/db-scroll-area.js +1 -126
  121. package/dist/esm/components/db-search-bar.js +1 -668
  122. package/dist/esm/components/db-section-app-download.js +1 -101
  123. package/dist/esm/components/db-section-contact.js +1 -102
  124. package/dist/esm/components/db-section-cta.js +1 -150
  125. package/dist/esm/components/db-section-faq.js +1 -120
  126. package/dist/esm/components/db-section-feature-grid.js +1 -137
  127. package/dist/esm/components/db-section-feature.js +1 -179
  128. package/dist/esm/components/db-section-hero.js +1 -243
  129. package/dist/esm/components/db-section-logo-cloud.js +1 -138
  130. package/dist/esm/components/db-section-pricing.js +1 -166
  131. package/dist/esm/components/db-section-stats.js +1 -129
  132. package/dist/esm/components/db-section-testimonials.js +1 -252
  133. package/dist/esm/components/db-section.js +1 -385
  134. package/dist/esm/components/db-segment-control.js +1 -231
  135. package/dist/esm/components/db-select.js +1 -571
  136. package/dist/esm/components/db-share-icons.js +1 -40
  137. package/dist/esm/components/db-share.js +1 -489
  138. package/dist/esm/components/db-sidebar.js +1 -1068
  139. package/dist/esm/components/db-skeleton.js +1 -94
  140. package/dist/esm/components/db-spinner.js +1 -102
  141. package/dist/esm/components/db-split-view.js +1 -297
  142. package/dist/esm/components/db-stack.js +1 -46
  143. package/dist/esm/components/db-stat.js +1 -218
  144. package/dist/esm/components/db-stepper.js +1 -398
  145. package/dist/esm/components/db-table.js +1 -412
  146. package/dist/esm/components/db-tabs.js +1 -335
  147. package/dist/esm/components/db-testimonial.d.ts +2 -0
  148. package/dist/esm/components/db-testimonial.js +1 -162
  149. package/dist/esm/components/db-textarea.js +1 -233
  150. package/dist/esm/components/db-time-picker.js +1 -523
  151. package/dist/esm/components/db-timeline.js +1 -174
  152. package/dist/esm/components/db-toast.js +1 -158
  153. package/dist/esm/components/db-toggle.js +1 -162
  154. package/dist/esm/components/db-toolbar.js +1 -94
  155. package/dist/esm/components/db-tooltip.js +1 -440
  156. package/dist/esm/components/db-top-banner.js +1 -121
  157. package/dist/esm/components/db-tutorial.js +1 -324
  158. package/dist/esm/components/db-two-factor.d.ts +60 -0
  159. package/dist/esm/components/db-two-factor.js +1 -0
  160. package/dist/esm/components/db-video-player.js +1 -503
  161. package/dist/esm/components/db-youtube-player.js +1 -194
  162. package/dist/esm/components/highlight-code.js +1 -65
  163. package/dist/esm/components/input-validation.js +1 -50
  164. package/dist/esm/components/marketing-block-host-utils.js +1 -9
  165. package/dist/esm/components/number-scrub.js +1 -117
  166. package/dist/esm/components/random-gradient-palettes.js +1 -67
  167. package/dist/esm/components/section-block-header-utils.js +1 -48
  168. package/dist/esm/helpers/confirm.js +1 -52
  169. package/dist/esm/helpers/toast.js +1 -73
  170. package/dist/esm/index.d.ts +16 -0
  171. package/dist/esm/index.js +1 -111
  172. package/dist/esm/node_modules/.pnpm/prismjs@1.30.0/node_modules/prismjs/components/prism-bash.js +1 -0
  173. package/dist/esm/node_modules/.pnpm/prismjs@1.30.0/node_modules/prismjs/components/prism-clike.js +1 -0
  174. package/dist/esm/node_modules/.pnpm/prismjs@1.30.0/node_modules/prismjs/components/prism-css.js +1 -0
  175. package/dist/esm/node_modules/.pnpm/prismjs@1.30.0/node_modules/prismjs/components/prism-javascript.js +1 -0
  176. package/dist/esm/node_modules/.pnpm/prismjs@1.30.0/node_modules/prismjs/components/prism-json.js +1 -0
  177. package/dist/esm/node_modules/.pnpm/prismjs@1.30.0/node_modules/prismjs/components/prism-jsx.js +1 -0
  178. package/dist/esm/node_modules/.pnpm/prismjs@1.30.0/node_modules/prismjs/components/prism-markup.js +1 -0
  179. package/dist/esm/node_modules/.pnpm/prismjs@1.30.0/node_modules/prismjs/components/prism-scss.js +1 -0
  180. package/dist/esm/node_modules/.pnpm/prismjs@1.30.0/node_modules/prismjs/components/prism-tsx.js +1 -0
  181. package/dist/esm/node_modules/.pnpm/prismjs@1.30.0/node_modules/prismjs/components/prism-typescript.js +1 -0
  182. package/dist/index.cjs +1 -55414
  183. package/dist/index.css +1 -1
  184. package/dist/index.d.ts +1091 -2
  185. package/dist/index.esm.js +1 -38284
  186. package/dist/react/chunks/lottie-14e7addb.js +1 -0
  187. package/dist/react/index.cjs +1 -56503
  188. package/dist/react/index.d.ts +1620 -54
  189. package/dist/react/index.esm.js +1 -39373
  190. package/package.json +1 -1
  191. package/dist/chunks/lottie-e273b936.js +0 -17032
  192. package/dist/esm/node_modules/prismjs/components/prism-bash.js +0 -235
  193. package/dist/esm/node_modules/prismjs/components/prism-clike.js +0 -31
  194. package/dist/esm/node_modules/prismjs/components/prism-css.js +0 -64
  195. package/dist/esm/node_modules/prismjs/components/prism-javascript.js +0 -172
  196. package/dist/esm/node_modules/prismjs/components/prism-json.js +0 -27
  197. package/dist/esm/node_modules/prismjs/components/prism-jsx.js +0 -143
  198. package/dist/esm/node_modules/prismjs/components/prism-markup.js +0 -186
  199. package/dist/esm/node_modules/prismjs/components/prism-scss.js +0 -81
  200. package/dist/esm/node_modules/prismjs/components/prism-tsx.js +0 -15
  201. package/dist/esm/node_modules/prismjs/components/prism-typescript.js +0 -60
  202. package/dist/react/chunks/lottie-72a4c6d4.js +0 -17032
@@ -1,746 +1 @@
1
- import { hexToRgb, rgbToHsv, hsvToRgb, toHex, rgbToHsl, formatColor, normalizeHex, isHex, clamp } from './color-picker-utils.js';
2
- import { getDbComponentIconSize, createDbIcon } from './db-icons.js';
3
-
4
- const SIZES = ['s', 'm', 'l'];
5
- const TYPES = ['dropdown', 'modal'];
6
- const POSITIONS = ['bottom-start', 'bottom-end', 'top-start', 'top-end'];
7
- const DEFAULT_COLOR = '#006FFF';
8
- const DEFAULT_RGB = hexToRgb(DEFAULT_COLOR) ?? { r: 0, g: 111, b: 255 };
9
- const DEFAULT_HSV = rgbToHsv(DEFAULT_RGB.r, DEFAULT_RGB.g, DEFAULT_RGB.b);
10
- const FORMAT_OPTIONS = JSON.stringify([
11
- { label: 'HEX', value: 'hex' },
12
- { label: 'RGB', value: 'rgb' },
13
- { label: 'RGBA', value: 'rgba' },
14
- { label: 'HSL', value: 'hsl' },
15
- { label: 'HSLA', value: 'hsla' },
16
- ]);
17
- class DbColorPicker extends HTMLElement {
18
- constructor() {
19
- super(...arguments);
20
- this.h = DEFAULT_HSV.h;
21
- this.s = DEFAULT_HSV.s;
22
- this.v = DEFAULT_HSV.v;
23
- this.a = 100;
24
- this.open = false;
25
- this.format = 'hex';
26
- this.colorInput = DEFAULT_COLOR;
27
- this.alphaInput = '100';
28
- this.isCopied = false;
29
- this.tempColor = '';
30
- this.dragging = null;
31
- this.mounted = false;
32
- this.copiedTimer = null;
33
- this.debounceTimer = null;
34
- this.lastEmittedHex = '';
35
- this.suppressNextValueSync = false;
36
- this.didMount = false;
37
- this.triggerInputFocused = false;
38
- this.panelInputFocused = false;
39
- this.panelAlphaInputFocused = false;
40
- this.handleOutsideClick = (event) => {
41
- if (!this.open || this.getOption('type', TYPES, 'dropdown') !== 'dropdown')
42
- return;
43
- const target = event.target;
44
- if (this.contains(target))
45
- return;
46
- if (this.formatSelectEl?.contains(target))
47
- return;
48
- this.open = false;
49
- this.update();
50
- };
51
- this.handleDocumentMouseMove = (event) => {
52
- if (this.dragging !== 'area')
53
- return;
54
- if (this.getBool('disabled') || this.getBool('readonly'))
55
- return;
56
- this.updateFromArea(event.clientX, event.clientY);
57
- };
58
- this.handleDocumentMouseUp = () => {
59
- if (this.dragging !== 'area')
60
- return;
61
- this.dragging = null;
62
- this.onColorStateChanged(true);
63
- };
64
- this.handleModalCancel = () => {
65
- const rgb = hexToRgb(this.tempColor);
66
- if (rgb) {
67
- const hsv = rgbToHsv(rgb.r, rgb.g, rgb.b);
68
- this.h = hsv.h;
69
- this.s = hsv.s;
70
- this.v = hsv.v;
71
- }
72
- this.open = false;
73
- this.update();
74
- this.dispatchEvent(new CustomEvent('db-cancel', { bubbles: true, composed: true }));
75
- };
76
- this.handleModalApply = () => {
77
- this.open = false;
78
- this.actuallyEmit();
79
- this.dispatchEvent(new CustomEvent('db-apply', { detail: { value: this.getOutward() }, bubbles: true, composed: true }));
80
- this.update();
81
- };
82
- }
83
- static get observedAttributes() {
84
- return [
85
- 'size',
86
- 'type',
87
- 'position',
88
- 'value',
89
- 'default-value',
90
- 'show-input',
91
- 'show-alpha',
92
- 'show-format-selector',
93
- 'show-copy-button',
94
- 'show-eyedropper',
95
- 'disabled',
96
- 'readonly',
97
- 'full-width',
98
- 'error',
99
- 'on-change-format',
100
- 'change-debounce-ms',
101
- 'emit-during-drag',
102
- ];
103
- }
104
- connectedCallback() {
105
- if (!this.mounted)
106
- this.mount();
107
- this.syncFromValueAttr(true);
108
- this.update();
109
- document.addEventListener('mousedown', this.handleOutsideClick);
110
- document.addEventListener('mousemove', this.handleDocumentMouseMove);
111
- document.addEventListener('mouseup', this.handleDocumentMouseUp);
112
- }
113
- disconnectedCallback() {
114
- document.removeEventListener('mousedown', this.handleOutsideClick);
115
- document.removeEventListener('mousemove', this.handleDocumentMouseMove);
116
- document.removeEventListener('mouseup', this.handleDocumentMouseUp);
117
- if (this.copiedTimer)
118
- clearTimeout(this.copiedTimer);
119
- if (this.debounceTimer)
120
- clearTimeout(this.debounceTimer);
121
- }
122
- attributeChangedCallback(name) {
123
- if (!this.mounted)
124
- return;
125
- if (name === 'value' || name === 'default-value')
126
- this.syncFromValueAttr(false);
127
- this.update();
128
- }
129
- getBool(name, fallback = false) {
130
- if (!this.hasAttribute(name))
131
- return fallback;
132
- const value = this.getAttribute(name);
133
- return value === '' || value === 'true';
134
- }
135
- getOption(name, values, fallback) {
136
- const value = this.getAttribute(name);
137
- return value && values.includes(value) ? value : fallback;
138
- }
139
- getIconSize() {
140
- return getDbComponentIconSize(this.getOption('size', SIZES, 'm'));
141
- }
142
- getRgb() {
143
- return hsvToRgb(this.h, this.s, this.v);
144
- }
145
- getHex() {
146
- const rgb = this.getRgb();
147
- return toHex(rgb.r, rgb.g, rgb.b);
148
- }
149
- getHsl() {
150
- const rgb = this.getRgb();
151
- return rgbToHsl(rgb.r, rgb.g, rgb.b);
152
- }
153
- getUiFormatted() {
154
- const rgb = this.getRgb();
155
- const hex = this.getHex();
156
- const hsl = this.getHsl();
157
- return formatColor(this.format, rgb, hex, hsl, this.a);
158
- }
159
- getOutward() {
160
- const onChangeFormat = this.getAttribute('on-change-format') === 'ui' ? 'ui' : 'hex';
161
- return onChangeFormat === 'hex' ? this.getHex() : this.getUiFormatted();
162
- }
163
- syncFromValueAttr(initial) {
164
- const raw = this.getAttribute('value')
165
- ?? (initial ? this.getAttribute('default-value') : null)
166
- ?? (initial ? DEFAULT_COLOR : null);
167
- if (raw == null)
168
- return;
169
- const norm = normalizeHex(raw);
170
- if (!isHex(norm))
171
- return;
172
- if (!initial && this.suppressNextValueSync && norm === this.lastEmittedHex) {
173
- this.suppressNextValueSync = false;
174
- return;
175
- }
176
- const rgb = hexToRgb(norm);
177
- if (!rgb)
178
- return;
179
- const hsv = rgbToHsv(rgb.r, rgb.g, rgb.b);
180
- this.h = hsv.h;
181
- this.s = hsv.s;
182
- this.v = hsv.v;
183
- this.colorInput = norm;
184
- this.lastEmittedHex = norm;
185
- }
186
- actuallyEmit() {
187
- const currentHex = normalizeHex(this.getHex());
188
- const parentHex = this.hasAttribute('value') && isHex(this.getAttribute('value') || '')
189
- ? normalizeHex(this.getAttribute('value') || '')
190
- : null;
191
- if (parentHex && parentHex === currentHex)
192
- return;
193
- if (this.lastEmittedHex === currentHex)
194
- return;
195
- this.lastEmittedHex = currentHex;
196
- this.suppressNextValueSync = true;
197
- this.setAttribute('value', currentHex);
198
- this.dispatchEvent(new CustomEvent('db-change', {
199
- detail: { value: this.getOutward() },
200
- bubbles: true,
201
- composed: true,
202
- }));
203
- }
204
- scheduleEmit() {
205
- const emitDuringDrag = this.getBool('emit-during-drag', true);
206
- if (this.dragging && !emitDuringDrag)
207
- return;
208
- const debounceMs = Number(this.getAttribute('change-debounce-ms') || '0');
209
- if (debounceMs > 0) {
210
- if (this.debounceTimer)
211
- clearTimeout(this.debounceTimer);
212
- this.debounceTimer = setTimeout(() => this.actuallyEmit(), debounceMs);
213
- return;
214
- }
215
- this.actuallyEmit();
216
- }
217
- onColorStateChanged(fromDragEnd = false) {
218
- if (!this.didMount) {
219
- this.didMount = true;
220
- return;
221
- }
222
- if (fromDragEnd && !this.getBool('emit-during-drag', true)) {
223
- this.scheduleEmit();
224
- return;
225
- }
226
- this.scheduleEmit();
227
- }
228
- updateFromArea(clientX, clientY) {
229
- const el = this.colorFieldEl;
230
- if (!el)
231
- return;
232
- const rect = el.getBoundingClientRect();
233
- const x = clamp(clientX - rect.left, 0, rect.width);
234
- const y = clamp(clientY - rect.top, 0, rect.height);
235
- this.s = (x / rect.width) * 100;
236
- this.v = 100 - (y / rect.height) * 100;
237
- this.updateVisuals();
238
- this.onColorStateChanged();
239
- }
240
- getInputValue(el) {
241
- if (!el)
242
- return '';
243
- return el.getAttribute('value') ?? '';
244
- }
245
- setInputValue(el, value) {
246
- if (!el || value === undefined)
247
- return;
248
- el.setAttribute('value', value);
249
- }
250
- getPanelControlSize() {
251
- const size = this.getOption('size', SIZES, 'm');
252
- return size === 's' ? 's' : 'm';
253
- }
254
- commitColorInput(source) {
255
- const str = (source === 'trigger'
256
- ? this.triggerInputEl?.value
257
- : this.getInputValue(this.panelValueInputEl)
258
- ?? '').trim();
259
- if (/^#([0-9A-Fa-f]{6})$/.test(str)) {
260
- const rgb = hexToRgb(str);
261
- const hsv = rgbToHsv(rgb.r, rgb.g, rgb.b);
262
- this.h = hsv.h;
263
- this.s = hsv.s;
264
- this.v = hsv.v;
265
- this.colorInput = normalizeHex(str);
266
- this.updateVisuals();
267
- this.onColorStateChanged();
268
- return;
269
- }
270
- let m = /^rgb\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*\)$/i.exec(str);
271
- if (m) {
272
- const r = clamp(parseInt(m[1], 10), 0, 255);
273
- const g = clamp(parseInt(m[2], 10), 0, 255);
274
- const b = clamp(parseInt(m[3], 10), 0, 255);
275
- const hsv = rgbToHsv(r, g, b);
276
- this.h = hsv.h;
277
- this.s = hsv.s;
278
- this.v = hsv.v;
279
- this.colorInput = `rgb(${r}, ${g}, ${b})`;
280
- this.updateVisuals();
281
- this.onColorStateChanged();
282
- return;
283
- }
284
- m = /^rgba\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(0|1|0?\.\d+)\s*\)$/i.exec(str);
285
- if (m) {
286
- const r = clamp(parseInt(m[1], 10), 0, 255);
287
- const g = clamp(parseInt(m[2], 10), 0, 255);
288
- const b = clamp(parseInt(m[3], 10), 0, 255);
289
- const alpha = parseFloat(m[4]);
290
- const hsv = rgbToHsv(r, g, b);
291
- this.h = hsv.h;
292
- this.s = hsv.s;
293
- this.v = hsv.v;
294
- this.a = Math.round(alpha * 100);
295
- this.alphaInput = String(this.a);
296
- this.colorInput = `rgba(${r}, ${g}, ${b}, ${alpha})`;
297
- this.updateVisuals();
298
- this.onColorStateChanged();
299
- }
300
- }
301
- commitAlphaInput() {
302
- const n = Number(this.getInputValue(this.panelAlphaInputEl).trim() || this.alphaInput);
303
- if (!Number.isNaN(n) && n >= 0 && n <= 100) {
304
- const rounded = Math.round(n);
305
- this.a = rounded;
306
- this.alphaInput = String(rounded);
307
- this.updateVisuals();
308
- this.onColorStateChanged();
309
- }
310
- else {
311
- this.alphaInput = String(this.a);
312
- if (this.panelAlphaInputEl)
313
- this.setInputValue(this.panelAlphaInputEl, this.alphaInput);
314
- }
315
- }
316
- async onCopy() {
317
- try {
318
- await navigator.clipboard.writeText(this.getUiFormatted());
319
- this.isCopied = true;
320
- this.updateCopyButtons();
321
- if (this.copiedTimer)
322
- clearTimeout(this.copiedTimer);
323
- this.copiedTimer = setTimeout(() => {
324
- this.isCopied = false;
325
- this.updateCopyButtons();
326
- }, 1600);
327
- }
328
- catch {
329
- // noop
330
- }
331
- }
332
- async onEyedrop() {
333
- try {
334
- if ('EyeDropper' in window) {
335
- const eye = new window.EyeDropper();
336
- const { sRGBHex } = await eye.open();
337
- const rgb = hexToRgb(sRGBHex);
338
- if (!rgb)
339
- return;
340
- const hsv = rgbToHsv(rgb.r, rgb.g, rgb.b);
341
- this.h = hsv.h;
342
- this.s = hsv.s;
343
- this.v = hsv.v;
344
- this.updateVisuals();
345
- this.onColorStateChanged();
346
- }
347
- }
348
- catch {
349
- // noop
350
- }
351
- }
352
- toggleOpen() {
353
- if (this.getBool('disabled') || this.getBool('readonly'))
354
- return;
355
- const type = this.getOption('type', TYPES, 'dropdown');
356
- if (type === 'modal') {
357
- this.tempColor = this.getHex();
358
- this.open = true;
359
- this.update();
360
- return;
361
- }
362
- this.open = !this.open;
363
- this.update();
364
- }
365
- mount() {
366
- this.mounted = true;
367
- const iconSize = this.getIconSize();
368
- this.triggerEl = document.createElement('div');
369
- this.triggerEl.className = 'designbase-wc-color-picker__trigger';
370
- this.triggerEl.addEventListener('click', () => this.toggleOpen());
371
- const colorDisplay = document.createElement('div');
372
- colorDisplay.className = 'designbase-wc-color-picker__color-display';
373
- this.colorBoxEl = document.createElement('div');
374
- this.colorBoxEl.className = 'designbase-wc-color-picker__color-box';
375
- colorDisplay.append(this.colorBoxEl);
376
- this.triggerInputEl = document.createElement('input');
377
- this.triggerInputEl.type = 'text';
378
- this.triggerInputEl.className = 'designbase-wc-color-picker__input';
379
- this.triggerInputEl.placeholder = '#000000';
380
- this.triggerInputEl.addEventListener('click', (e) => e.stopPropagation());
381
- this.triggerInputEl.addEventListener('focus', () => {
382
- this.triggerInputFocused = true;
383
- });
384
- this.triggerInputEl.addEventListener('blur', () => {
385
- this.triggerInputFocused = false;
386
- const next = this.format === 'hex' ? normalizeHex(this.getUiFormatted()) : this.getUiFormatted();
387
- if (this.triggerInputEl && this.triggerInputEl.value !== next) {
388
- this.triggerInputEl.value = next;
389
- this.colorInput = next;
390
- }
391
- });
392
- this.triggerInputEl.addEventListener('input', () => {
393
- if (this.triggerInputEl)
394
- this.colorInput = this.triggerInputEl.value;
395
- });
396
- this.triggerInputEl.addEventListener('keydown', (e) => {
397
- if (e.key === 'Enter')
398
- this.commitColorInput('trigger');
399
- });
400
- const actions = document.createElement('div');
401
- actions.className = 'designbase-wc-color-picker__trigger-actions';
402
- this.triggerCopyBtn = document.createElement('button');
403
- this.triggerCopyBtn.type = 'button';
404
- this.triggerCopyBtn.className = 'designbase-wc-color-picker__copy-button-inline';
405
- this.triggerCopyBtn.setAttribute('aria-label', 'Copy color value');
406
- this.triggerCopyBtn.addEventListener('click', (e) => {
407
- e.stopPropagation();
408
- void this.onCopy();
409
- });
410
- this.toggleBtn = document.createElement('button');
411
- this.toggleBtn.type = 'button';
412
- this.toggleBtn.className = 'designbase-wc-color-picker__toggle';
413
- this.toggleBtn.setAttribute('aria-label', 'Toggle color picker');
414
- this.toggleBtn.append(createDbIcon('chevron-down', iconSize));
415
- actions.append(this.triggerCopyBtn, this.toggleBtn);
416
- this.triggerEl.append(colorDisplay, this.triggerInputEl, actions);
417
- this.dropdownEl = document.createElement('div');
418
- this.dropdownEl.className = 'designbase-wc-color-picker__dropdown';
419
- this.dropdownEl.addEventListener('click', (e) => e.stopPropagation());
420
- this.dropdownEl.hidden = true;
421
- const selector = document.createElement('div');
422
- selector.className = 'designbase-wc-color-picker__selector';
423
- const colorArea = document.createElement('div');
424
- colorArea.className = 'designbase-wc-color-picker__color-area';
425
- this.colorFieldEl = document.createElement('div');
426
- this.colorFieldEl.className = 'designbase-wc-color-picker__color-field';
427
- this.colorPointerEl = document.createElement('div');
428
- this.colorPointerEl.className = 'designbase-wc-color-picker__color-pointer';
429
- this.colorFieldEl.append(this.colorPointerEl);
430
- this.colorFieldEl.addEventListener('mousedown', (e) => {
431
- if (this.getBool('disabled') || this.getBool('readonly'))
432
- return;
433
- e.preventDefault();
434
- this.dragging = 'area';
435
- this.updateFromArea(e.clientX, e.clientY);
436
- });
437
- this.colorFieldEl.addEventListener('touchstart', (e) => {
438
- if (this.getBool('disabled') || this.getBool('readonly'))
439
- return;
440
- this.dragging = 'area';
441
- const t = e.touches[0];
442
- this.updateFromArea(t.clientX, t.clientY);
443
- }, { passive: true });
444
- this.colorFieldEl.addEventListener('touchmove', (e) => {
445
- if (this.dragging !== 'area')
446
- return;
447
- const t = e.touches[0];
448
- this.updateFromArea(t.clientX, t.clientY);
449
- }, { passive: true });
450
- this.colorFieldEl.addEventListener('touchend', () => {
451
- if (this.dragging !== 'area')
452
- return;
453
- this.dragging = null;
454
- this.onColorStateChanged(true);
455
- });
456
- colorArea.append(this.colorFieldEl);
457
- const controls = document.createElement('div');
458
- controls.className = 'designbase-wc-color-picker__controls';
459
- this.eyedropperBtnEl = document.createElement('db-button');
460
- this.eyedropperBtnEl.setAttribute('variant', 'tertiary');
461
- this.eyedropperBtnEl.setAttribute('size', this.getPanelControlSize());
462
- this.eyedropperBtnEl.setAttribute('icon-only', '');
463
- this.eyedropperBtnEl.setAttribute('start-icon', 'eyedropper');
464
- this.eyedropperBtnEl.setAttribute('aria-label', 'Eyedropper tool');
465
- this.eyedropperBtnEl.addEventListener('click', () => void this.onEyedrop());
466
- const sliderContainer = document.createElement('div');
467
- sliderContainer.className = 'designbase-wc-color-picker__slider-container';
468
- const hueWrap = document.createElement('div');
469
- hueWrap.className = 'designbase-wc-color-picker__hue-slider';
470
- this.hueSliderEl = document.createElement('input');
471
- this.hueSliderEl.type = 'range';
472
- this.hueSliderEl.min = '0';
473
- this.hueSliderEl.max = '360';
474
- this.hueSliderEl.className = 'designbase-wc-color-picker__slider designbase-wc-color-picker__slider--hue';
475
- this.hueSliderEl.addEventListener('input', () => {
476
- this.h = parseFloat(this.hueSliderEl.value);
477
- this.updateVisuals();
478
- this.onColorStateChanged();
479
- });
480
- hueWrap.append(this.hueSliderEl);
481
- this.alphaSliderWrapEl = document.createElement('div');
482
- this.alphaSliderWrapEl.className = 'designbase-wc-color-picker__alpha-slider';
483
- this.alphaSliderEl = document.createElement('input');
484
- this.alphaSliderEl.type = 'range';
485
- this.alphaSliderEl.min = '0';
486
- this.alphaSliderEl.max = '100';
487
- this.alphaSliderEl.className = 'designbase-wc-color-picker__slider designbase-wc-color-picker__slider--alpha';
488
- this.alphaSliderEl.addEventListener('input', () => {
489
- this.a = clamp(parseInt(this.alphaSliderEl.value, 10), 0, 100);
490
- this.alphaInput = String(this.a);
491
- if (this.panelAlphaInputEl)
492
- this.setInputValue(this.panelAlphaInputEl, this.alphaInput);
493
- this.updateVisuals();
494
- this.onColorStateChanged();
495
- });
496
- this.alphaSliderWrapEl.append(this.alphaSliderEl);
497
- sliderContainer.append(hueWrap, this.alphaSliderWrapEl);
498
- controls.append(this.eyedropperBtnEl, sliderContainer);
499
- const valueDisplay = document.createElement('div');
500
- valueDisplay.className = 'designbase-wc-color-picker__value-display';
501
- this.formatSelectEl = document.createElement('db-select');
502
- this.formatSelectEl.setAttribute('size', 's');
503
- this.formatSelectEl.setAttribute('position', 'top');
504
- this.formatSelectEl.setAttribute('show-clear-button', 'false');
505
- this.formatSelectEl.setAttribute('options', FORMAT_OPTIONS);
506
- this.formatSelectEl.addEventListener('db-change', (e) => {
507
- const detail = e.detail;
508
- const next = detail?.value;
509
- if (next) {
510
- this.format = next;
511
- this.updateVisuals();
512
- }
513
- });
514
- this.panelValueInputEl = document.createElement('db-input');
515
- this.panelValueInputEl.classList.add('designbase-wc-color-picker__value-input');
516
- this.panelValueInputEl.setAttribute('size', 's');
517
- this.panelValueInputEl.setAttribute('placeholder', '#000000');
518
- this.panelValueInputEl.addEventListener('db-focus', () => {
519
- this.panelInputFocused = true;
520
- });
521
- this.panelValueInputEl.addEventListener('db-blur', () => {
522
- this.panelInputFocused = false;
523
- const next = this.format === 'hex' ? normalizeHex(this.getUiFormatted()) : this.getUiFormatted();
524
- if (this.getInputValue(this.panelValueInputEl) !== next) {
525
- this.setInputValue(this.panelValueInputEl, next);
526
- this.colorInput = next;
527
- }
528
- });
529
- this.panelValueInputEl.addEventListener('db-input', (e) => {
530
- const detail = e.detail;
531
- if (detail?.value !== undefined)
532
- this.colorInput = detail.value;
533
- });
534
- this.panelValueInputEl.addEventListener('keydown', (e) => {
535
- if (e.key === 'Enter')
536
- this.commitColorInput('panel');
537
- });
538
- this.panelAlphaWrapEl = document.createElement('div');
539
- this.panelAlphaWrapEl.className = 'designbase-wc-color-picker__alpha-input-wrap';
540
- this.panelAlphaInputEl = document.createElement('db-input');
541
- this.panelAlphaInputEl.classList.add('designbase-wc-color-picker__alpha-input');
542
- this.panelAlphaInputEl.setAttribute('size', 's');
543
- this.panelAlphaInputEl.setAttribute('placeholder', '100');
544
- this.panelAlphaInputEl.setAttribute('aria-label', 'Alpha percent');
545
- this.panelAlphaInputEl.addEventListener('db-focus', () => {
546
- this.panelAlphaInputFocused = true;
547
- });
548
- this.panelAlphaInputEl.addEventListener('db-blur', () => {
549
- this.panelAlphaInputFocused = false;
550
- this.commitAlphaInput();
551
- });
552
- this.panelAlphaInputEl.addEventListener('db-input', (e) => {
553
- const detail = e.detail;
554
- const cleaned = (detail?.value ?? '').replace(/[^\d]/g, '').slice(0, 3);
555
- if (cleaned !== detail?.value) {
556
- this.setInputValue(this.panelAlphaInputEl, cleaned);
557
- }
558
- this.alphaInput = cleaned;
559
- });
560
- this.panelAlphaInputEl.addEventListener('keydown', (e) => {
561
- if (e.key === 'Enter')
562
- this.commitAlphaInput();
563
- });
564
- const alphaSuffix = document.createElement('span');
565
- alphaSuffix.className = 'designbase-wc-color-picker__alpha-suffix';
566
- alphaSuffix.textContent = '%';
567
- this.panelAlphaWrapEl.append(this.panelAlphaInputEl, alphaSuffix);
568
- this.panelCopyBtnEl = document.createElement('db-button');
569
- this.panelCopyBtnEl.setAttribute('variant', 'tertiary');
570
- this.panelCopyBtnEl.setAttribute('size', 's');
571
- this.panelCopyBtnEl.setAttribute('icon-only', '');
572
- this.panelCopyBtnEl.setAttribute('start-icon', 'copy');
573
- this.panelCopyBtnEl.setAttribute('aria-label', 'Copy color value');
574
- this.panelCopyBtnEl.addEventListener('click', () => void this.onCopy());
575
- valueDisplay.append(this.formatSelectEl, this.panelValueInputEl, this.panelAlphaWrapEl, this.panelCopyBtnEl);
576
- selector.append(colorArea, controls, valueDisplay);
577
- this.selectorEl = selector;
578
- this.dropdownEl.append(selector);
579
- this.modalEl = document.createElement('db-modal');
580
- this.modalEl.setAttribute('size', 's');
581
- this.modalEl.setAttribute('title', '색상 선택');
582
- this.modalEl.setAttribute('close-on-escape', 'true');
583
- this.modalEl.addEventListener('db-close', this.handleModalCancel);
584
- this.modalBodyEl = document.createElement('div');
585
- this.modalBodyEl.setAttribute('data-modal-part', 'body');
586
- const modalFooter = document.createElement('div');
587
- modalFooter.setAttribute('data-modal-part', 'footer');
588
- modalFooter.style.display = 'flex';
589
- modalFooter.style.gap = '8px';
590
- modalFooter.style.justifyContent = 'flex-end';
591
- const cancelBtn = document.createElement('db-button');
592
- cancelBtn.setAttribute('variant', 'tertiary');
593
- cancelBtn.setAttribute('size', 'm');
594
- cancelBtn.textContent = '취소';
595
- cancelBtn.addEventListener('click', this.handleModalCancel);
596
- const applyBtn = document.createElement('db-button');
597
- applyBtn.setAttribute('variant', 'primary');
598
- applyBtn.setAttribute('size', 'm');
599
- applyBtn.textContent = '적용';
600
- applyBtn.addEventListener('click', this.handleModalApply);
601
- modalFooter.append(cancelBtn, applyBtn);
602
- this.modalEl.append(this.modalBodyEl, modalFooter);
603
- this.replaceChildren(this.triggerEl, this.dropdownEl, this.modalEl);
604
- }
605
- updateCopyButtons() {
606
- const iconSize = this.getIconSize();
607
- const icon = this.isCopied ? 'done' : 'copy';
608
- if (this.triggerCopyBtn) {
609
- this.triggerCopyBtn.replaceChildren(createDbIcon(icon, iconSize));
610
- }
611
- if (this.panelCopyBtnEl) {
612
- this.panelCopyBtnEl.setAttribute('start-icon', icon);
613
- }
614
- }
615
- updateVisuals() {
616
- const rgb = this.getRgb();
617
- const hex = this.getHex();
618
- const showAlpha = this.getBool('show-alpha', true);
619
- const uiFormatted = this.getUiFormatted();
620
- const displayValue = this.format === 'hex' ? normalizeHex(uiFormatted) : uiFormatted;
621
- if (!this.triggerInputFocused && this.triggerInputEl) {
622
- this.triggerInputEl.value = displayValue;
623
- }
624
- if (!this.panelInputFocused && this.panelValueInputEl) {
625
- this.setInputValue(this.panelValueInputEl, displayValue);
626
- }
627
- if (this.colorBoxEl) {
628
- this.colorBoxEl.style.backgroundColor = showAlpha
629
- ? `rgba(${rgb.r}, ${rgb.g}, ${rgb.b}, ${this.a / 100})`
630
- : hex;
631
- }
632
- if (this.colorFieldEl) {
633
- this.colorFieldEl.style.backgroundImage = `
634
- linear-gradient(to top, rgba(0,0,0,1), rgba(0,0,0,0)),
635
- linear-gradient(to right, #ffffff, hsl(${this.h}, 100%, 50%))
636
- `;
637
- }
638
- if (this.colorPointerEl) {
639
- this.colorPointerEl.style.left = `${this.s}%`;
640
- this.colorPointerEl.style.top = `${100 - this.v}%`;
641
- this.colorPointerEl.style.backgroundColor = `rgb(${rgb.r}, ${rgb.g}, ${rgb.b})`;
642
- }
643
- if (this.hueSliderEl) {
644
- this.hueSliderEl.value = String(this.h);
645
- this.hueSliderEl.style.background = `linear-gradient(to right,
646
- hsl(0,100%,50%),
647
- hsl(60,100%,50%),
648
- hsl(120,100%,50%),
649
- hsl(180,100%,50%),
650
- hsl(240,100%,50%),
651
- hsl(300,100%,50%),
652
- hsl(360,100%,50%))`;
653
- }
654
- if (this.alphaSliderEl) {
655
- this.alphaSliderEl.value = String(this.a);
656
- this.alphaSliderEl.style.backgroundImage = `
657
- linear-gradient(45deg, var(--db-border-base) 25%, transparent 25%),
658
- linear-gradient(-45deg, var(--db-border-base) 25%, transparent 25%),
659
- linear-gradient(45deg, transparent 75%, var(--db-border-base) 75%),
660
- linear-gradient(-45deg, transparent 75%, var(--db-border-base) 75%),
661
- linear-gradient(to right, rgba(${rgb.r}, ${rgb.g}, ${rgb.b}, 0), rgba(${rgb.r}, ${rgb.g}, ${rgb.b}, 1))
662
- `;
663
- this.alphaSliderEl.style.backgroundSize = '8px 8px,8px 8px,8px 8px,8px 8px,100% 100%';
664
- this.alphaSliderEl.style.backgroundPosition = '0 0,0 4px,4px -4px,-4px 0,0 0';
665
- this.alphaSliderEl.style.backgroundColor = 'var(--db-surface-base)';
666
- }
667
- if (this.panelAlphaInputEl && !this.panelAlphaInputFocused) {
668
- this.setInputValue(this.panelAlphaInputEl, this.alphaInput);
669
- }
670
- if (this.formatSelectEl && this.formatSelectEl.getAttribute('value') !== this.format) {
671
- this.formatSelectEl.setAttribute('value', this.format);
672
- }
673
- this.colorInput = displayValue;
674
- }
675
- update() {
676
- if (!this.mounted)
677
- return;
678
- const size = this.getOption('size', SIZES, 'm');
679
- const type = this.getOption('type', TYPES, 'dropdown');
680
- const position = this.getOption('position', POSITIONS, 'bottom-start');
681
- const disabled = this.getBool('disabled');
682
- const readonly = this.getBool('readonly');
683
- const showInput = this.getBool('show-input', true);
684
- const showAlpha = this.getBool('show-alpha', true);
685
- const showFormatSelector = this.getBool('show-format-selector', true);
686
- const showCopyButton = this.getBool('show-copy-button', true);
687
- const showEyedropper = this.getBool('show-eyedropper', true);
688
- const eyedropperAvailable = typeof window !== 'undefined' && 'EyeDropper' in window;
689
- this.className = [
690
- 'designbase-wc-color-picker',
691
- `designbase-wc-color-picker--${size}`,
692
- `designbase-wc-color-picker--${position}`,
693
- this.open && type === 'dropdown' && 'designbase-wc-color-picker--open',
694
- this.getBool('full-width') && 'designbase-wc-color-picker--full-width',
695
- this.getBool('error') && 'designbase-wc-color-picker--error',
696
- disabled && 'designbase-wc-color-picker--disabled',
697
- readonly && 'designbase-wc-color-picker--readonly',
698
- !showInput && 'designbase-wc-color-picker--no-input',
699
- ]
700
- .filter(Boolean)
701
- .join(' ');
702
- if (this.triggerInputEl) {
703
- this.triggerInputEl.hidden = !showInput;
704
- this.triggerInputEl.disabled = disabled;
705
- this.triggerInputEl.readOnly = readonly;
706
- }
707
- if (this.triggerCopyBtn) {
708
- this.triggerCopyBtn.hidden = !(showInput && showCopyButton);
709
- this.triggerCopyBtn.disabled = disabled;
710
- }
711
- if (this.toggleBtn)
712
- this.toggleBtn.disabled = disabled;
713
- if (this.dropdownEl) {
714
- this.dropdownEl.className = `designbase-wc-color-picker__dropdown designbase-wc-color-picker__dropdown--${position}`;
715
- this.dropdownEl.hidden = !(type === 'dropdown' && this.open);
716
- }
717
- if (this.alphaSliderWrapEl)
718
- this.alphaSliderWrapEl.hidden = !showAlpha;
719
- if (this.panelAlphaWrapEl)
720
- this.panelAlphaWrapEl.hidden = !showAlpha;
721
- if (this.formatSelectEl)
722
- this.formatSelectEl.hidden = !showFormatSelector;
723
- if (this.panelCopyBtnEl)
724
- this.panelCopyBtnEl.hidden = !showCopyButton;
725
- if (this.eyedropperBtnEl)
726
- this.eyedropperBtnEl.hidden = !(showEyedropper && eyedropperAvailable);
727
- if (this.selectorEl) {
728
- const host = type === 'modal' ? this.modalBodyEl : this.dropdownEl;
729
- if (host && this.selectorEl.parentElement !== host) {
730
- host.append(this.selectorEl);
731
- }
732
- }
733
- if (this.modalEl) {
734
- if (type === 'modal' && this.open)
735
- this.modalEl.setAttribute('open', 'true');
736
- else
737
- this.modalEl.removeAttribute('open');
738
- }
739
- this.updateCopyButtons();
740
- this.updateVisuals();
741
- }
742
- }
743
- if (!customElements.get('db-color-picker'))
744
- customElements.define('db-color-picker', DbColorPicker);
745
-
746
- export { DbColorPicker };
1
+ import{hexToRgb as t,rgbToHsv as e,hsvToRgb as i,toHex as s,rgbToHsl as l,formatColor as n,normalizeHex as o,isHex as a,clamp as r}from"./color-picker-utils.js";import{getDbComponentIconSize as h,createDbIcon as d}from"./db-icons.js";const p=["s","m","l"],u=["dropdown","modal"],c=["bottom-start","bottom-end","top-start","top-end"],g="#006FFF",E=t(g)??{r:0,g:111,b:255},m=e(E.r,E.g,E.b),b=JSON.stringify([{label:"HEX",value:"hex"},{label:"RGB",value:"rgb"},{label:"RGBA",value:"rgba"},{label:"HSL",value:"hsl"},{label:"HSLA",value:"hsla"}]);class DbColorPicker extends HTMLElement{constructor(){super(...arguments),this.h=m.h,this.s=m.s,this.v=m.v,this.a=100,this.open=!1,this.format="hex",this.colorInput=g,this.alphaInput="100",this.isCopied=!1,this.tempColor="",this.dragging=null,this.mounted=!1,this.copiedTimer=null,this.debounceTimer=null,this.lastEmittedHex="",this.suppressNextValueSync=!1,this.didMount=!1,this.triggerInputFocused=!1,this.panelInputFocused=!1,this.panelAlphaInputFocused=!1,this.handleOutsideClick=t=>{if(!this.open||"dropdown"!==this.getOption("type",u,"dropdown"))return;const e=t.target;this.contains(e)||this.formatSelectEl?.contains(e)||(this.open=!1,this.update())},this.handleDocumentMouseMove=t=>{"area"===this.dragging&&(this.getBool("disabled")||this.getBool("readonly")||this.updateFromArea(t.clientX,t.clientY))},this.handleDocumentMouseUp=()=>{"area"===this.dragging&&(this.dragging=null,this.onColorStateChanged(!0))},this.handleModalCancel=()=>{const i=t(this.tempColor);if(i){const t=e(i.r,i.g,i.b);this.h=t.h,this.s=t.s,this.v=t.v}this.open=!1,this.update(),this.dispatchEvent(new CustomEvent("db-cancel",{bubbles:!0,composed:!0}))},this.handleModalApply=()=>{this.open=!1,this.actuallyEmit(),this.dispatchEvent(new CustomEvent("db-apply",{detail:{value:this.getOutward()},bubbles:!0,composed:!0})),this.update()}}static get observedAttributes(){return["size","type","position","value","default-value","show-input","show-alpha","show-format-selector","show-copy-button","show-eyedropper","disabled","readonly","full-width","error","on-change-format","change-debounce-ms","emit-during-drag"]}connectedCallback(){this.mounted||this.mount(),this.syncFromValueAttr(!0),this.update(),document.addEventListener("mousedown",this.handleOutsideClick),document.addEventListener("mousemove",this.handleDocumentMouseMove),document.addEventListener("mouseup",this.handleDocumentMouseUp)}disconnectedCallback(){document.removeEventListener("mousedown",this.handleOutsideClick),document.removeEventListener("mousemove",this.handleDocumentMouseMove),document.removeEventListener("mouseup",this.handleDocumentMouseUp),this.copiedTimer&&clearTimeout(this.copiedTimer),this.debounceTimer&&clearTimeout(this.debounceTimer)}attributeChangedCallback(t){this.mounted&&("value"!==t&&"default-value"!==t||this.syncFromValueAttr(!1),this.update())}getBool(t,e=!1){if(!this.hasAttribute(t))return e;const i=this.getAttribute(t);return""===i||"true"===i}getOption(t,e,i){const s=this.getAttribute(t);return s&&e.includes(s)?s:i}getIconSize(){return h(this.getOption("size",p,"m"))}getRgb(){return i(this.h,this.s,this.v)}getHex(){const t=this.getRgb();return s(t.r,t.g,t.b)}getHsl(){const t=this.getRgb();return l(t.r,t.g,t.b)}getUiFormatted(){const t=this.getRgb(),e=this.getHex(),i=this.getHsl();return n(this.format,t,e,i,this.a)}getOutward(){return"hex"===("ui"===this.getAttribute("on-change-format")?"ui":"hex")?this.getHex():this.getUiFormatted()}syncFromValueAttr(i){const s=this.getAttribute("value")??(i?this.getAttribute("default-value"):null)??(i?g:null);if(null==s)return;const l=o(s);if(!a(l))return;if(!i&&this.suppressNextValueSync&&l===this.lastEmittedHex)return void(this.suppressNextValueSync=!1);const n=t(l);if(!n)return;const r=e(n.r,n.g,n.b);this.h=r.h,this.s=r.s,this.v=r.v,this.colorInput=l,this.lastEmittedHex=l}actuallyEmit(){const t=o(this.getHex()),e=this.hasAttribute("value")&&a(this.getAttribute("value")||"")?o(this.getAttribute("value")||""):null;e&&e===t||this.lastEmittedHex!==t&&(this.lastEmittedHex=t,this.suppressNextValueSync=!0,this.setAttribute("value",t),this.dispatchEvent(new CustomEvent("db-change",{detail:{value:this.getOutward()},bubbles:!0,composed:!0})))}scheduleEmit(){const t=this.getBool("emit-during-drag",!0);if(this.dragging&&!t)return;const e=Number(this.getAttribute("change-debounce-ms")||"0");if(e>0)return this.debounceTimer&&clearTimeout(this.debounceTimer),void(this.debounceTimer=setTimeout(()=>this.actuallyEmit(),e));this.actuallyEmit()}onColorStateChanged(t=!1){this.didMount?(!t||this.getBool("emit-during-drag",!0),this.scheduleEmit()):this.didMount=!0}updateFromArea(t,e){const i=this.colorFieldEl;if(!i)return;const s=i.getBoundingClientRect(),l=r(t-s.left,0,s.width),n=r(e-s.top,0,s.height);this.s=l/s.width*100,this.v=100-n/s.height*100,this.updateVisuals(),this.onColorStateChanged()}getInputValue(t){return t?t.getAttribute("value")??"":""}setInputValue(t,e){t&&void 0!==e&&t.setAttribute("value",e)}getPanelControlSize(){return"s"===this.getOption("size",p,"m")?"s":"m"}commitColorInput(i){const s=("trigger"===i?this.triggerInputEl?.value:this.getInputValue(this.panelValueInputEl)??"").trim();if(/^#([0-9A-Fa-f]{6})$/.test(s)){const i=t(s),l=e(i.r,i.g,i.b);return this.h=l.h,this.s=l.s,this.v=l.v,this.colorInput=o(s),this.updateVisuals(),void this.onColorStateChanged()}let l=/^rgb\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*\)$/i.exec(s);if(l){const t=r(parseInt(l[1],10),0,255),i=r(parseInt(l[2],10),0,255),s=r(parseInt(l[3],10),0,255),n=e(t,i,s);return this.h=n.h,this.s=n.s,this.v=n.v,this.colorInput=`rgb(${t}, ${i}, ${s})`,this.updateVisuals(),void this.onColorStateChanged()}if(l=/^rgba\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(0|1|0?\.\d+)\s*\)$/i.exec(s),l){const t=r(parseInt(l[1],10),0,255),i=r(parseInt(l[2],10),0,255),s=r(parseInt(l[3],10),0,255),n=parseFloat(l[4]),o=e(t,i,s);this.h=o.h,this.s=o.s,this.v=o.v,this.a=Math.round(100*n),this.alphaInput=String(this.a),this.colorInput=`rgba(${t}, ${i}, ${s}, ${n})`,this.updateVisuals(),this.onColorStateChanged()}}commitAlphaInput(){const t=Number(this.getInputValue(this.panelAlphaInputEl).trim()||this.alphaInput);if(!Number.isNaN(t)&&t>=0&&t<=100){const e=Math.round(t);this.a=e,this.alphaInput=String(e),this.updateVisuals(),this.onColorStateChanged()}else this.alphaInput=String(this.a),this.panelAlphaInputEl&&this.setInputValue(this.panelAlphaInputEl,this.alphaInput)}async onCopy(){try{await navigator.clipboard.writeText(this.getUiFormatted()),this.isCopied=!0,this.updateCopyButtons(),this.copiedTimer&&clearTimeout(this.copiedTimer),this.copiedTimer=setTimeout(()=>{this.isCopied=!1,this.updateCopyButtons()},1600)}catch{}}async onEyedrop(){try{if("EyeDropper"in window){const i=new window.EyeDropper,{sRGBHex:s}=await i.open(),l=t(s);if(!l)return;const n=e(l.r,l.g,l.b);this.h=n.h,this.s=n.s,this.v=n.v,this.updateVisuals(),this.onColorStateChanged()}}catch{}}toggleOpen(){if(this.getBool("disabled")||this.getBool("readonly"))return;if("modal"===this.getOption("type",u,"dropdown"))return this.tempColor=this.getHex(),this.open=!0,void this.update();this.open=!this.open,this.update()}mount(){this.mounted=!0;const t=this.getIconSize();this.triggerEl=document.createElement("div"),this.triggerEl.className="designbase-wc-color-picker__trigger",this.triggerEl.addEventListener("click",()=>this.toggleOpen());const e=document.createElement("div");e.className="designbase-wc-color-picker__color-display",this.colorBoxEl=document.createElement("div"),this.colorBoxEl.className="designbase-wc-color-picker__color-box",e.append(this.colorBoxEl),this.triggerInputEl=document.createElement("input"),this.triggerInputEl.type="text",this.triggerInputEl.className="designbase-wc-color-picker__input",this.triggerInputEl.placeholder="#000000",this.triggerInputEl.addEventListener("click",t=>t.stopPropagation()),this.triggerInputEl.addEventListener("focus",()=>{this.triggerInputFocused=!0}),this.triggerInputEl.addEventListener("blur",()=>{this.triggerInputFocused=!1;const t="hex"===this.format?o(this.getUiFormatted()):this.getUiFormatted();this.triggerInputEl&&this.triggerInputEl.value!==t&&(this.triggerInputEl.value=t,this.colorInput=t)}),this.triggerInputEl.addEventListener("input",()=>{this.triggerInputEl&&(this.colorInput=this.triggerInputEl.value)}),this.triggerInputEl.addEventListener("keydown",t=>{"Enter"===t.key&&this.commitColorInput("trigger")});const i=document.createElement("div");i.className="designbase-wc-color-picker__trigger-actions",this.triggerCopyBtn=document.createElement("button"),this.triggerCopyBtn.type="button",this.triggerCopyBtn.className="designbase-wc-color-picker__copy-button-inline",this.triggerCopyBtn.setAttribute("aria-label","Copy color value"),this.triggerCopyBtn.addEventListener("click",t=>{t.stopPropagation(),this.onCopy()}),this.toggleBtn=document.createElement("button"),this.toggleBtn.type="button",this.toggleBtn.className="designbase-wc-color-picker__toggle",this.toggleBtn.setAttribute("aria-label","Toggle color picker"),this.toggleBtn.append(d("chevron-down",t)),i.append(this.triggerCopyBtn,this.toggleBtn),this.triggerEl.append(e,this.triggerInputEl,i),this.dropdownEl=document.createElement("div"),this.dropdownEl.className="designbase-wc-color-picker__dropdown",this.dropdownEl.addEventListener("click",t=>t.stopPropagation()),this.dropdownEl.hidden=!0;const s=document.createElement("div");s.className="designbase-wc-color-picker__selector";const l=document.createElement("div");l.className="designbase-wc-color-picker__color-area",this.colorFieldEl=document.createElement("div"),this.colorFieldEl.className="designbase-wc-color-picker__color-field",this.colorPointerEl=document.createElement("div"),this.colorPointerEl.className="designbase-wc-color-picker__color-pointer",this.colorFieldEl.append(this.colorPointerEl),this.colorFieldEl.addEventListener("mousedown",t=>{this.getBool("disabled")||this.getBool("readonly")||(t.preventDefault(),this.dragging="area",this.updateFromArea(t.clientX,t.clientY))}),this.colorFieldEl.addEventListener("touchstart",t=>{if(this.getBool("disabled")||this.getBool("readonly"))return;this.dragging="area";const e=t.touches[0];this.updateFromArea(e.clientX,e.clientY)},{passive:!0}),this.colorFieldEl.addEventListener("touchmove",t=>{if("area"!==this.dragging)return;const e=t.touches[0];this.updateFromArea(e.clientX,e.clientY)},{passive:!0}),this.colorFieldEl.addEventListener("touchend",()=>{"area"===this.dragging&&(this.dragging=null,this.onColorStateChanged(!0))}),l.append(this.colorFieldEl);const n=document.createElement("div");n.className="designbase-wc-color-picker__controls",this.eyedropperBtnEl=document.createElement("db-button"),this.eyedropperBtnEl.setAttribute("variant","tertiary"),this.eyedropperBtnEl.setAttribute("size",this.getPanelControlSize()),this.eyedropperBtnEl.setAttribute("icon-only",""),this.eyedropperBtnEl.setAttribute("start-icon","eyedropper"),this.eyedropperBtnEl.setAttribute("aria-label","Eyedropper tool"),this.eyedropperBtnEl.addEventListener("click",()=>{this.onEyedrop()});const a=document.createElement("div");a.className="designbase-wc-color-picker__slider-container";const h=document.createElement("div");h.className="designbase-wc-color-picker__hue-slider",this.hueSliderEl=document.createElement("input"),this.hueSliderEl.type="range",this.hueSliderEl.min="0",this.hueSliderEl.max="360",this.hueSliderEl.className="designbase-wc-color-picker__slider designbase-wc-color-picker__slider--hue",this.hueSliderEl.addEventListener("input",()=>{this.h=parseFloat(this.hueSliderEl.value),this.updateVisuals(),this.onColorStateChanged()}),h.append(this.hueSliderEl),this.alphaSliderWrapEl=document.createElement("div"),this.alphaSliderWrapEl.className="designbase-wc-color-picker__alpha-slider",this.alphaSliderEl=document.createElement("input"),this.alphaSliderEl.type="range",this.alphaSliderEl.min="0",this.alphaSliderEl.max="100",this.alphaSliderEl.className="designbase-wc-color-picker__slider designbase-wc-color-picker__slider--alpha",this.alphaSliderEl.addEventListener("input",()=>{this.a=r(parseInt(this.alphaSliderEl.value,10),0,100),this.alphaInput=String(this.a),this.panelAlphaInputEl&&this.setInputValue(this.panelAlphaInputEl,this.alphaInput),this.updateVisuals(),this.onColorStateChanged()}),this.alphaSliderWrapEl.append(this.alphaSliderEl),a.append(h,this.alphaSliderWrapEl),n.append(this.eyedropperBtnEl,a);const p=document.createElement("div");p.className="designbase-wc-color-picker__value-display",this.formatSelectEl=document.createElement("db-select"),this.formatSelectEl.setAttribute("size","s"),this.formatSelectEl.setAttribute("position","top"),this.formatSelectEl.setAttribute("show-clear-button","false"),this.formatSelectEl.setAttribute("options",b),this.formatSelectEl.addEventListener("db-change",t=>{const e=t.detail,i=e?.value;i&&(this.format=i,this.updateVisuals())}),this.panelValueInputEl=document.createElement("db-input"),this.panelValueInputEl.classList.add("designbase-wc-color-picker__value-input"),this.panelValueInputEl.setAttribute("size","s"),this.panelValueInputEl.setAttribute("placeholder","#000000"),this.panelValueInputEl.addEventListener("db-focus",()=>{this.panelInputFocused=!0}),this.panelValueInputEl.addEventListener("db-blur",()=>{this.panelInputFocused=!1;const t="hex"===this.format?o(this.getUiFormatted()):this.getUiFormatted();this.getInputValue(this.panelValueInputEl)!==t&&(this.setInputValue(this.panelValueInputEl,t),this.colorInput=t)}),this.panelValueInputEl.addEventListener("db-input",t=>{const e=t.detail;void 0!==e?.value&&(this.colorInput=e.value)}),this.panelValueInputEl.addEventListener("keydown",t=>{"Enter"===t.key&&this.commitColorInput("panel")}),this.panelAlphaWrapEl=document.createElement("div"),this.panelAlphaWrapEl.className="designbase-wc-color-picker__alpha-input-wrap",this.panelAlphaInputEl=document.createElement("db-input"),this.panelAlphaInputEl.classList.add("designbase-wc-color-picker__alpha-input"),this.panelAlphaInputEl.setAttribute("size","s"),this.panelAlphaInputEl.setAttribute("placeholder","100"),this.panelAlphaInputEl.setAttribute("aria-label","Alpha percent"),this.panelAlphaInputEl.addEventListener("db-focus",()=>{this.panelAlphaInputFocused=!0}),this.panelAlphaInputEl.addEventListener("db-blur",()=>{this.panelAlphaInputFocused=!1,this.commitAlphaInput()}),this.panelAlphaInputEl.addEventListener("db-input",t=>{const e=t.detail,i=(e?.value??"").replace(/[^\d]/g,"").slice(0,3);i!==e?.value&&this.setInputValue(this.panelAlphaInputEl,i),this.alphaInput=i}),this.panelAlphaInputEl.addEventListener("keydown",t=>{"Enter"===t.key&&this.commitAlphaInput()});const u=document.createElement("span");u.className="designbase-wc-color-picker__alpha-suffix",u.textContent="%",this.panelAlphaWrapEl.append(this.panelAlphaInputEl,u),this.panelCopyBtnEl=document.createElement("db-button"),this.panelCopyBtnEl.setAttribute("variant","tertiary"),this.panelCopyBtnEl.setAttribute("size","s"),this.panelCopyBtnEl.setAttribute("icon-only",""),this.panelCopyBtnEl.setAttribute("start-icon","copy"),this.panelCopyBtnEl.setAttribute("aria-label","Copy color value"),this.panelCopyBtnEl.addEventListener("click",()=>{this.onCopy()}),p.append(this.formatSelectEl,this.panelValueInputEl,this.panelAlphaWrapEl,this.panelCopyBtnEl),s.append(l,n,p),this.selectorEl=s,this.dropdownEl.append(s),this.modalEl=document.createElement("db-modal"),this.modalEl.setAttribute("size","s"),this.modalEl.setAttribute("title","색상 선택"),this.modalEl.setAttribute("close-on-escape","true"),this.modalEl.addEventListener("db-close",this.handleModalCancel),this.modalBodyEl=document.createElement("div"),this.modalBodyEl.setAttribute("data-modal-part","body");const c=document.createElement("div");c.setAttribute("data-modal-part","footer"),c.style.display="flex",c.style.gap="8px",c.style.justifyContent="flex-end";const g=document.createElement("db-button");g.setAttribute("variant","tertiary"),g.setAttribute("size","m"),g.textContent="취소",g.addEventListener("click",this.handleModalCancel);const E=document.createElement("db-button");E.setAttribute("variant","primary"),E.setAttribute("size","m"),E.textContent="적용",E.addEventListener("click",this.handleModalApply),c.append(g,E),this.modalEl.append(this.modalBodyEl,c),this.replaceChildren(this.triggerEl,this.dropdownEl,this.modalEl)}updateCopyButtons(){const t=this.getIconSize(),e=this.isCopied?"done":"copy";this.triggerCopyBtn&&this.triggerCopyBtn.replaceChildren(d(e,t)),this.panelCopyBtnEl&&this.panelCopyBtnEl.setAttribute("start-icon",e)}updateVisuals(){const t=this.getRgb(),e=this.getHex(),i=this.getBool("show-alpha",!0),s=this.getUiFormatted(),l="hex"===this.format?o(s):s;!this.triggerInputFocused&&this.triggerInputEl&&(this.triggerInputEl.value=l),!this.panelInputFocused&&this.panelValueInputEl&&this.setInputValue(this.panelValueInputEl,l),this.colorBoxEl&&(this.colorBoxEl.style.backgroundColor=i?`rgba(${t.r}, ${t.g}, ${t.b}, ${this.a/100})`:e),this.colorFieldEl&&(this.colorFieldEl.style.backgroundImage=`\n linear-gradient(to top, rgba(0,0,0,1), rgba(0,0,0,0)),\n linear-gradient(to right, #ffffff, hsl(${this.h}, 100%, 50%))\n `),this.colorPointerEl&&(this.colorPointerEl.style.left=`${this.s}%`,this.colorPointerEl.style.top=100-this.v+"%",this.colorPointerEl.style.backgroundColor=`rgb(${t.r}, ${t.g}, ${t.b})`),this.hueSliderEl&&(this.hueSliderEl.value=String(this.h),this.hueSliderEl.style.background="linear-gradient(to right,\n hsl(0,100%,50%),\n hsl(60,100%,50%),\n hsl(120,100%,50%),\n hsl(180,100%,50%),\n hsl(240,100%,50%),\n hsl(300,100%,50%),\n hsl(360,100%,50%))"),this.alphaSliderEl&&(this.alphaSliderEl.value=String(this.a),this.alphaSliderEl.style.backgroundImage=`\n linear-gradient(45deg, var(--db-border-base) 25%, transparent 25%),\n linear-gradient(-45deg, var(--db-border-base) 25%, transparent 25%),\n linear-gradient(45deg, transparent 75%, var(--db-border-base) 75%),\n linear-gradient(-45deg, transparent 75%, var(--db-border-base) 75%),\n linear-gradient(to right, rgba(${t.r}, ${t.g}, ${t.b}, 0), rgba(${t.r}, ${t.g}, ${t.b}, 1))\n `,this.alphaSliderEl.style.backgroundSize="8px 8px,8px 8px,8px 8px,8px 8px,100% 100%",this.alphaSliderEl.style.backgroundPosition="0 0,0 4px,4px -4px,-4px 0,0 0",this.alphaSliderEl.style.backgroundColor="var(--db-surface-base)"),this.panelAlphaInputEl&&!this.panelAlphaInputFocused&&this.setInputValue(this.panelAlphaInputEl,this.alphaInput),this.formatSelectEl&&this.formatSelectEl.getAttribute("value")!==this.format&&this.formatSelectEl.setAttribute("value",this.format),this.colorInput=l}update(){if(!this.mounted)return;const t=this.getOption("size",p,"m"),e=this.getOption("type",u,"dropdown"),i=this.getOption("position",c,"bottom-start"),s=this.getBool("disabled"),l=this.getBool("readonly"),n=this.getBool("show-input",!0),o=this.getBool("show-alpha",!0),a=this.getBool("show-format-selector",!0),r=this.getBool("show-copy-button",!0),h=this.getBool("show-eyedropper",!0),d="undefined"!=typeof window&&"EyeDropper"in window;if(this.className=["designbase-wc-color-picker",`designbase-wc-color-picker--${t}`,`designbase-wc-color-picker--${i}`,this.open&&"dropdown"===e&&"designbase-wc-color-picker--open",this.getBool("full-width")&&"designbase-wc-color-picker--full-width",this.getBool("error")&&"designbase-wc-color-picker--error",s&&"designbase-wc-color-picker--disabled",l&&"designbase-wc-color-picker--readonly",!n&&"designbase-wc-color-picker--no-input"].filter(Boolean).join(" "),this.triggerInputEl&&(this.triggerInputEl.hidden=!n,this.triggerInputEl.disabled=s,this.triggerInputEl.readOnly=l),this.triggerCopyBtn&&(this.triggerCopyBtn.hidden=!(n&&r),this.triggerCopyBtn.disabled=s),this.toggleBtn&&(this.toggleBtn.disabled=s),this.dropdownEl&&(this.dropdownEl.className=`designbase-wc-color-picker__dropdown designbase-wc-color-picker__dropdown--${i}`,this.dropdownEl.hidden=!("dropdown"===e&&this.open)),this.alphaSliderWrapEl&&(this.alphaSliderWrapEl.hidden=!o),this.panelAlphaWrapEl&&(this.panelAlphaWrapEl.hidden=!o),this.formatSelectEl&&(this.formatSelectEl.hidden=!a),this.panelCopyBtnEl&&(this.panelCopyBtnEl.hidden=!r),this.eyedropperBtnEl&&(this.eyedropperBtnEl.hidden=!(h&&d)),this.selectorEl){const t="modal"===e?this.modalBodyEl:this.dropdownEl;t&&this.selectorEl.parentElement!==t&&t.append(this.selectorEl)}this.modalEl&&("modal"===e&&this.open?this.modalEl.setAttribute("open","true"):this.modalEl.removeAttribute("open")),this.updateCopyButtons(),this.updateVisuals()}}customElements.get("db-color-picker")||customElements.define("db-color-picker",DbColorPicker);export{DbColorPicker};