@telegram-apps/sdk-svelte
Svelte.js package providing utilities that developers may find useful when developing a mini application.
TIP
Since this package offers utility functions that extend the functionality of @telegram-apps/sdk, it is recommended to review the SDK package documentation first.
Installation
Before proceeding, it is assumed that you have already installed the svelte-js
package, as it is a peer dependency of this package.
pnpm i @telegram-apps/sdk-svelte
npm i @telegram-apps/sdk-svelte
yarn add @telegram-apps/sdk-svelte
INFO
This package fully re-exports the @telegram-apps/sdk package, so you don't need to install it separately.
Usage
Here is a simple usage example of the package:
<script>
import { init, backButton } from '@telegram-apps/sdk-svelte';
import { BackButton } from './BackButton.svelte';
// Initialize the package.
init();
</script>
<template>
<BackButton />
</template>
/**
* Component which controls the Back Button visibility.
*/
<script>
import { onMount, onDestroy } from 'svelte';
import { backButton, useSignal } from '@telegram-apps/sdk-svelte';
const isVisible = useSignal(backButton.isVisible);
$: console.log('The button is', isVisible.value ? 'visible' : 'invisible')
onMount(() => {
backButton.show();
});
onDestroy(() => {
backButton.hide();
});
</script>
<template></template>
Hooks
useSignal
A helper that allows you to use our signals in the application. It returns a Svelte ref which updates every time, our signal changes.
<script>
import { onMount, onDestroy } from 'svelte';
import { backButton, useSignal } from '@telegram-apps/sdk-svelte';
const isVisible = useSignal(backButton.isVisible);
$: console.log('The button is', isVisible.value ? 'visible' : 'invisible')
onMount(() => {
backButton.show();
});
onDestroy(() => {
backButton.hide();
});
</script>
<template></template>
useLaunchParams
A function that returns the mini application's launch parameters.
<script>
import { useLaunchParams } from '@telegram-apps/sdk-svelte';
const lp = useLaunchParams();
</script>
<template>
<div>Start param: {{ lp.startParam }}</div>
</template>