Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(label)!: remove BaseLabel #2632

Merged
merged 10 commits into from
Apr 11, 2024
17 changes: 17 additions & 0 deletions .changeset/label-close-remove.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
"@patternfly/elements": major
---
`<pf-label>`: when clicking close button, `close` event is fired.
Now, if that event is not cancelled, the label will remove itself from the document.

To restore previous behaviour:

```js
import { LabelCloseEvent } from '@patternfly/elements/pf-label/pf-label.js';
label.addEventListener('close', function(event) {
if (event instanceof LabelCloseEvent) {
event.preventDefault();
return false;
}
});
```
4 changes: 4 additions & 0 deletions .changeset/remove-base-label.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
"@patternfly/elements": major
---
`<pf-label>`: Removed `BaseLabel` class. Reimplement (recommended) or extend `PfLabel`.
1 change: 0 additions & 1 deletion elements/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
"./pf-jump-links/pf-jump-links-item.js": "./pf-jump-links/pf-jump-links-item.js",
"./pf-jump-links/pf-jump-links-list.js": "./pf-jump-links/pf-jump-links-list.js",
"./pf-jump-links/pf-jump-links.js": "./pf-jump-links/pf-jump-links.js",
"./pf-label/BaseLabel.js": "./pf-label/BaseLabel.js",
"./pf-label/pf-label.js": "./pf-label/pf-label.js",
"./pf-select/pf-select.js": "./pf-select/pf-select.js",
"./pf-select/pf-listbox.js": "./pf-select/pf-listbox.js",
Expand Down
44 changes: 0 additions & 44 deletions elements/pf-label/BaseLabel.css

This file was deleted.

53 changes: 0 additions & 53 deletions elements/pf-label/BaseLabel.ts

This file was deleted.

11 changes: 11 additions & 0 deletions elements/pf-label/docs/pf-label.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@
</div>

{% htmlexample %}
<style>
.visually-hidden-class {
clip: rect(0 0 0 0);
clip-path: inset(50%);
height: 1px;
overflow: hidden;
position: absolute;
white-space: nowrap;
width: 1px;
}
</style>
<pf-label color="red">
Red <span class="visually-hidden-class">Warning</span>
</pf-label>
Expand Down
43 changes: 38 additions & 5 deletions elements/pf-label/pf-label.css
Original file line number Diff line number Diff line change
@@ -1,27 +1,61 @@
#pf-container {
display: contents;
:host {
position: relative;
white-space: nowrap;
border: 0;
}

pf-icon, ::slotted(pf-icon) {
color: currentColor;
}

:host,
#container {
--pf-global--icon--FontSize--sm: 14px;
display: inline-flex;
align-items: center;
vertical-align: middle;
}

