@tma.js/sdk-react 0.4.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.
Files changed (117) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +28 -0
  3. package/dist/lib/browser.js +2 -0
  4. package/dist/lib/browser.js.map +1 -0
  5. package/dist/lib/index.cjs +2 -0
  6. package/dist/lib/index.cjs.map +1 -0
  7. package/dist/lib/index.mjs +2 -0
  8. package/dist/lib/index.mjs.map +1 -0
  9. package/dist/types/index.d.ts +2 -0
  10. package/dist/types/lib/back-button/index.d.ts +3 -0
  11. package/dist/types/lib/back-button/types.d.ts +1 -0
  12. package/dist/types/lib/back-button/useBackButton.d.ts +5 -0
  13. package/dist/types/lib/back-button/withBackButton.d.ts +9 -0
  14. package/dist/types/lib/closing-behavior/index.d.ts +3 -0
  15. package/dist/types/lib/closing-behavior/types.d.ts +1 -0
  16. package/dist/types/lib/closing-behavior/useClosingBehaviour.d.ts +5 -0
  17. package/dist/types/lib/closing-behavior/withClosingBehaviour.d.ts +9 -0
  18. package/dist/types/lib/cloud-storage.d.ts +14 -0
  19. package/dist/types/lib/haptic-feedback/index.d.ts +3 -0
  20. package/dist/types/lib/haptic-feedback/types.d.ts +1 -0
  21. package/dist/types/lib/haptic-feedback/useHapticFeedback.d.ts +5 -0
  22. package/dist/types/lib/haptic-feedback/withHapticFeedback.d.ts +9 -0
  23. package/dist/types/lib/hooks.d.ts +26 -0
  24. package/dist/types/lib/index.d.ts +13 -0
  25. package/dist/types/lib/init-data/index.d.ts +3 -0
  26. package/dist/types/lib/init-data/types.d.ts +1 -0
  27. package/dist/types/lib/init-data/useInitData.d.ts +5 -0
  28. package/dist/types/lib/init-data/withInitData.d.ts +9 -0
  29. package/dist/types/lib/launch-params/index.d.ts +3 -0
  30. package/dist/types/lib/launch-params/types.d.ts +1 -0
  31. package/dist/types/lib/launch-params/useLaunchParams.d.ts +5 -0
  32. package/dist/types/lib/launch-params/withLaunchParams.d.ts +9 -0
  33. package/dist/types/lib/main-button/index.d.ts +3 -0
  34. package/dist/types/lib/main-button/types.d.ts +1 -0
  35. package/dist/types/lib/main-button/useMainButton.d.ts +5 -0
  36. package/dist/types/lib/main-button/withMainButton.d.ts +9 -0
  37. package/dist/types/lib/popup/index.d.ts +3 -0
  38. package/dist/types/lib/popup/types.d.ts +1 -0
  39. package/dist/types/lib/popup/usePopup.d.ts +5 -0
  40. package/dist/types/lib/popup/withPopup.d.ts +9 -0
  41. package/dist/types/lib/qr-scanner/index.d.ts +3 -0
  42. package/dist/types/lib/qr-scanner/types.d.ts +1 -0
  43. package/dist/types/lib/qr-scanner/useQRScanner.d.ts +5 -0
  44. package/dist/types/lib/qr-scanner/withQRScanner.d.ts +9 -0
  45. package/dist/types/lib/theme-params/index.d.ts +3 -0
  46. package/dist/types/lib/theme-params/types.d.ts +1 -0
  47. package/dist/types/lib/theme-params/useThemeParams.d.ts +5 -0
  48. package/dist/types/lib/theme-params/withThemeParams.d.ts +9 -0
  49. package/dist/types/lib/viewport/index.d.ts +3 -0
  50. package/dist/types/lib/viewport/types.d.ts +1 -0
  51. package/dist/types/lib/viewport/useViewport.d.ts +5 -0
  52. package/dist/types/lib/viewport/withViewport.d.ts +9 -0
  53. package/dist/types/lib/web-app/index.d.ts +3 -0
  54. package/dist/types/lib/web-app/types.d.ts +1 -0
  55. package/dist/types/lib/web-app/useWebApp.d.ts +5 -0
  56. package/dist/types/lib/web-app/withWebApp.d.ts +9 -0
  57. package/dist/types/provider/SDKProvider.d.ts +10 -0
  58. package/dist/types/provider/context.d.ts +3 -0
  59. package/dist/types/provider/hocs.d.ts +9 -0
  60. package/dist/types/provider/hooks.d.ts +11 -0
  61. package/dist/types/provider/index.d.ts +5 -0
  62. package/dist/types/provider/types.d.ts +28 -0
  63. package/package.json +70 -0
  64. package/src/index.ts +2 -0
  65. package/src/lib/back-button/index.ts +3 -0
  66. package/src/lib/back-button/types.ts +1 -0
  67. package/src/lib/back-button/useBackButton.ts +13 -0
  68. package/src/lib/back-button/withBackButton.tsx +18 -0
  69. package/src/lib/closing-behavior/index.ts +3 -0
  70. package/src/lib/closing-behavior/types.ts +1 -0
  71. package/src/lib/closing-behavior/useClosingBehaviour.ts +13 -0
  72. package/src/lib/closing-behavior/withClosingBehaviour.tsx +18 -0
  73. package/src/lib/cloud-storage.tsx +28 -0
  74. package/src/lib/haptic-feedback/index.ts +3 -0
  75. package/src/lib/haptic-feedback/types.ts +1 -0
  76. package/src/lib/haptic-feedback/useHapticFeedback.ts +9 -0
  77. package/src/lib/haptic-feedback/withHapticFeedback.tsx +18 -0
  78. package/src/lib/hooks.ts +51 -0
  79. package/src/lib/index.ts +13 -0
  80. package/src/lib/init-data/index.ts +3 -0
  81. package/src/lib/init-data/types.ts +1 -0
  82. package/src/lib/init-data/useInitData.ts +9 -0
  83. package/src/lib/init-data/withInitData.tsx +18 -0
  84. package/src/lib/launch-params/index.ts +3 -0
  85. package/src/lib/launch-params/types.ts +1 -0
  86. package/src/lib/launch-params/useLaunchParams.ts +11 -0
  87. package/src/lib/launch-params/withLaunchParams.tsx +18 -0
  88. package/src/lib/main-button/index.ts +3 -0
  89. package/src/lib/main-button/types.ts +1 -0
  90. package/src/lib/main-button/useMainButton.ts +16 -0
  91. package/src/lib/main-button/withMainButton.tsx +18 -0
  92. package/src/lib/popup/index.ts +3 -0
  93. package/src/lib/popup/types.ts +1 -0
  94. package/src/lib/popup/usePopup.ts +13 -0
  95. package/src/lib/popup/withPopup.tsx +18 -0
  96. package/src/lib/qr-scanner/index.ts +3 -0
  97. package/src/lib/qr-scanner/types.ts +1 -0
  98. package/src/lib/qr-scanner/useQRScanner.ts +13 -0
  99. package/src/lib/qr-scanner/withQRScanner.tsx +18 -0
  100. package/src/lib/theme-params/index.ts +3 -0
  101. package/src/lib/theme-params/types.ts +1 -0
  102. package/src/lib/theme-params/useThemeParams.ts +13 -0
  103. package/src/lib/theme-params/withThemeParams.tsx +18 -0
  104. package/src/lib/viewport/index.ts +3 -0
  105. package/src/lib/viewport/types.ts +1 -0
  106. package/src/lib/viewport/useViewport.ts +15 -0
  107. package/src/lib/viewport/withViewport.tsx +18 -0
  108. package/src/lib/web-app/index.ts +3 -0
  109. package/src/lib/web-app/types.ts +1 -0
  110. package/src/lib/web-app/useWebApp.ts +13 -0
  111. package/src/lib/web-app/withWebApp.tsx +18 -0
  112. package/src/provider/SDKProvider.tsx +47 -0
  113. package/src/provider/context.ts +11 -0
  114. package/src/provider/hocs.tsx +18 -0
  115. package/src/provider/hooks.ts +23 -0
  116. package/src/provider/index.ts +5 -0
  117. package/src/provider/types.ts +34 -0
