ExamplesPlaygroundReference Source

coral-spectrum/coral-component-colorinput/src/scripts/ColorInputItem.js

  1. /**
  2. * Copyright 2019 Adobe. All rights reserved.
  3. * This file is licensed to you under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License. You may obtain a copy
  5. * of the License at http://www.apache.org/licenses/LICENSE-2.0
  6. *
  7. * Unless required by applicable law or agreed to in writing, software distributed under
  8. * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
  9. * OF ANY KIND, either express or implied. See the License for the specific language
  10. * governing permissions and limitations under the License.
  11. */
  12.  
  13. import {BaseComponent} from '../../../coral-base-component';
  14. import Color from './Color';
  15. import {transform} from '../../../coral-utils';
  16. import {Decorator} from '../../../coral-decorator';
  17.  
  18. /**
  19. @class Coral.ColorInput.Item
  20. @classdesc A ColorInput Item component
  21. @htmltag coral-colorinput-item
  22. @extends {HTMLElement}
  23. @extends {BaseComponent}
  24. */
  25. const ColorInputItem = Decorator(class extends BaseComponent(HTMLElement) {
  26. /**
  27. The value of the color. This value can be set in different formats (HEX, RGB, RGBA, HSB, HSL, HSLA and CMYK).
  28. Corrects a hex value, if it is represented by 3 or 6 characters with or without '#'.
  29.  
  30. e.g:
  31. HEX: #FFFFFF
  32. RGB: rgb(16,16,16)
  33. RGBA: rgba(215,40,40,0.9)
  34. HSB: hsb(360,100,100)
  35. HSL: hsl(360,100,100)
  36. HSLA: hsla(360,100%,100%, 0.9)
  37. CMYK: cmyk(0,100,50,0)
  38.  
  39. @type {String}
  40. @default ""
  41. @htmlattribute value
  42. @htmlattributereflected
  43. */
  44. get value() {
  45. return this._value || '';
  46. }
  47.  
  48. set value(value) {
  49. // invalid values fallback to empty string
  50. const color = new Color();
  51. color.value = value;
  52.  
  53. // invalid values fallback to empty string
  54. this._value = color.rgb !== null ? value : '';
  55. this._reflectAttribute('value', this._value);
  56. }
  57.  
  58. /**
  59. Whether the Item is selected.
  60. @type {Boolean}
  61. @default false
  62. @htmlattribute selected
  63. @htmlattributereflected
  64. */
  65. get selected() {
  66. return this._selected || false;
  67. }
  68.  
  69. set selected(value) {
  70. value = transform.booleanAttr(value);
  71.  
  72. this._selected = value;
  73. this._reflectAttribute('selected', this._selected);
  74.  
  75. this.classList.toggle('is-selected', this._selected);
  76. this.setAttribute('aria-selected', this._selected);
  77.  
  78. this.trigger('coral-colorinput-item:_selectedchanged');
  79. }
  80.  
  81. /** @ignore */
  82. static get observedAttributes() {
  83. return super.observedAttributes.concat(['selected', 'value']);
  84. }
  85.  
  86. /** @ignore */
  87. render() {
  88. super.render();
  89.  
  90. // adds the role to support accessibility
  91. this.setAttribute('role', 'option');
  92. }
  93. });
  94.  
  95. export default ColorInputItem;