@tma.js/sdk 0.13.2 → 0.13.3

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.
@@ -9,40 +9,6 @@ import { State } from '../../state/index.js';
9
9
 
10
10
  import type { ThemeParamsEvents, ThemeParamsState } from './types.js';
11
11
 
12
- function prepareThemeParams(value: ThemeParamsType): ThemeParamsState {
13
- const {
14
- accentTextColor = null,
15
- backgroundColor = null,
16
- buttonColor = null,
17
- buttonTextColor = null,
18
- destructiveTextColor = null,
19
- headerBackgroundColor = null,
20
- hintColor = null,
21
- linkColor = null,
22
- secondaryBackgroundColor = null,
23
- sectionBackgroundColor = null,
24
- sectionHeaderTextColor = null,
25
- subtitleTextColor = null,
26
- textColor = null,
27
- } = value;
28
-
29
- return {
30
- accentTextColor,
31
- backgroundColor,
32
- buttonColor,
33
- buttonTextColor,
34
- destructiveTextColor,
35
- headerBackgroundColor,
36
- hintColor,
37
- linkColor,
38
- secondaryBackgroundColor,
39
- sectionBackgroundColor,
40
- sectionHeaderTextColor,
41
- subtitleTextColor,
42
- textColor,
43
- };
44
- }
45
-
46
12
  /**
47
13
  * Contains information about currently used theme by application.
48
14
  * @see https://core.telegram.org/bots/webapps#themeparams
@@ -71,7 +37,7 @@ export class ThemeParams {
71
37
  */
72
38
  static sync(themeParams: ThemeParams): void {
73
39
  on('theme_changed', (event) => {
74
- themeParams.state.set(prepareThemeParams(parse(event.theme_params)));
40
+ themeParams.state.set(parse(event.theme_params));
75
41
  });
76
42
  }
77
43
 
@@ -94,35 +60,59 @@ export class ThemeParams {
94
60
  private readonly state: State<ThemeParamsState>;
95
61
 
96
62
  constructor(params: ThemeParamsType) {
97
- this.state = new State(prepareThemeParams(params), this.ee);
63
+ this.state = new State(params, this.ee);
64
+ }
65
+
66
+ get accentTextColor(): RGB | null {
67
+ return this.get('accentTextColor');
98
68
  }
99
69
 
100
70
  /**
101
71
  * Returns background color.
102
72
  */
103
73
  get backgroundColor(): RGB | null {
104
- return this.state.get('backgroundColor');
74
+ return this.get('backgroundColor');
105
75
  }
106
76
 
107
77
  /**
108
78
  * Returns button color.
109
79
  */
110
80
  get buttonColor(): RGB | null {
111
- return this.state.get('buttonColor');
81
+ return this.get('buttonColor');
112
82
  }
113
83
 
114
84
  /**
115
85
  * Returns button text color.
116
86
  */
117
87
  get buttonTextColor(): RGB | null {
118
- return this.state.get('buttonTextColor');
88
+ return this.get('buttonTextColor');
89
+ }
90
+
91
+ get destructiveTextColor(): RGB | null {
92
+ return this.get('destructiveTextColor');
93
+ }
94
+
95
+ /**
96
+ * Retrieves palette color value by its name.
97
+ * @param key - palette key name.
98
+ */
99
+ get(key: keyof ThemeParamsType): RGB | null {
100
+ return this.state.get(key) || null;
101
+ }
102
+
103
+ getState(): ThemeParamsType {
104
+ return this.state.getState();
105
+ }
106
+
107
+ get headerBackgroundColor(): RGB | null {
108
+ return this.get('headerBackgroundColor');
119
109
  }
120
110
 
121
111
  /**
122
112
  * Returns hint color.
123
113
  */
124
114
  get hintColor(): RGB | null {
125
- return this.state.get('hintColor');
115
+ return this.get('hintColor');
126
116
  }
127
117
 
128
118
  /**
@@ -137,7 +127,7 @@ export class ThemeParams {
137
127
  * Returns current link color.
138
128
  */
139
129
  get linkColor(): RGB | null {
140
- return this.state.get('linkColor');
130
+ return this.get('linkColor');
141
131
  }
142
132
 
143
133
  /**
@@ -154,13 +144,25 @@ export class ThemeParams {
154
144
  * Returns secondary background color.
155
145
  */
156
146
  get secondaryBackgroundColor(): RGB | null {
157
- return this.state.get('secondaryBackgroundColor');
147
+ return this.get('secondaryBackgroundColor');
148
+ }
149
+
150
+ get sectionBackgroundColor(): RGB | null {
151
+ return this.get('sectionBackgroundColor');
152
+ }
153
+
154
+ get sectionHeaderTextColor(): RGB | null {
155
+ return this.get('sectionHeaderTextColor');
156
+ }
157
+
158
+ get subtitleTextColor(): RGB | null {
159
+ return this.get('subtitleTextColor');
158
160
  }
159
161
 
160
162
  /**
161
163
  * Returns text color.
162
164
  */
163
165
  get textColor(): RGB | null {
164
- return this.state.get('textColor');
166
+ return this.get('textColor');
165
167
  }
166
168
  }
@@ -1,15 +1,8 @@
1
- import type { HasUndefined, If } from '@tma.js/util-types';
2
1
  import type { ThemeParams } from '@tma.js/theme-params';
