@tma.js/sdk-react 0.4.12 → 0.4.14

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 (56) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +28 -28
  3. package/dist/index.cjs.map +1 -1
  4. package/dist/index.mjs.map +1 -1
  5. package/package.json +68 -68
  6. package/src/index.ts +2 -2
  7. package/src/lib/back-button/index.ts +3 -3
  8. package/src/lib/back-button/types.ts +1 -1
  9. package/src/lib/back-button/useBackButton.ts +13 -13
  10. package/src/lib/back-button/withBackButton.tsx +18 -18
  11. package/src/lib/closing-behavior/index.ts +3 -3
  12. package/src/lib/closing-behavior/types.ts +1 -1
  13. package/src/lib/closing-behavior/useClosingBehaviour.ts +13 -13
  14. package/src/lib/closing-behavior/withClosingBehaviour.tsx +18 -18
  15. package/src/lib/cloud-storage.tsx +28 -28
  16. package/src/lib/haptic-feedback/index.ts +3 -3
  17. package/src/lib/haptic-feedback/types.ts +1 -1
  18. package/src/lib/haptic-feedback/useHapticFeedback.ts +9 -9
  19. package/src/lib/haptic-feedback/withHapticFeedback.tsx +18 -18
  20. package/src/lib/hooks.ts +52 -52
  21. package/src/lib/index.ts +13 -13
  22. package/src/lib/init-data/index.ts +3 -3
  23. package/src/lib/init-data/types.ts +1 -1
  24. package/src/lib/init-data/useInitData.ts +9 -9
  25. package/src/lib/init-data/withInitData.tsx +18 -18
  26. package/src/lib/launch-params/index.ts +3 -3
  27. package/src/lib/launch-params/types.ts +1 -1
  28. package/src/lib/launch-params/useLaunchParams.ts +11 -11
  29. package/src/lib/launch-params/withLaunchParams.tsx +18 -18
  30. package/src/lib/main-button/index.ts +3 -3
  31. package/src/lib/main-button/types.ts +1 -1
  32. package/src/lib/main-button/useMainButton.ts +16 -16
  33. package/src/lib/main-button/withMainButton.tsx +18 -18
  34. package/src/lib/popup/index.ts +3 -3
  35. package/src/lib/popup/types.ts +1 -1
  36. package/src/lib/popup/usePopup.ts +13 -13
  37. package/src/lib/popup/withPopup.tsx +18 -18
  38. package/src/lib/qr-scanner/index.ts +3 -3
  39. package/src/lib/qr-scanner/types.ts +1 -1
  40. package/src/lib/qr-scanner/useQRScanner.ts +13 -13
  41. package/src/lib/qr-scanner/withQRScanner.tsx +18 -18
  42. package/src/lib/theme-params.tsx +29 -29
  43. package/src/lib/viewport/index.ts +3 -3
  44. package/src/lib/viewport/types.ts +1 -1
  45. package/src/lib/viewport/useViewport.ts +15 -15
  46. package/src/lib/viewport/withViewport.tsx +18 -18
  47. package/src/lib/web-app/index.ts +3 -3
  48. package/src/lib/web-app/types.ts +1 -1
  49. package/src/lib/web-app/useWebApp.ts +13 -13
  50. package/src/lib/web-app/withWebApp.tsx +18 -18
  51. package/src/provider/SDKProvider.tsx +47 -47
  52. package/src/provider/context.ts +11 -11
  53. package/src/provider/hocs.tsx +18 -18
  54. package/src/provider/hooks.ts +23 -23
  55. package/src/provider/index.ts +5 -5
  56. package/src/provider/types.ts +34 -34
