@olenbetong/synergi-react 0.6.7

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.
Files changed (92) hide show
  1. package/dist/esm/ob.react.css +708 -0
  2. package/dist/esm/ob.react.js +3077 -0
  3. package/dist/esm/ob.react.min.css +2 -0
  4. package/dist/esm/ob.react.min.css.map +7 -0
  5. package/dist/esm/ob.react.min.js +8 -0
  6. package/dist/esm/ob.react.min.js.map +7 -0
  7. package/dist/iife/ob.react.css +708 -0
  8. package/dist/iife/ob.react.js +3089 -0
  9. package/dist/iife/ob.react.min.css +2 -0
  10. package/dist/iife/ob.react.min.css.map +7 -0
  11. package/dist/iife/ob.react.min.js +8 -0
  12. package/dist/iife/ob.react.min.js.map +7 -0
  13. package/dist/styles/styles.css +1 -0
  14. package/dist/styles/styles.css.map +1 -0
  15. package/es/Checkbox/index.css +1 -0
  16. package/es/ColorCard/index.css +1 -0
  17. package/es/DateNavigator/index.css +1 -0
  18. package/es/LinkedCardList/index.css +1 -0
  19. package/es/PageBanner/index.css +1 -0
  20. package/es/ProgressBar/index.css +1 -0
  21. package/es/Sidebar/index.css +1 -0
  22. package/es/Spinner/index.css +1 -0
  23. package/es/SplitContainer/index.css +1 -0
  24. package/es/ValueToggle/index.css +1 -0
  25. package/es/appframe-react/src/useDebounce.d.ts +8 -0
  26. package/es/appframe-react/src/useDebounce.js +16 -0
  27. package/es/index.css +1 -0
  28. package/es/styles/_mixins.css +0 -0
  29. package/es/styles/_variables.css +0 -0
  30. package/es/synergi-react/src/Checkbox/index.d.ts +9 -0
  31. package/es/synergi-react/src/Checkbox/index.js +41 -0
  32. package/es/synergi-react/src/ColorCard/index.d.ts +14 -0
  33. package/es/synergi-react/src/ColorCard/index.js +16 -0
  34. package/es/synergi-react/src/DateNavigator/index.d.ts +6 -0
  35. package/es/synergi-react/src/DateNavigator/index.js +51 -0
  36. package/es/synergi-react/src/LinkedCardList/index.d.ts +9 -0
  37. package/es/synergi-react/src/LinkedCardList/index.js +13 -0
  38. package/es/synergi-react/src/PageBanner/index.d.ts +27 -0
  39. package/es/synergi-react/src/PageBanner/index.js +24 -0
  40. package/es/synergi-react/src/Portal/index.d.ts +6 -0
  41. package/es/synergi-react/src/Portal/index.js +32 -0
  42. package/es/synergi-react/src/ProgressBar/index.d.ts +4 -0
  43. package/es/synergi-react/src/ProgressBar/index.js +7 -0
  44. package/es/synergi-react/src/Sidebar/index.d.ts +10 -0
  45. package/es/synergi-react/src/Sidebar/index.js +57 -0
  46. package/es/synergi-react/src/Spinner/index.d.ts +10 -0
  47. package/es/synergi-react/src/Spinner/index.js +6 -0
  48. package/es/synergi-react/src/SplitContainer/index.d.ts +20 -0
  49. package/es/synergi-react/src/SplitContainer/index.js +151 -0
  50. package/es/synergi-react/src/ValueToggle/index.d.ts +19 -0
  51. package/es/synergi-react/src/ValueToggle/index.js +38 -0
  52. package/es/synergi-react/src/index.d.ts +15 -0
  53. package/es/synergi-react/src/index.js +15 -0
  54. package/es/synergi-react/src/useDebounce.d.ts +9 -0
  55. package/es/synergi-react/src/useDebounce.js +17 -0
  56. package/es/synergi-react/src/usePersistedSetting.d.ts +7 -0
  57. package/es/synergi-react/src/usePersistedSetting.js +53 -0
  58. package/es/synergi-react/src/useSearchParamState.d.ts +13 -0
  59. package/es/synergi-react/src/useSearchParamState.js +42 -0
  60. package/es/synergi-react/src/useTranslation/index.d.ts +7 -0
  61. package/es/synergi-react/src/useTranslation/index.js +64 -0
  62. package/package.json +90 -0
  63. package/src/Checkbox/index.scss +119 -0
  64. package/src/Checkbox/index.tsx +81 -0
  65. package/src/ColorCard/index.scss +154 -0
  66. package/src/ColorCard/index.tsx +58 -0
  67. package/src/DateNavigator/index.scss +60 -0
  68. package/src/DateNavigator/index.tsx +92 -0
  69. package/src/LinkedCardList/index.scss +86 -0
  70. package/src/LinkedCardList/index.tsx +38 -0
  71. package/src/PageBanner/index.scss +99 -0
  72. package/src/PageBanner/index.tsx +79 -0
  73. package/src/Portal/index.tsx +36 -0
  74. package/src/ProgressBar/index.scss +86 -0
  75. package/src/ProgressBar/index.tsx +13 -0
  76. package/src/Sidebar/index.scss +53 -0
  77. package/src/Sidebar/index.tsx +117 -0
  78. package/src/Spinner/index.scss +71 -0
  79. package/src/Spinner/index.tsx +21 -0
  80. package/src/SplitContainer/index.scss +43 -0
  81. package/src/SplitContainer/index.tsx +271 -0
  82. package/src/ValueToggle/index.scss +50 -0
  83. package/src/ValueToggle/index.tsx +75 -0
  84. package/src/global.d.ts +2 -0
  85. package/src/index.scss +9 -0
  86. package/src/index.ts +27 -0
  87. package/src/styles/_mixins.scss +19 -0
  88. package/src/styles/_variables.scss +70 -0
  89. package/src/useDebounce.ts +21 -0
  90. package/src/usePersistedSetting.ts +78 -0
  91. package/src/useSearchParamState.ts +78 -0
  92. package/src/useTranslation/index.ts +74 -0
