|
| 1 | +/** |
| 2 | + * Copyright 2026 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 | +import { PropertyValues } from 'lit'; |
| 13 | +import { property } from 'lit/decorators.js'; |
| 14 | + |
| 15 | +import { SpectrumElement } from '@spectrum-web-components/core/element/index.js'; |
| 16 | + |
| 17 | +import { |
| 18 | + AVATAR_DEFAULT_SIZE, |
| 19 | + AVATAR_VALID_SIZES, |
| 20 | + type AvatarSize, |
| 21 | +} from './Avatar.types.js'; |
| 22 | + |
| 23 | +/** |
| 24 | + * Base class for the avatar component. |
| 25 | + * |
| 26 | + * Provides the core API for displaying a circular profile image. Concrete |
| 27 | + * classes supply the stylesheet and render template. |
| 28 | + */ |
| 29 | +export abstract class AvatarBase extends SpectrumElement { |
| 30 | + // ───────────────────────── |
| 31 | + // STATIC |
| 32 | + // ───────────────────────── |
| 33 | + |
| 34 | + /** |
| 35 | + * @internal |
| 36 | + * |
| 37 | + * The set of valid numeric size values for the avatar. |
| 38 | + * |
| 39 | + * This is an internal property not intended for consumer use, but used in |
| 40 | + * internal validation logic, stories, and tests to keep them in sync with |
| 41 | + * the canonical type definition in `Avatar.types.ts`. |
| 42 | + */ |
| 43 | + static readonly VALID_SIZES: readonly AvatarSize[] = AVATAR_VALID_SIZES; |
| 44 | + |
| 45 | + // ────────────────── |
| 46 | + // CORE API |
| 47 | + // ────────────────── |
| 48 | + |
| 49 | + /** |
| 50 | + * URL of the profile image to display. |
| 51 | + */ |
| 52 | + @property({ type: String }) |
| 53 | + public src = ''; |
| 54 | + |
| 55 | + /** |
| 56 | + * Text description of the avatar image. |
| 57 | + */ |
| 58 | + @property({ type: String }) |
| 59 | + public alt: string | undefined; |
| 60 | + |
| 61 | + // ─────────────────── |
| 62 | + // SIZE API |
| 63 | + // ─────────────────── |
| 64 | + |
| 65 | + /** |
| 66 | + * The size of the avatar. Invalid values fall back to the default (500). |
| 67 | + */ |
| 68 | + @property({ type: Number, reflect: true }) |
| 69 | + public get size(): AvatarSize { |
| 70 | + return this._size; |
| 71 | + } |
| 72 | + |
| 73 | + public set size(value: AvatarSize) { |
| 74 | + const validSize = (AVATAR_VALID_SIZES as readonly number[]).includes( |
| 75 | + Number(value) |
| 76 | + ) |
| 77 | + ? (Number(value) as AvatarSize) |
| 78 | + : AVATAR_DEFAULT_SIZE; |
| 79 | + |
| 80 | + if (this._size === validSize) { |
| 81 | + return; |
| 82 | + } |
| 83 | + |
| 84 | + const oldSize = this._size; |
| 85 | + this._size = validSize; |
| 86 | + this.requestUpdate('size', oldSize); |
| 87 | + } |
| 88 | + |
| 89 | + private _size: AvatarSize = AVATAR_DEFAULT_SIZE; |
| 90 | + |
| 91 | + // ─────────────────────── |
| 92 | + // VISUAL API |
| 93 | + // ─────────────────────── |
| 94 | + |
| 95 | + /** |
| 96 | + * Renders a visual outline around the avatar image. |
| 97 | + */ |
| 98 | + @property({ type: Boolean, reflect: true }) |
| 99 | + public outline = false; |
| 100 | + |
| 101 | + /** |
| 102 | + * Renders the avatar at reduced opacity, indicating the entity is inactive or unavailable. |
| 103 | + */ |
| 104 | + @property({ type: Boolean, reflect: true }) |
| 105 | + public disabled = false; |
| 106 | + |
| 107 | + // ─────────────────────────── |
| 108 | + // ACCESSIBILITY API |
| 109 | + // ─────────────────────────── |
| 110 | + |
| 111 | + /** |
| 112 | + * Marks the avatar as decorative, hiding it from assistive technology. |
| 113 | + */ |
| 114 | + @property({ type: Boolean, reflect: true }) |
| 115 | + public decorative = false; |
| 116 | + |
| 117 | + // ────────────────────── |
| 118 | + // IMPLEMENTATION |
| 119 | + // ────────────────────── |
| 120 | + |
| 121 | + protected override firstUpdated(changes: PropertyValues): void { |
| 122 | + super.firstUpdated(changes); |
| 123 | + if (!this.hasAttribute('size')) { |
| 124 | + this.setAttribute('size', String(this.size)); |
| 125 | + } |
| 126 | + this._syncAriaHidden(); |
| 127 | + if (window.__swc?.DEBUG) { |
| 128 | + this._warnMissingAlt(); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + protected override updated(changes: PropertyValues): void { |
| 133 | + super.updated(changes); |
| 134 | + if (changes.has('decorative')) { |
| 135 | + this._syncAriaHidden(); |
| 136 | + } |
| 137 | + if (changes.has('alt') && window.__swc?.DEBUG) { |
| 138 | + this._warnMissingAlt(); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + private _syncAriaHidden(): void { |
| 143 | + if (this.decorative) { |
| 144 | + this.setAttribute('aria-hidden', 'true'); |
| 145 | + } else { |
| 146 | + this.removeAttribute('aria-hidden'); |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + private _warnMissingAlt(): void { |
| 151 | + if (this.alt === undefined && !this.decorative) { |
| 152 | + window.__swc?.warn( |
| 153 | + this, |
| 154 | + `<${this.localName}> is missing an \`alt\` attribute. Provide a text description or pass \`alt=""\` and mark it as \`decorative\`.`, |
| 155 | + 'https://opensource.adobe.com/spectrum-web-components/components/avatar/#accessibility', |
| 156 | + { |
| 157 | + type: 'accessibility', |
| 158 | + issues: [ |
| 159 | + 'Provide an `alt` attribute with meaningful alternative text, or', |
| 160 | + 'Set `alt=""` and mark the image as `decorative` (hidden from screen readers).', |
| 161 | + ], |
| 162 | + } |
| 163 | + ); |
| 164 | + } |
| 165 | + } |
| 166 | +} |
0 commit comments