#container {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
border-width: 0;
padding-top: var(--pf-c-label--PaddingTop, var(--pf-global--spacer--xs, 0.25rem));
padding-left: var(--pf-c-label--PaddingLeft, var(--pf-global--spacer--sm, 0.5rem));
padding-bottom: var(--pf-c-label--PaddingBottom, var(--pf-global--spacer--xs, 0.25rem));
padding-right: var(--pf-c-label--PaddingRight, var(--pf-global--spacer--sm, 0.5rem));
font-size: var(--pf-c-label--FontSize, 0.875em);
font-size: var(--pf-c-label--FontSize, var(--pf-global--FontSize--sm, 0.875rem));
color: var(--pf-c-label--Color, var(--pf-global--Color--100, #151515));
background-color: var(--pf-c-label--BackgroundColor, var(--pf-global--palette--black-150, #f5f5f5));
border-radius: var(--pf-c-label--BorderRadius, 30em);
max-width: var(--pf-c-label__content--MaxWidth, 100%);
color: var(--pf-c-label__content--Color, var(--pf-global--Color--100, #151515));

--pf-global--icon--FontSize--sm: 14px;
}

#container::before {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
pointer-events: none;
content: "";
border-radius: var(--pf-c-label--BorderRadius, 30em);
border: var(--pf-c-label__content--before--BorderWidth, 1px) solid var(--pf-c-label__content--before--BorderColor, var(--pf-global--palette--black-300, #d2d2d2));
}

[part=icon] {
display: none;
pointer-events: none;
}

.hasIcon [part=icon] {
display: inline-flex;
width: 1em;
}

.compact {
--pf-c-label--PaddingTop: var(--pf-c-label--m-compact--PaddingTop, 0);
--pf-c-label--PaddingRight: var(--pf-c-label--m-compact--PaddingRight, var(--pf-global--spacer--sm, 0.5rem));
Expand All @@ -30,7 +64,6 @@
--pf-global--icon--FontSize--sm: 12px;
}


.blue {
--pf-c-label__content--Color: var(--pf-c-label--m-blue__content--Color, var(--pf-global--info-color--200, #002952));
--pf-c-label--BackgroundColor: var(--pf-c-label--m-blue--BackgroundColor, var(--pf-global--palette--blue-50, #e7f1fa));
Expand Down
103 changes: 59 additions & 44 deletions elements/pf-label/pf-label.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,25 @@
import { html } from 'lit';
import { LitElement, html } from 'lit';
import { customElement } from 'lit/decorators/custom-element.js';
import { property } from 'lit/decorators/property.js';
import { classMap } from 'lit/directives/class-map.js';

import { ComposedEvent } from '@patternfly/pfe-core';

import { BaseLabel } from './BaseLabel.js';
import { SlotController } from '@patternfly/pfe-core/controllers/slot-controller.js';

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

import styles from './pf-label.css';

export type LabelVariant = (
| 'filled'
| 'outline'
);

export type LabelColor = (
| 'blue'
| 'cyan'
| 'green'
| 'orange'
| 'purple'
| 'red'
| 'grey'
| 'gold'
);
export class LabelCloseEvent extends Event {
constructor() {
super('close', { bubbles: true, cancelable: true });
}
}

/**
* The **label** component allows users to add specific element captions for user
* clarity and convenience.
* @summary Allows users to display meta data in a stylized form.
* @fires close - when a removable label's close button is clicked
* @fires {LabelCloseEvent} close - when a removable label's close button is clicked
* @cssprop {<length>} --pf-c-label--FontSize {@default `0.875em`}
* @cssprop {<length>} --pf-c-label--PaddingTop {@default `0.25rem`}
* @cssprop {<length>} --pf-c-label--PaddingRight {@default `0.5rem`}
Expand Down Expand Up @@ -92,11 +80,11 @@ export type LabelColor = (
* @cssprop {<length>} --pf-c-label--m-compact--PaddingLeft {@default `0.5rem`}
*/
@customElement('pf-label')
export class PfLabel extends BaseLabel {
static readonly styles = [...BaseLabel.styles, styles];
export class PfLabel extends LitElement {
static readonly styles = [styles];

static override readonly shadowRootOptions = {
...BaseLabel.shadowRootOptions,
...LitElement.shadowRootOptions,
delegatesFocus: true,
};

Expand All @@ -105,12 +93,22 @@ export class PfLabel extends BaseLabel {
* - Filled: Colored background with colored border.
* - Outline: White background with colored border.
*/
@property() variant: LabelVariant = 'filled';
@property() variant:
| 'filled'
| 'outline' = 'filled';

/**
* Changes the color of the label
*/
@property() color: LabelColor = 'grey';
@property() color:
| 'blue'
| 'cyan'
| 'green'
| 'orange'
| 'purple'
| 'red'
| 'grey'
| 'gold' = 'grey';

/** Shorthand for the `icon` slot, the value is icon name */
@property() icon?: string;
Expand All @@ -127,34 +125,51 @@ export class PfLabel extends BaseLabel {
/** Text label for a removable label's close button */
@property({ attribute: 'close-button-label' }) closeButtonLabel?: string;

/** Represents the state of the anonymous and icon slots */
#slots = new SlotController(this, null, 'icon');

override render() {
const { compact, truncated } = this;
const { variant, color, icon } = this;
const hasIcon = !!icon || this.#slots.hasSlotted('icon');
return html`
<span id="pf-container" class="${classMap({ compact, truncated })}">${super.render()}</span>
`;
}

protected override renderDefaultIcon() {
return !this.icon ? '' : html`
<pf-icon icon="${this.icon}" size="sm"></pf-icon>
<span id="container"
class="${classMap({
hasIcon,
compact,
truncated,
[variant ?? '']: !!variant,
[color ?? '']: !!color })}">
<slot name="icon" part="icon">
<pf-icon ?hidden="${!icon}"
size="sm"
.icon="${this.icon || undefined as unknown as string}"></pf-icon>
</slot>
<slot id="text"></slot>
<span part="close-button" ?hidden=${!this.removable}>
<pf-button plain
@click="${this.#onClickClose}"
label="${this.closeButtonLabel ?? 'remove'}">
<svg viewBox="0 0 352 512">
<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"/>
</svg>
</pf-button>
</span>
</span>
`;
}

protected override renderSuffix() {
return !this.removable ? '' : html`
<span part="close-button" ?hidden=${!this.removable}>
<pf-button plain
@click="${() => this.dispatchEvent(new ComposedEvent('close'))}"
label="${this.closeButtonLabel ?? 'remove'}">
<svg viewBox="0 0 352 512">
<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"/>
</svg>
</pf-button>
</span>
`;
#onClickClose() {
if (this.removable && this.dispatchEvent(new LabelCloseEvent())) {
this.remove();
}
}
}

export type LabelVariant = PfLabel['variant'];

export type LabelColor = PfLabel['color'];

declare global {
interface HTMLElementTagNameMap {
'pf-label': PfLabel;
Expand Down
Loading
Loading