-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add opportunity to use custom prefixes in StyleSheet #3015
Changes from 3 commits
1a892d4
b32a136
505ef28
0aac3d0
bfb7cb1
727e47b
827d856
45a4334
cff79b5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,9 @@ class CSSRulesHolderState : CSSRulesHolder { | |
/** | ||
* Represents a collection of the css style rules. | ||
* StyleSheet needs to be mounted. | ||
* | ||
* @param prefix Will be used as prefix with current style. Pass `null` to use default value (classname of realization) | ||
* | ||
* @see [Style] | ||
* | ||
* Example: | ||
|
@@ -38,12 +41,21 @@ class CSSRulesHolderState : CSSRulesHolder { | |
* ``` | ||
*/ | ||
open class StyleSheet( | ||
prefix: String?, | ||
private val rulesHolder: CSSRulesHolder = CSSRulesHolderState(), | ||
val usePrefix: Boolean = true, | ||
) : StyleSheetBuilder, CSSRulesHolder by rulesHolder { | ||
private val boundClasses = mutableMapOf<String, CSSRuleDeclarationList>() | ||
val prefix = prefix ?: "${this::class.simpleName}-" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. does Also to preserve the compatibility, maybe it's worth to add a public I gues There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
it will likely go into 1.6 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like I resolved this point |
||
|
||
constructor( | ||
rulesHolder: CSSRulesHolder = CSSRulesHolderState(), | ||
usePrefix: Boolean = true | ||
) : this( | ||
if (usePrefix) null else "", | ||
rulesHolder | ||
) | ||
|
||
protected fun style(cssRule: CSSBuilder.() -> Unit) = CSSHolder(usePrefix, cssRule) | ||
protected fun style(cssRule: CSSBuilder.() -> Unit) = CSSHolder(prefix, cssRule) | ||
|
||
/** | ||
* Example: | ||
|
@@ -69,7 +81,7 @@ open class StyleSheet( | |
* } | ||
* ``` | ||
*/ | ||
protected fun keyframes(cssKeyframes: CSSKeyframesBuilder.() -> Unit) = CSSKeyframesHolder(usePrefix, cssKeyframes) | ||
protected fun keyframes(cssKeyframes: CSSKeyframesBuilder.() -> Unit) = CSSKeyframesHolder(prefix, cssKeyframes) | ||
|
||
companion object { | ||
private var counter = 0 | ||
|
@@ -88,13 +100,12 @@ open class StyleSheet( | |
} | ||
} | ||
|
||
protected class CSSHolder(private val usePrefix: Boolean, private val cssBuilder: CSSBuilder.() -> Unit) { | ||
protected class CSSHolder(private val prefix: String, private val cssBuilder: CSSBuilder.() -> Unit) { | ||
operator fun provideDelegate( | ||
sheet: StyleSheet, | ||
property: KProperty<*> | ||
): ReadOnlyProperty<Any?, String> { | ||
val sheetName = if (usePrefix) "${sheet::class.simpleName}-" else "" | ||
val className = "$sheetName${property.name}" | ||
val className = "$prefix${property.name}" | ||
val selector = object : CSSSelector() { | ||
override fun asString() = ".${className}" | ||
} | ||
|
@@ -110,15 +121,14 @@ open class StyleSheet( | |
* See [keyframes] | ||
*/ | ||
protected class CSSKeyframesHolder( | ||
private val usePrefix: Boolean, | ||
private val prefix: String, | ||
private val keyframesBuilder: CSSKeyframesBuilder.() -> Unit | ||
) { | ||
operator fun provideDelegate( | ||
sheet: StyleSheet, | ||
property: KProperty<*> | ||
): ReadOnlyProperty<Any?, CSSNamedKeyframes> { | ||
val sheetName = if (usePrefix) "${sheet::class.simpleName}-" else "" | ||
val keyframesName = "$sheetName${property.name}" | ||
val keyframesName = "$prefix${property.name}" | ||
val rule = buildKeyframes(keyframesName, keyframesBuilder) | ||
sheet.add(rule) | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it make sense in your opinion to name this constructor parameter something like
customNamePrefix
or justcustomPrefix
?So it's not confused with
protected val prefix: String
...Then it will be easier with this line:
val usePrefix: Boolean = prefix == null
// by looking at this line it's not clear right away whatprefix
is meant here (from constrcutor of the property)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
renamed to
customPrefix
:)