@tma.js/sdk-react 0.5.4 → 1.0.1

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 (150) hide show
  1. package/dist/dts/createHoc.d.ts +12 -0
  2. package/dist/dts/createHook.d.ts +13 -0
  3. package/dist/dts/index.d.ts +3 -2
  4. package/dist/dts/provider/SDKContext.d.ts +3 -0
  5. package/dist/dts/provider/SDKProvider.d.ts +4 -8
  6. package/dist/dts/provider/index.d.ts +5 -3
  7. package/dist/dts/provider/types.d.ts +15 -21
  8. package/dist/dts/provider/useInitResultDynamicValue.d.ts +2 -0
  9. package/dist/dts/provider/useInitResultValue.d.ts +7 -0
  10. package/dist/dts/provider/useSDKContext.d.ts +5 -0
  11. package/dist/dts/provider/{hocs.d.ts → withSDKContext.d.ts} +3 -3
  12. package/dist/dts/tools/back-button.d.ts +8 -0
  13. package/dist/dts/tools/closing-behavior.d.ts +8 -0
  14. package/dist/dts/tools/cloud-storage.d.ts +8 -0
  15. package/dist/dts/tools/haptic-feedback.d.ts +8 -0
  16. package/dist/dts/tools/index.d.ts +16 -0
  17. package/dist/dts/tools/init-data-raw.d.ts +8 -0
  18. package/dist/dts/tools/init-data.d.ts +8 -0
  19. package/dist/dts/tools/invoice.d.ts +8 -0
  20. package/dist/dts/tools/launch-params.d.ts +13 -0
  21. package/dist/dts/tools/main-button.d.ts +8 -0
  22. package/dist/dts/tools/mini-app.d.ts +8 -0
  23. package/dist/dts/tools/popup.d.ts +8 -0
  24. package/dist/dts/tools/post-event.d.ts +8 -0
  25. package/dist/dts/tools/qr-scanner.d.ts +8 -0
  26. package/dist/dts/tools/theme-params.d.ts +8 -0
  27. package/dist/dts/tools/utils.d.ts +8 -0
  28. package/dist/dts/tools/viewport.d.ts +8 -0
  29. package/dist/dts/types.d.ts +23 -0
  30. package/dist/index.cjs +1 -1
  31. package/dist/index.cjs.map +1 -1
  32. package/dist/index.mjs +116 -203
  33. package/dist/index.mjs.map +1 -1
  34. package/package.json +7 -9
  35. package/src/createHoc.tsx +32 -0
  36. package/src/createHook.ts +31 -0
  37. package/src/index.ts +45 -2
  38. package/src/provider/SDKContext.ts +9 -0
  39. package/src/provider/SDKProvider.tsx +54 -37
  40. package/src/provider/index.ts +5 -3
  41. package/src/provider/types.ts +15 -22
  42. package/src/provider/useInitResultDynamicValue.ts +34 -0
  43. package/src/provider/useInitResultValue.ts +16 -0
  44. package/src/provider/useSDKContext.ts +11 -0
  45. package/src/provider/withSDKContext.tsx +21 -0
  46. package/src/tools/back-button.ts +12 -0
  47. package/src/tools/closing-behavior.ts +12 -0
  48. package/src/tools/cloud-storage.ts +12 -0
  49. package/src/tools/haptic-feedback.ts +12 -0
  50. package/src/tools/index.ts +16 -0
  51. package/src/tools/init-data-raw.ts +12 -0
  52. package/src/tools/init-data.ts +12 -0
  53. package/src/tools/invoice.ts +12 -0
  54. package/src/tools/launch-params.tsx +21 -0
  55. package/src/tools/main-button.ts +12 -0
  56. package/src/tools/mini-app.ts +12 -0
  57. package/src/tools/popup.ts +12 -0
  58. package/src/tools/post-event.ts +12 -0
  59. package/src/tools/qr-scanner.ts +12 -0
  60. package/src/tools/theme-params.ts +12 -0
  61. package/src/tools/utils.ts +12 -0
  62. package/src/tools/viewport.ts +12 -0
  63. package/src/types.ts +29 -0
  64. package/dist/dts/lib/back-button/index.d.ts +0 -3
  65. package/dist/dts/lib/back-button/types.d.ts +0 -1
  66. package/dist/dts/lib/back-button/useBackButton.d.ts +0 -5
  67. package/dist/dts/lib/back-button/withBackButton.d.ts +0 -9
  68. package/dist/dts/lib/closing-behavior/index.d.ts +0 -3
  69. package/dist/dts/lib/closing-behavior/types.d.ts +0 -1
  70. package/dist/dts/lib/closing-behavior/useClosingBehaviour.d.ts +0 -5
  71. package/dist/dts/lib/closing-behavior/withClosingBehaviour.d.ts +0 -9
  72. package/dist/dts/lib/cloud-storage.d.ts +0 -14
  73. package/dist/dts/lib/haptic-feedback/index.d.ts +0 -3
  74. package/dist/dts/lib/haptic-feedback/types.d.ts +0 -1
  75. package/dist/dts/lib/haptic-feedback/useHapticFeedback.d.ts +0 -5
  76. package/dist/dts/lib/haptic-feedback/withHapticFeedback.d.ts +0 -9
  77. package/dist/dts/lib/hooks.d.ts +0 -26
  78. package/dist/dts/lib/index.d.ts +0 -13
  79. package/dist/dts/lib/init-data.d.ts +0 -20
  80. package/dist/dts/lib/launch-params/index.d.ts +0 -3
  81. package/dist/dts/lib/launch-params/types.d.ts +0 -1
  82. package/dist/dts/lib/launch-params/useLaunchParams.d.ts +0 -5
  83. package/dist/dts/lib/launch-params/withLaunchParams.d.ts +0 -9
  84. package/dist/dts/lib/main-button/index.d.ts +0 -3
  85. package/dist/dts/lib/main-button/types.d.ts +0 -1
  86. package/dist/dts/lib/main-button/useMainButton.d.ts +0 -5
  87. package/dist/dts/lib/main-button/withMainButton.d.ts +0 -9
  88. package/dist/dts/lib/popup/index.d.ts +0 -3
  89. package/dist/dts/lib/popup/types.d.ts +0 -1
  90. package/dist/dts/lib/popup/usePopup.d.ts +0 -5
  91. package/dist/dts/lib/popup/withPopup.d.ts +0 -9
  92. package/dist/dts/lib/qr-scanner/index.d.ts +0 -3
  93. package/dist/dts/lib/qr-scanner/types.d.ts +0 -1
  94. package/dist/dts/lib/qr-scanner/useQRScanner.d.ts +0 -5
  95. package/dist/dts/lib/qr-scanner/withQRScanner.d.ts +0 -9
  96. package/dist/dts/lib/theme-params.d.ts +0 -13
  97. package/dist/dts/lib/viewport/index.d.ts +0 -3
  98. package/dist/dts/lib/viewport/types.d.ts +0 -1
  99. package/dist/dts/lib/viewport/useViewport.d.ts +0 -5
  100. package/dist/dts/lib/viewport/withViewport.d.ts +0 -9
  101. package/dist/dts/lib/web-app/index.d.ts +0 -3
  102. package/dist/dts/lib/web-app/types.d.ts +0 -1
  103. package/dist/dts/lib/web-app/useWebApp.d.ts +0 -5
  104. package/dist/dts/lib/web-app/withWebApp.d.ts +0 -9
  105. package/dist/dts/provider/context.d.ts +0 -3
  106. package/dist/dts/provider/hooks.d.ts +0 -11
  107. package/src/lib/back-button/index.ts +0 -3
  108. package/src/lib/back-button/types.ts +0 -1
  109. package/src/lib/back-button/useBackButton.ts +0 -13
  110. package/src/lib/back-button/withBackButton.tsx +0 -18
  111. package/src/lib/closing-behavior/index.ts +0 -3
  112. package/src/lib/closing-behavior/types.ts +0 -1
  113. package/src/lib/closing-behavior/useClosingBehaviour.ts +0 -13
  114. package/src/lib/closing-behavior/withClosingBehaviour.tsx +0 -18
  115. package/src/lib/cloud-storage.tsx +0 -28
  116. package/src/lib/haptic-feedback/index.ts +0 -3
  117. package/src/lib/haptic-feedback/types.ts +0 -1
  118. package/src/lib/haptic-feedback/useHapticFeedback.ts +0 -9
  119. package/src/lib/haptic-feedback/withHapticFeedback.tsx +0 -18
  120. package/src/lib/hooks.ts +0 -52
  121. package/src/lib/index.ts +0 -13
  122. package/src/lib/init-data.tsx +0 -43
  123. package/src/lib/launch-params/index.ts +0 -3
  124. package/src/lib/launch-params/types.ts +0 -1
  125. package/src/lib/launch-params/useLaunchParams.ts +0 -11
  126. package/src/lib/launch-params/withLaunchParams.tsx +0 -18
  127. package/src/lib/main-button/index.ts +0 -3
  128. package/src/lib/main-button/types.ts +0 -1
  129. package/src/lib/main-button/useMainButton.ts +0 -16
  130. package/src/lib/main-button/withMainButton.tsx +0 -18
  131. package/src/lib/popup/index.ts +0 -3
  132. package/src/lib/popup/types.ts +0 -1
  133. package/src/lib/popup/usePopup.ts +0 -13
  134. package/src/lib/popup/withPopup.tsx +0 -18
  135. package/src/lib/qr-scanner/index.ts +0 -3
  136. package/src/lib/qr-scanner/types.ts +0 -1
  137. package/src/lib/qr-scanner/useQRScanner.ts +0 -13
  138. package/src/lib/qr-scanner/withQRScanner.tsx +0 -18
  139. package/src/lib/theme-params.tsx +0 -29
  140. package/src/lib/viewport/index.ts +0 -3
  141. package/src/lib/viewport/types.ts +0 -1
  142. package/src/lib/viewport/useViewport.ts +0 -15
  143. package/src/lib/viewport/withViewport.tsx +0 -18
  144. package/src/lib/web-app/index.ts +0 -3
  145. package/src/lib/web-app/types.ts +0 -1
  146. package/src/lib/web-app/useWebApp.ts +0 -13
  147. package/src/lib/web-app/withWebApp.tsx +0 -18
  148. package/src/provider/context.ts +0 -11
  149. package/src/provider/hocs.tsx +0 -18
  150. package/src/provider/hooks.ts +0 -23
