Skip to content

Commit 4bdfa18

Browse files
committed
Color Input:
- Adds support for native color picket in modern browsers Now if the user click on the color preview area the color picker will apepar. See #78208
1 parent 6a0cbf8 commit 4bdfa18

2 files changed

Lines changed: 60 additions & 9 deletions

File tree

src/elements/Input/ColorInput.coffee

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,36 @@ class CUI.ColorInput extends CUI.Input
1414
@opts.leftControlElement = new CUI.Button
1515
tabindex: -1
1616
onClick: (ev, btn) =>
17-
@__input.focus()
17+
if @isDisabled()
18+
return
19+
@__colorPicker?.click()
1820
super()
1921

22+
render: ->
23+
super()
24+
@__colorPicker = CUI.dom.$element("input", "cui-color-picker-native",
25+
type: "color"
26+
tabindex: "-1"
27+
)
28+
29+
CUI.Events.listen
30+
node: @__colorPicker
31+
type: "input"
32+
call: =>
33+
@__input.value = @__colorPicker.value
34+
@__toggleColor()
35+
36+
CUI.Events.listen
37+
node: @__colorPicker
38+
type: "change"
39+
call: =>
40+
@__input.value = @__colorPicker.value
41+
@__toggleColor()
42+
new CUI.Event(type: "input", node: @__input).dispatch()
43+
44+
CUI.dom.append(@DOM, @__colorPicker)
45+
@
46+
2047
onDataChanged: (ev, info) ->
2148
super(ev, info)
2249
@__toggleColor()
@@ -28,12 +55,19 @@ class CUI.ColorInput extends CUI.Input
2855
else
2956
@_leftControlElement.DOM.style.setProperty("--btn-background", @__data[@_name])
3057
@_leftControlElement.removeClass('is-empty')
58+
@__syncColorPicker(@__data[@_name])
59+
60+
__syncColorPicker: (value) ->
61+
if not @__colorPicker
62+
return
63+
if value and /^#[0-9a-fA-F]{6}$/.test(value)
64+
@__colorPicker.value = value
3165

3266
__toggleColor: ->
3367
if @__input.value.length > 0 and @__checkInputInternal()
34-
# Set the background color of the @_leftControlElement to the value of the input
3568
@_leftControlElement.DOM.style.setProperty("--btn-background", @__input.value)
3669
@_leftControlElement.removeClass('is-empty')
70+
@__syncColorPicker(@__input.value)
3771
else
38-
@_leftControlElement.DOM.style.removeProperty("--btn-background");
72+
@_leftControlElement.DOM.style.removeProperty("--btn-background")
3973
@_leftControlElement.addClass('is-empty')

src/scss/themes/fylr/components/_input.scss

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -280,8 +280,6 @@ textarea {
280280
.cui-color-input,
281281
.cui-icon-input {
282282
> .cui-button {
283-
cursor: default;
284-
285283
> .cui-button-visual {
286284
--btn-border-width: 0;
287285
--btn-background-hover: var(--btn-background);
@@ -294,16 +292,35 @@ textarea {
294292

295293
// Color Input
296294
.cui-color-input {
297-
> .cui-button.is-empty > .cui-button-visual {
298-
--btn-background: transparent;
295+
position: relative;
296+
297+
> .cui-button {
298+
cursor: pointer;
299+
300+
&.is-empty > .cui-button-visual {
301+
--btn-background: transparent;
299302

300-
background: linear-gradient(135deg, $gray-200 0, $gray-200 50%, $gray-300 50.01%, $gray-300);
303+
background: linear-gradient(135deg, $gray-200 0, $gray-200 50%, $gray-300 50.01%, $gray-300);
304+
}
305+
}
306+
307+
> .cui-color-picker-native {
308+
position: absolute;
309+
left: 0;
310+
top: 100%;
311+
width: 0;
312+
height: 0;
313+
opacity: 0;
314+
overflow: hidden;
315+
pointer-events: none;
301316
}
302317
}
303318

304-
// Icon input
319+
// Icon input
305320
.cui-icon-input {
306321
> .cui-button {
322+
cursor: default;
323+
307324
> .cui-button-visual {
308325
--btn-background: #{$input-background};
309326

0 commit comments

Comments
 (0)