Skip to content

StyleBuilder.AddStyle Method

Ed Charbeneau edited this page Jan 16, 2020 · 1 revision

AddStyle(string prop, string value)

Adds an in-line style to the builder with space separator and closing semicolon.

public StyleBuilder AddStyle(string prop, string value)

Example

var StyleToRender = new StyleBuilder("color", "blue")
                            .AddStyle("padding", "35px")
                            .Build();

// StyleToRender: "color:blue;padding:35px;"

AddStyle(string value, bool when = true)

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)

Example

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;"

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

public StyleBuilder AddStyle(string prop, string value, Func<bool> when = null)

Example

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;"

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

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)

Example

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;"

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

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)

Example

// contribute?

AddStyle(StyleBuilder builder, bool when = true)

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

public StyleBuilder AddStyle(StyleBuilder builder, bool when = true)

Example

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?

AddStyle(StyleBuilder builder, Func<bool> when = null)

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

public StyleBuilder AddStyle(StyleBuilder builder, Func<bool> when = null)

Example

// contribute?