@@ -1,18 +1,18 @@
1
- import React, { type ComponentType } from 'react';
2
-
3
- import { useQRScanner } from './useQRScanner.js';
4
- import type { QRScanner } from './types.js';
5
-
6
- /**
7
- * HOC which passes QRScanner SDK component to wrapped React component.
8
- * @param Component - component to wrap.
9
- */
10
- export function withQRScanner<P extends { qrScanner?: QRScanner }>(
11
- Component: ComponentType<P>,
12
- ): ComponentType<Omit<P, 'qrScanner'>> {
13
- return function WithQRScanner(props: Omit<P, 'qrScanner'>) {
14
- const p = { ...props, qrScanner: useQRScanner() } as P;
15
-
16
- return <Component {...p} />;
17
- };
18
- }
1
+ import React, { type ComponentType } from 'react';
2
+
3
+ import { useQRScanner } from './useQRScanner.js';
4
+ import type { QRScanner } from './types.js';
5
+
6
+ /**
7
+ * HOC which passes QRScanner SDK component to wrapped React component.
8
+ * @param Component - component to wrap.
9
+ */
10
+ export function withQRScanner<P extends { qrScanner?: QRScanner }>(
11
+ Component: ComponentType<P>,
12
+ ): ComponentType<Omit<P, 'qrScanner'>> {
13
+ return function WithQRScanner(props: Omit<P, 'qrScanner'>) {
14
+ const p = { ...props, qrScanner: useQRScanner() } as P;
15
+
16
+ return <Component {...p} />;
17
+ };
18
+ }
@@ -1,29 +1,29 @@
1
- import React, { type ComponentType } from 'react';
2
- import type { ThemeParams } from '@tma.js/sdk';
3
-
4
- import { useUnit } from '../provider/index.js';
5
- import { useEventsTracking } from './hooks.js';
6
-
7
- /**
8
- * Returns ThemeParams component instance.
9
- */
10
- export function useThemeParams(): ThemeParams {
11
- const theme = useUnit('themeParams');
12
- useEventsTracking(theme, ['changed']);
13
-
14
- return theme;
15
- }
16
-
17
- /**
18
- * HOC which passes ThemeParams SDK component to wrapped React component.
19
- * @param Component - component to wrap.
20
- */
21
- export function withThemeParams<P extends { themeParams?: ThemeParams }>(
22
- Component: ComponentType<P>,
23
- ): ComponentType<Omit<P, 'themeParams'>> {
24
- return function WithThemeParams(props: Omit<P, 'themeParams'>) {
25
- const p = { ...props, themeParams: useThemeParams() } as P;
26
-
27
- return <Component {...p} />;
28
- };
29
- }
1
+ import React, { type ComponentType } from 'react';
2
+ import type { ThemeParams } from '@tma.js/sdk';
3
+
4
+ import { useUnit } from '../provider/index.js';
5
+ import { useEventsTracking } from './hooks.js';
6
+
7
+ /**
8
+ * Returns ThemeParams component instance.
9
+ */
10
+ export function useThemeParams(): ThemeParams {
11
+ const theme = useUnit('themeParams');
12
+ useEventsTracking(theme, ['changed']);
13
+
14
+ return theme;
15
+ }
16
+
17
+ /**
18
+ * HOC which passes ThemeParams SDK component to wrapped React component.
19
+ * @param Component - component to wrap.
20
+ */
21
+ export function withThemeParams<P extends { themeParams?: ThemeParams }>(
22
+ Component: ComponentType<P>,
23
+ ): ComponentType<Omit<P, 'themeParams'>> {
24
+ return function WithThemeParams(props: Omit<P, 'themeParams'>) {
25
+ const p = { ...props, themeParams: useThemeParams() } as P;
26
+
27
+ return <Component {...p} />;
28
+ };
29
+ }
@@ -1,3 +1,3 @@
1
- export * from './types.js';
2
- export * from './useViewport.js';
3
- export * from './withViewport.js';
1
+ export * from './types.js';
2
+ export * from './useViewport.js';
3
+ export * from './withViewport.js';
@@ -1 +1 @@
1
- export type { Viewport } from '@tma.js/sdk';
1
+ export type { Viewport } from '@tma.js/sdk';
@@ -1,15 +1,15 @@
1
- import { useUnit } from '../../provider/index.js';
2
- import { useEventsTracking } from '../hooks.js';
3
- import type { Viewport } from './types.js';
4
-
5
- /**
6
- * Returns Viewport component instance.
7
- */
8
- export function useViewport(): Viewport {
9
- const viewport = useUnit('viewport');
10
- useEventsTracking(viewport, [
11
- 'stableHeightChanged', 'isExpandedChanged', 'heightChanged', 'widthChanged',
12
- ]);
13
-
14
- return viewport;
15
- }
1
+ import { useUnit } from '../../provider/index.js';
2
+ import { useEventsTracking } from '../hooks.js';
3
+ import type { Viewport } from './types.js';
4
+
5
+ /**
6
+ * Returns Viewport component instance.
7
+ */
8
+ export function useViewport(): Viewport {
9
+ const viewport = useUnit('viewport');
10
+ useEventsTracking(viewport, [
11
+ 'stableHeightChanged', 'isExpandedChanged', 'heightChanged', 'widthChanged',
12
+ ]);
13
+
14
+ return viewport;
15
+ }
@@ -1,18 +1,18 @@
1
- import React, { type ComponentType } from 'react';
2
-
3
- import { useViewport } from './useViewport.js';
4
- import type { Viewport } from './types.js';
5
-
6
- /**
7
- * HOC which passes Viewport SDK component to wrapped React component.
8
- * @param Component - component to wrap.
9
- */
10
- export function withViewport<P extends { viewport?: Viewport }>(
11
- Component: ComponentType<P>,
12
- ): ComponentType<Omit<P, 'viewport'>> {
13
- return function WithViewport(props: Omit<P, 'viewport'>) {
14
- const p = { ...props, viewport: useViewport() } as P;
15
-
16
- return <Component {...p} />;
17
- };
18
- }
1
+ import React, { type ComponentType } from 'react';
2
+
3
+ import { useViewport } from './useViewport.js';
4
+ import type { Viewport } from './types.js';
5
+
6
+ /**
7
+ * HOC which passes Viewport SDK component to wrapped React component.
8
+ * @param Component - component to wrap.
9
+ */
10
+ export function withViewport<P extends { viewport?: Viewport }>(
11
+ Component: ComponentType<P>,
12
+ ): ComponentType<Omit<P, 'viewport'>> {
13
+ return function WithViewport(props: Omit<P, 'viewport'>) {
14
+ const p = { ...props, viewport: useViewport() } as P;
15
+
16
+ return <Component {...p} />;
17
+ };
18
+ }
@@ -1,3 +1,3 @@
1
- export * from './types.js';
2
- export * from './useWebApp.js';
3
- export * from './withWebApp.js';
1
+ export * from './types.js';
2
+ export * from './useWebApp.js';
3
+ export * from './withWebApp.js';
@@ -1 +1 @@
1
- export type { WebApp } from '@tma.js/sdk';
1
+ export type { WebApp } from '@tma.js/sdk';
@@ -1,13 +1,13 @@
1
- import { useUnit } from '../../provider/index.js';
2
- import type { WebApp } from './types.js';
3
- import { useEventsTracking } from '../hooks.js';
4
-
5
- /**
6
- * Returns WebApp component instance.
7
- */
8
- export function useWebApp(): WebApp {
9
- const webApp = useUnit('webApp');
10
- useEventsTracking(webApp, ['headerColorChanged', 'backgroundColorChanged']);
11
-
12
- return webApp;
13
- }
1
+ import { useUnit } from '../../provider/index.js';
2
+ import type { WebApp } from './types.js';
3
+ import { useEventsTracking } from '../hooks.js';
4
+
5
+ /**
6
+ * Returns WebApp component instance.
7
+ */
8
+ export function useWebApp(): WebApp {
9
+ const webApp = useUnit('webApp');
10
+ useEventsTracking(webApp, ['headerColorChanged', 'backgroundColorChanged']);
11
+
12
+ return webApp;
13
+ }
@@ -1,18 +1,18 @@
1
- import React, { type ComponentType } from 'react';
2
-
3
- import { useWebApp } from './useWebApp.js';
4
- import type { WebApp } from './types.js';
5
-
6
- /**
7
- * HOC which passes WebApp SDK component to wrapped React component.
8
- * @param Component - component to wrap.
9
- */
10
- export function withWebApp<P extends { webApp?: WebApp }>(
11
- Component: ComponentType<P>,
12
- ): ComponentType<Omit<P, 'webApp'>> {
13
- return function WithWebApp(props: Omit<P, 'webApp'>) {
14
- const p = { ...props, webApp: useWebApp() } as P;
15
-
16
- return <Component {...p} />;
17
- };
18
- }
1
+ import React, { type ComponentType } from 'react';
2
+
3
+ import { useWebApp } from './useWebApp.js';
4
+ import type { WebApp } from './types.js';
5
+
6
+ /**
7
+ * HOC which passes WebApp SDK component to wrapped React component.
8
+ * @param Component - component to wrap.
9
+ */
10
+ export function withWebApp<P extends { webApp?: WebApp }>(
11
+ Component: ComponentType<P>,
12
+ ): ComponentType<Omit<P, 'webApp'>> {
13
+ return function WithWebApp(props: Omit<P, 'webApp'>) {
14
+ const p = { ...props, webApp: useWebApp() } as P;
15
+
16
+ return <Component {...p} />;
17
+ };
18
+ }
@@ -1,47 +1,47 @@
1
- import React, {
2
- memo,
3
- useEffect,
4
- useMemo,
5
- useState,
6
- type PropsWithChildren,
7
- } from 'react';
8
- import { init, type InitOptions } from '@tma.js/sdk';
9
-
10
- import { sdkContext } from './context.js';
11
- import type { SDKComponents, SDKContext } from './types.js';
12
-
13
- export type SDKInitOptions = InitOptions;
14
-
15
- export type SDKProviderProps = PropsWithChildren<{ initOptions?: SDKInitOptions }>;
16
-
17
- /**
18
- * Component which provides access to SDK components.
19
- */
20
- export const SDKProvider = memo<SDKProviderProps>((props) => {
21
- const { children, initOptions } = props;
22
- const [didInit, setDidInit] = useState(false);
23
- const [error, setError] = useState<null | unknown>(null);
24
- const [components, setComponents] = useState<SDKComponents | null>(null);
25
-
26
- // Initialize SDK on DOM attach.
27
- useEffect(() => {
28
- // Update init status.
29
- setDidInit(true);
30
-
31
- // Init SDK.
32
- init(initOptions)
33
- .then(setComponents)
34
- .catch(setError);
35
- // eslint-disable-next-line react-hooks/exhaustive-deps
36
- }, []);
37
-
38
- const context = useMemo<SDKContext>(() => ({
39
- components,
40
- didInit,
41
- error,
42
- }), [didInit, components, error]);
43
-
44
- return <sdkContext.Provider value={context}>{children}</sdkContext.Provider>;
45
- });
46
-
47
- SDKProvider.displayName = 'SDKProvider';
1
+ import React, {
2
+ memo,
3
+ useEffect,
4
+ useMemo,
5
+ useState,
6
+ type PropsWithChildren,
7
+ } from 'react';
8
+ import { init, type InitOptions } from '@tma.js/sdk';
9
+
10
+ import { sdkContext } from './context.js';
11
+ import type { SDKComponents, SDKContext } from './types.js';
12
+
13
+ export type SDKInitOptions = InitOptions;
14
+
15
+ export type SDKProviderProps = PropsWithChildren<{ initOptions?: SDKInitOptions }>;
16
+
17
+ /**
18
+ * Component which provides access to SDK components.
19
+ */
20
+ export const SDKProvider = memo<SDKProviderProps>((props) => {
21
+ const { children, initOptions } = props;
22
+ const [didInit, setDidInit] = useState(false);
23
+ const [error, setError] = useState<null | unknown>(null);
24
+ const [components, setComponents] = useState<SDKComponents | null>(null);
25
+
26
+ // Initialize SDK on DOM attach.
27
+ useEffect(() => {
28
+ // Update init status.
29
+ setDidInit(true);
30
+
31
+ // Init SDK.
32
+ init(initOptions)
33
+ .then(setComponents)
34
+ .catch(setError);
35
+ // eslint-disable-next-line react-hooks/exhaustive-deps
36
+ }, []);
37
+
38
+ const context = useMemo<SDKContext>(() => ({
39
+ components,
40
+ didInit,
41
+ error,
42
+ }), [didInit, components, error]);
43
+
44
+ return <sdkContext.Provider value={context}>{children}</sdkContext.Provider>;
45
+ });
46
+
47
+ SDKProvider.displayName = 'SDKProvider';
@@ -1,11 +1,11 @@
1
- import { createContext } from 'react';
2
-
3
- import type { SDKContext } from './types.js';
4
-
5
- export const sdkContext = createContext<SDKContext>({
6
- components: null,
7
- didInit: false,
8
- error: null,
9
- });
10
-
11
- sdkContext.displayName = 'SDKContext';
1
+ import { createContext } from 'react';
2
+
3
+ import type { SDKContext } from './types.js';
4
+
5
+ export const sdkContext = createContext<SDKContext>({
6
+ components: null,
7
+ didInit: false,
8
+ error: null,
9
+ });
10
+
11
+ sdkContext.displayName = 'SDKContext';
@@ -1,18 +1,18 @@
1
- import React, { type ComponentType } from 'react';
2
-
3
- import type { SDKContext } from './types.js';
4
- import { useSDK } from './hooks.js';
5
-
6
- /**
7
- * HOC which passes SDK context to wrapped React component.
8
- * @param Component - component to wrap.
9
- */
10
- export function withSDK<P extends { sdk?: SDKContext }>(
11
- Component: ComponentType<P>,
12
- ): ComponentType<Omit<P, 'sdk'>> {
13
- return function WithLaunchParams(props: Omit<P, 'sdk'>) {
14
- const p = { ...props, sdk: useSDK() } as P;
15
-
16
- return <Component {...p} />;
17
- };
18
- }
1
+ import React, { type ComponentType } from 'react';
2
+
3
+ import type { SDKContext } from './types.js';
4
+ import { useSDK } from './hooks.js';
5
+
6
+ /**
7
+ * HOC which passes SDK context to wrapped React component.
8
+ * @param Component - component to wrap.
9
+ */
10
+ export function withSDK<P extends { sdk?: SDKContext }>(
11
+ Component: ComponentType<P>,
12
+ ): ComponentType<Omit<P, 'sdk'>> {
13
+ return function WithLaunchParams(props: Omit<P, 'sdk'>) {
14
+ const p = { ...props, sdk: useSDK() } as P;
15
+
16
+ return <Component {...p} />;
17
+ };
18
+ }
@@ -1,23 +1,23 @@
1
- import { useContext } from 'react';
2
-
3
- import { sdkContext } from './context.js';
4
- import type { SDKComponent, SDKComponentName } from './types.js';
5
-
6
- /**
7
- * Returns current SDK information.
8
- */
9
- export const useSDK = () => useContext(sdkContext);
10
-
11
- /**
12
- * Returns value by its field name from SDK init result.
13
- * @param name - component name.
14
- * @throws {Error} SDK is not initialized.
15
- */
16
- export function useUnit<N extends SDKComponentName>(name: N): SDKComponent<N> {
17
- const { components } = useSDK();
18
-
19
- if (components === null) {
20
- throw new Error(`Unable to get unit "${name}" as long as SDK is not initialized.`);
21
- }
22
- return components[name];
23
- }
1
+ import { useContext } from 'react';
2
+
3
+ import { sdkContext } from './context.js';
4
+ import type { SDKComponent, SDKComponentName } from './types.js';
5
+
6
+ /**
7
+ * Returns current SDK information.
8
+ */
9
+ export const useSDK = () => useContext(sdkContext);
10
+
11
+ /**
12
+ * Returns value by its field name from SDK init result.
13
+ * @param name - component name.
14
+ * @throws {Error} SDK is not initialized.
15
+ */
16
+ export function useUnit<N extends SDKComponentName>(name: N): SDKComponent<N> {
17
+ const { components } = useSDK();
18
+
19
+ if (components === null) {
20
+ throw new Error(`Unable to get unit "${name}" as long as SDK is not initialized.`);
21
+ }
22
+ return components[name];
23
+ }
@@ -1,5 +1,5 @@
1
- export * from './context.js';
2
- export * from './hocs.js';
3
- export * from './hooks.js';
4
- export * from './SDKProvider.js';
5
- export * from './types.js';
1
+ export * from './context.js';
2
+ export * from './hocs.js';
3
+ export * from './hooks.js';
4
+ export * from './SDKProvider.js';
5
+ export * from './types.js';
@@ -1,34 +1,34 @@
1
- import type { InitResult } from '@tma.js/sdk';
2
-
3
- /**
4
- * List of provided SDK components.
5
- */
6
- export type SDKComponents = InitResult;
7
-
8
- /**
9
- * Known SDK component name.
10
- */
11
- export type SDKComponentName = keyof SDKComponents;
12
-
13
- /**
14
- * Returns SDK component type by its name.
15
- */
16
- export type SDKComponent<N extends SDKComponentName> = SDKComponents[N];
17
-
18
- export interface SDKContext {
19
- /**
20
- * Components ready to use. This value will be null until initialization
21
- * is done.
22
- */
23
- components: SDKComponents | null;
24
-
25
- /**
26
- * States, that SDK called `init()` method.
27
- */
28
- didInit: boolean;
29
-
30
- /**
31
- * Error occurred during initialization.
32
- */
33
- error: unknown | null;
34
- }
1
+ import type { InitResult } from '@tma.js/sdk';
2
+
3
+ /**
4
+ * List of provided SDK components.
5
+ */
6
+ export type SDKComponents = InitResult;
7
+
8
+ /**
9
+ * Known SDK component name.
10
+ */
11
+ export type SDKComponentName = keyof SDKComponents;
12
+
13
+ /**
14
+ * Returns SDK component type by its name.
15
+ */
16
+ export type SDKComponent<N extends SDKComponentName> = SDKComponents[N];
17
+
18
+ export interface SDKContext {
19
+ /**
20
+ * Components ready to use. This value will be null until initialization
21
+ * is done.
22
+ */
23
+ components: SDKComponents | null;
24
+
25
+ /**
26
+ * States, that SDK called `init()` method.
27
+ */
28
+ didInit: boolean;
29
+
30
+ /**
31
+ * Error occurred during initialization.
32
+ */
33
+ error: unknown | null;
34
+ }