-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dbc14d4
commit 72202b8
Showing
2 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
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
66 changes: 66 additions & 0 deletions
66
...ompose/src/jsMain/kotlin/dev/inmo/micro_utils/coroutines/compose/StyleSheetsAggregator.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,66 @@ | ||
package dev.inmo.micro_utils.coroutines.compose | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.collectAsState | ||
import androidx.compose.runtime.remember | ||
import dev.inmo.micro_utils.coroutines.SpecialMutableStateFlow | ||
import kotlinx.coroutines.flow.StateFlow | ||
import kotlinx.coroutines.flow.asStateFlow | ||
import kotlinx.coroutines.flow.debounce | ||
import org.jetbrains.compose.web.css.CSSRulesHolder | ||
import org.jetbrains.compose.web.css.Style | ||
import org.jetbrains.compose.web.css.StyleSheet | ||
|
||
/** | ||
* Aggregator of Compose CSS StyleSheet. Allowing to add [StyleSheet] in it and draw it in one place without requiring | ||
* to add `Style(stylesheet)` on every compose function call | ||
*/ | ||
object StyleSheetsAggregator { | ||
private val _stylesFlow = SpecialMutableStateFlow<Set<CSSRulesHolder>>(emptySet()) | ||
val stylesFlow: StateFlow<Set<CSSRulesHolder>> = _stylesFlow.asStateFlow() | ||
|
||
@Composable | ||
fun draw() { | ||
_stylesFlow.debounce(13L).collectAsState(emptySet()).value.forEach { | ||
Style(it) | ||
} | ||
} | ||
|
||
/** | ||
* Adding [styleSheet] into the [Set] of included stylesheets. If you called [enableStyleSheetsAggregator], | ||
* new styles will be enabled in the document | ||
*/ | ||
fun addStyleSheet(styleSheet: CSSRulesHolder) { | ||
_stylesFlow.value += styleSheet | ||
} | ||
|
||
/** | ||
* Removed [styleSheet] into the [Set] of included stylesheets | ||
*/ | ||
fun removeStyleSheet(styleSheet: CSSRulesHolder) { | ||
_stylesFlow.value -= styleSheet | ||
} | ||
} | ||
|
||
/** | ||
* Drawing [StyleSheetsAggregator] in place. You may pass [Set] of [CSSRulesHolder]/[StyleSheet]s as preset of styles | ||
*/ | ||
@Composable | ||
fun enableStyleSheetsAggregator( | ||
stylesPreset: Set<CSSRulesHolder> = emptySet(), | ||
) { | ||
remember { | ||
stylesPreset.forEach { | ||
StyleSheetsAggregator.addStyleSheet(it) | ||
} | ||
} | ||
StyleSheetsAggregator.draw() | ||
} | ||
|
||
/** | ||
* Will include [this] [CSSRulesHolder]/[StyleSheet] in the [StyleSheetsAggregator] using its | ||
* [StyleSheetsAggregator.addStyleSheet] | ||
*/ | ||
fun CSSRulesHolder.includeInStyleSheetsAggregator() { | ||
StyleSheetsAggregator.addStyleSheet(this) | ||
} |