Last active
February 19, 2019 01:54
-
-
Save EdCharbeneau/58483c84c58a3cd54d297e6b9ba6ac83 to your computer and use it in GitHub Desktop.
Css Builder Extensions
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
<li aria-selected=@IsActive role="tab" class="@ClassToRender"> | |
... | |
</li> | |
@functions { | |
... | |
protected bool IsActive => ContainerTabSet.ActiveTab == this; | |
[Parameter] protected bool Disabled { get; set; } | |
[Parameter] protected string Class { get; set; } | |
protected string ClassToRender { get; set; } | |
protected override void OnParametersSet() | |
{ | |
ClassToRender = new CssBuilder(Class) // User supplied CSS | |
.AddClass("tab-item") // base CSS | |
.AddClass("tab-state-active tab-tab-on-top", IsActive) | |
.AddClass("tab-state-default", !IsActive) | |
.AddClass("tab-state-disabled", Disabled) | |
.Build(); | |
} | |
} |
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
public class CssBuilder | |
{ | |
private readonly StringBuilder sb; | |
public CssBuilder() => sb = new StringBuilder(); | |
public CssBuilder(string value) => sb = new StringBuilder(value); | |
public CssBuilder AddValue(string value) | |
{ | |
sb.Append(value); | |
return this; | |
} | |
public CssBuilder AddClass(string value) => AddValue(" ").AddValue(value); | |
public CssBuilder AddClass(string value, bool when = true) => condition ? this.AddClass(value) : this; | |
public CssBuilder AddClass(string value, Func<bool> when = null) => this.AddClass(value, when()); | |
public string Build() => ToString(); | |
public override string ToString() => sb.ToString(); | |
} |
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
static class CssExtensions | |
{ | |
public static StringBuilder AppendCss(this StringBuilder sb, string value) => sb.Append(" ").Append(value); | |
public static StringBuilder AppendCssWhen(this StringBuilder sb, string value, bool condition) => condition ? sb.AppendCss(value) : sb; | |
public static StringBuilder AppendCssWhen(this StringBuilder sb, string value, Func<bool> condition) => condition() ? sb.AppendCss(value) : sb; | |
public static string ToCssString(this StringBuilder sb) => sb.ToString().Trim(); | |
} | |
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
<li aria-selected=@IsActive role="tab" class="@ClassToRender"> | |
... | |
</li> | |
@functions { | |
... | |
protected bool IsActive => ContainerTabSet.ActiveTab == this; | |
[Parameter] protected bool Disabled { get; set; } | |
[Parameter] protected string Class { get; set; } | |
protected string ClassToRender { get; set; } | |
protected override void OnParametersSet() | |
{ | |
ClassToRender = new StringBuilder(Class) // User supplied CSS | |
.AppendCss("tab-item") // Base CSS | |
.AppendCssWhen("tab-state--active tab-on-top", IsActive) | |
.AppendCssWhen("tab-state--default", !IsActive) | |
.AppendCssWhen("tab-state--disabled", Disabled) | |
.ToCssString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment