From 557fb4f8dc95f43ce3593a3720a7516cb0687733 Mon Sep 17 00:00:00 2001 From: MapleLeaf <19603573+itsMapleLeaf@users.noreply.github.com> Date: Mon, 3 Jan 2022 04:06:55 -0600 Subject: [PATCH] use simple script for popover menu --- .../src/components/main-navigation.tsx | 25 +++++--- .../src/components/popover-menu.client.tsx | 38 ++++++++++++ .../docs-new/src/components/popover-menu.tsx | 61 +++---------------- .../src/helpers/serve-compiled-script.ts | 26 ++++++++ packages/docs-new/src/html.tsx | 2 + packages/docs-new/src/main.tsx | 8 +++ 6 files changed, 102 insertions(+), 58 deletions(-) create mode 100644 packages/docs-new/src/components/popover-menu.client.tsx create mode 100644 packages/docs-new/src/helpers/serve-compiled-script.ts diff --git a/packages/docs-new/src/components/main-navigation.tsx b/packages/docs-new/src/components/main-navigation.tsx index 7b6001d..717635c 100644 --- a/packages/docs-new/src/components/main-navigation.tsx +++ b/packages/docs-new/src/components/main-navigation.tsx @@ -1,14 +1,9 @@ import React from "react" import { guideLinks } from "../data/guide-links" import { mainLinks } from "../data/main-links" -import { createHydrater } from "../helpers/hydration" import { linkClass } from "../styles/components" import { AppLink } from "./app-link" -import type { MainNavigationMobileMenuData } from "./main-navigation-mobile-menu" - -const MenuHydrater = await createHydrater( - new URL("./main-navigation-mobile-menu.tsx", import.meta.url).pathname, -) +import { PopoverMenu } from "./popover-menu" export function MainNavigation() { return ( @@ -22,7 +17,23 @@ export function MainNavigation() { ))} ) diff --git a/packages/docs-new/src/components/popover-menu.client.tsx b/packages/docs-new/src/components/popover-menu.client.tsx new file mode 100644 index 0000000..813d1d9 --- /dev/null +++ b/packages/docs-new/src/components/popover-menu.client.tsx @@ -0,0 +1,38 @@ +import clsx from "clsx" + +const menus = document.querySelectorAll("[data-popover]") + +for (const menu of menus) { + const button = menu.querySelector("[data-popover-button]")! + + const panel = menu.querySelector("[data-popover-panel]")! + const panelClasses = clsx`${panel.className} transition-all` + + const visibleClass = clsx`${panelClasses} visible opacity-100 translate-y-0` + const hiddenClass = clsx`${panelClasses} invisible opacity-0 translate-y-2` + + let visible = false + + const setVisible = (newVisible: boolean) => { + visible = newVisible + panel.className = visible ? visibleClass : hiddenClass + + if (!visible) return + + requestAnimationFrame(() => { + const handleClose = (event: MouseEvent) => { + if (panel.contains(event.target as Node)) return + setVisible(false) + window.removeEventListener("click", handleClose) + } + window.addEventListener("click", handleClose) + }) + } + + const toggleVisible = () => setVisible(!visible) + + button.addEventListener("click", toggleVisible) + + setVisible(false) + panel.hidden = false +} diff --git a/packages/docs-new/src/components/popover-menu.tsx b/packages/docs-new/src/components/popover-menu.tsx index 028ac47..49c46ee 100644 --- a/packages/docs-new/src/components/popover-menu.tsx +++ b/packages/docs-new/src/components/popover-menu.tsx @@ -1,63 +1,22 @@ import { MenuAlt4Icon } from "@heroicons/react/outline" -import { useRect } from "@reach/rect" import clsx from "clsx" -import React, { useRef, useState } from "react" -import { FocusOn } from "react-focus-on" +import React from "react" import { linkClass } from "../styles/components" -// todo: remove useRect usage and rely on css absolute positioning instead export function PopoverMenu({ children }: { children: React.ReactNode }) { - const [visible, setVisible] = useState(false) - - const buttonRef = useRef(null) - const buttonRect = useRect(buttonRef) - - const panelRef = useRef(null) - const panelRect = useRect(panelRef) - - /* eslint-disable jsx-a11y/no-static-element-interactions */ - /* eslint-disable jsx-a11y/click-events-have-key-events */ return ( - <> - - - setVisible(false)} - onEscapeKey={() => setVisible(false)} + + ) } diff --git a/packages/docs-new/src/helpers/serve-compiled-script.ts b/packages/docs-new/src/helpers/serve-compiled-script.ts new file mode 100644 index 0000000..0f590ba --- /dev/null +++ b/packages/docs-new/src/helpers/serve-compiled-script.ts @@ -0,0 +1,26 @@ +import { build } from "esbuild" +import type { RequestHandler } from "express" +import { readFile } from "node:fs/promises" +import { dirname } from "node:path" + +export async function serveCompiledScript( + scriptFilePath: string, +): Promise { + const scriptBuild = await build({ + bundle: true, + stdin: { + contents: await readFile(scriptFilePath, "utf-8"), + sourcefile: scriptFilePath, + loader: "tsx", + resolveDir: dirname(scriptFilePath), + }, + target: ["chrome89", "firefox89"], + format: "esm", + write: false, + }) + + return (req, res) => { + res.setHeader("Content-Type", "application/javascript") + res.end(scriptBuild.outputFiles[0]!.contents) + } +} diff --git a/packages/docs-new/src/html.tsx b/packages/docs-new/src/html.tsx index 00658c0..e74e963 100644 --- a/packages/docs-new/src/html.tsx +++ b/packages/docs-new/src/html.tsx @@ -31,6 +31,8 @@ export function Html({ +