@kwantis-id3/frontend-library 0.13.6 → 0.14.0

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 CHANGED
@@ -35,12 +35,12 @@ At the moment, the library contains the following components:
35
35
  - Button
36
36
  - Select (both single and multi)
37
37
  - Slider
38
+ - Textfield
38
39
 
39
40
  The following are under development or on our roadmap:
40
41
  - Label
41
42
  - Checkbox
42
43
  - Radio
43
- - Textfield
44
44
  - Datepicker
45
45
  - Timepicker
46
46
  - Datetimepicker
@@ -51,10 +51,10 @@ The following are under development or on our roadmap:
51
51
 
52
52
  You can find various examples of usage in the projects stories.
53
53
 
54
- ### Theming
54
+ ## Theming
55
55
  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:
56
56
 
57
- ```js
57
+ ```ts
58
58
  export const defaultThemeColors = {
59
59
  primary: { main: "#1d4e89", contrastText: "#ffffff" },
60
60
  secondary: { main: "#00b2ca", contrastText: "#ffffff" },
@@ -67,7 +67,7 @@ export const defaultThemeColors = {
67
67
  ```
68
68
 
69
69
  You can override the default theme by passing a custom theme to the `ThemeContextProvider` component, like this:
70
- ```js
70
+ ```ts
71
71
  import { ThemeContextProvider } from "@kwantis-id3/frontend-library";
72
72
 
73
73
  const customTheme = {
@@ -91,7 +91,7 @@ const App = () => {
91
91
 
92
92
  To add custom properties to the theme, you can extend the `ThemeColorsObject` interface in a **.d.ts** file, like this:
93
93
 
94
- ```js
94
+ ```ts
95
95
  import "@kwantis-id3/frontend-library";
96
96
 
97
97
  declare module "@kwantis-id3/frontend-library" {
@@ -102,11 +102,58 @@ declare module "@kwantis-id3/frontend-library" {
102
102
  }
103
103
  ```
104
104
 
105
- ### The Color prop
105
+ ## The useThemeContext hook
106
+ The library exports a `useThemeContext` hook, that can be used to access the theme object from any component,
107
+ exposing a `ThemeProperties` object.
108
+
109
+ ```ts
110
+ import { useThemeContext } from "@kwantis-id3/frontend-library";
111
+
112
+ const MyComponent = () => {
113
+ const theme = useThemeContext();
114
+
115
+ useEffect(() => {
116
+ console.log(theme);
117
+ }, [theme]);
118
+
119
+ return <div>...</div>;
120
+ };
121
+ ```
122
+
123
+ Through the `ThemeProperties` object, you can access the theme colors either directly, or through the `getColor(color: string) => Color` property,
124
+ that will return a valid Color based on the string parameter that it’s passed to it.
125
+
126
+ ```ts
127
+ const theme = useThemeContext();
128
+ const primaryColor = theme.colors.primary.main;
129
+ // or
130
+ const primaryColor = theme.getColor("primary");
131
+ ```
132
+
133
+ Basically, the property `getColor` can recognise if you are passing a key of the theme, and return it’s corresponding object, or if you are passing it a hex string, or a rgb string, and so on.
134
+
135
+ In the latter case, the contrastText property will automatically be generated to grant the best readability possible.
136
+
137
+ If the passed string is not a valid color, the function will default to the primary color of the theme.
138
+ ```ts
139
+ const tertiartyColor = theme.getColor("tertiary");
140
+ console.log(tertiaryColor); // { main: "#7dcfb6", contrastText: "#ffffff" }
141
+
142
+ const fuchsia = theme.getColor("fuchsia");
143
+ console.log(fuchsia); // { main: "fuchsia", contrastText: "#000000" }
144
+
145
+ const rgba = theme.getColor("rgba(130, 146, 9, 0.5)");
146
+ console.log(rgba); // { main: "rgba(130, 146, 9, 0.4)", contrastText: "#000000" }
147
+
148
+ const notValid= theme.getColor("whatever123");
149
+ console.log(notValid); // { main: "#1d4e89", contrastText: "#ffffff" }
150
+ ```
151
+
152
+ ## The Color prop
106
153
  Each component that has a **color** prop, accepts a string.
107
154
  The string can be a color name, a hex code, or a css color function.
108
155
 
109
- ```js
156
+ ```tsx
110
157
  // Color name
111
158
  <Button color="primary" />
112
159
 
@@ -117,3 +164,39 @@ The string can be a color name, a hex code, or a css color function.
117
164
  <Button color="rgb(29, 78, 137)" />
118
165
  ```
119
166
 
167
+ ## The styled utility and transientOptions
168
+ The library exports a `styled` utility, that can be used to create styled components.
169
+ It works exactly like the `styled` utility from the `@emotion/styled` library, but it automatically passes the theme to the component.
170
+
171
+ ```tsx
172
+ import { styled } from "@kwantis-id3/frontend-library";
173
+
174
+ const StyledButton = styled.button`
175
+ background-color: ${(props) => props.theme.colors.primary.main};
176
+ color: ${(props) => props.theme.colors.primary.contrastText};
177
+ `;
178
+
179
+ const MyComponent = () => {
180
+ return <StyledButton>Click me!</StyledButton>;
181
+ };
182
+ ```
183
+
184
+ It also automatically updated if the Theme interface is extended, so you don’t have to worry about updating it manually.
185
+
186
+ If you want to create a styled component with props, you can do so using the `transientOptions` utility, like this:
187
+
188
+ ```tsx
189
+ import { styled, transientOptions } from "@kwantis-id3/frontend-library";
190
+
191
+ export const StyledDiv = styled("div", transientOptions)<{
192
+ $bgColor: string;
193
+ }>`
194
+ background: ${(props) => props.$bgColor};
195
+ `;
196
+
197
+ const MyComponent = () => {
198
+ return <StyledDiv $bgColor="red">I'm red!</StyledDiv>;
199
+ };
200
+ ```
201
+ It's important that the props defined this way are prefixed with a `$` sign, to avoid conflicts.
202
+