chore(web): migration svelte 5 syntax (#13883)

This commit is contained in:
Alex
2024-11-14 08:43:25 -06:00
committed by GitHub
parent 9203a61709
commit 0b3742cf13
310 changed files with 6435 additions and 4176 deletions

View File

@@ -1,4 +1,4 @@
<script lang="ts" context="module">
<script lang="ts" module>
export type AccordionState = Set<string>;
const { get: getAccordionState, set: setAccordionState } = createContext<Writable<AccordionState>>();
@@ -11,25 +11,33 @@
import { page } from '$app/stores';
import { handlePromiseError } from '$lib/utils';
import { goto } from '$app/navigation';
import type { Snippet } from 'svelte';
const getParamValues = (param: string) => {
return new Set(($page.url.searchParams.get(param) || '').split(' ').filter((x) => x !== ''));
};
export let queryParam: string;
export let state: Writable<AccordionState> = writable(getParamValues(queryParam));
interface Props {
queryParam: string;
state?: Writable<AccordionState>;
children?: Snippet;
}
let { queryParam, state = writable(getParamValues(queryParam)), children }: Props = $props();
setAccordionState(state);
$: if (queryParam && $state) {
const searchParams = new URLSearchParams($page.url.searchParams);
if ($state.size > 0) {
searchParams.set(queryParam, [...$state].join(' '));
} else {
searchParams.delete(queryParam);
}
$effect(() => {
if (queryParam && $state) {
const searchParams = new URLSearchParams($page.url.searchParams);
if ($state.size > 0) {
searchParams.set(queryParam, [...$state].join(' '));
} else {
searchParams.delete(queryParam);
}
handlePromiseError(goto(`?${searchParams.toString()}`, { replaceState: true, noScroll: true, keepFocus: true }));
}
handlePromiseError(goto(`?${searchParams.toString()}`, { replaceState: true, noScroll: true, keepFocus: true }));
}
});
</script>
<slot />
{@render children?.()}

View File

@@ -1,21 +1,34 @@
<script lang="ts">
import { slide } from 'svelte/transition';
import { getAccordionState } from './setting-accordion-state.svelte';
import { onDestroy } from 'svelte';
import { onDestroy, onMount, type Snippet } from 'svelte';
import Icon from '$lib/components/elements/icon.svelte';
const accordionState = getAccordionState();
export let title: string;
export let subtitle = '';
export let key: string;
export let isOpen = $accordionState.has(key);
export let autoScrollTo = false;
export let icon = '';
interface Props {
title: string;
subtitle?: string;
key: string;
isOpen?: boolean;
autoScrollTo?: boolean;
icon?: string;
subtitleSnippet?: Snippet;
children?: Snippet;
}
let accordionElement: HTMLDivElement;
let {
title,
subtitle = '',
key,
isOpen = $bindable($accordionState.has(key)),
autoScrollTo = false,
icon = '',
subtitleSnippet,
children,
}: Props = $props();
$: setIsOpen(isOpen);
let accordionElement: HTMLDivElement | undefined = $state();
const setIsOpen = (isOpen: boolean) => {
if (isOpen) {
@@ -23,7 +36,7 @@
if (autoScrollTo) {
setTimeout(() => {
accordionElement.scrollIntoView({
accordionElement?.scrollIntoView({
behavior: 'smooth',
block: 'start',
});
@@ -38,6 +51,15 @@
onDestroy(() => {
setIsOpen(false);
});
const onclick = () => {
isOpen = !isOpen;
setIsOpen(isOpen);
};
onMount(() => {
setIsOpen(isOpen);
});
</script>
<div
@@ -49,7 +71,7 @@
<button
type="button"
aria-expanded={isOpen}
on:click={() => (isOpen = !isOpen)}
{onclick}
class="flex w-full place-items-center justify-between text-left"
>
<div>
@@ -62,9 +84,9 @@
</h2>
</div>
<slot name="subtitle">
{#if subtitleSnippet}{@render subtitleSnippet()}{:else}
<p class="text-sm dark:text-immich-dark-fg mt-1">{subtitle}</p>
</slot>
{/if}
</div>
<div
@@ -88,7 +110,7 @@
{#if isOpen}
<ul transition:slide={{ duration: 150 }} class="mb-2 ml-4">
<slot />
{@render children?.()}
</ul>
{/if}
</div>

View File

@@ -3,10 +3,14 @@
import type { ResetOptions } from '$lib/utils/dipatch';
import { t } from 'svelte-i18n';
export let showResetToDefault = true;
export let disabled = false;
export let onReset: (options: ResetOptions) => void;
export let onSave: () => void;
interface Props {
showResetToDefault?: boolean;
disabled?: boolean;
onReset: (options: ResetOptions) => void;
onSave: () => void;
}
let { showResetToDefault = true, disabled = false, onReset, onSave }: Props = $props();
</script>
<div class="mt-8 flex justify-between gap-2">
@@ -14,7 +18,7 @@
{#if showResetToDefault}
<button
type="button"
on:click={() => onReset({ default: true })}
onclick={() => onReset({ default: true })}
class="bg-none text-sm font-medium text-immich-primary hover:text-immich-primary/75 dark:text-immich-dark-primary hover:dark:text-immich-dark-primary/75"
>
{$t('reset_to_default')}
@@ -23,7 +27,7 @@
</div>
<div class="right">
<Button {disabled} size="sm" color="gray" on:click={() => onReset({ default: false })}>{$t('reset')}</Button>
<Button type="submit" {disabled} size="sm" on:click={() => onSave()}>{$t('save')}</Button>
<Button {disabled} size="sm" color="gray" onclick={() => onReset({ default: false })}>{$t('reset')}</Button>
<Button type="submit" {disabled} size="sm" onclick={() => onSave()}>{$t('save')}</Button>
</div>
</div>

View File

@@ -4,13 +4,25 @@
import { fly } from 'svelte/transition';
import { t } from 'svelte-i18n';
export let value: string[];
export let options: { value: string; text: string }[];
export let label = '';
export let desc = '';
export let name = '';
export let isEdited = false;
export let disabled = false;
interface Props {
value: string[];
options: { value: string; text: string }[];
label?: string;
desc?: string;
name?: string;
isEdited?: boolean;
disabled?: boolean;
}
let {
value = $bindable(),
options,
label = '',
desc = '',
name = '',
isEdited = false,
disabled = false,
}: Props = $props();
function handleCheckboxChange(option: string) {
value = value.includes(option) ? value.filter((item) => item !== option) : [...value, option];
@@ -46,7 +58,7 @@
checked={value.includes(option.value)}
{disabled}
labelClass="text-gray-500 dark:text-gray-300"
on:change={() => handleCheckboxChange(option.value)}
onchange={() => handleCheckboxChange(option.value)}
/>
{/each}
</div>

View File

@@ -3,14 +3,29 @@
import { fly } from 'svelte/transition';
import Combobox, { type ComboBoxOption } from '$lib/components/shared-components/combobox.svelte';
import { t } from 'svelte-i18n';
import type { Snippet } from 'svelte';
export let title: string;
export let comboboxPlaceholder: string;
export let subtitle = '';
export let isEdited = false;
export let options: ComboBoxOption[];
export let selectedOption: ComboBoxOption;
export let onSelect: (combobox: ComboBoxOption | undefined) => void;
interface Props {
title: string;
comboboxPlaceholder: string;
subtitle?: string;
isEdited?: boolean;
options: ComboBoxOption[];
selectedOption: ComboBoxOption;
onSelect: (combobox: ComboBoxOption | undefined) => void;
children?: Snippet;
}
let {
title,
comboboxPlaceholder,
subtitle = '',
isEdited = false,
options,
selectedOption,
onSelect,
children,
}: Props = $props();
</script>
<div class="grid grid-cols-2">
@@ -33,6 +48,6 @@
</div>
<div class="flex items-center">
<Combobox label={title} hideLabel={true} {selectedOption} {options} placeholder={comboboxPlaceholder} {onSelect} />
<slot />
{@render children?.()}
</div>
</div>

View File

@@ -3,14 +3,27 @@
import { fly } from 'svelte/transition';
import Dropdown, { type RenderedOption } from '$lib/components/elements/dropdown.svelte';
import { t } from 'svelte-i18n';
import type { Snippet } from 'svelte';
export let title: string;
export let subtitle = '';
export let options: RenderedOption[];
export let selectedOption: RenderedOption;
export let isEdited = false;
interface Props {
title: string;
subtitle?: string;
options: RenderedOption[];
selectedOption: RenderedOption;
isEdited?: boolean;
onToggle: (option: RenderedOption) => void;
children?: Snippet;
}
export let onToggle: (option: RenderedOption) => void;
let {
title,
subtitle = '',
options,
selectedOption = $bindable(),
isEdited = false,
onToggle,
children,
}: Props = $props();
</script>
<div class="flex place-items-center justify-between">
@@ -30,7 +43,7 @@
</div>
<p class="text-sm dark:text-immich-dark-fg">{subtitle}</p>
<slot />
{@render children?.()}
</div>
<div class="w-fit">
<Dropdown

View File

@@ -1,7 +1,7 @@
import { SettingInputFieldType } from '$lib/constants';
import { render } from '@testing-library/svelte';
import userEvent from '@testing-library/user-event';
// @ts-expect-error the import works but tsc check errors
import SettingInputField, { SettingInputFieldType } from './setting-input-field.svelte';
import SettingInputField from './setting-input-field.svelte';
describe('SettingInputField component', () => {
it('validates number input on blur', async () => {

View File

@@ -1,36 +1,47 @@
<script lang="ts" context="module">
export enum SettingInputFieldType {
EMAIL = 'email',
TEXT = 'text',
NUMBER = 'number',
PASSWORD = 'password',
COLOR = 'color',
}
</script>
<script lang="ts">
import { quintOut } from 'svelte/easing';
import type { FormEventHandler } from 'svelte/elements';
import { fly } from 'svelte/transition';
import PasswordField from '../password-field.svelte';
import { t } from 'svelte-i18n';
import { onMount, tick } from 'svelte';
import { onMount, tick, type Snippet } from 'svelte';
import { SettingInputFieldType } from '$lib/constants';
export let inputType: SettingInputFieldType;
export let value: string | number;
export let min = Number.MIN_SAFE_INTEGER;
export let max = Number.MAX_SAFE_INTEGER;
export let step = '1';
export let label = '';
export let desc = '';
export let title = '';
export let required = false;
export let disabled = false;
export let isEdited = false;
export let autofocus = false;
export let passwordAutocomplete: AutoFill = 'current-password';
interface Props {
inputType: SettingInputFieldType;
value: string | number;
min?: number;
max?: number;
step?: string;
label?: string;
description?: string;
title?: string;
required?: boolean;
disabled?: boolean;
isEdited?: boolean;
autofocus?: boolean;
passwordAutocomplete?: AutoFill;
descriptionSnippet?: Snippet;
}
let input: HTMLInputElement;
let {
inputType,
value = $bindable(),
min = Number.MIN_SAFE_INTEGER,
max = Number.MAX_SAFE_INTEGER,
step = '1',
label = '',
description = '',
title = '',
required = false,
disabled = false,
isEdited = false,
autofocus = false,
passwordAutocomplete = 'current-password',
descriptionSnippet,
}: Props = $props();
let input: HTMLInputElement | undefined = $state();
const handleChange: FormEventHandler<HTMLInputElement> = (e) => {
value = e.currentTarget.value;
@@ -73,12 +84,12 @@
{/if}
</div>
{#if desc}
{#if description}
<p class="immich-form-label pb-2 text-sm" id="{label}-desc">
{desc}
{description}
</p>
{:else}
<slot name="desc" />
{@render descriptionSnippet?.()}
{/if}
{#if inputType !== SettingInputFieldType.PASSWORD}
@@ -87,7 +98,7 @@
<input
bind:this={input}
class="immich-form-input w-full pb-2 rounded-none mr-1"
aria-describedby={desc ? `${label}-desc` : undefined}
aria-describedby={description ? `${label}-desc` : undefined}
aria-labelledby="{label}-label"
id={label}
name={label}
@@ -97,7 +108,7 @@
{step}
{required}
{value}
on:change={handleChange}
onchange={handleChange}
{disabled}
{title}
/>
@@ -107,7 +118,7 @@
bind:this={input}
class="immich-form-input w-full pb-2"
class:color-picker={inputType === SettingInputFieldType.COLOR}
aria-describedby={desc ? `${label}-desc` : undefined}
aria-describedby={description ? `${label}-desc` : undefined}
aria-labelledby="{label}-label"
id={label}
name={label}
@@ -117,14 +128,14 @@
{step}
{required}
{value}
on:change={handleChange}
onchange={handleChange}
{disabled}
{title}
/>
</div>
{:else}
<PasswordField
aria-describedby={desc ? `${label}-desc` : undefined}
aria-describedby={description ? `${label}-desc` : undefined}
aria-labelledby="{label}-label"
id={label}
name={label}

View File

@@ -5,15 +5,29 @@
import Icon from '$lib/components/elements/icon.svelte';
import { mdiChevronDown } from '@mdi/js';
export let value: string | number;
export let options: { value: string | number; text: string }[];
export let label = '';
export let desc = '';
export let name = '';
export let isEdited = false;
export let number = false;
export let disabled = false;
export let onSelect: (setting: string | number) => void = () => {};
interface Props {
value: string | number;
options: { value: string | number; text: string }[];
label?: string;
desc?: string;
name?: string;
isEdited?: boolean;
number?: boolean;
disabled?: boolean;
onSelect?: (setting: string | number) => void;
}
let {
value = $bindable(),
options,
label = '',
desc = '',
name = '',
isEdited = false,
number = false,
disabled = false,
onSelect = () => {},
}: Props = $props();
const handleChange = (e: Event) => {
value = (e.target as HTMLInputElement).value;
@@ -62,7 +76,7 @@
{name}
id="{name}-select"
bind:value
on:change={handleChange}
onchange={handleChange}
>
{#each options as option}
<option value={option.value}>{option.text}</option>

View File

@@ -4,18 +4,32 @@
import Slider from '$lib/components/elements/slider.svelte';
import { generateId } from '$lib/utils/generate-id';
import { t } from 'svelte-i18n';
import type { Snippet } from 'svelte';
export let title: string;
export let subtitle = '';
export let checked = false;
export let disabled = false;
export let isEdited = false;
export let onToggle: (isChecked: boolean) => void = () => {};
interface Props {
title: string;
subtitle?: string;
checked?: boolean;
disabled?: boolean;
isEdited?: boolean;
onToggle?: (isChecked: boolean) => void;
children?: Snippet;
}
let {
title,
subtitle = '',
checked = $bindable(false),
disabled = false,
isEdited = false,
onToggle = () => {},
children,
}: Props = $props();
let id: string = generateId();
$: sliderId = `${id}-slider`;
$: subtitleId = subtitle ? `${id}-subtitle` : undefined;
let sliderId = $derived(`${id}-slider`);
let subtitleId = $derived(subtitle ? `${id}-subtitle` : undefined);
</script>
<div class="flex place-items-center justify-between">
@@ -37,7 +51,7 @@
{#if subtitle}
<p id={subtitleId} class="text-sm dark:text-immich-dark-fg">{subtitle}</p>
{/if}
<slot />
{@render children?.()}
</div>
<Slider id={sliderId} bind:checked {disabled} {onToggle} ariaDescribedBy={subtitleId} />

View File

@@ -2,13 +2,27 @@
import { quintOut } from 'svelte/easing';
import { fly } from 'svelte/transition';
import { t } from 'svelte-i18n';
import type { Snippet } from 'svelte';
export let value: string;
export let label = '';
export let desc = '';
export let required = false;
export let disabled = false;
export let isEdited = false;
interface Props {
value: string;
label?: string;
description?: string;
required?: boolean;
disabled?: boolean;
isEdited?: boolean;
descriptionSnippet?: Snippet;
}
let {
value = $bindable(),
label = '',
description = '',
required = false,
disabled = false,
isEdited = false,
descriptionSnippet,
}: Props = $props();
const handleInput = (e: Event) => {
value = (e.target as HTMLInputElement).value;
@@ -32,23 +46,23 @@
{/if}
</div>
{#if desc}
{#if description}
<p class="immich-form-label pb-2 text-sm" id="{label}-desc">
{desc}
{description}
</p>
{:else}
<slot name="desc" />
{@render descriptionSnippet?.()}
{/if}
<textarea
class="immich-form-input w-full pb-2"
aria-describedby={desc ? `${label}-desc` : undefined}
aria-describedby={description ? `${label}-desc` : undefined}
aria-labelledby="{label}-label"
id={label}
name={label}
{required}
{value}
on:input={handleInput}
oninput={handleInput}
{disabled}
></textarea>
</div>