-
Notifications
You must be signed in to change notification settings - Fork 25
CssBuilder.AddClass Method
Adds a CSS Class to the builder with space separator.
public CssBuilder AddClass(string value)
var ClassToRender = new CssBuilder("item-one")
.AddClass("item-two")
.Build();
// ClassToRender: "item-one item-two"
Adds a conditional CSS Class to the builder with space separator.
public CssBuilder AddClass(string value, bool when = true)
var ClassToRender = CssBuilder.Default("item-one")
.AddClass("item-two", when: true)
.AddClass("item-three", when: false)
.AddClass("item-four")
.Build();
// ClassToRender: "item-one item-two item-four"
public CssBuilder AddClass(string value, Func<bool> when = null)
Func<bool> isRendered = () => false;
var ClassToRender = CssBuilder.Default("item-one")
.AddClass("item-two", when: true)
.AddClass("item-three", when: isRendered)
.AddClass("item-four")
.Build();
// ClassToRender: "item-one item-two item-four"
Adds a conditional CSS Class to the builder with space separator.
public CssBuilder AddClass(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 CssBuilder to easily chain something as complex as a dictionary lookup and ToString method call without throwing a key not found exception.
IReadOnlyDictionary<string, object> attributes = new Dictionary<string, object> { { "class", "my-custom-class-1" } };
var ClassToRender = CssBuilder.Default("item-one")
.AddClass(()=> attributes["class"].ToString(), when: attributes.ContainsKey("class"))
.Build();
// ClassToRender: "item-one my-custom-class-1"
Adds a conditional CSS Class to the builder with space separator.
public CssBuilder AddClass(Func<string> value, Func<bool> when = null)
// contribute?
Adds a conditional nested CssBuilder to the builder with space separator.
public CssBuilder AddClass(CssBuilder builder, bool when = true)
The CssBuilder can also accept other CssBuilder structs as parameters. This is helpful for scenarios where an entire tree of values needs to be conditional.
var ClassToRender = new CssBuilder("item-one")
.AddClass("item-two", when: false)
.AddClass(new CssBuilder("b-item-one")
.AddClass("b-item-two", false)
.AddClass("b-item-three"),
when: true)
.AddClass("item-four")
.AddClass("item-five", when: false)
.Build();
//ClassToRender: "item-one b-item-one b-item-three item-four"
Adds a conditional nested CssBuilder to the builder with space separator.
public CssBuilder AddClass(CssBuilder builder, Func<bool> when = null)
// contribute?