Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Compose HTML #163

Open
GabrielLasso opened this issue Oct 20, 2023 · 3 comments
Open

Support for Compose HTML #163

GabrielLasso opened this issue Oct 20, 2023 · 3 comments

Comments

@GabrielLasso
Copy link

It would be nice of we can use Precompose with Compose HTML.

You can use this template to try it out: https://github.com/JetBrains/compose-multiplatform-html-library-template

I changed the main.kt to:

class CounterViewModel: ViewModel() {
    val counter: MutableState<Int> = mutableStateOf(0)
}

fun main() {
    renderComposable(rootElementId = "root") {
        Body()
    }
}

@Composable
fun Body() {
    val viewModel: CounterViewModel = viewModel(CounterViewModel::class) {
        CounterViewModel()
    }
    Div {
        Text("Clicked: ${viewModel.counter.value}")
    }
    Button(
        attrs = {
            onClick { _ ->
                viewModel.counter.value++
            }
        }
    ) {
        Text("Click")
    }
}

And added this to build.gradle.kts:

                implementation("moe.tlaster:precompose:1.5.4")
                implementation("moe.tlaster:precompose-viewmodel:1.5.4")

But when I run I get "No StateHolder provided" error... I know that I should change the renderComposable call for something, but I didnt found anything for HTML in the documentation

@GabrielLasso
Copy link
Author

I changed the main function to


fun main() {
    renderComposable(rootElementId = "root") {
        val holder: PreComposeWindowHolder = remember { PreComposeWindowHolder() }
        CompositionLocalProvider(LocalLifecycleOwner provides holder,
                                 LocalStateHolder provides holder.stateHolder,
                                 LocalBackDispatcherOwner provides holder) {
            Body()
        }
    }
}

And it worked.

It would be nice to have a built in function to this

@GabrielLasso
Copy link
Author

navigation won't work, but at least view model works :)

@Tlaster
Copy link
Owner

Tlaster commented Oct 21, 2023

Currently PreCompose does not have something like hash router, I'm working on it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants