adnbn-ui 0.2.2 → 0.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +3 -6
- package/src/index.ts +1 -1
- package/src/providers/theme/ThemeProvider.tsx +29 -6
- package/src/providers/theme/ThemeStorage.tsx +36 -0
- package/src/providers/theme/index.ts +1 -0
- package/src/providers/ui/UIProvider.tsx +6 -3
- package/src/providers/ui/styles/reset.scss +0 -1
- package/src/types/theme.ts +7 -0
- package/src/providers/theme/styles/reset.scss +0 -128
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "adnbn-ui",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.3",
|
|
4
4
|
"description": "A comprehensive React UI component library designed exclusively for the AddonBone browser extension framework with customizable theming and consistent design patterns",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"react",
|
|
@@ -41,10 +41,7 @@
|
|
|
41
41
|
"./plugin": "./src/plugin/index.ts",
|
|
42
42
|
"./theme": "./src/styles/mixins.scss"
|
|
43
43
|
},
|
|
44
|
-
"sideEffects":
|
|
45
|
-
"*.css",
|
|
46
|
-
"*.scss"
|
|
47
|
-
],
|
|
44
|
+
"sideEffects": false,
|
|
48
45
|
"scripts": {
|
|
49
46
|
"dev": "vite",
|
|
50
47
|
"build": "tsc -b && vite build",
|
|
@@ -55,7 +52,7 @@
|
|
|
55
52
|
"format": "prettier --write ."
|
|
56
53
|
},
|
|
57
54
|
"dependencies": {
|
|
58
|
-
"adnbn": "^0.1
|
|
55
|
+
"adnbn": "^0.2.1",
|
|
59
56
|
"autosize": "^6.0.1",
|
|
60
57
|
"classnames": "^2.5.1",
|
|
61
58
|
"odometer": "^0.4.8",
|
package/src/index.ts
CHANGED
|
@@ -2,24 +2,47 @@ import React, {FC, PropsWithChildren, useCallback, useEffect, useState} from "re
|
|
|
2
2
|
|
|
3
3
|
import {ThemeContext} from "./context";
|
|
4
4
|
|
|
5
|
-
import {Theme} from "../../types/theme";
|
|
5
|
+
import {Theme, ThemeStorageContract} from "../../types/theme";
|
|
6
6
|
import {Config} from "../../types/config";
|
|
7
7
|
|
|
8
8
|
const isDarkMedia = () => window?.matchMedia("(prefers-color-scheme: dark)")?.matches;
|
|
9
9
|
|
|
10
|
-
const
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
10
|
+
const isValid = (theme: Theme | undefined): theme is Theme => {
|
|
11
|
+
return !!theme && [Theme.Light, Theme.Dark].includes(theme);
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export interface ThemeProviderProps extends Pick<Config, "components"> {
|
|
15
|
+
storage?: ThemeStorageContract;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const ThemeProvider: FC<PropsWithChildren<ThemeProviderProps>> = ({children, components, storage}) => {
|
|
19
|
+
const [theme, setTheme] = useState<Theme>(() => (isDarkMedia() ? Theme.Dark : Theme.Light));
|
|
14
20
|
|
|
15
21
|
const changeTheme = useCallback((theme: Theme) => {
|
|
16
|
-
|
|
22
|
+
if (storage) {
|
|
23
|
+
storage.change(theme).catch(e => console.error("ThemeProvider: set theme to storage error", e));
|
|
24
|
+
} else {
|
|
25
|
+
setTheme(theme);
|
|
26
|
+
}
|
|
17
27
|
}, []);
|
|
18
28
|
|
|
19
29
|
const toggleTheme = useCallback(() => {
|
|
20
30
|
changeTheme(theme === Theme.Dark ? Theme.Light : Theme.Dark);
|
|
21
31
|
}, [theme, changeTheme]);
|
|
22
32
|
|
|
33
|
+
useEffect(() => {
|
|
34
|
+
if (!storage) return;
|
|
35
|
+
|
|
36
|
+
storage
|
|
37
|
+
.get()
|
|
38
|
+
.then(newTheme => isValid(newTheme) && setTheme(newTheme))
|
|
39
|
+
.catch(e => console.error("ThemeProvider: get theme from storage error", e));
|
|
40
|
+
|
|
41
|
+
const unsubscribe = storage.watch(newTheme => isValid(newTheme) && setTheme(newTheme));
|
|
42
|
+
|
|
43
|
+
return () => unsubscribe();
|
|
44
|
+
}, [storage]);
|
|
45
|
+
|
|
23
46
|
useEffect(() => {
|
|
24
47
|
document.querySelector("html")?.setAttribute("theme", theme);
|
|
25
48
|
}, [theme]);
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import {Storage} from "adnbn/storage";
|
|
2
|
+
|
|
3
|
+
import {Theme, ThemeStorageContract} from "../../types/theme";
|
|
4
|
+
|
|
5
|
+
export default class implements ThemeStorageContract {
|
|
6
|
+
private readonly storage = new Storage<Record<string, Theme>>({
|
|
7
|
+
area: "local",
|
|
8
|
+
namespace: "adnbn-ui",
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
private readonly key = "theme";
|
|
12
|
+
|
|
13
|
+
public async get(): Promise<Theme | undefined> {
|
|
14
|
+
return await this.storage.get(this.key);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
public async change(theme: Theme): Promise<void> {
|
|
18
|
+
return this.storage.set(this.key, theme);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
public async toggle(): Promise<void> {
|
|
22
|
+
let theme = await this.get();
|
|
23
|
+
|
|
24
|
+
if (!theme) {
|
|
25
|
+
theme = Theme.Dark;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
await this.change(theme === Theme.Dark ? Theme.Light : Theme.Dark);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
public watch(callback: (theme: Theme) => void): () => void {
|
|
32
|
+
return this.storage.watch({
|
|
33
|
+
[this.key]: newValue => newValue && callback(newValue),
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
}
|
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import React, {FC, PropsWithChildren} from "react";
|
|
1
|
+
import React, {FC, PropsWithChildren, useRef} from "react";
|
|
2
2
|
import {merge} from "ts-deepmerge";
|
|
3
3
|
|
|
4
|
-
import {ExtraProvider, IconsProvider, ThemeProvider} from "../index";
|
|
4
|
+
import {ExtraProvider, IconsProvider, ThemeProvider, ThemeStorage} from "../index";
|
|
5
5
|
|
|
6
|
+
import {ThemeStorageContract} from "../../types/theme";
|
|
6
7
|
import {ComponentsProps, Config, ExtraProps, Icons} from "../../types/config";
|
|
7
8
|
|
|
8
9
|
import "./styles/default.scss";
|
|
@@ -14,6 +15,8 @@ import config from "adnbn-ui-config";
|
|
|
14
15
|
export type UIProviderProps = Partial<Config>;
|
|
15
16
|
|
|
16
17
|
const UIProvider: FC<PropsWithChildren<UIProviderProps>> = ({children, components = {}, extra = {}, icons = {}}) => {
|
|
18
|
+
const storage = useRef<ThemeStorageContract>(new ThemeStorage());
|
|
19
|
+
|
|
17
20
|
const componentsProps: ComponentsProps = merge(config.components || {}, components);
|
|
18
21
|
|
|
19
22
|
const extraProps: ExtraProps = merge(config.extra || {}, extra);
|
|
@@ -21,7 +24,7 @@ const UIProvider: FC<PropsWithChildren<UIProviderProps>> = ({children, component
|
|
|
21
24
|
const svgIcons: Icons = merge(config.icons || {}, icons);
|
|
22
25
|
|
|
23
26
|
return (
|
|
24
|
-
<ThemeProvider components={componentsProps}>
|
|
27
|
+
<ThemeProvider components={componentsProps} storage={storage.current}>
|
|
25
28
|
<ExtraProvider extra={extraProps}>
|
|
26
29
|
<IconsProvider icons={svgIcons}>{children}</IconsProvider>
|
|
27
30
|
</ExtraProvider>
|
|
@@ -120,7 +120,6 @@ body {
|
|
|
120
120
|
letter-spacing: var(--letter-spacing);
|
|
121
121
|
-webkit-font-smoothing: antialiased;
|
|
122
122
|
-moz-osx-font-smoothing: grayscale;
|
|
123
|
-
overflow: hidden;
|
|
124
123
|
will-change: background-color, color;
|
|
125
124
|
transition:
|
|
126
125
|
background-color var(--transition-speed-md),
|
package/src/types/theme.ts
CHANGED
|
@@ -2,3 +2,10 @@ export enum Theme {
|
|
|
2
2
|
Light = "light",
|
|
3
3
|
Dark = "dark",
|
|
4
4
|
}
|
|
5
|
+
|
|
6
|
+
export interface ThemeStorageContract {
|
|
7
|
+
get: () => Promise<Theme | undefined>;
|
|
8
|
+
change: (theme: Theme) => Promise<void>;
|
|
9
|
+
toggle: () => Promise<void>;
|
|
10
|
+
watch: (callback: (theme: Theme) => void) => () => void;
|
|
11
|
+
}
|
|
@@ -1,128 +0,0 @@
|
|
|
1
|
-
a,
|
|
2
|
-
abbr,
|
|
3
|
-
acronym,
|
|
4
|
-
address,
|
|
5
|
-
applet,
|
|
6
|
-
article,
|
|
7
|
-
aside,
|
|
8
|
-
audio,
|
|
9
|
-
b,
|
|
10
|
-
big,
|
|
11
|
-
blockquote,
|
|
12
|
-
body,
|
|
13
|
-
canvas,
|
|
14
|
-
caption,
|
|
15
|
-
center,
|
|
16
|
-
cite,
|
|
17
|
-
code,
|
|
18
|
-
dd,
|
|
19
|
-
del,
|
|
20
|
-
details,
|
|
21
|
-
dfn,
|
|
22
|
-
div,
|
|
23
|
-
dl,
|
|
24
|
-
dt,
|
|
25
|
-
em,
|
|
26
|
-
embed,
|
|
27
|
-
fieldset,
|
|
28
|
-
figcaption,
|
|
29
|
-
figure,
|
|
30
|
-
footer,
|
|
31
|
-
form,
|
|
32
|
-
h1,
|
|
33
|
-
h2,
|
|
34
|
-
h3,
|
|
35
|
-
h4,
|
|
36
|
-
h5,
|
|
37
|
-
h6,
|
|
38
|
-
header,
|
|
39
|
-
hgroup,
|
|
40
|
-
html,
|
|
41
|
-
i,
|
|
42
|
-
iframe,
|
|
43
|
-
img,
|
|
44
|
-
ins,
|
|
45
|
-
kbd,
|
|
46
|
-
label,
|
|
47
|
-
legend,
|
|
48
|
-
li,
|
|
49
|
-
main,
|
|
50
|
-
mark,
|
|
51
|
-
menu,
|
|
52
|
-
nav,
|
|
53
|
-
object,
|
|
54
|
-
ol,
|
|
55
|
-
output,
|
|
56
|
-
p,
|
|
57
|
-
pre,
|
|
58
|
-
q,
|
|
59
|
-
ruby,
|
|
60
|
-
s,
|
|
61
|
-
samp,
|
|
62
|
-
section,
|
|
63
|
-
small,
|
|
64
|
-
span,
|
|
65
|
-
strike,
|
|
66
|
-
strong,
|
|
67
|
-
sub,
|
|
68
|
-
summary,
|
|
69
|
-
sup,
|
|
70
|
-
table,
|
|
71
|
-
tbody,
|
|
72
|
-
td,
|
|
73
|
-
tfoot,
|
|
74
|
-
th,
|
|
75
|
-
thead,
|
|
76
|
-
time,
|
|
77
|
-
tr,
|
|
78
|
-
tt,
|
|
79
|
-
u,
|
|
80
|
-
ul,
|
|
81
|
-
var,
|
|
82
|
-
video {
|
|
83
|
-
margin: 0;
|
|
84
|
-
padding: 0;
|
|
85
|
-
border: 0;
|
|
86
|
-
vertical-align: baseline;
|
|
87
|
-
outline: none !important;
|
|
88
|
-
box-sizing: border-box;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
article,
|
|
92
|
-
aside,
|
|
93
|
-
details,
|
|
94
|
-
figcaption,
|
|
95
|
-
figure,
|
|
96
|
-
footer,
|
|
97
|
-
header,
|
|
98
|
-
hgroup,
|
|
99
|
-
main,
|
|
100
|
-
menu,
|
|
101
|
-
nav,
|
|
102
|
-
section {
|
|
103
|
-
display: block;
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
input:focus,
|
|
107
|
-
select:focus,
|
|
108
|
-
textarea:focus,
|
|
109
|
-
button:focus {
|
|
110
|
-
outline: none;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
body {
|
|
114
|
-
background-color: var(--bg-primary-color);
|
|
115
|
-
color: var(--text-primary-color);
|
|
116
|
-
font-family: var(--font-family), "sans-serif";
|
|
117
|
-
font-size: var(--font-size);
|
|
118
|
-
font-weight: var(--font-weight);
|
|
119
|
-
line-height: var(--line-height);
|
|
120
|
-
letter-spacing: var(--letter-spacing);
|
|
121
|
-
-webkit-font-smoothing: antialiased;
|
|
122
|
-
-moz-osx-font-smoothing: grayscale;
|
|
123
|
-
overflow: hidden;
|
|
124
|
-
will-change: background-color, color;
|
|
125
|
-
transition:
|
|
126
|
-
background-color var(--transition-speed-md),
|
|
127
|
-
color var(--transition-speed-md);
|
|
128
|
-
}
|