Skip to content

Commit f24d30d

Browse files
authored
Merge c7d0ee8 into b48870e
2 parents b48870e + c7d0ee8 commit f24d30d

File tree

9 files changed

+155
-165
lines changed

9 files changed

+155
-165
lines changed

.changeset/label-close-remove.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
"@patternfly/elements": major
3+
---
4+
`<pf-label>`: when clicking close button, `close` event is fired.
5+
Now, if that event is not cancelled, the label will remove itself from the document.
6+
7+
To restore previous behaviour:
8+
9+
```js
10+
import { LabelCloseEvent } from '@patternfly/elements/pf-label/pf-label.js';
11+
label.addEventListener('close', function(event) {
12+
if (event instanceof LabelCloseEvent) {
13+
event.preventDefault();
14+
return false;
15+
}
16+
});
17+
```

.changeset/remove-base-label.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
"@patternfly/elements": major
3+
---
4+
`<pf-label>`: Removed `BaseLabel` class. Reimplement (recommended) or extend `PfLabel`.

elements/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
"./pf-jump-links/pf-jump-links-item.js": "./pf-jump-links/pf-jump-links-item.js",
4242
"./pf-jump-links/pf-jump-links-list.js": "./pf-jump-links/pf-jump-links-list.js",
4343
"./pf-jump-links/pf-jump-links.js": "./pf-jump-links/pf-jump-links.js",
44-
"./pf-label/BaseLabel.js": "./pf-label/BaseLabel.js",
4544
"./pf-label/pf-label.js": "./pf-label/pf-label.js",
4645
"./pf-select/pf-select.js": "./pf-select/pf-select.js",
4746
"./pf-select/pf-listbox.js": "./pf-select/pf-listbox.js",

elements/pf-label/BaseLabel.css

Lines changed: 0 additions & 44 deletions
This file was deleted.

elements/pf-label/BaseLabel.ts

Lines changed: 0 additions & 53 deletions
This file was deleted.

elements/pf-label/docs/pf-label.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,17 @@
3131
</div>
3232

3333
{% htmlexample %}
34+
<style>
35+
.visually-hidden-class {
36+
clip: rect(0 0 0 0);
37+
clip-path: inset(50%);
38+
height: 1px;
39+
overflow: hidden;
40+
position: absolute;
41+
white-space: nowrap;
42+
width: 1px;
43+
}
44+
</style>
3445
<pf-label color="red">
3546
Red <span class="visually-hidden-class">Warning</span>
3647
</pf-label>

elements/pf-label/pf-label.css

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,61 @@
1-
#pf-container {
2-
display: contents;
1+
:host {
2+
position: relative;
3+
white-space: nowrap;
4+
border: 0;
35
}
46

7+
pf-icon, ::slotted(pf-icon) {
8+
color: currentColor;
9+
}
10+
11+
:host,
512
#container {
6-
--pf-global--icon--FontSize--sm: 14px;
13+
display: inline-flex;
14+
align-items: center;
15+
vertical-align: middle;
16+
}
717

18+
#container {
19+
overflow: hidden;
20+
text-overflow: ellipsis;
21+
white-space: nowrap;
22+
border-width: 0;
823
padding-top: var(--pf-c-label--PaddingTop, var(--pf-global--spacer--xs, 0.25rem));
924
padding-left: var(--pf-c-label--PaddingLeft, var(--pf-global--spacer--sm, 0.5rem));
1025
padding-bottom: var(--pf-c-label--PaddingBottom, var(--pf-global--spacer--xs, 0.25rem));
1126
padding-right: var(--pf-c-label--PaddingRight, var(--pf-global--spacer--sm, 0.5rem));
12-
font-size: var(--pf-c-label--FontSize, 0.875em);
27+
font-size: var(--pf-c-label--FontSize, var(--pf-global--FontSize--sm, 0.875rem));
1328
color: var(--pf-c-label--Color, var(--pf-global--Color--100, #151515));
1429
background-color: var(--pf-c-label--BackgroundColor, var(--pf-global--palette--black-150, #f5f5f5));
1530
border-radius: var(--pf-c-label--BorderRadius, 30em);
1631
max-width: var(--pf-c-label__content--MaxWidth, 100%);
1732
color: var(--pf-c-label__content--Color, var(--pf-global--Color--100, #151515));
33+
34+
--pf-global--icon--FontSize--sm: 14px;
1835
}
1936

2037
#container::before {
38+
position: absolute;
39+
top: 0;
40+
right: 0;
41+
bottom: 0;
42+
left: 0;
43+
pointer-events: none;
44+
content: "";
2145
border-radius: var(--pf-c-label--BorderRadius, 30em);
2246
border: var(--pf-c-label__content--before--BorderWidth, 1px) solid var(--pf-c-label__content--before--BorderColor, var(--pf-global--palette--black-300, #d2d2d2));
2347
}
2448

49+
[part=icon] {
50+
display: none;
51+
pointer-events: none;
52+
}
53+
54+
.hasIcon [part=icon] {
55+
display: inline-flex;
56+
width: 1em;
57+
}
58+
2559
.compact {
2660
--pf-c-label--PaddingTop: var(--pf-c-label--m-compact--PaddingTop, 0);
2761
--pf-c-label--PaddingRight: var(--pf-c-label--m-compact--PaddingRight, var(--pf-global--spacer--sm, 0.5rem));
@@ -30,7 +64,6 @@
3064
--pf-global--icon--FontSize--sm: 12px;
3165
}
3266

33-
3467
.blue {
3568
--pf-c-label__content--Color: var(--pf-c-label--m-blue__content--Color, var(--pf-global--info-color--200, #002952));
3669
--pf-c-label--BackgroundColor: var(--pf-c-label--m-blue--BackgroundColor, var(--pf-global--palette--blue-50, #e7f1fa));

elements/pf-label/pf-label.ts

Lines changed: 59 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,25 @@
1-
import { html } from 'lit';
1+
import { LitElement, html } from 'lit';
22
import { customElement } from 'lit/decorators/custom-element.js';
33
import { property } from 'lit/decorators/property.js';
44
import { 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

108
import '@patternfly/elements/pf-button/pf-button.js';
119

1210
import 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+
158173
declare global {
159174
interface HTMLElementTagNameMap {
160175
'pf-label': PfLabel;

0 commit comments

Comments
 (0)