Skip to content

Commit

Permalink
feat: support for generating an array of CSS classes for block, eleme…
Browse files Browse the repository at this point in the history
…nt and modifier (#7)

* 🆕 feat: Add CssProvider

* update

* remove debug code

* refacotr a name of method

* update

* udpate version to 1.2.0
  • Loading branch information
capdiem authored Feb 21, 2024
1 parent 0775422 commit f0106ff
Show file tree
Hide file tree
Showing 13 changed files with 221 additions and 103 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ obj/
/packages/
riderModule.iml
/_ReSharper.Caches/
.idea/
.idea/
.vs/
12 changes: 12 additions & 0 deletions BemIt.Tests/BlockTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@ public void Extend()
Assert.Equal("m-list-item-title", _block.Extend("title").Build());
}

[Fact]
public void Extend_element()
{
Assert.Equal("m-list-item-title__intro", _block.Extend("title").Element("intro").Build());
}

[Fact]
public void Extend_element_modifier()
{
Assert.Equal("m-list-item-title__intro m-list-item-title__intro--hoverable", _block.Extend("title").Element("intro").Modifier("hoverable").Build());
}

[Fact]
public void Block_modifier_empty_ctor()
{
Expand Down
13 changes: 11 additions & 2 deletions BemIt.sln
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@

Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BemIt", "BemIt\BemIt.csproj", "{DD0FDF41-698B-464E-829E-13F0AD00809E}"
# Visual Studio Version 17
VisualStudioVersion = 17.9.34310.174
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BemIt", "BemIt\BemIt.csproj", "{DD0FDF41-698B-464E-829E-13F0AD00809E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BemIt.Tests", "BemIt.Tests\BemIt.Tests.csproj", "{F686E4F1-1AA0-4473-B7FB-F3654B7377E1}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BemIt.Tests", "BemIt.Tests\BemIt.Tests.csproj", "{F686E4F1-1AA0-4473-B7FB-F3654B7377E1}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -19,4 +22,10 @@ Global
{F686E4F1-1AA0-4473-B7FB-F3654B7377E1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F686E4F1-1AA0-4473-B7FB-F3654B7377E1}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {0D42B177-36B3-4337-856C-0C54B0B452E0}
EndGlobalSection
EndGlobal
36 changes: 20 additions & 16 deletions BemIt.sln.DotSettings.user
Original file line number Diff line number Diff line change
@@ -1,38 +1,42 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=3938f614_002D8c5b_002D4d0c_002D9317_002Dcc628d53109f/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" Name="Block" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;&#xD;
&lt;TestAncestor&gt;&#xD;
&lt;TestId&gt;xUnit::F686E4F1-1AA0-4473-B7FB-F3654B7377E1::net6.0::BemIt.Tests.UnitTest1.Block&lt;/TestId&gt;&#xD;
&lt;TestId&gt;F686E4F1-1AA0-4473-B7FB-F3654B7377E1::net6.0::xUnit::BemIt.Tests.UnitTest1.Block&lt;/TestId&gt;&#xD;
&lt;/TestAncestor&gt;&#xD;
&lt;/SessionState&gt;</s:String>
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=3a528811_002D1ad3_002D4b36_002Dbd6b_002D4761f88f961e/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" Name="All tests from &amp;lt;BemIt.Tests&amp;gt; #2" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;&#xD;
&lt;Project Location="F:\code\BemIt\BemIt.Tests" Presentation="&amp;lt;BemIt.Tests&amp;gt;" /&gt;&#xD;
&lt;/SessionState&gt;</s:String>
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=5b198ea2_002D635b_002D46f0_002Dbea1_002D7227609c93d5/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" IsActive="True" Name="Block_modifier_with_true_condition #2" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;&#xD;
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=5b198ea2_002D635b_002D46f0_002Dbea1_002D7227609c93d5/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" Name="Block_modifier_with_true_condition #2" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;&#xD;
&lt;Or&gt;&#xD;
&lt;TestAncestor&gt;&#xD;
&lt;TestId&gt;xUnit::F686E4F1-1AA0-4473-B7FB-F3654B7377E1::net6.0::BemIt.Tests.BlockTests.Block_modifier_with_true_condition&lt;/TestId&gt;&#xD;
&lt;TestId&gt;xUnit::F686E4F1-1AA0-4473-B7FB-F3654B7377E1::net6.0::BemIt.Tests.ElementTests.Element_modifier&lt;/TestId&gt;&#xD;
&lt;TestId&gt;xUnit::F686E4F1-1AA0-4473-B7FB-F3654B7377E1::net6.0::BemIt.Tests.BlockTests.ChildBlock&lt;/TestId&gt;&#xD;
&lt;TestId&gt;xUnit::F686E4F1-1AA0-4473-B7FB-F3654B7377E1::net6.0::BemIt.Tests.ElementTests&lt;/TestId&gt;&#xD;
&lt;TestId&gt;xUnit::F686E4F1-1AA0-4473-B7FB-F3654B7377E1::net6.0::BemIt.Tests.ModifierTests.Modifier_with_true_condition&lt;/TestId&gt;&#xD;
&lt;TestId&gt;xUnit::F686E4F1-1AA0-4473-B7FB-F3654B7377E1::net6.0::BemIt.Tests.ModifierTests.Modifier_with_two_mods&lt;/TestId&gt;&#xD;
&lt;TestId&gt;xUnit::F686E4F1-1AA0-4473-B7FB-F3654B7377E1::net6.0::BemIt.Tests.ModifierTests.Modifier_three_withs&lt;/TestId&gt;&#xD;
&lt;TestId&gt;xUnit::F686E4F1-1AA0-4473-B7FB-F3654B7377E1::net6.0::BemIt.Tests.ModifierTests.Modifier_with_enum_with_one_mod&lt;/TestId&gt;&#xD;
&lt;TestId&gt;xUnit::F686E4F1-1AA0-4473-B7FB-F3654B7377E1::net6.0::BemIt.Tests.ModifierTests.Modifier_with_enum_with_multi&lt;/TestId&gt;&#xD;
&lt;TestId&gt;xUnit::F686E4F1-1AA0-4473-B7FB-F3654B7377E1::net6.0::BemIt.Tests.ModifierTests.Modifier_with_args_with_enum&lt;/TestId&gt;&#xD;
&lt;TestId&gt;F686E4F1-1AA0-4473-B7FB-F3654B7377E1::net6.0::xUnit::BemIt.Tests.BlockTests.Block_modifier_with_true_condition&lt;/TestId&gt;&#xD;
&lt;TestId&gt;F686E4F1-1AA0-4473-B7FB-F3654B7377E1::net6.0::xUnit::BemIt.Tests.ElementTests.Element_modifier&lt;/TestId&gt;&#xD;
&lt;TestId&gt;F686E4F1-1AA0-4473-B7FB-F3654B7377E1::net6.0::xUnit::BemIt.Tests.BlockTests.ChildBlock&lt;/TestId&gt;&#xD;
&lt;TestId&gt;F686E4F1-1AA0-4473-B7FB-F3654B7377E1::net6.0::xUnit::BemIt.Tests.ElementTests&lt;/TestId&gt;&#xD;
&lt;TestId&gt;F686E4F1-1AA0-4473-B7FB-F3654B7377E1::net6.0::xUnit::BemIt.Tests.ModifierTests.Modifier_with_true_condition&lt;/TestId&gt;&#xD;
&lt;TestId&gt;F686E4F1-1AA0-4473-B7FB-F3654B7377E1::net6.0::xUnit::BemIt.Tests.ModifierTests.Modifier_with_two_mods&lt;/TestId&gt;&#xD;
&lt;TestId&gt;F686E4F1-1AA0-4473-B7FB-F3654B7377E1::net6.0::xUnit::BemIt.Tests.ModifierTests.Modifier_three_withs&lt;/TestId&gt;&#xD;
&lt;TestId&gt;F686E4F1-1AA0-4473-B7FB-F3654B7377E1::net6.0::xUnit::BemIt.Tests.ModifierTests.Modifier_with_enum_with_one_mod&lt;/TestId&gt;&#xD;
&lt;TestId&gt;F686E4F1-1AA0-4473-B7FB-F3654B7377E1::net6.0::xUnit::BemIt.Tests.ModifierTests.Modifier_with_enum_with_multi&lt;/TestId&gt;&#xD;
&lt;TestId&gt;F686E4F1-1AA0-4473-B7FB-F3654B7377E1::net6.0::xUnit::BemIt.Tests.ModifierTests.Modifier_with_args_with_enum&lt;/TestId&gt;&#xD;
&lt;/TestAncestor&gt;&#xD;
&lt;ProjectFile&gt;F686E4F1-1AA0-4473-B7FB-F3654B7377E1/f:BlockTests.cs&lt;/ProjectFile&gt;&#xD;
&lt;/Or&gt;&#xD;
&lt;/SessionState&gt;</s:String>
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=608d3728_002Dfa3b_002D416d_002Dbccb_002D1f8d8412878b/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" Name="Block_modifier_with_true_condition" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;&#xD;
&lt;TestAncestor&gt;&#xD;
&lt;TestId&gt;xUnit::F686E4F1-1AA0-4473-B7FB-F3654B7377E1::net6.0::BemIt.Tests.BlockTests.Block_modifier_with_true_condition&lt;/TestId&gt;&#xD;
&lt;TestId&gt;xUnit::F686E4F1-1AA0-4473-B7FB-F3654B7377E1::net6.0::BemIt.Tests.BlockTests.Block_modifier&lt;/TestId&gt;&#xD;
&lt;TestId&gt;F686E4F1-1AA0-4473-B7FB-F3654B7377E1::net6.0::xUnit::BemIt.Tests.BlockTests.Block_modifier_with_true_condition&lt;/TestId&gt;&#xD;
&lt;TestId&gt;F686E4F1-1AA0-4473-B7FB-F3654B7377E1::net6.0::xUnit::BemIt.Tests.BlockTests.Block_modifier&lt;/TestId&gt;&#xD;
&lt;/TestAncestor&gt;&#xD;
&lt;/SessionState&gt;</s:String>
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=638b456a_002D975f_002D4204_002D9ead_002Da67e5256e5ab/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" Name="All tests from &amp;lt;BemIt.Tests&amp;gt;" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;&#xD;
&lt;Project Location="F:\code\BemIt\BemIt.Tests" Presentation="&amp;lt;BemIt.Tests&amp;gt;" /&gt;&#xD;
&lt;/SessionState&gt;</s:String>
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=ef8a8c4e_002D1ab0_002D4100_002D941d_002D1033707de375/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" Name="All tests from UnitTest1.cs" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;&#xD;
&lt;ProjectFile&gt;F686E4F1-1AA0-4473-B7FB-F3654B7377E1/f:UnitTest1.cs&lt;/ProjectFile&gt;&#xD;
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=ef8a8c4e_002D1ab0_002D4100_002D941d_002D1033707de375/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" IsActive="True" Name="All tests from UnitTest1.cs" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;&#xD;
&lt;Or&gt;&#xD;
&lt;ProjectFile&gt;F686E4F1-1AA0-4473-B7FB-F3654B7377E1/f:BlockTests.cs&lt;/ProjectFile&gt;&#xD;
&lt;ProjectFile&gt;F686E4F1-1AA0-4473-B7FB-F3654B7377E1/f:ElementTests.cs&lt;/ProjectFile&gt;&#xD;
&lt;ProjectFile&gt;F686E4F1-1AA0-4473-B7FB-F3654B7377E1/f:ModifierTests.cs&lt;/ProjectFile&gt;&#xD;
&lt;/Or&gt;&#xD;
&lt;/SessionState&gt;</s:String></wpf:ResourceDictionary>
25 changes: 23 additions & 2 deletions BemIt/Abstract/IBem.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,38 @@
namespace BemIt;
using System.Collections.Generic;

namespace BemIt;

public interface IBem
{
/// <summary>
/// The name of the block or element
/// </summary>
string Name { get; }

/// <summary>
/// Adds original class names
/// </summary>
/// <param name="classNames">a single or a list of class names</param>
/// <returns></returns>
IBem AddClass(params string?[] classNames);

/// <summary>
/// Adds original class name if condition is true
/// </summary>
/// <param name="className">a single or a list of class names</param>
/// <param name="condition"></param>
/// <returns></returns>
IBem AddClass(string className, bool condition);

/// <summary>
/// Generates a list of css classes
/// </summary>
/// <returns></returns>
IEnumerable<string> GenerateCssClasses();

/// <summary>
/// Builds the css class
/// </summary>
/// <returns></returns>
string Build();
}
}
4 changes: 2 additions & 2 deletions BemIt/Abstract/IBlockOrElement.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace BemIt;

public interface IBlockOrElement: IBem
public interface IBlockOrElement
{
string Name { get; }
}
}
42 changes: 39 additions & 3 deletions BemIt/BemBase.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
using System.Collections.Generic;
using System.Linq;

