|
| 1 | +import { LitElement, css, html } from 'lit'; |
| 2 | +import { classMap } from 'lit/directives/class-map.js'; |
| 3 | + |
| 4 | +/** |
| 5 | + * A tile component that can be used to display a range selection state. |
| 6 | + * |
| 7 | + * This component is specifically designed for cc-range-selector and is not meant to be used standalone. |
| 8 | + * It is a presentational component that displays visual states based on properties set by its parent. |
| 9 | + * All interactive behaviors (click handling, keyboard navigation, etc.) are managed by the parent cc-range-selector component. |
| 10 | + * |
| 11 | + * @cssdisplay inline-flex |
| 12 | + * |
| 13 | + * @slot - Content displayed as the main part of the tile. This slot should contain the option's label or visual content and should not be empty. |
| 14 | + */ |
| 15 | +export class CcRangeSelectorOption extends LitElement { |
| 16 | + static get properties() { |
| 17 | + return { |
| 18 | + disabled: { type: Boolean, reflect: true }, |
| 19 | + dragging: { type: Boolean, reflect: true }, |
| 20 | + error: { type: Boolean, reflect: true }, |
| 21 | + pointer: { type: Boolean, reflect: true }, |
| 22 | + readonly: { type: Boolean, reflect: true }, |
| 23 | + selected: { type: Boolean, reflect: true }, |
| 24 | + }; |
| 25 | + } |
| 26 | + |
| 27 | + constructor() { |
| 28 | + super(); |
| 29 | + |
| 30 | + /** @type {boolean} Whether the component should be disabled (default: 'false') */ |
| 31 | + this.disabled = false; |
| 32 | + |
| 33 | + /** @type {boolean} Whether the option is within a drag selection range (default: 'false') */ |
| 34 | + this.dragging = false; |
| 35 | + |
| 36 | + /** @type {boolean} Whether the component should be in error mode when not disabled nor readonly (default: 'false') */ |
| 37 | + this.error = false; |
| 38 | + |
| 39 | + /** @type {boolean} Whether to show a pointer cursor for interactive states (default: 'false') */ |
| 40 | + this.pointer = false; |
| 41 | + |
| 42 | + /** @type {boolean} Whether the component should be readonly when not disabled (default: 'false') */ |
| 43 | + this.readonly = false; |
| 44 | + |
| 45 | + /** @type {boolean} Whether the option is currently selected when not dragging (default: 'false') */ |
| 46 | + this.selected = false; |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Renders the option with appropriate visual states based on its properties. |
| 51 | + * Calculates CSS classes for disabled, readonly, error, dragging, selected, and pointer states. |
| 52 | + * @return {import('lit').TemplateResult} |
| 53 | + */ |
| 54 | + render() { |
| 55 | + // Calculate CSS classes based on component state |
| 56 | + // State priority: disabled > readonly > error > dragging > selected |
| 57 | + // Note: dragging state takes visual priority over selected state during drag operations |
| 58 | + const classes = { |
| 59 | + disabled: this.disabled, |
| 60 | + readonly: !this.disabled && this.readonly, |
| 61 | + error: !this.disabled && !this.readonly && this.error, |
| 62 | + dragging: this.dragging, |
| 63 | + selected: !this.dragging && this.selected, // Selected styling is hidden during dragging |
| 64 | + }; |
| 65 | + |
| 66 | + return html` |
| 67 | + <div class="wrapper ${classMap(classes)}"> |
| 68 | + <slot></slot> |
| 69 | + </div> |
| 70 | + `; |
| 71 | + } |
| 72 | + |
| 73 | + static get styles() { |
| 74 | + return [ |
| 75 | + // language=CSS |
| 76 | + css` |
| 77 | + /* region global */ |
| 78 | + :host { |
| 79 | + --cc-icon-size: 1.25em; |
| 80 | +
|
| 81 | + border-radius: var(--cc-border-radius-default, 0.25em); |
| 82 | + display: inline-flex; |
| 83 | + overflow: hidden; |
| 84 | + width: fit-content; |
| 85 | + } |
| 86 | +
|
| 87 | + .wrapper { |
| 88 | + align-items: stretch; |
| 89 | + display: flex; |
| 90 | + flex: 1 1 auto; |
| 91 | + line-height: 1.5; |
| 92 | + } |
| 93 | + /* endregion */ |
| 94 | +
|
| 95 | + /* region body section */ |
| 96 | + ::slotted(*) { |
| 97 | + background-color: var(--cc-color-bg-default, #fff); |
| 98 | + border: 0.125em solid var(--cc-color-border-neutral, #bfbfbf); |
| 99 | + color: var(--cc-color-text-default, #262626); |
| 100 | + display: inline-block; |
| 101 | + flex: 1 1 auto; |
| 102 | + padding: 0.25em 0.5em; |
| 103 | + } |
| 104 | + /* endregion */ |
| 105 | +
|
| 106 | + /* region common states */ |
| 107 | + .disabled ::slotted(*) { |
| 108 | + background-color: var(--cc-color-bg-neutral, #f5f5f5); |
| 109 | + border-color: var(--cc-color-bg-neutral, #f5f5f5); |
| 110 | + color: var(--cc-color-text-disabled, #595959); |
| 111 | + } |
| 112 | +
|
| 113 | + .readonly ::slotted(*) { |
| 114 | + background-color: var(--cc-color-bg-neutral-active, #d9d9d9); |
| 115 | + border-color: var(--cc-color-bg-neutral-active, #d9d9d9); |
| 116 | + } |
| 117 | +
|
| 118 | + .selected ::slotted(*) { |
| 119 | + background-color: var(--cc-color-bg-primary, #3569aa); |
| 120 | + border-color: var(--cc-color-bg-primary, #3569aa); |
| 121 | + color: var(--cc-color-text-inverted, #fff); |
| 122 | + } |
| 123 | +
|
| 124 | + .dragging ::slotted(*) { |
| 125 | + background-color: var(--cc-color-bg-primary-weaker, #e6eff8); |
| 126 | + border-color: var(--cc-color-bg-primary, #3569aa); |
| 127 | + border-style: dotted; |
| 128 | + color: var(--cc-color-text-primary-strong, #002c9d); |
| 129 | + user-select: none; |
| 130 | + } |
| 131 | +
|
| 132 | + .error ::slotted(*) { |
| 133 | + background-color: var(--cc-color-bg-danger-weaker, #ffe4e1); |
| 134 | + border-color: var(--cc-color-bg-danger-weaker, #ffe4e1); |
| 135 | + color: var(--cc-color-text-danger, #be242d); |
| 136 | + } |
| 137 | + /* endregion */ |
| 138 | +
|
| 139 | + /* region selected & disabled */ |
| 140 | + .selected.disabled ::slotted(*) { |
| 141 | + background-color: var(--color-grey-60, #737373); |
| 142 | + border-color: var(--color-grey-60, #737373); |
| 143 | + color: var(--cc-color-text-inverted, #fff); |
| 144 | + } |
| 145 | + /* endregion */ |
| 146 | +
|
| 147 | + /* region selected & readonly */ |
| 148 | + .selected.readonly ::slotted(*) { |
| 149 | + background-color: var(--cc-color-bg-primary-weak, #cedcff); |
| 150 | + border-color: var(--cc-color-bg-primary-weak, #cedcff); |
| 151 | + color: var(--cc-color-text-primary-strong, #002c9d); |
| 152 | + } |
| 153 | + /* endregion */ |
| 154 | +
|
| 155 | + /* region selected & error */ |
| 156 | + .selected.error ::slotted(*) { |
| 157 | + background-color: var(--cc-color-bg-danger, #be242d); |
| 158 | + border-color: var(--cc-color-bg-danger, #be242d); |
| 159 | + color: var(--cc-color-text-inverted, #fff); |
| 160 | + } |
| 161 | + /* endregion */ |
| 162 | +
|
| 163 | + /* region dragging & error */ |
| 164 | + .dragging.error ::slotted(*) { |
| 165 | + border-color: var(--cc-color-bg-danger, #be242d); |
| 166 | + } |
| 167 | + /* endregion */ |
| 168 | +
|
| 169 | + /* region hover */ |
| 170 | + /* Hover state only applies when option is in its default interactive state |
| 171 | + (not disabled, readonly, error, selected, or dragging) */ |
| 172 | + .wrapper:not(.selected, .dragging, .disabled, .readonly, .error) :hover::slotted(*) { |
| 173 | + border-color: var(--cc-color-border-neutral-hovered, #595959); |
| 174 | + } |
| 175 | +
|
| 176 | + .wrapper.error:not(.selected, .dragging) :hover::slotted(*) { |
| 177 | + background-color: var(--cc-color-bg-danger-weak, #fbc8c2); |
| 178 | + border-color: var(--cc-color-bg-danger-weak, #fbc8c2); |
| 179 | + } |
| 180 | + /* endregion */ |
| 181 | +
|
| 182 | + /* region pointer */ |
| 183 | + :host([pointer]) { |
| 184 | + cursor: pointer; |
| 185 | + } |
| 186 | + /* endregion */ |
| 187 | + `, |
| 188 | + ]; |
| 189 | + } |
| 190 | +} |
| 191 | + |
| 192 | +window.customElements.define('cc-range-selector-option', CcRangeSelectorOption); |
0 commit comments