Skip to content

Commit f6cd1ab

Browse files
Merge pull request #2096 from appwrite/small-fixes-svelte-5
svelte5: code snippet components and optimize markup
2 parents 6eb9df5 + 1fbbb14 commit f6cd1ab

File tree

2 files changed

+29
-44
lines changed

2 files changed

+29
-44
lines changed

src/lib/components/MultiCodeContextless.svelte

Lines changed: 26 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import { getCodeHtml, type Language } from '$lib/utils/code';
44
import { copy } from '$lib/utils/copy';
55
import { platformMap } from '$lib/utils/references';
6-
import { writable } from 'svelte/store';
6+
import { SvelteSet } from 'svelte/reactivity';
77
88
interface Props {
99
selected?: Language;
@@ -14,18 +14,26 @@
1414
1515
let { selected = $bindable('js'), data = [], width = null, height = null }: Props = $props();
1616
17-
let snippets = $derived(writable(new Set(data.map((d) => d.language))));
18-
const getSnippets = () => {
19-
return snippets;
20-
};
21-
22-
let content = $derived(data.find((d) => d.language === selected)?.content ?? '');
23-
24-
let platform = $derived(data.find((d) => d.language === selected)?.platform ?? '');
17+
const snippets = $derived(new SvelteSet(data.map((d) => d.language)));
18+
const content = $derived(data.find((d) => d.language === selected)?.content ?? '');
19+
const platform = $derived(data.find((d) => d.language === selected)?.platform ?? '');
20+
const result = $derived(
21+
getCodeHtml({
22+
content,
23+
language: selected ?? 'sh',
24+
withLineNumbers: true
25+
})
26+
);
27+
const options = $derived(
28+
Array.from(snippets).map((language) => ({
29+
value: language,
30+
label: platformMap[language]
31+
}))
32+
);
2533
26-
getSnippets().subscribe((n) => {
27-
if (selected === null && n.size > 0) {
28-
selected = Array.from(n)[0] as Language;
34+
$effect(() => {
35+
if (selected === null && snippets.size > 0) {
36+
selected = Array.from(snippets)[0] as Language;
2937
}
3038
});
3139
@@ -35,9 +43,7 @@
3543
} as const;
3644
type CopyStatusType = keyof typeof CopyStatus;
3745
type CopyStatusValue = (typeof CopyStatus)[CopyStatusType];
38-
39-
let copyText = $state<CopyStatusValue>(CopyStatus.Copy);
40-
46+
let copyText: CopyStatusValue = $state(CopyStatus.Copy);
4147
async function handleCopy() {
4248
await copy(content);
4349
@@ -46,28 +52,13 @@
4652
copyText = CopyStatus.Copy;
4753
}, 1000);
4854
}
49-
50-
let result = $derived(
51-
getCodeHtml({
52-
content,
53-
language: selected ?? 'sh',
54-
withLineNumbers: true
55-
})
56-
);
57-
let options = $derived(
58-
Array.from($snippets).map((language) => ({
59-
value: language,
60-
label: platformMap[language]
61-
}))
62-
);
6355
</script>
6456

6557
<section
6658
class="dark web-code-snippet mx-auto w-full lg:!max-w-[90vw]"
6759
aria-label="code-snippet panel"
68-
style={`width: ${width ? width / 16 + 'rem' : 'inherit'}; height: ${
69-
height ? height / 16 + 'rem' : 'inherit'
70-
}`}
60+
style:width={width ? width / 16 + 'rem' : 'inherit'}
61+
style:height={height ? height / 16 + 'rem' : 'inherit'}
7162
>
7263
<header class="web-code-snippet-header">
7364
<div class="web-code-snippet-header-start">
@@ -79,9 +70,9 @@
7970
</div>
8071
<div class="web-code-snippet-header-end">
8172
<ul class="buttons-list flex gap-3">
82-
{#if $snippets.entries.length}
73+
{#if snippets.size}
8374
<li class="buttons-list-item flex self-center">
84-
<Select bind:value={selected} bind:options />
75+
<Select bind:value={selected} {options} />
8576
</li>
8677
{/if}
8778
<li class="buttons-list-item" style="padding-inline-start: 13px">
@@ -104,7 +95,7 @@
10495
</header>
10596
<div
10697
class="web-code-snippet-content overflow-auto"
107-
style={`height: ${height ? height / 16 + 'rem' : 'inherit'}`}
98+
style:height={height ? height / 16 + 'rem' : 'inherit'}
10899
>
109100
<!-- eslint-disable-next-line svelte/no-at-html-tags -->
110101
{@html result}

src/lib/components/shared/dialog.svelte

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import { fade, scale } from 'svelte/transition';
33
import { createDialog, melt } from '@melt-ui/svelte';
44
import type { Snippet } from 'svelte';
5-
import { browser } from '$app/environment';
65
76
type Props = {
87
url: string;
@@ -12,7 +11,7 @@
1211
};
1312
1413
const {
15-
elements: { portalled, trigger, content, overlay },
14+
elements: { trigger, content, overlay },
1615
states: { open }
1716
} = createDialog({
1817
forceVisible: true,
@@ -27,14 +26,9 @@
2726
{@render children()}
2827
</div>
2928
{:else}
30-
<button
31-
class="contents cursor-pointer"
32-
onclick={() => {
33-
if (browser && window) window.open(url, '_blank');
34-
}}
35-
>
29+
<a href={url} target="_blank" rel="noopener noreferrer" class="contents cursor-pointer">
3630
{@render children()}
37-
</button>
31+
</a>
3832
{/if}
3933

4034
{#if $open}

0 commit comments

Comments
 (0)