|
| 1 | +/** |
| 2 | + * Copyright 2025 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 { |
| 13 | + PropertyValues, |
| 14 | + SizedMixin, |
| 15 | + SpectrumElement, |
| 16 | +} from '@spectrum-web-components/base'; |
| 17 | + |
| 18 | +import { ObserveSlotText } from '@spectrum-web-components/shared/src/observe-slot-text.js'; |
| 19 | +import { ObserveSlotPresence } from '@spectrum-web-components/shared/src/observe-slot-presence.js'; |
| 20 | + |
| 21 | +import { property } from '@spectrum-web-components/base/src/decorators.js'; |
| 22 | + |
| 23 | +export const BADGE_VARIANTS = [ |
| 24 | + 'accent', |
| 25 | + 'neutral', |
| 26 | + 'informative', |
| 27 | + 'positive', |
| 28 | + 'negative', |
| 29 | + 'notice', |
| 30 | + 'fuchsia', |
| 31 | + 'indigo', |
| 32 | + 'magenta', |
| 33 | + 'purple', |
| 34 | + 'seafoam', |
| 35 | + 'yellow', |
| 36 | + 'gray', |
| 37 | + 'red', |
| 38 | + 'orange', |
| 39 | + 'chartreuse', |
| 40 | + 'celery', |
| 41 | + 'green', |
| 42 | + 'cyan', |
| 43 | + 'blue', |
| 44 | +] as const; |
| 45 | +export type BadgeVariant = (typeof BADGE_VARIANTS)[number]; |
| 46 | +export const FIXED_VALUES = [ |
| 47 | + 'inline-start', |
| 48 | + 'inline-end', |
| 49 | + 'block-start', |
| 50 | + 'block-end', |
| 51 | +] as const; |
| 52 | +export type FixedValues = (typeof FIXED_VALUES)[number]; |
| 53 | + |
| 54 | +/** |
| 55 | + * @element sp-badge-base class |
| 56 | + */ |
| 57 | +export abstract class BadgeBase extends SizedMixin( |
| 58 | + ObserveSlotText(ObserveSlotPresence(SpectrumElement, '[slot="icon"]'), ''), |
| 59 | + { |
| 60 | + noDefaultSize: true, |
| 61 | + } |
| 62 | +) { |
| 63 | + @property({ type: String, reflect: true }) |
| 64 | + public variant: BadgeVariant = 'informative'; |
| 65 | + |
| 66 | + @property({ reflect: true }) |
| 67 | + public get fixed(): FixedValues | undefined { |
| 68 | + return this._fixed; |
| 69 | + } |
| 70 | + |
| 71 | + public set fixed(fixed: FixedValues | undefined) { |
| 72 | + if (fixed === this.fixed) return; |
| 73 | + const oldValue = this.fixed; |
| 74 | + this._fixed = fixed; |
| 75 | + if (fixed) { |
| 76 | + this.setAttribute('fixed', fixed); |
| 77 | + } else { |
| 78 | + this.removeAttribute('fixed'); |
| 79 | + } |
| 80 | + this.requestUpdate('fixed', oldValue); |
| 81 | + } |
| 82 | + |
| 83 | + private _fixed?: FixedValues; |
| 84 | + |
| 85 | + protected get hasIcon(): boolean { |
| 86 | + return this.slotContentIsPresent; |
| 87 | + } |
| 88 | + |
| 89 | + protected override update(changedProperties: PropertyValues): void { |
| 90 | + super.update(changedProperties); |
| 91 | + if (window.__swc.DEBUG) { |
| 92 | + if (!BADGE_VARIANTS.includes(this.variant)) { |
| 93 | + window.__swc.warn( |
| 94 | + this, |
| 95 | + `<${this.localName}> element expect the "variant" attribute to be one of the following:`, |
| 96 | + 'https://opensource.adobe.com/spectrum-web-components/components/badge/#variants', |
| 97 | + { |
| 98 | + issues: [...BADGE_VARIANTS], |
| 99 | + } |
| 100 | + ); |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | +} |
0 commit comments