@@ -1,13 +0,0 @@
1
- export * from './back-button/index.js';
2
- export * from './closing-behavior/index.js';
3
- export * from './haptic-feedback/index.js';
4
- export * from './launch-params/index.js';
5
- export * from './main-button/index.js';
6
- export * from './popup/index.js';
7
- export * from './qr-scanner/index.js';
8
- export * from './viewport/index.js';
9
- export * from './web-app/index.js';
10
- export * from './cloud-storage.js';
11
- export { usePostEvent } from './hooks.js';
12
- export * from './init-data.js';
13
- export * from './theme-params.js';
@@ -1,20 +0,0 @@
1
- import { type ComponentType } from 'react';
2
- import type { InitData } from '@tma.js/sdk';
3
- interface EnhancedProps {
4
- initData: InitData | null;
5
- initDataRaw: string | null;
6
- }
7
- /**
8
- * Returns InitData component instance.
9
- */
10
- export declare function useInitData(): InitData | null;
11
- /**
12
- * Returns init data raw representation.
13
- */
14
- export declare function useInitDataRaw(): string | null;
15
- /**
16
- * HOC which passes InitData SDK component to wrapped React component.
17
- * @param Component - component to wrap.
18
- */
19
- export declare function withInitData<P extends Partial<EnhancedProps>>(Component: ComponentType<P>): ComponentType<Omit<P, keyof EnhancedProps>>;
20
- export { InitData };
@@ -1,3 +0,0 @@
1
- export * from './types.js';
2
- export * from './useLaunchParams.js';
3
- export * from './withLaunchParams.js';
@@ -1 +0,0 @@
1
- export type { LaunchParams } from '@tma.js/launch-params';
@@ -1,5 +0,0 @@
1
- import type { LaunchParams } from './types.js';
2
- /**
3
- * The hook which returns launch parameters initially passed to Mini App.
4
- */
5
- export declare function useLaunchParams(): LaunchParams;
@@ -1,9 +0,0 @@
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'>>;
@@ -1,3 +0,0 @@
1
- export * from './types.js';
2
- export * from './useMainButton.js';
3
- export * from './withMainButton.js';
@@ -1 +0,0 @@
1
- export type { MainButton } from '@tma.js/sdk';
@@ -1,5 +0,0 @@
1
- import type { MainButton } from './types.js';
2
- /**
3
- * Returns MainButton component instance.
4
- */
5
- export declare function useMainButton(): MainButton;
@@ -1,9 +0,0 @@
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'>>;
@@ -1,3 +0,0 @@
1
- export * from './types.js';
2
- export * from './usePopup.js';
3
- export * from './withPopup.js';
@@ -1 +0,0 @@
1
- export type { Popup } from '@tma.js/sdk';
@@ -1,5 +0,0 @@
1
- import type { Popup } from './types.js';
2
- /**
3
- * Returns Popup component instance.
4
- */
5
- export declare function usePopup(): Popup;
@@ -1,9 +0,0 @@
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'>>;
@@ -1,3 +0,0 @@
1
- export * from './types.js';
2
- export * from './useQRScanner.js';
3
- export * from './withQRScanner.js';
@@ -1 +0,0 @@
1
- export type { QRScanner } from '@tma.js/sdk';
@@ -1,5 +0,0 @@
1
- import type { QRScanner } from './types.js';
2
- /**
3
- * Returns QRScanner component instance.
4
- */
5
- export declare function useQRScanner(): QRScanner;
@@ -1,9 +0,0 @@
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'>>;
@@ -1,13 +0,0 @@
1
- import { type ComponentType } from 'react';
2
- import type { ThemeParams } from '@tma.js/sdk';
3
- /**
4
- * Returns ThemeParams component instance.
5
- */
6
- export declare function useThemeParams(): ThemeParams;
7
- /**
8
- * HOC which passes ThemeParams SDK component to wrapped React component.
9
- * @param Component - component to wrap.
10
- */
11
- export declare function withThemeParams<P extends {
12
- themeParams?: ThemeParams;
13
- }>(Component: ComponentType<P>): ComponentType<Omit<P, 'themeParams'>>;
@@ -1,3 +0,0 @@
1
- export * from './types.js';
2
- export * from './useViewport.js';
3
- export * from './withViewport.js';
@@ -1 +0,0 @@
1
- export type { Viewport } from '@tma.js/sdk';
@@ -1,5 +0,0 @@
1
- import type { Viewport } from './types.js';
2
- /**
3
- * Returns Viewport component instance.
4
- */
5
- export declare function useViewport(): Viewport;
@@ -1,9 +0,0 @@
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'>>;
@@ -1,3 +0,0 @@
1
- export * from './types.js';
2
- export * from './useWebApp.js';
3
- export * from './withWebApp.js';
@@ -1 +0,0 @@
1
- export type { WebApp } from '@tma.js/sdk';
@@ -1,5 +0,0 @@
1
- import type { WebApp } from './types.js';
2
- /**
3
- * Returns WebApp component instance.
4
- */
5
- export declare function useWebApp(): WebApp;
@@ -1,9 +0,0 @@
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'>>;
@@ -1,3 +0,0 @@
1
- /// <reference types="react" />
2
- import type { SDKContext } from './types.js';
3
- export declare const sdkContext: import("react").Context<SDKContext>;
@@ -1,11 +0,0 @@
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>;
@@ -1,3 +0,0 @@
1
- export * from './types.js';
2
- export * from './useBackButton.js';
3
- export * from './withBackButton.js';
@@ -1 +0,0 @@
1
- export type { BackButton } from '@tma.js/sdk';
@@ -1,13 +0,0 @@
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
- }
@@ -1,18 +0,0 @@
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
- }
@@ -1,3 +0,0 @@
1
- export * from './types.js';
2
- export * from './useClosingBehaviour.js';
3
- export * from './withClosingBehaviour.js';
@@ -1 +0,0 @@
1
- export type { ClosingBehaviour } from '@tma.js/sdk';
@@ -1,13 +0,0 @@
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
- }
@@ -1,18 +0,0 @@
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
- }
@@ -1,28 +0,0 @@
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 };
@@ -1,3 +0,0 @@
1
- export * from './types.js';
2
- export * from './useHapticFeedback.js';
3
- export * from './withHapticFeedback.js';
@@ -1 +0,0 @@
1
- export type { HapticFeedback } from '@tma.js/sdk';
@@ -1,9 +0,0 @@
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
- }
@@ -1,18 +0,0 @@
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
- }
package/src/lib/hooks.ts DELETED
@@ -1,52 +0,0 @@
1
- import { useCallback, useState, useEffect } from 'react';
2
- import type { PostEvent } from '@tma.js/sdk';
3
-
4
- import { useUnit } from '../provider/index.js';
5
-
6
- interface Trackable {
7
- on: (event: any, ...args: any[]) => void;
8
- off: (event: any, ...args: any[]) => void;
9
- }
10
-
11
- type EventName<T extends Trackable> = T extends {
12
- on(event: infer E, ...args: any[]): any
13
- } ? E : never;
14
-
15
- /**
16
- * The hook which returns function used for force update.
17
- */
18
- export function useForceUpdate(): (() => void) {
19
- const [, setHash] = useState(0);
20
- // TODO: Maybe just assign new symbol?
21
- return useCallback(() => setHash((hash) => (hash + 1) % 1_000_000), []);
22
- }
23
-
24
- /**
25
- * The hook which listens to the specified object events and calls force update
26
- * in case, some of them was emitted.
27
- * @param trackable - object which allows events listening.
28
- * @param events - events to listen. This array will be memoized during first
29
- * hook call.
30
- */
31
- export function useEventsTracking<T extends Trackable>(
32
- trackable: T,
33
- events: EventName<T>[],
34
- ) {
35
- const forceUpdate = useForceUpdate();
36
-
37
- useEffect(() => {
38
- // Start listening to each event.
39
- events.forEach((event) => trackable.on(event, forceUpdate));
40
-
41
- // On cleanup remove event listeners.
42
- return () => events.forEach((event) => trackable.off(event, forceUpdate));
43
- }, [trackable, forceUpdate, events]);
44
- }
45
-
46
- /**
47
- * Returns event posting function which was created during SDK
48
- * initialization.
49
- */
50
- export function usePostEvent(): PostEvent {
51
- return useUnit('postEvent');
52
- }
package/src/lib/index.ts DELETED
@@ -1,13 +0,0 @@
1
- export * from './back-button/index.js';
2
- export * from './closing-behavior/index.js';
3
- export * from './haptic-feedback/index.js';
4
- export * from './launch-params/index.js';
5
- export * from './main-button/index.js';
6
- export * from './popup/index.js';
7
- export * from './qr-scanner/index.js';
8
- export * from './viewport/index.js';
9
- export * from './web-app/index.js';
10
- export * from './cloud-storage.js';
11
- export { usePostEvent } from './hooks.js';
12
- export * from './init-data.js';
13
- export * from './theme-params.js';
@@ -1,43 +0,0 @@
1
- import React, { type ComponentType } from 'react';
2
- import type { InitData } from '@tma.js/sdk';
3
-
4
- import { useUnit } from '../provider/index.js';
5
-
6
- interface EnhancedProps {
7
- initData: InitData | null;
8
- initDataRaw: string | null;
9
- }
10
-
11
- /**
12
- * Returns InitData component instance.
13
- */
14
- export function useInitData(): InitData | null {
15
- return useUnit('initData') || null;
16
- }
17
-
18
- /**
19
- * Returns init data raw representation.
20
- */
21
- export function useInitDataRaw(): string | null {
22
- return useUnit('initDataRaw') || null;
23
- }
24
-
25
- /**
26
- * HOC which passes InitData SDK component to wrapped React component.
27
- * @param Component - component to wrap.
28
- */
29
- export function withInitData<P extends Partial<EnhancedProps>>(
30
- Component: ComponentType<P>,
31
- ): ComponentType<Omit<P, keyof EnhancedProps>> {
32
- return function WithInitData(props) {
33
- const enhancedProps = {
34
- ...props,
35
- initData: useInitData(),
36
- initDataRaw: useInitDataRaw(),
37
- } as P;
38
-
39
- return <Component {...enhancedProps} />;
40
- };
41
- }
42
-
43
- export { InitData };
@@ -1,3 +0,0 @@
1
- export * from './types.js';
2
- export * from './useLaunchParams.js';
3
- export * from './withLaunchParams.js';
@@ -1 +0,0 @@
1
- export type { LaunchParams } from '@tma.js/launch-params';
@@ -1,11 +0,0 @@
1
- import { useMemo } from 'react';
2
- import { computeLaunchData } from '@tma.js/launch-params';
3
-
4
- import type { LaunchParams } from './types.js';
5
-
6
- /**
7
- * The hook which returns launch parameters initially passed to Mini App.
8
- */
9
- export function useLaunchParams(): LaunchParams {
10
- return useMemo(() => computeLaunchData().launchParams, []);
11
- }
@@ -1,18 +0,0 @@
1
- import React, { type ComponentType } from 'react';
2
-
3
- import { useLaunchParams } from './useLaunchParams.js';
4
- import type { LaunchParams } from './types.js';
5
-
6
- /**
7
- * HOC which passes launch parameters to wrapped React component.
8
- * @param Component - component to wrap.
9
- */
10
- export function withLaunchParams<P extends { launchParams?: LaunchParams }>(
11
- Component: ComponentType<P>,
12
- ): ComponentType<Omit<P, 'launchParams'>> {
13
- return function WithLaunchParams(props: Omit<P, 'launchParams'>) {
14
- const p = { ...props, launchParams: useLaunchParams() } as P;
15
-
16
- return <Component {...p} />;
17
- };
18
- }
@@ -1,3 +0,0 @@
1
- export * from './types.js';
2
- export * from './useMainButton.js';
3
- export * from './withMainButton.js';
@@ -1 +0,0 @@
1
- export type { MainButton } from '@tma.js/sdk';
@@ -1,16 +0,0 @@
1
- import { useUnit } from '../../provider/index.js';
2
- import { useEventsTracking } from '../hooks.js';
3
- import type { MainButton } from './types.js';
4
-
5
- /**
6
- * Returns MainButton component instance.
7
- */
8
- export function useMainButton(): MainButton {
9
- const mainButton = useUnit('mainButton');
10
- useEventsTracking(mainButton, [
11
- 'isEnabledChanged', 'backgroundColorChanged', 'isProgressVisibleChanged',
12
- 'textChanged', 'textColorChanged', 'isVisibleChanged',
13
- ]);
14
-
15
- return mainButton;
16
- }
@@ -1,18 +0,0 @@
1
- import React, { type ComponentType } from 'react';
2
-
3
- import { useMainButton } from './useMainButton.js';
4
- import type { MainButton } from './types.js';
5
-
6
- /**
7
- * HOC which passes MainButton SDK component to wrapped React component.
8
- * @param Component - component to wrap.
9
- */
10
- export function withMainButton<P extends { mainButton?: MainButton }>(
11
- Component: ComponentType<P>,
12
- ): ComponentType<Omit<P, 'mainButton'>> {
13
- return function WithMainButton(props: Omit<P, 'mainButton'>) {
14
- const p = { ...props, mainButton: useMainButton() } as P;
15
-
16
- return <Component {...p} />;
17
- };
18
- }
@@ -1,3 +0,0 @@
1
- export * from './types.js';
2
- export * from './usePopup.js';
3
- export * from './withPopup.js';
@@ -1 +0,0 @@
1
- export type { Popup } from '@tma.js/sdk';
@@ -1,13 +0,0 @@
1
- import { useUnit } from '../../provider/index.js';
2
- import { useEventsTracking } from '../hooks.js';
3
- import type { Popup } from './types.js';
4
-
5
- /**
6
- * Returns Popup component instance.
7
- */
8
- export function usePopup(): Popup {
9
- const popup = useUnit('popup');
10
- useEventsTracking(popup, ['isOpenedChanged']);
11
-
12
- return popup;
13
- }
@@ -1,18 +0,0 @@
1
- import React, { type ComponentType } from 'react';
2
-
3
- import { usePopup } from './usePopup.js';
4
- import type { Popup } from './types.js';
5
-
6
- /**
7
- * HOC which passes Popup SDK component to wrapped React component.
8
- * @param Component - component to wrap.
9
- */
10
- export function withPopup<P extends { popup?: Popup }>(
11
- Component: ComponentType<P>,
12
- ): ComponentType<Omit<P, 'popup'>> {
13
- return function WithPopup(props: Omit<P, 'popup'>) {
14
- const p = { ...props, popup: usePopup() } as P;
15
-
16
- return <Component {...p} />;
17
- };
18
- }
@@ -1,3 +0,0 @@
1
- export * from './types.js';
2
- export * from './useQRScanner.js';
3
- export * from './withQRScanner.js';
@@ -1 +0,0 @@
1
- export type { QRScanner } from '@tma.js/sdk';