etudes 1.2.0 → 2.1.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.
Files changed (126) hide show
  1. package/README.md +1 -1
  2. package/lib/Accordion.d.ts +80 -71
  3. package/lib/Accordion.js +173 -183
  4. package/lib/Accordion.js.map +1 -1
  5. package/lib/BurgerButton.d.ts +20 -7
  6. package/lib/BurgerButton.js +113 -20
  7. package/lib/BurgerButton.js.map +1 -1
  8. package/lib/Conditional.d.ts +2 -3
  9. package/lib/Conditional.js +0 -1
  10. package/lib/Conditional.js.map +1 -1
  11. package/lib/DebugConsole.d.ts +14 -23
  12. package/lib/DebugConsole.js +86 -87
  13. package/lib/DebugConsole.js.map +1 -1
  14. package/lib/Dial.d.ts +53 -16
  15. package/lib/Dial.js +121 -35
  16. package/lib/Dial.js.map +1 -1
  17. package/lib/Dropdown.d.ts +68 -83
  18. package/lib/Dropdown.js +226 -161
  19. package/lib/Dropdown.js.map +1 -1
  20. package/lib/Each.d.ts +2 -2
  21. package/lib/Each.js.map +1 -1
  22. package/lib/ExtractChild.d.ts +5 -4
  23. package/lib/ExtractChild.js +13 -11
  24. package/lib/ExtractChild.js.map +1 -1
  25. package/lib/ExtractChildren.d.ts +2 -2
  26. package/lib/ExtractChildren.js +7 -5
  27. package/lib/ExtractChildren.js.map +1 -1
  28. package/lib/FlatSVG.d.ts +30 -25
  29. package/lib/FlatSVG.js +28 -37
  30. package/lib/FlatSVG.js.map +1 -1
  31. package/lib/List.d.ts +97 -54
  32. package/lib/List.js +124 -63
  33. package/lib/List.js.map +1 -1
  34. package/lib/MasonryGrid.d.ts +27 -15
  35. package/lib/MasonryGrid.js +163 -148
  36. package/lib/MasonryGrid.js.map +1 -1
  37. package/lib/Panorama.d.ts +100 -21
  38. package/lib/Panorama.js +45 -44
  39. package/lib/Panorama.js.map +1 -1
  40. package/lib/PanoramaSlider.d.ts +65 -23
  41. package/lib/PanoramaSlider.js +150 -45
  42. package/lib/PanoramaSlider.js.map +1 -1
  43. package/lib/RangeSlider.d.ts +29 -89
  44. package/lib/RangeSlider.js +272 -286
  45. package/lib/RangeSlider.js.map +1 -1
  46. package/lib/Repeat.d.ts +4 -3
  47. package/lib/Repeat.js +3 -2
  48. package/lib/Repeat.js.map +1 -1
  49. package/lib/RotatingGallery.d.ts +38 -36
  50. package/lib/RotatingGallery.js +47 -17
  51. package/lib/RotatingGallery.js.map +1 -1
  52. package/lib/SelectableButton.d.ts +13 -4
  53. package/lib/SelectableButton.js +3 -14
  54. package/lib/SelectableButton.js.map +1 -1
  55. package/lib/Slider.d.ts +103 -41
  56. package/lib/Slider.js +182 -82
  57. package/lib/Slider.js.map +1 -1
  58. package/lib/StepwiseSlider.d.ts +146 -59
  59. package/lib/StepwiseSlider.js +248 -142
  60. package/lib/StepwiseSlider.js.map +1 -1
  61. package/lib/SwipeContainer.d.ts +13 -5
  62. package/lib/SwipeContainer.js +5 -15
  63. package/lib/SwipeContainer.js.map +1 -1
  64. package/lib/TextField.d.ts +1 -1
  65. package/lib/TextField.js +5 -15
  66. package/lib/TextField.js.map +1 -1
  67. package/lib/Video.d.ts +30 -51
  68. package/lib/Video.js +77 -119
  69. package/lib/Video.js.map +1 -1
  70. package/lib/WithTooltip.d.ts +16 -18
  71. package/lib/WithTooltip.js +167 -101
  72. package/lib/WithTooltip.js.map +1 -1
  73. package/lib/hooks/useDocumentTitle.d.ts +2 -1
  74. package/lib/hooks/useDocumentTitle.js +2 -1
  75. package/lib/hooks/useDocumentTitle.js.map +1 -1
  76. package/lib/hooks/useDragEffect.d.ts +26 -17
  77. package/lib/hooks/useDragEffect.js +30 -20
  78. package/lib/hooks/useDragEffect.js.map +1 -1
  79. package/lib/hooks/useElementRect.d.ts +2 -1
  80. package/lib/hooks/useElementRect.js +5 -4
  81. package/lib/hooks/useElementRect.js.map +1 -1
  82. package/lib/hooks/useInterval.d.ts +3 -3
  83. package/lib/hooks/useInterval.js +1 -1
  84. package/lib/hooks/useInterval.js.map +1 -1
  85. package/lib/hooks/useLoadImageEffect.d.ts +8 -8
  86. package/lib/hooks/useLoadImageEffect.js.map +1 -1
  87. package/lib/hooks/usePrevious.d.ts +6 -0
  88. package/lib/hooks/usePrevious.js +17 -0
  89. package/lib/hooks/usePrevious.js.map +1 -0
  90. package/lib/hooks/useResizeEffect.d.ts +3 -3
  91. package/lib/hooks/useResizeEffect.js +5 -8
  92. package/lib/hooks/useResizeEffect.js.map +1 -1
  93. package/lib/hooks/useSearchParamState.d.ts +11 -8
  94. package/lib/hooks/useSearchParamState.js +14 -10
  95. package/lib/hooks/useSearchParamState.js.map +1 -1
  96. package/lib/providers/I18nProvider.d.ts +179 -0
  97. package/lib/providers/I18nProvider.js +470 -0
  98. package/lib/providers/I18nProvider.js.map +1 -0
  99. package/lib/providers/ScrollPositionProvider.d.ts +15 -0
  100. package/lib/providers/ScrollPositionProvider.js +112 -0
  101. package/lib/providers/ScrollPositionProvider.js.map +1 -0
  102. package/lib/utils/asClassNameDict.d.ts +3 -0
  103. package/lib/utils/asClassNameDict.js +7 -0
  104. package/lib/utils/asClassNameDict.js.map +1 -0
  105. package/lib/utils/asComponentDict.d.ts +5 -0
  106. package/lib/utils/asComponentDict.js +23 -0
  107. package/lib/utils/asComponentDict.js.map +1 -0
  108. package/lib/utils/asStyleDict.d.ts +4 -0
  109. package/lib/utils/asStyleDict.js +7 -0
  110. package/lib/utils/asStyleDict.js.map +1 -0
  111. package/lib/utils/cloneStyledElement.d.ts +18 -0
  112. package/lib/utils/cloneStyledElement.js +63 -0
  113. package/lib/utils/cloneStyledElement.js.map +1 -0
  114. package/lib/utils/styles.d.ts +2 -0
  115. package/lib/utils/styles.js +22 -0
  116. package/lib/utils/styles.js.map +1 -0
  117. package/lib/utils/useDebug.d.ts +2 -0
  118. package/lib/utils/useDebug.js +46 -0
  119. package/lib/utils/useDebug.js.map +1 -0
  120. package/package.json +31 -38
  121. package/lib/AbstractSelectableCollection.d.ts +0 -94
  122. package/lib/AbstractSelectableCollection.js +0 -151
  123. package/lib/AbstractSelectableCollection.js.map +0 -1
  124. package/lib/types/index.d.ts +0 -7
  125. package/lib/types/index.js +0 -3
  126. package/lib/types/index.js.map +0 -1
