@instructure/ui-color-picker 8.25.1-snapshot-20

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 (201) hide show
  1. package/CHANGELOG.md +17 -0
  2. package/README.md +46 -0
  3. package/es/ColorContrast/ColorContrastLocator.js +54 -0
  4. package/es/ColorContrast/index.js +174 -0
  5. package/es/ColorContrast/props.js +40 -0
  6. package/es/ColorContrast/styles.js +99 -0
  7. package/es/ColorContrast/theme.js +55 -0
  8. package/es/ColorIndicator/ColorIndicatorLocator.js +40 -0
  9. package/es/ColorIndicator/index.js +91 -0
  10. package/es/ColorIndicator/props.js +31 -0
  11. package/es/ColorIndicator/styles.js +70 -0
  12. package/es/ColorIndicator/theme.js +53 -0
  13. package/es/ColorMixer/ColorMixerLocator.js +27 -0
  14. package/es/ColorMixer/ColorPalette/index.js +258 -0
  15. package/es/ColorMixer/ColorPalette/props.js +37 -0
  16. package/es/ColorMixer/ColorPalette/styles.js +81 -0
  17. package/es/ColorMixer/ColorPalette/theme.js +48 -0
  18. package/es/ColorMixer/RGBAInput/index.js +148 -0
  19. package/es/ColorMixer/RGBAInput/props.js +39 -0
  20. package/es/ColorMixer/RGBAInput/styles.js +63 -0
  21. package/es/ColorMixer/RGBAInput/theme.js +43 -0
  22. package/es/ColorMixer/Slider/index.js +218 -0
  23. package/es/ColorMixer/Slider/props.js +38 -0
  24. package/es/ColorMixer/Slider/styles.js +95 -0
  25. package/es/ColorMixer/Slider/theme.js +53 -0
  26. package/es/ColorMixer/index.js +232 -0
  27. package/es/ColorMixer/props.js +40 -0
  28. package/es/ColorMixer/styles.js +49 -0
  29. package/es/ColorMixer/utils/shallowCompare.js +32 -0
  30. package/es/ColorPicker/ColorPickerLocator.js +137 -0
  31. package/es/ColorPicker/index.js +487 -0
  32. package/es/ColorPicker/props.js +47 -0
  33. package/es/ColorPicker/styles.js +119 -0
  34. package/es/ColorPicker/theme.js +76 -0
  35. package/es/ColorPreset/ColorPresetLocator.js +27 -0
  36. package/es/ColorPreset/index.js +268 -0
  37. package/es/ColorPreset/props.js +36 -0
  38. package/es/ColorPreset/styles.js +102 -0
  39. package/es/ColorPreset/theme.js +53 -0
  40. package/es/index.js +28 -0
  41. package/lib/ColorContrast/ColorContrastLocator.js +66 -0
  42. package/lib/ColorContrast/index.js +169 -0
  43. package/lib/ColorContrast/props.js +51 -0
  44. package/lib/ColorContrast/styles.js +107 -0
  45. package/lib/ColorContrast/theme.js +63 -0
  46. package/lib/ColorIndicator/ColorIndicatorLocator.js +51 -0
  47. package/lib/ColorIndicator/index.js +82 -0
  48. package/lib/ColorIndicator/props.js +42 -0
  49. package/lib/ColorIndicator/styles.js +78 -0
  50. package/lib/ColorIndicator/theme.js +63 -0
  51. package/lib/ColorMixer/ColorMixerLocator.js +37 -0
  52. package/lib/ColorMixer/ColorPalette/index.js +252 -0
  53. package/lib/ColorMixer/ColorPalette/props.js +48 -0
  54. package/lib/ColorMixer/ColorPalette/styles.js +90 -0
  55. package/lib/ColorMixer/ColorPalette/theme.js +57 -0
  56. package/lib/ColorMixer/RGBAInput/index.js +142 -0
  57. package/lib/ColorMixer/RGBAInput/props.js +50 -0
  58. package/lib/ColorMixer/RGBAInput/styles.js +71 -0
  59. package/lib/ColorMixer/RGBAInput/theme.js +51 -0
  60. package/lib/ColorMixer/Slider/index.js +211 -0
  61. package/lib/ColorMixer/Slider/props.js +49 -0
  62. package/lib/ColorMixer/Slider/styles.js +103 -0
  63. package/lib/ColorMixer/Slider/theme.js +62 -0
  64. package/lib/ColorMixer/index.js +226 -0
  65. package/lib/ColorMixer/props.js +51 -0
  66. package/lib/ColorMixer/styles.js +57 -0
  67. package/lib/ColorMixer/utils/shallowCompare.js +40 -0
  68. package/lib/ColorPicker/ColorPickerLocator.js +151 -0
  69. package/lib/ColorPicker/index.js +500 -0
  70. package/lib/ColorPicker/props.js +58 -0
  71. package/lib/ColorPicker/styles.js +127 -0
  72. package/lib/ColorPicker/theme.js +85 -0
  73. package/lib/ColorPreset/ColorPresetLocator.js +37 -0
  74. package/lib/ColorPreset/index.js +274 -0
  75. package/lib/ColorPreset/props.js +47 -0
  76. package/lib/ColorPreset/styles.js +110 -0
  77. package/lib/ColorPreset/theme.js +61 -0
  78. package/lib/index.js +45 -0
  79. package/lib/package.json +1 -0
  80. package/package.json +62 -0
  81. package/src/ColorContrast/ColorContrastLocator.ts +51 -0
  82. package/src/ColorContrast/README.md +111 -0
  83. package/src/ColorContrast/index.tsx +189 -0
  84. package/src/ColorContrast/props.ts +140 -0
  85. package/src/ColorContrast/styles.ts +102 -0
  86. package/src/ColorContrast/theme.ts +62 -0
  87. package/src/ColorIndicator/ColorIndicatorLocator.ts +41 -0
  88. package/src/ColorIndicator/README.md +76 -0
  89. package/src/ColorIndicator/index.tsx +89 -0
  90. package/src/ColorIndicator/props.ts +68 -0
  91. package/src/ColorIndicator/styles.ts +88 -0
  92. package/src/ColorIndicator/theme.ts +58 -0
  93. package/src/ColorMixer/ColorMixerLocator.ts +29 -0
  94. package/src/ColorMixer/ColorPalette/index.tsx +250 -0
  95. package/src/ColorMixer/ColorPalette/props.ts +87 -0
  96. package/src/ColorMixer/ColorPalette/styles.ts +94 -0
  97. package/src/ColorMixer/ColorPalette/theme.ts +52 -0
  98. package/src/ColorMixer/README.md +98 -0
  99. package/src/ColorMixer/RGBAInput/index.tsx +172 -0
  100. package/src/ColorMixer/RGBAInput/props.ts +93 -0
  101. package/src/ColorMixer/RGBAInput/styles.ts +69 -0
  102. package/src/ColorMixer/RGBAInput/theme.ts +48 -0
  103. package/src/ColorMixer/Slider/index.tsx +242 -0
  104. package/src/ColorMixer/Slider/props.ts +94 -0
  105. package/src/ColorMixer/Slider/styles.ts +114 -0
  106. package/src/ColorMixer/Slider/theme.ts +59 -0
  107. package/src/ColorMixer/index.tsx +221 -0
  108. package/src/ColorMixer/props.ts +169 -0
  109. package/src/ColorMixer/styles.ts +51 -0
  110. package/src/ColorMixer/utils/shallowCompare.ts +35 -0
  111. package/src/ColorPicker/ColorPickerLocator.ts +124 -0
  112. package/src/ColorPicker/README.md +420 -0
  113. package/src/ColorPicker/index.tsx +609 -0
  114. package/src/ColorPicker/props.ts +293 -0
  115. package/src/ColorPicker/styles.ts +131 -0
  116. package/src/ColorPicker/theme.ts +80 -0
  117. package/src/ColorPreset/ColorPresetLocator.ts +29 -0
  118. package/src/ColorPreset/README.md +121 -0
  119. package/src/ColorPreset/index.tsx +328 -0
  120. package/src/ColorPreset/props.ts +162 -0
  121. package/src/ColorPreset/styles.ts +107 -0
  122. package/src/ColorPreset/theme.ts +58 -0
  123. package/src/index.ts +34 -0
  124. package/tsconfig.build.json +36 -0
  125. package/tsconfig.build.tsbuildinfo +1 -0
  126. package/types/ColorContrast/ColorContrastLocator.d.ts +592 -0
  127. package/types/ColorContrast/ColorContrastLocator.d.ts.map +1 -0
  128. package/types/ColorContrast/index.d.ts +61 -0
  129. package/types/ColorContrast/index.d.ts.map +1 -0
  130. package/types/ColorContrast/props.d.ts +62 -0
  131. package/types/ColorContrast/props.d.ts.map +1 -0
  132. package/types/ColorContrast/styles.d.ts +76 -0
  133. package/types/ColorContrast/styles.d.ts.map +1 -0
  134. package/types/ColorContrast/theme.d.ts +10 -0
  135. package/types/ColorContrast/theme.d.ts.map +1 -0
  136. package/types/ColorIndicator/ColorIndicatorLocator.d.ts +577 -0
  137. package/types/ColorIndicator/ColorIndicatorLocator.d.ts.map +1 -0
  138. package/types/ColorIndicator/index.d.ts +35 -0
  139. package/types/ColorIndicator/index.d.ts.map +1 -0
  140. package/types/ColorIndicator/props.d.ts +25 -0
  141. package/types/ColorIndicator/props.d.ts.map +1 -0
  142. package/types/ColorIndicator/styles.d.ts +15 -0
  143. package/types/ColorIndicator/styles.d.ts.map +1 -0
  144. package/types/ColorIndicator/theme.d.ts +11 -0
  145. package/types/ColorIndicator/theme.d.ts.map +1 -0
  146. package/types/ColorMixer/ColorMixerLocator.d.ts +566 -0
  147. package/types/ColorMixer/ColorMixerLocator.d.ts.map +1 -0
  148. package/types/ColorMixer/ColorPalette/index.d.ts +70 -0
  149. package/types/ColorMixer/ColorPalette/index.d.ts.map +1 -0
  150. package/types/ColorMixer/ColorPalette/props.d.ts +29 -0
  151. package/types/ColorMixer/ColorPalette/props.d.ts.map +1 -0
  152. package/types/ColorMixer/ColorPalette/styles.d.ts +15 -0
  153. package/types/ColorMixer/ColorPalette/styles.d.ts.map +1 -0
  154. package/types/ColorMixer/ColorPalette/theme.d.ts +10 -0
  155. package/types/ColorMixer/ColorPalette/theme.d.ts.map +1 -0
  156. package/types/ColorMixer/RGBAInput/index.d.ts +51 -0
  157. package/types/ColorMixer/RGBAInput/index.d.ts.map +1 -0
  158. package/types/ColorMixer/RGBAInput/props.d.ts +28 -0
  159. package/types/ColorMixer/RGBAInput/props.d.ts.map +1 -0
  160. package/types/ColorMixer/RGBAInput/styles.d.ts +15 -0
  161. package/types/ColorMixer/RGBAInput/styles.d.ts.map +1 -0
  162. package/types/ColorMixer/RGBAInput/theme.d.ts +10 -0
  163. package/types/ColorMixer/RGBAInput/theme.d.ts.map +1 -0
  164. package/types/ColorMixer/Slider/index.d.ts +64 -0
  165. package/types/ColorMixer/Slider/index.d.ts.map +1 -0
  166. package/types/ColorMixer/Slider/props.d.ts +26 -0
  167. package/types/ColorMixer/Slider/props.d.ts.map +1 -0
  168. package/types/ColorMixer/Slider/styles.d.ts +15 -0
  169. package/types/ColorMixer/Slider/styles.d.ts.map +1 -0
  170. package/types/ColorMixer/Slider/theme.d.ts +10 -0
  171. package/types/ColorMixer/Slider/theme.d.ts.map +1 -0
  172. package/types/ColorMixer/index.d.ts +59 -0
  173. package/types/ColorMixer/index.d.ts.map +1 -0
  174. package/types/ColorMixer/props.d.ts +91 -0
  175. package/types/ColorMixer/props.d.ts.map +1 -0
  176. package/types/ColorMixer/styles.d.ts +14 -0
  177. package/types/ColorMixer/styles.d.ts.map +1 -0
  178. package/types/ColorMixer/utils/shallowCompare.d.ts +3 -0
  179. package/types/ColorMixer/utils/shallowCompare.d.ts.map +1 -0
  180. package/types/ColorPicker/ColorPickerLocator.d.ts +4113 -0
  181. package/types/ColorPicker/ColorPickerLocator.d.ts.map +1 -0
  182. package/types/ColorPicker/index.d.ts +158 -0
  183. package/types/ColorPicker/index.d.ts.map +1 -0
  184. package/types/ColorPicker/props.d.ts +166 -0
  185. package/types/ColorPicker/props.d.ts.map +1 -0
  186. package/types/ColorPicker/styles.d.ts +17 -0
  187. package/types/ColorPicker/styles.d.ts.map +1 -0
  188. package/types/ColorPicker/theme.d.ts +10 -0
  189. package/types/ColorPicker/theme.d.ts.map +1 -0
  190. package/types/ColorPreset/ColorPresetLocator.d.ts +566 -0
  191. package/types/ColorPreset/ColorPresetLocator.d.ts.map +1 -0
  192. package/types/ColorPreset/index.d.ts +100 -0
  193. package/types/ColorPreset/index.d.ts.map +1 -0
  194. package/types/ColorPreset/props.d.ts +87 -0
  195. package/types/ColorPreset/props.d.ts.map +1 -0
  196. package/types/ColorPreset/styles.d.ts +77 -0
  197. package/types/ColorPreset/styles.d.ts.map +1 -0
  198. package/types/ColorPreset/theme.d.ts +10 -0
  199. package/types/ColorPreset/theme.d.ts.map +1 -0
  200. package/types/index.d.ts +11 -0
  201. package/types/index.d.ts.map +1 -0
