-
-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This ensures that Silk sites will update the browser UI affordances that surround the site when the theme changes. Fixes #627
- Loading branch information
1 parent
b59e923
commit 56dbdfe
Showing
3 changed files
with
60 additions
and
0 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
...d/silk-foundation/src/jsMain/kotlin/com/varabyte/kobweb/silk/theme/meta/MetaThemeColor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.varabyte.kobweb.silk.theme.meta | ||
|
||
import androidx.compose.runtime.* | ||
import com.varabyte.kobweb.silk.theme.colors.ColorMode | ||
import kotlinx.browser.document | ||
import org.jetbrains.compose.web.css.* | ||
|
||
@Composable | ||
fun MetaThemeColor(color: CSSColorValue) = MetaThemeColor(color, color) | ||
|
||
@Composable | ||
fun MetaThemeColor(light: CSSColorValue, dark: CSSColorValue) { | ||
run { | ||
// Add the following entries to the <head> element | ||
// | ||
// <meta name="theme-color" media="(prefers-color-scheme: light)" content="..."> | ||
// <meta name="theme-color" media="(prefers-color-scheme: dark)" content="..."> | ||
// <meta name="color-scheme" content="light|dark"> | ||
// | ||
// The color-scheme value will be updated dynamically based on | ||
LaunchedEffect(Unit) { | ||
val themeColorElements = document.querySelectorAll("""meta[name="theme-color"]""") | ||
if (themeColorElements.length == 0) { | ||
ColorMode.entries.toList().forEach { colorMode -> | ||
document.createElement("meta").apply { | ||
setAttribute("name", "theme-color") | ||
setAttribute("media", "(prefers-color-scheme: ${colorMode.name.lowercase()})") | ||
setAttribute("content", (if (colorMode.isLight) light else dark).toString()) | ||
}.also { document.head!!.appendChild(it) } | ||
} | ||
} | ||
} | ||
|
||
val colorMode = ColorMode.current | ||
LaunchedEffect(colorMode) { | ||
var colorSchemeElement = document.querySelector("""meta[name="color-scheme"]""") | ||
if (colorSchemeElement == null) { | ||
colorSchemeElement = document.createElement("meta").apply { | ||
setAttribute("name", "color-scheme") | ||
}.also { document.head!!.appendChild(it) } | ||
} | ||
colorSchemeElement.setAttribute("content", colorMode.name.lowercase()) | ||
} | ||
} | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
...widgets/src/jsMain/kotlin/com/varabyte/kobweb/silk/theme/colors/palette/MetaThemeColor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.varabyte.kobweb.silk.theme.meta | ||
|
||
import androidx.compose.runtime.* | ||
import com.varabyte.kobweb.silk.theme.colors.ColorMode | ||
import com.varabyte.kobweb.silk.theme.colors.palette.background | ||
import com.varabyte.kobweb.silk.theme.colors.palette.toPalette | ||
|
||
@Composable | ||
fun MetaThemeColor() { | ||
MetaThemeColor(ColorMode.LIGHT.toPalette().background, ColorMode.DARK.toPalette().background) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters