@arcblock/ux 2.6.8 → 2.7.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 (164) hide show
  1. package/babel.config.es.js +12 -0
  2. package/es/ActionButton/index.js +99 -0
  3. package/es/ActivityIndicator/index.js +180 -0
  4. package/es/Address/compact-text.js +105 -0
  5. package/es/Address/did-address.js +211 -0
  6. package/es/Address/index.js +23 -0
  7. package/es/Address/responsive-did-address.js +88 -0
  8. package/es/Alert/index.js +134 -0
  9. package/es/AnimationWaiter/default-animation.json +1 -0
  10. package/es/AnimationWaiter/index.js +239 -0
  11. package/es/Async/index.js +38 -0
  12. package/es/Avatar/did-motif.js +64 -0
  13. package/es/Avatar/etherscan-blockies.js +83 -0
  14. package/es/Avatar/index.js +176 -0
  15. package/es/Badge/index.js +98 -0
  16. package/es/Blocklet/blocklet.js +298 -0
  17. package/es/Blocklet/index.js +4 -0
  18. package/es/Blocklet/utils.js +52 -0
  19. package/es/BlockletNFT/index.js +412 -0
  20. package/es/Button/index.js +8 -0
  21. package/es/Button/wrap.js +140 -0
  22. package/es/ButtonGroup/index.js +6 -0
  23. package/es/CardSelector/index.js +131 -0
  24. package/es/Center/index.js +41 -0
  25. package/es/ClickToCopy/copy-button.js +72 -0
  26. package/es/ClickToCopy/hook.js +39 -0
  27. package/es/ClickToCopy/index.js +93 -0
  28. package/es/CodeBlock/LightBox.js +85 -0
  29. package/es/CodeBlock/index.js +226 -0
  30. package/es/Colors/index.js +2 -0
  31. package/es/Colors/themes/default.js +78 -0
  32. package/es/ContactForm/index.js +230 -0
  33. package/es/CookieConsent/index.js +113 -0
  34. package/es/CountDown/index.js +178 -0
  35. package/es/DID/index.js +105 -0
  36. package/es/Datatable/CustomToolbar.js +396 -0
  37. package/es/Datatable/DatatableContext.js +34 -0
  38. package/es/Datatable/TableSearch.js +165 -0
  39. package/es/Datatable/index.js +627 -0
  40. package/es/Datatable/utils.js +132 -0
  41. package/es/Dialog/confirm.js +90 -0
  42. package/es/Dialog/dialog.js +192 -0
  43. package/es/Dialog/index.js +3 -0
  44. package/es/DidLogo/index.js +31 -0
  45. package/es/DriftBot/index.js +70 -0
  46. package/es/Earth/countries.json +8057 -0
  47. package/es/Earth/index.js +521 -0
  48. package/es/Earth/util.js +51 -0
  49. package/es/Empty/index.js +64 -0
  50. package/es/ErrorBoundary/fallback.js +73 -0
  51. package/es/ErrorBoundary/index.js +1 -0
  52. package/es/Footer/index.js +172 -0
  53. package/es/Header/auto-hidden.js +35 -0
  54. package/es/Header/header.js +211 -0
  55. package/es/Header/index.js +2 -0
  56. package/es/Header/responsive-header.js +111 -0
  57. package/es/Icon/image.js +65 -0
  58. package/es/Icon/index.js +84 -0
  59. package/es/Img/index.js +217 -0
  60. package/es/InfoRow/index.js +87 -0
  61. package/es/Layout/dashboard/external-link.js +58 -0
  62. package/es/Layout/dashboard/full-page.js +53 -0
  63. package/es/Layout/dashboard/index.js +275 -0
  64. package/es/Layout/dashboard/sidebar.js +209 -0
  65. package/es/Layout/dashboard-legacy/header.js +174 -0
  66. package/es/Layout/dashboard-legacy/index.js +149 -0
  67. package/es/Layout/dashboard-legacy/sidebar.js +129 -0
  68. package/es/Layout/index.js +335 -0
  69. package/es/Locale/browser-lang.js +52 -0
  70. package/es/Locale/context.js +114 -0
  71. package/es/Locale/languages.js +60 -0
  72. package/es/Locale/selector.js +180 -0
  73. package/es/Locale/util.js +13 -0
  74. package/es/Logo/images/logo-dark-text.svg +3 -0
  75. package/es/Logo/images/logo-dark-top.svg +6 -0
  76. package/es/Logo/images/logo-light-text.svg +3 -0
  77. package/es/Logo/images/logo-light-top.svg +6 -0
  78. package/es/Logo/index.js +136 -0
  79. package/es/Metric/index.js +132 -0
  80. package/es/NFTDisplay/README.md +59 -0
  81. package/es/NFTDisplay/aspect-ratio-container.js +39 -0
  82. package/es/NFTDisplay/broken.js +18 -0
  83. package/es/NFTDisplay/demo/data/asset-state-display-url.json +7 -0
  84. package/es/NFTDisplay/demo/data/asset-state-gzipped-svg-1-1.json +10 -0
  85. package/es/NFTDisplay/demo/data/asset-state-gzipped-svg-374-130.json +10 -0
  86. package/es/NFTDisplay/demo/data/asset-state-gzipped-svg-with-foreign-object.json +20 -0
  87. package/es/NFTDisplay/demo/data/asset-state-svg.json +29 -0
  88. package/es/NFTDisplay/demo/data/asset-state-url.json +10 -0
  89. package/es/NFTDisplay/index.js +323 -0
  90. package/es/NFTDisplay/loading.js +18 -0
  91. package/es/NFTDisplay/svg-embedder/img.js +45 -0
  92. package/es/NFTDisplay/svg-embedder/inline-svg.js +39 -0
  93. package/es/NavMenu/index.js +2 -0
  94. package/es/NavMenu/nav-menu.js +286 -0
  95. package/es/NavMenu/style.js +176 -0
  96. package/es/PageScroller/index.js +286 -0
  97. package/es/PageScroller/story/FifthComponent.js +9 -0
  98. package/es/PageScroller/story/FirstComponent.js +9 -0
  99. package/es/PageScroller/story/FourthComponent.js +12 -0
  100. package/es/PageScroller/story/FullPage.js +47 -0
  101. package/es/PageScroller/story/PageContain.js +59 -0
  102. package/es/PageScroller/story/SecondComponent.js +9 -0
  103. package/es/PageScroller/story/ThirdComponent.js +9 -0
  104. package/es/PageScroller/story/index.css +115 -0
  105. package/es/PageScroller/usePrevValue.js +9 -0
  106. package/es/PricingTable/PricingPlan.js +124 -0
  107. package/es/PricingTable/index.js +53 -0
  108. package/es/QRCode/index.js +72 -0
  109. package/es/RelativeTime/index.js +98 -0
  110. package/es/Result/common.js +140 -0
  111. package/es/Result/demo/fixtures/result-image-404.svg +1 -0
  112. package/es/Result/index.js +33 -0
  113. package/es/Result/result.js +59 -0
  114. package/es/Result/translations.js +54 -0
  115. package/es/Screenshot/BaseScreenshot/index.js +91 -0
  116. package/es/Screenshot/BaseScreenshot/shells/Macbook.js +51 -0
  117. package/es/Screenshot/BaseScreenshot/shells/Phone.js +36 -0
  118. package/es/Screenshot/demo/images/bg-00.jpg +0 -0
  119. package/es/Screenshot/demo/images/bg-01.jpg +0 -0
  120. package/es/Screenshot/demo/images/bg-02.jpg +0 -0
  121. package/es/Screenshot/demo/images/bg-03.jpg +0 -0
  122. package/es/Screenshot/demo/images/bg-04.jpg +0 -0
  123. package/es/Screenshot/demo/images/bg-05.jpg +0 -0
  124. package/es/Screenshot/demo/images/bg-06.jpg +0 -0
  125. package/es/Screenshot/demo/images/bg-07.jpg +0 -0
  126. package/es/Screenshot/demo/images/bg-08.jpg +0 -0
  127. package/es/Screenshot/demo/images/bg-09.jpg +0 -0
  128. package/es/Screenshot/devices.css +1366 -0
  129. package/es/Screenshot/index.js +299 -0
  130. package/es/SessionManager/federated-login-detecter.js +166 -0
  131. package/es/SessionManager/index.js +468 -0
  132. package/es/SessionManager/user-popper.js +132 -0
  133. package/es/Sparkline/index.js +193 -0
  134. package/es/Spinner/index.js +28 -0
  135. package/es/SplitButton/index.js +144 -0
  136. package/es/Switch/index.js +96 -0
  137. package/es/Tabs/index.js +48 -0
  138. package/es/Tag/index.js +108 -0
  139. package/es/TextCollapse/index.js +92 -0
  140. package/es/Theme/index.js +16 -0
  141. package/es/Theme/theme-provider.js +39 -0
  142. package/es/Theme/theme.js +133 -0
  143. package/es/Toast/index.js +95 -0
  144. package/es/Util/deprecate.js +28 -0
  145. package/es/Util/index.js +298 -0
  146. package/es/Util/wallet.js +32 -0
  147. package/es/Video/index.js +89 -0
  148. package/es/Wallet/Action.js +119 -0
  149. package/es/Wallet/Download.js +331 -0
  150. package/es/Wallet/Open.js +45 -0
  151. package/es/Wallet/images/abtwallet.png +0 -0
  152. package/es/Wallet/images/android_download.svg +23 -0
  153. package/es/Wallet/images/app-store.svg +20 -0
  154. package/es/Wallet/images/google-play.svg +70 -0
  155. package/es/WebWalletSWKeeper/index.js +115 -0
  156. package/es/WechatPrompt/images/android.png +0 -0
  157. package/es/WechatPrompt/images/ios.png +0 -0
  158. package/es/WechatPrompt/index.js +88 -0
  159. package/es/index.js +38 -0
  160. package/es/withTheme/index.js +69 -0
  161. package/es/withTracker/README.md +34 -0
  162. package/es/withTracker/error_boundary.js +34 -0
  163. package/es/withTracker/index.js +56 -0
  164. package/package.json +272 -5
