@kwantis-id3/frontend-library 1.0.0-rc.5 → 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.
@@ -2,8 +2,6 @@ import React from "react";
2
2
  interface TextFieldProps extends React.InputHTMLAttributes<HTMLInputElement> {
3
3
  /** Classname given to the container div */
4
4
  containerClassName?: string;
5
- /** Disables the input */
6
- isDisabled?: boolean;
7
5
  /** The color of the input */
8
6
  color?: string;
9
7
  /** Change the styles of the input field */
@@ -0,0 +1,4 @@
1
+ /** @jsxImportSource @emotion/react */
2
+ import { FC } from "react";
3
+ import { TControlledTreeViewProps } from "./TreeViewInterfaces";
4
+ export declare const ControlledTreeView: FC<TControlledTreeViewProps>;
@@ -0,0 +1,3 @@
1
+ import { TTreeViewItem } from "./TreeViewInterfaces";
2
+ export declare const getRootItems: (items: TTreeViewItem[]) => TTreeViewItem[];
3
+ export declare const getChildrenItems: (items: TTreeViewItem[], parentId: string) => TTreeViewItem[];
@@ -0,0 +1,78 @@
1
+ import { Interpolation, Theme } from "@emotion/react";
2
+ import React from "react";
3
+ /**
4
+ * A callback to customize how items are rendered
5
+ * @property isOpen: A boolean indicating if the item is expanded
6
+ * @property hasChildren: A boolean indicating if the item has children
7
+ * @property item: the entire item object
8
+ */
9
+ export type TCustomRenderProps = {
10
+ isOpen: boolean;
11
+ item: TTreeViewItem;
12
+ };
13
+ /**
14
+ * A callback to customize how the content of the items is rendered
15
+ * @extends TCustomRenderProps
16
+ * @property icon: The icon of the item. If the renderIcon callback is provided, this will be the result of that callback
17
+ * @property title: The title of the item. If the renderTitle callback is provided, this will be the result of that callback
18
+ */
19
+ export type TRenderTriggerProps = TCustomRenderProps & {
20
+ icon: React.ReactNode;
21
+ title: React.ReactNode;
22
+ };
23
+ type TTreeItemFunctions = {
24
+ /** A callback to customize how the titles are rendered */
25
+ renderTitle?: (props: TCustomRenderProps) => React.ReactNode;
26
+ /** A callback to customize how the icons are rendered */
27
+ renderIcon?: (props: TCustomRenderProps) => React.ReactNode;
28
+ /** A callback to customize how the content of the items is rendered */
29
+ renderTrigger?: (props: TRenderTriggerProps) => React.ReactNode;
30
+ /** A callback executed when an item is focused. An item gets focused when you click on it. Focusing and item will also select it. */
31
+ onFocusItem?: (item: TTreeViewItem) => void;
32
+ /** A callback executed when an item is selected. To select an item, ctrl+click on it. */
33
+ onSelectItems?: (item: string[]) => void;
34
+ /** A callback executed when an item is expanded */
35
+ onExpandItem?: (item: TTreeViewItem) => void;
36
+ /** A callback executed when an item is collapsed */
37
+ onCollapseItem?: (item: TTreeViewItem) => void;
38
+ /** A callback to handle the expansion of items without children */
39
+ onMissingChildren?: (item: TTreeViewItem) => void;
40
+ };
41
+ type TTreeViewProps = TTreeItemFunctions & {
42
+ /** An array representing the items that the tree needs to render */
43
+ items: TTreeViewItem[];
44
+ /** Disable multi-selection possibility */
45
+ isMultiSelectionDisabled?: boolean;
46
+ /** Custom styles */
47
+ sx?: Interpolation<Theme>;
48
+ };
49
+ export type TUncontrolledTreeViewProps = TTreeViewProps & {
50
+ items: TTreeViewItem[];
51
+ };
52
+ export type TControlledTreeViewProps = TTreeViewProps & {
53
+ /** The current state of the Tree View. */
54
+ viewState: TViewState;
55
+ };
56
+ export type TViewState = {
57
+ /** The list of ids of items that are selected */
58
+ selectedItems?: string[];
59
+ /** The list of ids of items that are expanded */
60
+ expandedItems?: string[];
61
+ /** The focused item */
62
+ focusedItem?: string;
63
+ };
64
+ export type TTreeViewItem = TTreeItemFunctions & {
65
+ /** Item identifier */
66
+ id: string;
67
+ /** Item name */
68
+ name: string;
69
+ /** The possible children of this item */
70
+ children?: string[];
71
+ /** If the item should be rendered as a folder even with no children */
72
+ isFolder?: boolean;
73
+ /** A callback to fetch children asynchronously */
74
+ childrenAsync?: () => Promise<TTreeViewItem[]>;
75
+ /** Custom styles */
76
+ sx?: Interpolation<Theme>;
77
+ };
78
+ export {};
@@ -0,0 +1,24 @@
1
+ export declare const TreeView: import("@emotion/styled").StyledComponent<{
2
+ theme?: import("@emotion/react").Theme;
3
+ as?: React.ElementType;
4
+ }, import("react").DetailedHTMLProps<import("react").HTMLAttributes<HTMLDivElement>, HTMLDivElement>, {}>;
5
+ export declare const TreeItemsWrapper: import("@emotion/styled").StyledComponent<{
6
+ theme?: import("@emotion/react").Theme;
7
+ as?: React.ElementType;
8
+ }, import("react").DetailedHTMLProps<import("react").HTMLAttributes<HTMLUListElement>, HTMLUListElement>, {}>;
9
+ export declare const TreeItemTrigger: import("@emotion/styled").StyledComponent<{
10
+ theme?: import("@emotion/react").Theme;
11
+ as?: React.ElementType;
12
+ } & {
13
+ $isFocused: boolean;
14
+ $isExpanded: boolean;
15
+ $isSelected: boolean;
16
+ }, import("react").DetailedHTMLProps<import("react").LiHTMLAttributes<HTMLLIElement>, HTMLLIElement>, {}>;
17
+ export declare const TreeItemChildren: import("@emotion/styled").StyledComponent<{
18
+ theme?: import("@emotion/react").Theme;
19
+ as?: React.ElementType;
20
+ } & import("react").ClassAttributes<HTMLUListElement> & import("react").HTMLAttributes<HTMLUListElement> & {
21
+ theme?: import("@emotion/react").Theme;
22
+ } & {
23
+ $isExpanded: boolean;
24
+ }, {}, {}>;
@@ -0,0 +1,3 @@
1
+ import { FC } from "react";
2
+ import { TUncontrolledTreeViewProps } from "./TreeViewInterfaces";
3
+ export declare const UncontrolledTreeView: FC<TUncontrolledTreeViewProps>;
@@ -0,0 +1,3 @@
1
+ export * from "./TreeViewInterfaces";
2
+ export * from "./ControlledTreeView";
3
+ export * from "./UncontrolledTreeView";
@@ -11,3 +11,4 @@ export { Tag, TTagProps } from "./Tag";
11
11
  export { Card, CardHeader, CardContent, CardFooter, CardIndicators, CardMediaSection, TCardSectionProps, } from "./Card";
12
12
  export { Table, renderCell, TTableProps, TTableCell, TCellValues, TTableRow, } from "./Table";
13
13
  export { MultiViewList, TMultiViewListProps } from "./MultiViewList";
14
+ export { ControlledTreeView, UncontrolledTreeView, TControlledTreeViewProps, TUncontrolledTreeViewProps, TViewState, TTreeViewItem, TCustomRenderProps, } from "./TreeView";
@@ -6,15 +6,25 @@
6
6
  export declare const getContrastColor: (color: string) => string;
7
7
  /**
8
8
  *
9
- * @param color the color to darken
9
+ * @param color a color
10
10
  * @param strength the strength (0-100) to darken the color
11
11
  * @returns the color darkened by the strength
12
12
  */
13
13
  export declare const darkenColor: (color: string, strength?: number) => string;
14
14
  /**
15
15
  *
16
- * @param color the color to lighten
16
+ * @param color a color
17
17
  * @param strength the strength (0-100) to lighten the color
18
18
  * @returns the color lightened by the strength
19
19
  */
20
20
  export declare const lightenColor: (color: string, strength?: number) => string;
21
+ /**
22
+ * @param color a color
23
+ * @returns the hover color, based on if the color is dark or light
24
+ */
25
+ export declare const getHoverColor: (color: string) => string;
26
+ /**
27
+ * @param color a color
28
+ * @returns the active color, based on if the color is dark or light
29
+ */
30
+ export declare const getActiveColor: (color: string) => string;
@@ -2,6 +2,4 @@ import useIsMobile from "./isMobile";
2
2
  export { transientOptions } from "./transientOptions";
3
3
  export { styled } from "./styled";
4
4
  export { useIsMobile };
5
- export { getContrastColor } from "./colors";
6
- export { darkenColor } from "./colors";
7
- export { lightenColor } from "./colors";
5
+ export { getContrastColor, darkenColor, lightenColor, getHoverColor, getActiveColor } from "./colors";
package/dist/index.d.ts CHANGED
@@ -169,8 +169,6 @@ declare const Accordion: React__default.FC<TAccordionProps>;
169
169
  interface TextFieldProps extends React__default.InputHTMLAttributes<HTMLInputElement> {
170
170
  /** Classname given to the container div */
171
171
  containerClassName?: string;
172
- /** Disables the input */
173
- isDisabled?: boolean;
174
172
  /** The color of the input */
175
173
  color?: string;
176
174
  /** Change the styles of the input field */
@@ -447,6 +445,88 @@ type TMultiViewListProps<Cell extends TCellValues, Row extends TTableRow<Cell>>
447
445
 
448
446
  declare const MultiViewList: <Cell extends TCellValues, Row extends TTableRow<Cell>>(props: TMultiViewListProps<Cell, Row>) => _emotion_react_jsx_runtime.JSX.Element;
449
447
 
448
+ /**
449
+ * A callback to customize how items are rendered
450
+ * @property isOpen: A boolean indicating if the item is expanded
451
+ * @property hasChildren: A boolean indicating if the item has children
452
+ * @property item: the entire item object
453
+ */
454
+ type TCustomRenderProps = {
455
+ isOpen: boolean;
456
+ item: TTreeViewItem;
457
+ };
458
+ /**
459
+ * A callback to customize how the content of the items is rendered
460
+ * @extends TCustomRenderProps
461
+ * @property icon: The icon of the item. If the renderIcon callback is provided, this will be the result of that callback
462
+ * @property title: The title of the item. If the renderTitle callback is provided, this will be the result of that callback
463
+ */
464
+ type TRenderTriggerProps = TCustomRenderProps & {
465
+ icon: React__default.ReactNode;
466
+ title: React__default.ReactNode;
467
+ };
468
+ type TTreeItemFunctions = {
469
+ /** A callback to customize how the titles are rendered */
470
+ renderTitle?: (props: TCustomRenderProps) => React__default.ReactNode;
471
+ /** A callback to customize how the icons are rendered */
472
+ renderIcon?: (props: TCustomRenderProps) => React__default.ReactNode;
473
+ /** A callback to customize how the content of the items is rendered */
474
+ renderTrigger?: (props: TRenderTriggerProps) => React__default.ReactNode;
475
+ /** A callback executed when an item is focused. An item gets focused when you click on it. Focusing and item will also select it. */
476
+ onFocusItem?: (item: TTreeViewItem) => void;
477
+ /** A callback executed when an item is selected. To select an item, ctrl+click on it. */
478
+ onSelectItems?: (item: string[]) => void;
479
+ /** A callback executed when an item is expanded */
480
+ onExpandItem?: (item: TTreeViewItem) => void;
481
+ /** A callback executed when an item is collapsed */
482
+ onCollapseItem?: (item: TTreeViewItem) => void;
483
+ /** A callback to handle the expansion of items without children */
484
+ onMissingChildren?: (item: TTreeViewItem) => void;
485
+ };
486
+ type TTreeViewProps = TTreeItemFunctions & {
487
+ /** An array representing the items that the tree needs to render */
488
+ items: TTreeViewItem[];
489
+ /** Disable multi-selection possibility */
490
+ isMultiSelectionDisabled?: boolean;
491
+ /** Custom styles */
492
+ sx?: Interpolation$1<Theme>;
493
+ };
494
+ type TUncontrolledTreeViewProps = TTreeViewProps & {
495
+ items: TTreeViewItem[];
496
+ };
497
+ type TControlledTreeViewProps = TTreeViewProps & {
498
+ /** The current state of the Tree View. */
499
+ viewState: TViewState;
500
+ };
501
+ type TViewState = {
502
+ /** The list of ids of items that are selected */
503
+ selectedItems?: string[];
504
+ /** The list of ids of items that are expanded */
505
+ expandedItems?: string[];
506
+ /** The focused item */
507
+ focusedItem?: string;
508
+ };
509
+ type TTreeViewItem = TTreeItemFunctions & {
510
+ /** Item identifier */
511
+ id: string;
512
+ /** Item name */
513
+ name: string;
514
+ /** The possible children of this item */
515
+ children?: string[];
516
+ /** If the item should be rendered as a folder even with no children */
517
+ isFolder?: boolean;
518
+ /** A callback to fetch children asynchronously */
519
+ childrenAsync?: () => Promise<TTreeViewItem[]>;
520
+ /** Custom styles */
521
+ sx?: Interpolation$1<Theme>;
522
+ };
523
+
524
+ /** @jsxImportSource @emotion/react */
525
+
526
+ declare const ControlledTreeView: FC<TControlledTreeViewProps>;
527
+
528
+ declare const UncontrolledTreeView: FC<TUncontrolledTreeViewProps>;
529
+
450
530
  /**
451
531
  * Hook to check if the screen is mobile. The default breakpoint is 768px.
452
532
  * @param {number=} mobileBreakpoint breakpoint width to check against
@@ -473,17 +553,27 @@ declare const styled: CreateStyled;
473
553
  declare const getContrastColor: (color: string) => string;
474
554
  /**
475
555
  *
476
- * @param color the color to darken
556
+ * @param color a color
477
557
  * @param strength the strength (0-100) to darken the color
478
558
  * @returns the color darkened by the strength
479
559
  */
480
560
  declare const darkenColor: (color: string, strength?: number) => string;
481
561
  /**
482
562
  *
483
- * @param color the color to lighten
563
+ * @param color a color
484
564
  * @param strength the strength (0-100) to lighten the color
485
565
  * @returns the color lightened by the strength
486
566
  */
487
567
  declare const lightenColor: (color: string, strength?: number) => string;
568
+ /**
569
+ * @param color a color
570
+ * @returns the hover color, based on if the color is dark or light
571
+ */
572
+ declare const getHoverColor: (color: string) => string;
573
+ /**
574
+ * @param color a color
575
+ * @returns the active color, based on if the color is dark or light
576
+ */
577
+ declare const getActiveColor: (color: string) => string;
488
578
 
489
- export { Accordion, Button, ButtonProps, Card, CardContent, CardFooter, CardHeader, CardIndicators, CardMediaSection, Dropdown, DropdownItem, DropdownProps, IBackgroundColors, ICommonColors, IIndicatorColors, IPalette, IStandardPaletteColor, ITagColors, IThemeContextProps, IThemeContextValue, Indicator, InputField, Modal, MultiSelect, MultiSelectProps, MultiViewList, SingleSelect, SingleSelectProps, Slider, SliderProps, TAccordionProps, TCardSectionProps, TCellValues, TColorScale, TIndicatorProps, TIndicatorVariants, TMultiViewListProps, TTableCell, TTableProps, TTableRow, TTagProps, TThemeMode, Table, Tag, ThemeContextProvider, darkenColor, defaultDarkPalette, defaultLightPalette, getContrastColor, lightenColor, renderCell, styled, transientOptions, useIsMobile, useThemeContext };
579
+ export { Accordion, Button, ButtonProps, Card, CardContent, CardFooter, CardHeader, CardIndicators, CardMediaSection, ControlledTreeView, Dropdown, DropdownItem, DropdownProps, IBackgroundColors, ICommonColors, IIndicatorColors, IPalette, IStandardPaletteColor, ITagColors, IThemeContextProps, IThemeContextValue, Indicator, InputField, Modal, MultiSelect, MultiSelectProps, MultiViewList, SingleSelect, SingleSelectProps, Slider, SliderProps, TAccordionProps, TCardSectionProps, TCellValues, TColorScale, TControlledTreeViewProps, TCustomRenderProps, TIndicatorProps, TIndicatorVariants, TMultiViewListProps, TTableCell, TTableProps, TTableRow, TTagProps, TThemeMode, TTreeViewItem, TUncontrolledTreeViewProps, TViewState, Table, Tag, ThemeContextProvider, UncontrolledTreeView, darkenColor, defaultDarkPalette, defaultLightPalette, getActiveColor, getContrastColor, getHoverColor, lightenColor, renderCell, styled, transientOptions, useIsMobile, useThemeContext };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kwantis-id3/frontend-library",
3
- "version": "1.0.0-rc.5",
3
+ "version": "1.0.0-rc.6",
4
4
  "description": "Kwantis frontend components collection",
5
5
  "scriptsComments": {
6
6
  "storybook": "Starts storybook in development mode",
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
-