Skip to content

Commit 74a2b4e

Browse files
authored
feat(badge): s2 styling and render (#5718)
* feat(badge): s2 styling and render brought in from Spectrum CSS
1 parent 437735c commit 74a2b4e

File tree

4 files changed

+697
-341
lines changed

4 files changed

+697
-341
lines changed

second-gen/packages/core/components/badge/Badge.base.ts

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,16 @@ import { SizedMixin, SpectrumElement } from '@swc/core/shared/base';
1616
import { ObserveSlotPresence } from '@swc/core/shared/observe-slot-presence';
1717
import { ObserveSlotText } from '@swc/core/shared/observe-slot-text';
1818

19-
export const BADGE_VARIANTS = [
19+
export const BADGE_VARIANTS_SEMANTIC = [
2020
'accent',
2121
'neutral',
2222
'informative',
2323
'positive',
2424
'negative',
2525
'notice',
26+
] as const;
27+
28+
export const BADGE_VARIANTS_COLOR = [
2629
'fuchsia',
2730
'indigo',
2831
'magenta',
@@ -38,6 +41,11 @@ export const BADGE_VARIANTS = [
3841
'cyan',
3942
'blue',
4043
] as const;
44+
45+
export const BADGE_VARIANTS = [
46+
...BADGE_VARIANTS_SEMANTIC,
47+
...BADGE_VARIANTS_COLOR,
48+
] as const;
4149
export type BadgeVariant = (typeof BADGE_VARIANTS)[number];
4250
export const FIXED_VALUES = [
4351
'inline-start',
@@ -49,8 +57,9 @@ export type FixedValues = (typeof FIXED_VALUES)[number];
4957

5058
/**
5159
* @element sp-badge-base
52-
* @slot - The text label to display in the badge.
53-
* @slot icon - The icon to display in the badge.
60+
* @attribute {ElementSize} size - The size of the badge.
61+
* @attribute {BadgeVariant} variant - The variant of the badge.
62+
* @attribute {FixedValues} fixed - The fixed position of the badge.
5463
*/
5564
export abstract class BadgeBase extends SizedMixin(
5665
ObserveSlotText(ObserveSlotPresence(SpectrumElement, '[slot="icon"]'), ''),
@@ -61,27 +70,12 @@ export abstract class BadgeBase extends SizedMixin(
6170
@property({ type: String, reflect: true })
6271
public variant: BadgeVariant = 'informative';
6372

64-
@property({ reflect: true })
65-
public get fixed(): FixedValues | undefined {
66-
return this._fixed;
67-
}
68-
69-
public set fixed(fixed: FixedValues | undefined) {
70-
if (fixed === this.fixed) {
71-
return;
72-
}
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;
73+
@property({ type: String, reflect: true })
74+
public fixed?: FixedValues;
8475

76+
/**
77+
* @internal Used for rendering gap when the badge has an icon.
78+
*/
8579
protected get hasIcon(): boolean {
8680
return this.slotContentIsPresent;
8781
}

second-gen/packages/swc/components/badge/Badge.ts

Lines changed: 90 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,40 @@
1010
* governing permissions and limitations under the License.
1111
*/
1212

13-
import { CSSResultArray, html, nothing, TemplateResult } from 'lit';
13+
import { CSSResultArray, html, PropertyValues, TemplateResult } from 'lit';
14+
import { property } from 'lit/decorators.js';
15+
import { classMap } from 'lit/directives/class-map.js';
16+
import { when } from 'lit/directives/when.js';
1417

15-
import { BadgeBase } from '@swc/core/components/badge';
18+
import {
19+
BADGE_VARIANTS_COLOR as BADGE_VARIANTS_COLOR_BASE,
20+
BADGE_VARIANTS_SEMANTIC,
21+
BadgeBase,
22+
} from '@swc/core/components/badge';
23+
24+
export const BADGE_VARIANTS_COLOR = [
25+
...BADGE_VARIANTS_COLOR_BASE,
26+
'pink',
27+
'turquoise',
28+
'brown',
29+
'cinnamon',
30+
'silver',
31+
] as const;
32+
33+
export const BADGE_VARIANTS = [
34+
...BADGE_VARIANTS_SEMANTIC,
35+
...BADGE_VARIANTS_COLOR,
36+
] as const;
37+
export type BadgeVariant = (typeof BADGE_VARIANTS)[number];
1638

1739
import styles from './badge.css';
1840

1941
// Export types and values to avoid breaking changes
20-
export { BADGE_VARIANTS, FIXED_VALUES } from '@swc/core/components/badge';
21-
export type { BadgeVariant, FixedValues } from '@swc/core/components/badge';
42+
export {
43+
BADGE_VARIANTS_SEMANTIC,
44+
FIXED_VALUES,
45+
} from '@swc/core/components/badge';
46+
export type { FixedValues } from '@swc/core/components/badge';
2247

2348
/**
2449
* A badge component that displays short, descriptive information about an element.
@@ -28,14 +53,16 @@ export type { BadgeVariant, FixedValues } from '@swc/core/components/badge';
2853
* @since 1.0.0
2954
* @status stable
3055
* @github https://github.com/adobe/spectrum-web-components/tree/main/...
31-
* @figma https://www.figma.com/design/...
56+
* @figma https://www.figma.com/design/Mngz9H7WZLbrCvGQf3GnsY/S2-%2F-Desktop?node-id=36806-6551
57+
*
58+
* @attribute {BadgeVariant} variant - The variant of the badge.
59+
* @attribute {boolean} subtle - Whether the badge is subtle.
60+
* @attribute {boolean} outline - Whether the badge is outlined.
61+
* @attribute {FixedValues} fixed - The fixed position of the badge.
3262
*
3363
* @slot - Text label of the badge
3464
* @slot icon - Optional icon that appears to the left of the label
3565
*
36-
* @csspart label - The text content area of the badge
37-
* @csspart icon - The icon area of the badge (when present)
38-
*
3966
* @example
4067
* <swc-badge variant="positive">New</swc-badge>
4168
*
@@ -46,23 +73,68 @@ export type { BadgeVariant, FixedValues } from '@swc/core/components/badge';
4673
* </swc-badge>
4774
*/
4875
export class Badge extends BadgeBase {
76+
@property({ type: Boolean, reflect: true })
77+
public subtle: boolean = false;
78+
79+
@property({ type: Boolean, reflect: true })
80+
public outline: boolean = false;
81+
4982
public static override get styles(): CSSResultArray {
5083
return [styles];
5184
}
5285

5386
protected override render(): TemplateResult {
5487
return html`
55-
${this.hasIcon
56-
? html`
57-
<slot
58-
name="icon"
59-
?icon-only=${!this.slotHasContent}
60-
></slot>
61-
`
62-
: nothing}
63-
<div class="label">
64-
<slot></slot>
88+
<div
89+
class=${classMap({
90+
['spectrum-Badge']: true,
91+
[`spectrum-Badge--size${this.size?.toUpperCase()}`]:
92+
typeof this.size !== 'undefined',
93+
[`spectrum-Badge--${this.variant}`]:
94+
typeof this.variant !== 'undefined',
95+
[`spectrum-Badge--subtle`]: this.subtle,
96+
[`spectrum-Badge--outline`]: this.outline,
97+
[`spectrum-Badge--fixed-${this.fixed}`]:
98+
typeof this.fixed !== 'undefined',
99+
})}
100+
>
101+
${when(
102+
this.hasIcon,
103+
() => html`
104+
<div
105+
class=${classMap({
106+
[`spectrum-Badge-icon`]: true,
107+
[`spectrum-Badge-icon--no-label`]:
108+
!this.slotHasContent,
109+
})}
110+
>
111+
<slot name="icon"></slot>
112+
</div>
113+
`
114+
)}
115+
<div class="spectrum-Badge-label">
116+
<slot></slot>
117+
</div>
65118
</div>
66119
`;
67120
}
121+
122+
protected override update(changedProperties: PropertyValues): void {
123+
super.update(changedProperties);
124+
if (window.__swc?.DEBUG) {
125+
if (
126+
this.outline &&
127+
!BADGE_VARIANTS_SEMANTIC.includes(this.variant)
128+
) {
129+
window.__swc.warn(
130+
this,
131+
`<${this.localName}> element only supports the outline styling if the variant is a semantic color variant.`,
132+
'https://opensource.adobe.com/spectrum-web-components/components/badge/#variants',
133+
{
134+
issues: [...BADGE_VARIANTS_SEMANTIC],
135+
}
136+
);
137+
}
138+
}
139+
}
68140
}

0 commit comments

Comments
 (0)