1- import { html } from 'lit' ;
1+ import { LitElement , html } from 'lit' ;
22import { customElement } from 'lit/decorators/custom-element.js' ;
33import { property } from 'lit/decorators/property.js' ;
44import { classMap } from 'lit/directives/class-map.js' ;
55
6- import { ComposedEvent } from '@patternfly/pfe-core' ;
7-
8- import { BaseLabel } from './BaseLabel.js' ;
6+ import { SlotController } from '@patternfly/pfe-core/controllers/slot-controller.js' ;
97
108import '@patternfly/elements/pf-button/pf-button.js' ;
119
1210import styles from './pf-label.css' ;
1311
14- export type LabelVariant = (
15- | 'filled'
16- | 'outline'
17- ) ;
18-
19- export type LabelColor = (
20- | 'blue'
21- | 'cyan'
22- | 'green'
23- | 'orange'
24- | 'purple'
25- | 'red'
26- | 'grey'
27- | 'gold'
28- ) ;
12+ export class LabelCloseEvent extends Event {
13+ constructor ( ) {
14+ super ( 'close' , { bubbles : true , cancelable : true } ) ;
15+ }
16+ }
2917
3018/**
3119 * The **label** component allows users to add specific element captions for user
3220 * clarity and convenience.
3321 * @summary Allows users to display meta data in a stylized form.
34- * @fires close - when a removable label's close button is clicked
22+ * @fires { LabelCloseEvent } close - when a removable label's close button is clicked
3523 * @cssprop {<length>} --pf-c-label--FontSize {@default `0.875em`}
3624 * @cssprop {<length>} --pf-c-label--PaddingTop {@default `0.25rem`}
3725 * @cssprop {<length>} --pf-c-label--PaddingRight {@default `0.5rem`}
@@ -92,11 +80,11 @@ export type LabelColor = (
9280 * @cssprop {<length>} --pf-c-label--m-compact--PaddingLeft {@default `0.5rem`}
9381 */
9482@customElement ( 'pf-label' )
95- export class PfLabel extends BaseLabel {
96- static readonly styles = [ ... BaseLabel . styles , styles ] ;
83+ export class PfLabel extends LitElement {
84+ static readonly styles = [ styles ] ;
9785
9886 static override readonly shadowRootOptions = {
99- ...BaseLabel . shadowRootOptions ,
87+ ...LitElement . shadowRootOptions ,
10088 delegatesFocus : true ,
10189 } ;
10290
@@ -105,12 +93,22 @@ export class PfLabel extends BaseLabel {
10593 * - Filled: Colored background with colored border.
10694 * - Outline: White background with colored border.
10795 */
108- @property ( ) variant : LabelVariant = 'filled' ;
96+ @property ( ) variant :
97+ | 'filled'
98+ | 'outline' = 'filled' ;
10999
110100 /**
111101 * Changes the color of the label
112102 */
113- @property ( ) color : LabelColor = 'grey' ;
103+ @property ( ) color :
104+ | 'blue'
105+ | 'cyan'
106+ | 'green'
107+ | 'orange'
108+ | 'purple'
109+ | 'red'
110+ | 'grey'
111+ | 'gold' = 'grey' ;
114112
115113 /** Shorthand for the `icon` slot, the value is icon name */
116114 @property ( ) icon ?: string ;
@@ -127,34 +125,51 @@ export class PfLabel extends BaseLabel {
127125 /** Text label for a removable label's close button */
128126 @property ( { attribute : 'close-button-label' } ) closeButtonLabel ?: string ;
129127
128+ /** Represents the state of the anonymous and icon slots */
129+ #slots = new SlotController ( this , null , 'icon' ) ;
130+
130131 override render ( ) {
131132 const { compact, truncated } = this ;
133+ const { variant, color, icon } = this ;
134+ const hasIcon = ! ! icon || this . #slots. hasSlotted ( 'icon' ) ;
132135 return html `
133- < span id ="pf-container " class ="${ classMap ( { compact, truncated } ) } "> ${ super . render ( ) } </ span >
134- ` ;
135- }
136-
137- protected override renderDefaultIcon ( ) {
138- return ! this . icon ? '' : html `
139- < pf-icon icon ="${ this . icon } " size ="sm "> </ pf-icon >
136+ < span id ="container "
137+ class ="${ classMap ( {
138+ hasIcon,
139+ compact,
140+ truncated,
141+ [ variant ?? '' ] : ! ! variant ,
142+ [ color ?? '' ] : ! ! color } ) } ">
143+ < slot name ="icon " part ="icon ">
144+ < pf-icon ?hidden ="${ ! icon } "
145+ size ="sm "
146+ .icon ="${ this . icon || undefined as unknown as string } "> </ pf-icon >
147+ </ slot >
148+ < slot id ="text "> </ slot >
149+ < span part ="close-button " ?hidden =${ ! this . removable } >
150+ < pf-button plain
151+ @click ="${ this . #onClickClose} "
152+ label ="${ this . closeButtonLabel ?? 'remove' } ">
153+ < svg viewBox ="0 0 352 512 ">
154+ < path d ="M242.72 256l100.07-100.07c12.28-12.28 12.28-32.19 0-44.48l-22.24-22.24c-12.28-12.28-32.19-12.28-44.48 0L176 189.28 75.93 89.21c-12.28-12.28-32.19-12.28-44.48 0L9.21 111.45c-12.28 12.28-12.28 32.19 0 44.48L109.28 256 9.21 356.07c-12.28 12.28-12.28 32.19 0 44.48l22.24 22.24c12.28 12.28 32.2 12.28 44.48 0L176 322.72l100.07 100.07c12.28 12.28 32.2 12.28 44.48 0l22.24-22.24c12.28-12.28 12.28-32.19 0-44.48L242.72 256z "/>
155+ </ svg >
156+ </ pf-button >
157+ </ span >
158+ </ span >
140159 ` ;
141160 }
142161
143- protected override renderSuffix ( ) {
144- return ! this . removable ? '' : html `
145- < span part ="close-button " ?hidden =${ ! this . removable } >
146- < pf-button plain
147- @click ="${ ( ) => this . dispatchEvent ( new ComposedEvent ( 'close' ) ) } "
148- label ="${ this . closeButtonLabel ?? 'remove' } ">
149- < svg viewBox ="0 0 352 512 ">
150- < path d ="M242.72 256l100.07-100.07c12.28-12.28 12.28-32.19 0-44.48l-22.24-22.24c-12.28-12.28-32.19-12.28-44.48 0L176 189.28 75.93 89.21c-12.28-12.28-32.19-12.28-44.48 0L9.21 111.45c-12.28 12.28-12.28 32.19 0 44.48L109.28 256 9.21 356.07c-12.28 12.28-12.28 32.19 0 44.48l22.24 22.24c12.28 12.28 32.2 12.28 44.48 0L176 322.72l100.07 100.07c12.28 12.28 32.2 12.28 44.48 0l22.24-22.24c12.28-12.28 12.28-32.19 0-44.48L242.72 256z "/>
151- </ svg >
152- </ pf-button >
153- </ span >
154- ` ;
162+ #onClickClose( ) {
163+ if ( this . removable && this . dispatchEvent ( new LabelCloseEvent ( ) ) ) {
164+ this . remove ( ) ;
165+ }
155166 }
156167}
157168
169+ export type LabelVariant = PfLabel [ 'variant' ] ;
170+
171+ export type LabelColor = PfLabel [ 'color' ] ;
172+
158173declare global {
159174 interface HTMLElementTagNameMap {
160175 'pf-label' : PfLabel ;
0 commit comments