@trunkjs/browser-utils 1.0.43 → 1.0.48
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.ai-usage-info.md +180 -0
- package/CHANGELOG.md +20 -0
- package/index.d.ts +39 -0
- package/index.js +152 -103
- package/lib/wait-for.d.ts +9 -0
- package/mixins/LoaderMixin.d.ts +6 -0
- package/package.json +6 -1
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
# @trunkjs/browser-utils
|
|
2
|
+
|
|
3
|
+
## Purpose
|
|
4
|
+
|
|
5
|
+
Small browser-side helpers for DOM work, timing, events, storage, and custom-element mixins.
|
|
6
|
+
|
|
7
|
+
Use this package when you need:
|
|
8
|
+
- small DOM helpers
|
|
9
|
+
- debounce / wait helpers
|
|
10
|
+
- localStorage / sessionStorage state
|
|
11
|
+
- logging in custom elements
|
|
12
|
+
- responsive or loader-related custom-element mixins
|
|
13
|
+
|
|
14
|
+
Do **not** use it for server-only code.
|
|
15
|
+
|
|
16
|
+
## Import
|
|
17
|
+
|
|
18
|
+
```ts
|
|
19
|
+
import { create_element, Debouncer, waitForLoad } from '@trunkjs/browser-utils';
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Common examples
|
|
23
|
+
|
|
24
|
+
### Create DOM nodes
|
|
25
|
+
|
|
26
|
+
```ts
|
|
27
|
+
import { create_element } from '@trunkjs/browser-utils';
|
|
28
|
+
|
|
29
|
+
const el = create_element('button', { class: 'primary', disabled: true }, 'Save');
|
|
30
|
+
document.body.append(el);
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
### Debounce user input
|
|
34
|
+
|
|
35
|
+
```ts
|
|
36
|
+
import { Debouncer } from '@trunkjs/browser-utils';
|
|
37
|
+
|
|
38
|
+
const debouncer = new Debouncer(300, 2000);
|
|
39
|
+
|
|
40
|
+
input.addEventListener('input', async () => {
|
|
41
|
+
await debouncer.wait();
|
|
42
|
+
search(input.value);
|
|
43
|
+
});
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
### Wait for browser events
|
|
47
|
+
|
|
48
|
+
```ts
|
|
49
|
+
import { waitFor, waitForLoad, sleep, waitForAnimationEnd } from '@trunkjs/browser-utils';
|
|
50
|
+
|
|
51
|
+
await waitForLoad();
|
|
52
|
+
await waitFor(button, 'click');
|
|
53
|
+
await sleep(200);
|
|
54
|
+
await waitForAnimationEnd(dialog);
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### Persistent state via storage proxy
|
|
58
|
+
|
|
59
|
+
```ts
|
|
60
|
+
import { local_storage, session_storage } from '@trunkjs/browser-utils';
|
|
61
|
+
|
|
62
|
+
const prefs = local_storage('prefs', { theme: 'light', debug: false });
|
|
63
|
+
prefs.theme = 'dark';
|
|
64
|
+
|
|
65
|
+
const draft = session_storage('draft', { text: '' });
|
|
66
|
+
draft.text = 'hello';
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
### Logging inside custom elements
|
|
70
|
+
|
|
71
|
+
```ts
|
|
72
|
+
import { LoggingMixin } from '@trunkjs/browser-utils';
|
|
73
|
+
|
|
74
|
+
class MyEl extends LoggingMixin(HTMLElement) {
|
|
75
|
+
connectedCallback() {
|
|
76
|
+
this.log('connected');
|
|
77
|
+
this.warn('always visible');
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
```html
|
|
83
|
+
<my-el debug></my-el>
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
### Auto-bind events in custom elements
|
|
87
|
+
|
|
88
|
+
```ts
|
|
89
|
+
import { EventBindingsMixin, Listen } from '@trunkjs/browser-utils';
|
|
90
|
+
|
|
91
|
+
class MyEl extends EventBindingsMixin(HTMLElement) {
|
|
92
|
+
@Listen('click', { target: 'host' })
|
|
93
|
+
onClick() {
|
|
94
|
+
console.log('clicked');
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
@Listen('resize', { target: 'window' })
|
|
98
|
+
onResize() {
|
|
99
|
+
console.log('resized');
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
### Responsive custom element mode
|
|
105
|
+
|
|
106
|
+
```ts
|
|
107
|
+
import { BreakPointMixin } from '@trunkjs/browser-utils';
|
|
108
|
+
|
|
109
|
+
class MyEl extends BreakPointMixin(HTMLElement) {}
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
```css
|
|
113
|
+
:host {
|
|
114
|
+
--breakpoint: 'md,lg';
|
|
115
|
+
}
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
Result on the host element:
|
|
119
|
+
|
|
120
|
+
```html
|
|
121
|
+
<my-el mode="mobile"></my-el>
|
|
122
|
+
<!-- or mode="tablet" / mode="desktop" -->
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
### Loader-aware Lit elements
|
|
126
|
+
|
|
127
|
+
```ts
|
|
128
|
+
import { LitElement } from 'lit';
|
|
129
|
+
import { LoaderMixin, waitForReady, waitForVisual } from '@trunkjs/browser-utils';
|
|
130
|
+
|
|
131
|
+
class MyEl extends LoaderMixin(LitElement) {}
|
|
132
|
+
|
|
133
|
+
await waitForReady();
|
|
134
|
+
await waitForVisual();
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
### Detect slot content in Lit elements
|
|
138
|
+
|
|
139
|
+
```ts
|
|
140
|
+
import { LitElement, html } from 'lit';
|
|
141
|
+
import { SlotVisibilityMixin } from '@trunkjs/browser-utils';
|
|
142
|
+
|
|
143
|
+
class MyEl extends SlotVisibilityMixin(LitElement) {
|
|
144
|
+
render() {
|
|
145
|
+
return html`<slot></slot>`;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
Empty slots get the CSS class:
|
|
151
|
+
|
|
152
|
+
```html
|
|
153
|
+
<slot class="slot-empty"></slot>
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
## Other exports
|
|
157
|
+
|
|
158
|
+
```ts
|
|
159
|
+
import {
|
|
160
|
+
Stopwatch,
|
|
161
|
+
Logger,
|
|
162
|
+
getErrorLocation,
|
|
163
|
+
breakpoints,
|
|
164
|
+
getCurrentBreakpoint,
|
|
165
|
+
getBreakpointMinWidth,
|
|
166
|
+
waitForDomContentLoaded,
|
|
167
|
+
waitForReady,
|
|
168
|
+
waitForPreVisual,
|
|
169
|
+
waitForVisual,
|
|
170
|
+
debounce,
|
|
171
|
+
} from '@trunkjs/browser-utils';
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
## Agent hints
|
|
175
|
+
|
|
176
|
+
- Prefer this package over ad-hoc browser helper code.
|
|
177
|
+
- Prefer examples above over introducing new abstractions.
|
|
178
|
+
- Mixins are mainly for Custom Elements / Lit elements.
|
|
179
|
+
- Storage helpers persist JSON-like objects via property access.
|
|
180
|
+
- `waitForReady` / `waitForPreVisual` / `waitForVisual` integrate with `window.tj_loader_state` when present.
|
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,23 @@
|
|
|
1
|
+
## 1.0.48 (2026-05-15)
|
|
2
|
+
|
|
3
|
+
This was a version bump only for browser-utils to align it with other projects, there were no code changes.
|
|
4
|
+
|
|
5
|
+
## 1.0.47 (2026-05-07)
|
|
6
|
+
|
|
7
|
+
This was a version bump only for browser-utils to align it with other projects, there were no code changes.
|
|
8
|
+
|
|
9
|
+
## 1.0.46 (2026-05-07)
|
|
10
|
+
|
|
11
|
+
This was a version bump only for browser-utils to align it with other projects, there were no code changes.
|
|
12
|
+
|
|
13
|
+
## 1.0.45 (2026-05-07)
|
|
14
|
+
|
|
15
|
+
This was a version bump only for browser-utils to align it with other projects, there were no code changes.
|
|
16
|
+
|
|
17
|
+
## 1.0.44 (2026-05-07)
|
|
18
|
+
|
|
19
|
+
This was a version bump only for browser-utils to align it with other projects, there were no code changes.
|
|
20
|
+
|
|
1
21
|
## 1.0.43 (2026-04-23)
|
|
2
22
|
|
|
3
23
|
This was a version bump only for browser-utils to align it with other projects, there were no code changes.
|
package/index.d.ts
CHANGED
|
@@ -9,4 +9,43 @@ export * from './lib/wait-for';
|
|
|
9
9
|
export * from './mixins/BreakPointMixin';
|
|
10
10
|
export * from './mixins/EventBindingsMixin';
|
|
11
11
|
export * from './mixins/LoggingMixin';
|
|
12
|
+
export * from './mixins/LoaderMixin';
|
|
12
13
|
export * from './mixins/SlotVisibilityMixin';
|
|
14
|
+
export type ElementSpec = {
|
|
15
|
+
element: HTMLElement;
|
|
16
|
+
state: 'wait' | 'ready';
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* THIS IS A COPY of the Loader events
|
|
20
|
+
*/
|
|
21
|
+
declare global {
|
|
22
|
+
interface CustomEventMap {
|
|
23
|
+
/**
|
|
24
|
+
* Event to be fired from HtmlElement on connectedCallback to indicate to the loader to wait for a element
|
|
25
|
+
*/
|
|
26
|
+
'init:child-waitreq': CustomEvent<ElementSpec>;
|
|
27
|
+
/**
|
|
28
|
+
* Event to be fired from HtmlElement after the element is fully initialized and ready.
|
|
29
|
+
*/
|
|
30
|
+
'init:child-ready': CustomEvent<ElementSpec>;
|
|
31
|
+
/**
|
|
32
|
+
* Fired as soon as all Elements are ready and displayed but not visual yet (visability: hidden). This can be used to perform any last minute adjustments before the loader is hidden and the content is visible.
|
|
33
|
+
*/
|
|
34
|
+
'loader:ready': CustomEvent<void>;
|
|
35
|
+
/**
|
|
36
|
+
* Fired when the loader is hidden and the content is visible but before blend animation. This can be used to perform any actions that should only be performed when the content is visible, such as starting animations or loading additional resources.
|
|
37
|
+
*/
|
|
38
|
+
'loader:pre-visual': CustomEvent<void>;
|
|
39
|
+
/**
|
|
40
|
+
* Fired when the content is fully visible to the user. This can be used to perform any actions that should only be performed when the content is visible, such as starting animations or loading additional resources.
|
|
41
|
+
*/
|
|
42
|
+
'loader:visual': CustomEvent<void>;
|
|
43
|
+
}
|
|
44
|
+
interface Window {
|
|
45
|
+
/**
|
|
46
|
+
* Flag to indicate that the loader component is active and that custom elements should wait for the
|
|
47
|
+
* loader events before initializing. This is used to prevent
|
|
48
|
+
*/
|
|
49
|
+
tj_loader_state: "loading" | "ready" | "pre-visual" | "visual";
|
|
50
|
+
}
|
|
51
|
+
}
|
package/index.js
CHANGED
|
@@ -1,37 +1,37 @@
|
|
|
1
|
-
var
|
|
2
|
-
var
|
|
1
|
+
var D = Object.defineProperty;
|
|
2
|
+
var x = (e) => {
|
|
3
3
|
throw TypeError(e);
|
|
4
4
|
};
|
|
5
|
-
var
|
|
6
|
-
var
|
|
7
|
-
var u = (e, t, n) => (
|
|
8
|
-
const
|
|
5
|
+
var R = (e, t, n) => t in e ? D(e, t, { enumerable: !0, configurable: !0, writable: !0, value: n }) : e[t] = n;
|
|
6
|
+
var c = (e, t, n) => R(e, typeof t != "symbol" ? t + "" : t, n), k = (e, t, n) => t.has(e) || x("Cannot " + n);
|
|
7
|
+
var u = (e, t, n) => (k(e, t, "read from private field"), n ? n.call(e) : t.get(e)), h = (e, t, n) => t.has(e) ? x("Cannot add the same private member more than once") : t instanceof WeakSet ? t.add(e) : t.set(e, n), w = (e, t, n, i) => (k(e, t, "write to private field"), i ? i.call(e, n) : t.set(e, n), n), y = (e, t, n) => (k(e, t, "access private method"), n);
|
|
8
|
+
const v = [
|
|
9
9
|
{ name: "xs", minWidth: 0 },
|
|
10
10
|
{ name: "sm", minWidth: 576 },
|
|
11
11
|
{ name: "md", minWidth: 768 },
|
|
12
12
|
{ name: "lg", minWidth: 992 },
|
|
13
13
|
{ name: "xl", minWidth: 1200 },
|
|
14
14
|
{ name: "xxl", minWidth: 1400 }
|
|
15
|
-
],
|
|
15
|
+
], _ = v.reduce(
|
|
16
16
|
(e, t) => (e[t.name] = t.minWidth, e),
|
|
17
17
|
{}
|
|
18
18
|
);
|
|
19
19
|
function E(e) {
|
|
20
|
-
if (!(e in
|
|
20
|
+
if (!(e in _))
|
|
21
21
|
throw new Error(`Unknown breakpoint: ${e}`);
|
|
22
|
-
return
|
|
22
|
+
return _[e];
|
|
23
23
|
}
|
|
24
|
-
function
|
|
24
|
+
function O() {
|
|
25
25
|
return window.visualViewport ? window.visualViewport.width : window.innerWidth;
|
|
26
26
|
}
|
|
27
|
-
function
|
|
28
|
-
e === void 0 && (e =
|
|
29
|
-
for (let t =
|
|
30
|
-
if (e >=
|
|
31
|
-
return
|
|
27
|
+
function U(e) {
|
|
28
|
+
e === void 0 && (e = O());
|
|
29
|
+
for (let t = v.length - 1; t >= 0; t--)
|
|
30
|
+
if (e >= v[t].minWidth)
|
|
31
|
+
return v[t].name;
|
|
32
32
|
return "xs";
|
|
33
33
|
}
|
|
34
|
-
function
|
|
34
|
+
function J(e, t = {}, n = []) {
|
|
35
35
|
Array.isArray(n) || (n = [n]);
|
|
36
36
|
const i = document.createElement(e);
|
|
37
37
|
for (const r in t)
|
|
@@ -40,16 +40,16 @@ function G(e, t = {}, n = []) {
|
|
|
40
40
|
i.append(typeof r == "string" ? document.createTextNode(r) : r);
|
|
41
41
|
return i;
|
|
42
42
|
}
|
|
43
|
-
class
|
|
43
|
+
class P {
|
|
44
44
|
/**
|
|
45
45
|
*
|
|
46
46
|
* @param delay Debounce delay in milliseconds
|
|
47
47
|
* @param max_delay Maximum delay in milliseconds, if false then no maximum delay is applied
|
|
48
48
|
*/
|
|
49
49
|
constructor(t, n = !1) {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
50
|
+
c(this, "timeout", null);
|
|
51
|
+
c(this, "startTimeWithMs", 0);
|
|
52
|
+
c(this, "maxTimeout", null);
|
|
53
53
|
this.delay = t, this.max_delay = n;
|
|
54
54
|
}
|
|
55
55
|
async wait() {
|
|
@@ -71,20 +71,20 @@ class M {
|
|
|
71
71
|
}
|
|
72
72
|
}
|
|
73
73
|
}
|
|
74
|
-
function
|
|
74
|
+
function X(e, t = !1) {
|
|
75
75
|
const n = /* @__PURE__ */ new WeakMap();
|
|
76
76
|
return function(i, r) {
|
|
77
77
|
if (r.kind !== "method") return i;
|
|
78
78
|
const a = r.name;
|
|
79
|
-
return function(...
|
|
79
|
+
return function(...d) {
|
|
80
80
|
let s = n.get(this);
|
|
81
81
|
s || (s = /* @__PURE__ */ new Map(), n.set(this, s));
|
|
82
82
|
let o = s.get(a);
|
|
83
|
-
o || (o = new
|
|
83
|
+
o || (o = new P(e, t), s.set(a, o)), o.debounce(() => i.apply(this, d));
|
|
84
84
|
};
|
|
85
85
|
};
|
|
86
86
|
}
|
|
87
|
-
function
|
|
87
|
+
function Q(e) {
|
|
88
88
|
if (typeof e.lineNumber == "number")
|
|
89
89
|
return {
|
|
90
90
|
file: e.fileName || e.sourceURL,
|
|
@@ -106,7 +106,7 @@ function q(e) {
|
|
|
106
106
|
}
|
|
107
107
|
return { file: e.fileName || e.sourceURL };
|
|
108
108
|
}
|
|
109
|
-
class
|
|
109
|
+
class j {
|
|
110
110
|
constructor(t, n, i, r = "main") {
|
|
111
111
|
this._debug = t, this.myTag = n, this.myElementId = i, this.instanceId = r;
|
|
112
112
|
}
|
|
@@ -127,13 +127,13 @@ class O {
|
|
|
127
127
|
throw this.error(...t), new Error(n);
|
|
128
128
|
}
|
|
129
129
|
}
|
|
130
|
-
class
|
|
130
|
+
class Y {
|
|
131
131
|
constructor(t, n = !0) {
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
132
|
+
c(this, "label");
|
|
133
|
+
c(this, "last");
|
|
134
|
+
c(this, "startTime");
|
|
135
|
+
c(this, "running", !1);
|
|
136
|
+
c(this, "enabled");
|
|
137
137
|
this.label = t, this.enabled = n, this.startTime = this.last = performance.now(), this.running = !0;
|
|
138
138
|
}
|
|
139
139
|
lap(t = "") {
|
|
@@ -157,10 +157,10 @@ class X {
|
|
|
157
157
|
return this.running;
|
|
158
158
|
}
|
|
159
159
|
}
|
|
160
|
-
function
|
|
160
|
+
function V(e) {
|
|
161
161
|
return typeof e == "object" && e !== null && !Array.isArray(e);
|
|
162
162
|
}
|
|
163
|
-
function
|
|
163
|
+
function F(e) {
|
|
164
164
|
if (e != null)
|
|
165
165
|
try {
|
|
166
166
|
return JSON.parse(e);
|
|
@@ -168,28 +168,28 @@ function V(e) {
|
|
|
168
168
|
return;
|
|
169
169
|
}
|
|
170
170
|
}
|
|
171
|
-
function
|
|
171
|
+
function M(e) {
|
|
172
172
|
const t = JSON.stringify(e);
|
|
173
173
|
return t === void 0 ? "null" : t;
|
|
174
174
|
}
|
|
175
175
|
function K(e, t) {
|
|
176
176
|
const n = { ...t };
|
|
177
|
-
if (
|
|
177
|
+
if (V(e))
|
|
178
178
|
for (const i of Object.keys(t))
|
|
179
179
|
i in e && (n[i] = e[i]);
|
|
180
180
|
return n;
|
|
181
181
|
}
|
|
182
|
-
class
|
|
182
|
+
class C {
|
|
183
183
|
constructor(t, n, i) {
|
|
184
|
-
|
|
184
|
+
c(this, "cache");
|
|
185
185
|
this.backend = t, this.storageKey = n, this.initialValue = i;
|
|
186
186
|
}
|
|
187
187
|
read() {
|
|
188
188
|
if (this.cache) return this.cache;
|
|
189
|
-
const t = this.backend ?
|
|
189
|
+
const t = this.backend ? F(this.backend.getItem(this.storageKey)) : void 0, n = K(t, this.initialValue);
|
|
190
190
|
if (this.backend && this.backend.getItem(this.storageKey) == null)
|
|
191
191
|
try {
|
|
192
|
-
this.backend.setItem(this.storageKey,
|
|
192
|
+
this.backend.setItem(this.storageKey, M(n));
|
|
193
193
|
} catch {
|
|
194
194
|
}
|
|
195
195
|
return this.cache = n, n;
|
|
@@ -197,7 +197,7 @@ class P {
|
|
|
197
197
|
write(t) {
|
|
198
198
|
if (this.cache = t, !!this.backend)
|
|
199
199
|
try {
|
|
200
|
-
this.backend.setItem(this.storageKey,
|
|
200
|
+
this.backend.setItem(this.storageKey, M(t));
|
|
201
201
|
} catch {
|
|
202
202
|
}
|
|
203
203
|
}
|
|
@@ -211,8 +211,8 @@ class P {
|
|
|
211
211
|
},
|
|
212
212
|
set: (n, i, r) => {
|
|
213
213
|
if (typeof i != "string") return !1;
|
|
214
|
-
const
|
|
215
|
-
return
|
|
214
|
+
const d = { ...this.read() };
|
|
215
|
+
return d[i] = r, this.write(d), !0;
|
|
216
216
|
},
|
|
217
217
|
deleteProperty: (n, i) => {
|
|
218
218
|
if (typeof i != "string") return !1;
|
|
@@ -249,16 +249,16 @@ function A(e) {
|
|
|
249
249
|
const t = globalThis.window;
|
|
250
250
|
return (e === "session" ? t == null ? void 0 : t.sessionStorage : t == null ? void 0 : t.localStorage) ?? void 0;
|
|
251
251
|
}
|
|
252
|
-
function
|
|
253
|
-
return new
|
|
252
|
+
function Z(e, t) {
|
|
253
|
+
return new C(A("session"), e, t).asProxy();
|
|
254
254
|
}
|
|
255
|
-
function
|
|
256
|
-
return new
|
|
255
|
+
function tt(e, t) {
|
|
256
|
+
return new C(A("local"), e, t).asProxy();
|
|
257
257
|
}
|
|
258
|
-
function
|
|
258
|
+
function T(e, t, n) {
|
|
259
259
|
return new Promise((i, r) => {
|
|
260
|
-
const a = (
|
|
261
|
-
e.removeEventListener(t, a, n), i(
|
|
260
|
+
const a = (d) => {
|
|
261
|
+
e.removeEventListener(t, a, n), i(d);
|
|
262
262
|
};
|
|
263
263
|
e.addEventListener(t, a, n);
|
|
264
264
|
});
|
|
@@ -268,15 +268,24 @@ function z() {
|
|
|
268
268
|
document.addEventListener("DOMContentLoaded", () => e());
|
|
269
269
|
}) : Promise.resolve();
|
|
270
270
|
}
|
|
271
|
-
function
|
|
271
|
+
function et() {
|
|
272
|
+
return window.tj_loader_state ? window.tj_loader_state !== "loading" ? Promise.resolve() : T(window, "loader:ready") : L();
|
|
273
|
+
}
|
|
274
|
+
function nt() {
|
|
275
|
+
return window.tj_loader_state ? window.tj_loader_state === "pre-visual" || window.tj_loader_state === "visual" ? Promise.resolve() : T(window, "loader:pre-visual") : L();
|
|
276
|
+
}
|
|
277
|
+
function it() {
|
|
278
|
+
return window.tj_loader_state ? window.tj_loader_state === "visual" ? Promise.resolve() : T(window, "loader:visual") : L();
|
|
279
|
+
}
|
|
280
|
+
function L(e = window) {
|
|
272
281
|
return e || (e = window), e === window ? document.readyState === "complete" ? Promise.resolve() : new Promise((t) => window.addEventListener("load", () => t(), { once: !0 })) : e instanceof HTMLImageElement ? e.complete && e.naturalWidth !== 0 ? Promise.resolve() : new Promise((t, n) => {
|
|
273
282
|
e.addEventListener("load", () => t(), { once: !0 }), e.addEventListener("error", () => n(new Error("image error")), { once: !0 });
|
|
274
283
|
}) : e instanceof HTMLMediaElement ? e.readyState >= HTMLMediaElement.HAVE_FUTURE_DATA ? Promise.resolve() : new Promise((t) => e.addEventListener("loadeddata", () => t(), { once: !0 })) : new Promise((t) => e.addEventListener("load", () => t(), { once: !0 }));
|
|
275
284
|
}
|
|
276
|
-
function
|
|
285
|
+
function rt(e) {
|
|
277
286
|
return new Promise((t) => setTimeout(t, e));
|
|
278
287
|
}
|
|
279
|
-
function
|
|
288
|
+
function st(e) {
|
|
280
289
|
return new Promise((t) => {
|
|
281
290
|
const n = (i) => {
|
|
282
291
|
e.removeEventListener("animationend", n), t(i);
|
|
@@ -284,31 +293,31 @@ function nt(e) {
|
|
|
284
293
|
e.addEventListener("animationend", n);
|
|
285
294
|
});
|
|
286
295
|
}
|
|
287
|
-
function
|
|
296
|
+
function ot(e) {
|
|
288
297
|
var n, i;
|
|
289
298
|
class t extends e {
|
|
290
299
|
constructor() {
|
|
291
300
|
super(...arguments);
|
|
292
|
-
h(this, n, new
|
|
293
|
-
|
|
301
|
+
h(this, n, new P(200, 5e3));
|
|
302
|
+
c(this, "currentBreakPoint", null);
|
|
294
303
|
h(this, i, async () => {
|
|
295
304
|
var b;
|
|
296
305
|
await u(this, n).wait(), await z();
|
|
297
|
-
const
|
|
298
|
-
let o = getComputedStyle(
|
|
306
|
+
const d = this, s = window.innerWidth;
|
|
307
|
+
let o = getComputedStyle(d).getPropertyValue("--breakpoint");
|
|
299
308
|
if (!o || o === "")
|
|
300
309
|
return;
|
|
301
310
|
o = o.trim().replace(/^['"]|['"]$/g, "");
|
|
302
|
-
const
|
|
303
|
-
this.currentBreakPoint !== g && (E(f) <= E(g) ?
|
|
311
|
+
const l = o.split(","), m = l[0].trim(), f = ((b = l[1]) == null ? void 0 : b.trim()) ?? m, g = U(s);
|
|
312
|
+
this.currentBreakPoint !== g && (E(f) <= E(g) ? d.setAttribute("mode", "desktop") : E(m) > E(g) ? d.setAttribute("mode", "mobile") : d.setAttribute("mode", "tablet"));
|
|
304
313
|
});
|
|
305
314
|
}
|
|
306
315
|
connectedCallback() {
|
|
307
316
|
super.connectedCallback();
|
|
308
317
|
try {
|
|
309
318
|
u(this, i).call(this), window.addEventListener("resize", u(this, i)), u(this, i).call(this);
|
|
310
|
-
} catch (
|
|
311
|
-
throw console.error("Error in BreakPointMixin:",
|
|
319
|
+
} catch (d) {
|
|
320
|
+
throw console.error("Error in BreakPointMixin:", d, "in element", this), d;
|
|
312
321
|
}
|
|
313
322
|
}
|
|
314
323
|
disconnectedCallback() {
|
|
@@ -317,14 +326,14 @@ function it(e) {
|
|
|
317
326
|
}
|
|
318
327
|
return n = new WeakMap(), i = new WeakMap(), t;
|
|
319
328
|
}
|
|
320
|
-
const
|
|
321
|
-
function
|
|
329
|
+
const p = Symbol("listenerDefs"), W = Symbol("withEventBindings");
|
|
330
|
+
function at(e, t) {
|
|
322
331
|
const n = Array.isArray(e) ? e : [e];
|
|
323
332
|
return function(i, r) {
|
|
324
333
|
if (r.kind !== "method") throw new Error("@Listen nur für Methoden");
|
|
325
334
|
return r.addInitializer(function() {
|
|
326
335
|
const a = this;
|
|
327
|
-
(a[
|
|
336
|
+
(a[p] || (a[p] = [])).push({
|
|
328
337
|
method: r.name,
|
|
329
338
|
events: [...n],
|
|
330
339
|
opts: t
|
|
@@ -336,11 +345,11 @@ function rt(e, t) {
|
|
|
336
345
|
};
|
|
337
346
|
};
|
|
338
347
|
}
|
|
339
|
-
function
|
|
348
|
+
function H(e, t) {
|
|
340
349
|
var n;
|
|
341
350
|
return !t || t === "host" ? e : t === "document" ? e.ownerDocument ?? document : t === "window" ? ((n = e.ownerDocument) == null ? void 0 : n.defaultView) ?? window : t === "shadowRoot" ? e.shadowRoot ?? e : typeof t == "function" ? t(e) : t;
|
|
342
351
|
}
|
|
343
|
-
function
|
|
352
|
+
function ut(e) {
|
|
344
353
|
var n, i, $;
|
|
345
354
|
class t extends e {
|
|
346
355
|
constructor(...s) {
|
|
@@ -359,24 +368,24 @@ function st(e) {
|
|
|
359
368
|
}
|
|
360
369
|
}
|
|
361
370
|
return n = new WeakMap(), i = new WeakSet(), $ = function() {
|
|
362
|
-
var o,
|
|
371
|
+
var o, l, m;
|
|
363
372
|
(o = u(this, n)) == null || o.abort(), w(this, n, new AbortController());
|
|
364
|
-
const s = this[
|
|
373
|
+
const s = this[p] || [];
|
|
365
374
|
for (const f of s) {
|
|
366
|
-
const g =
|
|
367
|
-
for (const
|
|
368
|
-
g.addEventListener(
|
|
375
|
+
const g = H(this, (l = f.opts) == null ? void 0 : l.target), b = ((m = f.opts) == null ? void 0 : m.options) ?? {}, N = this[f.method].bind(this);
|
|
376
|
+
for (const B of f.events)
|
|
377
|
+
g.addEventListener(B, N, { ...b, signal: u(this, n).signal });
|
|
369
378
|
}
|
|
370
379
|
}, t;
|
|
371
380
|
}
|
|
372
|
-
let
|
|
373
|
-
function
|
|
381
|
+
let G = 1;
|
|
382
|
+
function dt(e) {
|
|
374
383
|
var n, i, r;
|
|
375
384
|
class t extends e {
|
|
376
385
|
constructor() {
|
|
377
386
|
super(...arguments);
|
|
378
387
|
h(this, n, null);
|
|
379
|
-
h(this, i,
|
|
388
|
+
h(this, i, G++);
|
|
380
389
|
h(this, r, null);
|
|
381
390
|
}
|
|
382
391
|
/**
|
|
@@ -395,7 +404,7 @@ function ot(e) {
|
|
|
395
404
|
}
|
|
396
405
|
getLogger(s = "main") {
|
|
397
406
|
const o = "<" + (this.tagName || this.constructor.name || "UnknownElement") + ">";
|
|
398
|
-
return u(this, r) || w(this, r, new
|
|
407
|
+
return u(this, r) || w(this, r, new j(this._debug, o, `${u(this, i)}`, s)), u(this, r);
|
|
399
408
|
}
|
|
400
409
|
debug(...s) {
|
|
401
410
|
this.getLogger().debug(...s);
|
|
@@ -415,25 +424,61 @@ function ot(e) {
|
|
|
415
424
|
}
|
|
416
425
|
return n = new WeakMap(), i = new WeakMap(), r = new WeakMap(), t;
|
|
417
426
|
}
|
|
418
|
-
function
|
|
427
|
+
function lt(e) {
|
|
428
|
+
class t extends e {
|
|
429
|
+
connectedCallback() {
|
|
430
|
+
this.dispatchEvent(new CustomEvent("init:child-waitreq", {
|
|
431
|
+
detail: {
|
|
432
|
+
element: this,
|
|
433
|
+
state: "connected"
|
|
434
|
+
},
|
|
435
|
+
bubbles: !0,
|
|
436
|
+
composed: !0
|
|
437
|
+
})), super.connectedCallback();
|
|
438
|
+
}
|
|
439
|
+
firstUpdated(i) {
|
|
440
|
+
var r;
|
|
441
|
+
(r = super.firstUpdated) == null || r.call(this, i), this.dispatchEvent(new CustomEvent("init:child-ready", {
|
|
442
|
+
detail: {
|
|
443
|
+
element: this,
|
|
444
|
+
state: "ready"
|
|
445
|
+
},
|
|
446
|
+
bubbles: !0,
|
|
447
|
+
composed: !0
|
|
448
|
+
}));
|
|
449
|
+
}
|
|
450
|
+
disconnectedCallback() {
|
|
451
|
+
super.disconnectedCallback(), this.dispatchEvent(new CustomEvent("init:child-ready", {
|
|
452
|
+
detail: {
|
|
453
|
+
element: this,
|
|
454
|
+
state: "disconnected"
|
|
455
|
+
},
|
|
456
|
+
bubbles: !0,
|
|
457
|
+
composed: !0
|
|
458
|
+
}));
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
return t;
|
|
462
|
+
}
|
|
463
|
+
function ct(e) {
|
|
419
464
|
var n, I, r, S;
|
|
420
465
|
class t extends e {
|
|
421
466
|
constructor() {
|
|
422
467
|
super(...arguments);
|
|
423
468
|
h(this, n);
|
|
424
469
|
h(this, r, (o) => {
|
|
425
|
-
const
|
|
426
|
-
|
|
470
|
+
const l = o.target;
|
|
471
|
+
(l.assignedNodes({ flatten: !0 }).filter((g) => y(this, n, S).call(this, g)).length > 0 || l.childNodes.length > 0) && l.classList.remove("slot-empty");
|
|
427
472
|
});
|
|
428
473
|
}
|
|
429
474
|
firstUpdated(o) {
|
|
430
|
-
var
|
|
431
|
-
(
|
|
475
|
+
var l;
|
|
476
|
+
(l = super.firstUpdated) == null || l.call(this, o), y(this, n, I).call(this);
|
|
432
477
|
}
|
|
433
478
|
}
|
|
434
479
|
return n = new WeakSet(), I = function() {
|
|
435
|
-
var
|
|
436
|
-
const o = (
|
|
480
|
+
var l;
|
|
481
|
+
const o = (l = this.shadowRoot) == null ? void 0 : l.querySelectorAll("slot");
|
|
437
482
|
o == null || o.forEach((m) => {
|
|
438
483
|
m.classList.add("slot-empty"), m.addEventListener("slotchange", (f) => u(this, r).call(this, f));
|
|
439
484
|
});
|
|
@@ -442,27 +487,31 @@ function at(e) {
|
|
|
442
487
|
}, t;
|
|
443
488
|
}
|
|
444
489
|
export {
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
J as
|
|
490
|
+
ot as BreakPointMixin,
|
|
491
|
+
P as Debouncer,
|
|
492
|
+
ut as EventBindingsMixin,
|
|
493
|
+
at as Listen,
|
|
494
|
+
lt as LoaderMixin,
|
|
495
|
+
j as Logger,
|
|
496
|
+
dt as LoggingMixin,
|
|
497
|
+
ct as SlotVisibilityMixin,
|
|
498
|
+
Y as Stopwatch,
|
|
499
|
+
_ as breakpointMap,
|
|
500
|
+
v as breakpoints,
|
|
501
|
+
J as create_element,
|
|
502
|
+
X as debounce,
|
|
457
503
|
E as getBreakpointMinWidth,
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
504
|
+
U as getCurrentBreakpoint,
|
|
505
|
+
Q as getErrorLocation,
|
|
506
|
+
O as getViewportWidth,
|
|
507
|
+
tt as local_storage,
|
|
508
|
+
Z as session_storage,
|
|
509
|
+
rt as sleep,
|
|
510
|
+
T as waitFor,
|
|
511
|
+
st as waitForAnimationEnd,
|
|
466
512
|
z as waitForDomContentLoaded,
|
|
467
|
-
|
|
513
|
+
L as waitForLoad,
|
|
514
|
+
nt as waitForPreVisual,
|
|
515
|
+
et as waitForReady,
|
|
516
|
+
it as waitForVisual
|
|
468
517
|
};
|
package/lib/wait-for.d.ts
CHANGED
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
export declare function waitFor<T>(target: EventTarget, eventName: string, options?: AddEventListenerOptions): Promise<T>;
|
|
2
2
|
export declare function waitForDomContentLoaded(): Promise<void>;
|
|
3
|
+
export declare function waitForReady(): Promise<void>;
|
|
4
|
+
/**
|
|
5
|
+
* Wait for Loader firing the pre-visual (Elements are visual but might be in animation to show up)
|
|
6
|
+
*/
|
|
7
|
+
export declare function waitForPreVisual(): Promise<void>;
|
|
8
|
+
/**
|
|
9
|
+
* Wait for Loader firing visual (the elements are fully visual to the user)
|
|
10
|
+
*/
|
|
11
|
+
export declare function waitForVisual(): Promise<void>;
|
|
3
12
|
/**
|
|
4
13
|
* Waits for the load event of the given element or the window if no element is provided.
|
|
5
14
|
*
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { LitElement } from 'lit';
|
|
2
|
+
type Constructor<T = object> = abstract new (...args: any[]) => T;
|
|
3
|
+
export interface LoaderMixinInterface {
|
|
4
|
+
}
|
|
5
|
+
export declare function LoaderMixin<TBase extends Constructor<LitElement>>(Base: TBase): TBase & Constructor<LoaderMixinInterface>;
|
|
6
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,7 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@trunkjs/browser-utils",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.48",
|
|
4
4
|
"main": "./index.js",
|
|
5
|
+
"repository": {
|
|
6
|
+
"directory": "packages/browser-utils",
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "https://github.com/trunkjs/trunkjs-monorepo"
|
|
9
|
+
},
|
|
5
10
|
"dependencies": {},
|
|
6
11
|
"type": "module",
|
|
7
12
|
"types": "./index.d.ts"
|