All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jsMain.styled.dynamicCss.DynamicCssDelegate.kt Maven / Gradle / Ivy

package styled.dynamicCss

import kotlinx.css.CssBuilder
import styled.StyleSheet
import kotlin.properties.ReadOnlyProperty
import kotlin.reflect.KProperty

/**
 * The only purpose of this class is to intercept the property's name which is used to construct the final CSS class name.
 * */
class DynamicCssDelegate internal constructor(
    private val sheet: StyleSheet,
    private val builder: CssBuilder.(T) -> Unit
) {
    /**
     * The single purpose of this class is to avoid creation of an anonymous class holding a reference to the parent.
     * */
    private class DynamicCssProviderProperty(
        private val provider: DynamicCssProvider
    ) : ReadOnlyProperty> {
        override fun getValue(thisRef: Any?, property: KProperty<*>): DynamicCssProvider = provider
    }

    /**
     * Intercepts the property name this delegate is assigned to and constructs the corresponding [DynamicCssProvider].
     * */
    operator fun provideDelegate(
        thisRef: Any?,
        providingProperty: KProperty<*>
    ): ReadOnlyProperty> =
        DynamicCssProviderProperty(DynamicCssProvider(sheet, providingProperty.name, builder))
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy