@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,250 @@
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 copyrigfht 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
+ import React, { Component } from 'react'
27
+
28
+ import { withStyle, jsx } from '@instructure/emotion'
29
+ import { addEventListener } from '@instructure/ui-dom-utils'
30
+ import shallowCompare from '../utils/shallowCompare'
31
+
32
+ import { View } from '@instructure/ui-view'
33
+ import type { ViewOwnProps } from '@instructure/ui-view'
34
+
35
+ import { propTypes, allowedProps } from './props'
36
+ import type { ColorPaletteProps, ColorPaletteState } from './props'
37
+ import type { HSVType } from '../props'
38
+
39
+ import generateStyle from './styles'
40
+ import generateComponentTheme from './theme'
41
+
42
+ /**
43
+ * ---
44
+ * private: true
45
+ * ---
46
+ * @tsProps
47
+ **/
48
+ @withStyle(generateStyle, generateComponentTheme)
49
+ class ColorPalette extends Component<ColorPaletteProps, ColorPaletteState> {
50
+ static propTypes = propTypes
51
+ static allowedProps = allowedProps
52
+ static readonly componentId = 'ColorMixer.Palette'
53
+
54
+ constructor(props: ColorPaletteProps) {
55
+ super(props)
56
+ this.state = {
57
+ colorPosition: { x: 0, y: 0 }
58
+ }
59
+ }
60
+
61
+ ref: Element | null = null
62
+ private _paletteRef: HTMLDivElement | null = null
63
+ private _mouseMoveListener?: { remove(): void }
64
+ private _mouseUpListener?: { remove(): void }
65
+
66
+ handleRef = (el: Element | null) => {
67
+ const { elementRef } = this.props
68
+
69
+ this.ref = el
70
+
71
+ if (typeof elementRef === 'function') {
72
+ elementRef(el)
73
+ }
74
+ }
75
+
76
+ componentDidMount() {
77
+ this.props.makeStyles?.(this.state)
78
+ this.setState({
79
+ colorPosition: this.calcPositionFromColor(this.props.color)
80
+ })
81
+ }
82
+
83
+ componentDidUpdate(prevProps: ColorPaletteProps) {
84
+ this.props.makeStyles?.(this.state)
85
+
86
+ if (shallowCompare(prevProps.color, this.props.color)) {
87
+ this.setState({
88
+ colorPosition: this.calcPositionFromColor(this.props.color)
89
+ })
90
+ }
91
+ }
92
+ componentWillUnmount() {
93
+ this.removeEventListeners()
94
+ }
95
+
96
+ get paletteWidth() {
97
+ return this._paletteRef!.getBoundingClientRect().width
98
+ }
99
+
100
+ get paletteHeight() {
101
+ return this._paletteRef!.getBoundingClientRect().height
102
+ }
103
+ calcSaturation = (position: number) =>
104
+ Math.round((position / this.paletteWidth) * 100) / 100
105
+ calcLuminance = (position: number) =>
106
+ Math.round(((this.paletteHeight - position) / this.paletteHeight) * 100) /
107
+ 100
108
+
109
+ calcPositionFromColor(hsv: HSVType) {
110
+ const { s, v } = hsv
111
+ return {
112
+ x: s * this.paletteWidth,
113
+ y: this.paletteHeight - v * this.paletteHeight
114
+ }
115
+ }
116
+
117
+ handlePaletteMouseDown(e: React.MouseEvent<ViewOwnProps, MouseEvent>) {
118
+ this.handleChange(e)
119
+
120
+ this._mouseMoveListener = addEventListener(
121
+ window,
122
+ 'mousemove',
123
+ this.handleChange
124
+ )
125
+ this._mouseUpListener = addEventListener(
126
+ window,
127
+ 'mouseup',
128
+ this.handleMouseUp
129
+ )
130
+ }
131
+
132
+ handleMouseUp = () => {
133
+ this.removeEventListeners()
134
+ }
135
+
136
+ removeEventListeners() {
137
+ this._mouseMoveListener?.remove()
138
+ this._mouseUpListener?.remove()
139
+ }
140
+
141
+ calcColorPosition(clientX: number, clientY: number) {
142
+ const { x, y } = this._paletteRef!.getBoundingClientRect()
143
+
144
+ return this.applyBoundaries(clientX - x, clientY - y)
145
+ }
146
+
147
+ applyBoundaries(x: number, y: number) {
148
+ let newXPosition = x
149
+ let newYPosition = y
150
+ if (x > this.paletteWidth) {
151
+ newXPosition = this.paletteWidth
152
+ }
153
+ if (x < 0) {
154
+ newXPosition = 0
155
+ }
156
+ if (y > this.paletteHeight) {
157
+ newYPosition = this.paletteHeight
158
+ }
159
+ if (y < 0) {
160
+ newYPosition = 0
161
+ }
162
+ return { newXPosition, newYPosition }
163
+ }
164
+
165
+ handleChange = (e: React.MouseEvent<ViewOwnProps, MouseEvent>) => {
166
+ if (this.props.disabled) return
167
+ const { clientX, clientY } = e
168
+ const { newXPosition, newYPosition } = this.calcColorPosition(
169
+ clientX,
170
+ clientY
171
+ )
172
+ this.setState({
173
+ colorPosition: { x: newXPosition, y: newYPosition }
174
+ })
175
+
176
+ this.props.onChange({
177
+ h: this.props.hue,
178
+ s: this.calcSaturation(newXPosition),
179
+ v: this.calcLuminance(newYPosition)
180
+ })
181
+ }
182
+
183
+ handleKeyDown(e: React.KeyboardEvent<ViewOwnProps>) {
184
+ if (this.props.disabled) return
185
+ const { key } = e
186
+ if (key === 'Tab') return
187
+ e.preventDefault()
188
+ let deltaX = 0
189
+ let deltaY = 0
190
+ if (key === 'ArrowLeft' || key === 'a') {
191
+ deltaX = -2
192
+ }
193
+ if (key === 'ArrowRight' || key === 'd') {
194
+ deltaX = 2
195
+ }
196
+ if (key === 'ArrowUp' || key === 'w') {
197
+ deltaY = -2
198
+ }
199
+ if (key === 'ArrowDown' || key === 's') {
200
+ deltaY = 2
201
+ }
202
+
203
+ const { newXPosition, newYPosition } = this.applyBoundaries(
204
+ this.state.colorPosition.x + deltaX,
205
+ this.state.colorPosition.y + deltaY
206
+ )
207
+
208
+ this.setState({
209
+ colorPosition: { x: newXPosition, y: newYPosition }
210
+ })
211
+ this.props.onChange({
212
+ h: this.props.hue,
213
+ s: this.calcSaturation(newXPosition),
214
+ v: this.calcLuminance(newYPosition)
215
+ })
216
+ }
217
+
218
+ render() {
219
+ return (
220
+ <View
221
+ elementRef={this.handleRef}
222
+ disabled={this.props.disabled}
223
+ position="relative"
224
+ background="transparent"
225
+ display="inline-block"
226
+ borderRadius="medium"
227
+ borderWidth="0"
228
+ padding="0"
229
+ as="div"
230
+ tabIndex={this.props.disabled ? undefined : 0}
231
+ onKeyDown={(e) => this.handleKeyDown(e)}
232
+ onMouseDown={(e) => this.handlePaletteMouseDown(e)}
233
+ aria-label={this.props.navigationExplanationScreenReaderLabel}
234
+ >
235
+ <div css={this.props.styles?.indicator} />
236
+ {this.props.disabled && (
237
+ <div css={this.props.styles?.disabledOverlay} />
238
+ )}
239
+ <div
240
+ css={this.props.styles?.palette}
241
+ ref={(ref) => {
242
+ this._paletteRef = ref
243
+ }}
244
+ />
245
+ </View>
246
+ )
247
+ }
248
+ }
249
+
250
+ export default ColorPalette
@@ -0,0 +1,87 @@
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
+ import PropTypes from 'prop-types'
26
+ import type { WithStyleProps, ComponentStyle } from '@instructure/emotion'
27
+
28
+ import type {
29
+ OtherHTMLAttributes,
30
+ PropValidators,
31
+ ColorMixerPaletteTheme
32
+ } from '@instructure/shared-types'
33
+ import type { HSVType } from '../props'
34
+
35
+ type ColorPaletteOwnProps = {
36
+ disabled?: boolean
37
+ hue: number
38
+ color: HSVType
39
+ width: number
40
+ height: number
41
+ indicatorRadius: number
42
+ onChange: (rgb: HSVType) => void
43
+ elementRef?: (element: Element | null) => void
44
+ navigationExplanationScreenReaderLabel: string
45
+ }
46
+
47
+ type ColorPaletteState = {
48
+ colorPosition: { x: number; y: number }
49
+ }
50
+
51
+ type PropKeys = keyof ColorPaletteOwnProps
52
+
53
+ type AllowedPropKeys = Readonly<Array<PropKeys>>
54
+
55
+ type ColorPaletteProps = ColorPaletteOwnProps &
56
+ WithStyleProps<ColorMixerPaletteTheme, ColorPaletteStyle> &
57
+ OtherHTMLAttributes<ColorPaletteOwnProps>
58
+
59
+ type ColorPaletteStyle = ComponentStyle<
60
+ 'ColorPalette' | 'indicator' | 'palette' | 'disabledOverlay'
61
+ >
62
+ const propTypes: PropValidators<PropKeys> = {
63
+ disabled: PropTypes.bool,
64
+ hue: PropTypes.number.isRequired,
65
+ color: PropTypes.object.isRequired,
66
+ width: PropTypes.number.isRequired,
67
+ height: PropTypes.number.isRequired,
68
+ indicatorRadius: PropTypes.number.isRequired,
69
+ onChange: PropTypes.func.isRequired,
70
+ elementRef: PropTypes.func,
71
+ navigationExplanationScreenReaderLabel: PropTypes.string.isRequired
72
+ }
73
+
74
+ const allowedProps: AllowedPropKeys = [
75
+ 'disabled',
76
+ 'hue',
77
+ 'color',
78
+ 'width',
79
+ 'height',
80
+ 'indicatorRadius',
81
+ 'onChange',
82
+ 'elementRef',
83
+ 'navigationExplanationScreenReaderLabel'
84
+ ]
85
+
86
+ export type { ColorPaletteProps, ColorPaletteState, ColorPaletteStyle }
87
+ export { propTypes, allowedProps }
@@ -0,0 +1,94 @@
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
+ import type {
26
+ ColorPaletteStyle,
27
+ ColorPaletteProps,
28
+ ColorPaletteState
29
+ } from './props'
30
+ import type { ColorMixerPaletteTheme } from '@instructure/shared-types'
31
+ import { px } from '@instructure/ui-utils'
32
+
33
+ /**
34
+ * ---
35
+ * private: true
36
+ * ---
37
+ * Generates the style object from the theme and provided additional information
38
+ * @param {Object} componentTheme The theme variable object.
39
+ * @param {Object} props the props of the component, the style is applied to
40
+ * @param {Object} state the state of the component, the style is applied to
41
+ * @return {Object} The final style object, which will be used in the component
42
+ */
43
+ const generateStyle = (
44
+ componentTheme: ColorMixerPaletteTheme,
45
+ props: ColorPaletteProps,
46
+ state: ColorPaletteState
47
+ ): ColorPaletteStyle => {
48
+ return {
49
+ ColorPalette: {
50
+ label: 'ColorPalette'
51
+ },
52
+ indicator: {
53
+ label: 'ColorPalette__indicator',
54
+ width: `${props.indicatorRadius / 8}rem`,
55
+ height: `${props.indicatorRadius / 8}rem`,
56
+ borderRadius: `${props.indicatorRadius / 8}rem`,
57
+ background: componentTheme.whiteColor,
58
+ position: 'absolute',
59
+ borderStyle: 'solid',
60
+ borderWidth: componentTheme.indicatorBorderWidth,
61
+ borderColor: componentTheme.indicatorBorderColor,
62
+ top: `${
63
+ state?.colorPosition?.y - px(`${props.indicatorRadius / 16}rem`)
64
+ }px`,
65
+ left: `${
66
+ state?.colorPosition?.x - px(`${props.indicatorRadius / 16}rem`)
67
+ }px`
68
+ },
69
+ palette: {
70
+ label: 'ColorPalette__palette',
71
+ width: `${props.width / 16}rem`,
72
+ height: `${props.height / 16}rem`,
73
+ borderRadius: componentTheme.paletteBorderRadius,
74
+ borderStyle: 'solid',
75
+ borderWidth: componentTheme.paletteBorderWidth,
76
+ boxSizing: 'border-box',
77
+ borderColor: componentTheme.colorIndicatorBorderColor,
78
+ background: `linear-gradient(to bottom, rgba(0,0,0,0), rgba(0,0,0,1)),
79
+ linear-gradient(to right, white, hsl(${props.hue},100%,50%))`
80
+ },
81
+ disabledOverlay: {
82
+ label: 'ColorPalette__disabledOverlay',
83
+ background: 'rgba(255,255,255,.5)',
84
+ zIndex: componentTheme.disabledOverlayZIndex,
85
+ width: `${props.width / 16 + 1}rem`,
86
+ height: `${props.height / 16 + 1}rem`,
87
+ position: 'absolute',
88
+ top: '-.5rem',
89
+ left: '-.5rem'
90
+ }
91
+ }
92
+ }
93
+
94
+ export default generateStyle
@@ -0,0 +1,52 @@
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
+ import type { Theme } from '@instructure/ui-themes'
26
+ import type { ColorMixerPaletteTheme } from '@instructure/shared-types'
27
+ import { colorIndicatorBorderColor } from '../../ColorIndicator/theme'
28
+
29
+ /**
30
+ * Generates the theme object for the component from the theme and provided additional information
31
+ * @param {Object} theme The actual theme object.
32
+ * @return {Object} The final theme object with the overrides and component variables
33
+ */
34
+ const generateComponentTheme = (theme: Theme): ColorMixerPaletteTheme => {
35
+ const { colors, borders, stacking } = theme
36
+
37
+ const componentVariables = {
38
+ indicatorBorderColor: colors.licorice,
39
+ whiteColor: colors.white,
40
+ colorIndicatorBorderColor,
41
+ indicatorBorderWidth: borders.widthSmall,
42
+ paletteBorderRadius: borders.radiusMedium,
43
+ paletteBorderWidth: borders.widthSmall,
44
+ disabledOverlayZIndex: stacking.topmost
45
+ }
46
+
47
+ return {
48
+ ...componentVariables
49
+ }
50
+ }
51
+
52
+ export default generateComponentTheme
@@ -0,0 +1,98 @@
1
+ ---
2
+ describes: ColorMixer
3
+ ---
4
+
5
+ A component for selecting a color. It lets pick from a palette manually or type in an RGBA color.
6
+
7
+ ### Color Mixer
8
+
9
+ ```js
10
+ ---
11
+ render: false
12
+ example: true
13
+ ---
14
+ class Example extends React.Component {
15
+ constructor(props) {
16
+ super(props);
17
+ this.state = {
18
+ value: "#328DCFC2",
19
+ };
20
+ }
21
+
22
+ render() {
23
+ return (
24
+ <div>
25
+ <div style={{ display: "flex" }}>
26
+ <ColorMixer
27
+ withAlpha
28
+ value={this.state.value}
29
+ onChange={(value) => this.setState({ value })}
30
+ rgbRedInputScreenReaderLabel='Input field for red'
31
+ rgbGreenInputScreenReaderLabel='Input field for green'
32
+ rgbBlueInputScreenReaderLabel='Input field for blue'
33
+ rgbAlphaInputScreenReaderLabel='Input field for alpha'
34
+ colorSliderNavigationExplanationScreenReaderLabel={`You are on a color slider. To navigate the slider left or right, use the 'A' and 'D' buttons respectively`}
35
+ alphaSliderNavigationExplanationScreenReaderLabel={`You are on an alpha slider. To navigate the slider left or right, use the 'A' and 'D' buttons respectively`}
36
+ colorPaletteNavigationExplanationScreenReaderLabel={`You are on a color palette. To navigate on the palette up, left, down or right, use the 'W', 'A', 'S' and 'D' buttons respectively`}
37
+ />
38
+ <div
39
+ style={{
40
+ width: "100px",
41
+ height: "160px",
42
+ borderRadius: "4px",
43
+ backgroundColor: this.state.value,
44
+ marginLeft: "25px",
45
+ boxSizing: "border-box",
46
+ borderStyle: "solid",
47
+ borderColor: "rgba(56, 74, 88, 0.6)",
48
+ }}
49
+ >
50
+ {this.state.value}
51
+ </div>
52
+ </div>
53
+ <div style={{ marginTop: "10px" }}>
54
+ <Button onClick={() => this.setState({ value: "#579b17" })}>
55
+ Jump to #579b17
56
+ </Button>
57
+ </div>
58
+ </div>
59
+ );
60
+ }
61
+ }
62
+
63
+ render(<Example />);
64
+
65
+
66
+ ```
67
+
68
+ ### Disabled Color Mixer
69
+
70
+ ```js
71
+ ---
72
+ render: false
73
+ example: true
74
+ ---
75
+ class Example extends React.Component {
76
+ render() {
77
+ return (
78
+ <ColorMixer
79
+ disabled
80
+ withAlpha
81
+ value="#328DCFC2"
82
+ onChange={() => {}}
83
+ rgbRedInputScreenReaderLabel='Input field for red'
84
+ rgbGreenInputScreenReaderLabel='Input field for green'
85
+ rgbBlueInputScreenReaderLabel='Input field for blue'
86
+ rgbAlphaInputScreenReaderLabel='Input field for alpha'
87
+ colorSliderNavigationExplanationScreenReaderLabel={`You are on a color slider. To navigate the slider left or right, use the 'A' and 'D' buttons respectively`}
88
+ alphaSliderNavigationExplanationScreenReaderLabel={`You are on an alpha slider. To navigate the slider left or right, use the 'A' and 'D' buttons respectively`}
89
+ colorPaletteNavigationExplanationScreenReaderLabel={`You are on a color palette. To navigate on the palette up, left, down or right, use the 'W', 'A', 'S' and 'D' buttons respectively`}
90
+ />
91
+ );
92
+ }
93
+ }
94
+
95
+ render(<Example />);
96
+
97
+
98
+ ```