@kwantis-id3/frontend-library 0.14.1 → 0.14.2
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/README.md +111 -1
- package/dist/esm/index.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -26,6 +26,7 @@ pnpm add @kwantis-id3/frontend-library
|
|
|
26
26
|
|
|
27
27
|
Note that the library uses @emotion/styled and @emotion/react as dev dependencies. Installing them in your project may cause conflicts.
|
|
28
28
|
The library however offers its own styled utility, that can be used to create styled components.
|
|
29
|
+
<br><br>
|
|
29
30
|
|
|
30
31
|
## Usage
|
|
31
32
|
The library has various components, each one with its own documentation. Please refer to the storybook documentation for more information.
|
|
@@ -51,7 +52,7 @@ The following are under development or on our roadmap:
|
|
|
51
52
|
|
|
52
53
|
|
|
53
54
|
You can find various examples of usage in the projects stories.
|
|
54
|
-
|
|
55
|
+
<br><br>
|
|
55
56
|
## Theming
|
|
56
57
|
The library internally uses the [emotion](https://emotion.sh/docs/introduction) library for styling. By default, the library uses a default theme, that looks like this:
|
|
57
58
|
|
|
@@ -103,6 +104,115 @@ declare module "@kwantis-id3/frontend-library" {
|
|
|
103
104
|
}
|
|
104
105
|
```
|
|
105
106
|
|
|
107
|
+
### Fetching the theme from an API
|
|
108
|
+
In case you fetch the theme from an API, there are a couple of things you need to do to avoid complications.
|
|
109
|
+
|
|
110
|
+
#### 1. Handling non-native theme keys
|
|
111
|
+
The library uses the `ThemeColorsObject` interface to define the theme colors. This interface is used to define the default theme, and to extend it with custom properties, as discussed in the previous section.
|
|
112
|
+
|
|
113
|
+
If you fetch the theme from an API, it is **strongly** advised to use a default theme containing the additional keys, and then override it with the fetched one.
|
|
114
|
+
|
|
115
|
+
Otherwise, the additional keys in the theme may be `undefined`, and accessing them before the theme has finished fetching will cause an error.
|
|
116
|
+
|
|
117
|
+
Here's an example on how to do it, using react query, assuming that the theme has been extended with a `newColor` property:
|
|
118
|
+
|
|
119
|
+
```ts
|
|
120
|
+
import { ThemeContextProvider, ThemeContextProps } from "@kwantis-id3/frontend-library";
|
|
121
|
+
import { useQuery } from "react-query";
|
|
122
|
+
|
|
123
|
+
const App = () => {
|
|
124
|
+
const defaultTheme: ThemeContextProps = {
|
|
125
|
+
colors: {
|
|
126
|
+
primary: { main: "#cccccc", contrastText: "#cccccc" },
|
|
127
|
+
secondary: { main: "#cccccc", contrastText: "#cccccc" },
|
|
128
|
+
tertiary: { main: "#cccccc", contrastText: "#cccccc" },
|
|
129
|
+
statusOk: { main: "#cccccc", contrastText: "#cccccc" },
|
|
130
|
+
statusWarning: { main: "#cccccc", contrastText: "#cccccc" },
|
|
131
|
+
statusCritical: { main: "#cccccc", contrastText: "#cccccc" },
|
|
132
|
+
statusNeutral: { main: "#cccccc", contrastText: "#cccccc" },
|
|
133
|
+
newColor: { main: "#cccccc", contrastText: "#cccccc" },
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const fetchedTheme = useQuery({
|
|
138
|
+
queryKey: ["theme"],
|
|
139
|
+
queryFn: getColors,
|
|
140
|
+
initialData: defaultTheme,
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
return (
|
|
144
|
+
<ThemeContextProvider theme={fetchedTheme.data}>
|
|
145
|
+
{...}
|
|
146
|
+
</ThemeContextProvider>
|
|
147
|
+
)
|
|
148
|
+
}
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
#### 2. Avoiding FOUC
|
|
152
|
+
If you fetch the theme from an API, you may experience a FOUC (Flash Of Unstyled Content) when the theme is being fetched.
|
|
153
|
+
|
|
154
|
+
This is because the default theme (either being the one native to the library, or a custom one like in the previous section) is applied before the fetched one is ready, and the two may have some differences (especially if your application needs to be deployed on various instances with different themes).
|
|
155
|
+
|
|
156
|
+
To avoid this, you have 3 possible solutions:
|
|
157
|
+
1. Do not render the application until the theme is ready
|
|
158
|
+
2. Store the theme in the local storage, and use it as the default theme. Please note that this approach will still show the FOUC in the following cases:
|
|
159
|
+
- The user has never visited the application before
|
|
160
|
+
- The user has cleared the local storage / is using a different browser or device
|
|
161
|
+
- The user has visited the application before, but the theme has changed since then
|
|
162
|
+
3. Use the `initialData` property of the `useQuery` hook to pass a default theme, since useQuery automatically caches the queries data. This approach will still show the FOUC in the following cases:
|
|
163
|
+
- The user has never visited the application before
|
|
164
|
+
- The user has cleared the local storage / is using a different browser or device
|
|
165
|
+
- The user has visited the application before, but the theme has changed since then
|
|
166
|
+
|
|
167
|
+
For case 3, the code remains unchanged from the previous section, so here's an example without react query for case 2:
|
|
168
|
+
|
|
169
|
+
```ts
|
|
170
|
+
import { ThemeContextProvider, ThemeContextProps } from "@kwantis-id3/frontend-library";
|
|
171
|
+
import React, { useEffect, useState } from "react";
|
|
172
|
+
import axios from "axios";
|
|
173
|
+
|
|
174
|
+
const App = () => {
|
|
175
|
+
const defaultTheme: ThemeContextProps = {
|
|
176
|
+
colors: {
|
|
177
|
+
primary: { main: "#cccccc", contrastText: "#cccccc" },
|
|
178
|
+
secondary: { main: "#cccccc", contrastText: "#cccccc" },
|
|
179
|
+
tertiary: { main: "#cccccc", contrastText: "#cccccc" },
|
|
180
|
+
statusOk: { main: "#cccccc", contrastText: "#cccccc" },
|
|
181
|
+
statusWarning: { main: "#cccccc", contrastText: "#cccccc" },
|
|
182
|
+
statusCritical: { main: "#cccccc", contrastText: "#cccccc" },
|
|
183
|
+
statusNeutral: { main: "#cccccc", contrastText: "#cccccc" },
|
|
184
|
+
newColor: { main: "#cccccc", contrastText: "#cccccc" },
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
const [theme, setTheme] = React.useState(() => { //initialize state with default or localstorage value
|
|
189
|
+
const localTheme = localStorage.getItem("myStoredTheme");
|
|
190
|
+
if (localTheme) {
|
|
191
|
+
return JSON.parse(localTheme) as ThemeContextProps;
|
|
192
|
+
}
|
|
193
|
+
return defaultTheme;
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
useEffect(() => {
|
|
197
|
+
axios.get<ThemeContextProps>("theme")
|
|
198
|
+
.then((response) => {
|
|
199
|
+
setTheme(response.data);
|
|
200
|
+
localStorage.setItem("myStoredTheme", JSON.stringify(response.data));
|
|
201
|
+
})
|
|
202
|
+
.catch ((error) => {
|
|
203
|
+
console.log(error);
|
|
204
|
+
})
|
|
205
|
+
}, []);
|
|
206
|
+
|
|
207
|
+
return (
|
|
208
|
+
<ThemeContextProvider theme={theme}>
|
|
209
|
+
{...}
|
|
210
|
+
</ThemeContextProvider>
|
|
211
|
+
)
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
```
|
|
215
|
+
|
|
106
216
|
## The useThemeContext hook
|
|
107
217
|
The library exports a `useThemeContext` hook, that can be used to access the theme object from any component,
|
|
108
218
|
exposing a `ThemeProperties` object.
|