|
| 1 | +import { createController } from 'remix/router' |
| 2 | +import { css, type Handle } from 'remix/ui' |
| 3 | + |
| 4 | +import { routes } from '../routes.ts' |
| 5 | + |
| 6 | +export default createController(routes, { |
| 7 | + actions: { |
| 8 | + async home(context) { |
| 9 | + await sleep(1000, context.request.signal) |
| 10 | + return <HomePage /> |
| 11 | + }, |
| 12 | + |
| 13 | + async about(context) { |
| 14 | + await sleep(1000, context.request.signal) |
| 15 | + return <AboutPage /> |
| 16 | + }, |
| 17 | + |
| 18 | + async greet(context) { |
| 19 | + let name = '' |
| 20 | + if (context.request.method === 'POST') { |
| 21 | + let formData = await context.request.formData() |
| 22 | + let value = formData.get('name') |
| 23 | + name = typeof value === 'string' ? value.trim() : '' |
| 24 | + } |
| 25 | + |
| 26 | + await sleep(1000, context.request.signal) |
| 27 | + return <GreetingPage name={name || 'friend'} /> |
| 28 | + }, |
| 29 | + }, |
| 30 | +}) |
| 31 | + |
| 32 | +function HomePage() { |
| 33 | + return () => ( |
| 34 | + <article> |
| 35 | + <p mix={eyebrowStyle}>Home</p> |
| 36 | + <h1 mix={titleStyle}>A client-only Remix app</h1> |
| 37 | + <p mix={bodyStyle}> |
| 38 | + This page came directly from a fetch router handler. No HTTP request or response was |
| 39 | + involved. |
| 40 | + </p> |
| 41 | + <form method="POST" action={routes.greet.href()} mix={formStyle}> |
| 42 | + <label htmlFor="name" mix={labelStyle}> |
| 43 | + What should we call you? |
| 44 | + </label> |
| 45 | + <div mix={formControlsStyle}> |
| 46 | + <input id="name" name="name" autoComplete="name" required mix={inputStyle} /> |
| 47 | + <button type="submit" mix={buttonStyle}> |
| 48 | + Submit |
| 49 | + </button> |
| 50 | + </div> |
| 51 | + </form> |
| 52 | + </article> |
| 53 | + ) |
| 54 | +} |
| 55 | + |
| 56 | +function AboutPage() { |
| 57 | + return () => ( |
| 58 | + <article> |
| 59 | + <p mix={eyebrowStyle}>About</p> |
| 60 | + <h1 mix={titleStyle}>URLs in, rendered UI out</h1> |
| 61 | + <p mix={bodyStyle}> |
| 62 | + Each route waits briefly before returning a <code>RemixNode</code>, so the loading and |
| 63 | + cancellation behavior is easy to see. |
| 64 | + </p> |
| 65 | + </article> |
| 66 | + ) |
| 67 | +} |
| 68 | + |
| 69 | +function GreetingPage(handle: Handle<{ name: string }>) { |
| 70 | + return () => ( |
| 71 | + <article> |
| 72 | + <p mix={eyebrowStyle}>Form submitted</p> |
| 73 | + <h1 mix={titleStyle}>Hello, {handle.props.name}!</h1> |
| 74 | + <p mix={bodyStyle}> |
| 75 | + POST submissions expose the Navigation API's form data through{' '} |
| 76 | + <code>context.request.formData()</code> without making an HTTP request. History traversals |
| 77 | + return here with GET because navigation entries do not retain <code>FormData</code>. |
| 78 | + </p> |
| 79 | + <form method="POST" action={routes.greet.href()} mix={formStyle}> |
| 80 | + <label htmlFor="next-name" mix={labelStyle}> |
| 81 | + Try another name |
| 82 | + </label> |
| 83 | + <div mix={formControlsStyle}> |
| 84 | + <input id="next-name" name="name" autoComplete="name" required mix={inputStyle} /> |
| 85 | + <button type="submit" mix={buttonStyle}> |
| 86 | + Submit again |
| 87 | + </button> |
| 88 | + </div> |
| 89 | + </form> |
| 90 | + <p mix={bodyStyle}> |
| 91 | + Because this form submits to the current URL, it replaces the current history entry. The{' '} |
| 92 | + <a href={routes.home.href()} mix={linkStyle}> |
| 93 | + first submission |
| 94 | + </a>{' '} |
| 95 | + pushed a new entry because it navigated here from another URL. |
| 96 | + </p> |
| 97 | + </article> |
| 98 | + ) |
| 99 | +} |
| 100 | + |
| 101 | +export function NotFoundPage() { |
| 102 | + return () => ( |
| 103 | + <article> |
| 104 | + <p mix={eyebrowStyle}>404</p> |
| 105 | + <h1 mix={titleStyle}>Page not found</h1> |
| 106 | + <p mix={bodyStyle}> |
| 107 | + Try going back to the{' '} |
| 108 | + <a href={routes.home.href()} mix={linkStyle}> |
| 109 | + home page |
| 110 | + </a> |
| 111 | + . |
| 112 | + </p> |
| 113 | + </article> |
| 114 | + ) |
| 115 | +} |
| 116 | + |
| 117 | +function sleep(milliseconds: number, signal: AbortSignal): Promise<void> { |
| 118 | + return new Promise((resolve, reject) => { |
| 119 | + if (signal.aborted) { |
| 120 | + reject(signal.reason) |
| 121 | + return |
| 122 | + } |
| 123 | + |
| 124 | + let timeout = setTimeout(() => { |
| 125 | + signal.removeEventListener('abort', handleAbort) |
| 126 | + resolve() |
| 127 | + }, milliseconds) |
| 128 | + |
| 129 | + function handleAbort() { |
| 130 | + clearTimeout(timeout) |
| 131 | + reject(signal.reason) |
| 132 | + } |
| 133 | + |
| 134 | + signal.addEventListener('abort', handleAbort, { once: true }) |
| 135 | + }) |
| 136 | +} |
| 137 | + |
| 138 | +const eyebrowStyle = css({ |
| 139 | + margin: '0 0 0.5rem', |
| 140 | + color: '#6a48d7', |
| 141 | + fontSize: '0.75rem', |
| 142 | + fontWeight: 700, |
| 143 | + letterSpacing: '0.12em', |
| 144 | + textTransform: 'uppercase', |
| 145 | +}) |
| 146 | + |
| 147 | +const titleStyle = css({ |
| 148 | + margin: 0, |
| 149 | + fontSize: 'clamp(2rem, 7vw, 3.5rem)', |
| 150 | + lineHeight: 1.05, |
| 151 | +}) |
| 152 | + |
| 153 | +const bodyStyle = css({ |
| 154 | + maxWidth: '38rem', |
| 155 | + margin: '1.5rem 0 0', |
| 156 | + color: '#5c5965', |
| 157 | + fontSize: '1.125rem', |
| 158 | + lineHeight: 1.7, |
| 159 | +}) |
| 160 | + |
| 161 | +const formStyle = css({ |
| 162 | + display: 'grid', |
| 163 | + gap: '0.75rem', |
| 164 | + maxWidth: '30rem', |
| 165 | + marginTop: '2rem', |
| 166 | +}) |
| 167 | + |
| 168 | +const labelStyle = css({ |
| 169 | + fontWeight: 700, |
| 170 | +}) |
| 171 | + |
| 172 | +const formControlsStyle = css({ |
| 173 | + display: 'flex', |
| 174 | + gap: '0.75rem', |
| 175 | +}) |
| 176 | + |
| 177 | +const inputStyle = css({ |
| 178 | + minWidth: 0, |
| 179 | + flex: 1, |
| 180 | + border: '1px solid #bcb4d4', |
| 181 | + borderRadius: '0.6rem', |
| 182 | + padding: '0.7rem 0.8rem', |
| 183 | + font: 'inherit', |
| 184 | +}) |
| 185 | + |
| 186 | +const buttonStyle = css({ |
| 187 | + border: 0, |
| 188 | + borderRadius: '0.6rem', |
| 189 | + padding: '0.7rem 1rem', |
| 190 | + color: 'white', |
| 191 | + backgroundColor: '#5b36d6', |
| 192 | + font: 'inherit', |
| 193 | + fontWeight: 700, |
| 194 | + cursor: 'pointer', |
| 195 | +}) |
| 196 | + |
| 197 | +const linkStyle = css({ |
| 198 | + color: '#5b36d6', |
| 199 | +}) |
0 commit comments