32 lines
824 B
TypeScript
32 lines
824 B
TypeScript
|
|
declare global {
|
||
|
|
interface Window {
|
||
|
|
Telegram?: {
|
||
|
|
WebApp?: {
|
||
|
|
ready?: () => void
|
||
|
|
expand?: () => void
|
||
|
|
initData?: string
|
||
|
|
colorScheme?: 'light' | 'dark'
|
||
|
|
themeParams?: Record<string, string>
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
export function getTelegramInitData(): string | undefined {
|
||
|
|
return window.Telegram?.WebApp?.initData || undefined
|
||
|
|
}
|
||
|
|
|
||
|
|
export function applyTelegramTheme(): void {
|
||
|
|
const webApp = window.Telegram?.WebApp
|
||
|
|
webApp?.ready?.()
|
||
|
|
webApp?.expand?.()
|
||
|
|
const theme = webApp?.themeParams || {}
|
||
|
|
const root = document.documentElement
|
||
|
|
Object.entries(theme).forEach(([key, value]) => {
|
||
|
|
if (typeof value === 'string') root.style.setProperty(`--tg-${key.replaceAll('_', '-')}`, value)
|
||
|
|
})
|
||
|
|
if (webApp?.colorScheme) root.dataset.theme = webApp.colorScheme
|
||
|
|
}
|
||
|
|
|
||
|
|
export {}
|