Skip to content

Commit

Permalink
refactor(sbb-title): extract base class (#2594)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeripeierSBB authored Apr 17, 2024
1 parent ffbcef7 commit 694bffe
Show file tree
Hide file tree
Showing 9 changed files with 139 additions and 136 deletions.
9 changes: 3 additions & 6 deletions src/components/dialog/dialog-title/dialog-title.scss
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
@use '../../core/styles' as sbb;

// Box-sizing rules contained in typography are not traversing Shadow DOM boundaries. We need to include box-sizing mixin in every component.
@include sbb.box-sizing;

:host {
--sbb-dialog-header-padding-block: var(--sbb-spacing-responsive-s) 0;
--sbb-dialog-title-padding-block: var(--sbb-spacing-responsive-s) 0;

display: contents;
}

:host([data-overflows]) {
--sbb-dialog-header-padding-block: var(--sbb-spacing-responsive-s);
--sbb-dialog-title-padding-block: var(--sbb-spacing-responsive-s);
}

.sbb-title {
Expand All @@ -28,7 +25,7 @@
align-items: start;
justify-content: space-between;
padding-inline: var(--sbb-dialog-padding-inline);
padding-block: var(--sbb-dialog-header-padding-block);
padding-block: var(--sbb-dialog-title-padding-block);
background-color: var(--sbb-dialog-background-color);
border-block-end: var(--sbb-dialog-title-border);
z-index: var(--sbb-dialog-z-index, var(--sbb-overlay-default-z-index));
Expand Down
10 changes: 3 additions & 7 deletions src/components/dialog/dialog-title/dialog-title.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { SbbLanguageController } from '../../core/controllers.js';
import type { Breakpoint } from '../../core/dom.js';
import { EventEmitter } from '../../core/eventing.js';
import { i18nCloseDialog, i18nGoBack } from '../../core/i18n.js';
import { SbbTitleElement } from '../../title.js';
import { SbbTitleBase } from '../../title.js';

import style from './dialog-title.scss?lit&inline';

Expand All @@ -19,14 +19,10 @@ import '../../button/transparent-button.js';
* It displays a title inside a dialog header.
*
* @event {CustomEvent<void>} requestBackAction - Emits whenever the back button is clicked.
* @cssprop --sbb-title-margin-block-start - This property is inherited from `SbbTitleElement`
* and is not relevant to dialog title margin customization.
* @cssprop --sbb-title-margin-block-end - This property is inherited from `SbbTitleElement`
* and is not relevant to dialog title margin customization.
*/
@customElement('sbb-dialog-title')
export class SbbDialogTitleElement extends SbbTitleElement {
public static override styles: CSSResultGroup = [SbbTitleElement.styles, style];
export class SbbDialogTitleElement extends SbbTitleBase {
public static override styles: CSSResultGroup = [SbbTitleBase.styles, style];
public static readonly events: Record<string, string> = {
backClick: 'requestBackAction',
} as const;
Expand Down
27 changes: 10 additions & 17 deletions src/components/dialog/dialog-title/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,30 +42,23 @@ If a back button is displayed it emits a `requestBackAction` event on click.

## Properties

| Name | Attribute | Privacy | Type | Default | Description |
| ------------------------- | --------------------------- | ------- | ---------------------------- | ------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `backButton` | `back-button` | public | `boolean` | `false` | Whether a back button is displayed next to the title. |
| `accessibilityCloseLabel` | `accessibility-close-label` | public | `\| string \| undefined` | | This will be forwarded as aria-label to the close button element. |
| `accessibilityBackLabel` | `accessibility-back-label` | public | `\| string \| undefined` | | This will be forwarded as aria-label to the back button element. |
| `hideOnScroll` | `hide-on-scroll` | public | `Breakpoint \| boolean` | `false` | Whether to hide the title up to a certain breakpoint. |
| `level` | `level` | public | `SbbTitleLevel` | `'2'` | Title level |
| `visualLevel` | `visual-level` | public | `SbbTitleLevel \| undefined` | `'3'` | Visual level for the title. Optional, if not set, the value of level will be used. |
| `negative` | `negative` | public | `boolean` | `false` | Negative coloring variant flag. |
| `visuallyHidden` | `visually-hidden` | public | `boolean \| undefined` | | Sometimes we need a title in the markup to present a proper hierarchy to the screen readers while we do not want to let that title appear visually. In this case we set visuallyHidden to true |
| Name | Attribute | Privacy | Type | Default | Description |
| ------------------------- | --------------------------- | ------- | ---------------------------- | ------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `backButton` | `back-button` | public | `boolean` | `false` | Whether a back button is displayed next to the title. |
| `accessibilityCloseLabel` | `accessibility-close-label` | public | `\| string \| undefined` | | This will be forwarded as aria-label to the close button element. |
| `accessibilityBackLabel` | `accessibility-back-label` | public | `\| string \| undefined` | | This will be forwarded as aria-label to the back button element. |
| `hideOnScroll` | `hide-on-scroll` | public | `Breakpoint \| boolean` | `false` | Whether to hide the title up to a certain breakpoint. |
| `level` | `level` | public | `SbbTitleLevel` | `'2'` | Title level |
| `visualLevel` | `visual-level` | public | `SbbTitleLevel \| undefined` | `'3'` | Visual level for the title. Optional, if not set, the value of level will be used. |
| `negative` | `negative` | public | `boolean` | `false` | Negative coloring variant flag. |
| `visuallyHidden` | `visually-hidden` | public | `boolean \| undefined` | | Sometimes we need a title in the markup to present a proper hierarchy to the screen readers while we do not want to let that title appear visually. In this case we set visuallyHidden to true. |

## Events

| Name | Type | Description | Inherited From |
| ------------------- | ------------------- | ------------------------------------------ | -------------- |
| `requestBackAction` | `CustomEvent<void>` | Emits whenever the back button is clicked. | |

## CSS Properties

| Name | Default | Description |
| -------------------------------- | --------------------------------- | ----------------------------------------------------------------------------------------------------------- |
| `--sbb-title-margin-block-start` | `var(--sbb-spacing-responsive-m)` | This property is inherited from `SbbTitleElement` and is not relevant to dialog title margin customization. |
| `--sbb-title-margin-block-end` | `var(--sbb-spacing-responsive-s)` | This property is inherited from `SbbTitleElement` and is not relevant to dialog title margin customization. |

## Slots

| Name | Description |
Expand Down
1 change: 1 addition & 0 deletions src/components/title.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './title/title.js';
export * from './title/title-base.js';
12 changes: 6 additions & 6 deletions src/components/title/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ If the title is needed, but it must to not be displayed, it's possible to use th

## Properties

| Name | Attribute | Privacy | Type | Default | Description |
| ---------------- | ----------------- | ------- | ---------------------------- | ------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `level` | `level` | public | `SbbTitleLevel` | `'1'` | Title level |
| `visualLevel` | `visual-level` | public | `SbbTitleLevel \| undefined` | | Visual level for the title. Optional, if not set, the value of level will be used. |
| `visuallyHidden` | `visually-hidden` | public | `boolean \| undefined` | | Sometimes we need a title in the markup to present a proper hierarchy to the screen readers while we do not want to let that title appear visually. In this case we set visuallyHidden to true |
| `negative` | `negative` | public | `boolean` | `false` | Negative coloring variant flag. |
| Name | Attribute | Privacy | Type | Default | Description |
| ---------------- | ----------------- | ------- | ---------------------------- | ------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `negative` | `negative` | public | `boolean` | `false` | Negative coloring variant flag. |
| `level` | `level` | public | `SbbTitleLevel` | `'1'` | Title level |
| `visualLevel` | `visual-level` | public | `SbbTitleLevel \| undefined` | | Visual level for the title. Optional, if not set, the value of level will be used. |
| `visuallyHidden` | `visually-hidden` | public | `boolean \| undefined` | | Sometimes we need a title in the markup to present a proper hierarchy to the screen readers while we do not want to let that title appear visually. In this case we set visuallyHidden to true. |

## CSS Properties

Expand Down
58 changes: 58 additions & 0 deletions src/components/title/title-base.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import type { CSSResultGroup, PropertyValues, TemplateResult } from 'lit';
import { LitElement } from 'lit';
import { property } from 'lit/decorators.js';
import { html, unsafeStatic } from 'lit/static-html.js';

import { hostAttributes } from '../core/decorators.js';
import { SbbNegativeMixin } from '../core/mixins.js';

import style from './title-common.scss?lit&inline';

export type SbbTitleLevel = '1' | '2' | '3' | '4' | '5' | '6';

/**
* It displays a title with a heading role.
*
* @slot - Use the unnamed slot to display the title.
*/
@hostAttributes({
role: 'heading',
})
export abstract class SbbTitleBase extends SbbNegativeMixin(LitElement) {
public static override styles: CSSResultGroup = style;

/** Title level */
@property({ reflect: true }) public level: SbbTitleLevel = '1';

/** Visual level for the title. Optional, if not set, the value of level will be used. */
@property({ attribute: 'visual-level', reflect: true })
public visualLevel?: SbbTitleLevel;

/**
* Sometimes we need a title in the markup to present a proper hierarchy
* to the screen readers while we do not want to let that title appear
* visually. In this case we set visuallyHidden to true.
*/
@property({ attribute: 'visually-hidden', reflect: true, type: Boolean })
public visuallyHidden?: boolean;

protected override willUpdate(changedProperties: PropertyValues): void {
super.willUpdate(changedProperties);

if (changedProperties.has('level')) {
this.setAttribute('aria-level', this.level);
}
}

protected override render(): TemplateResult {
const TAGNAME = `h${this.level}`;

/* eslint-disable lit/binding-positions */
return html`
<${unsafeStatic(TAGNAME)} class="sbb-title" role="presentation">
<slot></slot>
</${unsafeStatic(TAGNAME)}>
`;
/* eslint-enable lit/binding-positions */
}
}
53 changes: 53 additions & 0 deletions src/components/title/title-common.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
@use '../core/styles' as sbb;

// Box-sizing rules contained in typography are not traversing Shadow DOM boundaries. We need to include box-sizing mixin in every component.
@include sbb.box-sizing;

:host {
--sbb-title-text-color-normal: var(
--sbb-title-text-color-normal-override,
var(--sbb-color-charcoal)
);

display: block;
}

:host([negative]) {
@include sbb.title--negative;
}

:host([id]) {
@include sbb.scroll-margin-block-start;
}

.sbb-title {
color: var(--sbb-title-text-color-normal);

:host([visually-hidden]) & {
@include sbb.screen-reader-only;
}

:host(:is([level='1']:not([visual-level]), [visual-level='1'])) & {
@include sbb.title-1($exclude-spacing: true);
}

:host(:is([level='2']:not([visual-level]), [visual-level='2'])) & {
@include sbb.title-2($exclude-spacing: true);
}

:host(:is([level='3']:not([visual-level]), [visual-level='3'])) & {
@include sbb.title-3($exclude-spacing: true);
}

:host(:is([level='4']:not([visual-level]), [visual-level='4'])) & {
@include sbb.title-4($exclude-spacing: true);
}

:host(:is([level='5']:not([visual-level]), [visual-level='5'])) & {
@include sbb.title-5($exclude-spacing: true);
}

:host(:is([level='6']:not([visual-level]), [visual-level='6'])) & {
@include sbb.title-6($exclude-spacing: true);
}
}
48 changes: 0 additions & 48 deletions src/components/title/title.scss
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
@use '../core/styles' as sbb;

// Box-sizing rules contained in typography are not traversing Shadow DOM boundaries. We need to include box-sizing mixin in every component.
@include sbb.box-sizing;

:host {
--sbb-title-text-color-normal: var(
--sbb-title-text-color-normal-override,
var(--sbb-color-charcoal)
);
--sbb-title-margin-block-start: 0;
--sbb-title-margin-block-end: 0;

display: block;
margin-block: var(--sbb-title-margin-block-start) var(--sbb-title-margin-block-end);
}

Expand Down Expand Up @@ -45,43 +37,3 @@
:host(:where([level='6']:not([visual-level]), [visual-level='6']):where(:not([visually-hidden]))) {
--sbb-title-margin-block-end: var(--sbb-spacing-fixed-1x);
}

:host([negative]) {
@include sbb.title--negative;
}

:host([id]) {
@include sbb.scroll-margin-block-start;
}

.sbb-title {
color: var(--sbb-title-text-color-normal);

:host([visually-hidden]) & {
@include sbb.screen-reader-only;
}

:host(:is([level='1']:not([visual-level]), [visual-level='1'])) & {
@include sbb.title-1($exclude-spacing: true);
}

:host(:is([level='2']:not([visual-level]), [visual-level='2'])) & {
@include sbb.title-2($exclude-spacing: true);
}

:host(:is([level='3']:not([visual-level]), [visual-level='3'])) & {
@include sbb.title-3($exclude-spacing: true);
}

:host(:is([level='4']:not([visual-level]), [visual-level='4'])) & {
@include sbb.title-4($exclude-spacing: true);
}

:host(:is([level='5']:not([visual-level]), [visual-level='5'])) & {
@include sbb.title-5($exclude-spacing: true);
}

:host(:is([level='6']:not([visual-level]), [visual-level='6'])) & {
@include sbb.title-6($exclude-spacing: true);
}
}
Loading

0 comments on commit 694bffe

Please sign in to comment.