@petit-kit/scoped 0.0.6-beta.0 → 0.0.6
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/README.md +190 -149
- package/dist/index.js +1 -1
- package/package.json +11 -11
- package/dist/index.cjs +0 -2
- package/dist/index.cjs.map +0 -1
- package/dist/index.d.ts +0 -613
- package/dist/index.d.ts.map +0 -1
- package/dist/plugins/content/index.d.ts +0 -1
- package/dist/plugins/content/index.d.ts.map +0 -1
- package/dist/plugins/device/index.d.ts +0 -8
- package/dist/plugins/device/index.d.ts.map +0 -1
- package/dist/plugins/index.d.ts +0 -11
- package/dist/plugins/index.d.ts.map +0 -1
- package/dist/plugins/inview/index.d.ts +0 -63
- package/dist/plugins/inview/index.d.ts.map +0 -1
- package/dist/plugins/lenis/index.d.ts +0 -37
- package/dist/plugins/lenis/index.d.ts.map +0 -1
- package/dist/plugins/lerp/index.d.ts +0 -42
- package/dist/plugins/lerp/index.d.ts.map +0 -1
- package/dist/plugins/morph/index.d.ts +0 -92
- package/dist/plugins/morph/index.d.ts.map +0 -1
- package/dist/plugins/mouse/index.d.ts +0 -91
- package/dist/plugins/mouse/index.d.ts.map +0 -1
- package/dist/plugins/pointer/index.d.ts +0 -59
- package/dist/plugins/pointer/index.d.ts.map +0 -1
- package/dist/plugins/spring/index.d.ts +0 -42
- package/dist/plugins/spring/index.d.ts.map +0 -1
- package/dist/plugins/timer/index.d.ts +0 -82
- package/dist/plugins/timer/index.d.ts.map +0 -1
- package/dist/plugins/window/index.d.ts +0 -14
- package/dist/plugins/window/index.d.ts.map +0 -1
package/README.md
CHANGED
|
@@ -6,11 +6,11 @@
|
|
|
6
6
|
<br />
|
|
7
7
|
<br />
|
|
8
8
|
|
|
9
|
-
# Scoped - 0.0.
|
|
9
|
+
# Scoped - 0.0.6
|
|
10
10
|
|
|
11
11
|
### A lightweight, framework-agnostic library for building web components with reactive state, bindings, lifecycle hooks, template-based rendering and plugins.
|
|
12
12
|
|
|
13
|
-
**4.
|
|
13
|
+
**4.2 Kb** Gzipped - **12.7kb** Minified
|
|
14
14
|
|
|
15
15
|
<br />
|
|
16
16
|
|
|
@@ -34,13 +34,17 @@ It encourages expressiveness and rapid prototyping, giving you fine-grained cont
|
|
|
34
34
|
- [Event Delegation](#event-delegation)
|
|
35
35
|
- [Slots](#slots)
|
|
36
36
|
- [Lifecycle](#lifecycle)
|
|
37
|
+
- [Selection](#select)
|
|
37
38
|
- [Plugins](#plugins)
|
|
39
|
+
- [Happy](#happy)
|
|
38
40
|
|
|
39
41
|
<br />
|
|
40
42
|
<br />
|
|
41
43
|
|
|
42
44
|
# Installation
|
|
43
45
|
|
|
46
|
+
To install **Scoped**, you can use your favorite package manager.
|
|
47
|
+
|
|
44
48
|
```bash
|
|
45
49
|
npm install @petit-kit/scoped
|
|
46
50
|
# or
|
|
@@ -51,6 +55,8 @@ pnpm install @petit-kit/scoped
|
|
|
51
55
|
|
|
52
56
|
# Getting started
|
|
53
57
|
|
|
58
|
+
To get started with **Scoped**, you can create a new component using the `define` function.
|
|
59
|
+
|
|
54
60
|
```javascript
|
|
55
61
|
import { define } from '@petit-kit/scoped';
|
|
56
62
|
|
|
@@ -137,29 +143,30 @@ The `SetupFunction` is run only once on mount and should return a function that
|
|
|
137
143
|
|
|
138
144
|
`host` is the component itself, it got those methods:
|
|
139
145
|
|
|
140
|
-
| Method
|
|
141
|
-
|
|
|
142
|
-
|
|
|
143
|
-
|
|
|
144
|
-
|
|
|
145
|
-
|
|
|
146
|
-
|
|
|
147
|
-
|
|
|
148
|
-
|
|
|
146
|
+
| Method | Description |
|
|
147
|
+
| ----------------------------- | ------------------------------------------ |
|
|
148
|
+
| **host.setState(partial)** | Update state + full re-render |
|
|
149
|
+
| **host.updateState(partial)** | Update state, effects only (no re-render) |
|
|
150
|
+
| **host.setProps(partial)** | Update props programmatically |
|
|
151
|
+
| **host.scheduleUpdate()** | Schedule effects on next RAF |
|
|
152
|
+
| **host.update(fullRender)** | Force update (full or partial) |
|
|
153
|
+
| **host.forceRender()** | Force re-render even if template unchanged |
|
|
154
|
+
| **host.destroy()** | Clean up and run destroy callbacks |
|
|
149
155
|
|
|
150
156
|
## Templating
|
|
151
157
|
|
|
152
|
-
|
|
158
|
+
Inside your setup function, you can return a function that uses template literals for HTML generation.
|
|
153
159
|
|
|
154
160
|
### Basic Example
|
|
155
161
|
|
|
156
162
|
```typescript
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
<
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
`;
|
|
163
|
+
() => {
|
|
164
|
+
return () => `
|
|
165
|
+
<div>
|
|
166
|
+
<h2>Hello, ${props.name}!</h2>
|
|
167
|
+
</div>
|
|
168
|
+
`;
|
|
169
|
+
};
|
|
163
170
|
```
|
|
164
171
|
|
|
165
172
|
### Dynamic Content
|
|
@@ -167,16 +174,24 @@ return () => `
|
|
|
167
174
|
Interpolation with `${...}` gives you access to state, props, or anything in closure:
|
|
168
175
|
|
|
169
176
|
```typescript
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
177
|
+
() => {
|
|
178
|
+
return () => `
|
|
179
|
+
<ul>
|
|
180
|
+
${state.items
|
|
181
|
+
.map(
|
|
182
|
+
(item) => `
|
|
183
|
+
<li>${item.title}</li>
|
|
184
|
+
`
|
|
185
|
+
)
|
|
186
|
+
.join('')}
|
|
187
|
+
</ul>
|
|
188
|
+
`;
|
|
189
|
+
};
|
|
175
190
|
```
|
|
176
191
|
|
|
177
192
|
### Event Handlers
|
|
178
193
|
|
|
179
|
-
Use `on:eventName="handler"` to bind events, where
|
|
194
|
+
Use `on:eventName="handler"` to bind events, where **handler** is a function from your **actions** object or setup context:
|
|
180
195
|
|
|
181
196
|
```typescript
|
|
182
197
|
({ actions }) => {
|
|
@@ -187,16 +202,19 @@ Use `on:eventName="handler"` to bind events, where `handler` is a function from
|
|
|
187
202
|
};
|
|
188
203
|
```
|
|
189
204
|
|
|
190
|
-
Arrow functions or direct expressions are not supported
|
|
205
|
+
Arrow functions or direct expressions are not supported, you must use named action references.
|
|
191
206
|
|
|
192
207
|
### Referencing DOM Elements
|
|
193
208
|
|
|
194
209
|
Use the `ref` attribute to assign references:
|
|
195
210
|
|
|
196
211
|
```typescript
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
212
|
+
({ onMount, refs }) => {
|
|
213
|
+
onMount(() => console.log(refs.inputElement));
|
|
214
|
+
return () => `
|
|
215
|
+
<input ref="inputElement" type="text"></input>
|
|
216
|
+
`;
|
|
217
|
+
};
|
|
200
218
|
```
|
|
201
219
|
|
|
202
220
|
You can then access the element as `refs.inputEl` in your setup code or methods.
|
|
@@ -205,16 +223,6 @@ You can then access the element as `refs.inputEl` in your setup code or methods.
|
|
|
205
223
|
|
|
206
224
|
Bindings let you connect the value of a DOM property or attribute to your component's state or props, making the element update reactively when the state changes, and optionally syncing changes back to your state.
|
|
207
225
|
|
|
208
|
-
#### Value Binding
|
|
209
|
-
|
|
210
|
-
For `<input>`, `<textarea>`, and `<select>`, use `bind:value="stateKey"` to bind the value to a property in your `state`. When the user edits the input, the component will automatically update that property.
|
|
211
|
-
|
|
212
|
-
```typescript
|
|
213
|
-
return () => `
|
|
214
|
-
<input bind:value="message">
|
|
215
|
-
`;
|
|
216
|
-
```
|
|
217
|
-
|
|
218
226
|
#### Supported Bindings
|
|
219
227
|
|
|
220
228
|
- `bind:text="stateKey"` - Binds textContent
|
|
@@ -223,13 +231,22 @@ return () => `
|
|
|
223
231
|
- `bind:checked="isChecked"` — Binds the checked property of checkbox/radio
|
|
224
232
|
- `bind:prop="key"` — Generic property binding (any property, e.g. `bind:min`, `bind:max`)
|
|
225
233
|
|
|
226
|
-
|
|
234
|
+
<br />
|
|
227
235
|
|
|
228
236
|
```typescript
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
237
|
+
({ state }) => {
|
|
238
|
+
state.textValue = 'Hello, world!';
|
|
239
|
+
state.htmlValue = `<strong>Hello, world!</strong>`;
|
|
240
|
+
state.isChecked = true;
|
|
241
|
+
state.styleValue = `background-color: red;`;
|
|
242
|
+
|
|
243
|
+
return () => `
|
|
244
|
+
<p bind:text="textValue"></p>
|
|
245
|
+
<p bind:html="htmlValue"></p>
|
|
246
|
+
<input type="checkbox" bind:checked="isChecked">
|
|
247
|
+
<div bind:style="styleValue"></div>
|
|
248
|
+
`;
|
|
249
|
+
};
|
|
233
250
|
```
|
|
234
251
|
|
|
235
252
|
## State & props
|
|
@@ -240,8 +257,8 @@ State is a plain object that belongs to your component instance. It is fully rea
|
|
|
240
257
|
|
|
241
258
|
You can update state in two main ways:
|
|
242
259
|
|
|
243
|
-
- `host.setState(partial)
|
|
244
|
-
- `host.updateState(partial)
|
|
260
|
+
- `host.setState(partial)` - Merges the partial state and triggers a full re-render.
|
|
261
|
+
- `host.updateState(partial)` - Merges the partial state and only schedules effects/computed, but does **NOT** re-render the template.
|
|
245
262
|
|
|
246
263
|
```typescript
|
|
247
264
|
// Initialize state in setup (no re-render)
|
|
@@ -280,7 +297,7 @@ Props are available as the `props` object in the setup function:
|
|
|
280
297
|
|
|
281
298
|
```javascript
|
|
282
299
|
define(
|
|
283
|
-
'c-my-
|
|
300
|
+
'c-my-component',
|
|
284
301
|
{
|
|
285
302
|
props: {
|
|
286
303
|
value: { type: Number, default: 10 },
|
|
@@ -289,9 +306,9 @@ define(
|
|
|
289
306
|
},
|
|
290
307
|
({ props }) => {
|
|
291
308
|
return () => `
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
309
|
+
<p>Value: ${props.value}</p>
|
|
310
|
+
${props.label}
|
|
311
|
+
`;
|
|
295
312
|
}
|
|
296
313
|
);
|
|
297
314
|
```
|
|
@@ -300,7 +317,7 @@ Props are always kept up to date with attribute changes, and updating props from
|
|
|
300
317
|
|
|
301
318
|
**Two-way Binding:**
|
|
302
319
|
|
|
303
|
-
Scoped allows props
|
|
320
|
+
Scoped allows **props** ↔️ **state** syncing using the `link` helper:
|
|
304
321
|
|
|
305
322
|
```typescript
|
|
306
323
|
({ link }) => {
|
|
@@ -322,31 +339,42 @@ This updates the prop, reflects it as an attribute if needed, and triggers all u
|
|
|
322
339
|
|
|
323
340
|
Props are also automatically parsed from their attribute string values into the appropriate type, based on your definition (Number, Boolean, etc.), so you always work with type-safe values in your setup and template logic.
|
|
324
341
|
|
|
342
|
+
**Setting large objects/arrays as props:**
|
|
343
|
+
|
|
344
|
+
You can set large objects/arrays as props by using the `host.setProps(...)` method:
|
|
345
|
+
|
|
346
|
+
```typescript
|
|
347
|
+
const component = document.querySelector('c-my-component');
|
|
348
|
+
component.setProps({ data: largeArray, config: complexObject });
|
|
349
|
+
```
|
|
350
|
+
|
|
325
351
|
## Effects
|
|
326
352
|
|
|
327
353
|
Effects are functions that run in response to reactive changes and can be used for side effects, subscriptions, or manual cleanup logic within your components.
|
|
328
354
|
|
|
329
355
|
```typescript
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
(
|
|
342
|
-
|
|
343
|
-
)
|
|
356
|
+
({ effect }) => {
|
|
357
|
+
// Run on every render
|
|
358
|
+
effect(() => console.log('Rendered'));
|
|
359
|
+
|
|
360
|
+
// Run once (empty deps)
|
|
361
|
+
effect(() => {
|
|
362
|
+
const sub = api.subscribe();
|
|
363
|
+
return () => sub.unsubscribe();
|
|
364
|
+
}, []);
|
|
365
|
+
|
|
366
|
+
// Run when deps change
|
|
367
|
+
effect(
|
|
368
|
+
(deps) => console.log('Count:', deps[0]),
|
|
369
|
+
() => [state.count]
|
|
370
|
+
);
|
|
344
371
|
|
|
345
|
-
// Manual cleanup
|
|
346
|
-
const cleanup = effect(() => {
|
|
347
|
-
|
|
348
|
-
});
|
|
349
|
-
cleanup();
|
|
372
|
+
// Manual cleanup
|
|
373
|
+
const cleanup = effect(() => {
|
|
374
|
+
/* ... */
|
|
375
|
+
});
|
|
376
|
+
cleanup();
|
|
377
|
+
};
|
|
350
378
|
```
|
|
351
379
|
|
|
352
380
|
## Computed
|
|
@@ -354,36 +382,36 @@ cleanup();
|
|
|
354
382
|
Computed values are memoized values used to derive data from state or props and automatically update when their dependencies change.
|
|
355
383
|
|
|
356
384
|
```typescript
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
)
|
|
361
|
-
|
|
385
|
+
({ computed }) => {
|
|
386
|
+
const fullName = computed(
|
|
387
|
+
() => `${state.firstName} ${state.lastName}`, // getter
|
|
388
|
+
() => [state.firstName, state.lastName] // dependencies
|
|
389
|
+
);
|
|
390
|
+
return () => `<p>Name: ${fullName()}</p>`;
|
|
391
|
+
};
|
|
362
392
|
```
|
|
363
393
|
|
|
364
394
|
## Custom events
|
|
365
395
|
|
|
396
|
+
Custom events are a way to communicate between components.
|
|
397
|
+
|
|
366
398
|
### Emit
|
|
367
399
|
|
|
368
400
|
To emit a custom event from your component, use `emit(name, detail?)`:
|
|
369
401
|
|
|
370
402
|
```typescript
|
|
371
|
-
|
|
372
|
-
|
|
403
|
+
({ emit }) => {
|
|
404
|
+
emit('my-event', { message: 'Hello from the component!' });
|
|
373
405
|
};
|
|
374
|
-
|
|
375
|
-
return () => `<button on:click="handleButtonClick">Emit Event</button>`;
|
|
376
406
|
```
|
|
377
407
|
|
|
378
|
-
|
|
408
|
+
Listening to custom events in parent:
|
|
379
409
|
|
|
380
410
|
```javascript
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
});
|
|
386
|
-
</script>
|
|
411
|
+
const component = document.querySelector('c-my-component');
|
|
412
|
+
component.addEventListener('my-event', (e) => {
|
|
413
|
+
console.log('Received:', e.detail.message);
|
|
414
|
+
});
|
|
387
415
|
```
|
|
388
416
|
|
|
389
417
|
### Listen
|
|
@@ -391,103 +419,107 @@ return () => `<button on:click="handleButtonClick">Emit Event</button>`;
|
|
|
391
419
|
You can use `listen` to subscribe to events on any EventTarget (automatically cleaned up on destroy):
|
|
392
420
|
|
|
393
421
|
```typescript
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
(e) => {
|
|
400
|
-
console.log('Got custom event with detail:', e.detail);
|
|
401
|
-
}
|
|
402
|
-
);
|
|
403
|
-
});
|
|
422
|
+
({ listen }) => {
|
|
423
|
+
listen(window, 'my-event', (e) => {
|
|
424
|
+
console.log('Received:', e.detail.message);
|
|
425
|
+
});
|
|
426
|
+
};
|
|
404
427
|
```
|
|
405
428
|
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
### Event Delegation
|
|
429
|
+
## Event Delegation
|
|
409
430
|
|
|
410
431
|
`delegate` lets you efficiently handle events on descendants matching a selector:
|
|
411
432
|
|
|
412
433
|
```typescript
|
|
413
|
-
onMount
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
434
|
+
({ onMount, delegate }) => {
|
|
435
|
+
onMount(() => {
|
|
436
|
+
delegate('click', '.item', (e, target) => {
|
|
437
|
+
console.log('Clicked item:', target.textContent);
|
|
438
|
+
target.classList.toggle('active');
|
|
439
|
+
});
|
|
417
440
|
});
|
|
418
|
-
});
|
|
419
441
|
|
|
420
|
-
return () => `
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
`;
|
|
442
|
+
return () => `
|
|
443
|
+
<ul>
|
|
444
|
+
<li class="item">Apple</li>
|
|
445
|
+
<li class="item">Banana</li>
|
|
446
|
+
<li class="item">Cherry</li>
|
|
447
|
+
</ul>
|
|
448
|
+
`;
|
|
449
|
+
};
|
|
427
450
|
```
|
|
428
451
|
|
|
429
452
|
## Slots
|
|
430
453
|
|
|
431
454
|
Slots allow you to render children inside your custom element, making it easy to compose interfaces or pass in dynamic content.
|
|
432
455
|
|
|
433
|
-
### Basic Usage
|
|
434
|
-
|
|
435
456
|
By default, any child content placed inside your component tag will be rendered in the default slot:
|
|
436
457
|
|
|
437
458
|
```html
|
|
438
|
-
<my-
|
|
439
|
-
<h2>Title goes here</h2>
|
|
440
|
-
<p>Some description or content.</p>
|
|
441
|
-
</my-
|
|
459
|
+
<c-my-component>
|
|
460
|
+
<h2 slot="title">Title goes here</h2>
|
|
461
|
+
<p slot="description">Some description or content.</p>
|
|
462
|
+
</c-my-component>
|
|
442
463
|
```
|
|
443
464
|
|
|
444
465
|
In your component template, use:
|
|
445
466
|
|
|
446
467
|
```typescript
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
<slot></slot>
|
|
450
|
-
|
|
451
|
-
|
|
468
|
+
define('my-card', {}, () => {
|
|
469
|
+
return () => `
|
|
470
|
+
<aside><slot name="title"></slot></aside>
|
|
471
|
+
<main><slot name="description"></slot></main>
|
|
472
|
+
<slot></slot>
|
|
473
|
+
`;
|
|
474
|
+
});
|
|
452
475
|
```
|
|
453
476
|
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
Named slots let your users provide content for specific areas:
|
|
477
|
+
## Lifecycle
|
|
457
478
|
|
|
458
|
-
|
|
459
|
-
<my-layout>
|
|
460
|
-
<div slot="sidebar">Sidebar content</div>
|
|
461
|
-
<div slot="main">Main content</div>
|
|
462
|
-
</my-layout>
|
|
463
|
-
```
|
|
479
|
+
Lifecycle hooks let you run code at specific moments in the component's life, such as mount, update, or destruction.
|
|
464
480
|
|
|
465
|
-
|
|
481
|
+
| Method | Description |
|
|
482
|
+
| ------------------------ | ------------------------ |
|
|
483
|
+
| **`onMount(cb)`** | After mount |
|
|
484
|
+
| **`onDestroy(cb)`** | On destroy |
|
|
485
|
+
| **`onUpdate(cb)`** | After each update |
|
|
486
|
+
| **`onBeforeUpdate(cb)`** | Before each update |
|
|
487
|
+
| **`onFirstUpdate(cb)`** | Once, after first render |
|
|
488
|
+
| **`onPropsChanged(cb)`** | When props change |
|
|
466
489
|
|
|
467
|
-
|
|
468
|
-
return () => `
|
|
469
|
-
<aside><slot name="sidebar"></slot></aside>
|
|
470
|
-
<main><slot name="main"></slot></main>
|
|
471
|
-
`;
|
|
472
|
-
```
|
|
490
|
+
## Select
|
|
473
491
|
|
|
474
|
-
|
|
492
|
+
The `$` method lets you query elements inside your component's shadow or light DOM, returning either a single element or an array (if multiple elements match).
|
|
475
493
|
|
|
476
|
-
|
|
494
|
+
```typescript
|
|
495
|
+
define('c-my-component', {}, ({ $, host }) => {
|
|
496
|
+
onMount(() => {
|
|
497
|
+
const btn = $('button.primary'); // single element or null
|
|
498
|
+
const items = $('.list-item'); // array when multiple match
|
|
499
|
+
|
|
500
|
+
// From outside via host element:
|
|
501
|
+
const el = document.querySelector('c-my-component'); // self
|
|
502
|
+
const inner = el.$('.list-item'); // same API on host
|
|
503
|
+
});
|
|
477
504
|
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
505
|
+
return () => `<div>
|
|
506
|
+
<button class="primary">OK</button>
|
|
507
|
+
<span class="list-item">
|
|
508
|
+
A
|
|
509
|
+
</span>
|
|
510
|
+
<span class="list-item">
|
|
511
|
+
B
|
|
512
|
+
</span>
|
|
513
|
+
</div>`;
|
|
514
|
+
});
|
|
515
|
+
```
|
|
486
516
|
|
|
487
517
|
# Plugins
|
|
488
518
|
|
|
489
519
|
Scoped includes a set of optional plugins to extend or enhance component behavior. You can import any of these plugins and register them via the `plugins` option.
|
|
490
520
|
|
|
521
|
+
⏲ Document is working in progress
|
|
522
|
+
|
|
491
523
|
**Available Plugins:**
|
|
492
524
|
|
|
493
525
|
- **[lerpPlugin](src/plugins/lerp/README.md) & [springPlugin](src/plugins/spring/README.md)**
|
|
@@ -520,8 +552,7 @@ Scoped includes a set of optional plugins to extend or enhance component behavio
|
|
|
520
552
|
**Usage Example:**
|
|
521
553
|
|
|
522
554
|
```javascript
|
|
523
|
-
import { define } from '@petit-kit/scoped';
|
|
524
|
-
import { inViewPlugin, timerPlugin } from '@petit-kit/scoped/plugins';
|
|
555
|
+
import { define, inViewPlugin, timerPlugin } from '@petit-kit/scoped';
|
|
525
556
|
|
|
526
557
|
define(
|
|
527
558
|
'my-component',
|
|
@@ -538,3 +569,13 @@ define(
|
|
|
538
569
|
All plugins are tree-shakeable—import only what you need.
|
|
539
570
|
|
|
540
571
|
See each plugin's README (linked above) for API docs, options, and usage examples.
|
|
572
|
+
|
|
573
|
+
# Happy
|
|
574
|
+
|
|
575
|
+
The `happy` method logs a friendly version and repo message to your console—call it in your app to show appreciation and support for Scoped!
|
|
576
|
+
|
|
577
|
+
```javascript
|
|
578
|
+
import { happy } from '@petit-kit/scoped';
|
|
579
|
+
|
|
580
|
+
happy(); // 🙂🙃🙂🙃🙂🙃🙂
|
|
581
|
+
```
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
function t({from:t=0,to:e=1,mass:n=1,stiffness:i=120,damping:s=14,velocity:o=0,tolerance:r=.001,resumeOnTarget:c=!0}={}){function h(){return d(v)}function f(t){if(M)return h();if(u(v)&&u(S)&&u(b)){let e=!0;for(let o=0;o<v.length;o+=1){const c=v[o]-S[o],h=(-i*c-s*b[o])/n;b[o]+=h*t,v[o]+=b[o]*t;const f=v[o]-S[o];(Math.abs(b[o])>=r||Math.abs(f)>=r)&&(e=!1)}if(e){for(let t=0;t<v.length;t+=1)v[t]=S[t],b[t]=0;M=!0}return l.value=v,l.velocity=b,d(v)}if(a(v)&&a(S)&&a(b)){const e=l.objectKeys??Object.keys(v);let o=!0;for(const c of e){const e=v[c]-S[c],h=(-i*e-s*b[c])/n;b[c]+=h*t,v[c]+=b[c]*t;const f=v[c]-S[c];(Math.abs(b[c])>=r||Math.abs(f)>=r)&&(o=!1)}if(o){for(const t of e)v[t]=S[t],b[t]=0;M=!0}return l.value=v,l.velocity=b,d(v)}const e=S;let o=b;o+=(-i*(v-e)-s*o)/n*t,v+=o*t,b=o,l.value=v,l.velocity=b;const c=v-e;return Math.abs(o)<r&&Math.abs(c)<r&&(v=e,b=0,l.value=v,l.velocity=b,M=!0),v}const l=y({from:t,to:e,velocity:o,label:"Spring"}),g=l.normalizeInput;let v=l.value,b=l.velocity??o,S=l.target,j=null,M=!1;const O=new Set;return{setTarget:function(t){const e=g(t),n=!m(e,S);if(S=e,l.target=e,v=l.value,c&&M&&n){M=!1,j=null;for(const t of O)t(S)}},setValue:function(t,e={}){const{resetVelocity:n=!0,resetTime:i=!0,setTarget:s=!1,markDone:o=!1}=e;v=g(t),l.value=v,s&&(S=d(v),l.target=S);const r=M||!m(v,S);if(n&&(b=null!=l.arrayLength?p(0,l.arrayLength):null!=l.objectKeys?w(0,l.objectKeys):0,l.velocity=b),i&&(j=null),o&&(M=!0),r&&!o){M=!1,j=null;for(const t of O)t(S)}},getValue:h,isDone:function(){return M},onResume:function(t){return O.add(t),()=>{O.delete(t)}},step:f,next:function(t=performance.now()){if(null==j)return j=t,h();const e=(t-j)/1e3;j=t;const n=1/30;let i=e,s=h();for(;i>0&&!M;){const t=Math.min(i,n);s=f(t),i-=t}return s}}}function e(t,e){if(!e||"object"!=typeof e||null==e.type)return null!=t?t:e;const{type:n,default:i}=e;if(null==t)return i;try{switch(n){case String:return String(t);case Number:{const e=Number(t);return Number.isNaN(e)?i:e}case Boolean:return""===t||"true"===t||"false"!==t&&"0"!==t&&null!=t;case Object:try{return"string"==typeof t?JSON.parse(t):t}catch{return i}case Array:try{return"string"==typeof t?JSON.parse(t):t}catch{return Array.isArray(i)?i:[]}default:return t}}catch{return i}}function n(t,e,n,i){if(!i||"object"!=typeof i||!i.reflect)return;let s=null;const o=i.type;if(o===Boolean)return n?void t.setAttribute(e,""):void t.removeAttribute(e);if(o===Object||o===Array)try{s=null==n?null:JSON.stringify(n)}catch{s=null}else s=null==n?null:String(n);null==s?t.removeAttribute(e):t.setAttribute(e,s)}function i(t,i={},s){const{props:o={},shadow:r=!1,styles:c,plugins:h}=i,f=h??[],l=()=>{};class u extends HTMLElement{constructor(){super(),this.version=M,this.t={};for(const t of Object.keys(o)){const e=o[t];this.t[t]=e&&"object"==typeof e&&("type"in e||"default"in e)?e:{type:void 0,default:e,reflect:!1}}this.props={},this.state={},this.actions={},this.refs={},this.emit=this.emit.bind(this),this.listen=this.listen.bind(this),this.setState=this.setState.bind(this),this.updateState=this.updateState.bind(this),this.setProps=this.setProps.bind(this),this.scheduleUpdate=this.scheduleUpdate.bind(this),this.update=this.update.bind(this),this.forceRender=this.forceRender.bind(this),this.destroy=this.destroy.bind(this),this.i=null,this.o=null,this.h=!1,this.l=!1,this.u=r,this.m=r?this.attachShadow({mode:"open"}):this,this.p=null,this.S=[],this.j=[],this.M=[],this.O=[],this._=[],this.$=[],this.A=[],this.T=[],this.k=new Map,this.L=!1,this.F=!1,this.I={},this.R=!1,this.V=t,this.C=!1,this.H=new Set,this.D=!1,this.U=new Map,this.q=0,this.B=!1}N(t){const e=this.u?this.m.host:this;let n=t.parentElement;for(;n;){if(n===e)return!1;if(n.tagName.includes("-"))return!0;n=n.parentElement}return!1}static get observedAttributes(){return Object.keys(o)}attributeChangedCallback(t,n,i){if(n===i)return;const s=this.t[t],o=this.I[t],r=e(i,s);if(this.props[t]=r,this.L&&o!==r)for(const e of this.T)try{e(t,o,r)}catch(t){l(String(t?.message||t))}this.I[t]=r,this.H.has(t)?this.H.delete(t):this.i&&this.isConnected?this.D?this.C=!0:this.update(!0):this.C=!0}connectedCallback(){for(const t in this.t){if(!this.t.hasOwnProperty(t))continue;const n=e(this.getAttribute(t),this.t[t]);this.props[t]=n,this.I[t]=n}r||this.p||(this.p=this.W());let t=null;try{if(s){const e={props:this.props,state:this.state,actions:this.actions,refs:this.refs,emit:this.emit,listen:this.listen,updateState:this.updateState.bind(this),host:this,onMount:t=>this.M.push(t),onDestroy:t=>this.O.push(t),onUpdate:t=>this._.push(t),onBeforeUpdate:t=>this.$.push(t),onFirstUpdate:t=>this.A.push(t),onPropsChanged:t=>this.T.push(t),link:(t,e)=>{const i=e||t;this.state[i]=this.props[t],this.T.push((e,n,s)=>{e===t&&(Object.is(this.state[i],s)||(this.state[i]=s))});const s={fn:()=>{const e=this.state[i];if(Object.is(this.props[t],e))return;this.props[t]=e,this.I[t]=e;const s=this.t[t],o=s?{...s,reflect:!0}:s,r=this.getAttribute(t);this.H.add(t),n(this,t,e,o),r===this.getAttribute(t)&&this.H.delete(t)},deps:()=>[this.state[i]]};this.S.push(s)},computed:(t,e)=>{let n;if(void 0!==e)try{const t="function"==typeof e?e():e;Array.isArray(t)&&(n=t)}catch(t){String(t?.message||t)}const i={getter:t,deps:e,value:void 0!==e?t(n):t()};this.j.push(i);const s=()=>i.value;return s.P=i,this.J(s),s},effect:(t,e)=>{const n={fn:t,deps:e};return this.S.push(n),()=>this.K(n)},delegate:(t,e,n)=>(this.Z(t,e,n),()=>this.G(t,e,n))};for(const t of f)if(t)try{const n=t.extend(e,this);n&&"object"==typeof n&&Object.assign(e,n)}catch(t){l(String(t?.message||t))}t=s(e)}}catch(t){String(t?.message||t)}if(this.i="function"!=typeof t?()=>"":t,this.D=!0,this.update(!0),this.D=!1,this.C&&(this.C=!1,this.update(!0)),!this.L){this.L=!0;for(const t of this.M)try{t()}catch(t){l(String(t?.message||t))}}}disconnectedCallback(){this.destroy()}remove(){super.remove()}destroy(){for(const t of this.O)try{t()}catch(t){l(String(t?.message||t))}for(const t of this.S)if(t.cleanup){try{t.cleanup()}catch(t){l(String(t?.message||t))}t.cleanup=void 0}for(const[,t]of this.k)try{this.m.removeEventListener(t.eventType,t.listener)}catch{}this.k.clear(),this.L=!1}emit(t,e){this.dispatchEvent(new CustomEvent(t,{detail:e,bubbles:!0,composed:!0}))}listen(t,e,n,i){const s=n;t.addEventListener(e,s,i);const o=()=>{try{t.removeEventListener(e,s,i)}catch{}};return this.O.push(o),o}setState(t){let e=!1;const n=t,i=this.state;for(const t in n){if(!Object.prototype.hasOwnProperty.call(n,t))continue;const s=n[t];Object.is(i[t],s)||(i[t]=s,e=!0)}if(e)if(this.D||!this.L)this.update(!0);else{if(!this.i||!this.isConnected)return;if(this.h)return;this.h=!0,requestAnimationFrame(()=>{this.h=!1,this.i&&this.isConnected&&this.update(!0)})}}updateState(t){Object.assign(this.state,t),this.i&&this.isConnected&&this.X()}setProps(t){const e=Object.keys(t);if(0===e.length)return;const i=[];for(const s of e){const e=t[s],o=this.I[s];this.props[s]=e,this.L&&o!==e&&i.push(s);const r=this.t[s];r&&r.reflect&&n(this,s,e,r),this.L&&o===e||(this.I[s]=e)}if(this.L&&i.length>0)for(const e of i){const n=this.I[e],i=t[e];for(const t of this.T)try{t(e,n,i)}catch(t){l(String(t?.message||t))}}this.i&&this.isConnected?this.update(!0):this.C=!0}scheduleUpdate(){this.i&&this.isConnected&&this.X()}X(){this.l||this.h||(this.l=!0,("function"==typeof queueMicrotask?queueMicrotask:t=>Promise.resolve().then(t))(()=>{this.l=!1,this.i&&this.isConnected&&(this.h||this.update(!1))}))}update(t){if(this.i){if(t&&this.L)for(const t of this.$)try{t()}catch(t){l(String(t?.message||t))}if(t){this.Y();let t="";try{t=this.i()}catch(e){String(e?.message||e),t=""}if("string"!=typeof t&&(t=null==t?"":String(t)),t=((t,e)=>{const n={...this.props,...e};return t.replace(_,(t,e)=>{const i=n[e];return null==i?"":String(i)})})(t,this.state),!this.u){const e=`data-scope-owner="${this.V}"`;t=t.replace(/<slot(?![^>]*data-scope-owner)(\s|>)/g,`<slot ${e}$1`)}this.B=!1;const e=null!==this.o&&Object.is(this.o,t);let n=!1;e&&this.L||(this.u,this.m.innerHTML=t,this.o=t,n=!0),this.D?(this.tt(),("function"==typeof requestAnimationFrame?requestAnimationFrame:t=>setTimeout(t,0))(()=>{this.i&&this.isConnected&&(n&&!r&&this.projectSlots(),n&&this.et(),this.nt(),this.it())})):(n&&!r&&this.projectSlots(),n&&this.st(),this.nt(),this.it())}else this.B&&this.Y(),this.nt(),this.L&&this.it()}}forceRender(){this.o=null,this.i&&this.isConnected?this.D?this.C=!0:this.update(!0):this.C=!0}it(){if(!this.F){this.F=!0;for(const t of this.A)try{t()}catch(t){l(String(t?.message||t))}}for(const t of this._)try{t()}catch(t){l(String(t?.message||t))}this.ot()}ot(){const t=(this.u?this.m:this).querySelectorAll("*"),e=Object.prototype.hasOwnProperty,n=this.state,i=this.props;this.actions;for(let s=0;s<t.length;s++){const o=t[s];if(this.N(o))continue;if(0===o.attributes.length)continue;const r=o.attributes;for(let t=r.length-1;t>=0;t--){const s=r[t];if(!s.name.startsWith($))continue;const c=s.name.slice(5),h=s.value,f=h?h.trim():"";let l,u=!1;if(f){const t=this.U.get(f);if(t){t.P&&(this.B=!0);try{l=t()}catch{}u=!0}}if(!u){const t=f||c,s=e.call(n,t),o=!s&&e.call(i,t);s?l=n[t]:o&&(l=i[t])}if("text"===c){const t=null==l?"":String(l);o.textContent!==t&&(o.textContent=t)}else if("html"===c){const t=null==l?"":String(l);o.innerHTML!==t&&(o.innerHTML=t)}else if(c in o){if(!Object.is(o[c],l))try{o[c]=l}catch{}if("value"===c)try{null==l?o.removeAttribute("value"):o.setAttribute("value",String(l))}catch{}}else if(null!=l)try{o.setAttribute(c,String(l))}catch{}const a=`__scopeBind_${c}`,d=o[a];if(d){const t=d.rt;t&&o.removeEventListener(t,d),delete o[a]}}}}Y(){for(const t of this.j){let e,n=!0;if(void 0!==t.deps)try{const i="function"==typeof t.deps?t.deps():t.deps;if(Array.isArray(i)&&(e=i,t.prevDeps&&t.prevDeps.length===e.length)){n=!1;for(let i=0;i<e.length;i++)if(!Object.is(t.prevDeps[i],e[i])){n=!0;break}}}catch(t){l(String(t?.message||t)),n=!0,e=void 0}if(n){try{t.value=void 0!==t.deps?t.getter(e):t.getter()}catch(t){l(String(t?.message||t))}e&&(t.prevDeps=e.slice())}}}nt(){for(const t of this.S){let e,n=!0;if(void 0!==t.deps)try{const i="function"==typeof t.deps?t.deps():t.deps;if(Array.isArray(i)&&(e=i,t.prevDeps&&t.prevDeps.length===e.length)){n=!1;for(let i=0;i<e.length;i++)if(!Object.is(t.prevDeps[i],e[i])){n=!0;break}}}catch(t){l(String(t?.message||t)),n=!0,e=void 0}if(n){if(t.cleanup){try{t.cleanup()}catch{}t.cleanup=void 0}try{const n=void 0!==t.deps?t.fn(e):t.fn();"function"==typeof n&&(t.cleanup=n)}catch{}e&&(t.prevDeps=e.slice())}}}K(t){const e=this.S.indexOf(t);if(-1!==e){if(t.cleanup)try{t.cleanup()}catch{}this.S.splice(e,1)}}J(t){const e=t.ct;if(e&&"string"==typeof e)return this.U.set(e,t),e;const n=`__scope_bind_${++this.q}__`;this.U.set(n,t);try{t.ct=n,t.toString=()=>n}catch{}return n}tt(){const t=(this.u?this.m:this).querySelectorAll("[ref]"),e=this.refs;for(const t in e)e.hasOwnProperty(t)&&delete e[t];if(0!==t.length)for(let n=0;n<t.length;n++){const i=t[n];if(this.N(i))continue;const s=i.getAttribute("ref");s&&(e[s]?Array.isArray(e[s])?e[s].push(i):e[s]=[e[s],i]:e[s]=i)}}et(){const t=(this.u?this.m:this).querySelectorAll("*");for(let e=0;e<t.length;e++){const n=t[e];if(this.N(n))continue;if(0===n.attributes.length)continue;const i=n.attributes;for(let t=i.length-1;t>=0;t--){const e=i[t];if(!e.name.startsWith("on:"))continue;const s=e.name.slice(3),o=e.value,r=`__tinyHandler_${s}`,c=n[r];c&&n.removeEventListener(s,c),n.removeAttribute(e.name);const h=this.actions[o];if(h&&"function"==typeof h){const t=t=>{h.call(this.actions,t)};n[r]=t,n.addEventListener(s,t)}}}}st(){const t=(this.u?this.m:this).querySelectorAll("*"),e=this.refs;for(const t in e)e.hasOwnProperty(t)&&delete e[t];for(let n=0;n<t.length;n++){const i=t[n];if(this.N(i))continue;const s=i.getAttribute("ref");if(s&&(e[s]?Array.isArray(e[s])?e[s].push(i):e[s]=[e[s],i]:e[s]=i),i.attributes.length>0){const t=i.attributes;for(let e=t.length-1;e>=0;e--){const n=t[e];if(!n.name.startsWith("on:"))continue;const s=n.name.slice(3),o=n.value,r=`__tinyHandler_${s}`,c=i[r];c&&i.removeEventListener(s,c),i.removeAttribute(n.name);const h=this.actions[o];if(h&&"function"==typeof h){const t=t=>{h.call(this.actions,t)};i[r]=t,i.addEventListener(s,t)}}}}}W(){const t=new Map,e=this.childNodes,n=[];for(let t=0;t<e.length;t++)n.push(e[t]);for(let e=0;e<n.length;e++){const i=n[e];let s="";1===i.nodeType&&i.getAttribute&&(s=i.getAttribute("slot")||""),t.has(s)||t.set(s,[]),t.get(s).push(i)}for(let t=0;t<n.length;t++){const e=n[t];e.parentNode&&e.parentNode.removeChild(e)}return t}projectSlots(){const t=this.p||new Map,e=(this.u?this.m:this).querySelectorAll(`slot[data-scope-owner="${this.V}"]`);if(0!==e.length)for(let n=0;n<e.length;n++){const i=e[n],s=i.getAttribute("name")||"",o=t.get(s)||[];if(o.length){const t=document.createDocumentFragment();for(let e=0;e<o.length;e++){const n=o[e];let i;if(1===n.nodeType&&n.tagName.includes("-")&&n.p instanceof Map){const t=n,e=document.createElement(t.tagName.toLowerCase());for(let n=0;n<t.attributes.length;n++){const i=t.attributes[n];e.setAttribute(i.name,i.value)}for(const n of t.p.values())for(let t=0;t<n.length;t++)e.appendChild(n[t].cloneNode(!0));i=e}else i=n.cloneNode(!0);t.appendChild(i)}i.replaceWith(t)}else{const t=i.childNodes,e=[];for(let n=0;n<t.length;n++)e.push(t[n]);if(e.length>0){const t=document.createDocumentFragment();for(let n=0;n<e.length;n++)t.appendChild(e[n]);i.replaceWith(t)}}}}Z(t,e,n){const i=`${t}::${e}`;let s=this.k.get(i);if(!s){const n=t=>{const n=t.target&&t.target.closest?t.target.closest(e):null;if(n)for(const e of s.handlers)try{e(t,n)}catch{}};s={eventType:t,selector:e,listener:n,handlers:new Set},this.k.set(i,s),this.m.addEventListener(t,n)}s.handlers.add(n)}G(t,e,n){const i=`${t}::${e}`,s=this.k.get(i);if(s&&(s.handlers.delete(n),0===s.handlers.size)){try{this.m.removeEventListener(t,s.listener)}catch{}this.k.delete(i)}}}if(!customElements.get(t)){if(c&&"undefined"!=typeof document){const e=`scope-${t}-styles`;if(!document.getElementById(e)){const t=document.createElement("style");t.id=e,t.textContent=c,document.head.appendChild(t)}}try{customElements.define(t,u)}catch(t){String(t?.message||t)}}return u}const s=()=>({name:"device",extend:t=>{const e=new Map;return t.onDestroy(()=>{for(const[t,n]of e)t.removeEventListener("change",n);e.clear()}),{onMediaQuery:(t,n,i={})=>{if("undefined"==typeof window||"undefined"==typeof matchMedia)return()=>{};const{immediate:s=!0}=i,o=matchMedia(t),r=t=>{n(t.matches,t)};return o.addEventListener("change",r),e.set(o,r),s&&n(o.matches,null),()=>{o.removeEventListener("change",r),e.delete(o)}}}}}),o=(t,e={})=>({name:"morph",extend:(n,i)=>{const{ignoreActiveValue:s=!0,callbacks:o}=e,r=t(),c=i,h=c.m,f=c.u?ShadowRoot.prototype:HTMLElement.prototype,l=Object.getOwnPropertyDescriptor(f,"innerHTML"),u=t=>{r.morph(h,t,{morphStyle:"innerHTML",ignoreActiveValue:s,callbacks:o})};let a=!0;return Object.defineProperty(h,"innerHTML",{set(t){a?(l.set.call(this,t),a=!1):u(t)},get(){return l.get.call(this)},configurable:!0}),n.onDestroy(()=>{delete h.innerHTML}),{morph:u}}}),r=()=>({name:"window",extend:t=>{const e=new Set,n=new Set,i=new Set,s=(t,n={})=>{if("undefined"==typeof window)return()=>{};const{immediate:i=!0}=n,s=e=>{t(window.innerWidth,window.innerHeight,e)};return window.addEventListener("resize",s),e.add(s),i&&s(new UIEvent("resize")),()=>{window.removeEventListener("resize",s),e.delete(s)}};return t.onDestroy(()=>{if("undefined"!=typeof window){for(const t of e)window.removeEventListener("resize",t);e.clear();for(const t of i)t.disconnect();i.clear();for(const t of n)window.removeEventListener("scroll",t);n.clear()}}),{onViewportResize:s,onWindowResize:(t,e={})=>{if("undefined"==typeof window)return()=>{};if("undefined"==typeof ResizeObserver)return s((e,n,i)=>t(e,n,i),e);const{immediate:n=!0}=e,o=document.documentElement,r=new ResizeObserver(e=>{const n=e[0];if(!n)return;const{width:i,height:s}=n.contentRect;t(i,s,n)});if(r.observe(o),i.add(r),n){const e=o.getBoundingClientRect();t(e.width,e.height,new UIEvent("resize"))}return()=>{r.disconnect(),i.delete(r)}},onWindowScroll:(t,e={})=>{if("undefined"==typeof window)return()=>{};const{immediate:i=!0}=e,s=e=>{t(window.scrollX,window.scrollY,e)};return window.addEventListener("scroll",s,{passive:!0}),n.add(s),i&&s(new Event("scroll")),()=>{window.removeEventListener("scroll",s),n.delete(s)}}}}}),c=()=>({name:"inview",extend:(t,e)=>{const n=new Set,i=(t,e,i={})=>{if("undefined"==typeof window||"undefined"==typeof IntersectionObserver)return()=>{};const{immediate:s=!0,...o}=i;let r=!s;const c=new IntersectionObserver(n=>{for(const i of n)i.target===t&&(r?r=!1:e(i.isIntersecting,i))},o);return c.observe(t),n.add(c),()=>{c.unobserve(t),c.disconnect(),n.delete(c)}};return t.onDestroy(()=>{for(const t of n)t.disconnect();n.clear()}),{onInView:(t,n)=>i(e,t,n),observeInView:(t,e,n)=>i(t,e,n)}}}),h=t=>({name:"lenis",extend:e=>{const n=new Set;return e.onDestroy(()=>{for(const{lenis:t,handler:e}of n)"function"==typeof t.off&&t.off("scroll",e);n.clear()}),{onLenisScroll:e=>{const i=t();if(!i)return()=>{};const s=t=>{e(t)};i.on("scroll",s);const o={lenis:i,handler:s};return n.add(o),()=>{n.has(o)&&(n.delete(o),"function"==typeof i.off&&i.off("scroll",s))}}}}}),f=()=>({name:"timer",extend:t=>{const e=new Set,n=new Set,i=new Set,s={setTimeout:(t,n,...i)=>{let s;return s=setTimeout((...n)=>{e.delete(s),t(...n)},n,...i),e.add(s),s},setInterval:(t,e,...i)=>{const s=setInterval(t,e,...i);return n.add(s),s},raf:(t,e)=>{let n=0,s=!0,o=0;const r="number"==typeof e&&e>0?1e3/e:0,c=e=>{if(i.delete(n),s){if(r){if(e-o>=r){const n=o?e-o:r;o=e,t(e,n)}}else{const n=o?e-o:0;o=e,t(e,n)}n=requestAnimationFrame(c),i.add(n)}};return n=requestAnimationFrame(c),i.add(n),()=>{s&&(s=!1,i.delete(n),cancelAnimationFrame(n))}}};return t.onDestroy(()=>{for(const t of e)clearTimeout(t);e.clear();for(const t of n)clearInterval(t);n.clear();for(const t of i)cancelAnimationFrame(t);i.clear()}),{timer:s}}}),l=()=>({name:"mouse",extend:t=>{const e=new Map,n=(t,n)=>{if("undefined"==typeof window)return()=>{};const i=t=>{const e=t;n(e.clientX,e.clientY,e)};window.addEventListener(t,i);let s=e.get(t);return s||(s=new Set,e.set(t,s)),s.add(i),()=>{window.removeEventListener(t,i),s?.delete(i)}};return t.onDestroy(()=>{if("undefined"!=typeof window){for(const[t,n]of e){for(const e of n)window.removeEventListener(t,e);n.clear()}e.clear()}}),{onMouseMove:t=>n("mousemove",t),onMouseDown:t=>n("mousedown",t),onMouseUp:t=>n("mouseup",t),onMouseWheel:t=>(t=>{if("undefined"==typeof window)return()=>{};const n=e=>{const n=e;t(n.clientX,n.clientY,n.deltaY,n)};window.addEventListener("wheel",n);let i=e.get("wheel");return i||(i=new Set,e.set("wheel",i)),i.add(n),()=>{window.removeEventListener("wheel",n),i?.delete(n)}})(t)}}}),u=t=>Array.isArray(t),a=t=>null!=t&&"object"==typeof t&&!Array.isArray(t),d=t=>u(t)?t.slice():a(t)?{...t}:t,m=(t,e)=>{if(u(t)&&u(e)){if(t.length!==e.length)return!1;for(let n=0;n<t.length;n+=1)if(!Object.is(t[n],e[n]))return!1;return!0}if(a(t)&&a(e)){const n=Object.keys(t),i=Object.keys(e);if(n.length!==i.length)return!1;for(const i of n){if(!(i in e))return!1;if(!Object.is(t[i],e[i]))return!1}return!0}return!(u(t)||u(e)||a(t)||a(e))&&Object.is(t,e)},p=(t,e)=>Array.from({length:e},()=>t),w=(t,e)=>e.reduce((e,n)=>(e[n]=t,e),{}),y=({from:t,to:e,velocity:n,label:i})=>{const s={value:t,target:e,velocity:n,arrayLength:null,objectKeys:null,normalizeInput:t=>t},o=t=>{if(null==s.arrayLength){if(null!=s.objectKeys)throw new Error(`${i} value shape mismatch (array vs object).`);s.arrayLength=t,u(s.value)||(s.value=p(s.value,t)),u(s.target)||(s.target=p(s.target,t)),void 0===s.velocity||u(s.velocity)||(s.velocity=p(s.velocity,t))}},r=t=>{if(null==s.objectKeys){if(null!=s.arrayLength)throw new Error(`${i} value shape mismatch (object vs array).`);s.objectKeys=t,a(s.value)||(s.value=w(s.value,t)),a(s.target)||(s.target=w(s.target,t)),void 0===s.velocity||a(s.velocity)||(s.velocity=w(s.velocity,t))}},c=t=>{if(u(t)){if(null!=s.objectKeys)throw new Error(`${i} value shape mismatch (array vs object).`);if(null==s.arrayLength&&o(t.length),t.length!==s.arrayLength)throw new Error(`${i} value length mismatch (expected ${s.arrayLength}, got ${t.length}).`);return t.slice()}if(a(t)){if(null!=s.arrayLength)throw new Error(`${i} value shape mismatch (object vs array).`);const e=Object.keys(t);if(null==s.objectKeys&&r(e),s.objectKeys&&e.length!==s.objectKeys.length)throw new Error(`${i} value keys mismatch (expected ${s.objectKeys.length}, got ${e.length}).`);if(s.objectKeys)for(const e of s.objectKeys)if(!(e in t))throw new Error(`${i} value keys mismatch (missing key "${e}").`);return{...t}}return null!=s.arrayLength?p(t,s.arrayLength):null!=s.objectKeys?w(t,s.objectKeys):t};s.normalizeInput=c;const h=u(t)||u(e)||void 0!==n&&u(n),f=a(t)||a(e)||void 0!==n&&a(n);if(h&&f)throw new Error(`${i} value shape mismatch (array vs object).`);if(h){const i=u(t)?t.length:u(e)?e.length:n.length;o(i),s.value=c(t),s.target=c(e),void 0!==s.velocity&&(s.velocity=c(s.velocity))}else if(f){const i=a(t)?Object.keys(t):a(e)?Object.keys(e):Object.keys(n);r(i),s.value=c(t),s.target=c(e),void 0!==s.velocity&&(s.velocity=c(s.velocity))}return s};let g={x:-1,y:-1};const v={stiffness:300,damping:30,mass:1},b=()=>({name:"pointer",extend:e=>{const{onMouseMove:n}=l().extend(e,e.host),{timer:i}=f().extend(e,e.host);g.x=window.innerWidth/2,g.y=window.innerHeight/2;const s={x:t({from:g.x,to:g.x,...v}),y:t({from:g.y,to:g.y,...v})};return n((t,e)=>{s.x.setTarget(t),s.y.setTarget(e)}),{onPointerMove:t=>{let e=g.x,n=g.y;i.raf(i=>{const o=s.x.next(i),r=s.y.next(i);var c,h;e===o&&n===r||(t({x:o,y:r,v:(c={x:o,y:r},h={x:e,y:n},{x:c.x-h.x,y:c.y-h.y,magnitude:Math.sqrt((c.x-h.x)*(c.x-h.x)+(c.y-h.y)*(c.y-h.y))}).magnitude}),e=o,n=r)})},onMouseMove:n}}}),S=()=>({name:"lerp",extend:t=>{const{timer:e}=f().extend(t,t.host),n=new Set;return t.onDestroy(()=>{for(const t of n)t();n.clear()}),{createLerp:t=>function({from:t=0,to:e=1,lerp:n=.1,tolerance:i=.001,resumeOnTarget:s=!0}={}){function o(){return d(f)}function r(t){if(w)return o();const e=(t=>t<0?0:t>1?1:t)(n);if(0===e)return o();if(1===e)return f=d(l),c.value=f,w=!0,o();const s=t>0?1-Math.pow(1-e,60*t):0;if(u(f)&&u(l)){let t=!0;for(let e=0;e<f.length;e+=1)f[e]+=(l[e]-f[e])*s,Math.abs(l[e]-f[e])>=i&&(t=!1);if(t){for(let t=0;t<f.length;t+=1)f[t]=l[t];w=!0}return c.value=f,d(f)}if(a(f)&&a(l)){const t=c.objectKeys??Object.keys(f);let e=!0;for(const n of t)f[n]+=(l[n]-f[n])*s,Math.abs(l[n]-f[n])>=i&&(e=!1);if(e){for(const e of t)f[e]=l[e];w=!0}return c.value=f,d(f)}const r=l;return f+=(r-f)*s,c.value=f,Math.abs(r-f)<i&&(f=r,c.value=f,w=!0),f}const c=y({from:t,to:e,label:"Lerp"}),h=c.normalizeInput;let f=c.value,l=c.target,p=null,w=!1;const g=new Set;return{setTarget:function(t){const e=h(t),n=!m(e,l);if(l=e,c.target=e,f=c.value,s&&w&&n){w=!1,p=null;for(const t of g)t(l)}},setValue:function(t,e={}){const{resetTime:n=!0,setTarget:i=!1,markDone:s=!1}=e;f=h(t),c.value=f,i&&(l=d(f),c.target=l);const o=w||!m(f,l);if(n&&(p=null),s&&(w=!0),o&&!s){w=!1,p=null;for(const t of g)t(l)}},getValue:o,isDone:function(){return w},onResume:function(t){return g.add(t),()=>{g.delete(t)}},step:r,next:function(t=performance.now()){if(null==p)return p=t,o();const e=(t-p)/1e3;p=t;const n=1/30;let i=e,s=o();for(;i>0&&!w;){const t=Math.min(i,n);s=r(t),i-=t}return s}}}(t),runLerp:(t,i,s={})=>{const{fps:o,immediate:r=!0,stopWhenDone:c=!0}=s;let h=!1,f=null;r&&i(t.getValue(),t);const l=()=>{f||(f=e.raf(e=>{if(h)return;const n=t.next(e);i(n,t),c&&t.isDone()&&u()},o))},u=()=>{f&&(f(),f=null)};l();const a=t.onResume(()=>{!h&&c&&(r&&i(t.getValue(),t),l())}),d=()=>{h||(h=!0,a(),u(),n.delete(d))};return n.add(d),d}}}}),j=()=>({name:"spring",extend:e=>{const{timer:n}=f().extend(e,e.host),i=new Set;return e.onDestroy(()=>{for(const t of i)t();i.clear()}),{createSpring:e=>t(e),runSpring:(t,e,s={})=>{const{fps:o,immediate:r=!0,stopWhenDone:c=!0}=s;let h=!1,f=null;r&&e(t.getValue(),t);const l=()=>{f||(f=n.raf(n=>{if(h)return;const i=t.next(n);e(i,t),c&&t.isDone()&&u()},o))},u=()=>{f&&(f(),f=null)};l();const a=t.onResume(()=>{!h&&c&&(r&&e(t.getValue(),t),l())}),d=()=>{h||(h=!0,a(),u(),i.delete(d))};return i.add(d),d}}}}),M="0.0.5",O=()=>{console.info("The website is using @petit-kit/scoped v"+M,"\nhttps://github.com/petit-kit/scoped")},_=/\{([A-Za-z_$][\w$]*)\}/g,$="bind:";export{M as SCOPE_VERSION,i as define,s as devicePlugin,O as happy,c as inViewPlugin,h as lenisPlugin,S as lerpPlugin,o as morphPlugin,l as mousePlugin,b as pointerPlugin,j as springPlugin,f as timerPlugin,r as windowPlugin};
|
|
1
|
+
function t({from:t=0,to:e=1,mass:n=1,stiffness:i=120,damping:s=14,velocity:o=0,tolerance:r=.001,resumeOnTarget:c=!0}={}){function h(){return d(v)}function f(t){if(M)return h();if(u(v)&&u(S)&&u(b)){let e=!0;for(let o=0;o<v.length;o+=1){const c=v[o]-S[o],h=(-i*c-s*b[o])/n;b[o]+=h*t,v[o]+=b[o]*t;const f=v[o]-S[o];(Math.abs(b[o])>=r||Math.abs(f)>=r)&&(e=!1)}if(e){for(let t=0;t<v.length;t+=1)v[t]=S[t],b[t]=0;M=!0}return l.value=v,l.velocity=b,d(v)}if(a(v)&&a(S)&&a(b)){const e=l.objectKeys??Object.keys(v);let o=!0;for(const c of e){const e=v[c]-S[c],h=(-i*e-s*b[c])/n;b[c]+=h*t,v[c]+=b[c]*t;const f=v[c]-S[c];(Math.abs(b[c])>=r||Math.abs(f)>=r)&&(o=!1)}if(o){for(const t of e)v[t]=S[t],b[t]=0;M=!0}return l.value=v,l.velocity=b,d(v)}const e=S;let o=b;o+=(-i*(v-e)-s*o)/n*t,v+=o*t,b=o,l.value=v,l.velocity=b;const c=v-e;return Math.abs(o)<r&&Math.abs(c)<r&&(v=e,b=0,l.value=v,l.velocity=b,M=!0),v}const l=y({from:t,to:e,velocity:o,label:"Spring"}),g=l.normalizeInput;let v=l.value,b=l.velocity??o,S=l.target,j=null,M=!1;const O=new Set;return{setTarget:function(t){const e=g(t),n=!m(e,S);if(S=e,l.target=e,v=l.value,c&&M&&n){M=!1,j=null;for(const t of O)t(S)}},setValue:function(t,e={}){const{resetVelocity:n=!0,resetTime:i=!0,setTarget:s=!1,markDone:o=!1}=e;v=g(t),l.value=v,s&&(S=d(v),l.target=S);const r=M||!m(v,S);if(n&&(b=null!=l.arrayLength?p(0,l.arrayLength):null!=l.objectKeys?w(0,l.objectKeys):0,l.velocity=b),i&&(j=null),o&&(M=!0),r&&!o){M=!1,j=null;for(const t of O)t(S)}},getValue:h,isDone:function(){return M},onResume:function(t){return O.add(t),()=>{O.delete(t)}},step:f,next:function(t=performance.now()){if(null==j)return j=t,h();const e=(t-j)/1e3;j=t;const n=1/30;let i=e,s=h();for(;i>0&&!M;){const t=Math.min(i,n);s=f(t),i-=t}return s}}}function e(t,e){if(!e||"object"!=typeof e||null==e.type)return null!=t?t:e;const{type:n,default:i}=e;if(null==t)return i;try{switch(n){case String:return String(t);case Number:{const e=Number(t);return Number.isNaN(e)?i:e}case Boolean:return""===t||"true"===t||"false"!==t&&"0"!==t&&null!=t;case Object:try{return"string"==typeof t?JSON.parse(t):t}catch{return i}case Array:try{return"string"==typeof t?JSON.parse(t):t}catch{return Array.isArray(i)?i:[]}default:return t}}catch{return i}}function n(t,e,n,i){if(!i||"object"!=typeof i||!i.reflect)return;let s=null;const o=i.type;if(o===Boolean)return n?void t.setAttribute(e,""):void t.removeAttribute(e);if(o===Object||o===Array)try{s=null==n?null:JSON.stringify(n)}catch{s=null}else s=null==n?null:String(n);null==s?t.removeAttribute(e):t.setAttribute(e,s)}function i(t,i={},s){const{props:o={},shadow:r=!1,styles:c,plugins:h}=i,f=h??[],l=()=>{};class u extends HTMLElement{constructor(){super(),this.version=M,this.t={};for(const t of Object.keys(o)){const e=o[t];this.t[t]=e&&"object"==typeof e&&("type"in e||"default"in e)?e:{type:void 0,default:e,reflect:!1}}this.props={},this.state={},this.actions={},this.refs={},this.emit=this.emit.bind(this),this.listen=this.listen.bind(this),this.setState=this.setState.bind(this),this.updateState=this.updateState.bind(this),this.setProps=this.setProps.bind(this),this.scheduleUpdate=this.scheduleUpdate.bind(this),this.update=this.update.bind(this),this.forceRender=this.forceRender.bind(this),this.destroy=this.destroy.bind(this),this.$=this.$.bind(this),this.i=null,this.o=null,this.h=!1,this.l=!1,this.u=r,this.m=r?this.attachShadow({mode:"open"}):this,this.p=null,this.S=[],this.j=[],this.M=[],this.O=[],this._=[],this.A=[],this.T=[],this.k=[],this.L=new Map,this.F=!1,this.I=!1,this.R={},this.V=!1,this.C=t,this.H=!1,this.D=new Set,this.U=!1,this.q=new Map,this.B=0,this.N=!1}W(t){const e=this.u?this.m.host:this;let n=t.parentElement;for(;n;){if(n===e)return!1;if(n.tagName.includes("-"))return!0;n=n.parentElement}return!1}static get observedAttributes(){return Object.keys(o)}attributeChangedCallback(t,n,i){if(n===i)return;const s=this.t[t],o=this.R[t],r=e(i,s);if(this.props[t]=r,this.F&&o!==r)for(const e of this.k)try{e(t,o,r)}catch(t){l(String(t?.message||t))}this.R[t]=r,this.D.has(t)?this.D.delete(t):this.i&&this.isConnected?this.U?this.H=!0:this.update(!0):this.H=!0}connectedCallback(){for(const t in this.t){if(!this.t.hasOwnProperty(t))continue;const n=e(this.getAttribute(t),this.t[t]);this.props[t]=n,this.R[t]=n}r||this.p||(this.p=this.P());let t=null;try{if(s){const e={props:this.props,state:this.state,actions:this.actions,refs:this.refs,emit:this.emit,listen:this.listen,updateState:this.updateState.bind(this),$:this.$,host:this,onMount:t=>this.M.push(t),onDestroy:t=>this.O.push(t),onUpdate:t=>this._.push(t),onBeforeUpdate:t=>this.A.push(t),onFirstUpdate:t=>this.T.push(t),onPropsChanged:t=>this.k.push(t),link:(t,e)=>{const i=e||t;this.state[i]=this.props[t],this.k.push((e,n,s)=>{e===t&&(Object.is(this.state[i],s)||(this.state[i]=s))});const s={fn:()=>{const e=this.state[i];if(Object.is(this.props[t],e))return;this.props[t]=e,this.R[t]=e;const s=this.t[t],o=s?{...s,reflect:!0}:s,r=this.getAttribute(t);this.D.add(t),n(this,t,e,o),r===this.getAttribute(t)&&this.D.delete(t)},deps:()=>[this.state[i]]};this.S.push(s)},computed:(t,e)=>{let n;if(void 0!==e)try{const t="function"==typeof e?e():e;Array.isArray(t)&&(n=t)}catch(t){String(t?.message||t)}const i={getter:t,deps:e,value:void 0!==e?t(n):t()};this.j.push(i);const s=()=>i.value;return s.J=i,this.K(s),s},effect:(t,e)=>{const n={fn:t,deps:e};return this.S.push(n),()=>this.Z(n)},delegate:(t,e,n)=>(this.G(t,e,n),()=>this.X(t,e,n))};for(const t of f)if(t)try{const n=t.extend(e,this);n&&"object"==typeof n&&Object.assign(e,n)}catch(t){l(String(t?.message||t))}t=s(e)}}catch(t){String(t?.message||t)}if(this.i="function"!=typeof t?()=>"":t,this.U=!0,this.update(!0),this.U=!1,this.H&&(this.H=!1,this.update(!0)),!this.F){this.F=!0;for(const t of this.M)try{t()}catch(t){l(String(t?.message||t))}}}disconnectedCallback(){this.destroy()}remove(){super.remove()}$(t){const e=this.m.querySelectorAll(t);return 0===e.length?null:1===e.length?e[0]:Array.from(e)}destroy(){for(const t of this.O)try{t()}catch(t){l(String(t?.message||t))}for(const t of this.S)if(t.cleanup){try{t.cleanup()}catch(t){l(String(t?.message||t))}t.cleanup=void 0}for(const[,t]of this.L)try{this.m.removeEventListener(t.eventType,t.listener)}catch{}this.L.clear(),this.F=!1}emit(t,e){this.dispatchEvent(new CustomEvent(t,{detail:e,bubbles:!0,composed:!0}))}listen(t,e,n,i){const s=n;t.addEventListener(e,s,i);const o=()=>{try{t.removeEventListener(e,s,i)}catch{}};return this.O.push(o),o}setState(t){let e=!1;const n=t,i=this.state;for(const t in n){if(!Object.prototype.hasOwnProperty.call(n,t))continue;const s=n[t];Object.is(i[t],s)||(i[t]=s,e=!0)}if(e)if(this.U||!this.F)this.update(!0);else{if(!this.i||!this.isConnected)return;if(this.h)return;this.h=!0,requestAnimationFrame(()=>{this.h=!1,this.i&&this.isConnected&&this.update(!0)})}}updateState(t){Object.assign(this.state,t),this.i&&this.isConnected&&this.Y()}setProps(t){const e=Object.keys(t);if(0===e.length)return;const i=[];for(const s of e){const e=t[s],o=this.R[s];this.props[s]=e,this.F&&o!==e&&i.push(s);const r=this.t[s];r&&r.reflect&&n(this,s,e,r),this.F&&o===e||(this.R[s]=e)}if(this.F&&i.length>0)for(const e of i){const n=this.R[e],i=t[e];for(const t of this.k)try{t(e,n,i)}catch(t){l(String(t?.message||t))}}this.i&&this.isConnected?this.update(!0):this.H=!0}scheduleUpdate(){this.i&&this.isConnected&&this.Y()}Y(){this.l||this.h||(this.l=!0,("function"==typeof queueMicrotask?queueMicrotask:t=>Promise.resolve().then(t))(()=>{this.l=!1,this.i&&this.isConnected&&(this.h||this.update(!1))}))}update(t){if(this.i){if(t&&this.F)for(const t of this.A)try{t()}catch(t){l(String(t?.message||t))}if(t){this.tt();let t="";try{t=this.i()}catch(e){String(e?.message||e),t=""}if("string"!=typeof t&&(t=null==t?"":String(t)),t=((t,e)=>{const n={...this.props,...e};return t.replace($,(t,e)=>{const i=n[e];return null==i?"":String(i)})})(t,this.state),!this.u){const e=`data-scope-owner="${this.C}"`;t=t.replace(/<slot(?![^>]*data-scope-owner)(\s|>)/g,`<slot ${e}$1`)}this.N=!1;const e=null!==this.o&&Object.is(this.o,t);let n=!1;e&&this.F||(this.u,this.m.innerHTML=t,this.o=t,n=!0),this.U?(this.et(),("function"==typeof requestAnimationFrame?requestAnimationFrame:t=>setTimeout(t,0))(()=>{this.i&&this.isConnected&&(n&&!r&&this.projectSlots(),n&&this.nt(),this.it(),this.st())})):(n&&!r&&this.projectSlots(),n&&this.ot(),this.it(),this.st())}else this.N&&this.tt(),this.it(),this.F&&this.st()}}forceRender(){this.o=null,this.i&&this.isConnected?this.U?this.H=!0:this.update(!0):this.H=!0}st(){if(!this.I){this.I=!0;for(const t of this.T)try{t()}catch(t){l(String(t?.message||t))}}for(const t of this._)try{t()}catch(t){l(String(t?.message||t))}this.rt()}rt(){const t=(this.u?this.m:this).querySelectorAll("*"),e=Object.prototype.hasOwnProperty,n=this.state,i=this.props;this.actions;for(let s=0;s<t.length;s++){const o=t[s];if(this.W(o))continue;if(0===o.attributes.length)continue;const r=o.attributes;for(let t=r.length-1;t>=0;t--){const s=r[t];if(!s.name.startsWith(_))continue;const c=s.name.slice(5),h=s.value,f=h?h.trim():"";let l,u=!1;if(f){const t=this.q.get(f);if(t){t.J&&(this.N=!0);try{l=t()}catch{}u=!0}}if(!u){const t=f||c,s=e.call(n,t),o=!s&&e.call(i,t);s?l=n[t]:o&&(l=i[t])}if("text"===c){const t=null==l?"":String(l);o.textContent!==t&&(o.textContent=t)}else if("html"===c){const t=null==l?"":String(l);o.innerHTML!==t&&(o.innerHTML=t)}else if(c in o){if(!Object.is(o[c],l))try{o[c]=l}catch{}if("value"===c)try{null==l?o.removeAttribute("value"):o.setAttribute("value",String(l))}catch{}}else if(null!=l)try{o.setAttribute(c,String(l))}catch{}const a=`__scopeBind_${c}`,d=o[a];if(d){const t=d.ct;t&&o.removeEventListener(t,d),delete o[a]}}}}tt(){for(const t of this.j){let e,n=!0;if(void 0!==t.deps)try{const i="function"==typeof t.deps?t.deps():t.deps;if(Array.isArray(i)&&(e=i,t.prevDeps&&t.prevDeps.length===e.length)){n=!1;for(let i=0;i<e.length;i++)if(!Object.is(t.prevDeps[i],e[i])){n=!0;break}}}catch(t){l(String(t?.message||t)),n=!0,e=void 0}if(n){try{t.value=void 0!==t.deps?t.getter(e):t.getter()}catch(t){l(String(t?.message||t))}e&&(t.prevDeps=e.slice())}}}it(){for(const t of this.S){let e,n=!0;if(void 0!==t.deps)try{const i="function"==typeof t.deps?t.deps():t.deps;if(Array.isArray(i)&&(e=i,t.prevDeps&&t.prevDeps.length===e.length)){n=!1;for(let i=0;i<e.length;i++)if(!Object.is(t.prevDeps[i],e[i])){n=!0;break}}}catch(t){l(String(t?.message||t)),n=!0,e=void 0}if(n){if(t.cleanup){try{t.cleanup()}catch{}t.cleanup=void 0}try{const n=void 0!==t.deps?t.fn(e):t.fn();"function"==typeof n&&(t.cleanup=n)}catch{}e&&(t.prevDeps=e.slice())}}}Z(t){const e=this.S.indexOf(t);if(-1!==e){if(t.cleanup)try{t.cleanup()}catch{}this.S.splice(e,1)}}K(t){const e=t.ht;if(e&&"string"==typeof e)return this.q.set(e,t),e;const n=`__scope_bind_${++this.B}__`;this.q.set(n,t);try{t.ht=n,t.toString=()=>n}catch{}return n}et(){const t=(this.u?this.m:this).querySelectorAll("[ref]"),e=this.refs;for(const t in e)e.hasOwnProperty(t)&&delete e[t];if(0!==t.length)for(let n=0;n<t.length;n++){const i=t[n];if(this.W(i))continue;const s=i.getAttribute("ref");s&&(e[s]?Array.isArray(e[s])?e[s].push(i):e[s]=[e[s],i]:e[s]=i)}}nt(){const t=(this.u?this.m:this).querySelectorAll("*");for(let e=0;e<t.length;e++){const n=t[e];if(this.W(n))continue;if(0===n.attributes.length)continue;const i=n.attributes;for(let t=i.length-1;t>=0;t--){const e=i[t];if(!e.name.startsWith("on:"))continue;const s=e.name.slice(3),o=e.value,r=`__tinyHandler_${s}`,c=n[r];c&&n.removeEventListener(s,c),n.removeAttribute(e.name);const h=this.actions[o];if(h&&"function"==typeof h){const t=t=>{h.call(this.actions,t)};n[r]=t,n.addEventListener(s,t)}}}}ot(){const t=(this.u?this.m:this).querySelectorAll("*"),e=this.refs;for(const t in e)e.hasOwnProperty(t)&&delete e[t];for(let n=0;n<t.length;n++){const i=t[n];if(this.W(i))continue;const s=i.getAttribute("ref");if(s&&(e[s]?Array.isArray(e[s])?e[s].push(i):e[s]=[e[s],i]:e[s]=i),i.attributes.length>0){const t=i.attributes;for(let e=t.length-1;e>=0;e--){const n=t[e];if(!n.name.startsWith("on:"))continue;const s=n.name.slice(3),o=n.value,r=`__tinyHandler_${s}`,c=i[r];c&&i.removeEventListener(s,c),i.removeAttribute(n.name);const h=this.actions[o];if(h&&"function"==typeof h){const t=t=>{h.call(this.actions,t)};i[r]=t,i.addEventListener(s,t)}}}}}P(){const t=new Map,e=this.childNodes,n=[];for(let t=0;t<e.length;t++)n.push(e[t]);for(let e=0;e<n.length;e++){const i=n[e];let s="";1===i.nodeType&&i.getAttribute&&(s=i.getAttribute("slot")||""),t.has(s)||t.set(s,[]),t.get(s).push(i)}for(let t=0;t<n.length;t++){const e=n[t];e.parentNode&&e.parentNode.removeChild(e)}return t}projectSlots(){const t=this.p||new Map,e=(this.u?this.m:this).querySelectorAll(`slot[data-scope-owner="${this.C}"]`);if(0!==e.length)for(let n=0;n<e.length;n++){const i=e[n],s=i.getAttribute("name")||"",o=t.get(s)||[];if(o.length){const t=document.createDocumentFragment();for(let e=0;e<o.length;e++){const n=o[e];let i;if(1===n.nodeType&&n.tagName.includes("-")&&n.p instanceof Map){const t=n,e=document.createElement(t.tagName.toLowerCase());for(let n=0;n<t.attributes.length;n++){const i=t.attributes[n];e.setAttribute(i.name,i.value)}for(const n of t.p.values())for(let t=0;t<n.length;t++)e.appendChild(n[t].cloneNode(!0));i=e}else i=n.cloneNode(!0);t.appendChild(i)}i.replaceWith(t)}else{const t=i.childNodes,e=[];for(let n=0;n<t.length;n++)e.push(t[n]);if(e.length>0){const t=document.createDocumentFragment();for(let n=0;n<e.length;n++)t.appendChild(e[n]);i.replaceWith(t)}}}}G(t,e,n){const i=`${t}::${e}`;let s=this.L.get(i);if(!s){const n=t=>{const n=t.target&&t.target.closest?t.target.closest(e):null;if(n)for(const e of s.handlers)try{e(t,n)}catch{}};s={eventType:t,selector:e,listener:n,handlers:new Set},this.L.set(i,s),this.m.addEventListener(t,n)}s.handlers.add(n)}X(t,e,n){const i=`${t}::${e}`,s=this.L.get(i);if(s&&(s.handlers.delete(n),0===s.handlers.size)){try{this.m.removeEventListener(t,s.listener)}catch{}this.L.delete(i)}}}if(!customElements.get(t)){if(c&&"undefined"!=typeof document){const e=`scope-${t}-styles`;if(!document.getElementById(e)){const t=document.createElement("style");t.id=e,t.textContent=c,document.head.appendChild(t)}}try{customElements.define(t,u)}catch(t){String(t?.message||t)}}return u}const s=()=>({name:"device",extend:t=>{const e=new Map;return t.onDestroy(()=>{for(const[t,n]of e)t.removeEventListener("change",n);e.clear()}),{onMediaQuery:(t,n,i={})=>{if("undefined"==typeof window||"undefined"==typeof matchMedia)return()=>{};const{immediate:s=!0}=i,o=matchMedia(t),r=t=>{n(t.matches,t)};return o.addEventListener("change",r),e.set(o,r),s&&n(o.matches,null),()=>{o.removeEventListener("change",r),e.delete(o)}}}}}),o=(t,e={})=>({name:"morph",extend:(n,i)=>{const{ignoreActiveValue:s=!0,callbacks:o}=e,r=t(),c=i,h=c.m,f=c.u?ShadowRoot.prototype:HTMLElement.prototype,l=Object.getOwnPropertyDescriptor(f,"innerHTML"),u=t=>{r.morph(h,t,{morphStyle:"innerHTML",ignoreActiveValue:s,callbacks:o})};let a=!0;return Object.defineProperty(h,"innerHTML",{set(t){a?(l.set.call(this,t),a=!1):u(t)},get(){return l.get.call(this)},configurable:!0}),n.onDestroy(()=>{delete h.innerHTML}),{morph:u}}}),r=()=>({name:"window",extend:t=>{const e=new Set,n=new Set,i=new Set,s=(t,n={})=>{if("undefined"==typeof window)return()=>{};const{immediate:i=!0}=n,s=e=>{t(window.innerWidth,window.innerHeight,e)};return window.addEventListener("resize",s),e.add(s),i&&s(new UIEvent("resize")),()=>{window.removeEventListener("resize",s),e.delete(s)}};return t.onDestroy(()=>{if("undefined"!=typeof window){for(const t of e)window.removeEventListener("resize",t);e.clear();for(const t of i)t.disconnect();i.clear();for(const t of n)window.removeEventListener("scroll",t);n.clear()}}),{onViewportResize:s,onWindowResize:(t,e={})=>{if("undefined"==typeof window)return()=>{};if("undefined"==typeof ResizeObserver)return s((e,n,i)=>t(e,n,i),e);const{immediate:n=!0}=e,o=document.documentElement,r=new ResizeObserver(e=>{const n=e[0];if(!n)return;const{width:i,height:s}=n.contentRect;t(i,s,n)});if(r.observe(o),i.add(r),n){const e=o.getBoundingClientRect();t(e.width,e.height,new UIEvent("resize"))}return()=>{r.disconnect(),i.delete(r)}},onWindowScroll:(t,e={})=>{if("undefined"==typeof window)return()=>{};const{immediate:i=!0}=e,s=e=>{t(window.scrollX,window.scrollY,e)};return window.addEventListener("scroll",s,{passive:!0}),n.add(s),i&&s(new Event("scroll")),()=>{window.removeEventListener("scroll",s),n.delete(s)}}}}}),c=()=>({name:"inview",extend:(t,e)=>{const n=new Set,i=(t,e,i={})=>{if("undefined"==typeof window||"undefined"==typeof IntersectionObserver)return()=>{};const{immediate:s=!0,...o}=i;let r=!s;const c=new IntersectionObserver(n=>{for(const i of n)i.target===t&&(r?r=!1:e(i.isIntersecting,i))},o);return c.observe(t),n.add(c),()=>{c.unobserve(t),c.disconnect(),n.delete(c)}};return t.onDestroy(()=>{for(const t of n)t.disconnect();n.clear()}),{onInView:(t,n)=>i(e,t,n),observeInView:(t,e,n)=>i(t,e,n)}}}),h=t=>({name:"lenis",extend:e=>{const n=new Set;return e.onDestroy(()=>{for(const{lenis:t,handler:e}of n)"function"==typeof t.off&&t.off("scroll",e);n.clear()}),{onLenisScroll:e=>{const i=t();if(!i)return()=>{};const s=t=>{e(t)};i.on("scroll",s);const o={lenis:i,handler:s};return n.add(o),()=>{n.has(o)&&(n.delete(o),"function"==typeof i.off&&i.off("scroll",s))}}}}}),f=()=>({name:"timer",extend:t=>{const e=new Set,n=new Set,i=new Set,s={setTimeout:(t,n,...i)=>{let s;return s=setTimeout((...n)=>{e.delete(s),t(...n)},n,...i),e.add(s),s},setInterval:(t,e,...i)=>{const s=setInterval(t,e,...i);return n.add(s),s},raf:(t,e)=>{let n=0,s=!0,o=0;const r="number"==typeof e&&e>0?1e3/e:0,c=e=>{if(i.delete(n),s){if(r){if(e-o>=r){const n=o?e-o:r;o=e,t(e,n)}}else{const n=o?e-o:0;o=e,t(e,n)}n=requestAnimationFrame(c),i.add(n)}};return n=requestAnimationFrame(c),i.add(n),()=>{s&&(s=!1,i.delete(n),cancelAnimationFrame(n))}}};return t.onDestroy(()=>{for(const t of e)clearTimeout(t);e.clear();for(const t of n)clearInterval(t);n.clear();for(const t of i)cancelAnimationFrame(t);i.clear()}),{timer:s}}}),l=()=>({name:"mouse",extend:t=>{const e=new Map,n=(t,n)=>{if("undefined"==typeof window)return()=>{};const i=t=>{const e=t;n(e.clientX,e.clientY,e)};window.addEventListener(t,i);let s=e.get(t);return s||(s=new Set,e.set(t,s)),s.add(i),()=>{window.removeEventListener(t,i),s?.delete(i)}};return t.onDestroy(()=>{if("undefined"!=typeof window){for(const[t,n]of e){for(const e of n)window.removeEventListener(t,e);n.clear()}e.clear()}}),{onMouseMove:t=>n("mousemove",t),onMouseDown:t=>n("mousedown",t),onMouseUp:t=>n("mouseup",t),onMouseWheel:t=>(t=>{if("undefined"==typeof window)return()=>{};const n=e=>{const n=e;t(n.clientX,n.clientY,n.deltaY,n)};window.addEventListener("wheel",n);let i=e.get("wheel");return i||(i=new Set,e.set("wheel",i)),i.add(n),()=>{window.removeEventListener("wheel",n),i?.delete(n)}})(t)}}}),u=t=>Array.isArray(t),a=t=>null!=t&&"object"==typeof t&&!Array.isArray(t),d=t=>u(t)?t.slice():a(t)?{...t}:t,m=(t,e)=>{if(u(t)&&u(e)){if(t.length!==e.length)return!1;for(let n=0;n<t.length;n+=1)if(!Object.is(t[n],e[n]))return!1;return!0}if(a(t)&&a(e)){const n=Object.keys(t),i=Object.keys(e);if(n.length!==i.length)return!1;for(const i of n){if(!(i in e))return!1;if(!Object.is(t[i],e[i]))return!1}return!0}return!(u(t)||u(e)||a(t)||a(e))&&Object.is(t,e)},p=(t,e)=>Array.from({length:e},()=>t),w=(t,e)=>e.reduce((e,n)=>(e[n]=t,e),{}),y=({from:t,to:e,velocity:n,label:i})=>{const s={value:t,target:e,velocity:n,arrayLength:null,objectKeys:null,normalizeInput:t=>t},o=t=>{if(null==s.arrayLength){if(null!=s.objectKeys)throw new Error(`${i} value shape mismatch (array vs object).`);s.arrayLength=t,u(s.value)||(s.value=p(s.value,t)),u(s.target)||(s.target=p(s.target,t)),void 0===s.velocity||u(s.velocity)||(s.velocity=p(s.velocity,t))}},r=t=>{if(null==s.objectKeys){if(null!=s.arrayLength)throw new Error(`${i} value shape mismatch (object vs array).`);s.objectKeys=t,a(s.value)||(s.value=w(s.value,t)),a(s.target)||(s.target=w(s.target,t)),void 0===s.velocity||a(s.velocity)||(s.velocity=w(s.velocity,t))}},c=t=>{if(u(t)){if(null!=s.objectKeys)throw new Error(`${i} value shape mismatch (array vs object).`);if(null==s.arrayLength&&o(t.length),t.length!==s.arrayLength)throw new Error(`${i} value length mismatch (expected ${s.arrayLength}, got ${t.length}).`);return t.slice()}if(a(t)){if(null!=s.arrayLength)throw new Error(`${i} value shape mismatch (object vs array).`);const e=Object.keys(t);if(null==s.objectKeys&&r(e),s.objectKeys&&e.length!==s.objectKeys.length)throw new Error(`${i} value keys mismatch (expected ${s.objectKeys.length}, got ${e.length}).`);if(s.objectKeys)for(const e of s.objectKeys)if(!(e in t))throw new Error(`${i} value keys mismatch (missing key "${e}").`);return{...t}}return null!=s.arrayLength?p(t,s.arrayLength):null!=s.objectKeys?w(t,s.objectKeys):t};s.normalizeInput=c;const h=u(t)||u(e)||void 0!==n&&u(n),f=a(t)||a(e)||void 0!==n&&a(n);if(h&&f)throw new Error(`${i} value shape mismatch (array vs object).`);if(h){const i=u(t)?t.length:u(e)?e.length:n.length;o(i),s.value=c(t),s.target=c(e),void 0!==s.velocity&&(s.velocity=c(s.velocity))}else if(f){const i=a(t)?Object.keys(t):a(e)?Object.keys(e):Object.keys(n);r(i),s.value=c(t),s.target=c(e),void 0!==s.velocity&&(s.velocity=c(s.velocity))}return s};let g={x:-1,y:-1};const v={stiffness:300,damping:30,mass:1},b=()=>({name:"pointer",extend:e=>{const{onMouseMove:n}=l().extend(e,e.host),{timer:i}=f().extend(e,e.host);g.x=window.innerWidth/2,g.y=window.innerHeight/2;const s={x:t({from:g.x,to:g.x,...v}),y:t({from:g.y,to:g.y,...v})};return n((t,e)=>{s.x.setTarget(t),s.y.setTarget(e)}),{onPointerMove:t=>{let e=g.x,n=g.y;i.raf(i=>{const o=s.x.next(i),r=s.y.next(i);var c,h;e===o&&n===r||(t({x:o,y:r,v:(c={x:o,y:r},h={x:e,y:n},{x:c.x-h.x,y:c.y-h.y,magnitude:Math.sqrt((c.x-h.x)*(c.x-h.x)+(c.y-h.y)*(c.y-h.y))}).magnitude}),e=o,n=r)})},onMouseMove:n}}}),S=()=>({name:"lerp",extend:t=>{const{timer:e}=f().extend(t,t.host),n=new Set;return t.onDestroy(()=>{for(const t of n)t();n.clear()}),{createLerp:t=>function({from:t=0,to:e=1,lerp:n=.1,tolerance:i=.001,resumeOnTarget:s=!0}={}){function o(){return d(f)}function r(t){if(w)return o();const e=(t=>t<0?0:t>1?1:t)(n);if(0===e)return o();if(1===e)return f=d(l),c.value=f,w=!0,o();const s=t>0?1-Math.pow(1-e,60*t):0;if(u(f)&&u(l)){let t=!0;for(let e=0;e<f.length;e+=1)f[e]+=(l[e]-f[e])*s,Math.abs(l[e]-f[e])>=i&&(t=!1);if(t){for(let t=0;t<f.length;t+=1)f[t]=l[t];w=!0}return c.value=f,d(f)}if(a(f)&&a(l)){const t=c.objectKeys??Object.keys(f);let e=!0;for(const n of t)f[n]+=(l[n]-f[n])*s,Math.abs(l[n]-f[n])>=i&&(e=!1);if(e){for(const e of t)f[e]=l[e];w=!0}return c.value=f,d(f)}const r=l;return f+=(r-f)*s,c.value=f,Math.abs(r-f)<i&&(f=r,c.value=f,w=!0),f}const c=y({from:t,to:e,label:"Lerp"}),h=c.normalizeInput;let f=c.value,l=c.target,p=null,w=!1;const g=new Set;return{setTarget:function(t){const e=h(t),n=!m(e,l);if(l=e,c.target=e,f=c.value,s&&w&&n){w=!1,p=null;for(const t of g)t(l)}},setValue:function(t,e={}){const{resetTime:n=!0,setTarget:i=!1,markDone:s=!1}=e;f=h(t),c.value=f,i&&(l=d(f),c.target=l);const o=w||!m(f,l);if(n&&(p=null),s&&(w=!0),o&&!s){w=!1,p=null;for(const t of g)t(l)}},getValue:o,isDone:function(){return w},onResume:function(t){return g.add(t),()=>{g.delete(t)}},step:r,next:function(t=performance.now()){if(null==p)return p=t,o();const e=(t-p)/1e3;p=t;const n=1/30;let i=e,s=o();for(;i>0&&!w;){const t=Math.min(i,n);s=r(t),i-=t}return s}}}(t),runLerp:(t,i,s={})=>{const{fps:o,immediate:r=!0,stopWhenDone:c=!0}=s;let h=!1,f=null;r&&i(t.getValue(),t);const l=()=>{f||(f=e.raf(e=>{if(h)return;const n=t.next(e);i(n,t),c&&t.isDone()&&u()},o))},u=()=>{f&&(f(),f=null)};l();const a=t.onResume(()=>{!h&&c&&(r&&i(t.getValue(),t),l())}),d=()=>{h||(h=!0,a(),u(),n.delete(d))};return n.add(d),d}}}}),j=()=>({name:"spring",extend:e=>{const{timer:n}=f().extend(e,e.host),i=new Set;return e.onDestroy(()=>{for(const t of i)t();i.clear()}),{createSpring:e=>t(e),runSpring:(t,e,s={})=>{const{fps:o,immediate:r=!0,stopWhenDone:c=!0}=s;let h=!1,f=null;r&&e(t.getValue(),t);const l=()=>{f||(f=n.raf(n=>{if(h)return;const i=t.next(n);e(i,t),c&&t.isDone()&&u()},o))},u=()=>{f&&(f(),f=null)};l();const a=t.onResume(()=>{!h&&c&&(r&&e(t.getValue(),t),l())}),d=()=>{h||(h=!0,a(),u(),i.delete(d))};return i.add(d),d}}}}),M="0.0.5",O=()=>{console.info("The website is using @petit-kit/scoped v"+M,"\nhttps://github.com/petit-kit/scoped")},$=/\{([A-Za-z_$][\w$]*)\}/g,_="bind:";export{M as SCOPE_VERSION,i as define,s as devicePlugin,O as happy,c as inViewPlugin,h as lenisPlugin,S as lerpPlugin,o as morphPlugin,l as mousePlugin,b as pointerPlugin,j as springPlugin,f as timerPlugin,r as windowPlugin};
|
|
2
2
|
//# sourceMappingURL=index.js.map
|