Skip to content

Commit da7b810

Browse files
authored
Merge pull request #39 from vkruglikov/rc
useWebApp hook
2 parents d44d6b7 + c28cc2e commit da7b810

20 files changed

+39
-18
lines changed

.changeset/modern-wolves-dream.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@vkruglikov/react-telegram-web-app': minor
3+
---
4+
5+
useWebApp hook

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ const Content = () => {
8585
This hook provides `CloudStorage` object as Promise functions
8686
- [useInitData](./docs/README.md#useinitdata) -
8787
This hook provides `InitDataUnsafe` and `InitData` object
88+
- [useWebApp](./docs/README.md#usewebapp) -
89+
This hook just provides native `WebApp` object
8890

8991
## 🛣 Roadmap
9092

global.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { WebApp } from './src/core';
1+
import { WebApp } from './twa-types';
22

33
declare global {
44
interface Window {

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"unpkg": "lib/react-telegram-web-app.umd.js",
2323
"types": "lib/index.d.ts",
2424
"scripts": {
25-
"build": "rm -rf ./lib/*; microbundle build --sourcemap=false && cp -R src/core/twa-types lib/core/twa-types",
25+
"build": "rm -rf ./lib/*; microbundle build --sourcemap=false",
2626
"dev": "npm run build && microbundle watch --compress=false",
2727
"prepare": "husky install",
2828
"changeset": "changeset",

src/core/context.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { createContext, MutableRefObject } from 'react';
2-
import { WebApp } from './twa-types';
32

43
export const DEFAULT_WEBAPP =
54
typeof window !== 'undefined' && window?.Telegram?.WebApp
65
? window.Telegram.WebApp
76
: null;
87

9-
export const webAppContext = createContext<WebApp | null>(DEFAULT_WEBAPP);
8+
export const webAppContext =
9+
createContext<typeof DEFAULT_WEBAPP>(DEFAULT_WEBAPP);
1010

1111
/**
1212
* This object describe options be able to set through WebAppProvider

src/core/index.ts

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
export { WebApp } from './twa-types';
21
export { default as useSmoothButtonsTransition } from './useSmoothButtonsTransition';
32
export { default as useWebApp } from './useWebApp';
43
export * from './context';

src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,4 @@ export {
5454
InitDataUnsafe,
5555
} from './useInitData';
5656
export type { Options } from './core';
57+
export { default as useWebApp } from './useWebApp';

src/useWebApp.ts

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { useWebApp as _useWebApp } from './core';
2+
3+
/**
4+
* This hook just provides native {@link telegram!WebApp} object
5+
*
6+
* ```tsx
7+
* import { useWebApp } from "@vkruglikov/react-telegram-web-app";
8+
*
9+
* const WebApp = useWebApp();
10+
*
11+
* console.log(WebApp.version);
12+
* ```
13+
* @group Hooks
14+
*/
15+
const useWebApp = () => _useWebApp();
16+
17+
export default useWebApp;

tests/__snapshots__/package.test.ts.snap

+2-9
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,6 @@ exports[`Package /lib should have correct /lib structure 1`] = `
88
[
99
"/core/context.d.ts",
1010
"/core/index.d.ts",
11-
[
12-
"/core/twa-types/WebApp.d.ts",
13-
"/core/twa-types/WebAppVersion_6.1.d.ts",
14-
"/core/twa-types/WebAppVersion_6.2.d.ts",
15-
"/core/twa-types/WebAppVersion_6.4.d.ts",
16-
"/core/twa-types/WebAppVersion_6.7.d.ts",
17-
"/core/twa-types/WebAppVersion_6.9.d.ts",
18-
"/core/twa-types/index.d.ts",
19-
],
2011
"/core/useSmoothButtonsTransition.d.ts",
2112
"/core/useWebApp.d.ts",
2213
],
@@ -34,6 +25,7 @@ exports[`Package /lib should have correct /lib structure 1`] = `
3425
"/useShowPopup.d.ts",
3526
"/useSwitchInlineQuery.d.ts",
3627
"/useThemeParams.d.ts",
28+
"/useWebApp.d.ts",
3729
]
3830
`;
3931

@@ -51,5 +43,6 @@ exports[`Package /lib should have exports from modules 1`] = `
5143
"useShowPopup": [Function],
5244
"useSwitchInlineQuery": [Function],
5345
"useThemeParams": [Function],
46+
"useWebApp": [Function],
5447
}
5548
`;

tests/useExpand.test.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { act, renderHook } from '@testing-library/react';
22

3-
import { useWebApp, WebApp } from '../src/core';
3+
import { useWebApp } from '../src/core';
4+
import { WebApp } from '../twa-types';
45
import useExpand from '../src/useExpand';
56

67
describe('useExpand', () => {

tests/useReadTextFromClipboard.test.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { renderHook } from '@testing-library/react';
22
import useReadTextFromClipboard from '../src/useReadTextFromClipboard';
3-
import { useWebApp, WebApp } from '../src/core';
3+
import { useWebApp } from '../src/core';
4+
import { WebApp } from '../twa-types';
45

56
describe('useReadTextFromClipboard', () => {
67
it('checks correct call WebApp.readTextFromClipboard api', async () => {

tests/useShowPopup.test.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { renderHook } from '@testing-library/react';
22
import useShowPopup from '../src/useShowPopup';
3-
import { useWebApp, WebApp } from '../src/core';
3+
import { useWebApp } from '../src/core';
4+
import { WebApp } from '../twa-types';
45

56
describe('useShowPopup', () => {
67
it('checks correct call WebApp.showPopup api', async () => {

tests/useThemeParams.test.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { act, renderHook } from '@testing-library/react';
22

3-
import { useWebApp, WebApp } from '../src/core';
3+
import { useWebApp } from '../src/core';
4+
import { WebApp } from '../twa-types';
45
import useThemeParams from '../src/useThemeParams';
56

67
describe('useThemeParams', () => {
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)