@kwantis-id3/frontend-library 1.0.0-rc.4 → 1.0.0-rc.6

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 DELETED
@@ -1,333 +0,0 @@
1
- # Hi! This is Kwantis Frontend Library
2
-
3
- This project contains a collection of reusable components, developed for the kwantis' software ecosystem.
4
-
5
- To import the library inside your project, please follow the instructions below.
6
-
7
- ## Installation
8
-
9
- With npm
10
-
11
- ```bash
12
- npm install @kwantis-id3/frontend-library
13
- ```
14
-
15
- With yarn
16
-
17
- ```bash
18
- yarn add @kwantis-id3/frontend-library
19
- ```
20
-
21
- With pnpm
22
-
23
- ```bash
24
- pnpm add @kwantis-id3/frontend-library
25
- ```
26
-
27
- Note that the library uses @emotion/styled and @emotion/react as dev dependencies. Installing them in your project may cause conflicts.
28
- The library however offers its own styled utility, that can be used to create styled components.
29
- <br><br>
30
-
31
- ## Usage
32
- The library has various components, each one with its own documentation. Please refer to the storybook documentation for more information.
33
-
34
- ### Components List
35
- At the moment, the library contains the following components:
36
- - Accordion
37
- - Button
38
- - Select (both single and multi)
39
- - Slider
40
- - InputField, can be used as
41
- - Datepicker
42
- - Timepicker
43
- - Datetimepicker
44
- - TextField
45
- - NumberField
46
- - Text alternatives (email, password, url, tel, etc.)
47
-
48
- - Multilevel Dropdown
49
- - Modal
50
-
51
- The following are under development or on our roadmap in no particular order:
52
- - Label
53
- - Checkbox
54
- - Radio
55
- - ~~Datepicker~~ (supported by InputField)
56
- - ~~Timepicker~~ (supported by InputField)
57
- - ~~Datetimepicker~~ (supported by InputField)
58
- - Table
59
- - Tabs
60
-
61
-
62
- You can find various examples of usage in the projects stories.
63
- <br><br>
64
- ## Theming
65
- 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:
66
-
67
- ```ts
68
- export const defaultThemeColors = {
69
- primary: { main: "#1d4e89", contrastText: "#ffffff" },
70
- secondary: { main: "#00b2ca", contrastText: "#ffffff" },
71
- tertiary: { main: "#7dcfb6", contrastText: "#ffffff" },
72
- statusOk: { main: "#00e200", contrastText: "#ffffff" },
73
- statusWarning: { main: "#efff00", contrastText: "#ffffff" },
74
- statusCritical: { main: "#ff3838", contrastText: "#ffffff" },
75
- statusNeutral: { main: "#7b8089", contrastText: "#ffffff" },
76
- };
77
- ```
78
-
79
- You can override the default theme by passing a custom theme to the `ThemeContextProvider` component, like this:
80
- ```ts
81
- import { ThemeContextProvider } from "@kwantis-id3/frontend-library";
82
-
83
- const customTheme = {
84
- primary: { main: "#1d4e89", contrastText: "#ffffff" },
85
- secondary: { main: "#00b2ca", contrastText: "#ffffff" },
86
- tertiary: { main: "#7dcfb6", contrastText: "#ffffff" },
87
- statusOk: { main: "#00e200", contrastText: "#ffffff" },
88
- statusWarning: { main: "#efff00", contrastText: "#ffffff" },
89
- statusCritical: { main: "#ff3838", contrastText: "#ffffff" },
90
- statusNeutral: { main: "#7b8089", contrastText: "#ffffff" },
91
- };
92
-
93
- const App = () => {
94
- return (
95
- <ThemeContextProvider theme={customTheme}>
96
- <YourApp />
97
- </ThemeContextProvider>
98
- );
99
- };
100
- ```
101
-
102
- To add custom properties to the theme, you can extend the `ThemeColorsObject` interface in a **.d.ts** file, like this:
103
-
104
- ```ts
105
- import "@kwantis-id3/frontend-library";
106
-
107
- declare module "@kwantis-id3/frontend-library" {
108
- interface ThemeColorsObject {
109
- newColor: Color;
110
- newColor2: Color;
111
- }
112
- }
113
- ```
114
-
115
- ### Fetching the theme from an API
116
- In case you fetch the theme from an API, there are a couple of things you need to do to avoid complications.
117
-
118
- #### 1. Handling non-native theme keys
119
- 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.
120
-
121
- 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.
122
-
123
- Otherwise, the additional keys in the theme may be `undefined`, and accessing them before the theme has finished fetching will cause an error.
124
-
125
- Here's an example on how to do it, using react query, assuming that the theme has been extended with a `newColor` property:
126
-
127
- ```ts
128
- import { ThemeContextProvider, ThemeContextProps } from "@kwantis-id3/frontend-library";
129
- import { useQuery } from "react-query";
130
-
131
- const App = () => {
132
- const defaultTheme: ThemeContextProps = {
133
- colors: {
134
- primary: { main: "#cccccc", contrastText: "#cccccc" },
135
- secondary: { main: "#cccccc", contrastText: "#cccccc" },
136
- tertiary: { main: "#cccccc", contrastText: "#cccccc" },
137
- statusOk: { main: "#cccccc", contrastText: "#cccccc" },
138
- statusWarning: { main: "#cccccc", contrastText: "#cccccc" },
139
- statusCritical: { main: "#cccccc", contrastText: "#cccccc" },
140
- statusNeutral: { main: "#cccccc", contrastText: "#cccccc" },
141
- newColor: { main: "#cccccc", contrastText: "#cccccc" },
142
- }
143
- }
144
-
145
- const fetchedTheme = useQuery({
146
- queryKey: ["theme"],
147
- queryFn: getColors,
148
- initialData: defaultTheme,
149
- });
150
-
151
- return (
152
- <ThemeContextProvider theme={fetchedTheme.data}>
153
- {...}
154
- </ThemeContextProvider>
155
- )
156
- }
157
- ```
158
-
159
- #### 2. Avoiding FOUC
160
- If you fetch the theme from an API, you may experience a FOUC (Flash Of Unstyled Content) when the theme is being fetched.
161
-
162
- 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).
163
-
164
- To avoid this, you have 3 possible solutions:
165
- 1. Do not render the application until the theme is ready
166
- 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:
167
- - The user has never visited the application before
168
- - The user has cleared the local storage / is using a different browser or device
169
- - The user has visited the application before, but the theme has changed since then
170
- 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:
171
- - The user has never visited the application before
172
- - The user has cleared the local storage / is using a different browser or device
173
- - The user has visited the application before, but the theme has changed since then
174
-
175
- For case 3, the code remains unchanged from the previous section, so here's an example without react query for case 2:
176
-
177
- ```ts
178
- import { ThemeContextProvider, ThemeContextProps } from "@kwantis-id3/frontend-library";
179
- import React, { useEffect, useState } from "react";
180
- import axios from "axios";
181
-
182
- const App = () => {
183
- const defaultTheme: ThemeContextProps = {
184
- colors: {
185
- primary: { main: "#cccccc", contrastText: "#cccccc" },
186
- secondary: { main: "#cccccc", contrastText: "#cccccc" },
187
- tertiary: { main: "#cccccc", contrastText: "#cccccc" },
188
- statusOk: { main: "#cccccc", contrastText: "#cccccc" },
189
- statusWarning: { main: "#cccccc", contrastText: "#cccccc" },
190
- statusCritical: { main: "#cccccc", contrastText: "#cccccc" },
191
- statusNeutral: { main: "#cccccc", contrastText: "#cccccc" },
192
- newColor: { main: "#cccccc", contrastText: "#cccccc" },
193
- }
194
- }
195
-
196
- const [theme, setTheme] = React.useState(() => { //initialize state with default or localstorage value
197
- const localTheme = localStorage.getItem("myStoredTheme");
198
- if (localTheme) {
199
- return JSON.parse(localTheme) as ThemeContextProps;
200
- }
201
- return defaultTheme;
202
- });
203
-
204
- useEffect(() => {
205
- axios.get<ThemeContextProps>("theme")
206
- .then((response) => {
207
- setTheme(response.data);
208
- localStorage.setItem("myStoredTheme", JSON.stringify(response.data));
209
- })
210
- .catch ((error) => {
211
- console.log(error);
212
- })
213
- }, []);
214
-
215
- return (
216
- <ThemeContextProvider theme={theme}>
217
- {...}
218
- </ThemeContextProvider>
219
- )
220
- }
221
-
222
- ```
223
-
224
- ## The useThemeContext hook
225
- The library exports a `useThemeContext` hook, that can be used to access the theme object from any component,
226
- exposing a `ThemeProperties` object.
227
-
228
- ```ts
229
- import { useThemeContext } from "@kwantis-id3/frontend-library";
230
-
231
- const MyComponent = () => {
232
- const theme = useThemeContext();
233
-
234
- useEffect(() => {
235
- console.log(theme);
236
- }, [theme]);
237
-
238
- return <div>...</div>;
239
- };
240
- ```
241
-
242
- Through the `ThemeProperties` object, you can access the theme colors either directly, or through the `getColor(color: string) => Color` property,
243
- that will return a valid Color based on the string parameter that it’s passed to it.
244
-
245
- ```ts
246
- const theme = useThemeContext();
247
- const primaryColor = theme.colors.primary.main;
248
- // or
249
- const primaryColor = theme.getColor("primary");
250
- ```
251
-
252
- Basically, the property `getColor` can be used to:
253
- - Access the theme colors, in 2 ways:
254
- - Access the color directly, if the passed string is a valid key of the `ThemeColorsObject` interface. This is the same as accessing the color directly from the `colors` property, giving you access to the possible custom properties that you may have added to the theme.
255
- - Access the color through a path, if the passed string is a valid path of the `ThemeColorsObject` interface. This returns a `Color` object, that has only the `main` and `contrastText` properties, where `main` is the color that you are accessing, and `contrastText` is generated automatically to grant the best readability possible.
256
- - Generate a valid `Color` object, if the passed string is a valid hex code or css color function
257
-
258
-
259
-
260
- In the latter case, the contrastText property will automatically be generated to grant the best readability possible.
261
-
262
- If the passed string is not a valid color, the function will default to the primary color of the theme.
263
- ```ts
264
- const tertiartyColor = theme.getColor("tertiary");
265
- console.log(tertiaryColor); // { main: "#7dcfb6", contrastText: "#ffffff" }
266
-
267
- const tertiartyTextColor = theme.getColor("tertiary.contrastText");
268
- console.log(tertiaryTextColor); // { main: "#ffffff", contrastText: "#000000"}
269
-
270
- const fuchsia = theme.getColor("fuchsia");
271
- console.log(fuchsia); // { main: "fuchsia", contrastText: "#000000" }
272
-
273
- const rgba = theme.getColor("rgba(130, 146, 9, 0.5)");
274
- console.log(rgba); // { main: "rgba(130, 146, 9, 0.4)", contrastText: "#000000" }
275
-
276
- const notValid= theme.getColor("whatever123");
277
- console.log(notValid); // { main: "#1d4e89", contrastText: "#ffffff" }
278
- ```
279
-
280
- ## The Color prop
281
- Each component that has a **color** prop, accepts a string.
282
- The string can be a color name, a color path, a hex code, or a css color function.
283
-
284
- ```tsx
285
- // Color name
286
- <Button color="primary" />
287
-
288
- // Color path
289
- <Button color="primary.contrastText" />
290
-
291
- // Hex code
292
- <Button color="#1d4e89" />
293
-
294
- // Css color function
295
- <Button color="rgb(29, 78, 137)" />
296
- ```
297
-
298
- ## The styled utility and transientOptions
299
- The library exports a `styled` utility, that can be used to create styled components.
300
- It works exactly like the `styled` utility from the `@emotion/styled` library, but it automatically passes the theme to the component.
301
-
302
- ```tsx
303
- import { styled } from "@kwantis-id3/frontend-library";
304
-
305
- const StyledButton = styled.button`
306
- background-color: ${(props) => props.theme.colors.primary.main};
307
- color: ${(props) => props.theme.colors.primary.contrastText};
308
- `;
309
-
310
- const MyComponent = () => {
311
- return <StyledButton>Click me!</StyledButton>;
312
- };
313
- ```
314
-
315
- It also automatically updated if the Theme interface is extended, so you don’t have to worry about updating it manually.
316
-
317
- If you want to create a styled component with props, you can do so using the `transientOptions` utility, like this:
318
-
319
- ```tsx
320
- import { styled, transientOptions } from "@kwantis-id3/frontend-library";
321
-
322
- export const StyledDiv = styled("div", transientOptions)<{
323
- $bgColor: string;
324
- }>`
325
- background: ${(props) => props.$bgColor};
326
- `;
327
-
328
- const MyComponent = () => {
329
- return <StyledDiv $bgColor="red">I'm red!</StyledDiv>;
330
- };
331
- ```
332
- It's important that the props defined this way are prefixed with a `$` sign, to avoid conflicts with the native HTML props.
333
-