@hyvor/design 0.0.52 → 0.0.53

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.
@@ -1,6 +1,8 @@
1
- <script>import { onMount, setContext } from "svelte";
2
- onMount(() => {
3
- setContext("i18n", "not set");
4
- });
1
+ <script>import { setContext } from "svelte";
2
+ import { InternationalizationService } from "./i18n.js";
3
+ export let languages;
4
+ const i18n = new InternationalizationService(languages);
5
+ setContext("i18n", i18n);
5
6
  </script>
6
7
 
8
+ <slot />
@@ -1,10 +1,15 @@
1
1
  import { SvelteComponent } from "svelte";
2
+ import { type Language } from "./i18n.js";
2
3
  declare const __propDef: {
3
- props: Record<string, never>;
4
+ props: {
5
+ languages: Language[];
6
+ };
4
7
  events: {
5
8
  [evt: string]: CustomEvent<any>;
6
9
  };
7
- slots: {};
10
+ slots: {
11
+ default: {};
12
+ };
8
13
  };
9
14
  export type InternationalizationProviderProps = typeof __propDef.props;
10
15
  export type InternationalizationProviderEvents = typeof __propDef.events;
@@ -1,43 +1,63 @@
1
- <script>import IconButton from "./../IconButton/IconButton.svelte";
2
- import Box from "../Box/Box.svelte";
3
- import { IconCaretUp } from "@hyvor/icons";
4
- import { page } from "$app/stores";
5
- const i18n = $page.data.i18n;
1
+ <script>import { getContext } from "svelte";
2
+ import Dropdown from "../Dropdown/Dropdown.svelte";
3
+ import Button from "../Button/Button.svelte";
4
+ import {} from "./i18n.js";
5
+ import ActionList from "../ActionList/ActionList.svelte";
6
+ import ActionListItem from "../ActionList/ActionListItem.svelte";
7
+ import Text from "../Text/Text.svelte";
8
+ import { IconCaretDown } from "@hyvor/icons";
9
+ import IconButton from "../IconButton/IconButton.svelte";
10
+ export let position = "bottom";
11
+ export let align = "start";
12
+ export let caret = IconCaretDown;
13
+ export let icon = false;
14
+ export let size = "medium";
15
+ const i18n = getContext("i18n");
16
+ const currentLanguage = i18n ? i18n.localeLanguage : void 0;
17
+ let show = false;
18
+ function handleClick(language) {
19
+ i18n.setLocale(language.code);
20
+ show = false;
21
+ }
6
22
  </script>
7
23
 
8
24
 
