Skip to content

Commit

Permalink
Refactor LayoutMiddleBox for auth pages; solve className on body tag
Browse files Browse the repository at this point in the history
  • Loading branch information
tordans committed Dec 23, 2022
1 parent e6785e2 commit daa28fd
Show file tree
Hide file tree
Showing 14 changed files with 79 additions and 49 deletions.
6 changes: 3 additions & 3 deletions src/auth/components/LoginForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { useMutation } from "@blitzjs/rpc"
import { AuthenticationError, PromiseReturnType } from "blitz"
import login from "src/auth/mutations/login"
import { Login } from "src/auth/validations"
import { FormLayout } from "src/core/components/forms"
import { Form, FORM_ERROR } from "src/core/components/forms/Form"
import { LabeledTextField } from "src/core/components/forms/LabeledTextField"
import { Link } from "src/core/components/links"
import { LayoutMiddleBox } from "src/core/layouts"

type LoginFormProps = {
onSuccess?: (user: PromiseReturnType<typeof login>) => void
Expand All @@ -16,7 +16,7 @@ export const LoginForm = (props: LoginFormProps) => {
const [loginMutation] = useMutation(login)

return (
<FormLayout title="Einloggen" subtitle="Willkommen zurück!">
<LayoutMiddleBox title="Einloggen" subtitle="Willkommen zurück!">
<Form
className="space-y-6"
submitText="Einloggen"
Expand Down Expand Up @@ -50,7 +50,7 @@ export const LoginForm = (props: LoginFormProps) => {
<div className="mt-4">
Oder <Link href={Routes.SignupPage()}>registrieren</Link>
</div>
</FormLayout>
</LayoutMiddleBox>
)
}

Expand Down
6 changes: 3 additions & 3 deletions src/auth/components/SignupForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { Routes } from "@blitzjs/next"
import { useMutation } from "@blitzjs/rpc"
import signup from "src/auth/mutations/signup"
import { Signup } from "src/auth/validations"
import { FormLayout } from "src/core/components/forms"
import { Form, FORM_ERROR } from "src/core/components/forms/Form"
import { LabeledTextField } from "src/core/components/forms/LabeledTextField"
import { Link } from "src/core/components/links"
import { LayoutMiddleBox } from "src/core/layouts"

type SignupFormProps = {
onSuccess?: () => void
Expand All @@ -29,7 +29,7 @@ export const SignupForm = (props: SignupFormProps) => {
}

return (
<FormLayout title="Registrieren" subtitle="Willkommen!">
<LayoutMiddleBox title="Registrieren" subtitle="Willkommen!">
<Form
className="space-y-6"
submitText="Registrieren"
Expand Down Expand Up @@ -67,7 +67,7 @@ export const SignupForm = (props: SignupFormProps) => {
anmelden
</Link>
</div>
</FormLayout>
</LayoutMiddleBox>
)
}

Expand Down
31 changes: 0 additions & 31 deletions src/core/components/forms/FormLayout.tsx

This file was deleted.

1 change: 0 additions & 1 deletion src/core/components/forms/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export * from "./Form"
export * from "./LabeledTextField"
export * from "./FormLayout"
5 changes: 4 additions & 1 deletion src/core/layouts/Layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ import { BlitzLayout } from "@blitzjs/next"
import Head from "next/head"
import React from "react"

type Props = { children?: React.ReactNode }
type Props = {
children?: React.ReactNode
}

export const Layout: BlitzLayout<Props> = ({ children }) => {
return (
<>
<Head>
{/* Reminder: We cannot use this to set the <body class>. See index.css for our workaround. */}
<link rel="icon" href="/favicon.svg" />
</Head>
{children}
Expand Down
8 changes: 5 additions & 3 deletions src/core/layouts/LayoutArticle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import { BlitzLayout } from "@blitzjs/next"
import React from "react"
import { Layout } from "./Layout"

type Props = { children?: React.ReactNode }
type Props = {
children?: React.ReactNode
}

export const LayoutArticle: BlitzLayout<Props> = ({ children, ...props }) => {
export const LayoutArticle: BlitzLayout<Props> = ({ children }) => {
return (
<Layout {...props}>
<Layout>
<div className="prose mx-auto">{children}</div>
</Layout>
)
Expand Down
38 changes: 38 additions & 0 deletions src/core/layouts/LayoutMiddleBox.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { EyeDropperIcon } from "@heroicons/react/24/solid"
import Head from "next/head"
import React from "react"
import { Layout } from "./Layout"
import { Logo } from "./Logo"

type Props = {
title?: string
subtitle?: string
children: React.ReactNode
}

export const LayoutMiddleBox: React.FC<Props> = ({ title, subtitle, children }) => {
return (
<Layout>
<div className="set-bg-indigo-50-on-body">
<div className="flex min-h-full flex-col justify-center py-12 sm:px-6 lg:px-8">
<div className="sm:mx-auto sm:w-full sm:max-w-md">
<Logo />

{Boolean(title) && (
<h2 className="mt-6 text-center text-3xl font-bold tracking-tight text-gray-900">
{title}
</h2>
)}
{Boolean(subtitle) && (
<p className="mt-2 text-center text-sm text-gray-600">{subtitle}</p>
)}
</div>

<div className="mt-8 sm:mx-auto sm:w-full sm:max-w-md">
<div className="bg-white py-8 px-4 shadow sm:rounded-lg sm:px-10">{children}</div>
</div>
</div>
</div>
</Layout>
)
}
6 changes: 6 additions & 0 deletions src/core/layouts/Logo/Logo.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { EyeDropperIcon } from "@heroicons/react/24/solid"
import React from "react"

export const Logo: React.FC = () => {
return <EyeDropperIcon className="mx-auto h-12 w-auto" />
}
1 change: 1 addition & 0 deletions src/core/layouts/Logo/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./Logo"
2 changes: 2 additions & 0 deletions src/core/layouts/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export * from "./Layout"
export * from "./LayoutArticle"
export * from "./LayoutMiddleBox"
export * from "./Logo"
export * from "./MetaTags"
11 changes: 11 additions & 0 deletions src/core/styles/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@
#__next {
@apply flex w-full h-full flex-none flex-col;
}

/*
This is wild…
The next.js Head component does not provide a way to add classes to the <body> tag.
One can only add static lasses via _document.tsx.
There are complex solutions involving `pagePros` – thanks @ https://stackoverflow.com/a/66358460/729221
However, we can use css instead – thanks @ https://stackoverflow.com/a/21253034/729221
*/
body:has(#__next .set-bg-indigo-50-on-body) {
@apply bg-indigo-50;
}
}

@layer components {
Expand Down
1 change: 1 addition & 0 deletions src/pages/_document.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class MyDocument extends Document {
// const initialProps = await Document.getInitialProps(ctx)
// return {...initialProps}
// }

render() {
return (
<Html lang="de" className="h-full">
Expand Down
7 changes: 3 additions & 4 deletions src/pages/auth/forgot-password.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@ import { BlitzPage } from "@blitzjs/next"
import { useMutation } from "@blitzjs/rpc"
import forgotPassword from "src/auth/mutations/forgotPassword"
import { ForgotPassword } from "src/auth/validations"
import { FormLayout } from "src/core/components/forms"
import { Form, FORM_ERROR } from "src/core/components/forms/Form"
import { LabeledTextField } from "src/core/components/forms/LabeledTextField"
import { Layout, MetaTags } from "src/core/layouts"
import { Layout, LayoutMiddleBox, MetaTags } from "src/core/layouts"

const ForgotPasswordPage: BlitzPage = () => {
const [forgotPasswordMutation, { isSuccess }] = useMutation(forgotPassword)

return (
<Layout>
<MetaTags noindex title="Passwort vergessen" />
<FormLayout title="Passwort vergessen">
<LayoutMiddleBox title="Passwort vergessen">
{isSuccess ? (
<>
<h2>Anfrage empfangen</h2>
Expand Down Expand Up @@ -41,7 +40,7 @@ const ForgotPasswordPage: BlitzPage = () => {
<LabeledTextField name="email" label="Email" placeholder="Email" />
</Form>
)}
</FormLayout>
</LayoutMiddleBox>
</Layout>
)
}
Expand Down
5 changes: 2 additions & 3 deletions src/pages/auth/reset-password.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ import { useRouter } from "next/router"
import { useEffect, useState } from "react"
import resetPassword from "src/auth/mutations/resetPassword"
import { ResetPassword } from "src/auth/validations"
import { FormLayout } from "src/core/components/forms"
import { Form, FORM_ERROR } from "src/core/components/forms/Form"
import { LabeledTextField } from "src/core/components/forms/LabeledTextField"
import { Layout, MetaTags } from "src/core/layouts"
import { Layout, LayoutMiddleBox, MetaTags } from "src/core/layouts"

const ResetPasswordPage: BlitzPage = () => {
const [token, setToken] = useState("")
Expand Down Expand Up @@ -70,7 +69,7 @@ ResetPasswordPage.redirectAuthenticatedTo = "/"
ResetPasswordPage.getLayout = (page) => (
<Layout>
<MetaTags noindex title="Passwort vergessen" />
<FormLayout title="Neues Passwort vergeben">{page}</FormLayout>
<LayoutMiddleBox title="Neues Passwort vergeben">{page}</LayoutMiddleBox>
</Layout>
)

Expand Down

0 comments on commit daa28fd

Please sign in to comment.