svelte-origin 1.0.0-next.23 → 1.0.0-next.25

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/LLM.md CHANGED
@@ -102,8 +102,7 @@ function $origin<Parents extends OriginFactory[], T>(parents: Parents, definitio
102
102
 
103
103
  | Property | Description |
104
104
  |----------|-------------|
105
- | `props: $attrs({...})` | Props schema (becomes component props) |
106
- | `attrs: $attrs({...})` | Alternative name for props schema |
105
+ | `props: $attrs({...})` | Props schema (recommended name) |
107
106
  | `_privateField` | Private state/method (not exposed on instance) |
108
107
  | `get propertyName()` | Derived value (use `$derived` inside) |
109
108
  | `methodName()` | Method accessible on the instance |
@@ -313,6 +312,99 @@ export const Timer = $origin({
313
312
  - Can return a cleanup function that runs when the component is destroyed
314
313
  - Useful for side effects, subscriptions, or DOM interactions
315
314
 
315
+ ## Origin Destructuring
316
+
317
+ Destructure methods and props from origin instances with automatic handling.
318
+
319
+ ### Method Destructuring
320
+
321
+ Methods are automatically bound to preserve `this` context:
322
+
323
+ ```svelte
324
+ <script lang="ts">
325
+ import { Counter } from './counter.svelte'
326
+ let counter = $attrs.origin(Counter)
327
+
328
+ // Destructure methods - automatically bound
329
+ let { increment, decrement } = counter
330
+
331
+ // Transforms to:
332
+ // let increment = counter.increment.bind(counter)
333
+ // let decrement = counter.decrement.bind(counter)
334
+ </script>
335
+
336
+ <button onclick={increment}>+</button>
337
+ <button onclick={decrement}>-</button>
338
+ ```
339
+
340
+ ### Props Destructuring
341
+
342
+ Props are automatically wrapped in `$derived` for reactivity:
343
+
344
+ ```svelte
345
+ <script lang="ts">
346
+ import { Counter } from './counter.svelte'
347
+ let counter = $attrs.origin(Counter)
348
+
349
+ // Destructure props - automatically reactive
350
+ let { count, label } = counter.props
351
+
352
+ // Transforms to:
353
+ // let count = $derived(counter.props.count)
354
+ // let label = $derived(counter.props.label)
355
+ </script>
356
+
357
+ <p>{label}: {count}</p>
358
+ ```
359
+
360
+ ### Nested Props Destructuring
361
+
362
+ Combine method and props destructuring in a single pattern:
363
+
364
+ ```svelte
365
+ <script lang="ts">
366
+ let { increment, props: { count, label } } = counter
367
+
368
+ // Transforms to:
369
+ // let increment = counter.increment.bind(counter)
370
+ // let count = $derived(counter.props.count)
371
+ // let label = $derived(counter.props.label)
372
+ </script>
373
+ ```
374
+
375
+ ### Aliases and Defaults
376
+
377
+ ```svelte
378
+ <script lang="ts">
379
+ // Alias methods
380
+ let { increment: inc } = counter
381
+
382
+ // Alias props with defaults
383
+ let { count: counterValue = 0 } = counter.props
384
+
385
+ // Transforms to:
386
+ // let inc = counter.increment.bind(counter)
387
+ // let counterValue = $derived(counter.props.count ?? 0)
388
+ </script>
389
+ ```
390
+
391
+ ### Coexistence with $props()
392
+
393
+ When you have your own `$props()` and also use `$attrs.origin()`:
394
+
395
+ ```svelte
396
+ <script lang="ts">
397
+ import { Counter } from './counter.svelte'
398
+
399
+ let { myOwnProp = 'hello' } = $props()
400
+ let counter = $attrs.origin(Counter)
401
+
402
+ // Transforms to:
403
+ // let { myOwnProp = 'hello', ...___originAttrs } = $props()
404
+ // let counter = Counter(__attrsFor(Counter, ___originAttrs))
405
+ </script>
406
+ ```
407
+
316
408
  ## Attachments
317
409
 
318
410
  Origins can define **attachment methods** that run when an element is attached to the DOM. These are methods prefixed with `$` that receive the element and optionally return a cleanup function.
@@ -507,13 +599,13 @@ interface SvelteOriginFactory<TDef, TAllAttrs> {
507
599
 
508
600
  ```ts
509
601
  type SvelteOriginInstance<T> = {
510
- props: ReactiveAttrs<T['props']> // Or 'attrs' if that's what you named it
602
+ props: ReactiveAttrs<T['props']>
511
603
  super?: ParentInstance
512
604
  $attachments: SvelteOriginAttachments
513
605
  } & PublicMembers<T>
514
606
  ```
515
607
 
516
- **Note:** The `props`/`attrs` property name matches what you use in your origin definition.
608
+ **Note:** The property name `props` is the recommended convention for the props schema.
517
609
 
518
610
  ## Common Patterns
519
611
 
package/README.md CHANGED
@@ -121,8 +121,6 @@ svelteOrigin({
121
121
 
122
122
  Write origins in a file where runes are allowed (e.g. `.svelte.ts`).
123
123
 
124
- This README describes the **macro** style (preferred direction):
125
-
126
124
  ```ts
127
125
  // counter.svelte.ts
128
126
  export const Counter = $origin({
@@ -164,11 +162,11 @@ export const Counter = $origin({
164
162
  Notes:
165
163
 
166
164
  - `$origin(...)` is a **compile-time macro** provided by `svelte-origin` (not a Svelte rune)
167
- - `props: $attrs({...})` is also a macro in origin definitions
168
- - this uses `$attrs` to avoid conflict with Svelte's `$props()` rune
169
- - it compiles into the origin's prop schema and default/bindable handling
170
- - the property name (`props`) is arbitrary—you can name it `attrs`, `options`, or anything else
171
- - `$bindable(...)` inside `props: $attrs({...})` is treated as a marker (macro) and compiles away
165
+ - `props: $attrs({...})` defines the component props schema
166
+ - uses `$attrs` to avoid conflict with Svelte's `$props()` rune
167
+ - compiles into the origin's prop schema with defaults and bindable handling
168
+ - the property name (`props`) is the recommended convention
169
+ - `$bindable(...)` inside `props: $attrs({...})` marks props for two-way binding
172
170
  - `$state`/`$derived` are real Svelte runes
173
171
  - Properties prefixed with `_` (like `_incrementCount`) are private and not exposed on the instance type
174
172
 
@@ -393,6 +391,62 @@ svelte-package && svelte-origin post-process [dist-dir]
393
391
 
394
392
  See `svelte-origin --help` for all options.
395
393
 
394
+ ## Origin Destructuring
395
+
396
+ Destructure methods and props from origin instances with automatic handling:
397
+
398
+ ### Method Destructuring
399
+
400
+ Methods are automatically bound to preserve `this` context:
401
+
402
+ ```svelte
403
+ <script lang="ts">
404
+ import { Counter } from './counter.svelte'
405
+ let counter = $attrs.origin(Counter)
406
+
407
+ // Destructure methods - automatically bound
408
+ let { increment, decrement } = counter
409
+ </script>
410
+
411
+ <button onclick={increment}>+</button>
412
+ <button onclick={decrement}>-</button>
413
+ ```
414
+
415
+ ### Props Destructuring
416
+
417
+ Props are automatically wrapped in `$derived` for reactivity:
418
+
419
+ ```svelte
420
+ <script lang="ts">
421
+ import { Counter } from './counter.svelte'
422
+ let counter = $attrs.origin(Counter)
423
+
424
+ // Destructure props - automatically reactive
425
+ let { count, label } = counter.props
426
+ </script>
427
+
428
+ <p>{label}: {count}</p>
429
+ ```
430
+
431
+ ### Nested Destructuring
432
+
433
+ Combine method and props destructuring:
434
+
435
+ ```svelte
436
+ <script lang="ts">
437
+ let { increment, props: { count, label } } = counter
438
+ </script>
439
+ ```
440
+
441
+ ### Aliases and Defaults
442
+
443
+ ```svelte
444
+ <script lang="ts">
445
+ let { increment: inc } = counter
446
+ let { count: counterValue = 0 } = counter.props
447
+ </script>
448
+ ```
449
+
396
450
  ## Known limitations
397
451
 
398
452
  ### svelte-check type inference