namespace BemIt;

public class BemBase : IBem
public abstract class BemBase : IBem
{
public string Name { get; }

protected BemBase(string blockOrElement)
{
Name = blockOrElement;
}

protected List<string?> ClassNames { get; } = new();

// inherits
Expand All @@ -13,9 +21,37 @@ public IBem AddClass(params string?[] classNames)
return this;
}

// inherits
public IBem AddClass(string? className, bool condition)
{
if (condition)
{
ClassNames.Add(className);
}

return this;
}

// inherits
public virtual IEnumerable<string> GenerateCssClasses()
{
yield return Name;

foreach (var className in ClassNames.Where(className => !string.IsNullOrWhiteSpace(className)))
{
yield return className!;
}
}

// inherits
public virtual string Build()
{
return string.Join(" ", ClassNames).Trim();
return string.Join(" ", GenerateCssClasses());
}

// inherits
public override string ToString()
{
return Build();
}
}
}
2 changes: 1 addition & 1 deletion BemIt/BemIt.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<PackageId>BemIt</PackageId>
<PackageProjectUrl>https://github.com/capdiem/BemIt</PackageProjectUrl>
<Authors>capdiem</Authors>
<Version>1.1.2</Version>
<Version>1.2.0</Version>
<RepositoryUrl>https://github.com/capdiem/BemIt</RepositoryUrl>
<PackageTags>blazor;css;bem;block;element;modifier;</PackageTags>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
Expand Down
25 changes: 2 additions & 23 deletions BemIt/Block.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,10 @@ public class Block : BemBase, IBlockOrElement
/// Creates a block
/// </summary>
/// <param name="name">The name of the block</param>
public Block(string name)
public Block(string name) : base(name)
{
Name = name;
}

