Skip to content

Commit b747352

Browse files
committed
Sessings button wa added
1 parent 1c672ad commit b747352

File tree

9 files changed

+76
-19
lines changed

9 files changed

+76
-19
lines changed

Diff for: .changeset/lemon-apes-hunt.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+
Add SettingsButton

Diff for: docs/README.md

+31-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
- [BackButtonProps](interfaces/BackButtonProps.md)
1010
- [MainButtonProps](interfaces/MainButtonProps.md)
1111
- [ScanQrPopupParams](interfaces/ScanQrPopupParams.md)
12+
- [SettingsButtonProps](interfaces/SettingsButtonProps.md)
1213
- [ShowPopupButton](interfaces/ShowPopupButton.md)
1314
- [ShowPopupParams](interfaces/ShowPopupParams.md)
1415
- [ThemeParams](interfaces/ThemeParams.md)
@@ -55,6 +56,7 @@
5556

5657
- [BackButton](README.md#backbutton)
5758
- [MainButton](README.md#mainbutton)
59+
- [SettingsButton](README.md#settingsbutton)
5860
- [WebAppProvider](README.md#webappprovider)
5961

6062
## Type Aliases
@@ -245,6 +247,7 @@ This object describe options be able to set through WebAppProvider
245247

246248
| Name | Type | Description |
247249
| :--------------------------- | :-------- | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
250+
| `async?` | `boolean` | - |
248251
| `smoothButtonsTransition?` | `boolean` | When is `true`, we can smooth button transitions due to show(), hide() calls. So when you use MainButton or BackButton on multiple pages, there will be no noticeable flickering of the button during transitions **`Default Value`** `false` |
249252
| `smoothButtonsTransitionMs?` | `number` | **`Default Value`** `10` **`Remarks`** |
250253

@@ -493,9 +496,10 @@ You have to look original description switchInlineQuery in [telegram!WebApp](htt
493496
| `first_name` | `string` |
494497
| `id` | `number` |
495498
| `is_bot?` | `boolean` |
499+
| `is_premium?` | `boolean` |
496500
| `language_code?` | `string` |
497501
| `last_name?` | `string` |
498-
| `photo_url?` | `true` |
502+
| `photo_url?` | `string` |
499503
| `username?` | `string` |
500504

501505
## Hooks
@@ -574,7 +578,7 @@ readonly [[`ImpactOccurredFunction`](README.md#impactoccurredfunction), [`Notifi
574578

575579
### useInitData
576580

577-
**useInitData**(): readonly [[`InitDataUnsafe`](README.md#initdataunsafe), `string`]
581+
**useInitData**(): readonly [`undefined` \| [`InitDataUnsafe`](README.md#initdataunsafe), `undefined` \| `string`]
578582

579583
This hook provides `initDataUnsafe` and `initData`
580584
You have to look original description in [telegram!WebApp](https://core.telegram.org/bots/webapps#initializing-mini-apps), because hook just return this.
@@ -588,7 +592,7 @@ const [initDataUnsafe, initData] = useInitData();
588592

589593
#### Returns
590594

591-
readonly [[`InitDataUnsafe`](README.md#initdataunsafe), `string`]
595+
readonly [`undefined` \| [`InitDataUnsafe`](README.md#initdataunsafe), `undefined` \| `string`]
592596

593597
---
594598

@@ -757,6 +761,30 @@ import { MainButton } from '@vkruglikov/react-telegram-web-app';
757761

758762
---
759763

764+
### SettingsButton
765+
766+
**SettingsButton**(`props`): `null`
767+
768+
Renders a [telegram!SettingsButton](https://core.telegram.org/bots/webapps#settingsbutton) component in React app as [react!Component](https://reactjs.org/docs/react-component.html)
769+
770+
```tsx
771+
import { SettingsButton } from '@vkruglikov/react-telegram-web-app';
772+
773+
<SettingsButton onClick={() => console.log('Hello, I am settings button!')} />;
774+
```
775+
776+
#### Parameters
777+
778+
| Name | Type |
779+
| :------ | :--------------------------------------------------------- |
780+
| `props` | [`SettingsButtonProps`](interfaces/SettingsButtonProps.md) |
781+
782+
#### Returns
783+
784+
`null`
785+
786+
---
787+
760788
### WebAppProvider
761789

762790
**WebAppProvider**(`props`): `ReactElement`<`any`, `string` \| `JSXElementConstructor`<`any`\>\>

Diff for: docs/interfaces/SettingsButtonProps.md

-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ The props type of [`SettingsButton`](../README.md#settingsbutton).
2020

2121
▸ (): `void`
2222

23-
The settings button press event handler
24-
2523
##### Returns
2624

2725
`void`

Diff for: src/SettingsButton.tsx

+29-10
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,27 @@
1-
import { useContext, useEffect, useId } from 'react';
2-
import { useWebApp, useSmoothButtonsTransition, systemContext } from './core';
1+
import { useEffect } from 'react';
2+
import { useWebApp } from './core';
33

4+
/**
5+
* The props type of {@link SettingsButton | `SettingsButton`}.
6+
*/
47
export interface SettingsButtonProps {
58
onClick?: () => void;
69
}
710

11+
/**
12+
* Renders a {@link telegram!SettingsButton} component in React app as {@link react!Component}
13+
*
14+
* ```tsx
15+
* import { SettingsButton } from "@vkruglikov/react-telegram-web-app";
16+
*
17+
* <SettingsButton
18+
* onClick={() => console.log('Hello, I am settings button!')}
19+
* />
20+
* ```
21+
* @param props
22+
* @group React Components
23+
*/
824
const SettingsButton = ({ onClick }: SettingsButtonProps): null => {
9-
const system = useContext(systemContext);
10-
const buttonId = useId();
1125
const WebApp = useWebApp();
1226

1327
useEffect(() => {
@@ -21,12 +35,17 @@ const SettingsButton = ({ onClick }: SettingsButtonProps): null => {
2135
};
2236
}, [onClick, WebApp]);
2337

24-
useSmoothButtonsTransition({
25-
show: WebApp?.SettingsButton?.show,
26-
hide: WebApp?.SettingsButton?.hide,
27-
currentShowedIdRef: system.SettingsButton,
28-
id: buttonId,
29-
});
38+
useEffect(() => {
39+
if (!WebApp?.SettingsButton) {
40+
return;
41+
}
42+
43+
WebApp.SettingsButton.show?.();
44+
45+
return () => {
46+
WebApp.SettingsButton.hide?.();
47+
};
48+
}, [WebApp]);
3049

3150
return null;
3251
};

Diff for: src/core/context.ts

-2
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,11 @@ export const optionsContext = createContext<Options>(DEFAULT_OPTIONS);
3838
type SystemContext = {
3939
MainButton: MutableRefObject<null | string>;
4040
BackButton: MutableRefObject<null | string>;
41-
SettingsButton: MutableRefObject<null | string>;
4241
};
4342

4443
export const createSystemContextValue = () => ({
4544
MainButton: { current: null },
4645
BackButton: { current: null },
47-
SettingsButton: { current: null },
4846
});
4947

5048
export const systemContext = createContext<SystemContext>(

Diff for: src/core/twa-types/WebApp.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export declare namespace TelegramWebApp {
5656
last_name?: string;
5757
username?: string;
5858
language_code?: string;
59-
photo_url?: true;
59+
photo_url?: string;
6060
}
6161

6262
/**

Diff for: src/core/twa-types/WebAppVersion_7.0.d.ts

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export declare namespace TelegramWebAppVersion7_0 {
2+
interface WebApp extends TelegramWebAppVersion6_9.WebApp {
3+
// TODO Fix types SettingsButton
4+
SettingsButton?: any;
5+
}
6+
}

Diff for: src/core/twa-types/index.d.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ import { TelegramWebAppVersion6_2 } from './WebAppVersion_6.2';
44
import { TelegramWebAppVersion6_4 } from './WebAppVersion_6.4';
55
import { TelegramWebAppVersion6_7 } from './WebAppVersion_6.7';
66
import { TelegramWebAppVersion6_9 } from './WebAppVersion_6.9';
7+
import { TelegramWebAppVersion7_0 } from './WebAppVersion_7.0';
78

89
export type WebApp = TelegramWebApp.WebApp &
910
Partial<TelegramWebAppVersion6_1.WebApp> &
1011
Partial<TelegramWebAppVersion6_2.WebApp> &
1112
Partial<TelegramWebAppVersion6_4.WebApp> &
1213
Partial<TelegramWebAppVersion6_7.WebApp> &
13-
Partial<TelegramWebAppVersion6_9.WebApp>;
14+
Partial<TelegramWebAppVersion6_9.WebApp> &
15+
Partial<TelegramWebAppVersion7_0.WebApp>;

Diff for: typedoc.json

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"telegram": {
1010
"CloudStorage": "https://core.telegram.org/bots/webapps#cloudstorage",
1111
"MainButton": "https://core.telegram.org/bots/webapps#mainbutton",
12+
"SettingsButton": "https://core.telegram.org/bots/webapps#settingsbutton",
1213
"ScanQrPopupParams": "https://core.telegram.org/bots/webapps#scanqrpopupparams",
1314
"BackButton": "https://core.telegram.org/bots/webapps#backbutton",
1415
"PopupParams": "https://core.telegram.org/bots/webapps#popupparams",

0 commit comments

Comments
 (0)