Skip to content

Commit 1c672ad

Browse files
authored
Merge pull request #71 from New-dev0/master
feat: Add SettingsButton
2 parents cac4927 + da6aebc commit 1c672ad

File tree

6 files changed

+70
-1
lines changed

6 files changed

+70
-1
lines changed

Diff for: docs/interfaces/SettingsButtonProps.md

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
[@vkruglikov/react-telegram-web-app](../README.md) / SettingsButtonProps
2+
3+
# Interface: SettingsButtonProps
4+
5+
The props type of [`SettingsButton`](../README.md#settingsbutton).
6+
7+
## Table of contents
8+
9+
### Properties
10+
11+
- [onClick](SettingsButtonProps.md#onclick)
12+
13+
## Properties
14+
15+
### onClick
16+
17+
`Optional` **onClick**: () => `void`
18+
19+
#### Type declaration
20+
21+
▸ (): `void`
22+
23+
The settings button press event handler
24+
25+
##### Returns
26+
27+
`void`

Diff for: src/SettingsButton.tsx

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { useContext, useEffect, useId } from 'react';
2+
import { useWebApp, useSmoothButtonsTransition, systemContext } from './core';
3+
4+
export interface SettingsButtonProps {
5+
onClick?: () => void;
6+
}
7+
8+
const SettingsButton = ({ onClick }: SettingsButtonProps): null => {
9+
const system = useContext(systemContext);
10+
const buttonId = useId();
11+
const WebApp = useWebApp();
12+
13+
useEffect(() => {
14+
if (!onClick || !WebApp?.SettingsButton) {
15+
return;
16+
}
17+
18+
WebApp.SettingsButton.onClick(onClick);
19+
return () => {
20+
WebApp.SettingsButton.offClick(onClick);
21+
};
22+
}, [onClick, WebApp]);
23+
24+
useSmoothButtonsTransition({
25+
show: WebApp?.SettingsButton?.show,
26+
hide: WebApp?.SettingsButton?.hide,
27+
currentShowedIdRef: system.SettingsButton,
28+
id: buttonId,
29+
});
30+
31+
return null;
32+
};
33+
34+
export default SettingsButton;

Diff for: src/WebAppProvider.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ const WebAppProvider = ({
6262
const forceHideButtons = () => {
6363
globalWebApp?.MainButton?.hide();
6464
globalWebApp?.BackButton?.hide();
65+
globalWebApp?.SettingsButton?.hide();
6566
};
6667

6768
window.addEventListener('beforeunload', forceHideButtons);

Diff for: src/core/context.ts

+2
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,13 @@ 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>;
4142
};
4243

4344
export const createSystemContextValue = () => ({
4445
MainButton: { current: null },
4546
BackButton: { current: null },
47+
SettingsButton: { current: null },
4648
});
4749

4850
export const systemContext = createContext<SystemContext>(

Diff for: src/index.ts

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
export { default as MainButton, MainButtonProps } from './MainButton';
22
export { default as BackButton, BackButtonProps } from './BackButton';
3+
export {
4+
default as SettingsButton,
5+
SettingsButtonProps,
6+
} from './SettingsButton';
37
export {
48
default as useShowPopup,
59
ShowPopupFunction,

Diff for: src/useInitData.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ export type WebAppUser = {
2121
last_name?: string;
2222
username?: string;
2323
language_code?: string;
24-
photo_url?: true;
24+
photo_url?: string;
25+
is_premium?: boolean;
2526
added_to_attachment_menu?: true;
2627
allows_write_to_pm?: true;
2728
};

0 commit comments

Comments
 (0)