3
2
 
4
3
  import type { StateEvents } from '../../state/index.js';
5
4
 
6
- export type ThemeParamsState = {
7
- [K in keyof ThemeParams]-?: If<
8
- HasUndefined<ThemeParams[K]>,
9
- Exclude<ThemeParams[K], undefined> | null,
10
- ThemeParams[K]
11
- >;
12
- };
5
+ export type ThemeParamsState = ThemeParams;
13
6
 
14
7
  export type ThemeParamsEvents = StateEvents<ThemeParamsState>;
15
8
 
package/src/init/css.ts CHANGED
@@ -33,30 +33,6 @@ function setSizeVariable(name: string, size: number) {
33
33
  setVariable(name, `${size}px`);
34
34
  }
35
35
 
36
- /**
37
- * Creates CSS variables based on theme parameters.
38
- * @param themeParams - ThemeParams instance.
39
- */
40
- function createThemeVariables(themeParams: ThemeParams): void {
41
- const {
42
- backgroundColor,
43
- buttonTextColor,
44
- secondaryBackgroundColor,
45
- hintColor,
46
- buttonColor,
47
- linkColor,
48
- textColor,
49
- } = themeParams;
50
-
51
- setColorVariable('--tg-theme-bg-color', backgroundColor);
52
- setColorVariable('--tg-theme-button-color', buttonColor);
53
- setColorVariable('--tg-theme-button-text-color', buttonTextColor);
54
- setColorVariable('--tg-theme-hint-color', hintColor);
55
- setColorVariable('--tg-theme-link-color', linkColor);
56
- setColorVariable('--tg-theme-secondary-bg-color', secondaryBackgroundColor);
57
- setColorVariable('--tg-theme-text-color', textColor);
58
- }
59
-
60
36
  /**
61
37
  * Creates CSS variables based on Mini App background and header colors with
62
38
  * theme parameters.
@@ -72,16 +48,9 @@ function createWebAppVariables(webApp: WebApp, themeParams: ThemeParams): void {
72
48
  }
73
49
 
74
50
  /**
75
- * Creates CSS variables connected with theme parameters.
76
- *
77
- * Created variables:
78
- * - `--tg-theme-bg-color`
79
- * - `--tg-theme-button-color`
80
- * - `--tg-theme-button-text-color`
81
- * - `--tg-theme-hint-color`
82
- * - `--tg-theme-link-color`
83
- * - `--tg-theme-secondary-bg-color`
84
- * - `--tg-theme-text-color`
51
+ * Creates CSS variables connected with theme parameters. Created CSS variables names are
52
+ * following the pattern "--tg-theme-{name}". {name} is a theme parameters key name converted
53
+ * from snake to kebab case.
85
54
  *
86
55
  * Variables are being automatically updated in case, corresponding properties
87
56
  * updated in passed ThemeParams instance.
@@ -89,7 +58,14 @@ function createWebAppVariables(webApp: WebApp, themeParams: ThemeParams): void {
89
58
  * @param themeParams - ThemeParams instance.
90
59
  */
91
60
  export function bindThemeCSSVariables(themeParams: ThemeParams): void {
92
- const actualize = () => createThemeVariables(themeParams);
61
+ const actualize = () => {
62
+ const state = themeParams.getState();
63
+
64
+ Object.entries(state).forEach(([k, v]) => {
65
+ const key = k.replace(/[A-Z]/g, (match) => `-${match.toLowerCase()}`);
66
+ setColorVariable(`--tg-theme-${key}`, v || null);
67
+ });
68
+ };
93
69
 
94
70
  themeParams.on('changed', actualize);
95
71
 
@@ -1,6 +1,6 @@
1
1
  import type { EventEmitter } from '@tma.js/event-emitter';
2
2
 
3
- import type { StateEvents, StringKeys } from './types.js';
3
+ import type { StateEvents } from './types.js';
4
4
 
5
5
  /**
6
6
  * Represents state which is observable via passed EventEmitter.
@@ -15,18 +15,18 @@ export class State<S extends object> {
15
15
  }
16
16
  }
17
17
 
18
- private internalSet<K extends StringKeys<S>>(key: K, value: S[K]): boolean {
18
+ private internalSet<K extends keyof S>(key: K, value: S[K]): boolean {
19
19
  if (this.state[key] === value) {
20
20
  return false;
21
21
  }
22
22
 
23
23
  this.state[key] = value;
24
- this.emit(`${key}Changed`, value);
24
+ this.emit(`${String(key)}Changed`, value);
25
25
 
26
26
  return true;
27
27
  }
28
28
 
29
- set<K extends StringKeys<S>>(key: K, value: S[K]): void;
29
+ set<K extends keyof S>(key: K, value: S[K]): void;
30
30
  set(state: Partial<S>): void;
31
31
  set(keyOrState: any, value?: any): void {
32
32
  let didChange = false;
@@ -47,7 +47,11 @@ export class State<S extends object> {
47
47
  }
48
48
  }
49
49
 
50
- get<K extends StringKeys<S>>(key: K): Readonly<S[K]> {
50
+ get<K extends keyof S>(key: K): Readonly<S[K]> {
51
51
  return this.state[key];
52
52
  }
53
+
54
+ getState() {
55
+ return { ...this.state };
56
+ }
53
57
  }