@@ -0,0 +1,335 @@
1
+ import { useState } from 'react';
2
+ import PropTypes from 'prop-types';
3
+ import Helmet from 'react-helmet';
4
+ import Link from '@mui/material/Link';
5
+ import AppBar from '@mui/material/AppBar';
6
+ import Container from '@mui/material/Container';
7
+ import Drawer from '@mui/material/Drawer';
8
+ import Divider from '@mui/material/Divider';
9
+ import Toolbar from '@mui/material/Toolbar';
10
+ import Typography from '@mui/material/Typography';
11
+ import List from '@mui/material/List';
12
+ import ListItem from '@mui/material/ListItem';
13
+ import ListItemText from '@mui/material/ListItemText';
14
+ import IconButton from '@mui/material/IconButton';
15
+ import MenuIcon from '@mui/icons-material/Menu';
16
+ import Footer from '../Footer';
17
+ import OpenInWallet from '../Wallet/Open';
18
+ import Icon from '../Icon';
19
+ import Logo from '../Logo';
20
+ import { styled } from '../Theme';
21
+ import { jsx as _jsx } from "react/jsx-runtime";
22
+ import { jsxs as _jsxs } from "react/jsx-runtime";
23
+ import { Fragment as _Fragment } from "react/jsx-runtime";
24
+ export default function Layout({
25
+ title,
26
+ brand,
27
+ description,
28
+ links,
29
+ logo,
30
+ showLogo,
31
+ addons,
32
+ footer,
33
+ baseUrl,
34
+ homeUrl,
35
+ children,
36
+ variant,
37
+ contentOnly,
38
+ ...rest
39
+ }) {
40
+ const [drawerOpen, setDrawerOpen] = useState(false);
41
+ const onToggleDrawer = () => {
42
+ setDrawerOpen(!drawerOpen);
43
+ };
44
+ if (contentOnly) {
45
+ return /*#__PURE__*/_jsx(Container, {
46
+ children: children
47
+ });
48
+ }
49
+ let activeLink = '';
50
+ const {
51
+ pathname: currentPath
52
+ } = new URL(window.location);
53
+ links.forEach(link => {
54
+ if (currentPath.startsWith(link.url) && link.url.length > activeLink.length) {
55
+ activeLink = link.url;
56
+ }
57
+ });
58
+ const drawer = /*#__PURE__*/_jsxs("div", {
59
+ children: [/*#__PURE__*/_jsxs(Toolbar, {
60
+ className: "toolbar toolbar--drawer",
61
+ children: [!!showLogo && /*#__PURE__*/_jsx("div", {
62
+ className: "menu-logo",
63
+ children: logo
64
+ }), /*#__PURE__*/_jsx("div", {
65
+ style: {
66
+ flexGrow: 1,
67
+ overflow: 'hidden',
68
+ textOverflow: 'ellipsis'
69
+ },
70
+ children: brand
71
+ })]
72
+ }), /*#__PURE__*/_jsx(Divider, {}), /*#__PURE__*/_jsx(List, {
73
+ children: links.map(x => /*#__PURE__*/_jsx(Link, {
74
+ className: "nav-link",
75
+ href: x.url,
76
+ underline: "hover",
77
+ children: /*#__PURE__*/_jsx(ListItem, {
78
+ button: true,
79
+ className: activeLink === x.url ? 'drawer-highlight-nav' : '',
80
+ children: /*#__PURE__*/_jsxs(ListItemText, {
81
+ children: [x.icon && /*#__PURE__*/_jsx(Icon, {
82
+ name: x.icon,
83
+ size: 18 * (x.iconZoom || 1),
84
+ color: "inherit",
85
+ style: {
86
+ marginRight: '5px'
87
+ }
88
+ }), x.title]
89
+ })
90
+ })
91
+ }, x.url))
92
+ })]
93
+ });
94
+ return /*#__PURE__*/_jsxs(_Fragment, {
95
+ children: [/*#__PURE__*/_jsxs(Div, {
96
+ ...rest,
97
+ children: [/*#__PURE__*/_jsx(Helmet, {
98
+ title: title
99
+ }), /*#__PURE__*/_jsx(AppBar, {
100
+ position: "fixed",
101
+ className: `appbar appbar--${variant}`,
102
+ color: "default",
103
+ style: {
104
+ height: 56
105
+ },
106
+ children: /*#__PURE__*/_jsx(Container, {
107
+ disableGutters: true,
108
+ children: /*#__PURE__*/_jsxs(Toolbar, {
109
+ className: "toolbar",
110
+ children: [/*#__PURE__*/_jsx(IconButton, {
111
+ color: "inherit",
112
+ "aria-label": "open drawer",
113
+ edge: "start",
114
+ onClick: onToggleDrawer,
115
+ className: "menu-button",
116
+ size: "large",
117
+ children: /*#__PURE__*/_jsx(MenuIcon, {})
118
+ }), !!showLogo && /*#__PURE__*/_jsx("div", {
119
+ className: "menu-logo",
120
+ children: logo
121
+ }), /*#__PURE__*/_jsx(Typography, {
122
+ href: homeUrl,
123
+ component: "a",
124
+ variant: "h5",
125
+ color: "inherit",
126
+ noWrap: true,
127
+ display: "block",
128
+ className: "brand",
129
+ children: brand
130
+ }), description && /*#__PURE__*/_jsx(Typography, {
131
+ component: "small",
132
+ variant: "subtitle2",
133
+ color: "inherit",
134
+ noWrap: true,
135
+ className: "description",
136
+ children: description
137
+ }), /*#__PURE__*/_jsx("div", {
138
+ style: {
139
+ flexGrow: 1
140
+ }
141
+ }), /*#__PURE__*/_jsx("div", {
142
+ className: "nav-links",
143
+ children: links.map(x => /*#__PURE__*/_jsxs(Link, {
144
+ href: x.url,
145
+ className: `nav-link ${activeLink === x.url ? 'highlight-nav' : ''}`,
146
+ color: x.color,
147
+ underline: "hover",
148
+ children: [x.icon && /*#__PURE__*/_jsx(Icon, {
149
+ name: x.icon,
150
+ size: 20 * (x.iconZoom || 1),
151
+ color: "inherit",
152
+ style: {
153
+ marginRight: '5px'
154
+ }
155
+ }), x.title]
156
+ }, x.url))
157
+ }), addons]
158
+ })
159
+ })
160
+ }), /*#__PURE__*/_jsx("div", {
161
+ className: "toolbar"
162
+ }), /*#__PURE__*/_jsx(Container, {
163
+ style: {
164
+ marginTop: 16,
165
+ flex: 1
166
+ },
167
+ children: children
168
+ }), footer, !!baseUrl && /*#__PURE__*/_jsx(OpenInWallet, {
169
+ locale: "zh",
170
+ link: baseUrl
171
+ })]
172
+ }), /*#__PURE__*/_jsx(DrawerDiv, {
173
+ children: /*#__PURE__*/_jsx(Drawer, {
174
+ variant: "temporary",
175
+ open: drawerOpen,
176
+ onClose: onToggleDrawer,
177
+ classes: {
178
+ paper: 'drawer-paper'
179
+ },
180
+ ModalProps: {
181
+ keepMounted: true,
182
+ // Better open performance on mobile.
183
+ disablePortal: true
184
+ },
185
+ children: drawer
186
+ })
187
+ })]
188
+ });
189
+ }
190
+ Layout.propTypes = {
191
+ title: PropTypes.string.isRequired,
192
+ brand: PropTypes.any.isRequired,
193
+ description: PropTypes.any,
194
+ addons: PropTypes.any,
195
+ showLogo: PropTypes.bool,
196
+ logo: PropTypes.any,
197
+ links: PropTypes.array,
198
+ children: PropTypes.any.isRequired,
199
+ baseUrl: PropTypes.string,
200
+ homeUrl: PropTypes.string,
201
+ variant: PropTypes.oneOf(['shadow', 'border']),
202
+ footer: PropTypes.any,
203
+ contentOnly: PropTypes.bool
204
+ };
205
+ Layout.defaultProps = {
206
+ contentOnly: false,
207
+ baseUrl: '',
208
+ homeUrl: '/',
209
+ links: [],
210
+ showLogo: false,
211
+ logo: /*#__PURE__*/_jsx(Logo, {
212
+ showText: false,
213
+ style: {
214
+ width: '40px',
215
+ height: '40px'
216
+ }
217
+ }),
218
+ variant: 'shadow',
219
+ addons: undefined,
220
+ description: undefined,
221
+ footer: /*#__PURE__*/_jsx(Container, {
222
+ children: /*#__PURE__*/_jsx(Footer, {})
223
+ })
224
+ };
225
+ const Div = styled('div')`
226
+ width: 100%;
227
+ min-height: 100vh;
228
+ display: flex;
229
+ flex-direction: column;
230
+ .appbar {
231
+ &.appbar--border {
232
+ box-shadow: none;
233
+ &::before {
234
+ content: '';
235
+ position: absolute;
236
+ left: 0;
237
+ right: 0;
238
+ height: 1px;
239
+ bottom: -1px;
240
+ display: block;
241
+ background-color: rgba(0, 0, 0, 0.12);
242
+ }
243
+ }
244
+ }
245
+
246
+ .toolbar {
247
+ min-height: 56px;
248
+ background: inherit;
249
+ white-space: nowrap;
250
+ .menu-logo {
251
+ font-size: 0;
252
+ margin-right: 8px;
253
+ }
254
+
255
+ .nav-links {
256
+ display: flex;
257
+ align-items: center;
258
+ .nav-link {
259
+ margin: 8px 12px;
260
+ font-size: 16px;
261
+ display: flex;
262
+ align-items: center;
263
+ }
264
+
265
+ .highlight-nav {
266
+ font-weight: bolder;
267
+ }
268
+ }
269
+ .brand {
270
+ cursor: pointer;
271
+ text-decoration: none;
272
+ overflow: hidden;
273
+ text-overflow: ellipsis;
274
+ flex-shrink: 1;
275
+ }
276
+ .description {
277
+ color: #999;
278
+ font-size: 15px;
279
+ margin-left: 10px;
280
+ font-weight: normal;
281
+ flex-shrink: 999999;
282
+ }
283
+ }
284
+
285
+ ${props => props.theme.breakpoints.up('md')} {
286
+ .toolbar {
287
+ .menu-button {
288
+ display: none;
289
+ }
290
+ .menu-logo {
291
+ & + .brand {
292
+ padding-left: 45px;
293
+ margin-left: -45px;
294
+ }
295
+ }
296
+ }
297
+ }
298
+
299
+ ${props => props.theme.breakpoints.down('md')} {
300
+ .toolbar {
301
+ .nav-links,
302
+ .menu-logo,
303
+ .description {
304
+ display: none;
305
+ }
306
+ }
307
+ }
308
+ `;
309
+ const DrawerDiv = styled('nav')`
310
+ width: 240px;
311
+ .drawer-paper {
312
+ width: 240px;
313
+ }
314
+ .toolbar {
315
+ min-height: 56px;
316
+ }
317
+
318
+ a:hover,
319
+ a:active,
320
+ a:visited,
321
+ a:focus {
322
+ text-decoration: none;
323
+ }
324
+
325
+ .drawer-highlight-nav {
326
+ background-color: #eee;
327
+ }
328
+
329
+ .toolbar--drawer {
330
+ font-size: 18px;
331
+ .menu-logo {
332
+ display: inline-flex;
333
+ }
334
+ }
335
+ `;
@@ -0,0 +1,52 @@
1
+ /* eslint-disable no-param-reassign */
2
+ function startsWith(string, target, position) {
3
+ const {
4
+ length
5
+ } = string;
6
+ position = position == null ? 0 : position;
7
+ if (position < 0) {
8
+ position = 0;
9
+ } else if (position > length) {
10
+ position = length;
11
+ }
12
+ target = `${target}`;
13
+ // eslint-disable-next-line eqeqeq
14
+ return string.slice(position, position + target.length) == target;
15
+ }
16
+ function getBrowserLang() {
17
+ if (typeof window === 'undefined') {
18
+ return null;
19
+ }
20
+ const lang = window.navigator.languages && window.navigator.languages[0] || window.navigator.language || window.navigator.browserLanguage || window.navigator.userLanguage || window.navigator.systemLanguage || null;
21
+ return lang;
22
+ }
23
+ function normalizeCode(code) {
24
+ return (code || '').toLowerCase().replace(/-/, '_');
25
+ }
26
+ function getPreferredLanguage(options) {
27
+ if (!options) {
28
+ return getBrowserLang();
29
+ }
30
+ const {
31
+ languages,
32
+ fallback
33
+ } = options;
34
+ if (!options.languages) {
35
+ return fallback;
36
+ }
37
+
38
+ // some browsers report language as en-US instead of en_US
39
+ const browserLanguage = normalizeCode(getBrowserLang());
40
+ if (!browserLanguage) {
41
+ return fallback;
42
+ }
43
+ const match = languages.filter(lang => normalizeCode(lang) === browserLanguage);
44
+ if (match.length > 0) {
45
+ return match[0] || fallback;
46
+ }
47
+
48
+ // en == en_US
49
+ const matchCodeOnly = languages.filter(lang => startsWith(browserLanguage, lang));
50
+ return matchCodeOnly[0] || fallback;
51
+ }
52
+ export default getPreferredLanguage;
@@ -0,0 +1,114 @@
1
+ /* eslint-disable no-prototype-builtins */
2
+ import { createContext, useState, useEffect, useContext } from 'react';
3
+ import get from 'lodash/get';
4
+ import PropTypes from 'prop-types';
5
+ import Cookie from 'js-cookie';
6
+ import browserLang from './browser-lang';
7
+ import { translate } from './util';
8
+ import { getCookieOptions } from '../Util';
9
+ import { jsx as _jsx } from "react/jsx-runtime";
10
+ const cookieName = 'nf_lang';
11
+
12
+ // 跨应用传递多语言选择的方式是在 query string 中添加 locale 参数,LocaleSelector 要高优先级遵守这个参数
13
+ const getLocaleFromSearchParams = (languages, url = window.location.href) => {
14
+ const locale = new URL(url).searchParams.get('locale');
15
+ if (languages.find(x => x.code === locale)) {
16
+ return locale;
17
+ }
18
+ return null;
19
+ };
20
+ const setLocaleParam = (locale, url = window.location.href) => {
21
+ const urlObj = new URL(url);
22
+ const hasLocaleParam = urlObj.searchParams.has('locale');
23
+ if (hasLocaleParam) {
24
+ urlObj.searchParams.set('locale', locale);
25
+ window.history.replaceState({}, '', urlObj.href);
26
+ }
27
+ };
28
+ const getLocale = (languages = []) => {
29
+ const langParams = {
30
+ languages: languages.map(item => item.code),
31
+ // 取 languages 首个元素的 code 值, 如果不存在则取 'en'
32
+ fallback: languages?.[0]?.code || 'en'
33
+ };
34
+ return getLocaleFromSearchParams(languages) || Cookie.get(cookieName) || browserLang(langParams);
35
+ };
36
+ const setLocale = locale => {
37
+ Cookie.set(cookieName, locale, getCookieOptions());
38
+ setLocaleParam(locale);
39
+ };
40
+ const getLanguages = arg => {
41
+ const env = get(window, 'blocklet.languages');
42
+ if (Array.isArray(env) && env.length) {
43
+ return env;
44
+ }
45
+ if (Array.isArray(arg) && arg.length) {
46
+ return arg;
47
+ }
48
+ return [{
49
+ code: 'en',
50
+ name: 'English'
51
+ }, {
52
+ code: 'zh',
53
+ name: '简体中文'
54
+ }];
55
+ };
56
+ const LocaleContext = /*#__PURE__*/createContext();
57
+ const {
58
+ Provider,
59
+ Consumer
60
+ } = LocaleContext;
61
+ function LocaleProvider({
62
+ children,
63
+ locale,
64
+ fallbackLocale,
65
+ translations,
66
+ languages,
67
+ ...rest
68
+ }) {
69
+ const langs = getLanguages(languages);
70
+ const [currentLocale, setCurrentLocale] = useState(locale || getLocale(langs));
71
+ const changeLocale = newLocale => {
72
+ setCurrentLocale(newLocale);
73
+ setLocale(newLocale);
74
+ };
75
+ useEffect(() => {
76
+ const tmpLocale = locale || getLocale(langs);
77
+ if (tmpLocale !== currentLocale) {
78
+ changeLocale(locale);
79
+ }
80
+ // eslint-disable-next-line react-hooks/exhaustive-deps
81
+ }, [locale]);
82
+ const t = (key, data) => translate(translations, key, currentLocale, fallbackLocale, data);
83
+ return /*#__PURE__*/_jsx(Provider, {
84
+ value: {
85
+ locale: currentLocale,
86
+ changeLocale,
87
+ t,
88
+ languages: langs,
89
+ ...rest
90
+ },
91
+ children: children
92
+ });
93
+ }
94
+ function useLocaleContext() {
95
+ const context = useContext(LocaleContext);
96
+ return context;
97
+ }
98
+ LocaleProvider.propTypes = {
99
+ children: PropTypes.any.isRequired,
100
+ translations: PropTypes.object.isRequired,
101
+ locale: PropTypes.string,
102
+ // 会影响 translate(key) 的结果 - 当 key 无效时查找 fallbackLocale 对应的翻译结果
103
+ fallbackLocale: PropTypes.string,
104
+ languages: PropTypes.arrayOf(PropTypes.shape({
105
+ code: PropTypes.string,
106
+ name: PropTypes.string
107
+ }))
108
+ };
109
+ LocaleProvider.defaultProps = {
110
+ locale: '',
111
+ fallbackLocale: '',
112
+ languages: []
113
+ };
114
+ export { LocaleProvider, Consumer as LocaleConsumer, LocaleContext, useLocaleContext, setLocale, getLocale };
@@ -0,0 +1,60 @@
1
+ // from: https://github.com/meikidd/iso-639-1/blob/master/src/data.js
2
+ const languages = {
3
+ ar: {
4
+ name: 'Arabic',
5
+ nativeName: 'اَلْعَرَبِيَّةُ'
6
+ },
7
+ de: {
8
+ name: 'German',
9
+ nativeName: 'Deutsch'
10
+ },
11
+ en: {
12
+ name: 'English',
13
+ nativeName: 'English'
14
+ },
15
+ es: {
16
+ name: 'Spanish',
17
+ nativeName: 'Español'
18
+ },
19
+ fr: {
20
+ name: 'French',
21
+ nativeName: 'Français'
22
+ },
23
+ it: {
24
+ name: 'Italian',
25
+ nativeName: 'Italiano'
26
+ },
27
+ ja: {
28
+ name: 'Japanese',
29
+ nativeName: '日本語'
30
+ },
31
+ ko: {
32
+ name: 'Korean',
33
+ nativeName: '한국어'
34
+ },
35
+ mn: {
36
+ name: 'Mongolian',
37
+ nativeName: 'Монгол хэл'
38
+ },
39
+ pt: {
40
+ name: 'Portuguese',
41
+ nativeName: 'Português'
42
+ },
43
+ ru: {
44
+ name: 'Russian',
45
+ nativeName: 'Русский'
46
+ },
47
+ zh: {
48
+ name: 'Simplified Chinese',
49
+ nativeName: '简体中文'
50
+ },
51
+ 'zh-TW': {
52
+ name: 'Traditional Chinese',
53
+ nativeName: '繁體中文'
54
+ }
55
+ };
56
+ export default Object.keys(languages).map(code => ({
57
+ code,
58
+ ...languages[code]
59
+ }));
60
+ export { languages as map };