UNPKG

10.4 kBTypeScriptView Raw
1/// <reference types="react" />
2import { onEventType } from "./helpers/onEvent";
3export interface AttributeMap {
4 /** One of default types with defined parse */
5 type: AttributeTypes;
6 /** Option[name] realted to related attribute. Point if attrName !== propName */
7 prop?: string;
8 /** Custom parser for related attribute */
9 parse?: (attrValue: string) => any;
10}
11export declare const enum AttributeTypes {
12 /** Value presented by `true`, `false` or '' (empty string) */
13 bool = 0,
14 number = 1,
15 string = 2,
16 reference = 3,
17 parsedObject = 4,
18 parseCustom = 5,
19 /** Element accessed via `document.querySelector` */
20 selector = 6
21}
22declare global {
23 /** Translate function that need to replace to translate content according to requirements
24 * @param text Text that must be translated
25 * @param type Type of related text
26 * @returns the same string by default */
27 const __wupln: Window["__wupln"];
28 interface Window {
29 /** Translate function that need to replace to translate content according to requirements
30 * @param text Text that must be translated
31 * @param type Type of related text
32 * @returns the same string by default */
33 __wupln: (text: string, type: "aria" | "content" | "validation") => string;
34 }
35}
36/** Basic abstract class for every component in web-ui-pack */
37export default abstract class WUPBaseElement<TOptions extends Record<string, any> = Record<string, any>, Events extends WUP.Base.EventMap = WUP.Base.EventMap> extends HTMLElement {
38 #private;
39 /** Register control in the web to allow to use */
40 static $use(): void;
41 /** Reference to global style element used by web-ui-pack */
42 static get $refStyle(): HTMLStyleElement | null;
43 static set $refStyle(v: HTMLStyleElement | null);
44 /** StyleContent related to component */
45 static get $style(): string;
46 /** StyleContent related to component & inherited components */
47 static get $styleRoot(): string;
48 /** Get unique id for html elements; Every getter returns new id */
49 static get $uniqueId(): string;
50 /** Returns default class name for visually hidden element */
51 static get classNameHidden(): string;
52 /** Returns default class name for buttons with icons */
53 static get classNameBtnIcon(): string;
54 /** Returns map-type based on value */
55 static mapAttribute(value: any): AttributeTypes;
56 /** Returns map-model based on $defaults for mapping attributes & options */
57 static get mappedAttributes(): Record<string, AttributeMap>;
58 /** Array of options names to listen for changes; @returns `undefined` if need to observe for every option
59 * @defaultValue every option from $defaults` */
60 static get observedOptions(): Array<string> | null;
61 /** Array of attribute names to listen for changes
62 * @defaultValue every option from $defaults (in lowerCase) */
63 static get observedAttributes(): Array<string>;
64 /** Global default options applied to every element. Change it to configure default behavior OR use `element.$options` to change per item */
65 static $defaults: Record<string, any>;
66 /** Used to clone single value (from defaults) */
67 static cloneValue<T>(v: T): T;
68 /** Used to clone defaults to options on init; override it to clone */
69 static cloneDefaults<T extends Record<string, any>>(): T;
70 /** Merge options with $defaults; Object.assign merges values 'undefined' by the method replace undefined with defaults */
71 static mergeDefaults<T extends Record<string, any>>(opts: T): T;
72 /** Raw part of $options for internal usage (.$options is Proxy object and better avoid useless extra-calles via Proxy) */
73 protected _opts: TOptions;
74 get $options(): Partial<TOptions>;
75 /** Options inherited from `static.$defaults` and applied to element. Use this to change behavior per item OR use `$defaults` to change globally */
76 set $options(v: Partial<TOptions>);
77 static findAllProtos(t: unknown, protos: typeof WUPBaseElement[]): typeof WUPBaseElement[];
78 constructor();
79 /** Returns true if element is appended (result of setTimeout on connectedCallback) */
80 get $isReady(): boolean;
81 /** Returns if current element or some nested child is active/focused */
82 get $isFocused(): boolean;
83 /** Try to focus self or first possible children; returns true if successful */
84 focus(): boolean;
85 /** Remove focus from element on any nested active element */
86 blur(): void;
87 /** Called when need to parse attribute */
88 parse(text: string): any;
89 /** Clear this to prevent autofocus */
90 _willFocus?: ReturnType<typeof setTimeout>;
91 /** Called when element is added to document (after empty timeout - at the end of call stack) */
92 protected gotReady(): void;
93 /** Called when element is removed from document */
94 protected gotRemoved(): void;
95 /** Called on Init and every time as options/attributes changed */
96 protected gotChanges(propsChanged: Array<string> | null): void;
97 /** Called when element isReady and at least one of observedOptions is changed */
98 protected gotOptionsChanged(e: WUP.Base.OptionEvent): void;
99 /** Called once on Init */
100 protected gotRender(): void;
101 /** Browser calls this method when the element is added to the document */
102 protected connectedCallback(): void;
103 /** Browser calls this method when the element is removed from the document;
104 * (can be called many times if an element is repeatedly added/removed) */
105 protected disconnectedCallback(): void;
106 /** Prevent gotChanges call during the some attribute or options custom changes */
107 _isStopChanges: boolean;
108 /** Called when any of observedAttributes is changed */
109 protected gotAttributeChanged(name: string, value: string | null): void;
110 /** Browser calls this method when attrs pointed in observedAttributes is changed */
111 protected attributeChangedCallback(name: string, oldValue: string | null, newValue: string | null): void;
112 addEventListener<K extends keyof Events>(type: K, listener: (this: WUPBaseElement, ev: Events[K]) => any, options?: boolean | AddEventListenerOptions): void;
113 addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void;
114 removeEventListener<K extends keyof Events>(type: K, listener: (this: WUPBaseElement, ev: Events[K]) => any, options?: boolean | EventListenerOptions): void;
115 removeEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | EventListenerOptions): void;
116 /** Inits customEvent & calls dispatchEvent and returns created event; by default `cancelable: false, bubbles: true`
117 * @tutorial Troubleshooting
118 * * Default event bubbling: el.event.click > el.onclick >>> parent.event.click > parent.onclick etc.
119 * * Custom event bubbling: el.$onclick > el.event.$click >>> parent.event.$click otherwise impossible to stop propagation from on['event] of target directly */
120 fireEvent<K extends keyof Events, T extends Events[K]["detail"]>(type: K, eventInit?: CustomEventInit<T>): Event;
121 /** Array of removeEventListener() that called on remove */
122 protected disposeLst: Array<() => void>;
123 /** Add event listener and remove after component removed; @options.passive=true by default */
124 appendEvent<T extends keyof E, K extends HTMLElement | Document, E extends HTMLElementEventMap & Record<string, Event>>(...args: Parameters<onEventType<T, K, E>>): () => void;
125 /** Remove events/functions that was appended */
126 protected dispose(): void;
127 /** Returns true if el is instance of Node and contains pointed element
128 * @tutorial Troubleshooting
129 * * if element has position `fixed` or `absolute` then returns false */
130 includes(el: unknown): boolean;
131 /** Returns true if element contains eventTarget or it's eventTarget
132 * @tutorial Troubleshooting
133 * * if element has position `fixed` or `absolute` then returns false */
134 includesTarget(e: Event): boolean;
135 /** Find parent/self according with callback */
136 findParent(callback: (el: HTMLElement) => boolean, options?: {
137 bubbleCount: number;
138 }): HTMLElement | null;
139 /** Returns true if contains pointed element or has itself
140 * @tutorial Troubleshooting
141 * * if element has position `fixed` or `absolute` then returns false */
142 itsMe(el: Element | EventTarget | null): boolean;
143 /** Returns parsed value according to pointed type OR current value if something wrong;
144 * override method for implementation custom parsing OR static method mappedAttributes to redefine map-types */
145 parseAttr(type: AttributeTypes, attrValue: string, propName: string, attrName: string): any;
146 /** Remove attr if value falseOrEmpty; set '' or 'true' if true for HTMLELement
147 * @param isSetEmpty set if need '' instead of 'value' */
148 setAttr(attr: string, v: boolean | string | undefined | null, isSetEmpty?: boolean): void;
149 /** Remove all children in fastest way */
150 removeChildren(): void;
151 /** Throws unhandled error via empty setTimeout */
152 throwError(err: string | Error | unknown): void;
153 /** Removes empty attr [style] */
154 removeEmptyStyle(): void;
155}
156declare global {
157 namespace WUP.Base {
158 /** Cast not-supported props to optional string */
159 type toJSX<T> = {
160 [P in keyof T]?: T[P] extends number | boolean | string | undefined ? T[P] : string;
161 };
162 type OnlyNames<T> = {
163 [K in keyof T as K extends string ? `w-${K}` : never]?: unknown;
164 };
165 type OptionEvent<T extends Record<string, any> = Record<string, any>> = {
166 props: Array<Extract<keyof T, string>>;
167 target: T;
168 };
169 type EventMap<T = HTMLElementEventMap> = HTMLElementEventMap & Record<keyof T, Event>;
170 type ReactHTML<T> = React.DetailedHTMLProps<Omit<React.HTMLAttributes<T>, "className"> & {
171 class?: string | undefined;
172 /** @deprecated isn't supported for React & WebComponents; use attr `class` instead
173 * @see {@link https://react.dev/reference/react-dom/components#custom-html-elements} */
174 className?: never;
175 }, T>;
176 type JSXProps<T> = React.DetailedHTMLProps<Omit<React.HTMLAttributes<T>, "className"> & {
177 class?: string | undefined;
178 }, T>;
179 }
180}