Skip to content

CssBuilder.AddClass Method

Ed Charbeneau edited this page Jan 16, 2020 · 8 revisions

AddClass(string value)

Adds a CSS Class to the builder with space separator.

public CssBuilder AddClass(string value)

Example

var ClassToRender = new CssBuilder("item-one")
                            .AddClass("item-two")
                            .Build();

// ClassToRender: "item-one item-two"

AddClass(string value, bool when = true)

Adds a conditional CSS Class to the builder with space separator.

public CssBuilder AddClass(string value, bool when = true)

Example

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"

AddClass(string value, Func<bool> when = null)

public CssBuilder AddClass(string value, Func<bool> when = null)

Example

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"

AddClass(Func<string> value, bool when = true)

Adds a conditional CSS Class to the builder with space separator.

public CssBuilder AddClass(Func<string> value, bool when = true)

Example

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"

AddClass(Func<string> value, Func<bool> when = null)

Adds a conditional CSS Class to the builder with space separator.

public CssBuilder AddClass(Func<string> value, Func<bool> when = null)

Example

// contribute?

AddClass(CssBuilder builder, bool when = true)

Adds a conditional nested CssBuilder to the builder with space separator.

public CssBuilder AddClass(CssBuilder builder, bool when = true)

Example

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"

AddClass(CssBuilder builder, Func<bool> when = null)

Adds a conditional nested CssBuilder to the builder with space separator.

public CssBuilder AddClass(CssBuilder builder, Func<bool> when = null)

Example

// contribute?