Skip to content

Commit

Permalink
refactor: do not include header in root layout
Browse files Browse the repository at this point in the history
This patch removes the `<Header>` from the root layout and instead
creates a new `_header` layout that uses the sticky version of the
header and updates the `_layout` layout to include a header that is
simply part of the column flex.
  • Loading branch information
nicholaschiang committed Aug 7, 2023
1 parent e489f00 commit a24b102
Show file tree
Hide file tree
Showing 19 changed files with 78 additions and 55 deletions.
61 changes: 61 additions & 0 deletions app/components/header.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { Link, useMatches } from '@remix-run/react'
import { User, LogIn, LogOut, ChevronRight } from 'lucide-react'
import { Fragment } from 'react'

import { ThemeSwitcher } from 'components/theme-switcher'
import { buttonVariants } from 'components/ui/button'

import { type Handle } from 'root'
import { useOptionalUser } from 'utils'
import { cn } from 'utils/cn'

export function Header({ className }: { className?: string }) {
const matches = useMatches()
const user = useOptionalUser()
return (
<header
className={cn(
'bg-white/80 dark:bg-gray-950/80 backdrop-blur-xl border-b border-gray-200 dark:border-gray-700 px-6 flex items-center justify-between h-10 z-10',
className,
)}
>
<ol className='flex items-center gap-2 text-lg tracking-tighter lowercase'>
{matches
.filter((match) => match.handle && match.handle.breadcrumb)
.map((match, index) => (
<Fragment key={match.id}>
{index !== 0 && (
<ChevronRight className='text-gray-300 dark:text-gray-600 h-4 w-4 mt-0.5' />
)}
<li>{(match.handle as Handle).breadcrumb(match)}</li>
</Fragment>
))}
</ol>
<div className='flex items-center'>
{!matches.some((match) => match.id.includes('login')) && (
<Link
aria-label={user ? 'Log out' : 'Log in'}
className={buttonVariants({ size: 'icon', variant: 'ghost' })}
to={user ? '/logout' : '/login'}
>
{user ? (
<LogOut className='w-3 h-3' />
) : (
<LogIn className='w-3 h-3' />
)}
</Link>
)}
{user && (
<Link
aria-label='Edit profile'
className={buttonVariants({ size: 'icon', variant: 'ghost' })}
to='/profile'
>
<User className='w-3 h-3' />
</Link>
)}
<ThemeSwitcher />
</div>
</header>
)
}
55 changes: 1 addition & 54 deletions app/root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
useLoaderData,
useRouteError,
isRouteErrorResponse,
useMatches,
useNavigation,
useNavigationType,
type RouteMatch,
Expand All @@ -23,12 +22,8 @@ import {
json,
} from '@vercel/remix'
import cn from 'classnames'
import { User, LogIn, LogOut, ChevronRight } from 'lucide-react'
import NProgress from 'nprogress'
import { Fragment, type ReactNode, useEffect } from 'react'

import { ThemeSwitcher } from 'components/theme-switcher'
import { buttonVariants } from 'components/ui/button'
import { type ReactNode, useEffect } from 'react'

import tailwindStylesheetUrl from 'styles/tailwind.css'

Expand All @@ -41,7 +36,6 @@ import {
isTheme,
useTheme,
} from 'theme'
import { useOptionalUser } from 'utils'

export type Handle = { breadcrumb: (match: RouteMatch) => ReactNode }

Expand Down Expand Up @@ -241,7 +235,6 @@ function App({ data, children }: { data?: LoaderData; children: ReactNode }) {
<ThemeHead ssrTheme={Boolean(data?.theme)} />
</head>
<body className='bg-white text-gray-900 selection:bg-gray-200 selection:text-gray-900 dark:bg-gray-950 dark:text-gray-50 dark:selection:bg-gray-800 dark:selection:text-gray-50'>
<Header />
{children}
<script
dangerouslySetInnerHTML={{
Expand All @@ -258,52 +251,6 @@ function App({ data, children }: { data?: LoaderData; children: ReactNode }) {
)
}

function Header() {
const matches = useMatches()
const user = useOptionalUser()
return (
<header className='sticky top-0 bg-white/80 dark:bg-gray-950/80 backdrop-blur-xl border-b border-gray-200 dark:border-gray-700 px-6 flex items-center justify-between h-10 z-10'>
<ol className='flex items-center gap-2 text-lg tracking-tighter lowercase'>
{matches
.filter((match) => match.handle && match.handle.breadcrumb)
.map((match, index) => (
<Fragment key={match.id}>
{index !== 0 && (
<ChevronRight className='text-gray-300 dark:text-gray-600 h-4 w-4 mt-0.5' />
)}
<li>{(match.handle as Handle).breadcrumb(match)}</li>
</Fragment>
))}
</ol>
<div className='flex items-center'>
{!matches.some((match) => match.id.includes('login')) && (
<Link
aria-label={user ? 'Log out' : 'Log in'}
className={buttonVariants({ size: 'icon', variant: 'ghost' })}
to={user ? '/logout' : '/login'}
>
{user ? (
<LogOut className='w-3 h-3' />
) : (
<LogIn className='w-3 h-3' />
)}
</Link>
)}
{user && (
<Link
aria-label='Edit profile'
className={buttonVariants({ size: 'icon', variant: 'ghost' })}
to='/profile'
>
<User className='w-3 h-3' />
</Link>
)}
<ThemeSwitcher />
</div>
</header>
)
}

function ErrorDisplay({ children }: { children: ReactNode }) {
return (
<ThemeProvider specifiedTheme={null}>
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
12 changes: 12 additions & 0 deletions app/routes/_header.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Outlet } from '@remix-run/react'

import { Header } from 'components/header'

export default function HeaderLayout() {
return (
<>
<Header className='sticky top-0' />
<Outlet />
</>
)
}
5 changes: 4 additions & 1 deletion app/routes/_layout.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Prisma } from '@prisma/client'
import { Outlet } from '@remix-run/react'

import { Header } from 'components/header'

import { log } from 'log.server'

export function loader() {
Expand Down Expand Up @@ -46,7 +48,8 @@ export function loader() {

export default function LayoutPage() {
return (
<main className='fixed inset-0 pt-10 flex flex-col overflow-hidden'>
<main className='fixed inset-0 flex flex-col overflow-hidden'>
<Header className='flex-none' />
<Outlet />
</main>
)
Expand Down

0 comments on commit a24b102

Please sign in to comment.