Skip to content

Commit

Permalink
Sessings button wa added
Browse files Browse the repository at this point in the history
  • Loading branch information
vkruglikov committed Nov 24, 2024
1 parent 1c672ad commit b747352
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 19 deletions.
5 changes: 5 additions & 0 deletions .changeset/lemon-apes-hunt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@vkruglikov/react-telegram-web-app': minor
---

Add SettingsButton
34 changes: 31 additions & 3 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- [BackButtonProps](interfaces/BackButtonProps.md)
- [MainButtonProps](interfaces/MainButtonProps.md)
- [ScanQrPopupParams](interfaces/ScanQrPopupParams.md)
- [SettingsButtonProps](interfaces/SettingsButtonProps.md)
- [ShowPopupButton](interfaces/ShowPopupButton.md)
- [ShowPopupParams](interfaces/ShowPopupParams.md)
- [ThemeParams](interfaces/ThemeParams.md)
Expand Down Expand Up @@ -55,6 +56,7 @@

- [BackButton](README.md#backbutton)
- [MainButton](README.md#mainbutton)
- [SettingsButton](README.md#settingsbutton)
- [WebAppProvider](README.md#webappprovider)

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

| Name | Type | Description |
| :--------------------------- | :-------- | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `async?` | `boolean` | - |
| `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` |
| `smoothButtonsTransitionMs?` | `number` | **`Default Value`** `10` **`Remarks`** |

Expand Down Expand Up @@ -493,9 +496,10 @@ You have to look original description switchInlineQuery in [telegram!WebApp](htt
| `first_name` | `string` |
| `id` | `number` |
| `is_bot?` | `boolean` |
| `is_premium?` | `boolean` |
| `language_code?` | `string` |
| `last_name?` | `string` |
| `photo_url?` | `true` |
| `photo_url?` | `string` |
| `username?` | `string` |

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

### useInitData

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

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

#### Returns

readonly [[`InitDataUnsafe`](README.md#initdataunsafe), `string`]
readonly [`undefined` \| [`InitDataUnsafe`](README.md#initdataunsafe), `undefined` \| `string`]

---

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

---

### SettingsButton

**SettingsButton**(`props`): `null`

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)

```tsx
import { SettingsButton } from '@vkruglikov/react-telegram-web-app';

<SettingsButton onClick={() => console.log('Hello, I am settings button!')} />;
```

#### Parameters

| Name | Type |
| :------ | :--------------------------------------------------------- |
| `props` | [`SettingsButtonProps`](interfaces/SettingsButtonProps.md) |

#### Returns

`null`

---

### WebAppProvider

**WebAppProvider**(`props`): `ReactElement`<`any`, `string` \| `JSXElementConstructor`<`any`\>\>
Expand Down
2 changes: 0 additions & 2 deletions docs/interfaces/SettingsButtonProps.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ The props type of [`SettingsButton`](../README.md#settingsbutton).

▸ (): `void`

The settings button press event handler

##### Returns

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

/**
* The props type of {@link SettingsButton | `SettingsButton`}.
*/
export interface SettingsButtonProps {
onClick?: () => void;
}

/**
* Renders a {@link telegram!SettingsButton} component in React app as {@link react!Component}
*
* ```tsx
* import { SettingsButton } from "@vkruglikov/react-telegram-web-app";
*
* <SettingsButton
* onClick={() => console.log('Hello, I am settings button!')}
* />
* ```
* @param props
* @group React Components
*/
const SettingsButton = ({ onClick }: SettingsButtonProps): null => {
const system = useContext(systemContext);
const buttonId = useId();
const WebApp = useWebApp();

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

useSmoothButtonsTransition({
show: WebApp?.SettingsButton?.show,
hide: WebApp?.SettingsButton?.hide,
currentShowedIdRef: system.SettingsButton,
id: buttonId,
});
useEffect(() => {
if (!WebApp?.SettingsButton) {
return;
}

WebApp.SettingsButton.show?.();

return () => {
WebApp.SettingsButton.hide?.();
};
}, [WebApp]);

return null;
};
Expand Down
2 changes: 0 additions & 2 deletions src/core/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,11 @@ 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
2 changes: 1 addition & 1 deletion src/core/twa-types/WebApp.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export declare namespace TelegramWebApp {
last_name?: string;
username?: string;
language_code?: string;
photo_url?: true;
photo_url?: string;
}

/**
Expand Down
6 changes: 6 additions & 0 deletions src/core/twa-types/WebAppVersion_7.0.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export declare namespace TelegramWebAppVersion7_0 {
interface WebApp extends TelegramWebAppVersion6_9.WebApp {
// TODO Fix types SettingsButton
SettingsButton?: any;
}
}
4 changes: 3 additions & 1 deletion src/core/twa-types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import { TelegramWebAppVersion6_2 } from './WebAppVersion_6.2';
import { TelegramWebAppVersion6_4 } from './WebAppVersion_6.4';
import { TelegramWebAppVersion6_7 } from './WebAppVersion_6.7';
import { TelegramWebAppVersion6_9 } from './WebAppVersion_6.9';
import { TelegramWebAppVersion7_0 } from './WebAppVersion_7.0';

export type WebApp = TelegramWebApp.WebApp &
Partial<TelegramWebAppVersion6_1.WebApp> &
Partial<TelegramWebAppVersion6_2.WebApp> &
Partial<TelegramWebAppVersion6_4.WebApp> &
Partial<TelegramWebAppVersion6_7.WebApp> &
Partial<TelegramWebAppVersion6_9.WebApp>;
Partial<TelegramWebAppVersion6_9.WebApp> &
Partial<TelegramWebAppVersion7_0.WebApp>;
1 change: 1 addition & 0 deletions typedoc.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"telegram": {
"CloudStorage": "https://core.telegram.org/bots/webapps#cloudstorage",
"MainButton": "https://core.telegram.org/bots/webapps#mainbutton",
"SettingsButton": "https://core.telegram.org/bots/webapps#settingsbutton",
"ScanQrPopupParams": "https://core.telegram.org/bots/webapps#scanqrpopupparams",
"BackButton": "https://core.telegram.org/bots/webapps#backbutton",
"PopupParams": "https://core.telegram.org/bots/webapps#popupparams",
Expand Down

0 comments on commit b747352

Please sign in to comment.