Skip to content

Commit 10985da

Browse files
committed
example: scaffold web
1 parent 3d67547 commit 10985da

File tree

19 files changed

+1183
-9
lines changed

19 files changed

+1183
-9
lines changed

examples/example-web/.gitignore

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
pnpm-debug.log*
8+
lerna-debug.log*
9+
10+
node_modules
11+
dist
12+
dist-ssr
13+
*.local
14+
15+
# Editor directories and files
16+
.vscode/*
17+
!.vscode/extensions.json
18+
.idea
19+
.DS_Store
20+
*.suo
21+
*.ntvs*
22+
*.njsproj
23+
*.sln
24+
*.sw?
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require("@react-native-async-storage/eslint-config");

examples/example-web/index.html

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<link rel="icon" type="image/svg+xml" href="/react.svg" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<title>AsyncStorage for Web</title>
8+
</head>
9+
<body>
10+
<div id="root"></div>
11+
<script type="module" src="/src/main.tsx"></script>
12+
</body>
13+
</html>

examples/example-web/package.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "example-web",
3+
"private": true,
4+
"version": "0.0.0",
5+
"type": "module",
6+
"scripts": {
7+
"dev": "vite",
8+
"build": "tsc -b && vite build",
9+
"test:lint": "eslint .",
10+
"test:ts": "tsc --noEmit -p tsconfig.app.json",
11+
"preview": "vite preview"
12+
},
13+
"dependencies": {
14+
"@react-native-async-storage/async-storage": "workspace:*",
15+
"@react-native-async-storage/eslint-config": "workspace:*",
16+
"react": "19.1.1",
17+
"react-dom": "19.1.1"
18+
},
19+
"installConfig": {
20+
"hoistingLimits": "workspaces"
21+
},
22+
"devDependencies": {
23+
"@types/react": "19.1.10",
24+
"@types/react-dom": "19.1.7",
25+
"@vitejs/plugin-react": "5.0.0",
26+
"eslint": "9.34.0",
27+
"typescript": "5.9.2",
28+
"vite": "7.1.5"
29+
}
30+
}
Lines changed: 1 addition & 0 deletions
Loading

examples/example-web/src/App.css

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#root {
2+
max-width: 1280px;
3+
margin: 0 auto;
4+
padding: 2rem;
5+
text-align: center;
6+
}
7+
8+
.logo {
9+
height: 6em;
10+
padding: 1.5em;
11+
will-change: filter;
12+
transition: filter 300ms;
13+
}
14+
.logo:hover {
15+
filter: drop-shadow(0 0 2em #646cffaa);
16+
}
17+
.logo.react:hover {
18+
filter: drop-shadow(0 0 2em #61dafbaa);
19+
}
20+
21+
@keyframes logo-spin {
22+
from {
23+
transform: rotate(0deg);
24+
}
25+
to {
26+
transform: rotate(360deg);
27+
}
28+
}
29+
30+
@media (prefers-reduced-motion: no-preference) {
31+
a:nth-of-type(2) .logo {
32+
animation: logo-spin infinite 20s linear;
33+
}
34+
}
35+
36+
.card {
37+
padding: 2em;
38+
}
39+
40+
.read-the-docs {
41+
color: #888;
42+
}

examples/example-web/src/App.tsx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import "./App.css";
2+
import BasicCrud from "./BasicCrud";
3+
4+
function App() {
5+
return (
6+
<>
7+
<h1>AsyncStorage for web</h1>
8+
<div className="card">
9+
<BasicCrud />
10+
</div>
11+
</>
12+
);
13+
}
14+
15+
export default App;
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import type { FC } from "react";
2+
import { useState } from "react";
3+
import { createAsyncStorage } from "@react-native-async-storage/async-storage";
4+
import React from "react";
5+
6+
const STORAGE_KEY = "testing";
7+
8+
const BasicCrud: FC = () => {
9+
const [storedNumber, setStoredNumber] = useState<number | null>(null);
10+
const [storage] = useState(() => createAsyncStorage("test-web"));
11+
12+
function reportError(e: unknown) {
13+
alert(JSON.stringify(e, null, 2));
14+
}
15+
16+
async function readCurrent() {
17+
try {
18+
const value = await storage.getItem(STORAGE_KEY);
19+
setStoredNumber(value ? Number(value) : null);
20+
} catch (e: any) {
21+
reportError(e);
22+
}
23+
}
24+
25+
const increaseByTen = async () => {
26+
const newNumber = (storedNumber ?? 0) + 10;
27+
28+
try {
29+
await storage.setItem(STORAGE_KEY, `${newNumber}`);
30+
setStoredNumber(newNumber);
31+
await readCurrent();
32+
} catch (e) {
33+
reportError(e);
34+
}
35+
};
36+
37+
const removeItem = async () => {
38+
await storage.removeItem(STORAGE_KEY).catch(reportError);
39+
await readCurrent();
40+
};
41+
42+
const listAllKeys = async () => {
43+
try {
44+
const keys = await storage.getAllKeys();
45+
alert("keys: " + keys.join(", "));
46+
} catch (e) {
47+
reportError(e);
48+
}
49+
};
50+
51+
React.useEffect(() => {
52+
readCurrent();
53+
}, [storage]);
54+
55+
return (
56+
<div>
57+
<p>Currently stored: </p>
58+
<p>{storedNumber}</p>
59+
<button onClick={increaseByTen}> Increase by 10</button>
60+
<button onClick={removeItem}>remove item</button>
61+
<button onClick={listAllKeys}>list all keys</button>
62+
</div>
63+
);
64+
};
65+
66+
export default BasicCrud;
Lines changed: 1 addition & 0 deletions
Loading

examples/example-web/src/index.css

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
:root {
2+
font-family: system-ui, Avenir, Helvetica, Arial, sans-serif;
3+
line-height: 1.5;
4+
font-weight: 400;
5+
6+
color-scheme: light dark;
7+
color: rgba(255, 255, 255, 0.87);
8+
background-color: #242424;
9+
10+
font-synthesis: none;
11+
text-rendering: optimizeLegibility;
12+
-webkit-font-smoothing: antialiased;
13+
-moz-osx-font-smoothing: grayscale;
14+
}
15+
16+
a {
17+
font-weight: 500;
18+
color: #646cff;
19+
text-decoration: inherit;
20+
}
21+
a:hover {
22+
color: #535bf2;
23+
}
24+
25+
body {
26+
margin: 0;
27+
display: flex;
28+
place-items: center;
29+
min-width: 320px;
30+
min-height: 100vh;
31+
}
32+
33+
h1 {
34+
font-size: 3.2em;
35+
line-height: 1.1;
36+
}
37+
38+
button {
39+
border-radius: 8px;
40+
border: 1px solid transparent;
41+
padding: 0.6em 1.2em;
42+
font-size: 1em;
43+
font-weight: 500;
44+
font-family: inherit;
45+
background-color: #1a1a1a;
46+
cursor: pointer;
47+
transition: border-color 0.25s;
48+
}
49+
button:hover {
50+
border-color: #646cff;
51+
}
52+
button:focus,
53+
button:focus-visible {
54+
outline: 4px auto -webkit-focus-ring-color;
55+
}
56+
57+
@media (prefers-color-scheme: light) {
58+
:root {
59+
color: #213547;
60+
background-color: #ffffff;
61+
}
62+
a:hover {
63+
color: #747bff;
64+
}
65+
button {
66+
background-color: #f9f9f9;
67+
}
68+
}

0 commit comments

Comments
 (0)