Skip to content

Commit

Permalink
fix: review pt.6 - create common method for icon rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
DavideMininni-Fincons committed Jan 30, 2024
1 parent 9d4d702 commit 8b9d0f9
Show file tree
Hide file tree
Showing 21 changed files with 66 additions and 72 deletions.
6 changes: 1 addition & 5 deletions src/components/button/button-link/button-link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,7 @@ export class SbbButtonLinkElement extends SbbButtonCommonElementMixin(SbbLinkBas
/* eslint-disable lit/binding-positions */
return html`
<${unsafeStatic(TAG_NAME)} class="sbb-button" ${spread(attributes)}>
<span class="sbb-button__icon">
<slot name="icon">
${this.iconName ? html`<sbb-icon name="${this.iconName}"></sbb-icon>` : nothing}
</slot>
</span>
${this.renderIconSlot()}
<span class="sbb-button__label">
<slot></slot>
${
Expand Down
2 changes: 1 addition & 1 deletion src/components/button/button-link/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ Use the accessibility properties in case of an icon-only button to describe the
| `size` | `size` | public | `SbbButtonSize \| undefined` | `'l'` | Size variant, either l or m. |
| `isStatic` | `is-static` | public | `boolean` | `false` | Set this property to true if you want only a visual representation of a button, but no interaction (a span instead of a link/button will be rendered). |
| `negative` | `negative` | public | `boolean` | `false` | Negative coloring variant flag. |
| `disabled` | `disabled` | public | `boolean \| undefined` | `false` | Whether the button is disabled. |
| `disabled` | `disabled` | public | `boolean` | `false` | Whether the button is disabled. |
| `iconName` | `icon-name` | public | `string \| undefined` | | The icon name we want to use, choose from the small icon variants from the ui-icons category from here https://icons.app.sbb.ch. |
| `href` | `href` | public | `string \| undefined` | | The href value you want to link to. |
| `target` | `target` | public | `LinkTargetType \| string \| undefined` | | Where to display the linked URL. |
Expand Down
9 changes: 2 additions & 7 deletions src/components/button/button/button.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { nothing, type TemplateResult } from 'lit';
import type { TemplateResult } from 'lit';
import { customElement } from 'lit/decorators.js';
import { html } from 'lit/static-html.js';

Expand All @@ -24,12 +24,7 @@ export class SbbButtonElement extends SbbButtonCommonElementMixin(SbbButtonBaseE

return html`
<span class="sbb-button">
<span class="sbb-button__icon">
<slot name="icon">
${this.iconName ? html`<sbb-icon name="${this.iconName}"></sbb-icon>` : nothing}
</slot>
</span>
${this.renderIconSlot()}
<span class="sbb-button__label">
<slot></slot>
</span>
Expand Down
2 changes: 1 addition & 1 deletion src/components/button/button/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ Use the accessibility properties in case of an icon-only button to describe the
| `size` | `size` | public | `SbbButtonSize \| undefined` | `'l'` | Size variant, either l or m. |
| `isStatic` | `is-static` | public | `boolean` | `false` | Set this property to true if you want only a visual representation of a button, but no interaction (a span instead of a link/button will be rendered). |
| `negative` | `negative` | public | `boolean` | `false` | Negative coloring variant flag. |
| `disabled` | `disabled` | public | `boolean \| undefined` | `false` | Whether the button is disabled. |
| `disabled` | `disabled` | public | `boolean` | `false` | Whether the button is disabled. |
| `iconName` | `icon-name` | public | `string \| undefined` | | The icon name we want to use, choose from the small icon variants from the ui-icons category from here https://icons.app.sbb.ch. |
| `type` | `type` | public | `ButtonType \| undefined` | | The type attribute to use for the button. |
| `name` | `name` | public | `string \| undefined` | | The name attribute to use for the button. |
Expand Down
17 changes: 11 additions & 6 deletions src/components/button/common/button-common.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { CSSResultGroup, LitElement } from 'lit';
import { type CSSResultGroup, html, type LitElement, type TemplateResult } from 'lit';
import { property } from 'lit/decorators.js';

import type {
AbstractConstructor,
SbbDisabledMixinType,
SbbIconNameMixinType,
SbbNegativeMixinType,
import {
type AbstractConstructor,
type SbbDisabledMixinType,
type SbbIconNameMixinType,
type SbbNegativeMixinType,
} from '../../core/common-behaviors';
import {
NamedSlotStateController,
Expand Down Expand Up @@ -33,6 +33,7 @@ export declare class SbbButtonCommonElementMixinType
public disabled: boolean;
public iconName: string;
public negative: boolean;
public renderIconSlot: () => TemplateResult;
}

// eslint-disable-next-line @typescript-eslint/naming-convention
Expand Down Expand Up @@ -80,6 +81,10 @@ export const SbbButtonCommonElementMixin = <T extends AbstractConstructor<LitEle
super.disconnectedCallback();
this._handlerRepository.disconnect();
}

public override renderIconSlot(): TemplateResult {
return html` <span class="sbb-button__icon"> ${super.renderIconSlot()} </span> `;
}
}
return SbbButtonCommonElement as unknown as AbstractConstructor<SbbButtonCommonElementMixinType> &
T;
Expand Down
4 changes: 2 additions & 2 deletions src/components/core/common-behaviors/disabled-mixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ export declare class SbbDisabledMixinType {
export const SbbDisabledMixin = <T extends AbstractConstructor<LitElement>>(
superClass: T,
): AbstractConstructor<SbbDisabledMixinType> & T => {
abstract class SbbDisabled extends superClass implements Partial<SbbDisabledMixinType> {
abstract class SbbDisabled extends superClass implements SbbDisabledMixinType {
/** Whether the button is disabled. */
@property({ reflect: true, type: Boolean }) public disabled?: boolean = false;
@property({ reflect: true, type: Boolean }) public disabled: boolean = false;
}

return SbbDisabled as AbstractConstructor<SbbDisabledMixinType> & T;
Expand Down
18 changes: 15 additions & 3 deletions src/components/core/common-behaviors/icon-name-mixin.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,35 @@
import type { LitElement } from 'lit';
import { html, type LitElement, nothing, type TemplateResult } from 'lit';
import { property } from 'lit/decorators.js';

import type { AbstractConstructor } from './constructor';

export declare class SbbIconNameMixinType {
public iconName: string;
public iconName?: string;
public renderIconSlot(): TemplateResult;
}

// eslint-disable-next-line @typescript-eslint/naming-convention
export const SbbIconNameMixin = <T extends AbstractConstructor<LitElement>>(
superClass: T,
): AbstractConstructor<SbbIconNameMixinType> & T => {
abstract class SbbIconName extends superClass implements Partial<SbbIconNameMixinType> {
abstract class SbbIconName extends superClass implements SbbIconNameMixinType {
/**
* The icon name we want to use, choose from the small icon variants
* from the ui-icons category from here
* https://icons.app.sbb.ch.
*/
@property({ attribute: 'icon-name', reflect: true }) public iconName?: string;

/**
* @private
*/
public renderIconSlot(): TemplateResult {
return html`
<slot name="icon">
${this.iconName ? html`<sbb-icon name="${this.iconName}"></sbb-icon>` : nothing}
</slot>
`;
}
}

return SbbIconName as AbstractConstructor<SbbIconNameMixinType> & T;
Expand Down
4 changes: 2 additions & 2 deletions src/components/core/common-behaviors/negative-mixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ export declare class SbbNegativeMixinType {
export const SbbNegativeMixin = <T extends AbstractConstructor<LitElement>>(
superClass: T,
): AbstractConstructor<SbbNegativeMixinType> & T => {
abstract class SbbNegative extends superClass implements Partial<SbbNegativeMixinType> {
abstract class SbbNegative extends superClass implements SbbNegativeMixinType {
/** Negative coloring variant flag. */
@property({ reflect: true, type: Boolean }) public negative = false;
@property({ reflect: true, type: Boolean }) public negative: boolean = false;
}

return SbbNegative as AbstractConstructor<SbbNegativeMixinType> & T;
Expand Down
10 changes: 8 additions & 2 deletions src/components/header/common/header-action-common.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { html, type TemplateResult } from 'lit';
import { property } from 'lit/decorators.js';
import type { CSSResultGroup, LitElement } from 'lit/development';

Expand All @@ -13,6 +14,7 @@ import style from './header-action.scss?lit&inline';
export declare class SbbHeaderActionCommonElementMixinType implements SbbIconNameMixinType {
public expandFrom: SbbHorizontalFrom;
public iconName: string;
public renderIconSlot(): TemplateResult;
}

// eslint-disable-next-line @typescript-eslint/naming-convention
Expand Down Expand Up @@ -44,6 +46,10 @@ export const SbbHeaderActionCommonElementMixin = <T extends AbstractConstructor<

private _handlerRepository = new HandlerRepository(this, actionElementHandlerAspect);

private _updateExpanded(): void {
toggleDatasetEntry(this, 'expanded', !isBreakpoint('zero', this.expandFrom));
}

public override connectedCallback(): void {
super.connectedCallback();
this._documentResizeObserver.observe(document.documentElement);
Expand All @@ -57,8 +63,8 @@ export const SbbHeaderActionCommonElementMixin = <T extends AbstractConstructor<
this._handlerRepository.disconnect();
}

private _updateExpanded(): void {
toggleDatasetEntry(this, 'expanded', !isBreakpoint('zero', this.expandFrom));
public override renderIconSlot(): TemplateResult {
return html` <span class="sbb-header-action__icon"> ${super.renderIconSlot()} </span> `;
}
}
return SbbHeaderActionCommonElement as unknown as AbstractConstructor<SbbHeaderActionCommonElementMixinType> &
Expand Down
8 changes: 2 additions & 6 deletions src/components/header/header-button/header-button.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { nothing, type TemplateResult } from 'lit';
import type { TemplateResult } from 'lit';
import { customElement } from 'lit/decorators.js';
import { html } from 'lit/static-html.js';

Expand All @@ -23,11 +23,7 @@ export class SbbHeaderButtonElement extends SbbHeaderActionCommonElementMixin(
return html`
<span class="sbb-header-action">
<span class="sbb-header-action__wrapper">
<span class="sbb-header-action__icon">
<slot name="icon">
${this.iconName ? html`<sbb-icon name="${this.iconName}"></sbb-icon>` : nothing}
</slot>
</span>
${this.renderIconSlot()}
<span class="sbb-header-action__text">
<slot></slot>
</span>
Expand Down
6 changes: 1 addition & 5 deletions src/components/header/header-link/header-link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,7 @@ export class SbbHeaderLinkElement extends SbbHeaderActionCommonElementMixin(SbbL
return html`
<a class="sbb-header-action" ${spread(attributes)}>
<span class="sbb-header-action__wrapper">
<span class="sbb-header-action__icon">
<slot name="icon">
${this.iconName ? html`<sbb-icon name="${this.iconName}"></sbb-icon>` : nothing}
</slot>
</span>
${this.renderIconSlot()}
<span class="sbb-header-action__text">
<slot></slot>
${targetsNewWindow(this)
Expand Down
7 changes: 6 additions & 1 deletion src/components/link/common/link-common.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { CSSResultGroup, LitElement } from 'lit';
import { type CSSResultGroup, html, type LitElement, type TemplateResult } from 'lit';
import { property } from 'lit/decorators.js';

import type {
Expand Down Expand Up @@ -32,6 +32,7 @@ export declare class SbbLinkCommonElementMixinType
public disabled: boolean;
public iconName: string;
public negative: boolean;
public renderIconSlot(): TemplateResult;
}

// eslint-disable-next-line @typescript-eslint/naming-convention
Expand Down Expand Up @@ -81,6 +82,10 @@ export const SbbLinkCommonElementMixin = <T extends AbstractConstructor<LitEleme
super.disconnectedCallback();
this._handlerRepository.disconnect();
}

public override renderIconSlot(): TemplateResult {
return html` <span class="sbb-link__icon"> ${super.renderIconSlot()} </span> `;
}
}
return SbbLinkCommonElement as unknown as AbstractConstructor<SbbLinkCommonElementMixinType> & T;
};
8 changes: 1 addition & 7 deletions src/components/link/link-button/link-button.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,7 @@ export class SbbLinkButtonElement extends SbbLinkCommonElementMixin(SbbButtonBas
/* eslint-disable lit/binding-positions */
return html`
<span class="sbb-link">
${this.variant !== 'inline'
? html`<span class="sbb-link__icon">
<slot name="icon">
${this.iconName ? html` <sbb-icon name="${this.iconName}"></sbb-icon>` : nothing}
</slot>
</span>`
: nothing}
${this.variant !== 'inline' ? this.renderIconSlot() : nothing}
<slot></slot>
</span>
`;
Expand Down
2 changes: 1 addition & 1 deletion src/components/link/link-button/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ and it has also three sizes (`xs`, `s`, which is the default, and `m`) that are
| `isStatic` | `is-static` | public | `boolean` | `false` | Set this property to true if you want only a visual representation of a link, but no interaction (a span instead of a link/button will be rendered). |
| `iconPlacement` | `icon-placement` | public | `SbbIconPlacement \| undefined` | `'start'` | Moves the icon to the end of the component if set to true. |
| `negative` | `negative` | public | `boolean` | `false` | Negative coloring variant flag. |
| `disabled` | `disabled` | public | `boolean \| undefined` | `false` | Whether the button is disabled. |
| `disabled` | `disabled` | public | `boolean` | `false` | Whether the button is disabled. |
| `iconName` | `icon-name` | public | `string \| undefined` | | The icon name we want to use, choose from the small icon variants from the ui-icons category from here https://icons.app.sbb.ch. |
| `type` | `type` | public | `ButtonType \| undefined` | | The type attribute to use for the button. |
| `name` | `name` | public | `string \| undefined` | | The name attribute to use for the button. |
Expand Down
10 changes: 1 addition & 9 deletions src/components/link/link/link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,7 @@ export class SbbLinkElement extends SbbLinkCommonElementMixin(SbbLinkBaseElement
/* eslint-disable lit/binding-positions */
return html`
<${unsafeStatic(TAG_NAME)} class='sbb-link' ${spread(attributes)}>
${
this.variant !== 'inline'
? html`<span class="sbb-link__icon">
<slot name="icon">
${this.iconName ? html` <sbb-icon name="${this.iconName}"></sbb-icon>` : nothing}
</slot>
</span>`
: nothing
}
${this.variant !== 'inline' ? this.renderIconSlot() : nothing}
<slot></slot>
${
targetsNewWindow(this)
Expand Down
2 changes: 1 addition & 1 deletion src/components/link/link/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ and it has also three sizes (`xs`, `s`, which is the default, and `m`) that are
| `isStatic` | `is-static` | public | `boolean` | `false` | Set this property to true if you want only a visual representation of a link, but no interaction (a span instead of a link/button will be rendered). |
| `iconPlacement` | `icon-placement` | public | `SbbIconPlacement \| undefined` | `'start'` | Moves the icon to the end of the component if set to true. |
| `negative` | `negative` | public | `boolean` | `false` | Negative coloring variant flag. |
| `disabled` | `disabled` | public | `boolean \| undefined` | `false` | Whether the button is disabled. |
| `disabled` | `disabled` | public | `boolean` | `false` | Whether the button is disabled. |
| `iconName` | `icon-name` | public | `string \| undefined` | | The icon name we want to use, choose from the small icon variants from the ui-icons category from here https://icons.app.sbb.ch. |
| `href` | `href` | public | `string \| undefined` | | The href value you want to link to. |
| `target` | `target` | public | `LinkTargetType \| string \| undefined` | | Where to display the linked URL. |
Expand Down
7 changes: 6 additions & 1 deletion src/components/menu/common/menu-action-common.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { CSSResultGroup, LitElement } from 'lit';
import { type CSSResultGroup, html, type LitElement, type TemplateResult } from 'lit';
import { property } from 'lit/decorators.js';

import type {
Expand All @@ -17,6 +17,7 @@ export declare class SbbMenuActionCommonElementMixinType
public amount: string;
public disabled: boolean;
public iconName: string;
public renderIconSlot(): TemplateResult;
}

// eslint-disable-next-line @typescript-eslint/naming-convention
Expand All @@ -43,6 +44,10 @@ export const SbbMenuActionCommonElementMixin = <T extends AbstractConstructor<Li
super.disconnectedCallback();
this._handlerRepository.disconnect();
}

public override renderIconSlot(): TemplateResult {
return html` <span class="sbb-menu-action__icon"> ${super.renderIconSlot()} </span> `;
}
}
return SbbMenuActionCommonElement as unknown as AbstractConstructor<SbbMenuActionCommonElementMixinType> &
T;
Expand Down
6 changes: 1 addition & 5 deletions src/components/menu/menu-button/menu-button.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,7 @@ export class SbbMenuButtonElement extends SbbMenuActionCommonElementMixin(SbbBut
return html`
<span class="sbb-menu-action">
<span class="sbb-menu-action__content">
<span class="sbb-menu-action__icon">
<slot name="icon"
>${this.iconName ? html`<sbb-icon name=${this.iconName}></sbb-icon>` : nothing}</slot
>
</span>
${this.renderIconSlot()}
<span class="sbb-menu-action__label">
<slot></slot>
</span>
Expand Down
Loading

0 comments on commit 8b9d0f9

Please sign in to comment.