Skip to content

Commit

Permalink
refactor: rename forwardEventToHost to forwardEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
jeripeierSBB committed Dec 18, 2024
1 parent 5a6f71a commit 053e699
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 31 deletions.
15 changes: 11 additions & 4 deletions src/elements/core/eventing/forward-event.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
/**
* Forwards an event to the host element provided.
* This way, an event triggered in the ShadowDOM can cross its boundary and can be listened on the host component.
* Forwards an event to the element provided.
* This way, an event triggered in the Shadow DOM can cross its boundary and can be listened on e.g. the host component.
*/
export function forwardEventToHost(event: Event, host: HTMLElement | Document): void {
export function forwardEvent(event: Event, element: HTMLElement | Document): void {
const eventConstructor = Object.getPrototypeOf(event).constructor;
const copiedEvent: Event = new eventConstructor(event.type, event);
host.dispatchEvent(copiedEvent);
element.dispatchEvent(copiedEvent);
}

/**
* Forwards an event to the host element provided.
* This way, an event triggered in the ShadowDOM can cross its boundary and can be listened on the host component.
* @deprecated will be removed with next major version, use forwardEvent as alternative
*/
export const forwardEventToHost = forwardEvent;
4 changes: 2 additions & 2 deletions src/elements/datepicker/datepicker/datepicker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { SbbConnectedAbortController, SbbLanguageController } from '../../core/c
import { type DateAdapter, defaultDateAdapter } from '../../core/datetime.js';
import { forceType } from '../../core/decorators.js';
import { findInput, findReferencedElement } from '../../core/dom.js';
import { EventEmitter, forwardEventToHost } from '../../core/eventing.js';
import { EventEmitter, forwardEvent } from '../../core/eventing.js';
import { i18nDateChangedTo, i18nDatePickerPlaceholder } from '../../core/i18n.js';
import type { SbbDateLike, SbbValidationChangeEvent } from '../../core/interfaces.js';
import type { SbbDatepickerButton } from '../common.js';
Expand Down Expand Up @@ -243,7 +243,7 @@ class SbbDatepickerElement<T = Date> extends LitElement {
input.addEventListener(
'input',
(e) => {
forwardEventToHost(e, this);
forwardEvent(e, this);
this._parseInput();
},
options,
Expand Down
4 changes: 2 additions & 2 deletions src/elements/dialog/dialog-content/dialog-content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { CSSResultGroup, TemplateResult } from 'lit';
import { html, LitElement } from 'lit';
import { customElement } from 'lit/decorators.js';

import { forwardEventToHost } from '../../core/eventing.js';
import { forwardEvent } from '../../core/eventing.js';

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

Expand All @@ -18,7 +18,7 @@ class SbbDialogContentElement extends LitElement {

protected override render(): TemplateResult {
return html`
<div class="sbb-dialog-content" @scroll=${(e: Event) => forwardEventToHost(e, document)}>
<div class="sbb-dialog-content" @scroll=${(e: Event) => forwardEvent(e, document)}>
<slot></slot>
</div>
`;
Expand Down
4 changes: 2 additions & 2 deletions src/elements/file-selector/common/file-selector-common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { sbbInputModalityDetector } from '../../core/a11y.js';
import { SbbLanguageController } from '../../core/controllers.js';
import { forceType } from '../../core/decorators.js';
import { isLean } from '../../core/dom.js';
import { EventEmitter, forwardEventToHost } from '../../core/eventing.js';
import { EventEmitter, forwardEvent } from '../../core/eventing.js';
import {
i18nFileSelectorButtonLabel,
i18nFileSelectorCurrentlySelected,
Expand Down Expand Up @@ -183,7 +183,7 @@ export const SbbFileSelectorCommonElementMixin = <T extends Constructor<LitEleme
if (fileInput.files) {
this.createFileList(fileInput.files);
}
forwardEventToHost(event, this);
forwardEvent(event, this);
}

protected createFileList(files: FileList): void {
Expand Down
7 changes: 2 additions & 5 deletions src/elements/map-container/map-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { customElement, property, state } from 'lit/decorators.js';

import { SbbLanguageController } from '../core/controllers.js';
import { forceType } from '../core/decorators.js';
import { forwardEventToHost } from '../core/eventing.js';
import { forwardEvent } from '../core/eventing.js';
import { i18nMapContainerButtonLabel } from '../core/i18n.js';

import style from './map-container.scss?lit&inline';
Expand Down Expand Up @@ -97,10 +97,7 @@ class SbbMapContainerElement extends LitElement {
<div class="sbb-map-container__map">
<slot name="map"></slot>
</div>
<div
class="sbb-map-container__sidebar"
@scroll=${(e: Event) => forwardEventToHost(e, document)}
>
<div class="sbb-map-container__sidebar" @scroll=${(e: Event) => forwardEvent(e, document)}>
<span id="intersector"></span>
<slot></slot>
Expand Down
4 changes: 2 additions & 2 deletions src/elements/menu/menu/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
isZeroAnimationDuration,
SbbScrollHandler,
} from '../../core/dom.js';
import { forwardEventToHost } from '../../core/eventing.js';
import { forwardEvent } from '../../core/eventing.js';
import { SbbNamedSlotListMixin } from '../../core/mixins.js';
import {
getElementPosition,
Expand Down Expand Up @@ -415,7 +415,7 @@ class SbbMenuElement extends SbbNamedSlotListMixin<
>
<div
@click=${(event: Event) => this._closeOnInteractiveElementClick(event)}
@scroll=${(e: Event) => forwardEventToHost(e, document)}
@scroll=${(e: Event) => forwardEvent(e, document)}
class="sbb-menu__content"
>
${this.listChildren.length
Expand Down
7 changes: 2 additions & 5 deletions src/elements/overlay/overlay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { html, unsafeStatic } from 'lit/static-html.js';
import { getFirstFocusableElement, setModalityOnNextFocus } from '../core/a11y.js';
import { forceType } from '../core/decorators.js';
import { isZeroAnimationDuration } from '../core/dom.js';
import { EventEmitter, forwardEventToHost } from '../core/eventing.js';
import { EventEmitter, forwardEvent } from '../core/eventing.js';
import { i18nCloseDialog, i18nGoBack } from '../core/i18n.js';

import { overlayRefs, SbbOverlayBaseElement } from './overlay-base-element.js';
Expand Down Expand Up @@ -201,10 +201,7 @@ class SbbOverlayElement extends SbbOverlayBaseElement {
<div class="sbb-overlay__header">
${this.backButton ? backButton : nothing} ${closeButton}
</div>
<div
class="sbb-overlay__content"
@scroll=${(e: Event) => forwardEventToHost(e, document)}
>
<div class="sbb-overlay__content" @scroll=${(e: Event) => forwardEvent(e, document)}>
<sbb-container
class="sbb-overlay__content-container"
?expanded=${this.expanded}
Expand Down
4 changes: 2 additions & 2 deletions src/elements/slider/slider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { styleMap } from 'lit/directives/style-map.js';

import { SbbConnectedAbortController } from '../core/controllers.js';
import { forceType, hostAttributes } from '../core/decorators.js';
import { EventEmitter, forwardEventToHost } from '../core/eventing.js';
import { EventEmitter, forwardEvent } from '../core/eventing.js';
import {
type FormRestoreReason,
type FormRestoreState,
Expand Down Expand Up @@ -252,7 +252,7 @@ class SbbSliderElement extends SbbDisabledMixin(SbbFormAssociatedMixin(LitElemen

/** Emits the change event. */
private _emitChange(event: Event): void {
forwardEventToHost(event, this);
forwardEvent(event, this);
this._didChange.emit();
}

Expand Down
12 changes: 5 additions & 7 deletions src/elements/time-input/time-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { ref } from 'lit/directives/ref.js';

import { SbbLanguageController } from '../core/controllers.js';
import { findInput } from '../core/dom.js';
import { EventEmitter, forwardEventToHost } from '../core/eventing.js';
import { EventEmitter, forwardEvent } from '../core/eventing.js';
import { i18nTimeInputChange } from '../core/i18n.js';
import type { SbbDateLike, SbbValidationChangeEvent } from '../core/interfaces.js';

Expand Down Expand Up @@ -132,11 +132,9 @@ class SbbTimeInputElement extends LitElement {
this._inputElement.placeholder = 'HH:MM';
}

this._inputElement.addEventListener(
'input',
(event: Event) => forwardEventToHost(event, this),
{ signal: this._abortController.signal },
);
this._inputElement.addEventListener('input', (event: Event) => forwardEvent(event, this), {
signal: this._abortController.signal,
});
this._inputElement.addEventListener(
'keydown',
(event: KeyboardEvent) => this._preventCharInsert(event),
Expand Down Expand Up @@ -196,7 +194,7 @@ class SbbTimeInputElement extends LitElement {

/** Emits the change event. */
private _emitChange(event: Event): void {
forwardEventToHost(event, this);
forwardEvent(event, this);
this._didChange.emit();
}

Expand Down

0 comments on commit 053e699

Please sign in to comment.