The StyleInjector provides dynamic CSS injection for pseudo-selectors and media queries that can't be applied as inline styles. It generates scoped CSS rules and manages their lifecycle automatically.
Platforms: JS, WASM Package: code.yousef.summon.runtime Since: 0.4.8.4
StyleInjector)object StyleInjector {
fun injectPseudoSelectorStyles(
element: Element,
pseudoSelector: String,
styles: Map<String, String>
): String
fun injectMediaQueryStyles(
element: Element,
mediaQuery: String,
styles: Map<String, String>
): String
fun cleanupElementStyles(element: Element)
}
StyleInjectorWasm)object StyleInjectorWasm {
fun injectPseudoSelectorStyles(
elementId: String,
pseudoSelector: String,
styles: Map<String, String>
): String
fun injectMediaQueryStyles(
elementId: String,
mediaQuery: String,
styles: Map<String, String>
): String
fun cleanupElementStyles(elementId: String)
}
Injects CSS rules for pseudo-selectors like :hover, :focus, :active, etc.
JS Signature:
fun injectPseudoSelectorStyles(
element: Element,
pseudoSelector: String,
styles: Map<String, String>
): String
Parameters:
element - The DOM element to apply styles topseudoSelector - The pseudo-selector (e.g., :hover, :focus)styles - Map of CSS property names to valuesReturns: The generated unique class name
Example:
val element = document.getElementById("myButton")
StyleInjector.injectPseudoSelectorStyles(
element = element,
pseudoSelector = ":hover",
styles = mapOf(
"background-color" to "#0056b3",
"transform" to "scale(1.05)"
)
)
// Generates CSS like:
// .pseudo-hover-abc123:hover {
// background-color: #0056b3;
// transform: scale(1.05);
// }
Injects CSS rules with media queries for responsive design.
JS Signature:
fun injectMediaQueryStyles(
element: Element,
mediaQuery: String,
styles: Map<String, String>
): String
Parameters:
element - The DOM element to apply styles tomediaQuery - The media query condition (e.g., (min-width: 768px))styles - Map of CSS property names to valuesReturns: The generated unique class name
Example:
val element = document.getElementById("myBox")
StyleInjector.injectMediaQueryStyles(
element = element,
mediaQuery = "(min-width: 768px)",
styles = mapOf(
"padding" to "24px",
"font-size" to "18px"
)
)
// Generates CSS like:
// @media (min-width: 768px) {
// .media-abc123 {
// padding: 24px;
// font-size: 18px;
// }
// }
Removes all injected styles associated with an element.
JS Signature:
fun cleanupElementStyles(element: Element)
Parameters:
element - The DOM element to clean upExample:
val element = document.getElementById("myElement")
StyleInjector.cleanupElementStyles(element)
// Removes all <style> elements created for this element
The StyleInjector is automatically used by the PlatformRenderer when you use pseudo-selector or media query modifiers:
// This automatically uses StyleInjector
Box(modifier = Modifier()
.backgroundColor("#007bff")
.hover(Modifier().backgroundColor("#0056b3"))
.mediaQuery(MediaQuery.MinWidth(768)) {
padding("24px")
}
)
pseudo-hover-abc123<style> elements in <head>/* For pseudo-selector */
.pseudo-hover-abc123:hover {
background-color: #0056b3;
transform: scale(1.05);
}
/* For media query */
@media (min-width: 768px) {
.media-xyz789 {
padding: 24px;
font-size: 18px;
}
}
The following pseudo-selectors are automatically handled:
:hover - Mouse hover state:focus - Keyboard/mouse focus:active - Element being pressed/clicked:focus-within - Contains focused descendant:first-child - First child of parent:last-child - Last child of parent:nth-child() - Nth child with pattern:only-child - Only child of parent:visited - Visited links:disabled - Disabled form elements:checked - Checked checkboxes/radios