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

feat(button): add href for Link Variant #2814

Merged
merged 12 commits into from
Aug 18, 2024
5 changes: 5 additions & 0 deletions .changeset/popular-poets-march.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@patternfly/elements": minor
---

`<pf-button>`: Added `href` attribute to `<pf-button variant="link">`
2 changes: 2 additions & 0 deletions elements/pf-button/demo/variants.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ <h2>Link variant</h2>
<pf-button variant="link" icon="link" icon-position="right">Link</pf-button>
<pf-button variant="link" inline>Inline Link</pf-button>
<pf-button variant="link" danger>Danger Link</pf-button>
<pf-button variant="link" href="#link">Link</pf-button>
<pf-button variant="link" href="https://patternflyelements.org/" target="_blank" icon="location-arrow">Go to PatternFly</pf-button>

<h3>Disabled</h3>
<pf-button disabled
Expand Down
2 changes: 2 additions & 0 deletions elements/pf-button/docs/pf-button.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ pf-button + pf-button {
<pf-button variant="link" icon-set="patternfly" icon="arrow" icon-position="right">Link</pf-button>
<pf-button variant="link" inline>Inline Link</pf-button>
<pf-button variant="link" danger>Danger Link</pf-button>
<pf-button variant="link" href="#link">Link</pf-button>
<pf-button variant="link" href="https://patternflyelements.org/" target="_blank" icon="location-arrow">Go to PatternFly</pf-button>
{% endhtmlexample %}

### Plain button
Expand Down
5 changes: 5 additions & 0 deletions elements/pf-button/pf-button.css
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ pf-icon,
}
}

#button .anchor {
color: inherit;
text-decoration: none;
}

#text {
display: inline;
position: relative;
Expand Down
53 changes: 36 additions & 17 deletions elements/pf-button/pf-button.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,13 @@ export class PfButton extends LitElement {
/** Icon set for the `icon` property */
@property({ attribute: 'icon-set' }) iconSet?: string;

#internals = InternalsController.of(this, { role: 'button' });
/** Store the URL Link */
@property({ reflect: true }) href?: string;

/** Redirecting the URL Link to new Tab */
@property({ reflect: true }) target?: string;

#internals = InternalsController.of(this, { role: this.variant === 'link' ? 'none' : 'button' });

#slots = new SlotController(this, 'icon', null);

Expand All @@ -216,12 +222,19 @@ export class PfButton extends LitElement {
super.connectedCallback();
this.addEventListener('click', this.#onClick);
this.addEventListener('keydown', this.#onKeydown);
this.tabIndex = 0;
}

protected override willUpdate(): void {
this.#internals.ariaLabel = this.label || null;
this.#internals.ariaDisabled = String(!!this.disabled);
const isLink = this.variant === 'link' && this.href;
if (isLink) {
this.tabIndex = -1;
this.#internals.role = 'none';
} else {
this.tabIndex = 0;
this.#internals.role = 'button';
}
}

async formDisabledCallback(): Promise<void> {
Expand All @@ -231,8 +244,26 @@ export class PfButton extends LitElement {

override render(): TemplateResult<1> {
const hasIcon = !!this.icon || !!this.loading || this.#slots.hasSlotted('icon');
const { warning, variant, danger, loading, plain, inline, block, size } = this;
const { warning, variant, danger, loading, plain, inline, block, size, href, target } = this;

const disabled = this.#disabled;

const content = html`
<slot id="icon"
part="icon"
name="icon"
?hidden="${!hasIcon}">
<pf-icon role="presentation"
icon="${ifDefined(this.icon)}"
set="${ifDefined(this.iconSet)}"
?hidden="${!this.icon || this.loading}"></pf-icon>
<pf-spinner size="md"
?hidden="${!this.loading}"
aria-label="${this.getAttribute('loading-label') ?? 'loading'}"></pf-spinner>
</slot>
<slot id="text"></slot>
`;

return html`
<div id="button"
class="${classMap({
Expand All @@ -246,20 +277,8 @@ export class PfButton extends LitElement {
loading,
plain,
warning,
})}">
<slot id="icon"
part="icon"
name="icon"
?hidden="${!hasIcon}">
<pf-icon role="presentation"
icon="${ifDefined(this.icon)}"
set="${ifDefined(this.iconSet)}"
?hidden="${!this.icon || this.loading}"></pf-icon>
<pf-spinner size="md"
?hidden="${!this.loading}"
aria-label="${this.getAttribute('loading-label') ?? 'loading'}"></pf-spinner>
</slot>
<slot id="text"></slot>
})}">${!(variant === 'link' && href) ? content : html`
<a href="${href}" class="anchor" target="${ifDefined(target)}">${content}</a>`}
</div>
`;
}
Expand Down
Loading