Skip to content

Commit 857f4e6

Browse files
Demo - Migrate to Svelte 5 (#222)
1 parent 54f9b25 commit 857f4e6

File tree

3 files changed

+47
-42
lines changed

3 files changed

+47
-42
lines changed

demo/Index.svelte

Lines changed: 41 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -7,55 +7,56 @@
77
const urlParameter = new URLSearchParams(location.search);
88
const parsedUrlParameter = parseUrl(urlParameter.get('url'), 'https://github.com').href;
99
// Parse partial URL in the parameter so that it's shown as full URL in the field
10-
let urlField = parsedUrlParameter || '';
10+
let urlField = $state(parsedUrlParameter || '');
1111
1212
const allUrls = [...getAllUrls()].sort();
1313
14-
let parsedUrl;
1514
// Do not use ?? because it should work on empty strings
16-
$: parsedUrl = parseUrl(urlField || defaultUrl);
15+
let parsedUrl = $derived(parseUrl(urlField || defaultUrl));
1716
18-
let detections = [];
19-
$: {
20-
if (parsedUrl) {
21-
if (urlField) {
22-
urlParameter.set('url', urlField.replace('https://github.com', ''));
23-
history.replaceState(null, '', `?${urlParameter}`);
24-
} else {
25-
history.replaceState(null, '', location.pathname);
26-
}
27-
detections = Object.entries(urlDetection)
28-
.map(([name, detect]) => {
29-
if (typeof detect !== 'function') {
30-
return;
31-
}
17+
let detections = $derived(
18+
parsedUrl ? Object.entries(urlDetection)
19+
.map(([name, detect]) => {
20+
if (typeof detect !== 'function') {
21+
return;
22+
}
3223
33-
if (!String(detect).startsWith('()')) {
34-
return {
35-
name,
36-
detect,
37-
result: detect(parsedUrl)
38-
};
39-
} else {
40-
return {name};
41-
}
42-
})
43-
.filter(Boolean)
44-
.sort((a, b) => {
45-
// Pull true values to the top
46-
if (a.result || b.result) {
47-
return a.result ? b.result ? 0 : -1 : 1;
48-
}
24+
if (!String(detect).startsWith('()')) {
25+
return {
26+
name,
27+
detect,
28+
result: detect(parsedUrl)
29+
};
30+
} else {
31+
return {name};
32+
}
33+
})
34+
.filter(Boolean)
35+
.sort((a, b) => {
36+
// Pull true values to the top
37+
if (a.result || b.result) {
38+
return a.result ? b.result ? 0 : -1 : 1;
39+
}
4940
50-
// Push false values to the top
51-
if (a.detect || b.detect) {
52-
return a.detect ? b.detect ? 0 : 1 : -1;
53-
}
41+
// Push false values to the top
42+
if (a.detect || b.detect) {
43+
return a.detect ? b.detect ? 0 : 1 : -1;
44+
}
5445
55-
// DOM-based detections should be in the middle
56-
});
46+
// DOM-based detections should be in the middle
47+
}) : []
48+
);
49+
50+
$effect(() => {
51+
if (!parsedUrl) return;
52+
53+
if (urlField) {
54+
urlParameter.set('url', urlField.replace('https://github.com', ''));
55+
history.replaceState(null, '', `?${urlParameter}`);
56+
} else {
57+
history.replaceState(null, '', location.pathname);
5758
}
58-
}
59+
});
5960
</script>
6061

6162
<style>

demo/detections.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import { mount } from 'svelte';
2+
13
import Detections from './Detections.svelte';
24

3-
const app = new Detections({
5+
const app = mount(Detections, {
46
target: document.querySelector('main'),
57
});
68

demo/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import { mount } from 'svelte';
2+
13
import Index from './Index.svelte';
24

3-
const app = new Index({
5+
const app = mount(Index, {
46
target: document.querySelector('main'),
57
});
68

0 commit comments

Comments
 (0)