Skip to content

Instantly share code, notes, and snippets.

@hongbeomi
Created June 13, 2021 05:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hongbeomi/952d481c63aac498f5cd74b952c922a3 to your computer and use it in GitHub Desktop.
Save hongbeomi/952d481c63aac498f5cd74b952c922a3 to your computer and use it in GitHub Desktop.
@Composable
fun Dialog(
onDismissRequest: () -> Unit,
properties: DialogProperties = DialogProperties(),
content: @Composable () -> Unit
) {
val view = LocalView.current
val density = LocalDensity.current
val layoutDirection = LocalLayoutDirection.current
val composition = rememberCompositionContext()
val currentContent by rememberUpdatedState(content)
val dialogId = rememberSaveable { UUID.randomUUID() }
val dialog = remember(view, density) {
DialogWrapper(
onDismissRequest,
properties,
view,
layoutDirection,
density,
dialogId
).apply {
setContent(composition) {
// TODO(b/159900354): draw a scrim and add margins around the Compose Dialog, and
// consume clicks so they can't pass through to the underlying UI
DialogLayout(
Modifier.semantics { dialog() },
) {
currentContent()
}
}
}
}
DisposableEffect(dialog) {
dialog.show()
onDispose {
dialog.dismiss()
dialog.disposeComposition()
}
}
SideEffect {
dialog.updateParameters(
onDismissRequest = onDismissRequest,
properties = properties,
layoutDirection = layoutDirection
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment