impr: restructure src/lib
This commit is contained in:
376 files changed
+1000
-1033
No files matched your search
@@ -0,0 +1,61 @@
|
||||
<script lang="ts">
|
||||
const badgeColor = {
|
||||
'red-subtle':
|
||||
'bg-red-100 dark:bg-red-500/20 text-red-600 dark:text-red-400',
|
||||
'green-subtle': `bg-green-100 dark:bg-green-500/20 text-green-700 dark:text-green-400`,
|
||||
'yellow-subtle':
|
||||
'bg-yellow-100 dark:bg-yellow-500/20 text-yellow-700 dark:text-yellow-400',
|
||||
'gray-subtle':
|
||||
'bg-gray-100 dark:bg-gray-500/20 text-gray-700 dark:text-gray-300',
|
||||
'blue-subtle':
|
||||
'bg-blue-100 dark:bg-blue-500/20 text-blue-700 dark:text-blue-300',
|
||||
custom: '',
|
||||
}
|
||||
|
||||
const badgeRoundness = {
|
||||
full: 'rounded-full',
|
||||
md: 'rounded-md',
|
||||
custom: '',
|
||||
}
|
||||
|
||||
type BadgeColor = keyof typeof badgeColor
|
||||
type BadgeRoundness = keyof typeof badgeRoundness
|
||||
|
||||
interface Props {
|
||||
label?: string
|
||||
color?: BadgeColor
|
||||
rounding?: BadgeRoundness
|
||||
allowIconOnly?: boolean
|
||||
class?: string
|
||||
icon?: import('svelte').Snippet
|
||||
children?: import('svelte').Snippet
|
||||
}
|
||||
|
||||
let {
|
||||
label = '',
|
||||
color = 'gray-subtle',
|
||||
rounding = 'full',
|
||||
allowIconOnly = false,
|
||||
class: clazz = '',
|
||||
icon,
|
||||
children,
|
||||
...rest
|
||||
}: Props = $props()
|
||||
</script>
|
||||
|
||||
<span
|
||||
{...rest}
|
||||
class={[
|
||||
allowIconOnly && 'max-md:px-1.5 max-md:py-1.5',
|
||||
'text-xs font-medium flex items-center gap-1 ring-1 ring-black/20 dark:ring-white/20 ring-inset px-2 py-1',
|
||||
badgeRoundness[rounding],
|
||||
badgeColor[color],
|
||||
clazz,
|
||||
]}
|
||||
title={label}
|
||||
>
|
||||
{@render icon?.()}
|
||||
<span class={allowIconOnly ? 'sr-only md:contents' : 'contents'}>
|
||||
{@render children?.()}
|
||||
</span>
|
||||
</span>
|
||||
@@ -0,0 +1,327 @@
|
||||
<script module lang="ts">
|
||||
export type ButtonColor = keyof typeof buttonColor
|
||||
export type ButtonAlignment = keyof typeof buttonAlignment
|
||||
export type ButtonShadow = keyof typeof buttonShadow
|
||||
|
||||
export const buttonAlignment = {
|
||||
left: 'justify-start text-left origin-left',
|
||||
center: 'justify-center',
|
||||
right: 'justify-end text-right origin-right',
|
||||
}
|
||||
|
||||
export const buttonColor = {
|
||||
primary: 'btn-primary',
|
||||
secondary: 'btn-secondary',
|
||||
tertiary: 'btn-tertiary',
|
||||
danger: 'btn-danger',
|
||||
ghost: 'btn-ghost',
|
||||
'danger-subtle': 'text-red-500 hover:bg-red-500 hover:text-inherit!',
|
||||
'success-subtle': 'text-green-500 hover:bg-green-500 hover:text-inherit!',
|
||||
'warning-subtle': 'text-yellow-500 hover:bg-yellow-500 hover:text-inherit!',
|
||||
'blue-subtle': `text-blue-500 hover:bg-blue-500 hover:text-inherit!`,
|
||||
|
||||
none: '',
|
||||
}
|
||||
|
||||
export const buttonShadow = {
|
||||
sm: 'shadow-xs',
|
||||
none: 'shadow-none',
|
||||
}
|
||||
|
||||
export type ButtonSize = keyof typeof buttonSize
|
||||
|
||||
export const buttonSize = {
|
||||
xs: 'btn-xs',
|
||||
sm: 'btn-sm',
|
||||
md: 'btn-md',
|
||||
lg: 'btn-lg',
|
||||
xl: 'btn-xl',
|
||||
'square-sm': 'btn-square-sm',
|
||||
'square-md': 'btn-square-md',
|
||||
'square-lg': 'btn-square-lg',
|
||||
'square-xl': 'btn-square-xl',
|
||||
custom: '',
|
||||
}
|
||||
|
||||
const buttonRounding = {
|
||||
pill: 'rounded-full',
|
||||
xl: 'rounded-xl',
|
||||
lg: 'rounded-lg',
|
||||
md: 'rounded-md',
|
||||
none: '',
|
||||
}
|
||||
type ButtonRoundness = keyof typeof buttonRounding
|
||||
|
||||
const buttonWeight = {
|
||||
md: 'font-medium',
|
||||
none: '',
|
||||
}
|
||||
type ButtonWeight = keyof typeof buttonWeight
|
||||
|
||||
interface Props
|
||||
extends Omit<HTMLButtonAttributes | HTMLAnchorAttributes, 'prefix'> {
|
||||
loading?: boolean
|
||||
submit?: boolean
|
||||
type?: 'button' | 'none'
|
||||
color?: ButtonColor
|
||||
size?: ButtonSize
|
||||
rounding?: ButtonRoundness
|
||||
alignment?: ButtonAlignment
|
||||
shadow?: ButtonShadow
|
||||
loaderWidth?: number | undefined
|
||||
href?: string | undefined
|
||||
class?: ClassValue
|
||||
prefix?: Snippet
|
||||
children?: Snippet
|
||||
suffix?: Snippet
|
||||
onclick?: HTMLButtonAttributes['onclick']
|
||||
icon?: IconSource
|
||||
weight?: ButtonWeight
|
||||
}
|
||||
|
||||
export type { Props as ButtonProps }
|
||||
</script>
|
||||
|
||||
<script lang="ts">
|
||||
import type {
|
||||
ClassValue,
|
||||
HTMLAnchorAttributes,
|
||||
HTMLButtonAttributes,
|
||||
} from 'svelte/elements'
|
||||
|
||||
import Spinner from '../loader/Spinner.svelte'
|
||||
import type { Snippet } from 'svelte'
|
||||
import { Icon, type IconSource } from 'svelte-hero-icons'
|
||||
|
||||
let {
|
||||
loading = false,
|
||||
submit = false,
|
||||
type = 'button',
|
||||
color = 'secondary',
|
||||
size = 'md',
|
||||
rounding = 'xl',
|
||||
alignment = 'center',
|
||||
shadow = color != 'tertiary' ? 'none' : 'none',
|
||||
disabled,
|
||||
loaderWidth = undefined,
|
||||
href = undefined,
|
||||
class: clazz = '',
|
||||
prefix,
|
||||
children,
|
||||
suffix,
|
||||
icon,
|
||||
weight = 'md',
|
||||
...rest
|
||||
}: Props = $props()
|
||||
</script>
|
||||
|
||||
<svelte:element
|
||||
this={href ? 'a' : 'button'}
|
||||
role={href ? 'link' : 'button'}
|
||||
{href}
|
||||
{...rest}
|
||||
tabindex={disabled ? -1 : undefined}
|
||||
class={[
|
||||
type == 'button' && 'btn',
|
||||
buttonSize[size],
|
||||
buttonRounding[rounding],
|
||||
buttonShadow[shadow],
|
||||
buttonColor[color],
|
||||
buttonAlignment[alignment],
|
||||
buttonWeight[weight],
|
||||
(disabled || loading) && 'btn-disabled',
|
||||
alignment == 'center'
|
||||
? 'origin-center'
|
||||
: alignment == 'left'
|
||||
? 'origin-left'
|
||||
: 'origin-right',
|
||||
clazz,
|
||||
]}
|
||||
type={submit ? 'submit' : 'button'}
|
||||
>
|
||||
{#if loading}
|
||||
<Spinner width={loaderWidth ?? 16} />
|
||||
{:else if prefix}
|
||||
{@render prefix?.()}
|
||||
{:else if icon}
|
||||
<Icon
|
||||
src={icon}
|
||||
size="16"
|
||||
mini
|
||||
class={[color == 'secondary' && 'text-slate-600 dark:text-zinc-400']}
|
||||
/>
|
||||
{/if}
|
||||
{@render children?.()}
|
||||
{@render suffix?.()}
|
||||
</svelte:element>
|
||||
|
||||
<!--
|
||||
@component
|
||||
|
||||
@slot `prefix` -- Will be replaced if `loading` is `true`.
|
||||
@slot `suffix`
|
||||
-->
|
||||
|
||||
<style>
|
||||
@reference "../../../app.css";
|
||||
|
||||
:global {
|
||||
.btn {
|
||||
display: flex;
|
||||
flex-direction: row;
|
||||
align-items: center;
|
||||
font-size: var(--text-sm);
|
||||
gap: calc(var(--spacing) * 1.5);
|
||||
|
||||
transition: 75ms linear;
|
||||
|
||||
@variant hover {
|
||||
cursor: pointer;
|
||||
}
|
||||
}
|
||||
|
||||
.btn-primary {
|
||||
border: 1px solid transparent;
|
||||
background-color: var(--color-primary-900);
|
||||
color: var(--color-slate-50);
|
||||
|
||||
@variant dark {
|
||||
background-color: var(--color-primary-100) !important;
|
||||
color: var(--color-zinc-900);
|
||||
}
|
||||
|
||||
@variant hover {
|
||||
filter: brightness(120%);
|
||||
@variant dark {
|
||||
filter: brightness(90%);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.btn-secondary {
|
||||
border: 1px solid var(--color-slate-200);
|
||||
border-bottom-color: var(--color-slate-300);
|
||||
background-color: var(--color-white);
|
||||
|
||||
@variant dark {
|
||||
border: 1px solid var(--color-zinc-800);
|
||||
background-color: var(--color-zinc-900);
|
||||
}
|
||||
|
||||
@variant hover {
|
||||
background-color: var(--color-slate-50);
|
||||
@variant dark {
|
||||
background-color: var(--color-zinc-950);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.btn-tertiary {
|
||||
background-color: transparent;
|
||||
@variant hover {
|
||||
background-color: --alpha(var(--color-slate-200) / 50%);
|
||||
@variant dark {
|
||||
background-color: --alpha(var(--color-zinc-700) / 30%);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.btn-danger {
|
||||
/* border border-red-500 bg-red-500 hover:text-red-500 hover:bg-transparent text-white */
|
||||
|
||||
background-color: var(--color-red-600);
|
||||
color: var(--color-white);
|
||||
|
||||
@variant dark {
|
||||
background-color: var(--color-red-400);
|
||||
color: var(--color-black);
|
||||
}
|
||||
|
||||
@variant hover {
|
||||
filter: brightness(120%);
|
||||
@variant dark {
|
||||
filter: brightness(90%);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.btn-ghost {
|
||||
/* border border-slate-200 dark:border-zinc-800 bg-transparent
|
||||
hover:bg-slate-100 dark:hover:bg-zinc-800 dark:hover:border-zinc-700 dark:text-zinc-400 hover:text-inherit
|
||||
dark:hover:text-inherit */
|
||||
|
||||
border: 1px solid var(--color-slate-200);
|
||||
background-color: transparent;
|
||||
|
||||
@variant hover {
|
||||
background-color: var(--color-slate-100);
|
||||
}
|
||||
|
||||
@variant dark {
|
||||
border-color: var(--color-zinc-800);
|
||||
@variant hover {
|
||||
background-color: var(--color-zinc-800);
|
||||
border-color: var(--color-zinc-700);
|
||||
color: var(--color-zinc-200);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.btn-xs {
|
||||
padding: calc(var(--spacing) * 1) calc(var(--spacing) * 2);
|
||||
font-size: var(--text-xs);
|
||||
}
|
||||
|
||||
.btn-sm {
|
||||
padding: calc(var(--spacing) * 1.5) calc(var(--spacing) * 3);
|
||||
font-size: 12px;
|
||||
}
|
||||
|
||||
.btn-md {
|
||||
padding-block: calc(var(--spacing) * 1.5);
|
||||
padding-inline: calc(var(--spacing) * 3);
|
||||
font-size: var(--text-sm);
|
||||
}
|
||||
|
||||
.btn-lg {
|
||||
padding-block: calc(var(--spacing) * 2);
|
||||
padding-inline: calc(var(--spacing) * 3);
|
||||
font-size: var(--text-sm);
|
||||
}
|
||||
|
||||
.btn-xl {
|
||||
padding: calc(var(--spacing) * 3) calc(var(--spacing) * 6);
|
||||
font-size: var(--text-base);
|
||||
}
|
||||
|
||||
.btn-square-sm {
|
||||
width: calc(var(--spacing) * 6);
|
||||
height: calc(var(--spacing) * 6);
|
||||
}
|
||||
|
||||
.btn-square-md {
|
||||
width: calc(var(--spacing) * 8);
|
||||
height: calc(var(--spacing) * 8);
|
||||
}
|
||||
|
||||
.btn-square-lg {
|
||||
width: calc(var(--spacing) * 10);
|
||||
height: calc(var(--spacing) * 10);
|
||||
}
|
||||
|
||||
.btn-square-xl {
|
||||
width: calc(var(--spacing) * 12);
|
||||
height: calc(var(--spacing) * 12);
|
||||
}
|
||||
|
||||
.btn-disabled {
|
||||
pointer-events: none;
|
||||
filter: brightness(90%);
|
||||
cursor: normal;
|
||||
|
||||
@variant dark {
|
||||
filter: brightness(70%);
|
||||
}
|
||||
}
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,52 @@
|
||||
<script lang="ts">
|
||||
import type { Snippet } from 'svelte'
|
||||
import type { ClassValue } from 'svelte/elements'
|
||||
|
||||
interface Props {
|
||||
open?: boolean
|
||||
class?: ClassValue
|
||||
summary?: Snippet<[{ open: boolean }]>
|
||||
extended?: Snippet
|
||||
children?: Snippet<[{ open: boolean }]>
|
||||
}
|
||||
|
||||
let {
|
||||
open = $bindable(false),
|
||||
class: clazz = '',
|
||||
summary,
|
||||
extended,
|
||||
children,
|
||||
...rest
|
||||
}: Props = $props()
|
||||
|
||||
const id = $props.id()
|
||||
</script>
|
||||
|
||||
<div {...rest} class={['w-full', clazz]}>
|
||||
<label for={id} class="w-full">
|
||||
{@render summary?.({ open })}
|
||||
</label>
|
||||
{@render extended?.()}
|
||||
<input {id} class="hidden input" type="checkbox" bind:checked={open} />
|
||||
<div class="expand">
|
||||
{@render children?.({ open })}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<style>
|
||||
.expand {
|
||||
display: grid;
|
||||
grid-template-rows: 0fr;
|
||||
grid-template-columns: 100%;
|
||||
overflow: hidden;
|
||||
transition: grid-template-rows 0.5s cubic-bezier(0.19, 1, 0.22, 1);
|
||||
}
|
||||
|
||||
.input:checked + .expand {
|
||||
grid-template-rows: 1fr;
|
||||
}
|
||||
|
||||
.expand > :global(*) {
|
||||
min-height: 0;
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,61 @@
|
||||
<script lang="ts">
|
||||
import { Disclosure } from 'mono-svelte'
|
||||
import { Minus, Plus, Icon } from 'svelte-hero-icons'
|
||||
import type { ClassValue } from 'svelte/elements'
|
||||
interface Props {
|
||||
open?: boolean
|
||||
icon?: boolean
|
||||
class?: ClassValue
|
||||
title?: import('svelte').Snippet<[boolean | undefined]>
|
||||
extended?: import('svelte').Snippet
|
||||
content?: import('svelte').Snippet
|
||||
children?: import('svelte').Snippet
|
||||
}
|
||||
|
||||
let {
|
||||
open = $bindable(false),
|
||||
icon = true,
|
||||
class: clazz = '',
|
||||
title,
|
||||
extended,
|
||||
content,
|
||||
children,
|
||||
}: Props = $props()
|
||||
</script>
|
||||
|
||||
<Disclosure {extended} bind:open class={['gap-2', clazz]}>
|
||||
{#snippet summary()}
|
||||
<div
|
||||
class={[
|
||||
'font-medium w-full text-left flex flex-row items-center justify-between hover:text-primary-900',
|
||||
'dark:hover:text-primary-100 transition-colors z-0 group relative cursor-pointer',
|
||||
]}
|
||||
>
|
||||
<div class="flex flex-row gap-1 items-center w-full">
|
||||
{@render title?.(open)}
|
||||
</div>
|
||||
{#if icon}
|
||||
<div
|
||||
class={[
|
||||
'ml-auto',
|
||||
!open && 'rotate-90',
|
||||
'transition-transform duration-300 ease-out',
|
||||
]}
|
||||
>
|
||||
<Icon src={open ? Minus : Plus} size="15" micro class={[]} />
|
||||
</div>
|
||||
{/if}
|
||||
<div
|
||||
class="inset-0 -z-10 opacity-0 absolute bg-slate-200/50 dark:bg-zinc-900/50 rounded-full
|
||||
group-hover:opacity-100 group-hover:-inset-1 group-hover:-inset-x-2 transition-all"
|
||||
></div>
|
||||
</div>
|
||||
{/snippet}
|
||||
{#if content}
|
||||
{@render content()}
|
||||
{:else}
|
||||
<div class="text-slate-900 dark:text-zinc-100 *:mt-2">
|
||||
{@render children?.()}
|
||||
</div>
|
||||
{/if}
|
||||
</Disclosure>
|
||||
@@ -0,0 +1,80 @@
|
||||
<script lang="ts">
|
||||
import Button from '../button/Button.svelte'
|
||||
import { generateID } from '../forms/helper.js'
|
||||
import { Label } from '../index.js'
|
||||
|
||||
interface Props {
|
||||
accept?: string
|
||||
id?: string
|
||||
files?: FileList | null
|
||||
multiple?: boolean
|
||||
preview?: boolean
|
||||
label?: string | undefined
|
||||
class?: string
|
||||
customLabel?: import('svelte').Snippet
|
||||
button?: import('svelte').Snippet
|
||||
choose?: import('svelte').Snippet
|
||||
}
|
||||
|
||||
let {
|
||||
accept = '*',
|
||||
id = generateID(),
|
||||
files = $bindable(),
|
||||
multiple = false,
|
||||
preview = true,
|
||||
label = undefined,
|
||||
class: clazz = '',
|
||||
customLabel,
|
||||
button,
|
||||
choose,
|
||||
}: Props = $props()
|
||||
|
||||
let previewURLs = $derived(
|
||||
preview && files ? Array.from(files).map(URL.createObjectURL) : undefined,
|
||||
)
|
||||
</script>
|
||||
|
||||
<div class="flex flex-col gap-1 {clazz}">
|
||||
{#if customLabel || label}
|
||||
<Label for={id} text={label}>
|
||||
{@render customLabel?.()}
|
||||
</Label>
|
||||
{/if}
|
||||
<label
|
||||
class="w-max relative cursor-pointer space-x-2 flex flex-row items-center"
|
||||
>
|
||||
{#if button}{@render button()}{:else}
|
||||
<Button>Browse</Button>
|
||||
{/if}
|
||||
{#if previewURLs}
|
||||
<div
|
||||
class="flex flex-row items-center -space-x-1 hover:space-x-1 z-20 h-8"
|
||||
>
|
||||
{#each previewURLs as file (file)}
|
||||
<img
|
||||
src={file}
|
||||
onload={() => URL.revokeObjectURL(file)}
|
||||
alt=""
|
||||
class="w-8 h-8 object-cover rounded-full hover:w-16
|
||||
hover:h-16 transition-all border border-slate-200 ring-1
|
||||
ring-slate-50 dark:ring-zinc-950 bg-white dark:bg-zinc-950"
|
||||
/>
|
||||
{/each}
|
||||
</div>
|
||||
{/if}
|
||||
<span class="flex flex-row items-center text-slate-600 dark:text-zinc-400">
|
||||
{#if choose}{@render choose()}{:else if (files ?? []).length == 0}
|
||||
No file selected.
|
||||
{:else}
|
||||
{(files ?? []).length} files selected
|
||||
{/if}
|
||||
</span>
|
||||
<input
|
||||
type="file"
|
||||
bind:files
|
||||
{accept}
|
||||
{multiple}
|
||||
class="w-full h-full inset-0 opacity-0 absolute cursor-pointer"
|
||||
/>
|
||||
</label>
|
||||
</div>
|
||||
@@ -0,0 +1,37 @@
|
||||
<script lang="ts">
|
||||
import type { Snippet } from 'svelte'
|
||||
import type { ClassValue, HTMLAttributes } from 'svelte/elements'
|
||||
|
||||
interface Props extends HTMLAttributes<HTMLSpanElement> {
|
||||
for?: string | undefined
|
||||
/**
|
||||
* The `text` prop will take precedence over the slot.
|
||||
*/
|
||||
text?: string | undefined
|
||||
class?: ClassValue
|
||||
customText?: Snippet
|
||||
}
|
||||
|
||||
let {
|
||||
for: forID = undefined,
|
||||
text = undefined,
|
||||
class: clazz = '',
|
||||
customText,
|
||||
children,
|
||||
...rest
|
||||
}: Props = $props()
|
||||
</script>
|
||||
|
||||
<svelte:element
|
||||
this={text || customText ? 'label' : 'h3'}
|
||||
{...rest}
|
||||
for={forID}
|
||||
class={['text-sm text-slate-800 dark:text-zinc-200 font-medium', clazz]}
|
||||
>
|
||||
{#if text}
|
||||
<div class="inline-block">{text}</div>
|
||||
{:else if customText}
|
||||
{@render customText?.()}
|
||||
{/if}
|
||||
{@render children?.()}
|
||||
</svelte:element>
|
||||
@@ -0,0 +1,54 @@
|
||||
<script lang="ts">
|
||||
interface Props {
|
||||
checked?: boolean
|
||||
labelClass?: string
|
||||
class?: string
|
||||
children?: import('svelte').Snippet
|
||||
description?: import('svelte').Snippet
|
||||
}
|
||||
|
||||
let {
|
||||
checked = $bindable(),
|
||||
labelClass = '',
|
||||
class: clazz = '',
|
||||
children,
|
||||
description,
|
||||
}: Props = $props()
|
||||
</script>
|
||||
|
||||
<label
|
||||
class="font-normal cursor-pointer flex flex-row items-center gap-4 group {clazz}"
|
||||
>
|
||||
<div
|
||||
class="w-11 h-6 rounded-full relative z-[inherit] cursor-pointer flex flex-row transition-colors shadow-xs
|
||||
{checked
|
||||
? 'bg-primary-900 dark:bg-primary-100'
|
||||
: 'bg-slate-200 dark:bg-zinc-800'}
|
||||
p-0.5 shrink-0"
|
||||
>
|
||||
<input
|
||||
bind:checked
|
||||
type="checkbox"
|
||||
class="peer appearance-none absolute top-0 left-0 w-full h-full cursor-pointer z-10"
|
||||
/>
|
||||
<div
|
||||
class="box-border w-5 h-full bg-white dark:peer-checked:bg-black rounded-full shadow-xs
|
||||
group-active:w-6 transition peer-checked:translate-x-5 peer-checked:group-active:translate-x-4
|
||||
peer-checked:rtl:-translate-x-5 peer-checked:group-active:rtl:-translate-x-4"
|
||||
></div>
|
||||
</div>
|
||||
<div class="flex flex-col {labelClass}">
|
||||
{@render children?.()}
|
||||
{#if description}
|
||||
<span class="font-normal text-sm text-slate-700 dark:text-zinc-300">
|
||||
{@render description?.()}
|
||||
</span>
|
||||
{/if}
|
||||
</div>
|
||||
</label>
|
||||
|
||||
<style>
|
||||
.transition {
|
||||
transition: all 500ms cubic-bezier(0.075, 0.82, 0.165, 1);
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,104 @@
|
||||
<script module lang="ts">
|
||||
export type Size = keyof typeof sizeClass
|
||||
|
||||
export const sizeClass = {
|
||||
sm: 'p-3',
|
||||
md: 'p-4',
|
||||
lg: 'p-5',
|
||||
}
|
||||
|
||||
interface Props extends HTMLTextareaAttributes {
|
||||
label?: string | undefined
|
||||
value?: string
|
||||
placeholder?: string
|
||||
disabled?: boolean
|
||||
required?: boolean
|
||||
size?: Size
|
||||
id?: string
|
||||
rows?: number
|
||||
element?: HTMLTextAreaElement | undefined
|
||||
class?: ClassValue
|
||||
customLabel?: import('svelte').Snippet
|
||||
suffix?: import('svelte').Snippet
|
||||
children?: import('svelte').Snippet
|
||||
}
|
||||
|
||||
export type { Props as TextAreaProps }
|
||||
</script>
|
||||
|
||||
<script lang="ts">
|
||||
import type { ClassValue, HTMLTextareaAttributes } from 'svelte/elements'
|
||||
|
||||
import Label from '../forms/Label.svelte'
|
||||
import { generateID } from '../forms/helper.js'
|
||||
|
||||
const borderClass = `
|
||||
border border-slate-200 dark:border-zinc-800
|
||||
`
|
||||
|
||||
let {
|
||||
label = undefined,
|
||||
value = $bindable(),
|
||||
placeholder = '',
|
||||
disabled = false,
|
||||
required = false,
|
||||
size = 'md',
|
||||
id = generateID(),
|
||||
rows = 4,
|
||||
element = $bindable(),
|
||||
class: clazz = '',
|
||||
customLabel,
|
||||
suffix,
|
||||
children,
|
||||
...rest
|
||||
}: Props = $props()
|
||||
</script>
|
||||
|
||||
<div class="flex flex-col gap-1 {clazz}">
|
||||
{#if customLabel || label}
|
||||
<Label
|
||||
for={id}
|
||||
text={label}
|
||||
class="peer-invalid:text-red-500 {required
|
||||
? "after:content-['*'] after:text-red-500 after:ml-1"
|
||||
: ''}"
|
||||
>
|
||||
{@render customLabel?.()}
|
||||
</Label>
|
||||
{/if}
|
||||
<div
|
||||
class="rounded-xl flex flex-col items-center text-sm bg-white dark:bg-zinc-950 {clazz}"
|
||||
>
|
||||
<textarea
|
||||
{id}
|
||||
{placeholder}
|
||||
{disabled}
|
||||
{rows}
|
||||
bind:value
|
||||
bind:this={element}
|
||||
{...rest}
|
||||
class={[
|
||||
sizeClass[size],
|
||||
borderClass,
|
||||
`focus:border-slate-800 dark:focus:border-zinc-200 bg-white dark:bg-zinc-950
|
||||
focus:outline-hidden focus:ring-2 ring-slate-800/50 rounded-xl dark:ring-zinc-200/50
|
||||
transition-all text-sm w-full disabled:bg-slate-100
|
||||
disabled:cursor-not-allowed dark:disabled:bg-zinc-900 invalid:border-red-500!
|
||||
peer invalid:text-red-500 z-10`,
|
||||
suffix && 'rounded-b-none border-b-0',
|
||||
clazz,
|
||||
]}
|
||||
></textarea>
|
||||
{#if suffix}
|
||||
<div
|
||||
class="{borderClass} {sizeClass[
|
||||
size
|
||||
]} w-full border-t-0 rounded-xl rounded-t-none
|
||||
flex items-center"
|
||||
>
|
||||
{@render suffix?.()}
|
||||
</div>
|
||||
{/if}
|
||||
</div>
|
||||
{@render children?.()}
|
||||
</div>
|
||||
@@ -0,0 +1,131 @@
|
||||
<script module lang="ts">
|
||||
export type Size = keyof typeof sizeClass
|
||||
export type Shadow = keyof typeof shadowClass
|
||||
|
||||
export const sizeClass = {
|
||||
sm: 'px-3 py-1',
|
||||
md: 'px-3.5 py-1.5',
|
||||
lg: 'px-5 py-3',
|
||||
}
|
||||
|
||||
export const shadowClass = {
|
||||
sm: 'shadow-xs',
|
||||
none: 'shadow-none',
|
||||
}
|
||||
|
||||
interface Props extends Omit<HTMLInputAttributes, 'size' | 'prefix'> {
|
||||
label?: string | undefined
|
||||
placeholder?: string
|
||||
disabled?: boolean
|
||||
required?: boolean
|
||||
size?: Size
|
||||
id?: string
|
||||
inlineAffixes?: boolean
|
||||
shadow?: Shadow
|
||||
element?: HTMLInputElement | undefined
|
||||
class?: string
|
||||
customLabel?: import('svelte').Snippet
|
||||
prefix?: import('svelte').Snippet
|
||||
suffix?: import('svelte').Snippet
|
||||
children?: import('svelte').Snippet
|
||||
}
|
||||
|
||||
export type { Props as TextInputProps }
|
||||
</script>
|
||||
|
||||
<script lang="ts">
|
||||
import type { HTMLInputAttributes } from 'svelte/elements'
|
||||
|
||||
import Label from '../forms/Label.svelte'
|
||||
import { generateID } from '../forms/helper.js'
|
||||
|
||||
const borderClass = `border border-slate-200 border-b-slate-300 dark:border-zinc-800`
|
||||
|
||||
let {
|
||||
label = undefined,
|
||||
value = $bindable(),
|
||||
placeholder = '',
|
||||
disabled = false,
|
||||
required = false,
|
||||
size = 'md',
|
||||
id = generateID(),
|
||||
inlineAffixes = false,
|
||||
shadow = 'sm',
|
||||
element = $bindable(),
|
||||
class: clazz = '',
|
||||
customLabel: passedCustomLabel,
|
||||
prefix,
|
||||
suffix,
|
||||
children,
|
||||
...rest
|
||||
}: Props = $props()
|
||||
</script>
|
||||
|
||||
<div class="flex flex-col gap-1 {clazz}">
|
||||
{#if passedCustomLabel || label}
|
||||
<Label
|
||||
for={id}
|
||||
text={label}
|
||||
class="peer-invalid:text-red-500 {required
|
||||
? "after:content-['*'] after:text-red-500 after:ml-1"
|
||||
: ''}"
|
||||
>
|
||||
{@render passedCustomLabel?.()}
|
||||
</Label>
|
||||
{/if}
|
||||
<div
|
||||
class={[
|
||||
shadowClass[shadow],
|
||||
borderClass,
|
||||
`border focus-within:border-primary-900 dark:focus-within:border-primary-100 focus-within:ring-2
|
||||
ring-slate-300 dark:ring-zinc-700
|
||||
transition-colors
|
||||
rounded-xl flex flex-row items-center text-sm`,
|
||||
clazz,
|
||||
]}
|
||||
>
|
||||
{#if prefix}
|
||||
<div
|
||||
class={[
|
||||
'rounded-xl rounded-r-none text-slate-600 dark:text-zinc-400',
|
||||
inlineAffixes && 'bg-white dark:bg-zinc-900 pr-0 w-8',
|
||||
sizeClass[size],
|
||||
]}
|
||||
>
|
||||
{@render prefix?.()}
|
||||
</div>
|
||||
{/if}
|
||||
<input
|
||||
type="text"
|
||||
{id}
|
||||
{placeholder}
|
||||
{disabled}
|
||||
bind:value
|
||||
bind:this={element}
|
||||
{...rest}
|
||||
class={[
|
||||
sizeClass[size],
|
||||
`bg-white dark:bg-zinc-900
|
||||
focus:outline-hidden rounded-xl text-sm w-full disabled:bg-slate-100
|
||||
disabled:cursor-not-allowed dark:disabled:bg-zinc-800 invalid:border-red-500!
|
||||
peer invalid:text-red-500 z-10`,
|
||||
prefix && 'rounded-l-none',
|
||||
prefix && inlineAffixes && 'border-l-0',
|
||||
suffix && 'rounded-r-none',
|
||||
suffix && inlineAffixes && 'border-r-0',
|
||||
clazz,
|
||||
]}
|
||||
/>
|
||||
{#if suffix}
|
||||
<div
|
||||
class={[
|
||||
'rounded-xl rounded-l-none text-slate-600 dark:text-zinc-400',
|
||||
inlineAffixes && 'bg-white dark:bg-zinc-900 pl-0',
|
||||
]}
|
||||
>
|
||||
{@render suffix?.()}
|
||||
</div>
|
||||
{/if}
|
||||
</div>
|
||||
{@render children?.()}
|
||||
</div>
|
||||
@@ -0,0 +1 @@
|
||||
export const generateID = () => Math.floor(Math.random() * 1000000).toString()
|
||||
@@ -0,0 +1,58 @@
|
||||
<script lang="ts">
|
||||
import { locale } from '$lib/app/i18n'
|
||||
import { getContext, onDestroy, type Snippet } from 'svelte'
|
||||
import type { IconSource } from 'svelte-hero-icons'
|
||||
import type { HTMLOptionAttributes } from 'svelte/elements'
|
||||
|
||||
interface Props extends Omit<HTMLOptionAttributes, 'prefix'> {
|
||||
children: Snippet
|
||||
icon?: IconSource
|
||||
}
|
||||
let { children, icon, ...rest }: Props = $props()
|
||||
|
||||
let optionElement = $state<HTMLOptionElement>()
|
||||
let option = $derived({
|
||||
value: optionElement?.value ?? '',
|
||||
label: optionElement?.innerText ?? '',
|
||||
icon: icon,
|
||||
disabled: optionElement?.disabled,
|
||||
isLabel: optionElement?.getAttribute('data-label') == 'true',
|
||||
})
|
||||
|
||||
interface SelectContext {
|
||||
options: {
|
||||
value: string
|
||||
label: string
|
||||
icon?: IconSource
|
||||
disabled?: boolean
|
||||
isLabel?: boolean
|
||||
}[]
|
||||
}
|
||||
|
||||
const context = getContext<SelectContext>('select')
|
||||
|
||||
$effect(() => {
|
||||
if ($locale || !context.options.find((v) => v == option)) {
|
||||
const index = context.options.findIndex((i) => i.value == option.value)
|
||||
|
||||
if (index != -1) {
|
||||
context.options.splice(index, 1, option)
|
||||
} else {
|
||||
context.options.push(option)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
onDestroy(() => {
|
||||
const index = context.options.findIndex((i) => i.value == option.value)
|
||||
if (index != -1) {
|
||||
context.options.splice(index, 1)
|
||||
}
|
||||
})
|
||||
</script>
|
||||
|
||||
{#key $locale}
|
||||
<option {...rest} bind:this={optionElement}>
|
||||
{@render children()}
|
||||
</option>
|
||||
{/key}
|
||||
@@ -0,0 +1,182 @@
|
||||
<script lang="ts" module>
|
||||
interface Props<T> extends Omit<HTMLSelectAttributes, 'size'> {
|
||||
value?: T | string | undefined
|
||||
placeholder?: string | undefined
|
||||
label?: string | undefined
|
||||
size?: 'md' | 'sm'
|
||||
id?: string
|
||||
class?: ClassValue
|
||||
baseClass?: ClassValue
|
||||
selectClass?: ClassValue
|
||||
customLabel?: import('svelte').Snippet
|
||||
children?: import('svelte').Snippet
|
||||
customOption?: import('svelte').Snippet<
|
||||
[
|
||||
{
|
||||
option: {
|
||||
value: string
|
||||
label: string
|
||||
icon?: IconSource
|
||||
disabled?: boolean
|
||||
}
|
||||
selected: boolean
|
||||
},
|
||||
]
|
||||
>
|
||||
target?: Snippet<[Attachment]>
|
||||
oncontextmenu?: HTMLSelectAttributes['oncontextmenu']
|
||||
onchange?: HTMLSelectAttributes['onchange']
|
||||
placement?: Placement
|
||||
}
|
||||
|
||||
export type { Props as SelectProps }
|
||||
</script>
|
||||
|
||||
<script lang="ts" generics="T">
|
||||
import { setContext, tick, type Snippet } from 'svelte'
|
||||
import type { Placement } from 'svelte-floating-ui/dom'
|
||||
import {
|
||||
CheckCircle,
|
||||
ChevronUpDown,
|
||||
Icon,
|
||||
type IconSource,
|
||||
} from 'svelte-hero-icons'
|
||||
import type { Attachment } from 'svelte/attachments'
|
||||
import type { ClassValue, HTMLSelectAttributes } from 'svelte/elements'
|
||||
import { buttonSize } from '../../button/Button.svelte'
|
||||
import Menu from '../../popover/Menu.svelte'
|
||||
import MenuButton from '../../popover/MenuButton.svelte'
|
||||
import Label from '../Label.svelte'
|
||||
|
||||
let open = $state(false)
|
||||
let element: HTMLSelectElement | undefined = $state()
|
||||
|
||||
interface SelectContext {
|
||||
options: {
|
||||
value: string
|
||||
label: string
|
||||
icon?: IconSource
|
||||
disabled?: boolean
|
||||
isLabel?: boolean
|
||||
}[]
|
||||
}
|
||||
|
||||
const context = setContext<SelectContext>('select', {
|
||||
options: [],
|
||||
})
|
||||
|
||||
let {
|
||||
value = $bindable(undefined),
|
||||
placeholder = undefined,
|
||||
label = undefined,
|
||||
size = 'md',
|
||||
class: clazz = '',
|
||||
baseClass = '',
|
||||
selectClass = '',
|
||||
customLabel,
|
||||
children,
|
||||
customOption,
|
||||
oncontextmenu,
|
||||
onchange,
|
||||
placement = 'bottom',
|
||||
target: passedTarget,
|
||||
...rest
|
||||
}: Props<T> = $props()
|
||||
</script>
|
||||
|
||||
{#snippet selectTarget(attachment: Attachment)}
|
||||
<Label
|
||||
text={label}
|
||||
customText={customLabel}
|
||||
class={['space-y-1 relative max-w-full w-max min-w-0', baseClass]}
|
||||
>
|
||||
<div class="relative max-w-full" role="presentation">
|
||||
<select
|
||||
{@attach attachment}
|
||||
{...rest}
|
||||
bind:this={element}
|
||||
class={[
|
||||
buttonSize[size],
|
||||
'btn btn-secondary select rounded-xl appearance-none pr-6! w-full',
|
||||
selectClass,
|
||||
clazz,
|
||||
]}
|
||||
bind:value
|
||||
onmousedown={(e) => {
|
||||
e.preventDefault()
|
||||
}}
|
||||
onkeypress={(e) => {
|
||||
e.preventDefault()
|
||||
open = !open
|
||||
}}
|
||||
{onchange}
|
||||
{oncontextmenu}
|
||||
{placeholder}
|
||||
>
|
||||
{#if placeholder}
|
||||
<option disabled selected value="">{placeholder}</option>
|
||||
{/if}
|
||||
{@render children?.()}
|
||||
</select>
|
||||
<Icon
|
||||
src={ChevronUpDown}
|
||||
micro
|
||||
size="16"
|
||||
class="absolute bottom-1/2 translate-y-1/2 right-1 box-border pointer-events-none z-10 text-slate-600 dark:text-zinc-400"
|
||||
/>
|
||||
</div>
|
||||
</Label>
|
||||
{/snippet}
|
||||
|
||||
<Menu bind:open {placement}>
|
||||
{#snippet target(attachment)}
|
||||
{@const render = passedTarget ?? selectTarget}
|
||||
{@render render?.(attachment)}
|
||||
{/snippet}
|
||||
{#each context.options as option (option)}
|
||||
{#if customOption}{@render customOption({
|
||||
option,
|
||||
selected: option.value == value,
|
||||
})}{:else}
|
||||
<MenuButton
|
||||
onclick={async () => {
|
||||
value = option.value
|
||||
await tick()
|
||||
element?.dispatchEvent(new Event('change', { bubbles: true }))
|
||||
}}
|
||||
size="custom"
|
||||
disabled={option.disabled}
|
||||
color="none"
|
||||
class={[
|
||||
'min-h-0! py-1 hover:bg-slate-100 dark:hover:bg-zinc-800',
|
||||
option.value == value &&
|
||||
'bg-slate-100 dark:bg-zinc-800 text-primary-900 dark:text-primary-100 font-medium',
|
||||
option.disabled &&
|
||||
'pointer-events-none text-slate-600 dark:text-zinc-400',
|
||||
option.isLabel && 'text-xs mt-2',
|
||||
]}
|
||||
>
|
||||
{#if option.value == value}
|
||||
<Icon
|
||||
src={CheckCircle}
|
||||
size="16"
|
||||
micro
|
||||
class="text-primary-900 dark:text-primary-100"
|
||||
/>
|
||||
{:else if option.icon}
|
||||
<Icon
|
||||
src={option.icon}
|
||||
size="16"
|
||||
micro
|
||||
class="text-slate-600 dark:text-zinc-400"
|
||||
/>
|
||||
{/if}
|
||||
{@html option.label}
|
||||
</MenuButton>
|
||||
{/if}
|
||||
{/each}
|
||||
</Menu>
|
||||
|
||||
<style>
|
||||
@reference '../../../../app.css';
|
||||
</style>
|
||||
@@ -0,0 +1,33 @@
|
||||
// Reexport your entry components here
|
||||
|
||||
export { default as Button } from './button/Button.svelte'
|
||||
export { buttonColor, buttonSize, buttonShadow } from './button/Button.svelte'
|
||||
export { default as Spinner } from './loader/Spinner.svelte'
|
||||
export { default as TextLoader } from './loader/TextLoader.svelte'
|
||||
export { default as Material } from './materials/Material.svelte'
|
||||
|
||||
export { default as Label } from './forms/Label.svelte'
|
||||
export { default as FileInput } from './forms/FileInput.svelte'
|
||||
export { default as TextInput } from './forms/TextInput.svelte'
|
||||
export { default as TextArea } from './forms/TextArea.svelte'
|
||||
export { default as Select } from './forms/select/Select.svelte'
|
||||
export { default as Option } from './forms/select/Option.svelte'
|
||||
|
||||
export { default as Switch } from './forms/Switch.svelte'
|
||||
|
||||
export { default as Popover } from './popover/Popover.svelte'
|
||||
export { default as Menu } from './popover/Menu.svelte'
|
||||
export { default as MenuDivider } from './popover/MenuDivider.svelte'
|
||||
export { default as MenuButton } from './popover/MenuButton.svelte'
|
||||
export { default as Disclosure } from './disclosure/Disclosure.svelte'
|
||||
export { default as Expandable } from './disclosure/Expandable.svelte'
|
||||
export { default as Modal } from './modal/Modal.svelte'
|
||||
export { modal, action } from './modal/modal.js'
|
||||
export { default as ModalContainer } from './modal/ModalContainer.svelte'
|
||||
|
||||
export { toast, removeToast, toasts, toastColors } from './toast/toasts.js'
|
||||
export { default as ToastContainer } from './toast/ToastContainer.svelte'
|
||||
|
||||
export { default as Note } from './note/Note.svelte'
|
||||
export { default as Badge } from './badge/Badge.svelte'
|
||||
export { default as Search } from './search/Search.svelte'
|
||||
@@ -0,0 +1,43 @@
|
||||
<script lang="ts">
|
||||
interface Props {
|
||||
width?: number
|
||||
}
|
||||
|
||||
let { width = 16, ...rest }: Props = $props()
|
||||
</script>
|
||||
|
||||
<svg
|
||||
{...rest}
|
||||
{width}
|
||||
height={width}
|
||||
stroke="currentColor"
|
||||
fill="none"
|
||||
stroke-width="2"
|
||||
viewBox="0 0 24 24"
|
||||
stroke-linecap="round"
|
||||
stroke-linejoin="round"
|
||||
class="animate-spin"
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
aria-live="polite"
|
||||
role="status"
|
||||
>
|
||||
<line x1="12" y1="2" x2="12" y2="6"></line>
|
||||
<line x1="12" y1="18" x2="12" y2="22"></line>
|
||||
<line x1="4.93" y1="4.93" x2="7.76" y2="7.76"></line>
|
||||
<line x1="16.24" y1="16.24" x2="19.07" y2="19.07"></line>
|
||||
<line x1="2" y1="12" x2="6" y2="12"></line>
|
||||
<line x1="18" y1="12" x2="22" y2="12"></line>
|
||||
<line x1="4.93" y1="19.07" x2="7.76" y2="16.24"></line>
|
||||
<line x1="16.24" y1="7.76" x2="19.07" y2="4.93"></line>
|
||||
</svg>
|
||||
|
||||
<style>
|
||||
@keyframes spin {
|
||||
to {
|
||||
transform: rotate(360deg);
|
||||
}
|
||||
}
|
||||
.animate-spin {
|
||||
animation: spin 1s linear infinite;
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,46 @@
|
||||
<script lang="ts">
|
||||
import type { Snippet } from 'svelte'
|
||||
|
||||
interface Props {
|
||||
children: Snippet
|
||||
}
|
||||
|
||||
let { children }: Props = $props()
|
||||
</script>
|
||||
|
||||
<div
|
||||
class="text-loader font-medium text-lg tracking-tight"
|
||||
aria-busy="true"
|
||||
aria-live="polite"
|
||||
>
|
||||
{@render children?.()}
|
||||
</div>
|
||||
|
||||
<style>
|
||||
.text-loader {
|
||||
color: transparent;
|
||||
background: linear-gradient(
|
||||
to right,
|
||||
var(--color-zinc-500),
|
||||
var(--color-zinc-50),
|
||||
var(--color-zinc-500)
|
||||
);
|
||||
background-size: 200%;
|
||||
background-clip: text;
|
||||
display: inline;
|
||||
width: max-content;
|
||||
animation: pulse 1s infinite linear;
|
||||
}
|
||||
|
||||
@keyframes pulse {
|
||||
0% {
|
||||
background-position: 100%;
|
||||
}
|
||||
50% {
|
||||
background-position: 0%;
|
||||
}
|
||||
100% {
|
||||
background-position: -100%;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,84 @@
|
||||
<script lang="ts">
|
||||
import type { ClassValue, HTMLAttributes } from 'svelte/elements'
|
||||
|
||||
type Elevation = keyof typeof elevationClass
|
||||
type Padding = keyof typeof paddingClass
|
||||
type Rounding = keyof typeof roundedClass
|
||||
type Color = keyof typeof colorClass
|
||||
|
||||
const elevationClass = {
|
||||
flat: '',
|
||||
low: 'shadow-2xs',
|
||||
medium: 'shadow-xs',
|
||||
high: 'shadow-md',
|
||||
xhigh: 'shadow-lg',
|
||||
max: 'shadow-xl',
|
||||
}
|
||||
|
||||
const paddingClass = {
|
||||
none: 'p-0',
|
||||
sm: 'p-2',
|
||||
md: 'p-4',
|
||||
lg: 'p-5',
|
||||
xl: 'p-6',
|
||||
}
|
||||
|
||||
const roundedClass = {
|
||||
none: '',
|
||||
sm: 'rounded-xs',
|
||||
md: 'rounded-md',
|
||||
lg: 'rounded-lg',
|
||||
xl: 'rounded-xl',
|
||||
'2xl': 'rounded-2xl',
|
||||
full: 'rounded-full',
|
||||
}
|
||||
|
||||
const colorClass = {
|
||||
default:
|
||||
'bg-white dark:bg-zinc-900 border border-slate-200 dark:border-zinc-800 border-b-slate-300 dark:border-t-zinc-700',
|
||||
distinct:
|
||||
'bg-white dark:bg-zinc-900 border border-slate-200 dark:border-zinc-800 border-b-slate-300 dark:border-t-zinc-700',
|
||||
transparent:
|
||||
'border border-slate-200 dark:border-zinc-900 border-b-slate-300 dark:border-t-zinc-800',
|
||||
uniform:
|
||||
'bg-white dark:bg-zinc-950 border border-slate-200 dark:border-zinc-900',
|
||||
none: '',
|
||||
}
|
||||
|
||||
interface Props extends HTMLAttributes<HTMLElement> {
|
||||
elevation?: Elevation
|
||||
padding?: Padding
|
||||
rounding?: Rounding
|
||||
color?: Color
|
||||
element?: string
|
||||
class?: ClassValue
|
||||
children?: import('svelte').Snippet
|
||||
[key: string]: any
|
||||
}
|
||||
|
||||
let {
|
||||
elevation = 'flat',
|
||||
padding = 'md',
|
||||
rounding = 'xl',
|
||||
color = 'uniform',
|
||||
element = 'div',
|
||||
class: clazz = '',
|
||||
children,
|
||||
...rest
|
||||
}: Props = $props()
|
||||
</script>
|
||||
|
||||
<svelte:element
|
||||
this={element}
|
||||
{...rest}
|
||||
class={[
|
||||
elevationClass[elevation],
|
||||
paddingClass[padding],
|
||||
roundedClass[rounding],
|
||||
colorClass[color],
|
||||
'text-sm',
|
||||
clazz,
|
||||
]}
|
||||
>
|
||||
{@render children?.()}
|
||||
</svelte:element>
|
||||
@@ -0,0 +1,150 @@
|
||||
<script lang="ts">
|
||||
import { pushState, replaceState } from '$app/navigation'
|
||||
import { page } from '$app/state'
|
||||
import type { Snippet } from 'svelte'
|
||||
import { XMark } from 'svelte-hero-icons'
|
||||
import { backOut } from 'svelte/easing'
|
||||
import type { ClassValue } from 'svelte/elements'
|
||||
import { fade, scale } from 'svelte/transition'
|
||||
import { trapFocus } from 'trap-focus-svelte'
|
||||
import { Button } from '../index.js'
|
||||
import Portal from '../popover/Portal.svelte'
|
||||
|
||||
interface Props {
|
||||
action?: string | undefined
|
||||
open?: boolean
|
||||
title?: string | undefined | null
|
||||
dismissable?: boolean
|
||||
class?: ClassValue
|
||||
customTitle?: Snippet
|
||||
children?: Snippet
|
||||
actions?: Snippet<[{ action: string }]>
|
||||
ondismissed?: () => void
|
||||
onaction?: () => void
|
||||
}
|
||||
|
||||
let {
|
||||
action = undefined,
|
||||
open = $bindable(false),
|
||||
title = undefined,
|
||||
dismissable = true,
|
||||
customTitle,
|
||||
children,
|
||||
actions,
|
||||
ondismissed,
|
||||
onaction,
|
||||
class: clazz = '',
|
||||
}: Props = $props()
|
||||
|
||||
const modalId = crypto.randomUUID()
|
||||
|
||||
let el = $state<HTMLElement>()
|
||||
let hasHistoryEntry = false
|
||||
|
||||
$effect(() => {
|
||||
if (open && modalId && !hasHistoryEntry) {
|
||||
pushState('', {
|
||||
openModals: [...(page.state.openModals ?? []), modalId],
|
||||
})
|
||||
hasHistoryEntry = true
|
||||
}
|
||||
})
|
||||
|
||||
$effect(() => {
|
||||
if (modalId && hasHistoryEntry) {
|
||||
const currentModals = page.state.openModals ?? []
|
||||
const isInHistory = currentModals.includes(modalId)
|
||||
|
||||
if (!isInHistory && open) {
|
||||
replaceState('', {
|
||||
openModals: currentModals.filter((id) => id !== modalId),
|
||||
})
|
||||
hasHistoryEntry = false
|
||||
open = false
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
function onclose() {
|
||||
open = false
|
||||
|
||||
if ((page.state.openModals ?? []).includes(modalId)) history.back()
|
||||
hasHistoryEntry = false
|
||||
|
||||
ondismissed?.()
|
||||
}
|
||||
</script>
|
||||
|
||||
<Portal>
|
||||
{#if open}
|
||||
<!--svelte-ignore a11y_interactive_supports_focus-->
|
||||
<!--svelte-ignore a11y_click_events_have_key_events-->
|
||||
<div
|
||||
role="dialog"
|
||||
class={[
|
||||
'overflow-hidden fixed top-0 left-0 w-screen h-screen z-100',
|
||||
'flex flex-col items-center justify-center backdrop-blur-xs',
|
||||
'bg-white/50 dark:bg-black/50 box-border p-4',
|
||||
]}
|
||||
transition:fade|global={{ duration: 100 }}
|
||||
onclick={(e) => {
|
||||
// @ts-expect-error html node hell
|
||||
if (!el.contains(e.target)) onclose()
|
||||
}}
|
||||
>
|
||||
<div
|
||||
bind:this={el}
|
||||
use:trapFocus
|
||||
transition:scale|global={{
|
||||
start: 0.97,
|
||||
easing: backOut,
|
||||
duration: 300,
|
||||
}}
|
||||
class={[
|
||||
'w-full border border-slate-200 border-b-slate-300 dark:border-zinc-900',
|
||||
'rounded-2xl max-w-lg box-border mx-auto overscroll-contain shadow-lg overflow-auto',
|
||||
'p-5 flex flex-col gap-2 dark:bg-zinc-950 bg-slate-50 relative',
|
||||
clazz,
|
||||
]}
|
||||
>
|
||||
{#if dismissable}
|
||||
<Button
|
||||
class="absolute top-0 right-0 m-2 text-slate-600 dark:text-zinc-400"
|
||||
color="tertiary"
|
||||
size="square-sm"
|
||||
onclick={onclose}
|
||||
icon={XMark}
|
||||
></Button>
|
||||
{/if}
|
||||
{#if title !== null}
|
||||
<h1 class="font-medium tracking-tight text-xl leading-5 max-w-full">
|
||||
{#if customTitle}
|
||||
{@render customTitle?.()}
|
||||
{:else if title}
|
||||
{title}
|
||||
{/if}
|
||||
</h1>
|
||||
{/if}
|
||||
{@render children?.()}
|
||||
{#if action}
|
||||
<div class="mt-2 flex w-full">
|
||||
{#if actions}{@render actions({ action })}{:else}
|
||||
<Button
|
||||
class="w-full"
|
||||
onclick={() => {
|
||||
onaction?.()
|
||||
onclose()
|
||||
}}
|
||||
color="primary"
|
||||
size="lg"
|
||||
rounding="xl"
|
||||
>
|
||||
{action}
|
||||
</Button>
|
||||
{/if}
|
||||
</div>
|
||||
{/if}
|
||||
</div>
|
||||
</div>
|
||||
{/if}
|
||||
</Portal>
|
||||
@@ -0,0 +1,52 @@
|
||||
<script lang="ts">
|
||||
import { run } from 'svelte/legacy'
|
||||
|
||||
import Button from '../button/Button.svelte'
|
||||
import Modal from '../modal/Modal.svelte'
|
||||
import { Icon } from 'svelte-hero-icons'
|
||||
import { shownModal } from './modal.js'
|
||||
|
||||
let open = $state(false)
|
||||
|
||||
// reactivity hack
|
||||
run(() => {
|
||||
open = $shownModal ? true : false
|
||||
})
|
||||
</script>
|
||||
|
||||
{#if $shownModal}
|
||||
<Modal
|
||||
title={$shownModal.title}
|
||||
dismissable={$shownModal.dismissable}
|
||||
ondismissed={() => shownModal.set(undefined)}
|
||||
bind:open
|
||||
>
|
||||
{#if $shownModal.snippet}
|
||||
{@render $shownModal.snippet()}
|
||||
{/if}
|
||||
{#if $shownModal.body}
|
||||
<p>{$shownModal.body}</p>
|
||||
{/if}
|
||||
{#if $shownModal.actions}
|
||||
<div
|
||||
class="flex items-center gap-2 {$shownModal.actions.length >= 3
|
||||
? 'flex-col'
|
||||
: 'flex-row'}"
|
||||
>
|
||||
{#each $shownModal.actions as action (action)}
|
||||
<Button
|
||||
size="lg"
|
||||
class="flex-1 w-full"
|
||||
onclick={action.action}
|
||||
color={action.type}
|
||||
>
|
||||
{#if action.icon}
|
||||
<Icon src={action.icon} mini size="16" />
|
||||
{/if}
|
||||
{action.content}
|
||||
</Button>
|
||||
{/each}
|
||||
</div>
|
||||
{/if}
|
||||
</Modal>
|
||||
{/if}
|
||||
@@ -0,0 +1,70 @@
|
||||
import { t } from '$lib/app/i18n/translations'
|
||||
import { type Snippet } from 'svelte'
|
||||
import type { IconSource } from 'svelte-hero-icons'
|
||||
import { writable } from 'svelte/store'
|
||||
|
||||
export const shownModal = writable<Modal | undefined>()
|
||||
|
||||
interface Modal {
|
||||
actions: Action[]
|
||||
title: string
|
||||
body?: string
|
||||
/**
|
||||
* Whether the modal has a close button or not
|
||||
*/
|
||||
dismissable: boolean
|
||||
snippet?: Snippet
|
||||
type: 'error' | 'info' | 'success'
|
||||
}
|
||||
|
||||
type Action = {
|
||||
/**
|
||||
* What function to run when this action is clicked. If undefined, it'll close the window.
|
||||
*/
|
||||
action: () => void
|
||||
type: 'danger' | 'secondary' | 'primary'
|
||||
content: string
|
||||
icon?: IconSource
|
||||
close: boolean
|
||||
}
|
||||
|
||||
export const action = (action?: {
|
||||
action?: () => void
|
||||
type?: 'danger' | 'secondary' | 'primary'
|
||||
content?: string
|
||||
icon?: IconSource
|
||||
close?: boolean
|
||||
}): Action => ({
|
||||
action: action?.action
|
||||
? () => {
|
||||
action?.action?.()
|
||||
if (action.close) shownModal.set(undefined)
|
||||
}
|
||||
: () => shownModal.set(undefined),
|
||||
type: action?.type ?? 'secondary',
|
||||
content: action?.content || t.get('common.back'),
|
||||
close: action?.close ?? true,
|
||||
})
|
||||
|
||||
export function modal(inputModal: {
|
||||
actions?: Action[]
|
||||
title: string
|
||||
body?: string
|
||||
/**
|
||||
* Whether the modal has a close button or not
|
||||
*/
|
||||
dismissable?: boolean
|
||||
snippet?: Snippet
|
||||
type?: 'error' | 'info' | 'success'
|
||||
}) {
|
||||
const modal: Modal = {
|
||||
actions: inputModal.actions ?? [action()],
|
||||
dismissable: inputModal.dismissable ?? true,
|
||||
title: inputModal.title,
|
||||
body: inputModal.body,
|
||||
type: inputModal.type ?? 'info',
|
||||
snippet: inputModal.snippet,
|
||||
}
|
||||
|
||||
shownModal.set(modal)
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
<script lang="ts">
|
||||
import type { ClassValue } from 'svelte/elements'
|
||||
import { Material } from '../index.js'
|
||||
import { Icon, InformationCircle } from 'svelte-hero-icons'
|
||||
|
||||
interface Props {
|
||||
content?: string | undefined
|
||||
children?: import('svelte').Snippet
|
||||
class?: ClassValue
|
||||
}
|
||||
|
||||
let { content = undefined, children, class: clazz = '' }: Props = $props()
|
||||
</script>
|
||||
|
||||
<Material
|
||||
padding="none"
|
||||
color="uniform"
|
||||
class="flex flex-row space-x-2 items-center px-4 py-3 rounded-3xl! {clazz}"
|
||||
>
|
||||
<Icon
|
||||
src={InformationCircle}
|
||||
size="20"
|
||||
mini
|
||||
class="text-primary-900 dark:text-primary-100"
|
||||
/>
|
||||
<div class="flex flex-col md:flex-row items-center w-full">
|
||||
{#if children}
|
||||
{@render children?.()}
|
||||
{:else if content}
|
||||
<p>
|
||||
{content}
|
||||
</p>
|
||||
{/if}
|
||||
</div>
|
||||
</Material>
|
||||
@@ -0,0 +1,60 @@
|
||||
<script lang="ts">
|
||||
import {
|
||||
flip,
|
||||
offset,
|
||||
shift,
|
||||
type Middleware,
|
||||
type Placement,
|
||||
type Strategy,
|
||||
} from '@floating-ui/core'
|
||||
import Popover from '../popover/Popover.svelte'
|
||||
import type { Snippet } from 'svelte'
|
||||
import type { Attachment } from 'svelte/attachments'
|
||||
|
||||
interface Props {
|
||||
openOnHover?: boolean
|
||||
open?: boolean
|
||||
placement?: Placement
|
||||
middleware?: Middleware[]
|
||||
strategy?: Strategy
|
||||
class?: string
|
||||
target?: Snippet<[Attachment]>
|
||||
children?: import('svelte').Snippet<[boolean]>
|
||||
}
|
||||
|
||||
let {
|
||||
openOnHover = false,
|
||||
open = $bindable(false),
|
||||
placement = 'bottom-start',
|
||||
middleware = [offset(6), shift({ padding: 6 }), flip()],
|
||||
strategy = 'absolute',
|
||||
target,
|
||||
children,
|
||||
...rest
|
||||
}: Props = $props()
|
||||
</script>
|
||||
|
||||
<Popover
|
||||
{openOnHover}
|
||||
{placement}
|
||||
{middleware}
|
||||
{strategy}
|
||||
{target}
|
||||
{...rest}
|
||||
bind:open
|
||||
popoverClass="rounded-xl w-full backdrop-blur-md max-w-72"
|
||||
>
|
||||
{#snippet popover()}
|
||||
<div
|
||||
class="w-full max-w-sm max-h-128 overflow-auto list-none shadow-lg rounded-xl"
|
||||
>
|
||||
<div
|
||||
class="flex flex-col p-1 list-none bg-white/80 dark:bg-zinc-900/90 rounded-xl
|
||||
border border-slate-200 dark:border-zinc-800 gap-px"
|
||||
role="menu"
|
||||
>
|
||||
{@render children?.(open)}
|
||||
</div>
|
||||
</div>
|
||||
{/snippet}
|
||||
</Popover>
|
||||
@@ -0,0 +1,79 @@
|
||||
<script lang="ts">
|
||||
import type { Snippet } from 'svelte'
|
||||
import Button, {
|
||||
type ButtonAlignment,
|
||||
type ButtonColor,
|
||||
type ButtonProps,
|
||||
} from '../button/Button.svelte'
|
||||
import { ChevronRight, Icon, type IconSource } from 'svelte-hero-icons'
|
||||
import type { ClassValue } from 'svelte/elements'
|
||||
|
||||
interface Props extends ButtonProps {
|
||||
color?: ButtonColor
|
||||
alignment?: ButtonAlignment
|
||||
href?: string | undefined
|
||||
disabled?: boolean
|
||||
class?: ClassValue
|
||||
icon?: IconSource
|
||||
prefix?: Snippet
|
||||
children?: Snippet
|
||||
suffix?: Snippet
|
||||
nest?: boolean
|
||||
}
|
||||
|
||||
let {
|
||||
color = 'tertiary',
|
||||
alignment = 'left',
|
||||
href = undefined,
|
||||
disabled = false,
|
||||
class: clazz = '',
|
||||
icon,
|
||||
prefix: passedPrefix,
|
||||
children,
|
||||
suffix: passedSuffix,
|
||||
nest,
|
||||
...rest
|
||||
}: Props = $props()
|
||||
</script>
|
||||
|
||||
<Button
|
||||
{...rest}
|
||||
{color}
|
||||
type="none"
|
||||
size="custom"
|
||||
rounding="none"
|
||||
class={[
|
||||
'w-full px-3 py-1.5 min-h-7 duration-75 hover:cursor-default font-normal rounded-lg flex items-center gap-1 text-sm',
|
||||
disabled && 'opacity-70 pointer-events-none cursor-not-allowed',
|
||||
color == 'tertiary' && 'dark:hover:bg-zinc-800/70',
|
||||
clazz,
|
||||
]}
|
||||
{alignment}
|
||||
{href}
|
||||
{disabled}
|
||||
data-autoclose={nest ? 'false' : 'true'}
|
||||
shadow="none"
|
||||
role="menuitem"
|
||||
>
|
||||
{#snippet prefix()}
|
||||
<div
|
||||
class={[
|
||||
'contents shrink-0',
|
||||
color == 'tertiary' && 'text-slate-600 dark:text-zinc-400',
|
||||
]}
|
||||
>
|
||||
{#if icon}
|
||||
<Icon src={icon} micro size="16" />
|
||||
{:else}
|
||||
{@render passedPrefix?.()}
|
||||
{/if}
|
||||
</div>
|
||||
{/snippet}
|
||||
{@render children?.()}
|
||||
{#snippet suffix()}
|
||||
{@render passedSuffix?.()}
|
||||
{#if nest}
|
||||
<Icon src={ChevronRight} size="16" micro class="ml-auto" />
|
||||
{/if}
|
||||
{/snippet}
|
||||
</Button>
|
||||
@@ -0,0 +1,20 @@
|
||||
<script lang="ts">
|
||||
import type { Snippet } from 'svelte'
|
||||
|
||||
let {
|
||||
showLabel = false,
|
||||
children,
|
||||
}: { showLabel?: boolean; children: Snippet } = $props()
|
||||
</script>
|
||||
|
||||
<div
|
||||
class="text-slate-800 dark:text-zinc-200 text-xs font-medium mx-3 my-1.5 flex items-center gap-1"
|
||||
>
|
||||
<div class={[!showLabel && 'sr-only']}>{@render children?.()}</div>
|
||||
<hr
|
||||
class={[
|
||||
'shrink flex-1 border-slate-100 dark:border-zinc-800',
|
||||
!showLabel && '-mx-4',
|
||||
]}
|
||||
/>
|
||||
</div>
|
||||
@@ -0,0 +1,158 @@
|
||||
<script lang="ts">
|
||||
import {
|
||||
flip,
|
||||
offset,
|
||||
shift,
|
||||
type Middleware,
|
||||
type Placement,
|
||||
type Strategy,
|
||||
} from '@floating-ui/core'
|
||||
import { createFloatingActions } from 'svelte-floating-ui'
|
||||
import type { Attachment } from 'svelte/attachments'
|
||||
import { expoOut } from 'svelte/easing'
|
||||
import { scale } from 'svelte/transition'
|
||||
import { trapFocus } from 'trap-focus-svelte'
|
||||
import { Material } from '../index'
|
||||
import Portal from './Portal.svelte'
|
||||
|
||||
interface Props {
|
||||
openOnHover?: boolean
|
||||
open?: boolean
|
||||
autoClose?: boolean
|
||||
placement?: Placement
|
||||
middleware?: Middleware[]
|
||||
strategy?: Strategy
|
||||
popoverClass?: string
|
||||
target?: import('svelte').Snippet<[Attachment]>
|
||||
popover?: import('svelte').Snippet<[boolean]>
|
||||
children?: import('svelte').Snippet<[boolean]>
|
||||
}
|
||||
|
||||
let {
|
||||
openOnHover = false,
|
||||
open = $bindable(false),
|
||||
autoClose = true,
|
||||
placement = 'bottom-start',
|
||||
middleware = [offset(6), shift(), flip()],
|
||||
strategy = 'fixed',
|
||||
popoverClass = '',
|
||||
target,
|
||||
popover,
|
||||
children,
|
||||
}: Props = $props()
|
||||
|
||||
let popoverEl = $state<HTMLElement>()
|
||||
|
||||
let origins: Record<Placement, string> = {
|
||||
bottom: 'top',
|
||||
'bottom-end': 'top right',
|
||||
'bottom-start': 'top left',
|
||||
left: 'left',
|
||||
'left-end': 'left',
|
||||
'left-start': 'left',
|
||||
right: 'right',
|
||||
'right-end': 'right',
|
||||
'right-start': 'right',
|
||||
top: 'bottom',
|
||||
'top-end': 'bottom right',
|
||||
'top-start': 'bottom',
|
||||
}
|
||||
|
||||
const [floatingRef, floatingContent] = createFloatingActions({
|
||||
strategy: strategy,
|
||||
placement: placement,
|
||||
middleware: middleware,
|
||||
onComputed: ({ placement }) => {
|
||||
if (popoverEl) {
|
||||
popoverEl.style.transformOrigin = origins[placement]
|
||||
}
|
||||
},
|
||||
})
|
||||
|
||||
const menuAttach: Attachment = (element) => {
|
||||
const e = element as HTMLButtonElement
|
||||
|
||||
function toggle(force?: boolean) {
|
||||
if (!open && force == false) return
|
||||
|
||||
const newOpen = force ?? !open
|
||||
|
||||
if (!newOpen) {
|
||||
e.focus()
|
||||
}
|
||||
|
||||
open = newOpen
|
||||
}
|
||||
|
||||
const mouseLeave = () => openOnHover && toggle(false)
|
||||
const focus = () => openOnHover && toggle(true)
|
||||
const focusout = () => openOnHover && toggle(false)
|
||||
const click = () => {
|
||||
toggle()
|
||||
|
||||
if (autoClose) {
|
||||
const clickHandler = (event: Event) => {
|
||||
if (!e) return
|
||||
|
||||
const target = event.target as Element | null
|
||||
|
||||
if (!e?.contains(event.target as Element | null)) {
|
||||
if (target?.closest("[data-autoclose='false']") == null) {
|
||||
toggle(false)
|
||||
document.removeEventListener('click', clickHandler)
|
||||
}
|
||||
}
|
||||
}
|
||||
document.addEventListener('click', clickHandler)
|
||||
}
|
||||
}
|
||||
const keydown = (event: KeyboardEvent) => {
|
||||
if (event.key === 'Escape') {
|
||||
toggle(false)
|
||||
}
|
||||
}
|
||||
|
||||
e.addEventListener('mouseleave', mouseLeave)
|
||||
e.addEventListener('focus', focus)
|
||||
e.addEventListener('focusout', focusout)
|
||||
e.addEventListener('click', click)
|
||||
popoverEl?.addEventListener('keydown', keydown)
|
||||
|
||||
floatingRef(e)
|
||||
|
||||
return () => {
|
||||
e.removeEventListener('mouseleave', mouseLeave)
|
||||
e.removeEventListener('focus', focus)
|
||||
e.removeEventListener('focusout', focusout)
|
||||
e.removeEventListener('click', click)
|
||||
popoverEl?.removeEventListener('keydown', keydown)
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
{@render target?.(menuAttach)}
|
||||
|
||||
{#if open}
|
||||
<Portal class="z-150">
|
||||
<div
|
||||
transition:scale={{
|
||||
duration: 175,
|
||||
start: 0.95,
|
||||
opacity: 0,
|
||||
easing: expoOut,
|
||||
}}
|
||||
class={['z-150', popoverClass]}
|
||||
use:floatingContent
|
||||
use:trapFocus
|
||||
bind:this={popoverEl}
|
||||
>
|
||||
{#if popover}
|
||||
{@render popover(open)}
|
||||
{:else}
|
||||
<Material elevation="high" color="distinct" class="flex flex-col">
|
||||
{@render children?.(open)}
|
||||
</Material>
|
||||
{/if}
|
||||
</div>
|
||||
</Portal>
|
||||
{/if}
|
||||
@@ -0,0 +1,42 @@
|
||||
<script lang="ts">
|
||||
import { browser } from '$app/environment'
|
||||
|
||||
// https://github.com/sveltejs/svelte/issues/3088#issuecomment-505785516
|
||||
import { onMount, onDestroy, createEventDispatcher } from 'svelte'
|
||||
let slottedElements = $state<HTMLDivElement>(),
|
||||
portal: HTMLDivElement
|
||||
|
||||
const dispatch = createEventDispatcher<{ mounted: void }>()
|
||||
|
||||
onMount(() => {
|
||||
if (!browser) return
|
||||
portal = document?.createElement('div')
|
||||
if (portal) portal.className = 'portal-mount'
|
||||
document?.body.appendChild(portal)
|
||||
if (slottedElements) portal?.appendChild(slottedElements)
|
||||
dispatch('mounted')
|
||||
})
|
||||
|
||||
onDestroy(() => {
|
||||
portal.remove()
|
||||
})
|
||||
|
||||
interface Props {
|
||||
class?: string
|
||||
children?: import('svelte').Snippet
|
||||
}
|
||||
|
||||
let { class: clazz = '', children }: Props = $props()
|
||||
</script>
|
||||
|
||||
<div class="portal-initial-mount-point">
|
||||
<div bind:this={slottedElements} class="portal-content {clazz || ''}">
|
||||
{@render children?.()}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<style>
|
||||
.portal-initial-mount-point {
|
||||
display: none;
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,118 @@
|
||||
<script lang="ts" module>
|
||||
interface Props<T> extends Omit<TextInputProps, 'onselect' | 'children'> {
|
||||
query?: string
|
||||
selected?: T | undefined
|
||||
search: (query: string) => Promise<T[]>
|
||||
extractName: (item: T) => string
|
||||
select?: (item: T) => void
|
||||
input?: import('svelte').Snippet
|
||||
noresults?: import('svelte').Snippet
|
||||
children?: import('svelte').Snippet<
|
||||
[
|
||||
{
|
||||
select: (item?: T) => void
|
||||
item: T
|
||||
extractName: (item: T) => string
|
||||
},
|
||||
]
|
||||
>
|
||||
onselect?: (value?: T) => void
|
||||
oninput?: TextInputProps['oninput']
|
||||
}
|
||||
|
||||
export type { Props as SearchProps }
|
||||
</script>
|
||||
|
||||
<script lang="ts" generics="T">
|
||||
import type { TextInputProps } from 'mono-svelte/forms/TextInput.svelte'
|
||||
import { Menu, TextInput, Spinner, MenuButton } from '../index.js'
|
||||
import { debounce } from '../util/time.js'
|
||||
import { Icon, MagnifyingGlass } from 'svelte-hero-icons'
|
||||
|
||||
let items: T[] = $state([])
|
||||
|
||||
/**
|
||||
* This is here so that the menu doesn't open as soon as it's mounted.
|
||||
*/
|
||||
let openMenu = $state(false)
|
||||
let searching = $state(false)
|
||||
|
||||
let {
|
||||
query = $bindable(''),
|
||||
selected = $bindable(undefined),
|
||||
search,
|
||||
extractName,
|
||||
select = (item: T) => {
|
||||
selected = item
|
||||
query = extractName(item)
|
||||
onselect?.(item)
|
||||
},
|
||||
input,
|
||||
noresults,
|
||||
children,
|
||||
onselect,
|
||||
oninput,
|
||||
...rest
|
||||
}: Props<T> = $props()
|
||||
|
||||
const debounceFunc = debounce(async () => {
|
||||
searching = true
|
||||
openMenu = true
|
||||
items = await search(query)
|
||||
searching = false
|
||||
})
|
||||
</script>
|
||||
|
||||
<div class="relative">
|
||||
<Menu bind:open={openMenu}>
|
||||
{#snippet target(attachment)}
|
||||
{#if input}{@render input()}{:else}
|
||||
<TextInput
|
||||
{@attach attachment}
|
||||
bind:value={query}
|
||||
oninput={(e) => {
|
||||
searching = true
|
||||
openMenu = true
|
||||
oninput?.(e)
|
||||
debounceFunc()
|
||||
}}
|
||||
onfocus={(e) => {
|
||||
searching = true
|
||||
openMenu = true
|
||||
oninput?.(e)
|
||||
debounceFunc()
|
||||
}}
|
||||
{...rest}
|
||||
inlineAffixes
|
||||
>
|
||||
{#snippet prefix()}
|
||||
<div class="h-5 flex items-center">
|
||||
<Icon src={MagnifyingGlass} mini size="16" />
|
||||
</div>
|
||||
{/snippet}
|
||||
</TextInput>
|
||||
{/if}
|
||||
{/snippet}
|
||||
{#if searching}
|
||||
<div class="w-full h-24 grid place-items-center">
|
||||
<Spinner width={24} />
|
||||
</div>
|
||||
{:else if items.length == 0}
|
||||
<div class="text-center h-24 grid place-items-center">
|
||||
{#if noresults}{@render noresults()}{:else}No results found.{/if}
|
||||
</div>
|
||||
{:else}
|
||||
{#each items as item (item)}
|
||||
{#if children}{@render children({
|
||||
extractName,
|
||||
item,
|
||||
select,
|
||||
})}{:else}
|
||||
<MenuButton onclick={() => select(item)}>
|
||||
{extractName(item)}
|
||||
</MenuButton>
|
||||
{/if}
|
||||
{/each}
|
||||
{/if}
|
||||
</Menu>
|
||||
</div>
|
||||
@@ -0,0 +1,88 @@
|
||||
<script lang="ts">
|
||||
import { Spinner } from '../index.js'
|
||||
import { expoOut } from 'svelte/easing'
|
||||
import { type Toast, toasts, toastColors } from './toasts.js'
|
||||
import {
|
||||
InformationCircle,
|
||||
ExclamationTriangle,
|
||||
ExclamationCircle,
|
||||
CheckCircle,
|
||||
Icon,
|
||||
XMark,
|
||||
Check,
|
||||
} from 'svelte-hero-icons'
|
||||
import { fly } from 'svelte/transition'
|
||||
import Markdown from '$lib/app/markdown/Markdown.svelte'
|
||||
|
||||
interface Props {
|
||||
toast: Toast
|
||||
}
|
||||
|
||||
let { toast }: Props = $props()
|
||||
</script>
|
||||
|
||||
<div
|
||||
class={[
|
||||
toastColors[toast.type],
|
||||
'relative rounded-xl overflow-hidden flex flex-row items-center gap-2 px-3 py-3 border border-slate-200 dark:border-zinc-800',
|
||||
'bg-white dark:bg-zinc-925 shadow-lg',
|
||||
toast.long ? 'w-full max-w-lg' : 'w-80',
|
||||
]}
|
||||
transition:fly={{
|
||||
duration: 600,
|
||||
y: 8,
|
||||
opacity: 0,
|
||||
easing: expoOut,
|
||||
}}
|
||||
>
|
||||
{#if toast.loading}
|
||||
<div class="relative m-2 ml-4 shrink-0">
|
||||
<Spinner width={20} />
|
||||
</div>
|
||||
{:else}
|
||||
<Icon
|
||||
size="28"
|
||||
mini
|
||||
class={['relative self-center shrink-0 p-1 rounded-lg']}
|
||||
src={toast.type == 'info'
|
||||
? InformationCircle
|
||||
: toast.type == 'success'
|
||||
? CheckCircle
|
||||
: toast.type == 'warning'
|
||||
? ExclamationTriangle
|
||||
: toast.type == 'error'
|
||||
? ExclamationCircle
|
||||
: ExclamationCircle}
|
||||
/>
|
||||
{/if}
|
||||
<div class="flex flex-col break-words max-w-full text-inherit">
|
||||
{#if toast.title}
|
||||
<h1 class="text-base font-semibold">{toast.title}</h1>
|
||||
{/if}
|
||||
<Markdown
|
||||
source={toast.content}
|
||||
class={toast.long ? 'text-[15px]' : 'text-sm font-medium'}
|
||||
/>
|
||||
</div>
|
||||
<div class="absolute top-0 right-0 flex items-center gap-1 m-1">
|
||||
{#if toast.action}
|
||||
<button
|
||||
onclick={() => {
|
||||
toast.action?.()
|
||||
toasts.update((toasts) => toasts.filter((t) => t.id != toast.id))
|
||||
}}
|
||||
class="rounded-lg w-max transition-colors hover:bg-slate-100 dark:hover:bg-zinc-800 p-1 cursor-pointer"
|
||||
>
|
||||
<Icon src={Check} size="20" micro />
|
||||
</button>
|
||||
{/if}
|
||||
<button
|
||||
onclick={() => {
|
||||
toasts.update((toasts) => toasts.filter((t) => t.id != toast.id))
|
||||
}}
|
||||
class="rounded-lg w-max transition-colors hover:bg-slate-100 dark:hover:bg-zinc-800 p-1 cursor-pointer text-slate-600 dark:text-zinc-400"
|
||||
>
|
||||
<Icon src={XMark} size="16" micro />
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
@@ -0,0 +1,21 @@
|
||||
<script lang="ts">
|
||||
import Toast from './Toast.svelte'
|
||||
import { flip } from 'svelte/animate'
|
||||
import { toasts } from './toasts.js'
|
||||
import { expoOut } from 'svelte/easing'
|
||||
</script>
|
||||
|
||||
<div
|
||||
class="fixed right-0 bottom-0 flex flex-col items-end justify-end z-200 p-4
|
||||
group overflow-hidden h-screen min-w-96 pointer-events-none gap-4"
|
||||
>
|
||||
{#each $toasts as toast (toast.id)}
|
||||
<div
|
||||
animate:flip={{ duration: 300, easing: expoOut }}
|
||||
class="pointer-events-auto transition-all
|
||||
duration-300"
|
||||
>
|
||||
<Toast {toast} />
|
||||
</div>
|
||||
{/each}
|
||||
</div>
|
||||
@@ -0,0 +1,68 @@
|
||||
import { writable } from 'svelte/store'
|
||||
|
||||
type ToastType = keyof typeof toastColors
|
||||
|
||||
export const toastColors = {
|
||||
error: 'text-red-600 dark:text-red-400',
|
||||
warning: 'text-yellow-600 dark:text-yellow-400',
|
||||
success: 'text-green-600 dark:text-green-400',
|
||||
info: '',
|
||||
}
|
||||
|
||||
export interface Toast {
|
||||
id: number
|
||||
title?: string
|
||||
content: string
|
||||
type: ToastType
|
||||
loading?: boolean
|
||||
long?: boolean
|
||||
action?: () => void
|
||||
}
|
||||
|
||||
export const toasts = writable<Toast[]>([])
|
||||
|
||||
export function toast({
|
||||
title,
|
||||
content,
|
||||
type = 'info',
|
||||
duration = 5000,
|
||||
loading = false,
|
||||
long = false,
|
||||
action,
|
||||
}: {
|
||||
title?: string
|
||||
content: string
|
||||
type?: ToastType
|
||||
duration?: number
|
||||
loading?: boolean
|
||||
long?: boolean
|
||||
action?: () => void
|
||||
}) {
|
||||
let id = 0
|
||||
|
||||
toasts.update((toasts) => {
|
||||
id = Math.max(0, ...toasts.map((t) => t.id)) + 1
|
||||
|
||||
return [
|
||||
...toasts,
|
||||
{
|
||||
id: id,
|
||||
content: content,
|
||||
title: title,
|
||||
type: type,
|
||||
loading: loading,
|
||||
long: long,
|
||||
action: action,
|
||||
},
|
||||
]
|
||||
})
|
||||
|
||||
setTimeout(() => {
|
||||
toasts.update((toasts) => toasts.filter((toast) => toast.id != id))
|
||||
}, duration)
|
||||
|
||||
return id
|
||||
}
|
||||
|
||||
export const removeToast = (id: number) =>
|
||||
toasts.update((toasts) => toasts.filter((toast) => toast.id != id))
|
||||
@@ -0,0 +1,7 @@
|
||||
export const debounce = (fn: Function, ms = 300) => {
|
||||
let timeoutId: ReturnType<typeof setTimeout>
|
||||
return function (this: any, ...args: any[]) {
|
||||
clearTimeout(timeoutId)
|
||||
timeoutId = setTimeout(() => fn.apply(this, args), ms)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user