From 84bd503f74134625bab70d231b7b08f68e61eaea Mon Sep 17 00:00:00 2001 From: Emre Ayaz Date: Sat, 7 Dec 2024 12:47:53 +0300 Subject: [PATCH] Image Quick Fix - Dropdown Click outside --- fluid-ui-svelte/package.json | 2 +- .../src/lib/components/Dropdown.svelte | 22 +++++++-- fluid-ui-svelte/src/lib/index.ts | 4 ++ .../src/lib/primitives/Container.svelte | 14 +++--- .../src/lib/primitives/Image.svelte | 48 +++++++------------ fluid-ui-svelte/src/lib/types.ts | 1 + .../src/routes/images/+page.svelte | 8 ++-- 7 files changed, 55 insertions(+), 44 deletions(-) create mode 100644 fluid-ui-svelte/src/lib/types.ts diff --git a/fluid-ui-svelte/package.json b/fluid-ui-svelte/package.json index 4559be5..58d53c0 100644 --- a/fluid-ui-svelte/package.json +++ b/fluid-ui-svelte/package.json @@ -1,6 +1,6 @@ { "name": "fluid-ui-svelte", - "version": "0.1.6", + "version": "0.1.7", "author": { "name": "Emre Ayaz", "email": "emreayaz@frostium.io", diff --git a/fluid-ui-svelte/src/lib/components/Dropdown.svelte b/fluid-ui-svelte/src/lib/components/Dropdown.svelte index de515ce..167f9d5 100644 --- a/fluid-ui-svelte/src/lib/components/Dropdown.svelte +++ b/fluid-ui-svelte/src/lib/components/Dropdown.svelte @@ -2,37 +2,53 @@ import Container from "$lib/primitives/Container.svelte"; import Button from "$lib/primitives/Button.svelte"; import type { Snippet } from "svelte"; + import type { DropdownOptions } from "$lib/types.js"; let { class: className, triggerClass, contentClass, overrideDefaultStyling = false, isOpen = $bindable(false), + shouldCloseOnClickOutside = true, dropdownTrigger, dropdownContent, + triggerRawElement, + contentRawElement, }: { class?: string; triggerClass?: string; contentClass?: string; overrideDefaultStyling?: boolean; isOpen?: boolean; + shouldCloseOnClickOutside: boolean; dropdownTrigger: Snippet<[options: { isOpen: boolean; toggleDropdown: Function }]>; dropdownContent: Snippet<[options: { isOpen: boolean; toggleDropdown: Function }]>; + triggerRawElement: HTMLElement; + contentRawElement: HTMLElement; } = $props(); - const componentOptions = { + const componentOptions: DropdownOptions = { isOpen, toggleDropdown: () => { isOpen = !isOpen; }, }; + if (shouldCloseOnClickOutside) { + document.addEventListener("click", (e) => { + if (isOpen && e.target) { + if (!triggerRawElement.contains(e.target as HTMLElement) && !contentRawElement.contains(e.target as HTMLElement)) { + isOpen = false; + } + } + }); + } - + {@render dropdownTrigger(componentOptions)} - + {#if isOpen} {@render dropdownContent(componentOptions)} {/if} diff --git a/fluid-ui-svelte/src/lib/index.ts b/fluid-ui-svelte/src/lib/index.ts index a957f7c..1a84f4c 100644 --- a/fluid-ui-svelte/src/lib/index.ts +++ b/fluid-ui-svelte/src/lib/index.ts @@ -1,3 +1,7 @@ +//Types + +export type { DropdownOptions as DropdownOptions } from "$lib/types.ts"; + // Primitives export { default as Link } from "$lib/primitives/Link.svelte"; export { default as Button } from "$lib/primitives/Button.svelte"; diff --git a/fluid-ui-svelte/src/lib/primitives/Container.svelte b/fluid-ui-svelte/src/lib/primitives/Container.svelte index 4519567..219d447 100644 --- a/fluid-ui-svelte/src/lib/primitives/Container.svelte +++ b/fluid-ui-svelte/src/lib/primitives/Container.svelte @@ -2,46 +2,48 @@ import type { Snippet } from "svelte"; import type { HTMLAttributes } from "svelte/elements"; - const { + let { containerType = "div", class: className = "", overrideDefaultStyling = false, children, + rawElement = $bindable(), ...restProps }: { containerType?: "div" | "section" | "aside" | "nav" | "footer"; class?: string; overrideDefaultStyling?: boolean; children?: Snippet; + rawElement?: HTMLElement; } & HTMLAttributes = $props(); {#if containerType == "div"} -
+
{#if children} {@render children()} {/if}
{:else if containerType == "section"} -
+
{#if children} {@render children()} {/if}
{:else if containerType == "aside"} -