App state engine

appState is a reactive object with what the app knows about the screen, the connection and the system. Read it in a template or a $derived and it updates by itself.

What it does
Holds what the app knows about the screen, the connection and the system, and the history that lets you go back. It does not depend on anyone being signed in.
Exported from the package?
Yes. Import appState, navigateBack and initAppState from @davidnet-net/svelte-ui. AppShell calls initAppState for you.

Which one do I use?

What I needUse
Change the markup on a small screen, or when offlineappState
Follow what the system asks (dark mode, less motion)appState.systemPreference
A Back buttonnavigateBack

appState

typescript
typescript
const appState: AppStateType

The fields of appState that you read. You change sidebarOpen and hideNavigation; the rest is kept up to date by the engine.

When to use

Use it to adapt to the screen (isMobile, isTinyMobile), to the connection (isOffline, which waits 3 seconds so a short drop does not flash) and to the system (systemPreference).

When not to use, and what to use instead

For layout that CSS can do, use a media query: it needs no code and has no flash. Use appState.isMobile when the markup itself must differ. For the theme use currentTheme, which already looks at systemPreference.darkMode.

Options

NameTypeDefaultDescription
sidebarOpen boolean—Whether the sidebar is open.
isMobile boolean—The screen is 768 pixels wide or less. Also true when isTinyMobile is true.
isTinyMobile boolean—The screen is 480 pixels wide or less.
isOffline boolean—The browser has been offline for 3 seconds.
viteConnected boolean—Whether the dev server is connected. Always false in a production build.
hideNavigation boolean—Hides the navigation of AppShell, for pages that need the whole screen.
backendURL, accountFrontendURL string—The addresses of the backend and the account app, from the environment of the app.
tabID string | undefined—An id that is different for every tab.
systemPreference { darkMode: boolean; highContrast: boolean; reduceMotion: boolean }—What the system asks for: darkMode, highContrast and reduceMotion.
previousPage, historyStack string | undefined, string[]—The page before this one, and the pages visited in this tab.

Live example

CallResult
appState.isMobilefalse
appState.isTinyMobilefalse
appState.isOfflinefalse
appState.systemPreference.darkModefalse
appState.systemPreference.reduceMotionfalse
appState.previousPageundefined
Resize the window, or switch the system theme, and the results change.
svelte
svelte
<script lang="ts">
	import { appState } from "@davidnet-net/svelte-ui";
</script>

{#if appState.isMobile}
	<MobileMenu />
{:else}
	<DesktopMenu />
{/if}