sveld 0.25.6 → 0.25.7
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 +31 -4
- package/lib/writer/writer-ts-definitions-core.js +15 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -537,7 +537,7 @@ Example:
|
|
|
537
537
|
|
|
538
538
|
#### Svelte 5 Snippet Compatibility
|
|
539
539
|
|
|
540
|
-
For Svelte 5 compatibility, `sveld` automatically generates optional snippet props for
|
|
540
|
+
For Svelte 5 compatibility, `sveld` automatically generates optional snippet props for all slots. This allows consumers to use either the traditional slot syntax or Svelte 5's `{#snippet}` syntax.
|
|
541
541
|
|
|
542
542
|
For slots with props (e.g., `let:prop`), the generated type uses a Snippet-compatible signature:
|
|
543
543
|
|
|
@@ -556,7 +556,32 @@ slotName?: (this: void) => void;
|
|
|
556
556
|
- **`this: void`** – Ensures the snippet cannot be called with a `this` context, matching Svelte's internal enforcement that snippets are pure render functions
|
|
557
557
|
- **`...args: [Props]`** – Uses tuple spread for type-safe parameters. This accepts fixed-length tuples (like `[{ row: Row }]`) while rejecting array types (like `Props[]`), matching how Svelte's `Snippet<T>` type works
|
|
558
558
|
|
|
559
|
-
**
|
|
559
|
+
**Default slot (`children` prop):**
|
|
560
|
+
|
|
561
|
+
The default slot generates an optional `children` snippet prop:
|
|
562
|
+
|
|
563
|
+
```svelte
|
|
564
|
+
<!-- Component with default slot that passes props -->
|
|
565
|
+
<Dropdown {items} selectedId="1">
|
|
566
|
+
{#snippet children({ item, index })}
|
|
567
|
+
<span>{item.text} (#{index})</span>
|
|
568
|
+
{/snippet}
|
|
569
|
+
</Dropdown>
|
|
570
|
+
```
|
|
571
|
+
|
|
572
|
+
Generated types:
|
|
573
|
+
|
|
574
|
+
```ts
|
|
575
|
+
type DropdownProps = {
|
|
576
|
+
items: Item[];
|
|
577
|
+
selectedId?: string;
|
|
578
|
+
|
|
579
|
+
// Default slot as children snippet prop
|
|
580
|
+
children?: (this: void, ...args: [{ item: Item; index: number }]) => void;
|
|
581
|
+
};
|
|
582
|
+
```
|
|
583
|
+
|
|
584
|
+
**Named slots:**
|
|
560
585
|
|
|
561
586
|
```svelte
|
|
562
587
|
<!-- Using the generated types with Svelte 5 syntax -->
|
|
@@ -582,6 +607,9 @@ type DataTableProps<Row> = {
|
|
|
582
607
|
this: void,
|
|
583
608
|
...args: [{ row: Row; cell: DataTableCell<Row>; rowIndex: number; cellIndex: number }]
|
|
584
609
|
) => void;
|
|
610
|
+
|
|
611
|
+
// Default slot as children prop
|
|
612
|
+
children?: (this: void) => void;
|
|
585
613
|
};
|
|
586
614
|
|
|
587
615
|
export default class DataTable<Row> extends SvelteComponentTyped<
|
|
@@ -589,13 +617,12 @@ export default class DataTable<Row> extends SvelteComponentTyped<
|
|
|
589
617
|
{ /* events */ },
|
|
590
618
|
{
|
|
591
619
|
// Traditional slot definition (Svelte 3/4)
|
|
620
|
+
default: Record<string, never>;
|
|
592
621
|
cell: { row: Row; cell: DataTableCell<Row>; rowIndex: number; cellIndex: number };
|
|
593
622
|
}
|
|
594
623
|
> {}
|
|
595
624
|
```
|
|
596
625
|
|
|
597
|
-
> **Note:** Snippet props are only generated for named slots. The default slot does not generate a snippet prop to avoid conflicts with the `children` prop pattern.
|
|
598
|
-
|
|
599
626
|
### `@event`
|
|
600
627
|
|
|
601
628
|
Use the `@event` tag to type dispatched events. An event name is required and a description optional.
|
|
@@ -95,7 +95,7 @@ function genPropDef(def) {
|
|
|
95
95
|
});
|
|
96
96
|
// Generate snippet props for named slots (Svelte 5 compatibility)
|
|
97
97
|
// Skip default slots and slots that conflict with existing prop names
|
|
98
|
-
const
|
|
98
|
+
const named_snippet_props = (def.slots || [])
|
|
99
99
|
.filter((slot) => !slot.default && slot.name != null && !existingPropNames.has(slot.name))
|
|
100
100
|
.map((slot) => {
|
|
101
101
|
const slotName = slot.name;
|
|
@@ -108,6 +108,20 @@ function genPropDef(def) {
|
|
|
108
108
|
return `
|
|
109
109
|
${description}${key}?: ${snippetType};`;
|
|
110
110
|
});
|
|
111
|
+
// Generate children snippet prop for default slot (Svelte 5 compatibility)
|
|
112
|
+
const default_slot = (def.slots || []).find((slot) => slot.default || slot.name === null);
|
|
113
|
+
const children_snippet_prop = default_slot
|
|
114
|
+
? (() => {
|
|
115
|
+
const description = default_slot.description ? `/** ${default_slot.description} */\n ` : "";
|
|
116
|
+
const hasSlotProps = default_slot.slot_props && default_slot.slot_props !== "Record<string, never>";
|
|
117
|
+
const snippetType = hasSlotProps
|
|
118
|
+
? `(this: void, ...args: [${default_slot.slot_props}]) => void`
|
|
119
|
+
: "(this: void) => void";
|
|
120
|
+
return `
|
|
121
|
+
${description}children?: ${snippetType};`;
|
|
122
|
+
})()
|
|
123
|
+
: "";
|
|
124
|
+
const snippet_props = [...named_snippet_props, children_snippet_prop].filter(Boolean);
|
|
111
125
|
const props = [...initial_props, ...snippet_props].join("\n");
|
|
112
126
|
const props_name = `${def.moduleName}Props`;
|
|
113
127
|
let prop_def = EMPTY_STR;
|