Skip to content

Commit

Permalink
Set Silk variables on the DOM root instead of the "root" div
Browse files Browse the repository at this point in the history
The benefit of this change is that the colorScheme CSS style now applies
to the entire site, which is important as otherwise the site's scrollbars
don't pick up the color.
  • Loading branch information
bitspittle committed Dec 16, 2024
1 parent f50f4ab commit bc737e4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,13 @@ import androidx.compose.runtime.*
import com.varabyte.kobweb.compose.KobwebComposeStyles
import com.varabyte.kobweb.core.KobwebApp
import com.varabyte.kobweb.silk.init.SilkWidgetVariables
import com.varabyte.kobweb.silk.theme.colors.ColorMode
import kotlinx.browser.document

@Composable
fun SilkApp(content: @Composable () -> Unit) {
KobwebApp {
KobwebComposeStyles()
SilkFoundationStyles()
SilkWidgetVariables()

// Create a meta element to communicate the current theme color of the overall site to the browser
// This affects UI like scrollbars and form controls
run {
val colorMode = ColorMode.current
DisposableEffect(colorMode) {
val colorSchemeElement = document.createElement("meta").apply {
setAttribute("name", "color-scheme")
setAttribute("content", colorMode.name.lowercase())
}.also { document.head!!.appendChild(it) }

onDispose {
colorSchemeElement.remove()
}
}
}

content()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -372,18 +372,29 @@ val SilkColorsStyle = CssStyle.base {
/**
* Set all CSS variables needed by the Silk library to work.
*
* @param provideRootElement An element which must live at a point in the DOM tree above any Silk widgets. This method
* @param provideElement An element which must live at a point in the DOM tree above any Silk widgets. This method
* will be called inside a `remember` block, meaning it will only be triggered once per composition.
*/
@Composable
fun SilkWidgetVariables(provideRootElement: () -> HTMLElement) {
val rootElement = remember { provideRootElement() }
fun SilkWidgetVariables(provideElement: () -> HTMLElement) {
val rootElement = remember { provideElement() }
SilkWidgetVariables(rootElement)
}

/**
* Set all Silk variables on the DOM root.
*/
@Composable
fun SilkWidgetVariables() {
SilkWidgetVariables { document.documentElement as HTMLElement }
}

/**
* Set all Silk variables on an element with the target ID name.
*/
@Composable
fun SilkWidgetVariables(rootElementId: String = "root") {
SilkWidgetVariables { document.getElementById(rootElementId) as HTMLElement }
fun SilkWidgetVariables(elementId: String) {
SilkWidgetVariables { document.getElementById(elementId) as HTMLElement }
}

@Deprecated("Use `SilkWidgetVariables` instead, as it is more Compose-idiomatic.",
Expand All @@ -395,8 +406,8 @@ fun Document.setSilkWidgetVariables(rootElementId: String = "root") {
}

@Composable
fun SilkWidgetVariables(rootElement: HTMLElement) {
rootElement.setSilkWidgetVariables(ColorMode.current)
fun SilkWidgetVariables(element: HTMLElement) {
element.setSilkWidgetVariables(ColorMode.current)
}

@Deprecated("Use `SilkWidgetVariables` instead, as it is more Compose-idiomatic.",
Expand Down

0 comments on commit bc737e4

Please sign in to comment.