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,navigateBackandinitAppStatefrom@davidnet-net/svelte-ui. AppShell callsinitAppStatefor you.
Which one do I use?
| What I need | Use |
|---|---|
| Change the markup on a small screen, or when offline | appState |
| Follow what the system asks (dark mode, less motion) | appState.systemPreference |
| A Back button | navigateBack |
appState
typescript
typescript
const appState: AppStateTypeThe 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
| Name | Type | Default | Description |
|---|---|---|---|
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
| Call | Result |
|---|---|
| appState.isMobile | false |
| appState.isTinyMobile | false |
| appState.isOffline | false |
| appState.systemPreference.darkMode | false |
| appState.systemPreference.reduceMotion | false |
| appState.previousPage | undefined |
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}