feat: topbar and drawer for mobile layout

This commit is contained in:
zsavko
2023-04-09 22:18:43 +08:00
committed by zsakvo
parent 8176ff684d
commit a463c1a64a
3 changed files with 236 additions and 75 deletions
+140 -75
View File
@@ -1,6 +1,6 @@
import { cn } from "~/lib/utils"
import { useRouter } from "next/router"
import React, { useEffect } from "react"
import React, { useEffect, useState } from "react"
import { DISCORD_LINK, APP_NAME } from "~/lib/env"
import { getSiteLink } from "~/lib/helpers"
@@ -22,6 +22,8 @@ import { useTranslation } from "react-i18next"
import { Logo } from "~/components/common/Logo"
import { getStorage } from "~/lib/storage"
import { useGetPagesBySite } from "~/queries/page"
import { DashboardTopbar } from "./DashboardTopbar"
import { useMobileLayout } from "~/hooks/useMobileLayout"
export function DashboardLayout({
children,
@@ -45,6 +47,8 @@ export function DashboardLayout({
const [hasPermission, setHasPermission] = React.useState(false)
const { t } = useTranslation("dashboard")
const isMobileLayout = useMobileLayout()
useEffect(() => {
if (ssrReady) {
if (!isConnected) {
@@ -169,10 +173,20 @@ export function DashboardLayout({
/>
)}
<div className="flex h-screen">
<DashboardSidebar>
{(isOpen) => (
<>
<div className="flex-1 min-h-0 flex flex-col">
{isMobileLayout ? (
<DashboardTopbar
userWidget={
<div className="mb-2 px-2 pt-3 pb-2">
<ConnectButton
left={true}
size="base"
hideNotification={true}
hideName={false}
/>
</div>
}
drawerWidget={(close: any) => (
<>
<div className="mb-2 px-5 pt-3 pb-2 text-2xl font-extrabold flex items-center">
<div className="inline-block w-9 h-9 mr-3">
<Logo
@@ -182,39 +196,8 @@ export function DashboardLayout({
autoplay={false}
/>
</div>
{isOpen && "xLog"}
{"xLog"}
</div>
{userSite.data?.[0]?.username &&
subdomain &&
userSite.data[0].username !== subdomain && (
<div className="mb-2 px-5 pt-3 pb-2 bg-orange-50 text-center">
<div className="mb-2">
{isOpen && "You are operating"}
</div>
<Avatar
images={site.data?.avatars || []}
size={isOpen ? 60 : 40}
name={site.data?.name}
/>
{isOpen && (
<span className="flex flex-col justify-center">
<span className="block">{site.data?.name}</span>
<span className="block text-sm text-zinc-400">
@{site.data?.username}
</span>
</span>
)}
</div>
)}
<div className="mb-2 px-2 pt-3 pb-2">
<ConnectButton
left={true}
size="base"
hideNotification={true}
hideName={!isOpen}
/>
</div>
<div className="px-3 space-y-[2px] text-zinc-500 flex-1 min-h-0 overflow-y-auto">
{links.map((link) => {
const active =
@@ -224,47 +207,129 @@ export function DashboardLayout({
href: link.href,
})
return (
<UniLink
href={link.href}
key={link.text}
className={cn(
`flex px-4 h-12 items-center rounded-md space-x-2 w-full transition-colors`,
active
? `bg-slate-200 font-medium text-accent`
: `hover:bg-slate-200 hover:bg-opacity-50`,
!isOpen && "justify-center",
)}
onClick={link.onClick}
>
<span className={cn(link.icon, "text-xl")}></span>
{isOpen && <span>{t(link.text)}</span>}
</UniLink>
<div key={link.text} onClick={() => close()}>
<UniLink
href={link.href}
className={cn(
`flex px-4 h-12 items-center rounded-md space-x-2 w-full transition-colors`,
active
? `bg-slate-200 font-medium text-accent`
: `hover:bg-slate-200 hover:bg-opacity-50`,
!true && "justify-center",
)}
onClick={link.onClick}
>
<span className={cn(link.icon, "text-xl")}></span>
<span>{t(link.text)}</span>
</UniLink>
</div>
)
})}
</div>
</div>
<div className="flex items-center px-4 flex-col pb-4">
<UniLink
href={DISCORD_LINK}
className="space-x-1 text-zinc-500 hover:text-zinc-800 flex w-full h-12 items-center justify-center transition-colors mb-2"
>
<i className="i-mingcute:question-line text-lg" />
{isOpen && <span>{t("Need help?")}</span>}
</UniLink>
<UniLink
href={getSiteLink({
subdomain,
})}
className="space-x-2 border rounded-lg bg-slate-100 border-slate-200 text-accent hover:scale-105 transition-transform flex w-full h-12 items-center justify-center"
>
<span className="i-mingcute:home-1-line"></span>
{isOpen && <span>{t("View Site")}</span>}
</UniLink>
</div>
</>
)}
</DashboardSidebar>
{children}
</>
)}
>
{(isOpen) => <div>233</div>}
</DashboardTopbar>
) : (
<DashboardSidebar>
{(isOpen) => (
<>
<div className="flex-1 min-h-0 flex flex-col">
<div className="mb-2 px-5 pt-3 pb-2 text-2xl font-extrabold flex items-center">
<div className="inline-block w-9 h-9 mr-3">
<Logo
type="lottie"
width={36}
height={36}
autoplay={false}
/>
</div>
{isOpen && "xLog"}
</div>
{userSite.data?.[0]?.username &&
subdomain &&
userSite.data[0].username !== subdomain && (
<div className="mb-2 px-5 pt-3 pb-2 bg-orange-50 text-center">
<div className="mb-2">
{isOpen && "You are operating"}
</div>
<Avatar
images={site.data?.avatars || []}
size={isOpen ? 60 : 40}
name={site.data?.name}
/>
{isOpen && (
<span className="flex flex-col justify-center">
<span className="block">{site.data?.name}</span>
<span className="block text-sm text-zinc-400">
@{site.data?.username}
</span>
</span>
)}
</div>
)}
<div className="mb-2 px-2 pt-3 pb-2">
<ConnectButton
left={true}
size="base"
hideNotification={true}
hideName={!isOpen}
/>
</div>
<div className="px-3 space-y-[2px] text-zinc-500 flex-1 min-h-0 overflow-y-auto">
{links.map((link) => {
const active =
link.href &&
link.isActive({
pathname: router.asPath,
href: link.href,
})
return (
<UniLink
href={link.href}
key={link.text}
className={cn(
`flex px-4 h-12 items-center rounded-md space-x-2 w-full transition-colors`,
active
? `bg-slate-200 font-medium text-accent`
: `hover:bg-slate-200 hover:bg-opacity-50`,
!isOpen && "justify-center",
)}
onClick={link.onClick}
>
<span className={cn(link.icon, "text-xl")}></span>
{isOpen && <span>{t(link.text)}</span>}
</UniLink>
)
})}
</div>
</div>
<div className="flex items-center px-4 flex-col pb-4">
<UniLink
href={DISCORD_LINK}
className="space-x-1 text-zinc-500 hover:text-zinc-800 flex w-full h-12 items-center justify-center transition-colors mb-2"
>
<i className="i-mingcute:question-line text-lg" />
{isOpen && <span>{t("Need help?")}</span>}
</UniLink>
<UniLink
href={getSiteLink({
subdomain,
})}
className="space-x-2 border rounded-lg bg-slate-100 border-slate-200 text-accent hover:scale-105 transition-transform flex w-full h-12 items-center justify-center"
>
<span className="i-mingcute:home-1-line"></span>
{isOpen && <span>{t("View Site")}</span>}
</UniLink>
</div>
</>
)}
</DashboardSidebar>
)}
<div className={`${isMobileLayout ? "pt-16" : ""}`}>{children}</div>
</div>
</>
) : (
@@ -0,0 +1,72 @@
import { useState, useEffect, useMemo, Fragment } from "react"
import { setStorage, getStorage } from "~/lib/storage"
import { Bars3Icon, ChevronDownIcon } from "@heroicons/react/24/outline"
import { Logo } from "../common/Logo"
import { Popover, Transition } from "@headlessui/react"
export const DashboardTopbar: React.FC<{
children: (open: boolean) => React.ReactNode
userWidget: React.ReactNode
drawerWidget: (close: any) => React.ReactNode
}> = ({ children, userWidget, drawerWidget }) => {
const [isOpen, setIsOpen] = useState(false)
return (
<div
className={`${`w-full top-0 h-16`} bg-slate-50 z-10 transition-all flex flex-row fixed px-5 md:px-10 items-center`}
>
<Popover className="relative">
{({ open, close }) => (
<>
<Transition
as={Fragment}
enter="transition ease-out duration-200"
enterFrom="opacity-0"
enterTo="opacity-100"
leave="transition ease-in duration-150"
leaveFrom="opacity-100"
leaveTo="opacity-0"
>
<Popover.Overlay className="fixed inset-0 bg-black opacity-10" />
</Transition>
<Popover.Button
className={`
group inline-flex items-center rounded-md text-base font-medium hover:text-opacity-100 focus:outline-none focus-visible:ring-2 focus-visible:ring-white focus-visible:ring-opacity-75`}
onClick={() => {
setIsOpen(true)
}}
>
<Bars3Icon className="h-6 w-6" />
</Popover.Button>
<Transition
as={Fragment}
enter="transition ease-out duration-200"
enterFrom="opacity-0 transform -translate-x-full"
enterTo="opacity-100 transform translate-x-0"
leave="transition ease-in duration-150"
leaveFrom="opacity-100 transform translate-x-0"
leaveTo="opacity-0 transform -translate-x-full"
>
<Popover.Panel className="fixed bg-slate-50 h-screen left-0 top-0 z-10 w-sidebar transform sm:px-0 lg:max-w-3xl">
<div className="overflow-hidden rounded-lg shadow-lg ring-1 ring-black ring-opacity-5 h-full">
{drawerWidget(close)}
</div>
</Popover.Panel>
</Transition>
</>
)}
</Popover>
<div className="mb-2 px-5 pt-3 pb-2 text-2xl font-extrabold flex items-center">
<div className="inline-block w-9 h-9 mr-3">
<Logo type="lottie" width={36} height={36} autoplay={false} />
</div>
xLog
</div>
<div className="flex-1"></div>
{userWidget}
<div className="w-[1px] bg-border absolute top-0 right-0 bottom-0"></div>
</div>
)
}
+24
View File
@@ -0,0 +1,24 @@
import { useCallback, useEffect, useState } from "react"
const mobileWidth = 1024
export function useMobileLayout() {
const [isMobile, setIsMobile] = useState(false)
const onResize = useCallback(() => {
setIsMobile(window.innerWidth < mobileWidth)
}, [])
useEffect(() => {
window.addEventListener("resize", onResize)
return () => {
window.removeEventListener("resize", onResize)
}
}, [onResize])
useEffect(() => {
setIsMobile(window.innerWidth < mobileWidth)
}, [])
return isMobile
}