-
Notifications
You must be signed in to change notification settings - Fork 25
StyleBuilder.AddStyle Method
Adds an in-line style to the builder with space separator and closing semicolon.
public StyleBuilder AddStyle(string prop, string value)
var StyleToRender = new StyleBuilder("color", "blue")
.AddStyle("padding", "35px")
.Build();
// StyleToRender: "color:blue;padding:35px;"
Adds a conditional in-line style to the builder with space separator and closing semicolon.
public StyleBuilder AddStyle(string prop, string value, bool when = true)
bool isOnTop = false;
var StyleToRender = StyleBuilder.Default("color", "blue")
.AddStyle("padding", "35px", when: true)
.AddStyle("z-index", "999", when: isOnTop)
.AddStyle("z-index", "-1", when: !isOnTop)
.Build();
// StyleToRender: "color:blue;padding:35px;z-index:-1;"
public StyleBuilder AddStyle(string prop, string value, Func<bool> when = null)
Func<bool> hasPadding = () => true;
bool isOnTop = false;
var StyleToRender = StyleBuilder.Default("color", "blue")
.AddStyle("padding", "35px", when: hasPadding)
.AddStyle("z-index", "999", when: isOnTop)
.AddStyle("z-index", "-1", when: !isOnTop)
.Build();
// StyleToRender: "color:blue;padding:35px;z-index:-1;"
Adds a conditional in-line style to the builder with space separator and closing semicolon.
public StyleBuilder AddStyle(string prop, Func<string> value, bool when = true)
Using a Func<string>
as a parameter ensures that the method is called only when a condition is met. This allows StyleBuilder to easily chain something as complex as a dictionary lookup and ToString method call without throwing a key not found exception.
var hasBorder = true;
var isOnTop = false;
var top = 2;
var bottom = 10;
var left = 4;
var right = 20;
var StyleToRender = new StyleBuilder("background-color", "DodgerBlue")
.AddStyle("border-width", $"{top}px {right}px {bottom}px {left}px", when: hasBorder)
.AddStyle("z-index", "999", when: isOnTop)
.AddStyle("z-index", "-1", when: !isOnTop)
.AddStyle("padding", "35px")
.Build();
IReadOnlyDictionary<string, object> attributes = new Dictionary<string, object> { { "style", StyleToRender } };
var StyleToRender = new StyleBuilder()
.AddClass(
()=> attributes["class"].ToString(),
when: attributes.ContainsKey("class")
);
//StyleToRender = "background-color:DodgerBlue;border-width:2px 20px 10px 4px;z-index:-1;padding:35px;"
Adds a conditional in-line style to the builder with space separator and closing semicolon.
public StyleBuilder AddStyle(string prop, Func<string> value, Func<bool> when = null)
// contribute?
Adds a conditional nested StyleBuilder to the builder with space separator.
public StyleBuilder AddStyle(StyleBuilder builder, bool when = true)
The StyleBuilder can also accept other StyleBuilder structs as parameters. This is helpful for scenarios where an entire tree of values needs to be conditional.
// contribute?
Adds a conditional nested StyleBuilder to the builder with space separator.
public StyleBuilder AddStyle(StyleBuilder builder, Func<bool> when = null)
// contribute?