Compare commits
8 Commits
46bd6b935a
...
v0.3.5
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d3c4962495 | ||
|
|
3cdda64686 | ||
|
|
6630051d67 | ||
|
|
bf77a20ff9 | ||
|
|
42c9bc0bcc | ||
|
|
ed48b404d4 | ||
|
|
54f7924c4a | ||
|
|
d9cd2b406a |
@@ -4,7 +4,7 @@
|
|||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "https://gitea.auvem.com/svelte-toolkit/sui.git"
|
"url": "https://gitea.auvem.com/svelte-toolkit/sui.git"
|
||||||
},
|
},
|
||||||
"version": "0.3.2",
|
"version": "0.3.5",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "vite dev",
|
"dev": "vite dev",
|
||||||
"build": "vite build && pnpm run prepack",
|
"build": "vite build && pnpm run prepack",
|
||||||
|
|||||||
@@ -38,6 +38,8 @@
|
|||||||
})
|
})
|
||||||
| null;
|
| null;
|
||||||
dismiss?: Omit<BannerControlButton, 'framed'> | null;
|
dismiss?: Omit<BannerControlButton, 'framed'> | null;
|
||||||
|
/** if true, accept and decline buttons are swapped with more info */
|
||||||
|
swap?: boolean | null;
|
||||||
};
|
};
|
||||||
|
|
||||||
const defaultBannerControls: BannerControls = {
|
const defaultBannerControls: BannerControls = {
|
||||||
@@ -53,21 +55,8 @@
|
|||||||
},
|
},
|
||||||
dismiss: {
|
dismiss: {
|
||||||
action: (banner) => banner.close()
|
action: (banner) => banner.close()
|
||||||
}
|
},
|
||||||
};
|
swap: false
|
||||||
|
|
||||||
const mergeBannerControls = (
|
|
||||||
defaults: BannerControls,
|
|
||||||
overrides: BannerControls | null | undefined
|
|
||||||
): BannerControls => {
|
|
||||||
if (!overrides) return defaults;
|
|
||||||
|
|
||||||
return {
|
|
||||||
accept: overrides.accept ?? defaults.accept,
|
|
||||||
decline: overrides.decline ?? defaults.decline,
|
|
||||||
moreInfo: overrides.moreInfo ?? defaults.moreInfo,
|
|
||||||
dismiss: overrides.dismiss ?? defaults.dismiss
|
|
||||||
};
|
|
||||||
};
|
};
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
@@ -78,6 +67,7 @@
|
|||||||
import Button from './Button.svelte';
|
import Button from './Button.svelte';
|
||||||
import FramelessButton from './FramelessButton.svelte';
|
import FramelessButton from './FramelessButton.svelte';
|
||||||
import { X } from 'phosphor-svelte';
|
import { X } from 'phosphor-svelte';
|
||||||
|
import { mergeOverrideObject } from './util';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Banner provides a simple component to display a banner message with
|
* Banner provides a simple component to display a banner message with
|
||||||
@@ -114,7 +104,7 @@
|
|||||||
children
|
children
|
||||||
}: Props = $props();
|
}: Props = $props();
|
||||||
|
|
||||||
const controls = $derived(mergeBannerControls(defaultBannerControls, rawControls));
|
const controls = $derived(mergeOverrideObject(defaultBannerControls, rawControls));
|
||||||
|
|
||||||
const api: BannerAPI = {
|
const api: BannerAPI = {
|
||||||
open: () => (open = true),
|
open: () => (open = true),
|
||||||
@@ -157,6 +147,7 @@
|
|||||||
<FramelessButton
|
<FramelessButton
|
||||||
inverted={invertFrameless}
|
inverted={invertFrameless}
|
||||||
onclick={() => controls.dismiss?.action?.(api)}
|
onclick={() => controls.dismiss?.action?.(api)}
|
||||||
|
class="ml-auto"
|
||||||
>
|
>
|
||||||
{#if controls.dismiss?.label}
|
{#if controls.dismiss?.label}
|
||||||
{controls.dismiss.label}
|
{controls.dismiss.label}
|
||||||
@@ -170,7 +161,9 @@
|
|||||||
{@render children?.()}
|
{@render children?.()}
|
||||||
|
|
||||||
{#if controls !== null}
|
{#if controls !== null}
|
||||||
<div class="mt-4 flex flex-wrap justify-between gap-4">
|
<div
|
||||||
|
class={['mt-4 flex flex-wrap justify-between gap-4', controls.swap && 'flex-row-reverse']}
|
||||||
|
>
|
||||||
<!-- More info button/link -->
|
<!-- More info button/link -->
|
||||||
{#if controls.moreInfo}
|
{#if controls.moreInfo}
|
||||||
{#if controls.moreInfo.type === 'link' && controls.moreInfo.href}
|
{#if controls.moreInfo.type === 'link' && controls.moreInfo.href}
|
||||||
@@ -199,7 +192,9 @@
|
|||||||
{/if}
|
{/if}
|
||||||
{/if}
|
{/if}
|
||||||
|
|
||||||
<div class="flex justify-end gap-4">
|
<div
|
||||||
|
class={['flex justify-end gap-4', controls.swap ? 'mr-auto flex-row-reverse' : 'ml-auto']}
|
||||||
|
>
|
||||||
<!-- Decline button -->
|
<!-- Decline button -->
|
||||||
{@render buttonControl(controls.decline, handleDecline, false)}
|
{@render buttonControl(controls.decline, handleDecline, false)}
|
||||||
<!-- Accept button -->
|
<!-- Accept button -->
|
||||||
|
|||||||
@@ -41,24 +41,29 @@
|
|||||||
title: (title: string) => void;
|
title: (title: string) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type DialogControlButton = {
|
||||||
|
label?: string;
|
||||||
|
class?: ClassValue;
|
||||||
|
action?: (dialog: DialogAPI) => void;
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Configures the default dialog controls.
|
* Configures the default dialog controls.
|
||||||
*/
|
*/
|
||||||
export type DialogControlOpts = {
|
export type DialogControls = {
|
||||||
cancel?: {
|
cancel?: DialogControlButton | null;
|
||||||
label?: string;
|
ok?: DialogControlButton | null;
|
||||||
class?: ClassValue;
|
close?: Omit<DialogControlButton, 'label'> | null;
|
||||||
action?: (dialog: DialogAPI) => void;
|
|
||||||
};
|
|
||||||
ok?: {
|
|
||||||
label?: string;
|
|
||||||
class?: ClassValue;
|
|
||||||
action?: (dialog: DialogAPI) => void;
|
|
||||||
};
|
|
||||||
close?: {
|
|
||||||
class?: ClassValue;
|
|
||||||
action?: (dialog: DialogAPI) => void;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const defaultDialogControls: DialogControls = {
|
||||||
|
cancel: {
|
||||||
|
label: 'Cancel'
|
||||||
|
},
|
||||||
|
ok: {
|
||||||
|
label: 'OK'
|
||||||
|
},
|
||||||
|
close: {}
|
||||||
};
|
};
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
@@ -72,6 +77,7 @@
|
|||||||
import { X } from 'phosphor-svelte';
|
import { X } from 'phosphor-svelte';
|
||||||
import { ErrorMessage, type RawError } from './error';
|
import { ErrorMessage, type RawError } from './error';
|
||||||
import ErrorBox from './ErrorBox.svelte';
|
import ErrorBox from './ErrorBox.svelte';
|
||||||
|
import { mergeOverrideObject } from './util';
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
open?: boolean;
|
open?: boolean;
|
||||||
@@ -80,7 +86,7 @@
|
|||||||
size?: 'sm' | 'md' | 'lg' | 'max';
|
size?: 'sm' | 'md' | 'lg' | 'max';
|
||||||
class?: ClassValue;
|
class?: ClassValue;
|
||||||
children?: Snippet;
|
children?: Snippet;
|
||||||
controls?: Snippet | DialogControlOpts;
|
controls?: Snippet | DialogControls;
|
||||||
onopen?: (dialog: DialogAPI) => void;
|
onopen?: (dialog: DialogAPI) => void;
|
||||||
onclose?: (dialog: DialogAPI) => void;
|
onclose?: (dialog: DialogAPI) => void;
|
||||||
loading?: boolean;
|
loading?: boolean;
|
||||||
@@ -95,7 +101,7 @@
|
|||||||
size = 'sm',
|
size = 'sm',
|
||||||
class: classValue,
|
class: classValue,
|
||||||
children,
|
children,
|
||||||
controls,
|
controls: rawControls = defaultDialogControls,
|
||||||
onopen,
|
onopen,
|
||||||
onclose,
|
onclose,
|
||||||
loading = $bindable(false),
|
loading = $bindable(false),
|
||||||
@@ -103,6 +109,12 @@
|
|||||||
disabled = $bindable(false)
|
disabled = $bindable(false)
|
||||||
}: Props = $props();
|
}: Props = $props();
|
||||||
|
|
||||||
|
let controls = $derived(
|
||||||
|
typeof rawControls === 'function'
|
||||||
|
? rawControls
|
||||||
|
: mergeOverrideObject(defaultDialogControls, rawControls)
|
||||||
|
);
|
||||||
|
|
||||||
let dialogContainer = $state<HTMLDivElement | null>(null);
|
let dialogContainer = $state<HTMLDivElement | null>(null);
|
||||||
let error = $state<ErrorMessage | null>(null);
|
let error = $state<ErrorMessage | null>(null);
|
||||||
|
|
||||||
@@ -192,8 +204,10 @@
|
|||||||
|
|
||||||
{#if children}{@render children()}{:else}Dialog is empty{/if}
|
{#if children}{@render children()}{:else}Dialog is empty{/if}
|
||||||
|
|
||||||
|
<!-- Dialog Controls -->
|
||||||
<div class="mt-6 flex justify-end gap-4">
|
<div class="mt-6 flex justify-end gap-4">
|
||||||
{#if controls && typeof controls === 'function'}{@render controls()}{:else}
|
{#if controls && typeof controls === 'function'}{@render controls()}{:else}
|
||||||
|
{#if controls.cancel !== null}
|
||||||
<Button
|
<Button
|
||||||
class={controls?.cancel?.class}
|
class={controls?.cancel?.class}
|
||||||
onclick={() => {
|
onclick={() => {
|
||||||
@@ -207,6 +221,8 @@
|
|||||||
>
|
>
|
||||||
{controls?.cancel?.label || 'Cancel'}
|
{controls?.cancel?.label || 'Cancel'}
|
||||||
</Button>
|
</Button>
|
||||||
|
{/if}
|
||||||
|
{#if controls.ok !== null}
|
||||||
<Button
|
<Button
|
||||||
class={controls?.ok?.class}
|
class={controls?.ok?.class}
|
||||||
onclick={() => {
|
onclick={() => {
|
||||||
@@ -222,19 +238,28 @@
|
|||||||
{controls?.ok?.label || 'OK'}
|
{controls?.ok?.label || 'OK'}
|
||||||
</Button>
|
</Button>
|
||||||
{/if}
|
{/if}
|
||||||
|
{/if}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<!-- Close Button -->
|
||||||
|
{#if typeof controls === 'function' || controls?.close !== null}
|
||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
aria-label="close"
|
aria-label="close"
|
||||||
class="absolute top-4 right-4 inline-flex cursor-pointer items-center
|
class="absolute top-4 right-4 inline-flex cursor-pointer items-center
|
||||||
justify-center disabled:cursor-not-allowed disabled:opacity-50"
|
justify-center disabled:cursor-not-allowed disabled:opacity-50"
|
||||||
onclick={() => {
|
onclick={() => {
|
||||||
if (!frozen) open = false;
|
if (typeof controls !== 'function' && controls?.close?.action) {
|
||||||
|
controls?.close?.action?.(dialogAPI);
|
||||||
|
} else if (!frozen) {
|
||||||
|
open = false;
|
||||||
|
}
|
||||||
}}
|
}}
|
||||||
disabled={frozen}
|
disabled={frozen}
|
||||||
>
|
>
|
||||||
<X size="1.5em" weight="bold" />
|
<X size="1.5em" weight="bold" />
|
||||||
</button>
|
</button>
|
||||||
|
{/if}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
{/snippet}
|
{/snippet}
|
||||||
|
|||||||
@@ -143,3 +143,55 @@ export const trimEdges = (str: string, char: string, trimStart?: boolean, trimEn
|
|||||||
|
|
||||||
return str.substring(start, end);
|
return str.substring(start, end);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// helper: only treat plain objects as mergeable
|
||||||
|
const isPlainObject = (v: unknown): v is Record<string, unknown> =>
|
||||||
|
typeof v === 'object' &&
|
||||||
|
v !== null &&
|
||||||
|
!Array.isArray(v) &&
|
||||||
|
Object.getPrototypeOf(v) === Object.prototype;
|
||||||
|
|
||||||
|
/** Merge two plain object maps. No `any` used. */
|
||||||
|
function mergePlainObjects(
|
||||||
|
baseObj: Record<string, unknown>,
|
||||||
|
overrideObj: Record<string, unknown>
|
||||||
|
): Record<string, unknown> {
|
||||||
|
const res: Record<string, unknown> = { ...baseObj };
|
||||||
|
|
||||||
|
for (const k of Object.keys(overrideObj)) {
|
||||||
|
const v = overrideObj[k];
|
||||||
|
if (v === undefined) continue; // undefined preserves base
|
||||||
|
const b = res[k];
|
||||||
|
|
||||||
|
if (isPlainObject(v) && isPlainObject(b)) {
|
||||||
|
res[k] = mergePlainObjects(b as Record<string, unknown>, v as Record<string, unknown>);
|
||||||
|
} else {
|
||||||
|
// primitives, null, arrays, non-plain objects replace base
|
||||||
|
res[k] = v;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Merge `base` with `override`.
|
||||||
|
* - `null` in `override` replaces (kept as valid override)
|
||||||
|
* - `undefined` in `override` is ignored (keeps base)
|
||||||
|
* - Only plain objects are deep-merged
|
||||||
|
* - If `override` is null/undefined we return a shallow copy of `base`
|
||||||
|
*/
|
||||||
|
export const mergeOverrideObject = <T extends Record<string, unknown>>(
|
||||||
|
base: T,
|
||||||
|
override?: Partial<T> | null
|
||||||
|
): T => {
|
||||||
|
if (override == null) return { ...base } as T;
|
||||||
|
|
||||||
|
// Use plain maps internally to avoid explicit any
|
||||||
|
const baseMap = { ...base } as Record<string, unknown>;
|
||||||
|
const overrideMap = override as Record<string, unknown>;
|
||||||
|
|
||||||
|
const merged = mergePlainObjects(baseMap, overrideMap);
|
||||||
|
|
||||||
|
return merged as T;
|
||||||
|
};
|
||||||
|
|||||||
@@ -83,7 +83,9 @@
|
|||||||
<Banner
|
<Banner
|
||||||
title="Manage Cookies"
|
title="Manage Cookies"
|
||||||
controls={{
|
controls={{
|
||||||
moreInfo: { label: 'More Info', type: 'link', href: '#!' }
|
moreInfo: { label: 'More Info', type: 'link', href: '#!' },
|
||||||
|
dismiss: null,
|
||||||
|
swap: true
|
||||||
}}
|
}}
|
||||||
onaccept={() => console.log('Cookies accepted!')}
|
onaccept={() => console.log('Cookies accepted!')}
|
||||||
ondecline={() => console.log('Cookies declined!')}
|
ondecline={() => console.log('Cookies declined!')}
|
||||||
@@ -521,7 +523,8 @@
|
|||||||
dialog.close();
|
dialog.close();
|
||||||
alert('Dialog submitted!');
|
alert('Dialog submitted!');
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
|
cancel: null
|
||||||
}}
|
}}
|
||||||
onopen={(dialog) => {
|
onopen={(dialog) => {
|
||||||
dialog.error('Example error message!');
|
dialog.error('Example error message!');
|
||||||
|
|||||||
Reference in New Issue
Block a user