Overlays
Concept
1. Why
Dialogs and bottom sheets should be written like small pages in their own files: their own content, their own state (and ViewModel/notifier), a typed result. Screens should stay free of dialog flags and hoisted sheet state.
Camouflage is a peer of Material, not a framework of its own. So, like Material, it plugs overlays into each platform's own navigator instead of inventing an overlay system (ADR-19):
| Platform | The navigator | How an overlay is opened | Why it feels like a page |
|---|---|---|---|
| Flutter | Navigator (and GoRouter on top of it) | show: showCamoDialog / showCamoBottomSheet push a Camouflage route and return Future<T?>, exactly like material_ui's showDialog | a route has its own lifecycle, so autoDispose providers inside it dispose when it closes |
| Compose | Navigation 3 back stack | register the dialog/sheet as a route (an entry with Camouflage scene metadata), then add its key to the back stack | each entry gets its own ViewModelStore (Nav3 entry decorator), is restorable, and is deep-linkable |
Both platforms share the same content components (CamoDialog, CamoBottomSheet), the same frames (scrim, focus trap, drag, keyboard avoidance, drawn by the skin), and the same dismissal rules.
2. Shape
Layering
| Layer | Lives in | Contains |
|---|---|---|
| Frames | core | CamoDialogFrame, CamoSheetFrame: the modal behavior around content. Usable standalone (declaratively, like Material's ModalBottomSheet) in apps with no navigator integration. |
| Content components | core | CamoDialog, CamoBottomSheet |
| Navigator integration | Flutter: core (routes are part of Flutter's widgets); Compose: the camouflage-navigation3 artifact | Flutter: CamoDialogRoute, CamoSheetRoute, showCamoDialog, showCamoBottomSheet. Compose: CamoDialogSceneStrategy, CamoBottomSheetSceneStrategy, metadata helpers, result passing. |
Core never contains a router. Compose's Navigation 3 integration is an official, separately published artifact, the same way Material ships adaptive-navigation3.
3. API
3.0 Stacking: what's visible behind an overlay
An overlay is stacked on top of the current page, and the page stays visible behind the scrim, dimmed but not hidden. That's the same as Material's showModalBottomSheet.
┌──────────────────────────┐
│ Home page (still drawn) │ ← visible, dimmed, can't be tapped
│░░░░░░░░ scrim ░░░░░░░░░░░│ ← colors.scrim × scrimAlpha; tap = dismissal request
│┌────────────────────────┐│
││ Choose country ═══ ││ ← the overlay (top of the stack)
│└────────────────────────┘│
└──────────────────────────┘
Why: Camouflage's overlay routes/scenes are non-opaque. A normal page is opaque, so the navigator stops drawing what's below it. A non-opaque entry makes the navigator (Flutter Navigator, also under GoRouter) or the scene (Navigation 3) keep drawing the entries below.
| The page behind | Behavior |
|---|---|
| Rendering | still drawn, unchanged (scroll position, running animations) |
| Touch | blocked by the scrim, where a tap is a dismissal request |
| Keyboard focus | trapped in the overlay |
| Screen readers | the page behind is hidden. Only the overlay is announced. |
| State | kept, not rebuilt or disposed. Closing the overlay reveals it as it was. |
Several overlays (a dialog from a sheet): everything below stays visible. By default each overlay brings its own scrim, so layers get progressively darker (Material's default). The design team can switch to a single scrim with the component-theme option stackedScrim = TopOnly (dialog and sheet themes).
Only overlays are see-through. If a full page was pushed on top of Home, a sheet opened from it shows that page behind, not Home.
3.1 Frames (core, both platforms)
fun CamoDialogFrame(onDismissRequest: () -> Unit, dismissible: Boolean = true, content: Slot)
fun CamoSheetFrame(onDismissRequest: () -> Unit, dismissible: Boolean = true, dragToDismiss: Boolean = true,
content: Slot)
A frame is visible while it's in the tree. It draws the scrim and container area, traps focus, handles back/Esc/drag as dismissal requests (onDismissRequest), avoids the keyboard, and captures nothing: it's composed where the navigator places it.
3.2 Flutter: show
Future<T?> showCamoDialog<T>({required BuildContext context, required WidgetBuilder builder, bool dismissible = true});
Future<T?> showCamoBottomSheet<T>({required BuildContext context, required WidgetBuilder builder,
bool dismissible = true, bool dragToDismiss = true});
// inside: Navigator.pop(context, result) closes with a result; dismissal pops with null
The one-file pattern: the overlay widget owns a static Future<T?> show(BuildContext context).
Also as GoRouter routes, without modifying GoRouter: CamoSheetPage / CamoDialogPage are Page classes that create the same Camouflage routes, so a GoRoute can declare a sheet (pageBuilder: (_, __) => CamoSheetPage(child: …)). await context.push<T>('/…') then returns its result. Same transitions, same frame. See Overlays.
3.3 Compose: register as a route (camouflage-navigation3)
// 1. keys
@Serializable data object CountryPicker : NavKey
// 2. register the entry with Camouflage sheet metadata
entry<CountryPicker>(metadata = CamoBottomSheetSceneStrategy.bottomSheet()) {
CountryPickerSheet(onPick = { results.set(CountryPicker, it); backStack.removeLastOrNull() })
}
// 3. NavDisplay with Camouflage scene strategies
NavDisplay(backStack, sceneStrategy = CamoDialogSceneStrategy<NavKey>() then CamoBottomSheetSceneStrategy<NavKey>(), …)
// 4. open it
backStack.add(CountryPicker)
Results come back through CamoNavResults (following the Nav3 "returning results" recipe):
backStack.showForResult<T>(key, results): a suspend convenience that adds the key and awaits the result. Flutter-like, for normal flows.results.consume<T>(key) { … }: delivered once, even after process death. Use it where the result must survive the app being killed while the overlay is open.
3.4 Dismissal (both)
Scrim tap, back, Esc and drag are dismissal requests:
- Flutter pops the route with
null. - Compose calls
onDismissRequest, which the scene maps to "remove this entry from the back stack".
To veto (while saving, or to ask "discard changes?"), content uses PopScope (Flutter) or OnDismissRequest { … } (Compose), or the overlay is shown/registered with dismissible = false.
4. Build steps
- Frames in core (dialog, then sheet): scrim, focus trap, back/Esc, drag, IME insets, max size, dismissal requests, and screen-reader modality.
- The content components
CamoDialogandCamoBottomSheet(CamoDialog, CamoBottomSheet). - Flutter:
CamoDialogRoute,CamoSheetRoute(PopupRoutes using the frames), and the twoshowfunctions. - Compose: the
camouflage-navigation3artifact with the two scene strategies, metadata helpers,OnDismissRequestintegration, andCamoNavResults. - Samples: the same
CountryPickerSheet(own ViewModel/notifier) andDeleteItemDialogon both platforms. - Tests:
- results round-trip;
- dismissal returns
null/ removes the entry; - a veto keeps it open;
- the entry's ViewModel/notifier is disposed on close;
- stacked overlays close LIFO;
- a nested-provider theme is visible inside (Flutter: capture; Compose: the scene renders inside the provider tree).
Done when
- Flutter:
await CountryPickerSheet.show(context)returns a typed result. - Compose:
backStack.add(CountryPicker)shows a Camouflage-styled sheet whose ViewModel is cleared when it closes, and the result reaches the caller. - Both look identical for the same skin and theme.
5. Edge cases
| Case | Decided behavior |
|---|---|
| Process death | Compose: back-stack entries are restorable, so an open sheet comes back (with its saveable state). Flutter: routes pushed with show aren't restored by default (the same as material_ui's showDialog). Use restorable routes later if needed. |
| An app without Navigation 3 (Compose) | Use the frames directly, declaratively: if (showSheet) CamoSheetFrame(onDismissRequest = { showSheet = false }) { CamoBottomSheet(…) }, the same as Material's ModalBottomSheet. |
| Nested provider theme | Flutter: show captures the skin and theme at the call site (routes are built under the Navigator). Compose: the NavDisplay sits inside the app's provider tree, and a screen-local theme override must be applied inside the entry's content. Documented. |
| Deep links to a sheet | Compose: natural (it's a key). Flutter: a GoRouter page that opens showCamoBottomSheet, or a GoRouter pageBuilder returning a CamoSheetPage, a later addition. |
| Snackbars, menus, tooltips | Later overlay kinds, following the same per-platform integration. |
- Content can veto a dismissal on both platforms:
OnDismissRequest { … }(Compose) covers the scrim, back, Esc, a drag, the close button and the screen-reader dismiss action; Flutter routes send all of them throughmaybePop, so onePopScopecovers them. StackedScrimis shared by dialogs and sheets (Kotlin: moved to theoverlaypackage), and the stacking depth counts both kinds of frame.- Not built yet: returning focus to the opener on Compose, and GoRouter integration tests (the
Pageclasses are tested withNavigator.pages, without a go_router dependency).
Implementation
1. Why
In Compose, Camouflage dialogs and bottom sheets are Navigation 3 routes. You register an entry with Camouflage dialog or sheet metadata, then add its key to the back stack. That's how Material integrates with Navigation 3 too (scene strategies), and it gives each overlay a real page lifecycle for free:
- its own
ViewModelStore(from Nav3's ViewModel entry decorator), cleared when the entry is popped; - saveable state, restorable after process death;
- deep-link support, because an overlay is just a key.
Core provides the frames: the modal behavior, usable standalone like Material's ModalBottomSheet. The camouflage-navigation3 artifact turns them into scene strategies.
2. Shape
How a scene strategy works: NavDisplay asks each strategy, in order, whether it can show the top entries. CamoBottomSheetSceneStrategy claims the top entry if its metadata carries the Camouflage sheet marker. It returns an overlay scene: Navigation 3 keeps rendering the entries underneath (the previous page stays visible behind the scrim, and NavDisplay's page transition isn't applied to it), and the sheet entry is drawn inside CamoSheetFrame. When the frame requests dismissal, the scene's onBack removes the entry from the back stack.
Stacking follows the base rules (Overlays §3.0): the page behind stays visible, dimmed, blocked for touch, focus and screen readers, and its state is kept. Each overlay adds a scrim unless stackedScrim = TopOnly.
3. API
3.1 Frames (core)
@Composable public fun CamoDialogFrame(
onDismissRequest: () -> Unit, dismissible: Boolean = true, content: @Composable () -> Unit)
@Composable public fun CamoSheetFrame(
onDismissRequest: () -> Unit, dismissible: Boolean = true, dragToDismiss: Boolean = true,
content: @Composable () -> Unit)
/** Inside frame content: veto dismissal requests (scrim, back, Esc, drag). Return true to allow. */
@Composable public fun OnDismissRequest(onRequest: () -> Boolean)
The frames are drawn in-layer (a full-size Box over the content, not a platform Dialog window), so they:
- look identical on all four targets;
- need no Android window-dim workaround;
- let future Glass skins blur what's behind them.
Modality is implemented in the frame:
- the scrim consumes pointer input;
- focus moves in and is trapped (
FocusRequester+ afocusGroupwithfocusPropertiesexit handling); paneTitlesemantics are set;- a multiplatform back handler is registered (Compose Multiplatform's
BackHandler, or theNavigationEventHandlerAPI, whichever is stable at M6); - Esc is handled via
onKeyEvent.
Standalone use (no Navigation 3):
var showPicker by rememberSaveable { mutableStateOf(false) }
if (showPicker) CamoSheetFrame(onDismissRequest = { showPicker = false }) {
CamoBottomSheet(title = "Choose country") { CountryList(onPick = { pick(it); showPicker = false }) }
}
3.2 Navigation 3 integration (camouflage-navigation3)
public class CamoDialogSceneStrategy<T : Any> : SceneStrategy<T> {
public companion object { public fun dialog(dismissible: Boolean = true): Map<String, Any> }
}
public class CamoBottomSheetSceneStrategy<T : Any> : SceneStrategy<T> {
public companion object { public fun bottomSheet(dismissible: Boolean = true, dragToDismiss: Boolean = true): Map<String, Any> }
}
/** Result passing between an overlay entry and its caller (Nav3 "returning results" recipe). */
@Stable public class CamoNavResults {
public fun <T> set(key: Any, value: T)
@Composable public fun <T> consume(key: Any, onResult: (T) -> Unit) // delivered once, then cleared
}
@Composable public fun rememberCamoNavResults(): CamoNavResults // saveable
/** Flutter-like convenience: add the key and await its result (null if dismissed). */
public suspend fun <T> NavBackStack<NavKey>.showForResult(key: NavKey, results: CamoNavResults): T?
showForResult vs consume:
showForResult | consume | |
|---|---|---|
| Reads like | val c = backStack.showForResult<Country>(CountryPicker, results) | results.consume<Country>(CountryPicker) { … } |
| Use for | normal flows (most cases) | results that must arrive even after process death while the overlay was open (a long form in a sheet) |
| How | registers a one-shot listener in results, adds the key, suspends until set or until the entry is popped without a result (then null) | the same store, observed by a composable |
Both use the same store, so moving a flow from one to the other is a one-line change.
The full picture (the sample):
@Serializable data object CountryPicker : NavKey
@Composable
fun AppNav() {
val backStack = rememberNavBackStack(Home)
val results = rememberCamoNavResults()
NavDisplay(
backStack = backStack,
onBack = { backStack.removeLastOrNull() },
sceneStrategy = CamoDialogSceneStrategy<NavKey>() then CamoBottomSheetSceneStrategy<NavKey>(),
entryDecorators = listOf(rememberSaveableStateHolderNavEntryDecorator(), rememberViewModelStoreNavEntryDecorator()),
entryProvider = entryProvider {
entry<Home> { HomeScreen(onPickCountry = { backStack.add(CountryPicker) }, results = results) }
entry<CountryPicker>(metadata = CamoBottomSheetSceneStrategy.bottomSheet()) {
CountryPickerSheet(onPick = { results.set(CountryPicker, it); backStack.removeLastOrNull() })
}
},
)
}
// CountryPickerSheet.kt: the sheet in its own file, with its own ViewModel (scoped to the entry)
@Composable
fun CountryPickerSheet(onPick: (Country) -> Unit, vm: CountryPickerViewModel = viewModel { CountryPickerViewModel() }) {
val state by vm.state.collectAsStateWithLifecycle()
CamoBottomSheet(title = "Choose country") { CountryList(state.countries, onPick) }
}
// HomeScreen: receive the result (either way)
scope.launch { backStack.showForResult<Country>(CountryPicker, results)?.let { viewModel.onEvent(HomeEvent.CountryPicked(it)) } }
// or, for process-death safety:
results.consume<Country>(CountryPicker) { viewModel.onEvent(HomeEvent.CountryPicked(it)) }
With your conventions:
- the Home ViewModel emits an Effect (
OpenCountryPicker); - the screen maps it to
backStack.add(CountryPicker); - the result goes back into
onEvent.
4. Build steps
CamoDialogFrameandCamoSheetFramein core (modality, back/Esc, drag, IME insets,OnDismissRequest).- The
:camouflage-navigation3module (KMP, all four targets), depending on core and JetBrains' multiplatform Navigation 3 artifacts. Add it to the BOM. CamoDialogSceneStrategy/CamoBottomSheetSceneStrategy(overlay scenes that keep the previous content visible) and the metadata helpers.CamoNavResults(saveable, consume-once).- The sample
AppNavabove +DeleteItemdialog. - Tests (
runComposeUiTestwith a realNavDisplay):- adding the key shows the sheet over Home;
- the picked value reaches Home once;
- a scrim click / back pops the entry;
OnDismissRequest { false }keeps it;- the entry's ViewModel
onClearedruns after the pop; StateRestorationTesterrestores an open sheet.
Done when
-
backStack.add(CountryPicker)opens a Camouflage sheet on all four targets, and its result reaches Home. - The sheet's ViewModel is cleared when it closes. An open sheet survives state restoration.
5. Edge cases
| Case | Decided behavior |
|---|---|
| Kotlin vs Flutter | Compose registers routes, and Flutter uses show (ADR-19). The content components and frames are identical. |
| Nested provider theme | The entry renders inside NavDisplay, which sits inside the app's root provider. A screen-local override (a promo section) doesn't reach an entry. Apply it inside the entry's content: CamouflageProvider(theme = promo) { CamoDialog(…) }. |
| Two overlays stacked (a dialog from a sheet) | Two entries on top of each other. Both strategies claim the top entry only, and the previous overlay scene stays visible beneath. |
Rotation without configChanges | The back stack and entries are saveable, so the open sheet comes back after recreation (better than the standalone frame, whose rememberSaveable flag also restores). |
| Navigation 3 API changes | They're isolated in camouflage-navigation3. Core and apps using standalone frames aren't affected. |
| Navigation 3 isn't mandatory | The standalone frames work with any router, or none. camouflage-navigation3 only adds "register sheets and dialogs as routes". |
| Navigation 2 (NavHost + graph) | Registering sheets as graph destinations needs custom Navigators plus camoDialog { } / camoBottomSheet { } builders (the approach of Google's material-navigation for Material's bottomSheet { }). It's a possible later camouflage-navigation2 add-on if a Navigation 2 app needs it. Until then, use the standalone frames. |
| Other routers (Voyager, Decompose) | The standalone frames. |
- The frames are full-window
Popups, not in-layer boxes, so a standalone frame covers the window wherever it's composed. Navigation 3 scenes compose the same frames. CamoOverlayExitlets a navigator play a frame's exit animation before removing it; the scene strategies pass one and call it fromOverlayScene.onRemove, so an entry the app pops (a confirm button) still slides or fades out.- Navigation 3 1.2.0:
NavDisplay(sceneStrategies = listOf(CamoDialogSceneStrategy(), CamoBottomSheetSceneStrategy())).CamoNavResultskeys results by the entry key'stoString()(data objects and data classes) and saves them, so values must be saveable;showForResultworks on any snapshot-state back stack (MutableList<K>).