twa-dev/sdk

repository·master·Indexed 43 days ago

https://github.com/twa-dev/sdk

A developer-friendly SDK for Telegram Web Apps (TWA) version 8.0.2, providing a typed wrapper for the WebApp object and declarative React components such as MainButton, SecondaryButton, BottomBar, and BackButton to manage TWA features.

Tokens
2.9K
Snippets
16
Records
19
Agent score
53%

What's inside twa-dev/sdk

  1. How BottomBar, MainButton, and SecondaryButton work together

    master
    In the TWA SDK, the bottom bar is a dedicated area that wraps the MainButton and SecondaryButton. When using the React implementation, you wrap your buttons inside the <BottomBar> component. This allows you to manage the bottom bar's background color (bgColor) and the lifecycle of the buttons declaratively within the React component tree.
  2. Use @twa-dev/sdk with React

    master

    For React projects, the SDK provides specialized components that simplify interacting with Telegram Web App elements. Instead of calling imperative methods on the WebApp object, you can use declarative components.

    Available React components include:

    • MainButton
    • SecondaryButton
    • BottomBar
    • BackButton
  3. Use @twa-dev/sdk in JavaScript/TypeScript

    master

    Instead of relying on the global window.Telegram.WebApp object provided by the Telegram script, you can import WebApp directly from the package. This allows for a cleaner, module-based development workflow with full TypeScript support.

    import WebApp from '@twa-dev/sdk'
    
    WebApp.showAlert('Hey there!');
  4. Install @twa-dev/sdk via npm

    master

    Install the @twa-dev/sdk package to use the Telegram Web Apps (TWA) SDK as a modern npm module instead of using the traditional <script> tag approach. This package provides TypeScript support.

    npm i @twa-dev/sdk
  5. Use the MainButton React component

    master

    The MainButton component provides a declarative React wrapper for the imperative Telegram Web Apps MainButton SDK. Instead of manually calling methods like .setText() or .show(), you can use the component with props that correspond to the underlying SDK methods.

    import { MainButton } from '@twa-dev/sdk/react';
    
    <MainButton text="Submit" onClick={() => alert('submitted')} />
  6. Use the BackButton React component

    master

    The @twa-dev/sdk/react package provides a declarative React component wrapper for the imperative Telegram Web Apps (TWA) BackButton SDK. Instead of manually calling BackButton.show() and BackButton.onClick(), you can use the <BackButton /> component and pass an onClick handler.

    import { BackButton } from '@twa-dev/sdk/react';
    
    // Inside your component
    <BackButton onClick={() => window.history.back()} />
  7. Use the BottomBar React component

    master

    The BottomBar component provides a declarative React wrapper for the imperative Telegram Web Apps bottom bar SDK. It acts as a container for MainButton and SecondaryButton components. Instead of calling window.Telegram.WebApp.setBottomBarColor manually, you can control the bottom bar appearance using the bgColor prop.

    import { BottomBar, MainButton, SecondaryButton } from '@twa-dev/sdk/react';
    
    <BottomBar bgColor="#ff0000">
      <MainButton text="Continue" onClick={() => alert('continue')} />
      <SecondaryButton text="Cancel" position="bottom" onClick={() => alert('cancelled')} />    
    </BottomBar>
  8. Use the SecondaryButton React component

    master

    The SecondaryButton component is a React wrapper for the imperative Telegram Web App SecondaryButton SDK. It allows you to control the TWA Secondary Button using a declarative React pattern instead of calling imperative methods like .setParams(), .show(), and .onClick() manually.

    import { MainButton, SecondaryButton } from '@twa-dev/sdk/react';
    
    <MainButton text="Continue" onClick={() => alert('continue')} />
    <SecondaryButton text="Cancel" position="bottom" onClick={() => alert('cancelled')} />