/// <summary>
/// The name of the block
/// </summary>
public string Name { get; }

/// <summary>
/// Creates a block extends from current block
/// </summary>
Expand Down Expand Up @@ -51,19 +45,4 @@ public Element Element(string element)
{
return new Element(Name, element);
}

/// <summary>
/// Builds the css class from a block
/// </summary>
/// <returns></returns>
public override string Build()
{
return (Name + " " + string.Join(" ", ClassNames)).Trim();
}

// inherit
public override string ToString()
{
return Build();
}
}
}
28 changes: 2 additions & 26 deletions BemIt/Element.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,7 @@ public class Element : BemBase, IBlockOrElement
/// </summary>
/// <param name="block">From which block</param>
/// <param name="element">The name of the element, appended to the block name with double underscore</param>
public Element(string block, string element)
public Element(string block, string element) : base($"{block}__{element}")
{
Name = $"{block}__{element}";
}

/// <summary>
/// The name of the element
/// </summary>
/// <example>
/// {block}__{element}
/// </example>
public string Name { get; }

/// <summary>
/// Builds the css class from a element
/// </summary>
/// <returns></returns>
public override string Build()
{
return (Name + " " + string.Join(" ", ClassNames)).Trim();
}

// inherit
public override string ToString()
{
return Build();
}
}
}
7 changes: 4 additions & 3 deletions BemIt/Extensions/BlockOrElementExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public static Modifier Modifier(this IBlockOrElement blockOrElement)
{
return new Modifier(blockOrElement.Name);
}

