Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Web/fix intial render #1120

Merged
merged 5 commits into from
Dec 15, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions apps/web/src/root.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ReactElement, ReactNode } from 'react'
import { Outlet, useLoaderData } from 'react-router'
import { Outlet, useLoaderData, useRouteLoaderData } from 'react-router'

import { Types, makeOpenGraphWebsite } from '@suddenlygiovanni/open-graph-protocol'

Expand Down Expand Up @@ -69,7 +69,8 @@ export function loader({ request }: Route.LoaderArgs) {

export function Layout({ children }: { children: ReactNode }): ReactElement {
// if there was an error running the loader, data could be missing
const data = useLoaderData<typeof loader | null>()
const data = useRouteLoaderData<typeof loader>('root')

const theme = useOptionalTheme()
return (
<Document
Expand Down
67 changes: 27 additions & 40 deletions apps/web/src/shell/header.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type ReactElement, type SyntheticEvent, memo, useCallback, useMemo } from 'react'
import type { ReactElement, SyntheticEvent } from 'react'

import { Layout } from '@suddenlygiovanni/ui/components/layout/layout.tsx'
import { NavigationMenuToggle } from '@suddenlygiovanni/ui/components/navigation-menu-toggle/navigation-menu-toggle.tsx'
Expand All @@ -24,45 +24,13 @@ const routes = (

const PRIMARY_NAVIGATION = 'primary-navigation'

export const Header = memo(function Header({
theme,
}: { theme: 'light' | 'dark' | null }): ReactElement {
export function Header({ theme }: { theme: 'light' | 'dark' | null }): ReactElement {
const [isMobileNavigationVisible, toggleMobileNavigationVisibility] = useToggle(false)

const handleMobileNavigationClick = useCallback(
<T extends HTMLElement>(event: SyntheticEvent<T>) => {
event.stopPropagation()
toggleMobileNavigationVisibility()
},
[toggleMobileNavigationVisibility],
)

const renderLi = useMemo(
() =>
routes.map(({ title, url, uri, description, disabled }) => (
<li
className={clsx(
'flex min-h-16 min-w-32 items-center justify-end outline-hidden md:min-h-fit md:min-w-fit',
)}
key={uri}
// onClick={stopPropagation}
>
<NavLink
aria-disabled={disabled ? 'true' : undefined}
aria-label={description}
onClick={handleMobileNavigationClick}
prefetch="intent"
role="menuitem"
to={url}
tabIndex={0}
>
{title}
</NavLink>
</li>
)),

[handleMobileNavigationClick],
)
function handleMobileNavigationClick<T extends HTMLElement>(event: SyntheticEvent<T>): void {
event.stopPropagation()
toggleMobileNavigationVisibility()
}

return (
<Layout.Header
Expand Down Expand Up @@ -105,7 +73,26 @@ export const Header = memo(function Header({
onClick={handleMobileNavigationClick}
onKeyDown={handleMobileNavigationClick}
>
{renderLi}
{routes.map(({ title, url, uri, description, disabled }) => (
<li
className={clsx(
'flex min-h-16 min-w-32 items-center justify-end outline-hidden md:min-h-fit md:min-w-fit',
)}
key={uri}
>
<NavLink
aria-disabled={disabled ? 'true' : undefined}
aria-label={description}
onClick={handleMobileNavigationClick}
prefetch="intent"
role="menuitem"
to={url}
tabIndex={0}
>
{title}
</NavLink>
</li>
))}
<ThemeSwitch
userPreference={theme}
className="ml-16 aspect-square"
Expand All @@ -115,4 +102,4 @@ export const Header = memo(function Header({
</div>
</Layout.Header>
)
})
}
28 changes: 15 additions & 13 deletions packages/ui/src/components/mode-toggle/mode-toggle.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type ReactNode, memo, useMemo } from 'react'
import type { FC, MouseEventHandler } from 'react'

import { Icons } from '#components/icons/icons.tsx'
import { clsx } from '#lib/utils.ts'
Expand All @@ -19,13 +19,15 @@ interface ModeToggleProps {
}

const NAME = 'ModeToggle'
const ModeToggle = memo(function ModeToggle({
setTheme,
theme,
className,
}: ModeToggleProps): ReactNode {
const isLight = useMemo<boolean>(() => theme === 'light', [theme])
const isDark = useMemo<boolean>(() => theme === 'dark', [theme])
const ModeToggle: FC<ModeToggleProps> = ({ setTheme, theme, className }) => {
const isLight = theme === 'light'
const isDark = theme === 'dark'

function makeToggleHandler(mode: Theme): MouseEventHandler<HTMLDivElement> {
return function toggleHandler(_) {
setTheme(mode)
}
}

return (
<DropdownMenu>
Expand All @@ -37,9 +39,9 @@ const ModeToggle = memo(function ModeToggle({
data-testid={NAME}
>
{isLight || theme === null ? (
<Icons.moon className={clsx('h-[1.2rem]', 'w-[1.2rem]')} />
<Icons.moon className={clsx('h-[1.2rem] w-[1.2rem]')} />
) : (
<Icons.sun className={clsx('h-[1.2rem]', 'w-[1.2rem]')} />
<Icons.sun className={clsx('h-[1.2rem] w-[1.2rem]')} />
)}

<span className="sr-only">Toggle theme</span>
Expand All @@ -48,20 +50,20 @@ const ModeToggle = memo(function ModeToggle({
<DropdownMenuContent align="end">
<DropdownMenuItem
disabled={isLight}
onClick={(): void => setTheme('light')}
onClick={makeToggleHandler('light')}
>
Light
</DropdownMenuItem>
<DropdownMenuItem
disabled={isDark}
onClick={(): void => setTheme('dark')}
onClick={makeToggleHandler('dark')}
>
Dark
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
)
})
}
ModeToggle.displayName = NAME

export { ModeToggle }
Loading