@@ -0,0 +1,78 @@
1
+ import { useEffect, useRef, useState } from "react";
2
+
3
+ type SearchParamValue = string | null;
4
+
5
+ /**
6
+ * A hook that wraps useState, and keeps the state value synced with
7
+ * a search parameter.
8
+ *
9
+ * @param {string} key They key used to store the value in the search parameters
10
+ * @param {SearchParamValue} fallbackValue Default value for the state. Only used if the search parameters do not contains a value for the key parameter
11
+ * @deprecated Use equivalent hook in @olenbetong/appframe-react instead
12
+ */
13
+ export function useSearchParamState(
14
+ key: string,
15
+ fallbackValue?: SearchParamValue | (() => SearchParamValue),
16
+ multiple?: false
17
+ ): [SearchParamValue, React.Dispatch<React.SetStateAction<SearchParamValue>>];
18
+ export function useSearchParamState(
19
+ key: string,
20
+ fallbackValue: string[] | (() => string[]),
21
+ multiple: true
22
+ ): [string[], React.Dispatch<React.SetStateAction<string[]>>];
23
+ export function useSearchParamState(
24
+ key: string,
25
+ fallbackValue?:
26
+ | string[]
27
+ | SearchParamValue
28
+ | (() => SearchParamValue)
29
+ | (() => string[]),
30
+ multiple: boolean = false
31
+ ):
32
+ | [SearchParamValue, React.Dispatch<React.SetStateAction<SearchParamValue>>]
33
+ | [string[], React.Dispatch<React.SetStateAction<string[]>>] {
34
+ let defaultRef = useRef<SearchParamValue | string[]>(multiple ? [] : null);
35
+ let [value, setValue] = useState<SearchParamValue | string[]>(() => {
36
+ let params = new URLSearchParams(window.location.search);
37
+ let value = multiple ? params.getAll(key) : params.get(key);
38
+
39
+ // Even if we don't use the default value here, we want to keep it for later
40
+ // so we can see if the default value is the active value. If that's the case,
41
+ // we remove the parameter from the search parameters.
42
+ if (typeof fallbackValue === "function") {
43
+ defaultRef.current = fallbackValue();
44
+ } else {
45
+ defaultRef.current = fallbackValue ?? (multiple ? [] : null);
46
+ }
47
+
48
+ if (!value) {
49
+ return defaultRef.current;
50
+ }
51
+
52
+ return value;
53
+ });
54
+
55
+ useEffect(() => {
56
+ let params = new URLSearchParams(window.location.search);
57
+
58
+ if (multiple) {
59
+ params.delete(key);
60
+ (value as string[]).forEach((v) => params.append(key, v));
61
+ } else if (value && value !== defaultRef.current) {
62
+ params.set(key, String(value));
63
+ } else {
64
+ params.delete(key);
65
+ }
66
+
67
+ let newPath = window.location.pathname;
68
+ let paramString = params.toString();
69
+ if (paramString.length) {
70
+ newPath += "?" + paramString;
71
+ }
72
+
73
+ window.history.replaceState({ path: newPath }, "", newPath);
74
+ }, [key, value]);
75
+
76
+ // @ts-ignore i give up
77
+ return [value, setValue];
78
+ }
@@ -0,0 +1,74 @@
1
+ import { useEffect, useRef, useState } from "react";
2
+
3
+ async function translateText(text: string, target: string, apiKey: string) {
4
+ let url = `https://translation.googleapis.com/language/translate/v2?key=${apiKey}`;
5
+ let body = JSON.stringify({
6
+ q: text,
7
+ target,
8
+ });
9
+
10
+ let options = {
11
+ body,
12
+ method: "POST",
13
+ };
14
+
15
+ let result = await fetch(url, options);
16
+ let json = await result.json();
17
+ let { data } = json;
18
+
19
+ if (data && data.translations) {
20
+ let [translation] = data.translations;
21
+
22
+ return {
23
+ from: translation.detectedSourceLanguage,
24
+ translation: translation.translatedText,
25
+ };
26
+ }
27
+
28
+ return false;
29
+ }
30
+
31
+ export function useTranslation(text: string, targetLanguage: string, apiKey: string) {
32
+ let [translation, setTranslation] = useState("");
33
+ let [error, setError] = useState<string | null>(null);
34
+ let [from, setSourceLang] = useState<string | null>(null);
35
+ let [isTranslating, setIsTranslating] = useState(false);
36
+ let textRef = useRef(text);
37
+
38
+ useEffect(() => {
39
+ textRef.current = text;
40
+ return () => {
41
+ setTranslation("");
42
+ setError(null);
43
+ setSourceLang(null);
44
+ setIsTranslating(false);
45
+ };
46
+ }, [apiKey, targetLanguage, text]);
47
+
48
+ async function translate() {
49
+ setIsTranslating(true);
50
+
51
+ try {
52
+ let result = await translateText(text, targetLanguage, apiKey);
53
+ if (result && textRef.current === text) {
54
+ let { from, translation } = result;
55
+ setSourceLang(from);
56
+ setTranslation(translation);
57
+ setIsTranslating(false);
58
+ }
59
+ } catch (error) {
60
+ if (textRef.current === text) {
61
+ setError((error as Error).message);
62
+ setIsTranslating(false);
63
+ }
64
+ }
65
+ }
66
+
67
+ return {
68
+ error,
69
+ from,
70
+ isTranslating,
71
+ translation,
72
+ translate,
73
+ };
74
+ }