@trunkjs/browser-utils 1.0.47 → 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 +4 -0
- package/package.json +1 -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,7 @@
|
|
|
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
|
+
|
|
1
5
|
## 1.0.47 (2026-05-07)
|
|
2
6
|
|
|
3
7
|
This was a version bump only for browser-utils to align it with other projects, there were no code changes.
|