10
10
* governing permissions and limitations under the License.
11
11
*/
12
12
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' ;
14
17
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 ] ;
16
38
17
39
import styles from './badge.css' ;
18
40
19
41
// 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' ;
22
47
23
48
/**
24
49
* A badge component that displays short, descriptive information about an element.
@@ -28,14 +53,16 @@ export type { BadgeVariant, FixedValues } from '@swc/core/components/badge';
28
53
* @since 1.0.0
29
54
* @status stable
30
55
* @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.
32
62
*
33
63
* @slot - Text label of the badge
34
64
* @slot icon - Optional icon that appears to the left of the label
35
65
*
36
- * @csspart label - The text content area of the badge
37
- * @csspart icon - The icon area of the badge (when present)
38
- *
39
66
* @example
40
67
* <swc-badge variant="positive">New</swc-badge>
41
68
*
@@ -46,23 +73,68 @@ export type { BadgeVariant, FixedValues } from '@swc/core/components/badge';
46
73
* </swc-badge>
47
74
*/
48
75
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
+
49
82
public static override get styles ( ) : CSSResultArray {
50
83
return [ styles ] ;
51
84
}
52
85
53
86
protected override render ( ) : TemplateResult {
54
87
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 >
65
118
</ div >
66
119
` ;
67
120
}
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
+ }
68
140
}
0 commit comments