ursus
09/27/2025, 7:42 PMChrimaeon
09/27/2025, 8:40 PMursus
09/27/2025, 9:21 PMChrimaeon
09/27/2025, 9:25 PMDisposableEffect so you can unregistered when the composable leaves the composition.ursus
09/27/2025, 9:26 PMChrimaeon
09/27/2025, 9:29 PMvar distance by remember { mutableDoubleStateOf(0.0) }
DisposableEffect(window) {
val scrollListener =
EventListener {
distance = window.scrollY
}
window.addEventListener("scroll", scrollListener)
onDispose {
window.removeEventListener("scroll", scrollListener)
}
}ursus
09/27/2025, 9:29 PMChrimaeon
09/27/2025, 9:30 PMursus
09/27/2025, 9:30 PMChrimaeon
09/27/2025, 9:31 PMursus
09/27/2025, 9:33 PMChrimaeon
09/27/2025, 9:34 PMone doesnt usually need to wrap a 1st party api with DisposableEffect in order to observe some propertywell. thats the purpose of side effects to make ānon compose worldā work with compose.
ursus
09/27/2025, 9:34 PMscrollState.scrollY , so just a property being readursus
09/27/2025, 9:34 PMwindow.scrollYChrimaeon
09/27/2025, 9:36 PMursus
09/27/2025, 9:36 PMursus
09/27/2025, 9:38 PMChrimaeon
09/27/2025, 9:38 PMS.
09/27/2025, 9:44 PMursus
09/27/2025, 9:45 PM@Composable
fun rememberWindowScrollY(): State<Double> {
val scrollY = remember { mutableStateOf(window.scrollY) }
DisposableEffect(Unit) {
val listener: (org.w3c.dom.events.Event) -> Unit = {
scrollY.value = window.scrollY
}
window.addEventListener("scroll", listener)
onDispose { window.removeEventListener("scroll", listener) }
}
return scrollY
}
like this? i.e. just to hide the mutable state inside and return it?S.
09/27/2025, 9:47 PMursus
09/27/2025, 9:48 PMS.
09/27/2025, 9:48 PMursus
09/27/2025, 9:49 PMursus
09/27/2025, 9:50 PMS.
09/27/2025, 9:51 PMursus
09/27/2025, 9:53 PMS.
09/27/2025, 9:54 PMursus
09/27/2025, 9:54 PMS.
09/27/2025, 9:57 PMwindow.scrollY itself does not. it's always the exact viewport offsetS.
09/27/2025, 9:59 PMwindow.scrollY to check the current position. it's just not possible to observe it's value outside of loops or event listenersursus
09/27/2025, 9:59 PMS.
09/27/2025, 10:01 PMS.
09/27/2025, 10:03 PM