|
| 1 | +import { |
| 2 | + CodeBlock, |
| 3 | + CodeBlockBody, |
| 4 | + CodeBlockCode, |
| 5 | + CodeBlockCopyButton, |
| 6 | + CodeBlockExpanderButton, |
| 7 | + CodeBlockHeader, |
| 8 | + CodeBlockTitle, |
| 9 | +} from "@/code-block"; |
| 10 | +import { code } from "@/code-block/code"; |
| 11 | +import { InlineCode } from "@/inline-code"; |
| 12 | +import { preventWrongThemeFlashScriptContent } from "@/theme-provider"; |
| 13 | +import { FileText } from "@phosphor-icons/react/FileText"; |
| 14 | +import type { HeadersFunction, MetaFunction } from "@remix-run/node"; |
| 15 | + |
| 16 | +export const meta: MetaFunction = () => { |
| 17 | + return [ |
| 18 | + { title: "@ngrok/mantle — Theme Provider" }, |
| 19 | + { name: "description", content: "mantle is ngrok's UI library and design system" }, |
| 20 | + ]; |
| 21 | +}; |
| 22 | + |
| 23 | +export const headers: HeadersFunction = () => { |
| 24 | + return { |
| 25 | + "Cache-Control": "max-age=300, stale-while-revalidate=604800", |
| 26 | + }; |
| 27 | +}; |
| 28 | + |
| 29 | +export default function Page() { |
| 30 | + return ( |
| 31 | + <div> |
| 32 | + <h1 className="text-5xl font-medium">Theme Provider</h1> |
| 33 | + <p className="mb-4 mt-4 text-xl text-gray-600"> |
| 34 | + ThemeProvider is a React Context Provider that provides the current theme to the application and a function to |
| 35 | + change it. |
| 36 | + </p> |
| 37 | + <div className="space-y-4"> |
| 38 | + <p> |
| 39 | + To use the <InlineCode>ThemeProvider</InlineCode>, wrap your application's entry point. This should be |
| 40 | + done as high in the component tree as possible. |
| 41 | + </p> |
| 42 | + <p> |
| 43 | + You should also add the <InlineCode>PreventWrongThemeFlash</InlineCode> component to the head of your |
| 44 | + application to prevent a Flash of Unstyled Content (FOUC) when the app first loads. |
| 45 | + </p> |
| 46 | + <CodeBlock className="rounded-b-lg rounded-t-none"> |
| 47 | + <CodeBlockHeader> |
| 48 | + <FileText className="h-5 w-5" weight="fill" /> |
| 49 | + <CodeBlockTitle>root.tsx</CodeBlockTitle> |
| 50 | + </CodeBlockHeader> |
| 51 | + <CodeBlockBody> |
| 52 | + <CodeBlockCopyButton /> |
| 53 | + <CodeBlockCode language="tsx">{code` |
| 54 | + import { PreventWrongThemeFlash, ThemeProvider } from "@ngrok/mantle"; |
| 55 | +
|
| 56 | + export default function App() { |
| 57 | + return ( |
| 58 | + <html className="h-full" lang="en-US" dir="ltr"> |
| 59 | + <head> |
| 60 | + // 👇 add this as high in the <head> as possible! |
| 61 | + <PreventWrongThemeFlash /> |
| 62 | + <meta charSet="utf-8" /> |
| 63 | + <meta name="author" content="ngrok" /> |
| 64 | + <meta name="viewport" content="width=device-width, initial-scale=1" /> |
| 65 | + <Meta /> |
| 66 | + <Links /> |
| 67 | + </head> |
| 68 | + <body className="h-full min-h-full overflow-y-scroll bg-background"> |
| 69 | + // 👇 wrap your app entry in the ThemeProvider |
| 70 | + <ThemeProvider> |
| 71 | + <Outlet /> |
| 72 | + </ThemeProvider> |
| 73 | + </body> |
| 74 | + </html> |
| 75 | + ); |
| 76 | + } |
| 77 | + `}</CodeBlockCode> |
| 78 | + <CodeBlockExpanderButton /> |
| 79 | + </CodeBlockBody> |
| 80 | + </CodeBlock> |
| 81 | + <p> |
| 82 | + Sometimes you cannot use the <InlineCode>PreventWrongThemeFlash</InlineCode> component because your webserver |
| 83 | + is not able to render React components. In this case, you can use the copy the following script and add it to |
| 84 | + your application's <InlineCode><head></InlineCode>: |
| 85 | + </p> |
| 86 | + <CodeBlock className="rounded-b-lg rounded-t-none"> |
| 87 | + <CodeBlockHeader> |
| 88 | + <FileText className="h-5 w-5" weight="fill" /> |
| 89 | + <CodeBlockTitle>index.html</CodeBlockTitle> |
| 90 | + </CodeBlockHeader> |
| 91 | + <CodeBlockBody> |
| 92 | + <CodeBlockCopyButton /> |
| 93 | + <CodeBlockCode language="html">{code`<script> |
| 94 | +${preventWrongThemeFlashScriptContent({ defaultTheme: "system" })} |
| 95 | +</script> |
| 96 | +`}</CodeBlockCode> |
| 97 | + <CodeBlockExpanderButton /> |
| 98 | + </CodeBlockBody> |
| 99 | + </CodeBlock> |
| 100 | + <p> |
| 101 | + Then, in your application, you can use the <InlineCode>useTheme</InlineCode> hook to get and change the |
| 102 | + current theme: |
| 103 | + </p> |
| 104 | + <CodeBlock className="rounded-b-lg rounded-t-none"> |
| 105 | + <CodeBlockHeader> |
| 106 | + <FileText className="h-5 w-5" weight="fill" /> |
| 107 | + <CodeBlockTitle>app.tsx</CodeBlockTitle> |
| 108 | + </CodeBlockHeader> |
| 109 | + <CodeBlockBody> |
| 110 | + <CodeBlockCopyButton /> |
| 111 | + <CodeBlockCode language="tsx">{code` |
| 112 | + import { |
| 113 | + isTheme, |
| 114 | + Select, |
| 115 | + SelectContent, |
| 116 | + SelectGroup, |
| 117 | + SelectItem, |
| 118 | + SelectLabel, |
| 119 | + SelectTrigger, |
| 120 | + theme, |
| 121 | + useTheme, |
| 122 | + } from "@ngrok/mantle"; |
| 123 | +
|
| 124 | + function App() { |
| 125 | + const [currentTheme, setTheme] = useTheme(); |
| 126 | +
|
| 127 | + return ( |
| 128 | + <> |
| 129 | + <Select |
| 130 | + value={currentTheme} |
| 131 | + onValueChange={(value) => { |
| 132 | + const maybeNewTheme = isTheme(value) ? value : undefined; |
| 133 | + if (maybeNewTheme) { |
| 134 | + setTheme(maybeNewTheme); |
| 135 | + } |
| 136 | + }} |
| 137 | + > |
| 138 | + <div className="ml-auto"> |
| 139 | + <span className="sr-only">Theme Switcher</span> |
| 140 | + <SelectTrigger className="w-min"> |
| 141 | + <Sun className="mr-1 h-6 w-6" /> |
| 142 | + </SelectTrigger> |
| 143 | + </div> |
| 144 | + <SelectContent> |
| 145 | + <SelectGroup> |
| 146 | + <SelectLabel>Choose a theme</SelectLabel> |
| 147 | + <SelectItem value={theme("system")}>System</SelectItem> |
| 148 | + <SelectItem value={theme("light")}>Light</SelectItem> |
| 149 | + <SelectItem value={theme("dark")}>Dark</SelectItem> |
| 150 | + <SelectItem value={theme("light-high-contrast")}>Light High Contrast</SelectItem> |
| 151 | + <SelectItem value={theme("dark-high-contrast")}>Dark High Contrast</SelectItem> |
| 152 | + </SelectGroup> |
| 153 | + </SelectContent> |
| 154 | + </Select> |
| 155 | + {/* The rest of your app... */} |
| 156 | + </> |
| 157 | + ); |
| 158 | + } |
| 159 | +`}</CodeBlockCode> |
| 160 | + </CodeBlockBody> |
| 161 | + <CodeBlockExpanderButton /> |
| 162 | + </CodeBlock> |
| 163 | + </div> |
| 164 | + </div> |
| 165 | + ); |
| 166 | +} |
0 commit comments