@@ -0,0 +1,179 @@
1
+ import Polyglot from 'node-polyglot';
2
+ import React, { Dispatch, PropsWithChildren } from 'react';
3
+ interface Translation {
4
+ [key: string]: Translation | string;
5
+ }
6
+ type I18nState = {
7
+ changeLocaleStrategy: 'action' | 'path' | 'query';
8
+ defaultLocale: string;
9
+ locale: string;
10
+ polyglots: Record<string, Polyglot>;
11
+ supportedLocales: string[];
12
+ getLocalizedPath?: (path: string) => string;
13
+ getLocalizedString: typeof Polyglot.prototype.t;
14
+ };
15
+ type I18nContextValue = {
16
+ state: I18nState;
17
+ dispatch?: Dispatch<I18nChangeLocaleAction>;
18
+ };
19
+ type I18nProviderProps = PropsWithChildren<{
20
+ changeLocaleStrategy?: I18nState['changeLocaleStrategy'];
21
+ defaultLocale?: I18nState['defaultLocale'];
22
+ translations: Record<string, Translation>;
23
+ }>;
24
+ type I18nChangeLocaleAction = {
25
+ type: '@i18n/CHANGE_LOCALE';
26
+ locale: string;
27
+ };
28
+ export declare const I18nContext: React.Context<I18nContextValue | undefined>;
29
+ /**
30
+ * Context provider whose value consists of the current i18n state. The method
31
+ * of modifying the locale is specified by `changeLocaleStrategy`, as follows:
32
+ * - If set to `action`, the locale can be modified by dispatching an action
33
+ * - If set to `path`, the locale is inferred from the current path name
34
+ * - If set to `query`, the locale is inferred from the search parameter
35
+ * `locale` in the current path
36
+ *
37
+ * @param props - See {@link I18nProviderProps}.
38
+ *
39
+ * @returns The context provider.
40
+ */
41
+ export default function I18nProvider({ children, defaultLocale, translations, changeLocaleStrategy, }: I18nProviderProps): JSX.Element;
42
+ /** @namespace Components */
43
+ /**
44
+ * Custom {@link Routes} container that generates child {@link Route} components
45
+ * for all locales supported by {@link I18nProvider}.
46
+ */
47
+ export declare function I18nRoutes({ children }: PropsWithChildren): JSX.Element;
48
+ /** @namespace Hooks */
49
+ /**
50
+ * Hook for retrieving the change locale function.
51
+ *
52
+ * @returns The change locale function.
53
+ */
54
+ export declare function useChangeLocale(): (locale: string) => void | undefined;
55
+ /**
56
+ * Hook for retrieving the current locale.
57
+ *
58
+ * @returns The current locale.
59
+ */
60
+ export declare function useLocale(): string;
61
+ /**
62
+ * Hook for retrieving the path localizing function for the current locale.
63
+ *
64
+ * @returns The path localizing function.
65
+ */
66
+ export declare function useLocalizedPath(): (path: string) => string;
67
+ /**
68
+ * Hook for retrieving the string localizing function for the current locale.
69
+ *
70
+ * @returns The string localizing function.
71
+ */
72
+ export declare function useLocalizedString(): (phrase: string, options?: number | Polyglot.InterpolationOptions | undefined) => string;
73
+ /**
74
+ * Hook for retrieving all supported locales.
75
+ *
76
+ * @returns All supported locales.
77
+ */
78
+ export declare function useSupportedLocales(): string[];
79
+ /** @namespace Utilities */
80
+ type URLParts = {
81
+ base?: string;
82
+ hash?: string;
83
+ host?: string;
84
+ path?: string;
85
+ port?: string;
86
+ protocol?: string;
87
+ query?: string;
88
+ };
89
+ type ResolveLocaleOptions = {
90
+ /**
91
+ * The locale to fallback to if one cannot be inferred from the provided URL.
92
+ */
93
+ defaultLocale?: string;
94
+ /**
95
+ * An array of supported locales to validate the inferred locale against. If
96
+ * it doesn't exist in the list of supported locales, the default locale (if
97
+ * specified) or `undefined` will be returned.
98
+ */
99
+ supportedLocales?: string[];
100
+ };
101
+ type ResolveLocalizedURLOptions = ResolveLocaleOptions & {
102
+ /**
103
+ * Specifies where in the URL the locale should be matched. If `resolver` is
104
+ * provided, this option is ignored.
105
+ */
106
+ resolveStrategy?: 'auto' | 'domain' | 'path' | 'query';
107
+ /**
108
+ * Custom resolver function.
109
+ *
110
+ * @param protocol - The matched protocol of the provided url, if available.
111
+ * @param host - The matched host of the provided url, if available.
112
+ * @param port - The matched port of the provided url, if available.
113
+ * @param path - The matched path of the provided url, if available.
114
+ *
115
+ * @returns The resolved locale.
116
+ */
117
+ resolver?: (urlParts: URLParts) => string | undefined;
118
+ };
119
+ type LocalizedURLInfo = {
120
+ /**
121
+ * The matched locale.
122
+ */
123
+ locale: string;
124
+ /**
125
+ * Specifies where in the URL the locale was matched.
126
+ */
127
+ resolveStrategy: ResolveLocalizedURLOptions['resolveStrategy'] | 'custom';
128
+ };
129
+ /**
130
+ * Resolves the specified locale with the provided options. All parameters are
131
+ * optional.
132
+ *
133
+ * @param locale - The locale to resolve.
134
+ * @param options - See {@link ResolveLocaleOptions}.
135
+ *
136
+ * @returns - The resolved locale.
137
+ */
138
+ export declare function resolveLocale(locale?: string, { defaultLocale, supportedLocales }?: ResolveLocaleOptions): string | undefined;
139
+ /**
140
+ * Retrieves the locale identifier from a URL. The default behavior of this
141
+ * function to look for the locale identifier in the domain first followed by
142
+ * the first directory of the path. You can provide a custom resolver.
143
+ *
144
+ * @param url - The URL, can be a full URL or a valid path.
145
+ * @param options - See {@link ResolveLocalizedURLOptions}.
146
+ *
147
+ * @returns The inferred locale if it exists.
148
+ */
149
+ export declare function getLocaleFromURL(url: string, { defaultLocale, resolveStrategy, resolver, supportedLocales }?: ResolveLocalizedURLOptions): LocalizedURLInfo | undefined;
150
+ /**
151
+ * Returns the unlocalized version of a URL.
152
+ *
153
+ * @param url - The URL.
154
+ * @param options - See {@link ResolveLocalizedURLOptions}.
155
+ *
156
+ * @returns The unlocalized URL.
157
+ */
158
+ export declare function getUnlocalizedURL(url: string, { resolveStrategy, resolver, supportedLocales }?: ResolveLocalizedURLOptions): string;
159
+ /**
160
+ * Returns the localized version of a URL.
161
+ *
162
+ * @param url - The URL.
163
+ * @param locale - The target locale.
164
+ * @param options - See {@link ResolveLocalizedURLOptions}.
165
+ *
166
+ * @returns The localized URL.
167
+ */
168
+ export declare function getLocalizedURL(url: string, locale: string, { defaultLocale, resolveStrategy, resolver, supportedLocales }?: ResolveLocalizedURLOptions): string;
169
+ /**
170
+ * Returns all localized versions of a URL based on the specified
171
+ * `supportedLocales`.
172
+ *
173
+ * @param url - The URL.
174
+ * @param options - See {@link ResolveLocalizedURLOptions}.
175
+ *
176
+ * @returns The localized URLs.
177
+ */
178
+ export declare function getLocalizedURLs(url: string, { defaultLocale, resolveStrategy, resolver, supportedLocales }?: ResolveLocalizedURLOptions): string[];
179
+ export {};
@@ -0,0 +1,470 @@
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
14
+ if (k2 === undefined) k2 = k;
15
+ var desc = Object.getOwnPropertyDescriptor(m, k);
16
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
17
+ desc = { enumerable: true, get: function() { return m[k]; } };
18
+ }
19
+ Object.defineProperty(o, k2, desc);
20
+ }) : (function(o, m, k, k2) {
21
+ if (k2 === undefined) k2 = k;
22
+ o[k2] = m[k];
23
+ }));
24
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
25
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
26
+ }) : function(o, v) {
27
+ o["default"] = v;
28
+ });
29
+ var __importStar = (this && this.__importStar) || function (mod) {
30
+ if (mod && mod.__esModule) return mod;
31
+ var result = {};
32
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
33
+ __setModuleDefault(result, mod);
34
+ return result;
35
+ };
36
+ var __read = (this && this.__read) || function (o, n) {
37
+ var m = typeof Symbol === "function" && o[Symbol.iterator];
38
+ if (!m) return o;
39
+ var i = m.call(o), r, ar = [], e;
40
+ try {
41
+ while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
42
+ }
43
+ catch (error) { e = { error: error }; }
44
+ finally {
45
+ try {
46
+ if (r && !r.done && (m = i["return"])) m.call(i);
47
+ }
48
+ finally { if (e) throw e.error; }
49
+ }
50
+ return ar;
51
+ };
52
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
53
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
54
+ if (ar || !(i in from)) {
55
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
56
+ ar[i] = from[i];
57
+ }
58
+ }
59
+ return to.concat(ar || Array.prototype.slice.call(from));
60
+ };
61
+ var __importDefault = (this && this.__importDefault) || function (mod) {
62
+ return (mod && mod.__esModule) ? mod : { "default": mod };
63
+ };
64
+ Object.defineProperty(exports, "__esModule", { value: true });
65
+ exports.getLocalizedURLs = exports.getLocalizedURL = exports.getUnlocalizedURL = exports.getLocaleFromURL = exports.resolveLocale = exports.useSupportedLocales = exports.useLocalizedString = exports.useLocalizedPath = exports.useLocale = exports.useChangeLocale = exports.I18nRoutes = exports.I18nContext = void 0;
66
+ var node_polyglot_1 = __importDefault(require("node-polyglot"));
67
+ var react_1 = __importStar(require("react"));
68
+ var react_router_1 = require("react-router");
69
+ var reducer = function (state, action) {
70
+ switch (action.type) {
71
+ case '@i18n/CHANGE_LOCALE':
72
+ return __assign(__assign({}, state), { locale: action.locale, getLocalizedString: function () {
73
+ var _a, _b;
74
+ var args = [];
75
+ for (var _i = 0; _i < arguments.length; _i++) {
76
+ args[_i] = arguments[_i];
77
+ }
78
+ return (_b = (_a = state.polyglots[action.locale]) === null || _a === void 0 ? void 0 : _a.t.apply(_a, __spreadArray([], __read(args), false))) !== null && _b !== void 0 ? _b : args[0];
79
+ } });
80
+ default:
81
+ return state;
82
+ }
83
+ };
84
+ exports.I18nContext = (0, react_1.createContext)(undefined);
85
+ /**
86
+ * Context provider whose value consists of the current i18n state. The method
87
+ * of modifying the locale is specified by `changeLocaleStrategy`, as follows:
88
+ * - If set to `action`, the locale can be modified by dispatching an action
89
+ * - If set to `path`, the locale is inferred from the current path name
90
+ * - If set to `query`, the locale is inferred from the search parameter
91
+ * `locale` in the current path
92
+ *
93
+ * @param props - See {@link I18nProviderProps}.
94
+ *
95
+ * @returns The context provider.
96
+ */
97
+ function I18nProvider(_a) {
98
+ var _b;
99
+ var children = _a.children, _c = _a.defaultLocale, defaultLocale = _c === void 0 ? 'en' : _c, translations = _a.translations, _d = _a.changeLocaleStrategy, changeLocaleStrategy = _d === void 0 ? 'path' : _d;
100
+ var _e = (0, react_router_1.useLocation)(), pathname = _e.pathname, search = _e.search, hash = _e.hash;
101
+ var url = "".concat(pathname).concat(search).concat(hash);
102
+ var supportedLocales = Object.keys(translations);
103
+ if (supportedLocales.indexOf(defaultLocale) < 0) {
104
+ console.warn("Provided supported locales do not contain the default locale <".concat(defaultLocale, ">"));
105
+ supportedLocales.push(defaultLocale);
106
+ }
107
+ var polyglots = (0, react_1.useMemo)(function () { return supportedLocales.reduce(function (out, locale) {
108
+ var _a;
109
+ return (__assign(__assign({}, out), (_a = {}, _a[locale] = new node_polyglot_1.default({ locale: locale, phrases: translations[locale] }), _a)));
110
+ }, {}); }, [translations]);
111
+ switch (changeLocaleStrategy) {
112
+ case 'action': {
113
+ var _f = __read((0, react_1.useReducer)(reducer, {
114
+ changeLocaleStrategy: changeLocaleStrategy,
115
+ defaultLocale: defaultLocale,
116
+ locale: defaultLocale,
117
+ polyglots: polyglots,
118
+ supportedLocales: supportedLocales,
119
+ getLocalizedString: function () {
120
+ var _a, _b;
121
+ var args = [];
122
+ for (var _i = 0; _i < arguments.length; _i++) {
123
+ args[_i] = arguments[_i];
124
+ }
125
+ return (_b = (_a = polyglots[defaultLocale]) === null || _a === void 0 ? void 0 : _a.t.apply(_a, __spreadArray([], __read(args), false))) !== null && _b !== void 0 ? _b : args[0];
126
+ },
127
+ }), 2), state = _f[0], dispatch = _f[1];
128
+ return (react_1.default.createElement(exports.I18nContext.Provider, { value: { state: state, dispatch: dispatch } }, children));
129
+ }
130
+ default: {
131
+ var resolveStrategy_1 = changeLocaleStrategy === 'path' ? 'path' : 'query';
132
+ var localeInfo = getLocaleFromURL(url, { defaultLocale: defaultLocale, resolveStrategy: resolveStrategy_1, supportedLocales: supportedLocales });
133
+ if (!localeInfo)
134
+ console.warn("Unable to infer locale from path <".concat(url, ">"));
135
+ var locale_1 = (_b = localeInfo === null || localeInfo === void 0 ? void 0 : localeInfo.locale) !== null && _b !== void 0 ? _b : defaultLocale;
136
+ var polyglot_1 = polyglots[locale_1];
137
+ if (!polyglot_1)
138
+ console.warn("Missing transtions for locale <".concat(locale_1, ">"));
139
+ var state = {
140
+ changeLocaleStrategy: changeLocaleStrategy,
141
+ defaultLocale: defaultLocale,
142
+ locale: locale_1,
143
+ polyglots: polyglots,
144
+ supportedLocales: supportedLocales,
145
+ getLocalizedPath: function (path) { return locale_1 === defaultLocale ? getUnlocalizedURL(path, { resolveStrategy: resolveStrategy_1, supportedLocales: supportedLocales }) : getLocalizedURL(path, locale_1, { defaultLocale: defaultLocale, resolveStrategy: changeLocaleStrategy, supportedLocales: supportedLocales }); },
146
+ getLocalizedString: function () {
147
+ var _a;
148
+ var args = [];
149
+ for (var _i = 0; _i < arguments.length; _i++) {
150
+ args[_i] = arguments[_i];
151
+ }
152
+ return (_a = polyglot_1 === null || polyglot_1 === void 0 ? void 0 : polyglot_1.t.apply(polyglot_1, __spreadArray([], __read(args), false))) !== null && _a !== void 0 ? _a : args[0];
153
+ },
154
+ };
155
+ return (react_1.default.createElement(exports.I18nContext.Provider, { value: { state: state } }, children));
156
+ }
157
+ }
158
+ }
159
+ exports.default = I18nProvider;
160
+ /** @namespace Components */
161
+ /**
162
+ * Custom {@link Routes} container that generates child {@link Route} components
163
+ * for all locales supported by {@link I18nProvider}.
164
+ */
165
+ function I18nRoutes(_a) {
166
+ var children = _a.children;
167
+ var context = (0, react_1.useContext)(exports.I18nContext);
168
+ if (!context)
169
+ throw Error('Cannot fetch the value of I18nContext, is the corresponding provider instated?');
170
+ var _b = context.state, defaultLocale = _b.defaultLocale, supportedLocales = _b.supportedLocales, changeLocaleStrategy = _b.changeLocaleStrategy;
171
+ switch (changeLocaleStrategy) {
172
+ case 'path':
173
+ return (react_1.default.createElement(react_router_1.Routes, null, supportedLocales.map(function (locale) { return (react_1.default.createElement(react_router_1.Route, { key: locale, path: locale === defaultLocale ? '/' : locale }, children)); })));
174
+ default:
175
+ return (react_1.default.createElement(react_router_1.Routes, null,
176
+ react_1.default.createElement(react_router_1.Route, { path: '/' }, children)));
177
+ }
178
+ }
179
+ exports.I18nRoutes = I18nRoutes;
180
+ /** @namespace Hooks */
181
+ /**
182
+ * Hook for retrieving the change locale function.
183
+ *
184
+ * @returns The change locale function.
185
+ */
186
+ function useChangeLocale() {
187
+ var context = (0, react_1.useContext)(exports.I18nContext);
188
+ if (!context)
189
+ throw Error('Cannot fetch the current i18n context, is the corresponding provider instated?');
190
+ if (context.state.changeLocaleStrategy === 'action') {
191
+ return function (locale) {
192
+ var _a;
193
+ return (_a = context.dispatch) === null || _a === void 0 ? void 0 : _a.call(context, {
194
+ locale: locale,
195
+ type: '@i18n/CHANGE_LOCALE',
196
+ });
197
+ };
198
+ }
199
+ else {
200
+ var _a = context.state, defaultLocale_1 = _a.defaultLocale, changeLocaleStrategy = _a.changeLocaleStrategy, supportedLocales_1 = _a.supportedLocales;
201
+ var _b = (0, react_router_1.useLocation)(), pathname = _b.pathname, search = _b.search, hash = _b.hash;
202
+ var path_1 = "".concat(pathname).concat(search).concat(hash);
203
+ var navigate_1 = (0, react_router_1.useNavigate)();
204
+ var resolveStrategy_2 = changeLocaleStrategy === 'path' ? 'path' : 'query';
205
+ return function (locale) {
206
+ var newPath = locale === defaultLocale_1
207
+ ? getUnlocalizedURL(path_1, { resolveStrategy: resolveStrategy_2, supportedLocales: supportedLocales_1 })
208
+ : getLocalizedURL(path_1, locale, { defaultLocale: defaultLocale_1, resolveStrategy: resolveStrategy_2, supportedLocales: supportedLocales_1 });
209
+ navigate_1(newPath);
210
+ };
211
+ }
212
+ }
213
+ exports.useChangeLocale = useChangeLocale;
214
+ /**
215
+ * Hook for retrieving the current locale.
216
+ *
217
+ * @returns The current locale.
218
+ */
219
+ function useLocale() {
220
+ var context = (0, react_1.useContext)(exports.I18nContext);
221
+ if (!context)
222
+ throw Error('Cannot fetch the current i18n context, is the corresponding provider instated?');
223
+ return context.state.locale;
224
+ }
225
+ exports.useLocale = useLocale;
226
+ /**
227
+ * Hook for retrieving the path localizing function for the current locale.
228
+ *
229
+ * @returns The path localizing function.
230
+ */
231
+ function useLocalizedPath() {
232
+ var _a;
233
+ var context = (0, react_1.useContext)(exports.I18nContext);
234
+ if (!context)
235
+ throw Error('Cannot fetch the current i18n context, is the corresponding provider instated?');
236
+ return (_a = context.state.getLocalizedPath) !== null && _a !== void 0 ? _a : (function (path) { return path; });
237
+ }
238
+ exports.useLocalizedPath = useLocalizedPath;
239
+ /**
240
+ * Hook for retrieving the string localizing function for the current locale.
241
+ *
242
+ * @returns The string localizing function.
243
+ */
244
+ function useLocalizedString() {
245
+ var context = (0, react_1.useContext)(exports.I18nContext);
246
+ if (!context)
247
+ throw Error('Cannot fetch the current i18n context, is the corresponding provider instated?');
248
+ return context.state.getLocalizedString;
249
+ }
250
+ exports.useLocalizedString = useLocalizedString;
251
+ /**
252
+ * Hook for retrieving all supported locales.
253
+ *
254
+ * @returns All supported locales.
255
+ */
256
+ function useSupportedLocales() {
257
+ var context = (0, react_1.useContext)(exports.I18nContext);
258
+ if (!context)
259
+ throw Error('Cannot fetch the current i18n context, is the corresponding provider instated?');
260
+ return context.state.supportedLocales;
261
+ }
262
+ exports.useSupportedLocales = useSupportedLocales;
263
+ /**
264
+ * Parses a URL into parts.
265
+ *
266
+ * @param url - The URL to parse.
267
+ *
268
+ * @returns The parsed result.
269
+ */
270
+ function parseURL(url) {
271
+ var regex = /((?:(.*):\/\/)?((?:[A-Za-z0-9-]+\.?)+)?(?::([0-9]+))?)([^?#]*)(?:\?([^#]*))?(?:#(.*))?/;
272
+ var parts = url.match(regex);
273
+ if (!parts)
274
+ return {};
275
+ return {
276
+ base: parts[1],
277
+ hash: parts[7],
278
+ host: parts[3],
279
+ path: parts[5],
280
+ port: parts[4],
281
+ protocol: parts[2],
282
+ query: parts[6],
283
+ };
284
+ }
285
+ /**
286
+ * Constructs a URL from {@link URLParts}.
287
+ *
288
+ * @param parts - See {@link URLParts}.
289
+ *
290
+ * @returns The constructed URL.
291
+ */
292
+ function constructURL(parts) {
293
+ var _a, _b, _c, _d;
294
+ var protocol = (_b = (_a = parts.protocol) === null || _a === void 0 ? void 0 : _a.concat('://')) !== null && _b !== void 0 ? _b : '';
295
+ var host = (_d = (_c = parts.host) === null || _c === void 0 ? void 0 : _c.concat('/')) !== null && _d !== void 0 ? _d : '';
296
+ var port = parts.port ? ":".concat(parts.port) : '';
297
+ var path = parts.path ? "/".concat(parts.path.split('/').filter(function (t) { return t; }).join('/')) : '';
298
+ var query = parts.query ? "?".concat(parts.query) : '';
299
+ var hash = parts.hash ? "#".concat(parts.hash) : '';
300
+ return "".concat(protocol).concat(host).concat(port).concat(path).concat(query).concat(hash);
301
+ }
302
+ /**
303
+ * Resolves the specified locale with the provided options. All parameters are
304
+ * optional.
305
+ *
306
+ * @param locale - The locale to resolve.
307
+ * @param options - See {@link ResolveLocaleOptions}.
308
+ *
309
+ * @returns - The resolved locale.
310
+ */
311
+ function resolveLocale(locale, _a) {
312
+ var _b = _a === void 0 ? {} : _a, defaultLocale = _b.defaultLocale, supportedLocales = _b.supportedLocales;
313
+ if (supportedLocales) {
314
+ if (locale && supportedLocales.indexOf(locale) >= 0)
315
+ return locale;
316
+ if (defaultLocale && supportedLocales.indexOf(defaultLocale) >= 0)
317
+ return defaultLocale;
318
+ return undefined;
319
+ }
320
+ return locale !== null && locale !== void 0 ? locale : defaultLocale;
321
+ }
322
+ exports.resolveLocale = resolveLocale;
323
+ /**
324
+ * Retrieves the locale identifier from a URL. The default behavior of this
325
+ * function to look for the locale identifier in the domain first followed by
326
+ * the first directory of the path. You can provide a custom resolver.
327
+ *
328
+ * @param url - The URL, can be a full URL or a valid path.
329
+ * @param options - See {@link ResolveLocalizedURLOptions}.
330
+ *
331
+ * @returns The inferred locale if it exists.
332
+ */
333
+ function getLocaleFromURL(url, _a) {
334
+ var _b, _c;
335
+ var _d = _a === void 0 ? {} : _a, defaultLocale = _d.defaultLocale, _e = _d.resolveStrategy, resolveStrategy = _e === void 0 ? 'auto' : _e, resolver = _d.resolver, supportedLocales = _d.supportedLocales;
336
+ var parts = parseURL(url);
337
+ if (resolver) {
338
+ var matchedLocale = resolver(parts);
339
+ if (matchedLocale && (!supportedLocales || supportedLocales.indexOf(matchedLocale) >= 0))
340
+ return { locale: matchedLocale, resolveStrategy: 'custom' };
341
+ if (defaultLocale && (!supportedLocales || supportedLocales.indexOf(defaultLocale) >= 0))
342
+ return { locale: defaultLocale, resolveStrategy: 'auto' };
343
+ return undefined;
344
+ }
345
+ else {
346
+ var matchedLocaleFromHost = (_b = parts.host) === null || _b === void 0 ? void 0 : _b.split('.').filter(function (t) { return t; })[0];
347
+ var matchedLocaleFromPath = (_c = parts.path) === null || _c === void 0 ? void 0 : _c.split('/').filter(function (t) { return t; })[0];
348
+ var matchedLocaleFromQuery = new URLSearchParams(parts.query).get('locale');
349
+ if (matchedLocaleFromHost && (resolveStrategy === 'auto' || resolveStrategy === 'domain') && (!supportedLocales || supportedLocales.indexOf(matchedLocaleFromHost) >= 0))
350
+ return { locale: matchedLocaleFromHost, resolveStrategy: 'domain' };
351
+ if (matchedLocaleFromPath && (resolveStrategy === 'auto' || resolveStrategy === 'path') && (!supportedLocales || supportedLocales.indexOf(matchedLocaleFromPath) >= 0))
352
+ return { locale: matchedLocaleFromPath, resolveStrategy: 'path' };
353
+ if (matchedLocaleFromQuery && (resolveStrategy === 'auto' || resolveStrategy === 'query') && (!supportedLocales || supportedLocales.indexOf(matchedLocaleFromQuery) >= 0))
354
+ return { locale: matchedLocaleFromQuery, resolveStrategy: 'query' };
355
+ if (defaultLocale && (!supportedLocales || supportedLocales.indexOf(defaultLocale) >= 0))
356
+ return { locale: defaultLocale, resolveStrategy: 'auto' };
357
+ return undefined;
358
+ }
359
+ }
360
+ exports.getLocaleFromURL = getLocaleFromURL;
361
+ /**
362
+ * Returns the unlocalized version of a URL.
363
+ *
364
+ * @param url - The URL.
365
+ * @param options - See {@link ResolveLocalizedURLOptions}.
366
+ *
367
+ * @returns The unlocalized URL.
368
+ */
369
+ function getUnlocalizedURL(url, _a) {
370
+ var _b = _a === void 0 ? {} : _a, _c = _b.resolveStrategy, resolveStrategy = _c === void 0 ? 'auto' : _c, resolver = _b.resolver, supportedLocales = _b.supportedLocales;
371
+ var currLocaleInfo = getLocaleFromURL(url, { resolveStrategy: resolveStrategy, resolver: resolver, supportedLocales: supportedLocales });
372
+ var parts = parseURL(url);
373
+ if (!currLocaleInfo)
374
+ return url;
375
+ switch (currLocaleInfo.resolveStrategy) {
376
+ case 'domain':
377
+ return constructURL(__assign(__assign({}, parts), { host: parts.host ? parts.host.split('.').filter(function (t) { return t; }).slice(1).join('.') : undefined }));
378
+ case 'query': {
379
+ if (!parts.query)
380
+ return url;
381
+ var searchParams = new URLSearchParams(parts.query);
382
+ searchParams.delete('locale');
383
+ return constructURL(__assign(__assign({}, parts), { query: searchParams.toString() }));
384
+ }
385
+ case 'path':
386
+ case 'auto':
387
+ default:
388
+ return constructURL(__assign(__assign({}, parts), { path: parts.path ? __spreadArray([], __read(parts.path.split('/').filter(function (t) { return t; }).slice(1)), false).join('/') : undefined }));
389
+ }
390
+ }
391
+ exports.getUnlocalizedURL = getUnlocalizedURL;
392
+ /**
393
+ * Returns the localized version of a URL.
394
+ *
395
+ * @param url - The URL.
396
+ * @param locale - The target locale.
397
+ * @param options - See {@link ResolveLocalizedURLOptions}.
398
+ *
399
+ * @returns The localized URL.
400
+ */
401
+ function getLocalizedURL(url, locale, _a) {
402
+ var _b = _a === void 0 ? {} : _a, defaultLocale = _b.defaultLocale, _c = _b.resolveStrategy, resolveStrategy = _c === void 0 ? 'auto' : _c, resolver = _b.resolver, supportedLocales = _b.supportedLocales;
403
+ var currLocaleInfo = getLocaleFromURL(url, { resolveStrategy: resolveStrategy, resolver: resolver, supportedLocales: supportedLocales });
404
+ var parts = parseURL(url);
405
+ var targetLocale = resolveLocale(locale, { defaultLocale: defaultLocale, supportedLocales: supportedLocales });
406
+ if (!targetLocale)
407
+ return url;
408
+ if (targetLocale === defaultLocale)
409
+ return getUnlocalizedURL(url, { resolveStrategy: resolveStrategy, resolver: resolver, supportedLocales: supportedLocales });
410
+ if (currLocaleInfo) {
411
+ switch (currLocaleInfo.resolveStrategy) {
412
+ case 'domain':
413
+ return constructURL(__assign(__assign({}, parts), { host: parts.host ? "".concat(targetLocale, ".").concat(parts.host.split('.').filter(function (t) { return t; }).slice(1).join('.')) : undefined }));
414
+ case 'query': {
415
+ if (!parts.query)
416
+ return url;
417
+ var searchParams = new URLSearchParams(parts.query);
418
+ if (searchParams.get('locale')) {
419
+ searchParams.set('locale', targetLocale);
420
+ }
421
+ else {
422
+ searchParams.set('locale', targetLocale);
423
+ }
424
+ return constructURL(__assign(__assign({}, parts), { query: searchParams.toString() }));
425
+ }
426
+ case 'path':
427
+ case 'auto':
428
+ default:
429
+ return constructURL(__assign(__assign({}, parts), { path: parts.path ? __spreadArray([targetLocale], __read(parts.path.split('/').filter(function (t) { return t; }).slice(1)), false).join('/') : undefined }));
430
+ }
431
+ }
432
+ else {
433
+ switch (resolveStrategy) {
434
+ case 'domain':
435
+ return constructURL(__assign(__assign({}, parts), { host: parts.host ? "".concat(targetLocale, ".").concat(parts.host) : undefined }));
436
+ case 'query': {
437
+ var searchParams = new URLSearchParams(parts.query);
438
+ if (targetLocale === defaultLocale) {
439
+ searchParams.delete('locale');
440
+ }
441
+ else {
442
+ searchParams.set('locale', targetLocale);
443
+ }
444
+ return constructURL(__assign(__assign({}, parts), { query: searchParams.toString() }));
445
+ }
446
+ case 'path':
447
+ case 'auto':
448
+ default:
449
+ return constructURL(__assign(__assign({}, parts), { path: parts.path ? __spreadArray([targetLocale], __read(parts.path.split('/').filter(function (t) { return t; })), false).join('/') : undefined }));
450
+ }
451
+ }
452
+ }
453
+ exports.getLocalizedURL = getLocalizedURL;
454
+ /**
455
+ * Returns all localized versions of a URL based on the specified
456
+ * `supportedLocales`.
457
+ *
458
+ * @param url - The URL.
459
+ * @param options - See {@link ResolveLocalizedURLOptions}.
460
+ *
461
+ * @returns The localized URLs.
462
+ */
463
+ function getLocalizedURLs(url, _a) {
464
+ var _b = _a === void 0 ? {} : _a, defaultLocale = _b.defaultLocale, _c = _b.resolveStrategy, resolveStrategy = _c === void 0 ? 'auto' : _c, resolver = _b.resolver, supportedLocales = _b.supportedLocales;
465
+ if (!supportedLocales)
466
+ return [];
467
+ return supportedLocales.map(function (locale) { return getLocalizedURL(url, locale, { defaultLocale: defaultLocale, resolveStrategy: resolveStrategy, resolver: resolver, supportedLocales: supportedLocales }); });
468
+ }
469
+ exports.getLocalizedURLs = getLocalizedURLs;
470
+ //# sourceMappingURL=I18nProvider.js.map