@@ -6,9 +6,13 @@ import {
66 deleteCssVar ,
77 setCssVar ,
88 supports ,
9+ on ,
10+ off ,
11+ type EventListener ,
912 type RGB ,
1013 type BottomBarColor ,
11- type BackgroundColor , MethodName ,
14+ type BackgroundColor ,
15+ type MethodName ,
1216} from '@telegram-apps/bridge' ;
1317import { isRGB } from '@telegram-apps/transformers' ;
1418import { isPageReload } from '@telegram-apps/navigation' ;
@@ -31,6 +35,7 @@ import {
3135 headerColorRGB ,
3236 bottomBarColorRGB ,
3337 backgroundColorRGB ,
38+ isActive ,
3439} from './signals.js' ;
3540import type { GetCssVarNameFn , HeaderColor , State } from './types.js' ;
3641
@@ -39,6 +44,7 @@ type StorageValue = State;
3944const SET_BG_COLOR_METHOD = 'web_app_set_background_color' ;
4045const SET_BOTTOM_BAR_COLOR_METHOD = 'web_app_set_bottom_bar_color' ;
4146const SET_HEADER_COLOR_METHOD = 'web_app_set_header_color' ;
47+ const VISIBILITY_CHANGED_EVENT = 'visibility_changed' ;
4248const COMPONENT_NAME = 'miniApp' ;
4349
4450const isSupportedSchema = {
@@ -138,6 +144,11 @@ export const close = wrapBasic('close', (returnBack?: boolean): void => {
138144 postEvent ( 'web_app_close' , { return_back : returnBack } ) ;
139145} ) ;
140146
147+ const onVisibilityChanged : EventListener < 'visibility_changed' > = ( data ) => {
148+ isActive . set ( data . is_visible ) ;
149+ saveState ( ) ;
150+ } ;
151+
141152/**
142153 * Mounts the component.
143154 *
@@ -165,6 +176,9 @@ export const mount = wrapSupported(
165176 setBackgroundColor . ifAvailable ( s ? s . backgroundColor : 'bg_color' ) ;
166177 setBottomBarColor . ifAvailable ( s ? s . bottomBarColor : 'bottom_bar_bg_color' ) ;
167178 setHeaderColor . ifAvailable ( s ? s . headerColor : 'bg_color' ) ;
179+ isActive . set ( s ? s . isActive : true ) ;
180+
181+ on ( VISIBILITY_CHANGED_EVENT , onVisibilityChanged ) ;
168182
169183 isMounted . set ( true ) ;
170184 }
@@ -280,5 +294,6 @@ export const setHeaderColor = wrapComplete(
280294 * Unmounts the component, removing the listener, saving the component state in the local storage.
281295 */
282296export function unmount ( ) : void {
297+ off ( VISIBILITY_CHANGED_EVENT , onVisibilityChanged ) ;
283298 isMounted . set ( false ) ;
284299}
0 commit comments