Skip to content

Commit

Permalink
feat(utils): add new utilities and mixins (#222)
Browse files Browse the repository at this point in the history
  • Loading branch information
mimshins authored Oct 14, 2024
1 parent 6f38ca4 commit c242deb
Show file tree
Hide file tree
Showing 11 changed files with 364 additions and 40 deletions.
40 changes: 0 additions & 40 deletions src/utils.ts

This file was deleted.

19 changes: 19 additions & 0 deletions src/utils/debounce.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const debounce = <T extends (...args: any[]) => any>(
func: T,
delay: number,
): ((...args: Parameters<T>) => void) => {
let timeoutId: ReturnType<typeof setTimeout>;

return function (this: ThisParameterType<T>, ...args: Parameters<T>) {
// eslint-disable-next-line @typescript-eslint/no-this-alias
const context = this;

clearTimeout(timeoutId);
timeoutId = setTimeout(() => {
func.apply(context, args);
}, delay);
};
};

export default debounce;
17 changes: 17 additions & 0 deletions src/utils/events/BaseEvent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
abstract class BaseEvent<T> extends Event {
private _details?: T;

public get details(): T | undefined {
return this._details;
}

constructor(name: string, eventInit?: EventInit & { details?: T }) {
const { details, ...init } = eventInit ?? {};

super(name, init);

this._details = details;
}
}

export default BaseEvent;
2 changes: 2 additions & 0 deletions src/utils/events/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { default as BaseEvent } from "./BaseEvent";
export { default as redispatchEvent } from "./redispatch-event";
25 changes: 25 additions & 0 deletions src/utils/events/redispatch-event.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Re-dispatches an event from the provided element.
*
* cherry-picked from: https://github.com/material-components/material-web/blob/a9ee4f5bc1d6702e5dc352eefed13a1d849577e3/internal/events/redispatch-event.ts#L28
*/
const redispatchEvent = (element: Element, event: Event) => {
// For bubbling events in SSR light DOM (or composed), stop their propagation
// and dispatch the copy.
if (event.bubbles && (!element.shadowRoot || event.composed)) {
event.stopPropagation();
}

const newEvent = Reflect.construct(event.constructor, [
event.type,
event,
]) as Event;

const dispatched = element.dispatchEvent(newEvent);

if (!dispatched) event.preventDefault();

return dispatched;
};

export default redispatchEvent;
5 changes: 5 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export * from "./events";
export * from "./mixins";

export { default as debounce } from "./debounce";
export { default as logger } from "./logger";
20 changes: 20 additions & 0 deletions src/utils/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const logger = (
message: string,
scope: string,
type: "error" | "default" | "warning" = "default",
) => {
const typesMap: Record<typeof type, Console["log"]> = {
/* eslint-disable no-console */
error: console.error,
default: console.log,
warning: console.warn,
/* eslint-enable no-console */
};

const logFn = typesMap[type];
const completeMessage = `[TAPSI][${scope}]: ${message}`;

logFn(completeMessage);
};

export default logger;
2 changes: 2 additions & 0 deletions src/utils/mixins/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { default as withElementInternals } from "./with-element-internals";
export { default as withFormAssociated } from "./with-form-associated";
10 changes: 10 additions & 0 deletions src/utils/mixins/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export type MixinBase<ExpectedBase = object> = abstract new (
// eslint-disable-next-line @typescript-eslint/no-explicit-any
...args: any[]
) => ExpectedBase;

export type MixinReturn<MixinBase, MixinClass = object> = (abstract new (
// eslint-disable-next-line @typescript-eslint/no-explicit-any
...args: any[]
) => MixinClass) &
MixinBase;
31 changes: 31 additions & 0 deletions src/utils/mixins/with-element-internals.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { type LitElement } from "lit";
import type { MixinBase, MixinReturn } from "./types";

export const internals = Symbol("internals");

export interface WithElementInternals {
[internals]: ElementInternals;
}

const withElementInternals = <T extends MixinBase<LitElement>>(
BaseClass: T,
): MixinReturn<T, WithElementInternals> => {
abstract class WithElementInternalsClass
extends BaseClass
implements WithElementInternals
{
private _internals?: ElementInternals;

public get [internals](): ElementInternals {
if (!this._internals) {
this._internals = (this as HTMLElement).attachInternals();
}

return this._internals;
}
}

return WithElementInternalsClass;
};

export default withElementInternals;
Loading

0 comments on commit c242deb

Please sign in to comment.