Skip to content

Commit 351a7c8

Browse files
authored
Merge pull request #10 from Asraye/fix/guild-sidebar-close
fix: close dropdown when clicking outside
2 parents ffb0869 + 1758a43 commit 351a7c8

1 file changed

Lines changed: 26 additions & 3 deletions

File tree

src/components/guilds/GuildSidebar.tsx

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {A, useNavigate, useParams} from "@solidjs/router";
2-
import {createMemo, createSignal, For, Match, Show, Switch} from "solid-js";
2+
import {createMemo, createSignal, For, Match, Show, Switch, onMount, onCleanup} from "solid-js";
33
import {getApi} from "../../api/Api";
44
import {GuildChannel} from "../../types/channel";
55
import {ModalId, useModal} from "../ui/Modal";
@@ -182,6 +182,24 @@ export default function GuildSidebar() {
182182
const [dropdownExpanded, setDropdownExpanded] = createSignal(false)
183183
const isOwner = createMemo(() => guild().owner_id === api.cache?.clientUser?.id)
184184

185+
let dropdownRef: HTMLUListElement | undefined
186+
let toggleRef: HTMLDivElement | undefined
187+
188+
onMount(() => {
189+
const handleClick = (e: MouseEvent) => {
190+
if (!dropdownExpanded()) return
191+
if (
192+
!dropdownRef?.contains(e.target as Node) &&
193+
!toggleRef?.contains(e.target as Node)
194+
) {
195+
setDropdownExpanded(false)
196+
}
197+
}
198+
199+
document.addEventListener("click", handleClick)
200+
onCleanup(() => document.removeEventListener("click", handleClick))
201+
})
202+
185203
const BaseContextMenu = () => (
186204
<Show when={guildPermissions()?.has('CREATE_INVITES')}>
187205
<ContextMenuButton
@@ -211,7 +229,6 @@ export default function GuildSidebar() {
211229
groups.forEach(group => group.children.sort((a, b) => {
212230
const aIsCategory = a.type === 'category'
213231
const bIsCategory = b.type === 'category'
214-
215232
if (aIsCategory && !bIsCategory) return 1
216233
if (!aIsCategory && bIsCategory) return -1
217234
return a.position - b.position
@@ -229,6 +246,7 @@ export default function GuildSidebar() {
229246
<RenderCategory id={props.channel.id} group={channels()?.get(props.channel.id)!} />
230247
</Show>
231248
)
249+
232250
const RenderCategory = (props: {
233251
id: bigint, group: { category: GuildChannel, children: GuildChannel[] }
234252
}) => {
@@ -310,6 +328,7 @@ export default function GuildSidebar() {
310328
)}
311329
>
312330
<div
331+
ref={toggleRef}
313332
class="box-border flex flex-col justify-end border-b-[1px] border-fg/5
314333
group hover:bg-2 transition-all duration-200 cursor-pointer relative w-full"
315334
classList={{ 'min-h-[150px]': !!guild().banner }}
@@ -351,7 +370,11 @@ export default function GuildSidebar() {
351370
</div>
352371
)}
353372
<Show when={dropdownExpanded()}>
354-
<ul tabIndex={0} class="flex flex-col py-1 absolute rounded-b-xl bg-bg-3/60 backdrop-blur inset-x-0 top-full z-[200]">
373+
<ul
374+
ref={dropdownRef}
375+
tabIndex={0}
376+
class="flex flex-col py-1 absolute rounded-b-xl bg-bg-3/60 backdrop-blur inset-x-0 top-full z-[200]"
377+
>
355378
<Show when={guildPermissions()?.has('CREATE_INVITES')}>
356379
<GuildDropdownButton
357380
icon={UserPlus}

0 commit comments

Comments
 (0)