@@ -0,0 +1 @@
1
+ export type { HapticFeedback } from '@tma.js/sdk';
@@ -0,0 +1,5 @@
1
+ import type { HapticFeedback } from './types.js';
2
+ /**
3
+ * Returns HapticFeedback component instance.
4
+ */
5
+ export declare function useHapticFeedback(): HapticFeedback;
@@ -0,0 +1,9 @@
1
+ import { type ComponentType } from 'react';
2
+ import type { HapticFeedback } from './types.js';
3
+ /**
4
+ * HOC which passes HapticFeedback SDK component to wrapped React component.
5
+ * @param Component - component to wrap.
6
+ */
7
+ export declare function withHapticFeedback<P extends {
8
+ hapticFeedback?: HapticFeedback;
9
+ }>(Component: ComponentType<P>): ComponentType<Omit<P, 'hapticFeedback'>>;
@@ -0,0 +1,26 @@
1
+ import type { PostEvent } from '@tma.js/sdk/types';
2
+ interface Trackable {
3
+ on: (event: any, ...args: any[]) => void;
4
+ off: (event: any, ...args: any[]) => void;
5
+ }
6
+ type EventName<T extends Trackable> = T extends {
7
+ on(event: infer E, ...args: any[]): any;
8
+ } ? E : never;
9
+ /**
10
+ * The hook which returns function used for force update.
11
+ */
12
+ export declare function useForceUpdate(): (() => void);
13
+ /**
14
+ * The hook which listens to the specified object events and calls force update
15
+ * in case, some of them was emitted.
16
+ * @param trackable - object which allows events listening.
17
+ * @param events - events to listen. This array will be memoized during first
18
+ * hook call.
19
+ */
20
+ export declare function useEventsTracking<T extends Trackable>(trackable: T, events: EventName<T>[]): void;
21
+ /**
22
+ * Returns event posting function which was created during SDK
23
+ * initialization.
24
+ */
25
+ export declare function usePostEvent(): PostEvent;
26
+ export {};
@@ -0,0 +1,13 @@
1
+ export * from './back-button/index.js';
2
+ export * from './closing-behavior/index.js';
3
+ export * from './haptic-feedback/index.js';
4
+ export * from './init-data/index.js';
5
+ export * from './launch-params/index.js';
6
+ export * from './main-button/index.js';
7
+ export * from './popup/index.js';
8
+ export * from './qr-scanner/index.js';
9
+ export * from './theme-params/index.js';
10
+ export * from './viewport/index.js';
11
+ export * from './web-app/index.js';
12
+ export * from './cloud-storage.js';
13
+ export { usePostEvent } from './hooks.js';
@@ -0,0 +1,3 @@
1
+ export * from './types.js';
2
+ export * from './useInitData.js';
3
+ export * from './withInitData.js';
@@ -0,0 +1 @@
1
+ export type { InitData } from '@tma.js/sdk';
@@ -0,0 +1,5 @@
1
+ import type { InitData } from './types.js';
2
+ /**
3
+ * Returns InitData component instance.
4
+ */
5
+ export declare function useInitData(): InitData | null;
@@ -0,0 +1,9 @@
1
+ import { type ComponentType } from 'react';
2
+ import type { InitData } from './types.js';
3
+ /**
4
+ * HOC which passes InitData SDK component to wrapped React component.
5
+ * @param Component - component to wrap.
6
+ */
7
+ export declare function withInitData<P extends {
8
+ initData?: InitData;
9
+ }>(Component: ComponentType<P>): ComponentType<Omit<P, 'initData'>>;
@@ -0,0 +1,3 @@
1
+ export * from './types.js';
2
+ export * from './useLaunchParams.js';
3
+ export * from './withLaunchParams.js';
@@ -0,0 +1 @@
1
+ export type { LaunchParams } from '@tma.js/sdk';
@@ -0,0 +1,5 @@
1
+ import type { LaunchParams } from './types.js';
2
+ /**
3
+ * The hook which returns launch parameters initially passed to Web App.
4
+ */
5
+ export declare function useLaunchParams(): LaunchParams;
@@ -0,0 +1,9 @@
1
+ import { type ComponentType } from 'react';
2
+ import type { LaunchParams } from './types.js';
3
+ /**
4
+ * HOC which passes launch parameters to wrapped React component.
5
+ * @param Component - component to wrap.
6
+ */
7
+ export declare function withLaunchParams<P extends {
8
+ launchParams?: LaunchParams;
9
+ }>(Component: ComponentType<P>): ComponentType<Omit<P, 'launchParams'>>;
@@ -0,0 +1,3 @@
1
+ export * from './types.js';
2
+ export * from './useMainButton.js';
3
+ export * from './withMainButton.js';
@@ -0,0 +1 @@
1
+ export type { MainButton } from '@tma.js/sdk';
@@ -0,0 +1,5 @@
1
+ import type { MainButton } from './types.js';
2
+ /**
3
+ * Returns MainButton component instance.
4
+ */
5
+ export declare function useMainButton(): MainButton;
@@ -0,0 +1,9 @@
1
+ import { type ComponentType } from 'react';
2
+ import type { MainButton } from './types.js';
3
+ /**
4
+ * HOC which passes MainButton SDK component to wrapped React component.
5
+ * @param Component - component to wrap.
6
+ */
7
+ export declare function withMainButton<P extends {
8
+ mainButton?: MainButton;
9
+ }>(Component: ComponentType<P>): ComponentType<Omit<P, 'mainButton'>>;
@@ -0,0 +1,3 @@
1
+ export * from './types.js';
2
+ export * from './usePopup.js';
3
+ export * from './withPopup.js';
@@ -0,0 +1 @@
1
+ export type { Popup } from '@tma.js/sdk';
@@ -0,0 +1,5 @@
1
+ import type { Popup } from './types.js';
2
+ /**
3
+ * Returns Popup component instance.
4
+ */
5
+ export declare function usePopup(): Popup;
@@ -0,0 +1,9 @@
1
+ import { type ComponentType } from 'react';
2
+ import type { Popup } from './types.js';
3
+ /**
4
+ * HOC which passes Popup SDK component to wrapped React component.
5
+ * @param Component - component to wrap.
6
+ */
7
+ export declare function withPopup<P extends {
8
+ popup?: Popup;
9
+ }>(Component: ComponentType<P>): ComponentType<Omit<P, 'popup'>>;
@@ -0,0 +1,3 @@
1
+ export * from './types.js';
2
+ export * from './useQRScanner.js';
3
+ export * from './withQRScanner.js';
@@ -0,0 +1 @@
1
+ export type { QRScanner } from '@tma.js/sdk';
@@ -0,0 +1,5 @@
1
+ import type { QRScanner } from './types.js';
2
+ /**
3
+ * Returns QRScanner component instance.
4
+ */
5
+ export declare function useQRScanner(): QRScanner;
@@ -0,0 +1,9 @@
1
+ import { type ComponentType } from 'react';
2
+ import type { QRScanner } from './types.js';
3
+ /**
4
+ * HOC which passes QRScanner SDK component to wrapped React component.
5
+ * @param Component - component to wrap.
6
+ */
7
+ export declare function withQRScanner<P extends {
8
+ qrScanner?: QRScanner;
9
+ }>(Component: ComponentType<P>): ComponentType<Omit<P, 'qrScanner'>>;
@@ -0,0 +1,3 @@
1
+ export * from './types.js';
2
+ export * from './useThemeParams.js';
3
+ export * from './withThemeParams.js';
@@ -0,0 +1 @@
1
+ export type { ThemeParams } from '@tma.js/sdk';
@@ -0,0 +1,5 @@
1
+ import type { ThemeParams } from './types.js';
2
+ /**
3
+ * Returns ThemeParams component instance.
4
+ */
5
+ export declare function useThemeParams(): ThemeParams;
@@ -0,0 +1,9 @@
1
+ import { type ComponentType } from 'react';
2
+ import type { ThemeParams } from './types.js';
3
+ /**
4
+ * HOC which passes ThemeParams SDK component to wrapped React component.
5
+ * @param Component - component to wrap.
6
+ */
7
+ export declare function withThemeParams<P extends {
8
+ themeParams?: ThemeParams;
9
+ }>(Component: ComponentType<P>): ComponentType<Omit<P, 'themeParams'>>;
@@ -0,0 +1,3 @@
1
+ export * from './types.js';
2
+ export * from './useViewport.js';
3
+ export * from './withViewport.js';
@@ -0,0 +1 @@
1
+ export type { Viewport } from '@tma.js/sdk';
@@ -0,0 +1,5 @@
1
+ import type { Viewport } from './types.js';
2
+ /**
3
+ * Returns Viewport component instance.
4
+ */
5
+ export declare function useViewport(): Viewport;
@@ -0,0 +1,9 @@
1
+ import { type ComponentType } from 'react';
2
+ import type { Viewport } from './types.js';
3
+ /**
4
+ * HOC which passes Viewport SDK component to wrapped React component.
5
+ * @param Component - component to wrap.
6
+ */
7
+ export declare function withViewport<P extends {
8
+ viewport?: Viewport;
9
+ }>(Component: ComponentType<P>): ComponentType<Omit<P, 'viewport'>>;
@@ -0,0 +1,3 @@
1
+ export * from './types.js';
2
+ export * from './useWebApp.js';
3
+ export * from './withWebApp.js';
@@ -0,0 +1 @@
1
+ export type { WebApp } from '@tma.js/sdk';
@@ -0,0 +1,5 @@
1
+ import type { WebApp } from './types.js';
2
+ /**
3
+ * Returns WebApp component instance.
4
+ */
5
+ export declare function useWebApp(): WebApp;
@@ -0,0 +1,9 @@
1
+ import { type ComponentType } from 'react';
2
+ import type { WebApp } from './types.js';
3
+ /**
4
+ * HOC which passes WebApp SDK component to wrapped React component.
5
+ * @param Component - component to wrap.
6
+ */
7
+ export declare function withWebApp<P extends {
8
+ webApp?: WebApp;
9
+ }>(Component: ComponentType<P>): ComponentType<Omit<P, 'webApp'>>;
@@ -0,0 +1,10 @@
1
+ import React, { type PropsWithChildren } from 'react';
2
+ import { type InitOptions } from '@tma.js/sdk';
3
+ export type SDKInitOptions = InitOptions;
4
+ export type SDKProviderProps = PropsWithChildren<{
5
+ initOptions?: SDKInitOptions;
6
+ }>;
7
+ /**
8
+ * Component which provides access to SDK components.
9
+ */
10
+ export declare const SDKProvider: React.NamedExoticComponent<SDKProviderProps>;
@@ -0,0 +1,3 @@
1
+ /// <reference types="react" />
2
+ import type { SDKContext } from './types.js';
3
+ export declare const sdkContext: import("react").Context<SDKContext>;
@@ -0,0 +1,9 @@
1
+ import { type ComponentType } from 'react';
2
+ import type { SDKContext } from './types.js';
3
+ /**
4
+ * HOC which passes SDK context to wrapped React component.
5
+ * @param Component - component to wrap.
6
+ */
7
+ export declare function withSDK<P extends {
8
+ sdk?: SDKContext;
9
+ }>(Component: ComponentType<P>): ComponentType<Omit<P, 'sdk'>>;
@@ -0,0 +1,11 @@
1
+ import type { SDKComponent, SDKComponentName } from './types.js';
2
+ /**
3
+ * Returns current SDK information.
4
+ */
5
+ export declare const useSDK: () => import("./types.js").SDKContext;
6
+ /**
7
+ * Returns value by its field name from SDK init result.
8
+ * @param name - component name.
9
+ * @throws {Error} SDK is not initialized.
10
+ */
11
+ export declare function useUnit<N extends SDKComponentName>(name: N): SDKComponent<N>;
@@ -0,0 +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';
@@ -0,0 +1,28 @@
1
+ import type { InitResult } from '@tma.js/sdk';
2
+ /**
3
+ * List of provided SDK components.
4
+ */
5
+ export type SDKComponents = InitResult;
6
+ /**
7
+ * Known SDK component name.
8
+ */
9
+ export type SDKComponentName = keyof SDKComponents;
10
+ /**
11
+ * Returns SDK component type by its name.
12
+ */
13
+ export type SDKComponent<N extends SDKComponentName> = SDKComponents[N];
14
+ export interface SDKContext {
15
+ /**
16
+ * Components ready to use. This value will be null until initialization
17
+ * is done.
18
+ */
19
+ components: SDKComponents | null;
20
+ /**
21
+ * States, that SDK called `init()` method.
22
+ */
23
+ didInit: boolean;
24
+ /**
25
+ * Error occurred during initialization.
26
+ */
27
+ error: unknown | null;
28
+ }
package/package.json ADDED
@@ -0,0 +1,70 @@
1
+ {
2
+ "name": "@tma.js/sdk-react",
3
+ "version": "0.4.3",
4
+ "description": "React bindings for Web Apps client SDK. Contains hooks, components and other useful tools which allow usage of React along with Web Apps client SDK.",
5
+ "author": "Vladislav Kibenko <wolfram.deus@gmail.com>",
6
+ "homepage": "https://github.com/Telegram-Mini-Apps/tma.js#readme",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git@github.com:Telegram-Mini-Apps/tma.js.git",
10
+ "directory": "packages/sdk-react"
11
+ },
12
+ "bugs": {
13
+ "url": "https://github.com/Telegram-Mini-Apps/tma.js/issues"
14
+ },
15
+ "keywords": [
16
+ "telegram-mini-apps",
17
+ "typescript",
18
+ "sdk",
19
+ "react"
20
+ ],
21
+ "license": "MIT",
22
+ "type": "module",
23
+ "sideEffects": false,
24
+ "files": [
25
+ "dist",
26
+ "src"
27
+ ],
28
+ "main": "dist/lib/index.cjs",
29
+ "browser": "dist/lib/browser.js",
30
+ "module": "dist/lib/index.mjs",
31
+ "types": "dist/types/index.d.ts",
32
+ "exports": {
33
+ ".": {
34
+ "types": "./dist/types/index.d.ts",
35
+ "import": "./dist/lib/index.mjs",
36
+ "require": "./dist/lib/index.cjs",
37
+ "default": "./dist/lib/index.cjs"
38
+ },
39
+ "./*": {
40
+ "types": "./dist/types/*.d.ts",
41
+ "import": "./dist/lib/*.mjs",
42
+ "require": "./dist/lib/*.cjs",
43
+ "default": "./dist/lib/*.cjs"
44
+ }
45
+ },
46
+ "peerDependencies": {
47
+ "@types/react": "^17.0.0 || ^18.0.0",
48
+ "react": "^17.0.0 || ^18.0.0"
49
+ },
50
+ "peerDependenciesMeta": {
51
+ "@types/react": {
52
+ "optional": true
53
+ }
54
+ },
55
+ "dependencies": {
56
+ "@tma.js/bridge": "1.3.2",
57
+ "@tma.js/sdk": "0.11.3"
58
+ },
59
+ "devDependencies": {
60
+ "tsconfig": "0.0.2",
61
+ "eslint-config-custom": "0.1.0"
62
+ },
63
+ "publishConfig": {
64
+ "access": "public"
65
+ },
66
+ "scripts": {
67
+ "lint": "eslint -c .eslintrc.cjs src/**/*",
68
+ "build": "rimraf dist && rollup --config rollup.config.js"
69
+ }
70
+ }
package/src/index.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from './lib/index.js';
2
+ export * from './provider/index.js';
@@ -0,0 +1,3 @@
1
+ export * from './types.js';
2
+ export * from './useBackButton.js';
3
+ export * from './withBackButton.js';
@@ -0,0 +1 @@
1
+ export type { BackButton } from '@tma.js/sdk';
@@ -0,0 +1,13 @@
1
+ import { useUnit } from '../../provider/index.js';
2
+ import { useEventsTracking } from '../hooks.js';
3
+ import type { BackButton } from './types.js';
4
+
5
+ /**
6
+ * Returns BackButton component instance.
7
+ */
8
+ export function useBackButton(): BackButton {
9
+ const backButton = useUnit('backButton');
10
+ useEventsTracking(backButton, ['isVisibleChanged']);
11
+
12
+ return backButton;
13
+ }
@@ -0,0 +1,18 @@
1
+ import React, { type ComponentType } from 'react';
2
+
3
+ import { useBackButton } from './useBackButton.js';
4
+ import type { BackButton } from './types.js';
5
+
6
+ /**
7
+ * HOC which passes BackButton SDK component to wrapped React component.
8
+ * @param Component - component to wrap.
9
+ */
10
+ export function withBackButton<P extends { backButton?: BackButton }>(
11
+ Component: ComponentType<P>,
12
+ ): ComponentType<Omit<P, 'backButton'>> {
13
+ return function WithBackButton(props: Omit<P, 'backButton'>) {
14
+ const p = { ...props, backButton: useBackButton() } as P;
15
+
16
+ return <Component {...p} />;
17
+ };
18
+ }
@@ -0,0 +1,3 @@
1
+ export * from './types.js';
2
+ export * from './useClosingBehaviour.js';
3
+ export * from './withClosingBehaviour.js';
@@ -0,0 +1 @@
1
+ export type { ClosingBehaviour } from '@tma.js/sdk';
@@ -0,0 +1,13 @@
1
+ import { useUnit } from '../../provider/index.js';
2
+ import { useEventsTracking } from '../hooks.js';
3
+ import type { ClosingBehaviour } from './types.js';
4
+
5
+ /**
6
+ * Returns ClosingBehaviour component instance.
7
+ */
8
+ export function useClosingBehaviour(): ClosingBehaviour {
9
+ const component = useUnit('closingBehavior');
10
+ useEventsTracking(component, ['isConfirmationNeededChanged']);
11
+
12
+ return component;
13
+ }
@@ -0,0 +1,18 @@
1
+ import React, { type ComponentType } from 'react';
2
+
3
+ import { useClosingBehaviour } from './useClosingBehaviour.js';
4
+ import type { ClosingBehaviour } from './types.js';
5
+
6
+ /**
7
+ * HOC which passes ClosingBehaviour SDK component to wrapped React component.
8
+ * @param Component - component to wrap.
9
+ */
10
+ export function withClosingBehaviour<P extends { closingBehaviour?: ClosingBehaviour }>(
11
+ Component: ComponentType<P>,
12
+ ): ComponentType<Omit<P, 'closingBehaviour'>> {
13
+ return function WithClosingConfirmation(props: Omit<P, 'closingBehaviour'>) {
14
+ const p = { ...props, closingBehaviour: useClosingBehaviour() } as P;
15
+
16
+ return <Component {...p} />;
17
+ };
18
+ }
@@ -0,0 +1,28 @@
1
+ import React, { type ComponentType } from 'react';
2
+
3
+ import type { CloudStorage } from '@tma.js/sdk';
4
+
5
+ import { useUnit } from '../provider/index.js';
6
+
7
+ /**
8
+ * Returns CloudStorage component instance.
9
+ */
10
+ export function useCloudStorage(): CloudStorage {
11
+ return useUnit('cloudStorage');
12
+ }
13
+
14
+ /**
15
+ * HOC which passes CloudStorage SDK component to wrapped React component.
16
+ * @param Component - component to wrap.
17
+ */
18
+ export function withCloudStorage<P extends { cloudStorage?: CloudStorage }>(
19
+ Component: ComponentType<P>,
20
+ ): ComponentType<Omit<P, 'cloudStorage'>> {
21
+ return function WithCloudStorage(props: Omit<P, 'cloudStorage'>) {
22
+ const p = { ...props, cloudStorage: useCloudStorage() } as P;
23
+
24
+ return <Component {...p} />;
25
+ };
26
+ }
27
+
28
+ export type { CloudStorage };
@@ -0,0 +1,3 @@
1
+ export * from './types.js';
2
+ export * from './useHapticFeedback.js';
3
+ export * from './withHapticFeedback.js';
@@ -0,0 +1 @@
1
+ export type { HapticFeedback } from '@tma.js/sdk';
@@ -0,0 +1,9 @@
1
+ import { useUnit } from '../../provider/index.js';
2
+ import type { HapticFeedback } from './types.js';
3
+
4
+ /**
5
+ * Returns HapticFeedback component instance.
6
+ */
7
+ export function useHapticFeedback(): HapticFeedback {
8
+ return useUnit('haptic');
9
+ }
@@ -0,0 +1,18 @@
1
+ import React, { type ComponentType } from 'react';
2
+
3
+ import { useHapticFeedback } from './useHapticFeedback.js';
4
+ import type { HapticFeedback } from './types.js';
5
+
6
+ /**
7
+ * HOC which passes HapticFeedback SDK component to wrapped React component.
8
+ * @param Component - component to wrap.
9
+ */
10
+ export function withHapticFeedback<P extends { hapticFeedback?: HapticFeedback }>(
11
+ Component: ComponentType<P>,
12
+ ): ComponentType<Omit<P, 'hapticFeedback'>> {
13
+ return function WithHapticFeedback(props: Omit<P, 'hapticFeedback'>) {
14
+ const p = { ...props, hapticFeedback: useHapticFeedback() } as P;
15
+
16
+ return <Component {...p} />;
17
+ };
18
+ }
@@ -0,0 +1,51 @@
1
+ import { useCallback, useState, useEffect } from 'react';
2
+ import type { PostEvent } from '@tma.js/sdk/types';
3
+ import { useUnit } from '../provider/index.js';
4
+
5
+ interface Trackable {
6
+ on: (event: any, ...args: any[]) => void;
7
+ off: (event: any, ...args: any[]) => void;
8
+ }
9
+
10
+ type EventName<T extends Trackable> = T extends {
11
+ on(event: infer E, ...args: any[]): any
12
+ } ? E : never;
13
+
14
+ /**
15
+ * The hook which returns function used for force update.
16
+ */
17
+ export function useForceUpdate(): (() => void) {
18
+ const [, setHash] = useState(0);
19
+ // TODO: Maybe just assign new symbol?
20
+ return useCallback(() => setHash((hash) => (hash + 1) % 1_000_000), []);
21
+ }
22
+
23
+ /**
24
+ * The hook which listens to the specified object events and calls force update
25
+ * in case, some of them was emitted.
26
+ * @param trackable - object which allows events listening.
27
+ * @param events - events to listen. This array will be memoized during first
28
+ * hook call.
29
+ */
30
+ export function useEventsTracking<T extends Trackable>(
31
+ trackable: T,
32
+ events: EventName<T>[],
33
+ ) {
34
+ const forceUpdate = useForceUpdate();
35
+
36
+ useEffect(() => {
37
+ // Start listening to each event.
38
+ events.forEach((event) => trackable.on(event, forceUpdate));
39
+
40
+ // On cleanup remove event listeners.
41
+ return () => events.forEach((event) => trackable.off(event, forceUpdate));
42
+ }, [trackable, forceUpdate, events]);
43
+ }
44
+
45
+ /**
46
+ * Returns event posting function which was created during SDK
47
+ * initialization.
48
+ */
49
+ export function usePostEvent(): PostEvent {
50
+ return useUnit('postEvent');
51
+ }