Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix theme customization issues #65

Merged
merged 2 commits into from
May 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Pure.Blazor.Components.AspNetCore;
public static class HostApplicationBuilderExtensions
{
public static IHostApplicationBuilder AddPureBlazorComponents(this IHostApplicationBuilder builder,
IPureTheme? theme = null)
PureTheme? theme = null)
{
// javascript
builder.Services.AddScoped<IElementUtils, ElementUtils>();
Expand All @@ -24,7 +24,7 @@ public static IHostApplicationBuilder AddPureBlazorComponents(this IHostApplicat
builder.Services.AddCascadingValue(sp =>
{
theme ??= new DefaultTheme();
var source = new CascadingValueSource<IPureTheme>(theme, isFixed: true);
var source = new CascadingValueSource<PureTheme>(theme, isFixed: true);
return source;
});

Expand Down
22 changes: 11 additions & 11 deletions src/Pure.Blazor.Components.Primitives/ComponentStyle.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
namespace Pure.Blazor.Components.Primitives;

public class ComponentStyle
public record ComponentStyle
{
private readonly IReadOnlyDictionary<Accent, string> accents;
private readonly IReadOnlyDictionary<PureVariant, Dictionary<Accent, string>> variants;
private readonly IReadOnlyDictionary<PureSize, string> sizes;
public readonly IReadOnlyDictionary<Accent, string> Accents;
public readonly IReadOnlyDictionary<PureVariant, Dictionary<Accent, string>> Variants;
public readonly IReadOnlyDictionary<PureSize, string> Sizes;

public ComponentStyle(string baseStyle,
IReadOnlyDictionary<Accent, string>? accents,
IReadOnlyDictionary<PureVariant, Dictionary<Accent, string>>? variants,
IReadOnlyDictionary<PureSize, string>? sizes)
{
this.accents = accents ?? new Dictionary<Accent, string>();
this.variants = variants ?? new Dictionary<PureVariant, Dictionary<Accent, string>>();
this.sizes = sizes ?? new Dictionary<PureSize, string>();
this.Accents = accents ?? new Dictionary<Accent, string>();
this.Variants = variants ?? new Dictionary<PureVariant, Dictionary<Accent, string>>();
this.Sizes = sizes ?? new Dictionary<PureSize, string>();
Base = baseStyle;
}

Expand All @@ -36,12 +36,12 @@ public ComponentStyle(string baseStyle,

public string Accent(Accent accent)
{
return accents.TryGetValue(accent, out var value) ? value : string.Empty;
return Accents.TryGetValue(accent, out var value) ? value : string.Empty;
}

public string Variant(PureVariant variant, Accent accent)
{
if (variants.TryGetValue(variant, out var value) && value.TryGetValue(accent, out var style))
if (Variants.TryGetValue(variant, out var value) && value.TryGetValue(accent, out var style))
{
return style;
}
Expand All @@ -51,6 +51,6 @@ public string Variant(PureVariant variant, Accent accent)

public string Size(PureSize size)
{
return sizes.TryGetValue(size, out var value) ? value : string.Empty;
return Sizes.TryGetValue(size, out var value) ? value : string.Empty;
}
}
}
19 changes: 0 additions & 19 deletions src/Pure.Blazor.Components.Primitives/IPureTheme.cs

This file was deleted.

2 changes: 1 addition & 1 deletion src/Pure.Blazor.Components.Primitives/PureComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ protected override void OnParametersSet()
/// The current theme styles
/// </summary>
[CascadingParameter]
public required IPureTheme PureTheme { get; set; }
public required PureTheme PureTheme { get; set; }

[Parameter] public RenderFragment? ChildContent { get; set; }

Expand Down
52 changes: 52 additions & 0 deletions src/Pure.Blazor.Components.Primitives/PureTheme.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
namespace Pure.Blazor.Components.Primitives;

public record PureTheme
{
public ButtonDefaults ButtonDefaults { get; set; } = new();
public IStylePrioritizer? StylePrioritizer { get; set; }
public Dictionary<string, ComponentStyle> Styles { get; set; } = new();

public ComponentStyle GetStyle(Type type)
{
return GetStyleByName(type.Name);
}

public ComponentStyle GetStyleByName(string name)
{
// TODO: decide if we want this to be an exceptional event
return Styles.GetValueOrDefault(name) ?? new ComponentStyle("", null, null, null);
}

/// <summary>
/// Merges the styles into the current theme, overwriting any existing styles.
/// </summary>
/// <param name="styles"></param>
public void Merge(Dictionary<string, ComponentStyle> styles)
{
Styles = Styles.MergeLeft(styles);
}
}

// https://stackoverflow.com/a/2679857/783284
internal static class DictionaryExtensions
{
// Works in C#3/VS2008:
// Returns a new dictionary of this ... others merged leftward.
// Keeps the type of 'this', which must be default-instantiable.
// Example:
// result = map.MergeLeft(other1, other2, ...)
public static T MergeLeft<T,K,V>(this T me, params IDictionary<K,V>[] others)
where T : IDictionary<K,V>, new()
{
T newMap = new T();
foreach (IDictionary<K,V> src in
(new List<IDictionary<K,V>> { me }).Concat(others)) {
// ^-- echk. Not quite there type-system.
foreach (KeyValuePair<K,V> p in src) {
newMap[p.Key] = p.Value;
}
}
return newMap;
}

}
77 changes: 38 additions & 39 deletions src/Pure.Blazor.Components/Common/DefaultTheme.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,50 +6,49 @@

namespace Pure.Blazor.Components.Common;

public class DefaultTheme : IPureTheme
public record DefaultTheme : PureTheme
{
public ButtonDefaults ButtonDefaults { get; set; } = new()
public DefaultTheme()
{
PressEffect = Effect.InsetShadow,
HoverEffect = Effect.Unset
};

public IStylePrioritizer StylePrioritizer { get; set; } = new StylePrioritizer();
public Dictionary<string, ComponentStyle> Styles { get; set; } = new()
{
{
nameof(PureButton),
new ComponentStyle(ButtonStyles.Base, null, ButtonStyles.Variants, ButtonStyles.Sizes)
},
ButtonDefaults.PressEffect = Effect.InsetShadow;
ButtonDefaults.HoverEffect = Effect.Unset;
StylePrioritizer = new StylePrioritizer();
Styles = new Dictionary<string, ComponentStyle>
{
nameof(PureIconButton),
new ComponentStyle(ButtonStyles.Base, null, ButtonStyles.Variants, ButtonStyles.Sizes)
},
{
nameof(PureDropdown),
new ComponentStyle(DropdownStyles.Base, null, null, DropdownStyles.Sizes)
{
InnerContainer = new ComponentStyle(DropdownStyles.Container.Base, null, null, null)
}
},
{
nameof(PureDropdownItem),
new ComponentStyle(DropdownStyles.MenuItem.Base, DropdownStyles.MenuItem.Accents, null,
DropdownStyles.MenuItem.Sizes)
},
{ nameof(PureBanner), new ComponentStyle(BannerStyles.Base, null, BannerStyles.Variants, null) },
{ nameof(PureLink), new ComponentStyle(LinkStyles.Base, null, null, null) },
{ nameof(PureBadge), new ComponentStyle("", null, BadgeStyles.Variants, null) },
{ nameof(PureAlert), new ComponentStyle(AlertStyles.Base, AlertStyles.Accents, null, null) },
{
nameof(PureColorIndicator),
new ComponentStyle("", null, null, null)
nameof(PureButton),
new ComponentStyle(ButtonStyles.Base, null, ButtonStyles.Variants, ButtonStyles.Sizes)
},
{
nameof(PureIconButton),
new ComponentStyle(ButtonStyles.Base, null, ButtonStyles.Variants, ButtonStyles.Sizes)
},
{
nameof(PureDropdown),
new ComponentStyle(DropdownStyles.Base, null, null, DropdownStyles.Sizes)
{
InnerContainer = new ComponentStyle(DropdownStyles.Container.Base, null, null, null)
}
},
{
nameof(PureDropdownItem),
new ComponentStyle(DropdownStyles.MenuItem.Base, DropdownStyles.MenuItem.Accents, null,
DropdownStyles.MenuItem.Sizes)
},
{ nameof(PureBanner), new ComponentStyle(BannerStyles.Base, null, BannerStyles.Variants, null) },
{ nameof(PureLink), new ComponentStyle(LinkStyles.Base, null, null, null) },
{ nameof(PureBadge), new ComponentStyle("", null, BadgeStyles.Variants, null) },
{ nameof(PureAlert), new ComponentStyle(AlertStyles.Base, AlertStyles.Accents, null, null) },
{
InnerContainer = new ComponentStyle("", IndicatorStyles.Background, null, null)
nameof(PureColorIndicator),
new ComponentStyle("", null, null, null)
{
OuterContainer = new ComponentStyle("", IndicatorStyles.Foreground, null, null)
InnerContainer = new ComponentStyle("", IndicatorStyles.Background, null, null)
{
OuterContainer = new ComponentStyle("", IndicatorStyles.Foreground, null, null)
}
}
}
},
};
},
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Pure.Blazor.Components;
public static class WebAssemblyHostBuilderExtensions
{
public static WebAssemblyHostBuilder AddPureBlazorComponents(this WebAssemblyHostBuilder builder,
IPureTheme? theme = null)
PureTheme? theme = null)
{
// javascript
builder.Services.AddSingleton<IElementUtils, ElementUtils>();
Expand All @@ -23,7 +23,7 @@ public static WebAssemblyHostBuilder AddPureBlazorComponents(this WebAssemblyHos
builder.Services.AddCascadingValue(sp =>
{
theme ??= new DefaultTheme();
var source = new CascadingValueSource<IPureTheme>(theme, isFixed: true);
var source = new CascadingValueSource<PureTheme>(theme, isFixed: true);
return source;
});
return builder;
Expand Down
Loading