|
1 | 1 | <p> |
2 | | - <img width="100%" src="https://assets.solidjs.com/banner?type=Primitives&background=tiles&project=cookies" alt="Solid Primitives cookies"> |
| 2 | + <img width="100%" src="https://assets.solidjs.com/banner?type=Primitives&background=tiles&project=start" alt="Solid Primitives start"> |
3 | 3 | </p> |
4 | 4 |
|
5 | | -# @solid-primitives/cookies |
| 5 | +# @solid-primitives/start |
6 | 6 |
|
7 | 7 | [](https://turborepo.org/) |
8 | | -[](https://bundlephobia.com/package/@solid-primitives/cookies) |
9 | | -[](https://www.npmjs.com/package/@solid-primitives/cookies) |
| 8 | +[](https://bundlephobia.com/package/@solid-primitives/start) |
| 9 | +[](https://www.npmjs.com/package/@solid-primitives/start) |
10 | 10 | [](https://github.com/solidjs-community/solid-primitives#contribution-process) |
11 | 11 |
|
12 | | -A set of primitives for handling cookies in solid |
| 12 | +A set of primitives for Solid Start |
13 | 13 |
|
14 | 14 | - [`createServerCookie`](#createservercookie) - Provides a getter and setter for a reactive cookie, which works isomorphically. |
15 | 15 | - [`createUserTheme`](#createusertheme) - Creates a Server Cookie providing a type safe way to store a theme and access it on the server or client. |
| 16 | +- [`getUserAgent`](#getuseragent) - Creates a Server Cookie providing a type safe way to store a theme and access it on the server or client. |
16 | 17 |
|
17 | 18 | ## Installation |
18 | 19 |
|
19 | 20 | ```bash |
20 | | -npm install @solid-primitives/cookies |
| 21 | +npm install @solid-primitives/start |
21 | 22 | # or |
22 | | -yarn add @solid-primitives/cookies |
| 23 | +yarn add @solid-primitives/start |
23 | 24 | # or |
24 | | -pnpm add @solid-primitives/cookies |
| 25 | +pnpm add @solid-primitives/start |
25 | 26 | ``` |
26 | 27 |
|
27 | 28 | ## How to use it |
@@ -62,17 +63,130 @@ theme(); // => "light" | "dark" | undefined |
62 | 63 | const [theme, setTheme] = createUserTheme("cookieName", { |
63 | 64 | defaultValue: "light", |
64 | 65 | }); |
| 66 | +``` |
65 | 67 |
|
66 | 68 | theme(); // => "light" | "dark" |
| 69 | + |
| 70 | +## `getUserAgent` |
| 71 | + |
| 72 | +Provides the value of the `userAgent` string isomorphically on the client or server |
| 73 | + |
| 74 | +```ts |
| 75 | +const userAgent: string | null = getUserAgent(); |
67 | 76 | ``` |
68 | 77 |
|
69 | 78 | ## Examples |
70 | 79 |
|
71 | | -Coming soon |
| 80 | +### `root.tsx` |
| 81 | + |
| 82 | +This is the main entry file for SolidStart demonstrating usage of the `createUserTheme` function. |
| 83 | + |
| 84 | +- Initializes the theme with the `createUserTheme` function. |
| 85 | +- Toggles between the "dark" and "light" themes using a button. |
| 86 | +- Sets the `data-theme` attribute adding compatibility between `createUserTheme` and libraries like [DaisyUI](https://daisyui.com/). |
| 87 | +- Includes a clean implementation of theming through a `Body` class. |
| 88 | +- Provides additional examples on using variant classes to style components. |
| 89 | + |
| 90 | +```tsx |
| 91 | +import { Suspense } from "solid-js"; |
| 92 | +import { |
| 93 | + A, |
| 94 | + Body, |
| 95 | + ErrorBoundary, |
| 96 | + FileRoutes, |
| 97 | + Head, |
| 98 | + Html, |
| 99 | + Meta, |
| 100 | + Routes, |
| 101 | + Scripts, |
| 102 | + Title, |
| 103 | +} from "solid-start"; |
| 104 | +import Counter from "./components/Counter"; |
| 105 | +import "./root.css"; |
| 106 | +import { createUserTheme } from "~/primitives/start"; |
| 107 | + |
| 108 | +export default function Root() { |
| 109 | + const [theme, setTheme] = createUserTheme("dark-light-theme", { defaultValue: "dark" }); |
| 110 | + const toggleTheme = () => setTheme(currentTheme => (currentTheme === "dark" ? "light" : "dark")); |
| 111 | + |
| 112 | + return ( |
| 113 | + // Set data-theme when using libraries like Daisyui: |
| 114 | + <Html lang="en" data-theme={theme()}> |
| 115 | + <Head> |
| 116 | + <Title>SolidStart - Bare</Title> |
| 117 | + <Meta charset="utf-8" /> |
| 118 | + <Meta name="viewport" content="width=device-width, initial-scale=1" /> |
| 119 | + </Head> |
| 120 | + // Apply theme as a class to the Body element: |
| 121 | + <Body class={theme()}> |
| 122 | + <div> |
| 123 | + <Counter |
| 124 | + classList={{ |
| 125 | + // Within components apply theme-dependent classes/variants: |
| 126 | + "dark-variant": theme() !== "light", |
| 127 | + "light-variant": theme() === "light", |
| 128 | + }} |
| 129 | + /> |
| 130 | + <button onClick={toggleTheme}> |
| 131 | + <span>Toggle Theme</span> |
| 132 | + </button> |
| 133 | + </div> |
| 134 | + <Scripts /> |
| 135 | + </Body> |
| 136 | + </Html> |
| 137 | + ); |
| 138 | +} |
| 139 | +``` |
| 140 | + |
| 141 | +### `root.css` |
| 142 | + |
| 143 | +Here is an absolute basic CSS file to demonstrate styling with the themes. |
| 144 | + |
| 145 | +- The `body` selector has general styles. |
| 146 | +- The `.dark` and `.light` classes provide specific styles for each theme. |
| 147 | +- The `.dark span` and `.light span` selectors show how to style specific elements based on the theme. |
| 148 | + |
| 149 | +```css |
| 150 | +body { |
| 151 | + font-family: Gordita, Roboto, Oxygen, Ubuntu, Cantarell, "Open Sans", "Helvetica Neue", sans-serif; |
| 152 | + height: "100vh"; |
| 153 | + width: "100vh"; |
| 154 | +} |
| 155 | + |
| 156 | +body.dark { |
| 157 | + background-color: black; |
| 158 | + color: aliceblue; |
| 159 | +} |
| 160 | +body.light { |
| 161 | + background-color: aliceblue; |
| 162 | + color: black; |
| 163 | +} |
| 164 | + |
| 165 | +.dark span { |
| 166 | + color: #b1d4ff; |
| 167 | +} |
| 168 | +.light span { |
| 169 | + color: #003677; |
| 170 | +} |
| 171 | +``` |
| 172 | + |
| 173 | +### `Counter.css` |
| 174 | + |
| 175 | +Continuation of the basic CSS scoped to the component using variant classes. |
| 176 | + |
| 177 | +```css |
| 178 | +.dark-variant { |
| 179 | + border: 2px solid #569cf3; |
| 180 | +} |
| 181 | + |
| 182 | +.light-variant { |
| 183 | + border: 2px solid #05ff22; |
| 184 | +} |
| 185 | +``` |
72 | 186 |
|
73 | 187 | ## Demo |
74 | 188 |
|
75 | | -You can view a demo of this primitive here: <https://codesandbox.io/p/sandbox/amazing-easley-wqk38i?file=%2Fsrc%2Fcookies_primitive%2Findex.ts%3A36%2C20> |
| 189 | +You can view a demo of this primitive here: <https://codesandbox.io/p/sandbox/amazing-easley-wqk38i?file=%2Fsrc%2Fstart_primitive%2Findex.ts%3A36%2C20> |
76 | 190 |
|
77 | 191 | ## Changelog |
78 | 192 |
|
|
0 commit comments