Skip to content

Commit

Permalink
Merge pull request #71 from New-dev0/master
Browse files Browse the repository at this point in the history
feat: Add SettingsButton
  • Loading branch information
vkruglikov authored Nov 24, 2024
2 parents cac4927 + da6aebc commit 1c672ad
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 1 deletion.
27 changes: 27 additions & 0 deletions docs/interfaces/SettingsButtonProps.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[@vkruglikov/react-telegram-web-app](../README.md) / SettingsButtonProps

# Interface: SettingsButtonProps

The props type of [`SettingsButton`](../README.md#settingsbutton).

## Table of contents

### Properties

- [onClick](SettingsButtonProps.md#onclick)

## Properties

### onClick

`Optional` **onClick**: () => `void`

#### Type declaration

▸ (): `void`

The settings button press event handler

##### Returns

`void`
34 changes: 34 additions & 0 deletions src/SettingsButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { useContext, useEffect, useId } from 'react';
import { useWebApp, useSmoothButtonsTransition, systemContext } from './core';

export interface SettingsButtonProps {
onClick?: () => void;
}

const SettingsButton = ({ onClick }: SettingsButtonProps): null => {
const system = useContext(systemContext);
const buttonId = useId();
const WebApp = useWebApp();

useEffect(() => {
if (!onClick || !WebApp?.SettingsButton) {
return;
}

WebApp.SettingsButton.onClick(onClick);
return () => {
WebApp.SettingsButton.offClick(onClick);
};
}, [onClick, WebApp]);

useSmoothButtonsTransition({
show: WebApp?.SettingsButton?.show,
hide: WebApp?.SettingsButton?.hide,
currentShowedIdRef: system.SettingsButton,
id: buttonId,
});

return null;
};

export default SettingsButton;
1 change: 1 addition & 0 deletions src/WebAppProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ const WebAppProvider = ({
const forceHideButtons = () => {
globalWebApp?.MainButton?.hide();
globalWebApp?.BackButton?.hide();
globalWebApp?.SettingsButton?.hide();
};

window.addEventListener('beforeunload', forceHideButtons);
Expand Down
2 changes: 2 additions & 0 deletions src/core/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,13 @@ export const optionsContext = createContext<Options>(DEFAULT_OPTIONS);
type SystemContext = {
MainButton: MutableRefObject<null | string>;
BackButton: MutableRefObject<null | string>;
SettingsButton: MutableRefObject<null | string>;
};

export const createSystemContextValue = () => ({
MainButton: { current: null },
BackButton: { current: null },
SettingsButton: { current: null },
});

export const systemContext = createContext<SystemContext>(
Expand Down
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
export { default as MainButton, MainButtonProps } from './MainButton';
export { default as BackButton, BackButtonProps } from './BackButton';
export {
default as SettingsButton,
SettingsButtonProps,
} from './SettingsButton';
export {
default as useShowPopup,
ShowPopupFunction,
Expand Down
3 changes: 2 additions & 1 deletion src/useInitData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ export type WebAppUser = {
last_name?: string;
username?: string;
language_code?: string;
photo_url?: true;
photo_url?: string;
is_premium?: boolean;
added_to_attachment_menu?: true;
allows_write_to_pm?: true;
};
Expand Down

0 comments on commit 1c672ad

Please sign in to comment.