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(sbb-icon): provide compatibility with sbb-angular #2971

Merged
merged 1 commit into from
Jul 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions src/elements/icon/icon.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { PropertyValues } from 'lit';
import { customElement, property } from 'lit/decorators.js';
import { html, type PropertyValues, type TemplateResult } from 'lit';
import { customElement, property, state } from 'lit/decorators.js';

import { SbbIconBase } from './icon-base.js';

Expand All @@ -17,6 +17,13 @@

private _defaultAriaLabel = '';

/**
* The sbb-angular library has a sbb-icon component as well. In order to provide
* compatibility with it (as some icons are used internally inside the other sbb-angular
* components) we need to check whether the attribute svgicon is used.
*/
@state() private _sbbAngularCompatibility = false;

protected override async fetchSvgIcon(namespace: string, name: string): Promise<string> {
// If the icon is changing, and we were using the defaultAriaLabel, reset it
if (this.getAttribute('aria-label') === this._defaultAriaLabel) {
Expand All @@ -42,13 +49,29 @@
}
}

public override attributeChangedCallback(
name: string,
_old: string | null,
value: string | null,
): void {
if (name === 'svgicon') {
this._sbbAngularCompatibility = !!value;

Check warning on line 58 in src/elements/icon/icon.ts

View check run for this annotation

Codecov / codecov/patch

src/elements/icon/icon.ts#L58

Added line #L58 was not covered by tests
} else {
super.attributeChangedCallback(name, _old, value);
}
}

protected override firstUpdated(changedProperties: PropertyValues<this>): void {
super.firstUpdated(changedProperties);

if (!this.hasAttribute('aria-hidden')) {
this.setAttribute('aria-hidden', 'true');
}
}

protected override render(): TemplateResult {
return this._sbbAngularCompatibility ? html`<slot></slot>` : super.render();
}
}

declare global {
Expand Down
Loading