Skip to content

Commit 5ddcdd5

Browse files
committed
alternative approach
1 parent e549829 commit 5ddcdd5

File tree

11 files changed

+44
-70
lines changed

11 files changed

+44
-70
lines changed

.changeset/wise-grapes-enjoy.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
'svelte': minor
33
---
44

5-
feat: Add `idPrefix` option in `render`/`mount`/`hydrate` functions
5+
feat: Add `idPrefix` option to `render`

packages/svelte/src/index.d.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ export interface ComponentConstructorOptions<
1616
props?: Props;
1717
context?: Map<any, any>;
1818
hydrate?: boolean;
19-
idPrefix?: string;
2019
intro?: boolean;
2120
recover?: boolean;
2221
sync?: boolean;
@@ -338,10 +337,6 @@ export type MountOptions<Props extends Record<string, any> = Record<string, any>
338337
* @default true
339338
*/
340339
intro?: boolean;
341-
/**
342-
* Provide a prefix for the generated ID from `$props.id`
343-
*/
344-
idPrefix?: string;
345340
} & ({} extends Props
346341
? {
347342
/**

packages/svelte/src/internal/client/dom/hydration.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,6 @@ export function set_hydrating(value) {
2020
hydrating = value;
2121
}
2222

23-
export let id_prefix = '';
24-
25-
/** @param {string} v */
26-
export function set_id_prefix(v) {
27-
id_prefix = v;
28-
}
29-
3023
/**
3124
* The node that is currently being hydrated. This starts out as the first node inside the opening
3225
* <!--[--> comment, and updates each time a component calls `$.child(...)` or `$.sibling(...)`.

packages/svelte/src/internal/client/dom/template.js

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/** @import { Effect, TemplateNode } from '#client' */
2-
import { hydrate_next, hydrate_node, hydrating, id_prefix, set_hydrate_node } from './hydration.js';
2+
import { hydrate_next, hydrate_node, hydrating, set_hydrate_node } from './hydration.js';
33
import { create_text, get_first_child, is_firefox } from './operations.js';
44
import { create_fragment_from_html } from './reconciler.js';
55
import { active_effect } from '../runtime.js';
@@ -250,12 +250,6 @@ export function append(anchor, dom) {
250250
anchor.before(/** @type {Node} */ (dom));
251251
}
252252

253-
let uid = 1;
254-
255-
export function reset_props_id() {
256-
uid = 1;
257-
}
258-
259253
/**
260254
* Create (or hydrate) an unique UID for the component instance.
261255
*/
@@ -264,12 +258,16 @@ export function props_id() {
264258
hydrating &&
265259
hydrate_node &&
266260
hydrate_node.nodeType === 8 &&
267-
hydrate_node.textContent?.startsWith(`#${id_prefix}s`)
261+
hydrate_node.textContent?.startsWith(`#`)
268262
) {
269263
const id = hydrate_node.textContent.substring(1);
270264
hydrate_next();
271265
return id;
272266
}
273267

274-
return `${id_prefix}c${uid++}`;
268+
// @ts-expect-error
269+
(window.__svelte ??= {}).uid ??= 1;
270+
271+
// @ts-expect-error
272+
return `c${window.__svelte.uid++}`;
275273
}

packages/svelte/src/internal/client/render.js

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ import {
1717
hydrate_node,
1818
hydrating,
1919
set_hydrate_node,
20-
set_hydrating,
21-
set_id_prefix
20+
set_hydrating
2221
} from './dom/hydration.js';
2322
import { array_from } from '../shared/utils.js';
2423
import {
@@ -87,15 +86,13 @@ export function mount(component, options) {
8786
* context?: Map<any, any>;
8887
* intro?: boolean;
8988
* recover?: boolean;
90-
* idPrefix?: string;
9189
* } : {
9290
* target: Document | Element | ShadowRoot;
9391
* props: Props;
9492
* events?: Record<string, (e: any) => any>;
9593
* context?: Map<any, any>;
9694
* intro?: boolean;
9795
* recover?: boolean;
98-
* idPrefix?: string;
9996
* }} options
10097
* @returns {Exports}
10198
*/
@@ -168,10 +165,7 @@ const document_listeners = new Map();
168165
* @param {MountOptions} options
169166
* @returns {Exports}
170167
*/
171-
function _mount(
172-
Component,
173-
{ target, anchor, props = {}, events, context, intro = true, idPrefix }
174-
) {
168+
function _mount(Component, { target, anchor, props = {}, events, context, intro = true }) {
175169
init_operations();
176170

177171
var registered_events = new Set();
@@ -215,14 +209,12 @@ function _mount(
215209
var anchor_node = anchor ?? target.appendChild(create_text());
216210

217211
branch(() => {
218-
if (context || idPrefix != null) {
212+
if (context) {
219213
push({});
220214
var ctx = /** @type {ComponentContext} */ (component_context);
221215
if (context) {
222216
ctx.c = context;
223217
}
224-
225-
set_id_prefix(idPrefix ? idPrefix + '-' : '');
226218
}
227219

228220
if (events) {
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { PUBLIC_VERSION } from '../version.js';
22

3-
if (typeof window !== 'undefined')
4-
// @ts-ignore
5-
(window.__svelte ||= { v: new Set() }).v.add(PUBLIC_VERSION);
3+
if (typeof window !== 'undefined') {
4+
// @ts-expect-error
5+
((window.__svelte ??= {}).v ??= new Set()).add(PUBLIC_VERSION);
6+
}

packages/svelte/src/legacy/legacy-client.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,7 @@ class Svelte4Component {
116116
props,
117117
context: options.context,
118118
intro: options.intro ?? false,
119-
recover: options.recover,
120-
idPrefix: options.idPrefix
119+
recover: options.recover
121120
});
122121

123122
// We don't flushSync for custom element wrappers or if the user doesn't want it

packages/svelte/tests/runtime-legacy/shared.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import { setup_html_equal } from '../html_equal.js';
1111
import { raf } from '../animation-helpers.js';
1212
import type { CompileOptions } from '#compiler';
1313
import { suite_with_variants, type BaseTest } from '../suite.js';
14-
import { reset_props_id } from '../../src/internal/client/dom/template.js';
1514

1615
type Assert = typeof import('vitest').assert & {
1716
htmlEqual(a: string, b: string, description?: string): void;
@@ -348,7 +347,10 @@ async function run_test_variant(
348347

349348
if (runes) {
350349
props = proxy({ ...(config.props || {}) });
351-
reset_props_id();
350+
351+
// @ts-expect-error
352+
globalThis.__svelte.uid = 1;
353+
352354
if (manual_hydrate) {
353355
hydrate_fn = () => {
354356
instance = hydrate(mod.default, {
@@ -364,8 +366,7 @@ async function run_test_variant(
364366
target,
365367
props,
366368
intro: config.intro,
367-
recover: config.recover ?? false,
368-
idPrefix: config.id_prefix
369+
recover: config.recover ?? false
369370
});
370371
}
371372
} else {

packages/svelte/tests/runtime-runes/samples/props-id-prefix/_config.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ export default test({
99
target.innerHTML,
1010
`
1111
<button>toggle</button>
12-
<h1>myPrefix-c1</h1>
13-
<p>myPrefix-c2</p>
14-
<p>myPrefix-c3</p>
15-
<p>myPrefix-c4</p>
12+
<h1>c1</h1>
13+
<p>c2</p>
14+
<p>c3</p>
15+
<p>c4</p>
1616
`
1717
);
1818
} else {
@@ -36,11 +36,11 @@ export default test({
3636
target.innerHTML,
3737
`
3838
<button>toggle</button>
39-
<h1>myPrefix-c1</h1>
40-
<p>myPrefix-c2</p>
41-
<p>myPrefix-c3</p>
42-
<p>myPrefix-c4</p>
43-
<p>myPrefix-c5</p>
39+
<h1>c1</h1>
40+
<p>c2</p>
41+
<p>c3</p>
42+
<p>c4</p>
43+
<p>c5</p>
4444
`
4545
);
4646
} else {
@@ -52,7 +52,7 @@ export default test({
5252
<p>myPrefix-s2</p>
5353
<p>myPrefix-s3</p>
5454
<p>myPrefix-s4</p>
55-
<p>myPrefix-c1</p>
55+
<p>c1</p>
5656
`
5757
);
5858
}

packages/svelte/types/index.d.ts

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ declare module 'svelte' {
1313
props?: Props;
1414
context?: Map<any, any>;
1515
hydrate?: boolean;
16-
idPrefix?: string;
1716
intro?: boolean;
1817
recover?: boolean;
1918
sync?: boolean;
@@ -335,10 +334,6 @@ declare module 'svelte' {
335334
* @default true
336335
*/
337336
intro?: boolean;
338-
/**
339-
* Provide a prefix for the generated ID from `$props.id`
340-
*/
341-
idPrefix?: string;
342337
} & ({} extends Props
343338
? {
344339
/**
@@ -490,15 +485,13 @@ declare module 'svelte' {
490485
context?: Map<any, any>;
491486
intro?: boolean;
492487
recover?: boolean;
493-
idPrefix?: string;
494488
} : {
495489
target: Document | Element | ShadowRoot;
496490
props: Props;
497491
events?: Record<string, (e: any) => any>;
498492
context?: Map<any, any>;
499493
intro?: boolean;
500494
recover?: boolean;
501-
idPrefix?: string;
502495
}): Exports;
503496
/**
504497
* Unmounts a component that was previously mounted using `mount` or `hydrate`.
@@ -1882,10 +1875,10 @@ declare module 'svelte/motion' {
18821875
* const tween = Tween.of(() => number);
18831876
* </script>
18841877
* ```
1885-
*
1878+
*
18861879
*/
18871880
static of<U>(fn: () => U, options?: TweenedOptions<U> | undefined): Tween<U>;
1888-
1881+
18891882
constructor(value: T, options?: TweenedOptions<T>);
18901883
/**
18911884
* Sets `tween.target` to `value` and returns a `Promise` that resolves if and when `tween.current` catches up to it.
@@ -1904,21 +1897,21 @@ declare module 'svelte/motion' {
19041897

19051898
declare module 'svelte/reactivity' {
19061899
export class SvelteDate extends Date {
1907-
1900+
19081901
constructor(...params: any[]);
19091902
#private;
19101903
}
19111904
export class SvelteSet<T> extends Set<T> {
1912-
1905+
19131906
constructor(value?: Iterable<T> | null | undefined);
1914-
1907+
19151908
add(value: T): this;
19161909
#private;
19171910
}
19181911
export class SvelteMap<K, V> extends Map<K, V> {
1919-
1912+
19201913
constructor(value?: Iterable<readonly [K, V]> | null | undefined);
1921-
1914+
19221915
set(key: K, value: V): this;
19231916
#private;
19241917
}
@@ -1928,7 +1921,7 @@ declare module 'svelte/reactivity' {
19281921
}
19291922
const REPLACE: unique symbol;
19301923
export class SvelteURLSearchParams extends URLSearchParams {
1931-
1924+
19321925
[REPLACE](params: URLSearchParams): void;
19331926
#private;
19341927
}
@@ -2000,7 +1993,7 @@ declare module 'svelte/reactivity' {
20001993
*/
20011994
export function createSubscriber(start: (update: () => void) => (() => void) | void): () => void;
20021995
class ReactiveValue<T> {
2003-
1996+
20041997
constructor(fn: () => T, onsubscribe: (update: () => void) => void);
20051998
get current(): T;
20061999
#private;
@@ -2065,7 +2058,7 @@ declare module 'svelte/reactivity/window' {
20652058
get current(): number | undefined;
20662059
};
20672060
class ReactiveValue<T> {
2068-
2061+
20692062
constructor(fn: () => T, onsubscribe: (update: () => void) => void);
20702063
get current(): T;
20712064
#private;

0 commit comments

Comments
 (0)