migrate: switch to callbacks instead of events

This commit is contained in:
Xylight committed 2024-10-26 11:36:31 -07:00
1 parent c7e2963390
commit b291aeebf8
67 files changed
+332 -357

No files matched your search

+20 -34
View File
@@ -65,9 +65,6 @@
</script>
<script lang="ts">
import { createBubbler } from 'svelte/legacy';
const bubble = createBubbler();
import Spinner from '../loader/Spinner.svelte'
type ButtonRoundness = 'pill' | 'xl' | 'lg' | 'md' | 'none'
@@ -115,27 +112,23 @@
},
}
interface Props {
loading?: boolean;
submit?: boolean;
color?: ButtonColor;
size?: ButtonSize;
rounding?: ButtonRoundness;
roundingSide?: ButtonRoundingSide;
alignment?: ButtonAlignment;
shadow?: ButtonShadow;
column?: boolean;
animations?: any;
loaderWidth?: number | undefined;
href?: string | undefined;
class?: string;
prefix?: import('svelte').Snippet;
children?: import('svelte').Snippet;
suffix?: import('svelte').Snippet;
loading?: boolean
submit?: boolean
color?: ButtonColor
size?: ButtonSize
rounding?: ButtonRoundness
roundingSide?: ButtonRoundingSide
alignment?: ButtonAlignment
shadow?: ButtonShadow
column?: boolean
animations?: any
loaderWidth?: number | undefined
href?: string | undefined
class?: string
prefix?: import('svelte').Snippet
children?: import('svelte').Snippet
suffix?: import('svelte').Snippet
[key: string]: any
}
@@ -150,9 +143,9 @@
shadow = 'none',
column = false,
animations = {
scale: false,
large: false,
},
scale: false,
large: false,
},
loaderWidth = undefined,
href = undefined,
class: clazz = '',
@@ -160,8 +153,7 @@
children,
suffix,
...rest
}: Props = $props();
}: Props = $props()
</script>
<svelte:element
@@ -169,12 +161,6 @@
role={href ? 'link' : 'button'}
{href}
{...rest}
onclick={bubble('click')}
oncontextmenu={bubble('contextmenu')}
ondrag={bubble('drag')}
ondrop={bubble('drop')}
ondragstart={bubble('dragstart')}
ondragend={bubble('dragend')}
class="
{loading ? buttonColor.secondary : buttonColor[color]}
{buttonSize[size]}
+7 -9
View File
@@ -4,7 +4,6 @@
import { Check, Icon, Clipboard } from 'svelte-hero-icons'
import { scale } from 'svelte/transition'
let copied = $state(false)
let pre: HTMLPreElement = $state()
@@ -18,11 +17,11 @@
}
interface Props {
src: string;
filename?: string | undefined;
htmlSrc?: string | undefined;
children?: import('svelte').Snippet;
class?: string;
src: string
filename?: string | undefined
htmlSrc?: string | undefined
children?: import('svelte').Snippet
class?: string
[key: string]: any
}
@@ -33,8 +32,7 @@
children,
class: clazz = '',
...rest
}: Props = $props();
}: Props = $props()
</script>
<Material
@@ -51,7 +49,7 @@
size="square-sm"
color="tertiary"
aria-label="Copy"
on:click={() => {
onclick={() => {
copied = true
copy()
setTimeout(() => (copied = false), 2000)
+7 -10
View File
@@ -3,21 +3,18 @@
import { Check, Icon, Clipboard } from 'svelte-hero-icons'
import { scale } from 'svelte/transition'
let copied = $state(false)
interface Props {
/**
* Show a copy button.
*/
value: string;
copy?: boolean;
class?: string;
* Show a copy button.
*/
value: string
copy?: boolean
class?: string
}
let { value, copy = true, class: clazz = '' }: Props = $props();
let { value, copy = true, class: clazz = '' }: Props = $props()
</script>
<div
@@ -32,7 +29,7 @@
size="square-md"
color="tertiary"
aria-label="Copy"
on:click={() => {
onclick={() => {
copied = true
navigator.clipboard.writeText(value)
setTimeout(() => (copied = false), 2000)
+29 -29
View File
@@ -1,7 +1,7 @@
<script lang="ts">
import { run, createBubbler } from 'svelte/legacy';
import { run, createBubbler } from 'svelte/legacy'
const bubble = createBubbler();
const bubble = createBubbler()
import {
buttonColor,
buttonSize,
@@ -24,12 +24,11 @@
type T = $$Generic
let open = $state(false)
let element: HTMLSelectElement | undefined = $state()
let options: { value: any; label: string | null; disabled: boolean }[] = $state([])
let options: { value: any; label: string | null; disabled: boolean }[] =
$state([])
// Capture all options from the select element
run(() => {
@@ -40,7 +39,7 @@
disabled: option.disabled,
}))
: []
});
})
const dispatcher = createEventDispatcher<{
change: any
@@ -48,16 +47,16 @@
input: any
}>()
interface Props {
value?: T | undefined;
placeholder?: string | undefined;
label?: string | undefined;
size?: ButtonSize;
shadow?: ButtonShadow;
id?: string;
class?: string;
customLabel?: import('svelte').Snippet;
children?: import('svelte').Snippet;
customOption?: import('svelte').Snippet<[any]>;
value?: T | undefined
placeholder?: string | undefined
label?: string | undefined
size?: ButtonSize
shadow?: ButtonShadow
id?: string
class?: string
customLabel?: import('svelte').Snippet
children?: import('svelte').Snippet
customOption?: import('svelte').Snippet<[any]>
}
let {
@@ -70,9 +69,8 @@
class: clazz = '',
customLabel,
children,
customOption
}: Props = $props();
customOption,
}: Props = $props()
</script>
<div class="flex flex-col gap-1 {clazz}">
@@ -85,8 +83,7 @@
<div class="w-full relative">
<Menu bind:open>
{#snippet target()}
<select
<select
{id}
bind:this={element}
class="{buttonSize[size]} {buttonShadow[
@@ -96,12 +93,12 @@
{clazz}"
bind:value
onmousedown={(e) => {
e.preventDefault()
}}
e.preventDefault()
}}
onkeypress={(e) => {
e.preventDefault()
open = !open
}}
e.preventDefault()
open = !open
}}
onchange={bubble('change')}
oncontextmenu={bubble('contextmenu')}
{placeholder}
@@ -111,13 +108,16 @@
{/if}
{@render children?.()}
</select>
{/snippet}
{/snippet}
{#each options as option}
{#if customOption}{@render customOption({ option, selected: option.value == value, })}{:else}
{#if customOption}{@render customOption({
option,
selected: option.value == value,
})}{:else}
<MenuButton
on:contextmenu={() => dispatcher('contextmenu')}
on:click={() => {
onclick={() => {
value = option.value
dispatcher('change')
}}
+14 -14
View File
@@ -8,13 +8,13 @@
import Portal from '../popover/Portal.svelte'
interface Props {
action?: string | undefined;
open?: boolean;
title?: string | undefined;
dismissable?: boolean;
customTitle?: import('svelte').Snippet;
children?: import('svelte').Snippet;
actions?: import('svelte').Snippet<[any]>;
action?: string | undefined
open?: boolean
title?: string | undefined
dismissable?: boolean
customTitle?: import('svelte').Snippet
children?: import('svelte').Snippet
actions?: import('svelte').Snippet<[any]>
}
let {
@@ -24,8 +24,8 @@
dismissable = true,
customTitle,
children,
actions
}: Props = $props();
actions,
}: Props = $props()
let el: any = $state()
@@ -70,14 +70,14 @@ bg-white/50 dark:bg-black/50 box-border p-4"
class="absolute top-0 right-0 m-2 text-slate-600 dark:text-zinc-400"
color="tertiary"
size="square-sm"
on:click={() => {
onclick={() => {
open = false
dispatcher('dismissed')
}}
>
{#snippet prefix()}
<Icon src={XMark} size="20" mini />
{/snippet}
<Icon src={XMark} size="20" mini />
{/snippet}
</Button>
{/if}
<h1 class="font-semibold text-xl max-w-full">
@@ -90,10 +90,10 @@ bg-white/50 dark:bg-black/50 box-border p-4"
{@render children?.()}
{#if action}
<div class="mt-2 flex w-full">
{#if actions}{@render actions({ action, })}{:else}
{#if actions}{@render actions({ action })}{:else}
<Button
class="w-full"
on:click={(e) => {
onclick={(e) => {
dispatcher('action', e)
open = false
dispatcher('dismissed')
+3 -3
View File
@@ -1,5 +1,5 @@
<script lang="ts">
import { run } from 'svelte/legacy';
import { run } from 'svelte/legacy'
import Button from '../button/Button.svelte'
import Modal from '../modal/Modal.svelte'
@@ -11,7 +11,7 @@
// reactivity hack
run(() => {
open = $shownModal ? true : false
});
})
</script>
{#if $shownModal}
@@ -32,7 +32,7 @@
<Button
size="lg"
class="flex-1 w-full"
on:click={action.action}
onclick={action.action}
color={action.type}
>
{#if action.icon}
-1
View File
@@ -34,7 +34,6 @@
<Button
{...rest}
on:click
{color}
rounding="none"
class="w-full px-2 rounded-lg min-h-[36px] font-normal {disabled
+24 -22
View File
@@ -4,12 +4,9 @@
import { createEventDispatcher } from 'svelte'
import { Icon, MagnifyingGlass } from 'svelte-hero-icons'
type T = $$Generic
let items: T[] = $state([])
/**
* This is here so that the menu doesn't open as soon as it's mounted.
*/
@@ -22,14 +19,14 @@
}>()
interface Props {
query?: string;
selected?: T | undefined;
search: (query: string) => Promise<T[]>;
extractName: (item: T) => string;
select?: any;
input?: import('svelte').Snippet;
noresults?: import('svelte').Snippet;
children?: import('svelte').Snippet<[any]>;
query?: string
selected?: T | undefined
search: (query: string) => Promise<T[]>
extractName: (item: T) => string
select?: any
input?: import('svelte').Snippet
noresults?: import('svelte').Snippet
children?: import('svelte').Snippet<[any]>
[key: string]: any
}
@@ -39,15 +36,15 @@
search,
extractName,
select = (item: T) => {
selected = item
query = extractName(item)
dispatcher('select', item)
},
selected = item
query = extractName(item)
dispatcher('select', item)
},
input,
noresults,
children,
...rest
}: Props = $props();
}: Props = $props()
const debounceFunc = debounce(async () => {
searching = true
@@ -60,7 +57,7 @@
<div class="relative">
<Menu bind:open={openMenu}>
{#snippet target()}
{#if input}{@render input()}{:else}
{#if input}{@render input()}{:else}
<TextInput
bind:value={query}
on:input={(e) => {
@@ -79,13 +76,13 @@
inlineAffixes
>
{#snippet prefix()}
<div class="h-5 flex items-center">
<div class="h-5 flex items-center">
<Icon src={MagnifyingGlass} mini size="16" />
</div>
{/snippet}
{/snippet}
</TextInput>
{/if}
{/snippet}
{/snippet}
{#if searching}
<div class="w-full h-24 grid place-items-center">
<Spinner width={24} />
@@ -96,8 +93,13 @@
</div>
{:else}
{#each items as item}
{#if children}{@render children({ extractName, item, dispatcher, select, })}{:else}
<MenuButton on:click={() => select(item)}>
{#if children}{@render children({
extractName,
item,
dispatcher,
select,
})}{:else}
<MenuButton onclick={() => select(item)}>
{extractName(item)}
</MenuButton>
{/if}