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 all 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
75 changes: 63 additions & 12 deletions apps/web/src/root.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,35 @@
import type { ReactElement, ReactNode } from 'react'
import { Outlet, useLoaderData } from 'react-router'
import {
Links,
Meta,
Outlet,
Scripts,
ScrollRestoration,
useLoaderData,
useRouteLoaderData,
} from 'react-router'

import { Types, makeOpenGraphWebsite } from '@suddenlygiovanni/open-graph-protocol'
import { Layout as _Layout } from '@suddenlygiovanni/ui/components/layout/layout.tsx'
import { clsx } from '@suddenlygiovanni/ui/lib/utils.ts'

import hero2800wAssetUrl from '~/assets/hero/giovanni_ravalico-profile_color_e4cily_c_scale,w_2800.webp'
import faviconAssertUrl from '~/assets/suddenly_giovanni-icon-white.svg'
import { config } from '~/config.ts'
import { Document, Footer, GeneralErrorBoundary, Header, Main } from '~/shell/index.tsx'
import { useOptionalTheme, useTheme } from '~/routes/resources/theme-switch.tsx'
import { Footer, GeneralErrorBoundary, Header } from '~/shell/index.tsx'
import fontStyleSheetUrl from '~/styles/fonts.css?url'
import tailwindStyleSheetUrl from '~/styles/tailwind.css?url'
import { getHints } from '~/utils/client-hints.tsx'
import { ClientHintCheck } from '~/utils/client-hints.tsx'
import { getEnv } from '~/utils/env.server.ts'
import { getDomainUrl } from '~/utils/misc.ts'
import { getTheme } from '~/utils/theme.server.ts'

// biome-ignore lint/nursery/useImportRestrictions: <explanation>
import type { Route } from './+types/root.ts'
import { useOptionalTheme, useTheme } from './routes/resources/theme-switch.tsx'

const { Body, Main } = _Layout

export const links: Route.LinksFunction = () => {
return [
Expand Down Expand Up @@ -67,18 +80,56 @@ export function loader({ request }: Route.LoaderArgs) {
}
}

export function Layout({ children }: { children: ReactNode }): ReactElement {
export function Layout(props: { 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
nonce={undefined}
theme={theme}
env={data?.ENV}
<html
className="min-h-screen"
data-theme={theme}
lang="en"
>
{children}
</Document>
<head>
<ClientHintCheck />
<meta charSet="utf-8" />
<meta
httpEquiv="Content-Type"
content="text/html;charset=utf-8"
/>
<meta
content="width=device-width, initial-scale=1"
name="viewport"
/>
<Meta />
<Links />
</head>
<Body className={clsx('min-h-full bg-background font-sans text-foreground antialiased')}>
{props.children}

{/**
* Manages scroll position for client-side transitions
* If you use a nonce-based content security policy for scripts, you must provide the
* `nonce` prop. Otherwise, omit the nonce prop as shown here.
*/}
<ScrollRestoration />

{/**
* Script tags go here
* If you use a nonce-based content security policy for scripts, you must provide the
* `nonce` prop.
* Otherwise, omit the nonce prop as shown here.
*/}
<script
// biome-ignore lint/security/noDangerouslySetInnerHtml: we need to set the ENV variable
dangerouslySetInnerHTML={{
__html: `window.ENV = ${JSON.stringify(data?.ENV, null, 2)};`,
}}
/>
<Scripts />
</Body>
</html>
)
}

Expand All @@ -89,7 +140,7 @@ export default function App(_: Route.ComponentProps): ReactElement {
return (
<>
<Header theme={requestInfo.userPrefs.theme} />
<Main>
<Main className="mx-auto my-8 h-full w-full max-w-4xl px-8">
<Outlet />
</Main>
<Footer />
Expand Down
56 changes: 0 additions & 56 deletions apps/web/src/shell/document.tsx

This file was deleted.

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>
)
})
}
2 changes: 0 additions & 2 deletions apps/web/src/shell/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
export { Main } from './main.tsx'
export { Header } from './header.tsx'
export { Footer } from './footer.tsx'
export { Document } from './document.tsx'
export { GeneralErrorBoundary } from './general-error-boundary.tsx'
7 changes: 0 additions & 7 deletions apps/web/src/shell/main.tsx

This file was deleted.

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