generated from chibat/chrome-extension-typescript-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.tsx
77 lines (71 loc) · 1.84 KB
/
options.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import React, { useEffect, useState } from "react";
import { createRoot } from "react-dom/client";
const Options = () => {
const [color, setColor] = useState<string>("");
const [status, setStatus] = useState<string>("");
const [like, setLike] = useState<boolean>(false);
useEffect(() => {
// Restores select box and checkbox state using the preferences
// stored in chrome.storage.
chrome.storage.sync.get(
{
favoriteColor: "red",
likesColor: true,
},
(items) => {
setColor(items.favoriteColor);
setLike(items.likesColor);
}
);
}, []);
const saveOptions = () => {
// Saves options to chrome.storage.sync.
chrome.storage.sync.set(
{
favoriteColor: color,
likesColor: like,
},
() => {
// Update status to let user know options were saved.
setStatus("Options saved.");
const id = setTimeout(() => {
setStatus("");
}, 1000);
return () => clearTimeout(id);
}
);
};
return (
<>
<div>
Favorite color: <select
value={color}
onChange={(event) => setColor(event.target.value)}
>
<option value="red">red</option>
<option value="green">green</option>
<option value="blue">blue</option>
<option value="yellow">yellow</option>
</select>
</div>
<div>
<label>
<input
type="checkbox"
checked={like}
onChange={(event) => setLike(event.target.checked)}
/>
I like colors.
</label>
</div>
<div>{status}</div>
<button onClick={saveOptions}>Save</button>
</>
);
};
const root = createRoot(document.getElementById("root")!);
root.render(
<React.StrictMode>
<Options />
</React.StrictMode>
);