9
- {#if i18n}
10
- <div class="language-toggle">
11
- <span class="current">
12
- <span class="flag">🇫🇷</span>
13
- <span class="name">Français (France)</span>
14
- <span class="icon"><IconCaretUp size={14} /></span>
25
+ {#if i18n && $currentLanguage}
26
+
27
+ <Dropdown bind:show={show} {position} {align}>
28
+ <span slot="trigger">
29
+ {#if icon}
30
+ <IconButton color="input" {size}>
31
+ {$currentLanguage.flag}
32
+ </IconButton>
33
+ {:else}
34
+ <Button color="input" {size}>
35
+ <span slot="start">{$currentLanguage.flag}</span>
36
+ {$currentLanguage.name}
37
+ <svelte:component slot="end" this={caret} size={12} />
38
+ </Button>
39
+ {/if}
15
40
  </span>
16
- </div>
41
+ <ActionList slot="content">
42
+ {#each i18n.languages as language (language.code)}
43
+ <ActionListItem on:click={() => handleClick(language)}>
44
+ <span class="flag" slot="start">{language.flag}</span>
45
+ <span class="name">
46
+ {language.name}
47
+ </span>
48
+ <Text small light>
49
+ {language.region}
50
+ </Text>
51
+ </ActionListItem>
52
+ {/each}
53
+ </ActionList>
54
+ </Dropdown>
55
+
17
56
  {/if}
18
57
 
19
58
  <style>
20
- .current {
21
- padding: 4px 15px;
22
- font-size: 15px;
23
- display: flex;
24
- align-items: center;
25
- cursor: pointer;
26
- box-shadow: var(--box-shadow);
27
- border-radius: var(--box-radius);
28
- background-color: var(--box-background);
29
- transition: .2s box-shadow;
30
- }
31
- .current:hover {
32
- box-shadow: var(--box-shadow-dark);
33
- }
34
59
  .flag {
35
60
  margin-right: 6px;
36
61
  font-size: 20px;
37
62
  }
38
- .icon {
39
- margin-left: 8px;
40
- display: inline-flex;
41
- align-items: center;
42
- }
43
63
  </style>
@@ -1,6 +1,14 @@
1
1
  import { SvelteComponent } from "svelte";
2
+ import { type ComponentProps, type ComponentType } from "svelte";
3
+ import Dropdown from "../Dropdown/Dropdown.svelte";
2
4
  declare const __propDef: {
3
- props: Record<string, never>;
5
+ props: {
6
+ position?: ComponentProps<Dropdown>['position'];
7
+ align?: ComponentProps<Dropdown>['align'];
8
+ caret?: ComponentType | undefined;
9
+ icon?: boolean | undefined;
10
+ size?: "small" | "medium" | undefined;
11
+ };
4
12
  events: {
5
13
  [evt: string]: CustomEvent<any>;
6
14
  };
@@ -1,5 +1,97 @@
1
- <script>import { page } from "$app/stores";
2
- console.log("T", $page.data);
1
+ <script>import { getContext, onMount, tick, afterUpdate } from "svelte";
2
+ import { getStringByKey, InternationalizationService } from "./i18n.js";
3
+ import { IntlMessageFormat } from "intl-messageformat";
4
+ import { browser } from "$app/environment";
5
+ export let key;
6
+ export let params = {};
7
+ let hasComponentParams = false;
8
+ function getParamsForBackend() {
9
+ let retParams = {};
10
+ for (let [key2, value] of Object.entries(params)) {
11
+ let newValue;
12
+ if (typeof value === "object" && value !== null && value.hasOwnProperty("component")) {
13
+ newValue = (chunks) => {
14
+ const children = typeof chunks === "string" ? chunks : chunks.join("");
15
+ return children;
16
+ };
17
+ hasComponentParams = true;
18
+ } else {
19
+ newValue = value;
20
+ }
21
+ retParams[key2] = newValue;
22
+ }
23
+ return retParams;
24
+ }
25
+ const componentBindings = /* @__PURE__ */ new Map();
26
+ function getParamsForFrontend() {
27
+ let retParams = {};
28
+ for (let [key2, value] of Object.entries(params)) {
29
+ let newValue;
30
+ if (typeof value === "object" && value !== null && value.hasOwnProperty("component")) {
31
+ const { component, props } = value;
32
+ newValue = (chunks) => {
33
+ const children = typeof chunks === "string" ? chunks : chunks.join("");
34
+ const id = key2 + "-" + Math.random().toString(36).substring(7) + "-" + Date.now().toString();
35
+ componentBindings.set(id, {
36
+ component,
37
+ props: {
38
+ ...props,
39
+ children
40
+ }
41
+ });
42
+ return '<span id="' + id + '">' + children + "</span>";
43
+ };
44
+ } else {
45
+ newValue = value;
46
+ }
47
+ retParams[key2] = newValue;
48
+ }
49
+ return retParams;
50
+ }
51
+ const i18n = getContext("i18n");
52
+ const locale = i18n.locale;
53
+ const strings = i18n.strings;
54
+ let message = getMessage(getParamsForBackend());
55
+ function getMessage(processedParams) {
56
+ const string = getStringByKey($strings, key);
57
+ if (string) {
58
+ const formatter = new IntlMessageFormat(string, $locale);
59
+ return formatter.format(processedParams);
60
+ }
61
+ return "";
62
+ }
63
+ function bindComponents() {
64
+ for (let [id, binding] of componentBindings) {
65
+ const el = document.getElementById(id);
66
+ if (el) {
67
+ el.innerHTML = "";
68
+ new binding.component({
69
+ target: el,
70
+ hydrate: true,
71
+ props: binding.props
72
+ });
73
+ }
74
+ }
75
+ }
76
+ async function renderFrontend() {
77
+ message = getMessage(getParamsForFrontend());
78
+ await tick();
79
+ if (hasComponentParams)
80
+ bindComponents();
81
+ }
82
+ let mounted = false;
83
+ $: {
84
+ $locale, $strings, params;
85
+ if (browser && mounted) {
86
+ renderFrontend();
87
+ }
88
+ }
89
+ onMount(async () => {
90
+ mounted = true;
91
+ if (hasComponentParams) {
92
+ renderFrontend();
93
+ }
94
+ });
3
95
  </script>
4
96
 
5
- { $page.data.lang.home.title }
97
+ {@html message}
@@ -1,6 +1,14 @@
1
1
  import { SvelteComponent } from "svelte";
2
+ import { type ComponentType } from "svelte";
3
+ import { type PrimitiveType } from 'intl-messageformat';
2
4
  declare const __propDef: {
3
- props: Record<string, never>;
5
+ props: {
6
+ key: string;
7
+ params?: Record<string, {
8
+ component: ComponentType;
9
+ props?: Record<string, any> | undefined;
10
+ } | PrimitiveType> | undefined;
11
+ };
4
12
  events: {
5
13
  [evt: string]: CustomEvent<any>;
6
14
  };
@@ -1,18 +1,33 @@
1
- type LoaderType = () => Promise<any>;
2
- interface Language {
3
- code: string;
1
+ /// <reference types="svelte" />
2
+ import { type Readable, type Writable } from "svelte/store";
3
+ export type i18nLoaderType = () => Promise<any>;
4
+ interface LanguageBase {
4
5
  name: string;
5
- loader: LoaderType;
6
- default: boolean;
6
+ flag: string;
7
+ region: string;
8
+ code: string;
9
+ default?: boolean;
10
+ }
11
+ interface LanguageWithStrings extends LanguageBase {
12
+ strings: Record<string, any>;
13
+ }
14
+ interface LanguageWithLoader extends LanguageBase {
15
+ loader: i18nLoaderType;
7
16
  }
8
- export declare class i18n {
9
- private current;
10
- private languages;
11
- constructor(current: string);
12
- register(code: string, name: string, loader: LoaderType, isDefault?: boolean): void;
17
+ export type Language = LanguageWithStrings | LanguageWithLoader;
18
+ export declare class InternationalizationService {
19
+ languages: LanguageWithLoader[];
20
+ locale: Writable<string>;
21
+ localeLanguage: Readable<LanguageWithLoader>;
22
+ strings: Writable<{}>;
23
+ stringsCache: Map<string, Record<string, any>>;
24
+ defaultStrings: Record<string, any>;
25
+ constructor(languages: Language[]);
26
+ setStrings(code: string): void;
27
+ setLocale(code: string): void;
28
+ register(language: Language): void;
13
29
  found(code: string): boolean;
14
- languageByCode(code: string): Language | undefined;
15
- getCurrent(): Language | undefined;
16
- load(code: string): Promise<any>;
30
+ languageByCode(code: string): LanguageWithLoader | undefined;
17
31
  }
32
+ export declare function getStringByKey(messages: Record<string, any>, key: string): string | undefined;
18
33
  export {};
@@ -1,15 +1,54 @@
1
- export class i18n {
2
- current;
1
+ import { deepmerge } from "deepmerge-ts";
2
+ import { writable, derived } from "svelte/store";
3
+ export class InternationalizationService {
3
4
  languages = [];
4
- constructor(current) {
5
- this.current = current;
5
+ locale;
6
+ localeLanguage;
7
+ strings = writable({});
8
+ stringsCache = new Map();
9
+ defaultStrings;
10
+ constructor(languages) {
11
+ const defaultLanguage = languages.find(l => l.default);
12
+ if (!defaultLanguage) {
13
+ throw new Error('Default language not found');
14
+ }
15
+ const defaultStrings = defaultLanguage.strings || null;
16
+ if (!defaultStrings) {
17
+ throw new Error('Default strings not found for the default language');
18
+ }
19
+ this.locale = writable(defaultLanguage.code);
20
+ this.localeLanguage = derived(this.locale, $locale => this.languageByCode($locale));
21
+ this.defaultStrings = defaultStrings;
22
+ this.strings.set(this.defaultStrings);
23
+ this.stringsCache.set(defaultLanguage.code, this.defaultStrings);
24
+ for (const language of languages) {
25
+ this.register(language);
26
+ }
6
27
  }
7
- register(code, name, loader, isDefault = false) {
28
+ setStrings(code) {
29
+ const defaultStrings = this.defaultStrings;
30
+ const strings = this.stringsCache.get(code) || {};
31
+ const merged = deepmerge(defaultStrings, strings);
32
+ this.strings.set(merged);
33
+ }
34
+ setLocale(code) {
35
+ this.locale.set(code);
36
+ if (this.stringsCache.has(code)) {
37
+ this.setStrings(code);
38
+ }
39
+ else {
40
+ this.languageByCode(code)?.loader().then(({ default: strings }) => {
41
+ this.stringsCache.set(code, strings);
42
+ this.setStrings(code);
43
+ });
44
+ }
45
+ }
46
+ register(language) {
8
47
  this.languages.push({
9
- code,
10
- name,
11
- loader,
12
- default: isDefault
48
+ ...language,
49
+ loader: language.hasOwnProperty('strings') ?
50
+ () => Promise.resolve(language.strings) :
51
+ language.loader,
13
52
  });
14
53
  }
15
54
  found(code) {
@@ -18,10 +57,17 @@ export class i18n {
18
57
  languageByCode(code) {
19
58
  return this.languages.find(l => l.code === code);
20
59
  }
21
- getCurrent() {
22
- return this.languageByCode(this.current);
23
- }
24
- async load(code) {
25
- return (await this.languageByCode(code)?.loader()).default;
60
+ }
61
+ export function getStringByKey(messages, key) {
62
+ const keys = key.split('.');
63
+ let value = messages;
64
+ for (const key of keys) {
65
+ if (value && typeof value === 'object' && key in value) {
66
+ value = value[key];
67
+ }
68
+ else {
69
+ return undefined; // Key not found or value is not an object
70
+ }
26
71
  }
72
+ return value;
27
73
  }
@@ -41,3 +41,7 @@ export { default as toast } from './Toast/toast.js';
41
41
  export { default as Tooltip } from './Tooltip/Tooltip.svelte';
42
42
  export { default as IconMessage } from './IconMessage/IconMessage.svelte';
43
43
  export { clickOutside } from './directives/clickOutside.js';
44
+ export { default as InternationalizationProvider } from './Internationalization/InternationalizationProvider.svelte';
45
+ export { default as LanguageToggle } from './Internationalization/LanguageToggle.svelte';
46
+ export { default as T } from './Internationalization/T.svelte';
47
+ export { type Language as InternationalizationLanguage, InternationalizationService } from './Internationalization/i18n.js';
@@ -42,3 +42,8 @@ export { default as Tooltip } from './Tooltip/Tooltip.svelte';
42
42
  export { default as IconMessage } from './IconMessage/IconMessage.svelte';
43
43
  // directives
44
44
  export { clickOutside } from './directives/clickOutside.js';
45
+ // i18n
46
+ export { default as InternationalizationProvider } from './Internationalization/InternationalizationProvider.svelte';
47
+ export { default as LanguageToggle } from './Internationalization/LanguageToggle.svelte';
48
+ export { default as T } from './Internationalization/T.svelte';
49
+ export { InternationalizationService } from './Internationalization/i18n.js';
@@ -52,7 +52,7 @@ content :global(p), content :global(li) {
52
52
  line-height: var(--line-height-content);
53
53
  }
54
54
 
55
- content :global(h1) {
55
+ content :global(h1:first-child) {
56
56
  margin-top: 0;
57
57
  font-size: 36px;
58
58
  font-weight: 600;
@@ -61,8 +61,7 @@ content :global(h1) {
61
61
  position: relative;
62
62
  display: table;
63
63
  }
64
-
65
- content :global(h1::after) {
64
+ content :global(h1:first-child):after {
66
65
  position: absolute;
67
66
  content: "";
68
67
  bottom: -13px;
@@ -114,12 +113,32 @@ content :global(a.heading-anchor-link) {
114
113
  align-items: center;
115
114
  }
116
115
 
116
+ content :global(h1),
117
117
  content :global(h2),
118
118
  content :global(h3),
119
119
  content :global(h4),
120
120
  content :global(h5),
121
121
  content :global(h6) {
122
122
  position: relative;
123
+ margin: 20px 0;
124
+ }
125
+ content :global(h1) {
126
+ font-size: 2em;
127
+ }
128
+ content :global(h2) {
129
+ font-size: 1.5em;
130
+ }
131
+ content :global(h3) {
132
+ font-size: 1.3em;
133
+ }
134
+ content :global(h4) {
135
+ font-size: 1.2em;
136
+ }
137
+ content :global(h5) {
138
+ font-size: 1.1em;
139
+ }
140
+ content :global(h6) {
141
+ font-size: 1em;
123
142
  }
124
143
 
125
144
  content :global(.heading-anchor:hover + .heading-anchor-link) {
@@ -14,7 +14,8 @@ onMount(() => {
14
14
  orderedList: false,
15
15
  hasInnerContainers: true,
16
16
  headingsOffset: 75,
17
- scrollSmooth: true
17
+ scrollSmooth: true,
18
+ scrollSmoothOffset: -75
18
19
  });
19
20
  });
20
21
  afterNavigate(() => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hyvor/design",
3
- "version": "0.0.52",
3
+ "version": "0.0.53",
4
4
  "license": "MIT",
5
5
  "private": false,
6
6
  "scripts": {
@@ -46,7 +46,11 @@
46
46
  "dependencies": {
47
47
  "@fontsource/readex-pro": "^5.0.8",
48
48
  "@hyvor/icons": "^0.0.3",
49
+ "deepmerge-ts": "^5.1.0",
49
50
  "highlight.js": "^11.9.0",
51
+ "i": "^0.3.7",
52
+ "intl-messageformat": "^10.5.11",
53
+ "npm": "^10.4.0",
50
54
  "svelte-awesome-color-picker": "^3.0.4",
51
55
  "tocbot": "^4.25.0"
52
56
  },