|
| 1 | +import { LitElement, html, css, nothing } from 'lit'; |
| 2 | + |
| 3 | +/** |
| 4 | + * OLChip - A pill-shaped interactive chip web component |
| 5 | + * |
| 6 | + * Supports two sizes, a selected state with a checkmark icon, |
| 7 | + * click events, and optional link behavior via href. |
| 8 | + * |
| 9 | + * @property {Boolean} selected - Whether the chip is in a selected state |
| 10 | + * @property {String} size - Chip size: "small" or "medium" (default) |
| 11 | + * @property {String} href - When set, the chip renders as a link |
| 12 | + * @property {String} count - Optional count displayed to the right of the label |
| 13 | + * @property {String} accessibleLabel - Override aria-label on the inner interactive element |
| 14 | + * |
| 15 | + * @fires ol-chip-select - Fired on click. detail: { selected: Boolean } |
| 16 | + * |
| 17 | + * @example |
| 18 | + * <ol-chip>Fiction</ol-chip> |
| 19 | + * |
| 20 | + * @example |
| 21 | + * <ol-chip selected>History</ol-chip> |
| 22 | + * |
| 23 | + * @example |
| 24 | + * <ol-chip size="small" count="76" href="/subjects/fiction">Fiction</ol-chip> |
| 25 | + */ |
| 26 | +export class OLChip extends LitElement { |
| 27 | + static properties = { |
| 28 | + selected: { type: Boolean, reflect: true }, |
| 29 | + size: { type: String, reflect: true }, |
| 30 | + href: { type: String }, |
| 31 | + count: { type: String }, |
| 32 | + accessibleLabel: { type: String, attribute: 'accessible-label' }, |
| 33 | + }; |
| 34 | + |
| 35 | + static styles = css` |
| 36 | + :host { |
| 37 | + --chip-padding-block: 6px; |
| 38 | + --chip-padding-inline: 12px; |
| 39 | + --chip-icon-size: 14px; |
| 40 | + --chip-icon-gap: 4px; |
| 41 | + display: inline-block; |
| 42 | + } |
| 43 | +
|
| 44 | + :host([size="small"]) { |
| 45 | + --chip-padding-block: 4px; |
| 46 | + --chip-padding-inline: 8px; |
| 47 | + --chip-icon-size: 12px; |
| 48 | + } |
| 49 | +
|
| 50 | + .chip { |
| 51 | + position: relative; |
| 52 | + display: inline-flex; |
| 53 | + align-items: center; |
| 54 | + padding: var(--chip-padding-block) var(--chip-padding-inline); |
| 55 | + border: var(--border-width) solid var(--color-border-subtle); |
| 56 | + border-radius: var(--border-radius-pill); |
| 57 | + font-family: var(--font-family-button); |
| 58 | + font-size: var(--font-size-body-medium); |
| 59 | + line-height: var(--line-height-chip); |
| 60 | + background: var(--white); |
| 61 | + color: var(--dark-grey); |
| 62 | + cursor: pointer; |
| 63 | + user-select: none; |
| 64 | + text-decoration: none; |
| 65 | + } |
| 66 | +
|
| 67 | + @media (hover: hover) and (pointer: fine) { |
| 68 | + .chip:hover { |
| 69 | + background: var(--lightest-grey); |
| 70 | + } |
| 71 | + } |
| 72 | +
|
| 73 | + .chip:active { |
| 74 | + transform: scale(0.97); |
| 75 | + } |
| 76 | +
|
| 77 | + .chip:focus-visible { |
| 78 | + outline: none; |
| 79 | + box-shadow: var(--box-shadow-focus); |
| 80 | + } |
| 81 | +
|
| 82 | + /* Selected state */ |
| 83 | + :host([selected]) .chip { |
| 84 | + padding-inline-start: calc(var(--chip-padding-inline) + var(--chip-icon-size) + var(--chip-icon-gap)); |
| 85 | + background: var(--primary-blue); |
| 86 | + border-color: var(--primary-blue); |
| 87 | + color: var(--white); |
| 88 | + } |
| 89 | +
|
| 90 | + @media (hover: hover) and (pointer: fine) { |
| 91 | + :host([selected]) .chip:hover { |
| 92 | + background: var(--primary-blue); |
| 93 | + filter: brightness(1.1); |
| 94 | + } |
| 95 | + } |
| 96 | +
|
| 97 | + /* Small size */ |
| 98 | + :host([size="small"]) .chip { |
| 99 | + font-size: var(--font-size-label-medium); |
| 100 | + } |
| 101 | +
|
| 102 | + /* Icon carousel — clips overflow so icons slide in/out */ |
| 103 | + .icon-slot { |
| 104 | + position: absolute; |
| 105 | + inset-inline-start: var(--chip-padding-inline); |
| 106 | + top: 50%; |
| 107 | + transform: translateY(-50%); |
| 108 | + width: var(--chip-icon-size); |
| 109 | + height: var(--chip-icon-size); |
| 110 | + } |
| 111 | +
|
| 112 | + .icon-carousel { |
| 113 | + display: flex; |
| 114 | + flex-direction: column; |
| 115 | + transition: transform 0.2s cubic-bezier(.25, .46, .45, .94); |
| 116 | + } |
| 117 | +
|
| 118 | + @media (hover: hover) and (pointer: fine) { |
| 119 | + .chip:hover .icon-carousel { |
| 120 | + transform: translateY(-50%); |
| 121 | + } |
| 122 | + } |
| 123 | +
|
| 124 | + .icon { |
| 125 | + width: var(--chip-icon-size); |
| 126 | + height: var(--chip-icon-size); |
| 127 | + flex-shrink: 0; |
| 128 | + transition: opacity 0.2s cubic-bezier(.25, .46, .45, .94), |
| 129 | + filter 0.2s cubic-bezier(.25, .46, .45, .94); |
| 130 | + } |
| 131 | +
|
| 132 | + .icon-check { |
| 133 | + opacity: 1; |
| 134 | + filter: blur(0); |
| 135 | + } |
| 136 | +
|
| 137 | + .icon-close { |
| 138 | + opacity: 0; |
| 139 | + filter: blur(2px); |
| 140 | + } |
| 141 | +
|
| 142 | + @media (hover: hover) and (pointer: fine) { |
| 143 | + .chip:hover .icon-check { |
| 144 | + opacity: 0; |
| 145 | + filter: blur(2px); |
| 146 | + } |
| 147 | +
|
| 148 | + .chip:hover .icon-close { |
| 149 | + opacity: 1; |
| 150 | + filter: blur(0); |
| 151 | + } |
| 152 | + } |
| 153 | +
|
| 154 | + /* Count */ |
| 155 | + .count { |
| 156 | + margin-inline-start: 4px; |
| 157 | + color: #777; |
| 158 | + font-size: 0.85em; |
| 159 | + font-variant-numeric: tabular-nums; |
| 160 | + } |
| 161 | +
|
| 162 | + :host([selected]) .count { |
| 163 | + color: #c6e1f0; |
| 164 | + } |
| 165 | + `; |
| 166 | + |
| 167 | + constructor() { |
| 168 | + super(); |
| 169 | + this.selected = false; |
| 170 | + this.size = 'medium'; |
| 171 | + this.href = null; |
| 172 | + this.count = null; |
| 173 | + this.accessibleLabel = null; |
| 174 | + } |
| 175 | + |
| 176 | + _handleClick() { |
| 177 | + this.dispatchEvent(new CustomEvent('ol-chip-select', { |
| 178 | + bubbles: true, |
| 179 | + composed: true, |
| 180 | + detail: { selected: !this.selected }, |
| 181 | + })); |
| 182 | + } |
| 183 | + |
| 184 | + _renderIcons() { |
| 185 | + if (!this.selected) return nothing; |
| 186 | + |
| 187 | + return html` |
| 188 | + <span class="icon-slot"> |
| 189 | + <span class="icon-carousel"> |
| 190 | + <svg |
| 191 | + class="icon icon-check" |
| 192 | + aria-hidden="true" |
| 193 | + viewBox="0 0 24 24" |
| 194 | + fill="none" |
| 195 | + stroke="currentColor" |
| 196 | + stroke-width="3" |
| 197 | + stroke-linecap="round" |
| 198 | + stroke-linejoin="round" |
| 199 | + > |
| 200 | + <path d="M20 6 9 17l-5-5"/> |
| 201 | + </svg> |
| 202 | + <svg |
| 203 | + class="icon icon-close" |
| 204 | + aria-hidden="true" |
| 205 | + viewBox="0 0 24 24" |
| 206 | + fill="none" |
| 207 | + stroke="currentColor" |
| 208 | + stroke-width="3" |
| 209 | + stroke-linecap="round" |
| 210 | + stroke-linejoin="round" |
| 211 | + > |
| 212 | + <path d="M18 6 6 18"/><path d="m6 6 12 12"/> |
| 213 | + </svg> |
| 214 | + </span> |
| 215 | + </span> |
| 216 | + `; |
| 217 | + } |
| 218 | + |
| 219 | + _renderCount() { |
| 220 | + if (this.count === null) return nothing; |
| 221 | + |
| 222 | + return html`<span class="count">${this.count}</span>`; |
| 223 | + } |
| 224 | + |
| 225 | + render() { |
| 226 | + const content = html` |
| 227 | + ${this._renderIcons()} |
| 228 | + <slot></slot> |
| 229 | + ${this._renderCount()} |
| 230 | + `; |
| 231 | + |
| 232 | + if (this.href) { |
| 233 | + return html` |
| 234 | + <a class="chip" href=${this.href} |
| 235 | + aria-label=${this.accessibleLabel || nothing} |
| 236 | + @click=${this._handleClick}> |
| 237 | + ${content} |
| 238 | + </a> |
| 239 | + `; |
| 240 | + } |
| 241 | + |
| 242 | + return html` |
| 243 | + <button class="chip" type="button" |
| 244 | + aria-label=${this.accessibleLabel || nothing} |
| 245 | + aria-pressed=${this.selected} |
| 246 | + @click=${this._handleClick}> |
| 247 | + ${content} |
| 248 | + </button> |
| 249 | + `; |
| 250 | + } |
| 251 | +} |
| 252 | + |
| 253 | +customElements.define('ol-chip', OLChip); |
0 commit comments