Code Monkey home page Code Monkey logo

react-tg-webapp's Introduction

React Telegram Web App

React wrapper for Telegram Web Apps.

This project is in early development stage. Breaking change may happen at any time.

Installation

yarn add react-tg-webapp

Usage

With React Context

React Telegram Web App is a React Context Provider. This will only provide Telegram object to the children components.

import { WebAppProvider } from "react-tg-webapp"

export default function App() {
  return (
    <WebAppProvider>
      <Children />
    </WebAppProvider>
  )
}

Then you can use Telegram.WebApp object in any children components.

import { useContext } from "react"
import { TelegramWebAppContext, MainButton } from "react-tg-webapp"

export default function Children() {
  const { WebApp, isReady } = useContext(TelegramWebAppContext)

  return (
    <div>
      <h1>WebApp is {isReady ? "ready" : "not ready"}</h1>
      <MainButton
        color="#000"
        text="Hello World"
        textColor="#fff"
        onClick={() => {
          console.log("Main button clicked")
        }}
      />
    </div>
  )
}

With HOC

The package also provides a HOC that under the hood also uses WebAppProvider context provider. With this HOC, you can use Telegram.WebApp object in any wrapped component without needing it to put inside a WebAppProvider children.

import { useContext } from "react"
import { TelegramWebAppContext, withTelegramWebApp, MainButton } from "react-tg-webapp"

function App() {
  const { WebApp, isReady } = useContext(TelegramWebAppContext)

  return (
    <div>
      <h1>WebApp is {isReady ? "ready" : "not ready"}</h1>
      <MainButton
        color="#000"
        text="Hello World"
        textColor="#fff"
        onClick={() => {
          console.log("Main button clicked")
        }}
      />
    </div>
  )
}

const AppWithWebApp = withTelegramWebApp(App)
export default AppWithWebApp

Components

Telegram Web App components will not render anything in the browser. It will only render the component in the Telegram interface.

MainButton

A component for displaying a button at the bottom of the web app in the Telegram interface.

Refer to

import { MainButton } from "react-tg-webapp"

export default function App() {
  return (
    <MainButton
      color="#000"
      text="Hello World"
      textColor="#fff"
      onClick={() => {
        console.log("Main button clicked")
      }}
    />
  )
}

BackButton

A component for displaying a back button at the header of the web app in the Telegram interface.

Refer to

import { BackButton } from "react-tg-webapp"

export default function App() {
  return (
    <BackButton
      onClick={() => {
        console.log("Back button clicked")
      }}
    />
  )
}

Hooks

useMetadata()

A hook for getting the Web App metadata.

useSendData()

A hook for sending data to the bot.

This mehtod is only available for Web Apps launched via a Keyboard Button

usePopup()

A hook for opening a popup in the Telegram interface.

function App() {
  const { WebApp } = useContext(TelegramWebAppContext)
  const { showPopup } = usePopup()

  const handleClose = async () => {
    const id = await showPopup({
      title: "WebApp",
      message: "Are you sure you want to close the app?",
      buttons: [
        { id: "1", text: "cancel", type: "cancel" },
        { id: "2", text: "Close", type: "destructive" },
      ],
    })
    if (id === "2") {
      WebApp.close()
    }
  }

  return <BackButton onClick={handleClose} />
}

useThemeParams()

A hook for getting theme parameters from the Telegram interface.

useScanQrPopupParams()

useHapticFeedback()

A hook for sending a haptic feedback to the Telegram Client.

react-tg-webapp's People

Contributors

mrmissx avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.