From 119c8a2e6c5ed73f329b898f16880b8192b21b0b Mon Sep 17 00:00:00 2001 From: Xanazf Date: Sun, 13 Oct 2024 00:04:38 +0300 Subject: [PATCH] added click outside sidebars handling, fixed some spacing issues --- .../navigation/sidebars/nav/index.tsx | 26 ++++++++++++-- .../navigation/sidebars/toc/index.tsx | 25 +++++++++++-- src/styles/docs/docs-types.css | 35 +++++++++++-------- src/styles/docs/docs.css | 16 ++++----- src/styles/docs/nav/nav.css | 28 +++++++++++---- src/styles/docs/toc/toc.css | 32 ++++++++++++----- src/styles/global.css | 22 +++++++----- 7 files changed, 132 insertions(+), 52 deletions(-) diff --git a/src/components/navigation/sidebars/nav/index.tsx b/src/components/navigation/sidebars/nav/index.tsx index b67840b..b57b987 100644 --- a/src/components/navigation/sidebars/nav/index.tsx +++ b/src/components/navigation/sidebars/nav/index.tsx @@ -1,4 +1,4 @@ -import { createSignal, type Component } from "solid-js"; +import { createSignal, onMount, type Component } from "solid-js"; import { LoadingSpinner, MenuToX, XToMenu } from "@icons"; import { Tree } from "./Tree"; @@ -7,6 +7,7 @@ import type { NavProps } from "../types"; const NavComponent: Component = props => { const [open, setOpen] = createSignal(false); const { tree, mobile, routes } = props; + let navRef: HTMLDivElement; if (!tree) { return ; @@ -28,8 +29,29 @@ const NavComponent: Component = props => { ); } + const handleClickOutside = (event: MouseEvent) => { + const isLink = "href" in (event.target || {}); + if ( + isLink || + (document.body.contains(event.target as Node) && + !navRef.contains(event.target as Node)) + ) { + setOpen(false); + } + }; + + onMount(() => { + window.addEventListener("click", handleClickOutside); + return () => { + window.removeEventListener("click", handleClickOutside); + }; + }); + return ( -