Skip to content

Commit 69cd152

Browse files
committed
Rename start package to cookies
1 parent debb49f commit 69cd152

12 files changed

Lines changed: 223 additions & 1882 deletions

File tree

packages/cookies/README.md

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<p>
2+
<img width="100%" src="https://assets.solidjs.com/banner?type=Primitives&background=tiles&project=cookies" alt="Solid Primitives cookies">
3+
</p>
4+
5+
# @solid-primitives/cookies
6+
7+
[![turborepo](https://img.shields.io/badge/built%20with-turborepo-cc00ff.svg?style=for-the-badge&logo=turborepo)](https://turborepo.org/)
8+
[![size](https://img.shields.io/bundlephobia/minzip/@solid-primitives/cookies?style=for-the-badge&label=size)](https://bundlephobia.com/package/@solid-primitives/cookies)
9+
[![version](https://img.shields.io/npm/v/@solid-primitives/cookies?style=for-the-badge)](https://www.npmjs.com/package/@solid-primitives/cookies)
10+
[![stage](https://img.shields.io/endpoint?style=for-the-badge&url=https%3A%2F%2Fraw.githubusercontent.com%2Fsolidjs-community%2Fsolid-primitives%2Fmain%2Fassets%2Fbadges%2Fstage-0.json)](https://github.com/solidjs-community/solid-primitives#contribution-process)
11+
12+
A set of primitives for handling cookies in solid
13+
14+
- [`createServerCookie`](#createservercookie) - Provides a getter and setter for a reactive cookie, which works isomorphically.
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+
17+
## Installation
18+
19+
```bash
20+
npm install @solid-primitives/cookies
21+
# or
22+
yarn add @solid-primitives/cookies
23+
# or
24+
pnpm add @solid-primitives/cookies
25+
```
26+
27+
## How to use it
28+
29+
## `createServerCookie`
30+
31+
A primitive for creating a cookie that can be accessed isomorphically on the client, or the server.
32+
33+
```ts
34+
const [cookie, setCookie] = createServerCookie("cookieName");
35+
36+
cookie(); // => string | undefined
37+
```
38+
39+
### Custom serialization
40+
41+
Custom cookie serializers and deserializers can also be implemented
42+
43+
```ts
44+
const [serverCookie, setServerCookie] = createServerCookie("coolCookie", {
45+
deserialize: str => (str ? str.split(" ") : []), // Deserializes cookie into a string[]
46+
serialize: val => (val ? val.join(" ") : ""), // serializes the value back into a string
47+
});
48+
49+
serverCookie(); // => string[]
50+
```
51+
52+
## `createUserTheme`
53+
54+
Composes `createServerCookie` to provide a type safe way to store a theme and access it on the server or client.
55+
56+
```ts
57+
const [theme, setTheme] = createUserTheme("cookieName");
58+
59+
theme(); // => "light" | "dark" | undefined
60+
61+
// with default value
62+
const [theme, setTheme] = createUserTheme("cookieName", {
63+
defaultValue: "light",
64+
});
65+
66+
theme(); // => "light" | "dark"
67+
```
68+
69+
## Examples
70+
71+
Coming soon
72+
73+
## Demo
74+
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>
76+
77+
## Changelog
78+
79+
See [CHANGELOG.md](./CHANGELOG.md)

packages/cookies/dev/index.tsx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { Component, createSignal } from "solid-js";
2+
3+
const App: Component = () => {
4+
const [count, setCount] = createSignal(0);
5+
const increment = () => setCount(count() + 1);
6+
7+
return (
8+
<div class="box-border flex min-h-screen w-full flex-col items-center justify-center space-y-4 bg-gray-800 p-24 text-white">
9+
<div class="wrapper-v">
10+
<h4>Counter component</h4>
11+
<p class="caption">it's very important...</p>
12+
<button class="btn" onClick={increment}>
13+
{count()}
14+
</button>
15+
</div>
16+
</div>
17+
);
18+
};
19+
20+
export default App;
Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
{
2-
"name": "@solid-primitives/start",
3-
"version": "0.0.4",
4-
"description": "A set of primitives for Solid Start",
5-
"author": "Thomas",
2+
"name": "@solid-primitives/cookies",
3+
"version": "0.0.1",
4+
"description": "A set of primitives for handling cookies in solid",
5+
"author": "Thomas Beer (https://github.com/Tommypop2)",
66
"contributors": [
7-
"Damian Tarnawski <gthetarnav@gmail.com>"
7+
"Damian Tarnawski <gthetarnav@gmail.com>",
8+
"Seth Murphy (https://github.com/eagerestwolf)"
89
],
910
"license": "MIT",
10-
"homepage": "https://primitives.solidjs.community/package/start",
11+
"homepage": "https://primitives.solidjs.community/package/cookies",
1112
"repository": {
1213
"type": "git",
1314
"url": "git+https://github.com/solidjs-community/solid-primitives.git"
@@ -16,7 +17,7 @@
1617
"url": "https://github.com/solidjs-community/solid-primitives/issues"
1718
},
1819
"primitive": {
19-
"name": "start",
20+
"name": "cookies",
2021
"stage": 0,
2122
"list": [
2223
"createServerCookie",
@@ -27,7 +28,6 @@
2728
"keywords": [
2829
"solid",
2930
"primitives",
30-
"solid-start",
3131
"ssr"
3232
],
3333
"private": false,
@@ -59,11 +59,9 @@
5959
"test:ssr": "pnpm run vitest --mode ssr"
6060
},
6161
"peerDependencies": {
62-
"solid-js": "^1.6.12",
63-
"solid-start": ">=0.2.26"
62+
"solid-js": "^1.8.18"
6463
},
6564
"devDependencies": {
66-
"solid-js": "^1.8.7",
67-
"solid-start": "^0.3.10"
65+
"solid-js": "^1.8.18"
6866
}
6967
}
Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import { createSignal, createEffect, Signal } from "solid-js";
2-
import { isServer } from "solid-js/web";
3-
import { parseCookie } from "solid-start";
4-
import { useRequest } from "solid-start/server/ServerContext.jsx";
2+
import { getRequestEvent, isServer } from "solid-js/web";
53

64
export type MaxAgeOptions = {
75
/**
@@ -23,6 +21,18 @@ export type ServerCookieOptions<T = string> = MaxAgeOptions & {
2321

2422
const YEAR = 365 * 24 * 60 * 60;
2523

24+
/*
25+
Original code by Chakra UI
26+
MIT Licensed, Copyright (c) 2019 Segun Adebayo.
27+
28+
Credits to the Chakra UI team:
29+
https://github.com/chakra-ui/chakra-ui/blob/%40chakra-ui/toast%401.2.0/packages/color-mode/src/storage-manager.ts
30+
*/
31+
32+
export function parseCookie(cookie: string, key: string): string | undefined {
33+
return cookie.match(new RegExp(`(^| )${key}=([^;]+)`))?.[2];
34+
}
35+
2636
/**
2737
* A primitive for creating a cookie that can be accessed isomorphically on the client, or the server
2838
*
@@ -53,9 +63,10 @@ export function createServerCookie<T>(
5363

5464
const [cookie, setCookie] = createSignal(
5565
deserialize(
56-
parseCookie(isServer ? (useRequest().request.headers.get("cookie") ?? "") : document.cookie)[
57-
name
58-
],
66+
parseCookie(
67+
isServer ? getRequestEvent()?.request.headers.get("cookie") ?? "" : document.cookie,
68+
name,
69+
),
5970
),
6071
);
6172

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { describe, test, expect } from "vitest";
2+
import { createRoot } from "solid-js";
3+
import { createPrimitiveTemplate } from "../src/index.js";
4+
5+
describe("createPrimitiveTemplate", () => {
6+
test("createPrimitiveTemplate return values", () => {
7+
const { value, setValue, dispose } = createRoot(dispose => {
8+
const [value, setValue] = createPrimitiveTemplate(true);
9+
expect(value(), "initial value should be true").toBe(true);
10+
11+
return { value, setValue, dispose };
12+
});
13+
14+
setValue(false);
15+
expect(value(), "value after change should be false").toBe(false);
16+
17+
dispose();
18+
});
19+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { describe, test, expect } from "vitest";
2+
import { createPrimitiveTemplate } from "../src/index.js";
3+
4+
describe("createPrimitiveTemplate", () => {
5+
test("doesn't break in SSR", () => {
6+
const [value, setValue] = createPrimitiveTemplate(true);
7+
expect(value(), "initial value should be true").toBe(true);
8+
});
9+
});

packages/start/CHANGELOG.md

Lines changed: 0 additions & 19 deletions
This file was deleted.

0 commit comments

Comments
 (0)