@@ -0,0 +1,609 @@
1
+ /*
2
+ * The MIT License (MIT)
3
+ *
4
+ * Copyright (c) 2015 - present Instructure, Inc.
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in all
14
+ * copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ * SOFTWARE.
23
+ */
24
+
25
+ /** @jsx jsx */
26
+ /** @jsxFrag React.Fragment */
27
+ import React, { Component } from 'react'
28
+
29
+ import { withStyle, jsx } from '@instructure/emotion'
30
+ import { warn } from '@instructure/console'
31
+ import { passthroughProps } from '@instructure/ui-react-utils'
32
+ import { testable } from '@instructure/ui-testable'
33
+ import {
34
+ colorToHex8,
35
+ isValid,
36
+ contrast as getContrast
37
+ } from '@instructure/ui-color-utils'
38
+
39
+ import { TextInput } from '@instructure/ui-text-input'
40
+ import { Tooltip } from '@instructure/ui-tooltip'
41
+ import { Button, IconButton } from '@instructure/ui-buttons'
42
+ import { Popover } from '@instructure/ui-popover'
43
+ import {
44
+ IconCheckDarkLine,
45
+ IconWarningLine,
46
+ IconTroubleLine,
47
+ IconInfoLine
48
+ } from '@instructure/ui-icons'
49
+
50
+ import ColorIndicator from '../ColorIndicator'
51
+ import ColorMixer from '../ColorMixer'
52
+ import ColorContrast from '../ColorContrast'
53
+ import ColorPreset from '../ColorPreset'
54
+
55
+ import generateStyle from './styles'
56
+ import generateComponentTheme from './theme'
57
+
58
+ import { propTypes, allowedProps } from './props'
59
+ import type {
60
+ ColorPickerProps,
61
+ ColorPickerState,
62
+ ContrastStrength,
63
+ MessageType
64
+ } from './props'
65
+
66
+ const acceptedCharactersForHEX = [
67
+ '0',
68
+ '1',
69
+ '2',
70
+ '3',
71
+ '4',
72
+ '5',
73
+ '6',
74
+ '7',
75
+ '8',
76
+ '9',
77
+ 'a',
78
+ 'A',
79
+ 'b',
80
+ 'B',
81
+ 'c',
82
+ 'C',
83
+ 'd',
84
+ 'D',
85
+ 'e',
86
+ 'E',
87
+ 'f',
88
+ 'F',
89
+ null
90
+ ]
91
+ /**
92
+ ---
93
+ category: components
94
+ ---
95
+ @tsProps
96
+ **/
97
+ @withStyle(generateStyle, generateComponentTheme)
98
+ @testable()
99
+ class ColorPicker extends Component<ColorPickerProps, ColorPickerState> {
100
+ static propTypes = propTypes
101
+ static allowedProps = allowedProps
102
+ static readonly componentId = 'ColorPicker'
103
+
104
+ static defaultProps = {
105
+ elementRef: () => null,
106
+ disabled: false,
107
+ label: 'Color',
108
+ checkContrast: false,
109
+ width: '22.5rem',
110
+ withAlpha: false
111
+ }
112
+
113
+ constructor(props: ColorPickerProps) {
114
+ super(props)
115
+
116
+ this.state = {
117
+ hexCode: '',
118
+ showHelperErrorMessages: false,
119
+ openColorPicker: false,
120
+ mixedColor: ''
121
+ }
122
+ }
123
+
124
+ ref: Element | null = null
125
+
126
+ handleRef = (el: Element | null) => {
127
+ const { elementRef } = this.props
128
+
129
+ this.ref = el
130
+
131
+ if (typeof elementRef === 'function') {
132
+ elementRef(el)
133
+ }
134
+ }
135
+
136
+ componentDidMount() {
137
+ this.props.makeStyles?.({ ...this.state, isSimple: this.isSimple })
138
+ this.checkSettings()
139
+
140
+ this.props.value && this.setState({ hexCode: this.props.value?.slice(1) })
141
+ }
142
+
143
+ componentDidUpdate(prevProps: ColorPickerProps) {
144
+ this.props.makeStyles?.({ ...this.state, isSimple: this.isSimple })
145
+
146
+ if (prevProps.value !== this.props.value) {
147
+ this.setState({
148
+ showHelperErrorMessages: false,
149
+ hexCode: this.props.value?.slice(1) || ''
150
+ })
151
+ }
152
+ this.checkSettings()
153
+ }
154
+
155
+ checkSettings = () => {
156
+ if (this.props.children && this.props.colorMixerSettings) {
157
+ warn(
158
+ false,
159
+ 'You should either use children, colorMixerSettings or neither, not both. In this case, the colorMixerSettings will be ignored',
160
+ ''
161
+ )
162
+ }
163
+ }
164
+
165
+ get renderMode() {
166
+ if (this.props.children) {
167
+ return 'customPopover'
168
+ }
169
+ if (this.props.colorMixerSettings) {
170
+ return 'defaultPopover'
171
+ }
172
+ return 'simple'
173
+ }
174
+
175
+ get isSimple() {
176
+ return this.renderMode === 'simple'
177
+ }
178
+ get isDefaultPopover() {
179
+ return this.renderMode === 'defaultPopover'
180
+ }
181
+ get isCustomPopover() {
182
+ return this.renderMode === 'customPopover'
183
+ }
184
+
185
+ getMinContrast(strength: ContrastStrength) {
186
+ return { min: 3, mid: 4.5, max: 7 }[strength]
187
+ }
188
+
189
+ renderMessages() {
190
+ const { hexCode, showHelperErrorMessages } = this.state
191
+
192
+ const isValidHex = isValid(hexCode)
193
+ const {
194
+ checkContrast,
195
+ renderMessages,
196
+ renderInvalidColorMessage,
197
+ renderIsRequiredMessage,
198
+ isRequired
199
+ } = this.props
200
+ const contrast = isValidHex
201
+ ? getContrast(
202
+ this.props.checkContrast?.contrastAgainst || '#fff',
203
+ hexCode,
204
+ 2
205
+ )
206
+ : undefined
207
+ const contrastStrength = checkContrast?.contrastStrength
208
+ ? checkContrast.contrastStrength
209
+ : 'mid'
210
+
211
+ const minContrast = this.getMinContrast(contrastStrength)
212
+ let invalidColorMessages: MessageType = []
213
+ let isRequiredMessages: MessageType = []
214
+ let generalMessages: MessageType = []
215
+ let contrastMessages: MessageType = []
216
+
217
+ if (checkContrast && contrast) {
218
+ const { renderContrastSuccessMessage, renderContrastErrorMessage } =
219
+ checkContrast
220
+
221
+ if (contrast < minContrast) {
222
+ contrastMessages =
223
+ typeof renderContrastErrorMessage === 'function'
224
+ ? renderContrastErrorMessage(contrast, minContrast)
225
+ : checkContrast.isStrict
226
+ ? [{ type: 'error', text: '' }]
227
+ : []
228
+ } else if (typeof renderContrastSuccessMessage === 'function') {
229
+ contrastMessages = renderContrastSuccessMessage(contrast, minContrast)
230
+ }
231
+ }
232
+ if (
233
+ showHelperErrorMessages &&
234
+ hexCode !== '' &&
235
+ !isValidHex &&
236
+ typeof renderInvalidColorMessage === 'function'
237
+ ) {
238
+ invalidColorMessages = renderInvalidColorMessage(hexCode)
239
+ }
240
+
241
+ if (isRequired && showHelperErrorMessages && hexCode === '') {
242
+ isRequiredMessages =
243
+ typeof renderIsRequiredMessage === 'function'
244
+ ? renderIsRequiredMessage()
245
+ : [{ type: 'error', text: '*' }]
246
+ }
247
+ if (typeof renderMessages === 'function') {
248
+ generalMessages = renderMessages(
249
+ hexCode,
250
+ isValidHex,
251
+ minContrast,
252
+ contrast
253
+ )
254
+ }
255
+
256
+ return [
257
+ ...invalidColorMessages,
258
+ ...isRequiredMessages,
259
+ ...generalMessages,
260
+ ...contrastMessages
261
+ ]
262
+ }
263
+ renderBeforeInput() {
264
+ const { styles } = this.props
265
+ return (
266
+ <div css={styles?.simpleColorContainer}>
267
+ {this.isSimple && <ColorIndicator color={`#${this.state.hexCode}`} />}
268
+ <div css={styles?.hashMarkContainer}>#</div>
269
+ </div>
270
+ )
271
+ }
272
+ renderAfterInput() {
273
+ const { checkContrast, styles } = this.props
274
+ const { hexCode } = this.state
275
+
276
+ if (checkContrast && isValid(hexCode)) {
277
+ const contrast = getContrast(
278
+ checkContrast.contrastAgainst || '#fff',
279
+ hexCode,
280
+ 2
281
+ )
282
+ const minContrast = this.getMinContrast(
283
+ checkContrast.contrastStrength ? checkContrast.contrastStrength : 'mid'
284
+ )
285
+
286
+ if (minContrast >= contrast) {
287
+ return (
288
+ <div css={styles?.errorIcons}>
289
+ {checkContrast.isStrict ? <IconTroubleLine /> : <IconWarningLine />}
290
+ </div>
291
+ )
292
+ }
293
+ return (
294
+ <div css={styles?.successIcon}>
295
+ <IconCheckDarkLine />
296
+ </div>
297
+ )
298
+ }
299
+ return null
300
+ }
301
+
302
+ handleOnChange(_event: React.ChangeEvent<HTMLInputElement>, value: string) {
303
+ const { onChange } = this.props
304
+ if (
305
+ value.length > (this.props.withAlpha ? 8 : 6) ||
306
+ value
307
+ .split('')
308
+ .find((char) => !acceptedCharactersForHEX.includes(char)) !== undefined
309
+ ) {
310
+ return
311
+ }
312
+ if (typeof onChange === 'function') {
313
+ onChange(`#${value}`)
314
+ } else {
315
+ this.setState({
316
+ showHelperErrorMessages: false,
317
+ hexCode: value
318
+ })
319
+ }
320
+ }
321
+
322
+ handleOnPaste(event: React.ClipboardEvent<unknown>) {
323
+ const pasted = event.clipboardData.getData('Text')
324
+ let toPaste = pasted
325
+ if (pasted[0] && pasted[0] === '#') {
326
+ toPaste = pasted.slice(1)
327
+ }
328
+
329
+ if (
330
+ (this.props.withAlpha && toPaste.length > 8) ||
331
+ (!this.props.withAlpha && toPaste.length > 6)
332
+ ) {
333
+ return event.preventDefault()
334
+ }
335
+
336
+ const newHex = `${this.state.hexCode}${toPaste}`
337
+ if (isValid(newHex)) {
338
+ if (typeof this.props.onChange === 'function') {
339
+ this.props.onChange(`#${newHex}`)
340
+ } else {
341
+ this.setState({
342
+ showHelperErrorMessages: false,
343
+ hexCode: newHex
344
+ })
345
+ }
346
+ }
347
+
348
+ event.preventDefault()
349
+ }
350
+
351
+ handleOnBlur() {
352
+ this.setState({ showHelperErrorMessages: true })
353
+ }
354
+
355
+ renderLabel() {
356
+ const { label, tooltip, styles } = this.props
357
+
358
+ return tooltip ? (
359
+ <div>
360
+ <span css={styles?.label}>{label}</span>
361
+ <span>
362
+ <Tooltip renderTip={tooltip}>
363
+ <IconInfoLine tabIndex={-1} />
364
+ </Tooltip>
365
+ </span>
366
+ </div>
367
+ ) : (
368
+ label
369
+ )
370
+ }
371
+
372
+ stripAlphaIfNeeded = (hex: string) =>
373
+ hex.length === 8 && hex.slice(-2) === 'FF' ? hex.slice(0, -2) : hex
374
+
375
+ renderPopover = () => (
376
+ <Popover
377
+ renderTrigger={
378
+ <IconButton
379
+ disabled={this.props.disabled}
380
+ screenReaderLabel={this.props.popoverButtonScreenReaderLabel || ''}
381
+ >
382
+ <ColorIndicator color={`#${this.state.hexCode}`} />
383
+ </IconButton>
384
+ }
385
+ isShowingContent={this.state.openColorPicker}
386
+ onShowContent={() => {
387
+ this.setState({ openColorPicker: true, mixedColor: this.state.hexCode })
388
+ }}
389
+ onHideContent={() => {
390
+ this.setState({ openColorPicker: false })
391
+ }}
392
+ on="click"
393
+ screenReaderLabel={this.props.popoverScreenReaderLabel || ''}
394
+ shouldContainFocus
395
+ shouldReturnFocus
396
+ shouldCloseOnDocumentClick
397
+ offsetY="10rem"
398
+ >
399
+ <div css={this.props.styles?.popoverContentContainer}>
400
+ {this.isDefaultPopover
401
+ ? this.renderDefaultPopoverContent()
402
+ : this.renderCustomPopoverContent()}
403
+ </div>
404
+ </Popover>
405
+ )
406
+
407
+ renderCustomPopoverContent = () => (
408
+ <div css={this.props.styles?.popoverContent}>
409
+ {typeof this.props?.children === 'function' &&
410
+ this.props?.children(
411
+ this.stripAlphaIfNeeded(this.state.mixedColor),
412
+ (color: string) => {
413
+ this.setState({ mixedColor: color })
414
+ },
415
+ () => {
416
+ this.setState({
417
+ openColorPicker: false,
418
+ hexCode: `${this.stripAlphaIfNeeded(this.state.mixedColor)}`
419
+ })
420
+ this.props?.onChange?.(
421
+ `#${this.stripAlphaIfNeeded(this.state.mixedColor.slice(1))}`
422
+ )
423
+ },
424
+ () =>
425
+ this.setState({
426
+ openColorPicker: false,
427
+ mixedColor: this.state.hexCode
428
+ })
429
+ )}
430
+ </div>
431
+ )
432
+ renderDefaultPopoverContent = () => (
433
+ <>
434
+ <div css={this.props.styles?.popoverContent}>
435
+ {this.props?.colorMixerSettings?.colorMixer && (
436
+ <ColorMixer
437
+ value={`#${this.state.mixedColor}`}
438
+ onChange={(newColor: string) =>
439
+ this.setState({ mixedColor: colorToHex8(newColor).slice(1) })
440
+ }
441
+ withAlpha={this.props.colorMixerSettings.colorMixer.withAlpha}
442
+ rgbRedInputScreenReaderLabel={
443
+ this.props.colorMixerSettings.colorMixer
444
+ .rgbRedInputScreenReaderLabel
445
+ }
446
+ rgbGreenInputScreenReaderLabel={
447
+ this.props.colorMixerSettings.colorMixer
448
+ .rgbGreenInputScreenReaderLabel
449
+ }
450
+ rgbBlueInputScreenReaderLabel={
451
+ this.props.colorMixerSettings.colorMixer
452
+ .rgbBlueInputScreenReaderLabel
453
+ }
454
+ rgbAlphaInputScreenReaderLabel={
455
+ this.props.colorMixerSettings.colorMixer
456
+ .rgbAlphaInputScreenReaderLabel
457
+ }
458
+ colorSliderNavigationExplanationScreenReaderLabel={
459
+ this.props.colorMixerSettings!.colorMixer
460
+ .colorSliderNavigationExplanationScreenReaderLabel
461
+ }
462
+ alphaSliderNavigationExplanationScreenReaderLabel={
463
+ this.props.colorMixerSettings!.colorMixer
464
+ .alphaSliderNavigationExplanationScreenReaderLabel
465
+ }
466
+ colorPaletteNavigationExplanationScreenReaderLabel={
467
+ this.props.colorMixerSettings!.colorMixer
468
+ .colorPaletteNavigationExplanationScreenReaderLabel
469
+ }
470
+ />
471
+ )}
472
+ {this.props?.colorMixerSettings?.colorPreset && (
473
+ <div css={this.props.styles?.popoverContentBlock}>
474
+ <ColorPreset
475
+ label={this.props.colorMixerSettings.colorPreset.label}
476
+ colors={this.props.colorMixerSettings.colorPreset.colors}
477
+ selected={this.state.mixedColor}
478
+ onSelect={(color: string) =>
479
+ this.setState({ mixedColor: color.slice(1) })
480
+ }
481
+ />
482
+ </div>
483
+ )}
484
+ {this.props?.colorMixerSettings?.colorContrast && (
485
+ <div css={this.props.styles?.popoverContentBlock}>
486
+ <ColorContrast
487
+ firstColor={
488
+ this.props.colorMixerSettings.colorContrast.firstColor
489
+ }
490
+ secondColor={`#${this.stripAlphaIfNeeded(this.state.mixedColor)}`}
491
+ label={this.props.colorMixerSettings.colorContrast.label}
492
+ successLabel={
493
+ this.props.colorMixerSettings.colorContrast.successLabel
494
+ }
495
+ failureLabel={
496
+ this.props.colorMixerSettings.colorContrast.failureLabel
497
+ }
498
+ normalTextLabel={
499
+ this.props.colorMixerSettings.colorContrast.normalTextLabel
500
+ }
501
+ largeTextLabel={
502
+ this.props.colorMixerSettings.colorContrast.largeTextLabel
503
+ }
504
+ graphicsTextLabel={
505
+ this.props.colorMixerSettings.colorContrast.graphicsTextLabel
506
+ }
507
+ firstColorLabel={
508
+ this.props.colorMixerSettings.colorContrast.firstColorLabel
509
+ }
510
+ secondColorLabel={
511
+ this.props.colorMixerSettings.colorContrast.secondColorLabel
512
+ }
513
+ />
514
+ </div>
515
+ )}
516
+ </div>
517
+ <div css={this.props.styles?.popoverFooter}>
518
+ <Button
519
+ onClick={() =>
520
+ this.setState({
521
+ openColorPicker: false,
522
+ mixedColor: this.state.hexCode
523
+ })
524
+ }
525
+ color="secondary"
526
+ margin="xx-small"
527
+ >
528
+ {this.props.colorMixerSettings?.popoverCloseButtonLabel}
529
+ </Button>
530
+ <Button
531
+ onClick={() => {
532
+ if (typeof this.props.onChange === 'function') {
533
+ this.props.onChange(
534
+ `#${this.stripAlphaIfNeeded(this.state.mixedColor)}`
535
+ )
536
+ this.setState({
537
+ openColorPicker: false
538
+ })
539
+ } else {
540
+ this.setState({
541
+ openColorPicker: false,
542
+ hexCode: `${this.stripAlphaIfNeeded(this.state.mixedColor)}`
543
+ })
544
+ }
545
+ }}
546
+ color="primary"
547
+ margin="xx-small"
548
+ >
549
+ {this.props.colorMixerSettings?.popoverAddButtonLabel}
550
+ </Button>
551
+ </div>
552
+ </>
553
+ )
554
+ render() {
555
+ const {
556
+ checkContrast,
557
+ colorMixerSettings,
558
+ disabled,
559
+ elementRef,
560
+ isRequired,
561
+ label,
562
+ onChange,
563
+ placeholderText,
564
+ popoverButtonScreenReaderLabel,
565
+ renderInvalidColorMessage,
566
+ renderIsRequiredMessage,
567
+ renderMessages,
568
+ tooltip,
569
+ value,
570
+ width,
571
+ withAlpha,
572
+ ...props
573
+ } = this.props
574
+ return (
575
+ <div
576
+ {...passthroughProps(props)}
577
+ css={this.props.styles?.colorPicker}
578
+ ref={this.handleRef}
579
+ >
580
+ <TextInput
581
+ isRequired={isRequired}
582
+ disabled={disabled}
583
+ renderLabel={() => this.renderLabel()}
584
+ display="inline-block"
585
+ width={width}
586
+ placeholder={placeholderText}
587
+ themeOverride={{ padding: '' }}
588
+ renderAfterInput={this.renderAfterInput()}
589
+ renderBeforeInput={this.renderBeforeInput()}
590
+ value={this.state.hexCode}
591
+ onChange={(event, value) => this.handleOnChange(event, value)}
592
+ onPaste={(event) => this.handleOnPaste(event)}
593
+ onBlur={() => this.handleOnBlur()}
594
+ messages={this.renderMessages()}
595
+ />
596
+ {!this.isSimple && (
597
+ <div css={this.props.styles?.colorMixerButtonContainer}>
598
+ <div css={this.props.styles?.colorMixerButtonWrapper}>
599
+ {this.renderPopover()}
600
+ </div>
601
+ </div>
602
+ )}
603
+ </div>
604
+ )
605
+ }
606
+ }
607
+
608
+ export { ColorPicker }
609
+ export default ColorPicker