public static Modifier Modifier(this IBlockOrElement blockOrElement, string modifier)
{
return new Modifier(blockOrElement.Name, modifier);
Expand All @@ -33,7 +33,8 @@ public static Modifier Modifier(this IBlockOrElement blockOrElement, Enum value,
return new Modifier(blockOrElement.Name, modifier);
}

public static Modifier Modifier(this IBlockOrElement blockOrElement, bool modifier, [CallerArgumentExpression("modifier")] string name = "")
public static Modifier Modifier(this IBlockOrElement blockOrElement, bool modifier,
[CallerArgumentExpression("modifier")] string name = "")
{
return new Modifier(blockOrElement.Name, name, modifier);
}
Expand Down Expand Up @@ -66,4 +67,4 @@ public static Modifier Modifier(this IBlockOrElement blockOrElement, bool modifi
{ name3, modifier3 },
});
}
}
}
5 changes: 3 additions & 2 deletions BemIt/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ public static string ToKebab(this string name)
#else
var split = Regex.Split(name, "(?<!^)(?=[A-Z])").Select(s => s.Trim('-'));
#endif
return string.Join("-", split).ToLowerInvariant();
return string.Join("-", split).ToLowerInvariant()
.TrimStart('_'); // for private fields like _myField
}
#if NET7_0_OR_GREATER

[GeneratedRegex("(?<!^)(?=[A-Z])")]
private static partial Regex MyRegex();
#endif
}
}
Loading

0 comments on commit f0106ff

Please sign in to comment.