Skip to content

Commit

Permalink
fix(label)!: remove BaseLabel
Browse files Browse the repository at this point in the history
Closes #2620
  • Loading branch information
bennypowers committed Oct 19, 2023
1 parent 24d43bd commit 3baa498
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 146 deletions.
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 @@ -35,7 +35,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-modal/pf-modal.js": "./pf-modal/pf-modal.js",
"./pf-panel/pf-panel.js": "./pf-panel/pf-panel.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.

41 changes: 37 additions & 4 deletions elements/pf-label/pf-label.css
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
#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));
Expand All @@ -15,13 +30,32 @@
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
93 changes: 49 additions & 44 deletions elements/pf-label/pf-label.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,27 @@
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 });
}
}

/**
* 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`}
*
Expand Down Expand Up @@ -112,22 +100,22 @@ 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 readonly shadowRootOptions: ShadowRootInit = { ...BaseLabel.shadowRootOptions, delegatesFocus: true };
static readonly shadowRootOptions: ShadowRootInit = { ...LitElement.shadowRootOptions, delegatesFocus: true };

/**
* Changes the style of the label.
* - 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 @@ -144,34 +132,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

0 comments on commit 3baa498

Please sign in to comment.