TailwindCssTagHelpers 0.4.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package TailwindCssTagHelpers --version 0.4.0                
NuGet\Install-Package TailwindCssTagHelpers -Version 0.4.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="TailwindCssTagHelpers" Version="0.4.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add TailwindCssTagHelpers --version 0.4.0                
#r "nuget: TailwindCssTagHelpers, 0.4.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install TailwindCssTagHelpers as a Cake Addin
#addin nuget:?package=TailwindCssTagHelpers&version=0.4.0

// Install TailwindCssTagHelpers as a Cake Tool
#tool nuget:?package=TailwindCssTagHelpers&version=0.4.0                

<img src="assets/logo.svg" align="left" height="45"> tailwindcss-tag-helpers

CI build status NuGet Package GitHub Package Registry Project license

ASP.NET tag helpers to make working with Tailwind CSS and Tailwind UI easier.

Included tag helpers:

Name Description
LinkAriaCurrentStateTagHelper Adds the aria-current attribute to links that are processed by LinkTagHelper and include an aria-current-state attribute
LinkTagHelper Applies css classes based on the href value and current url
ValidationStatusTagHelper Applies css classes based on the validation status of a model value

Installation

dotnet add package TailwindCssTagHelpers

Setup

In your _ViewImports.cshtml add:

@addTagHelper *, TailwindCssTagHelpers

In your Startup.cs add:

public void ConfigureServices(IServiceCollection services)
{
    services.AddTailwindCssTagHelpers(Configuration);
}

In your appsettings.json add:

{
  "TailwindCss": {
    "IncludeComments": true
  }
}

Settings

Name Default value Description
IncludeComments false Add html comments before the target tag with base, current, and default classes to help make development/debugging easier.

Usage

LinkAriaCurrentStateTagHelper

Adds the aria-current="*" attribute to links that are processed by the LinkTagHelper and include an aria-current-state="*" attribute.

<a
  asp-area="" asp-controller="Home" asp-action="Index"
  class="px-3 py-2 text-sm font-medium rounded-md"
  default-class="text-gray-300 hover:bg-gray-700 hover:text-white"
  current-class="text-white bg-gray-900"
  aria-current-state="Page"
>
  Home
</a>

Will output:

<a
  href="/"
  class="px-3 py-2 text-sm font-medium rounded-md text-white bg-gray-900"
  aria-current="page"
>
  Home
</a>
Attributes
Name Value Description
aria-current-state True, Page (default), Step The value to use for the aria-current attribute.

LinkTagHelper

The link tag helper will compare the href to the current url and apply one of two sets of css classes.

The default-class list will be applied if the urls don't match, and the current-class list will be applied if the urls do match.

If an immediate child element has a default-class or current-class attribute it will also have its class lists merged.

The naming of these attributes aligns with the comments found in the Tailwind UI templates and the -class suffix allows the attributes to automatically work with Headwind.

The matching method can be either Full (default) which ensures the link path and current path are the same, or Base which ensures the link path starts with, or is the same as, the current path.

Note: Query string values are not used for either method of matching.

<a
  asp-area="" asp-controller="Home" asp-action="Index"
  class="px-3 py-2 text-sm font-medium rounded-md"
  default-class="text-gray-300 hover:bg-gray-700 hover:text-white"
  current-class="text-white bg-gray-900"
  match="Base"
>
  Home
  <span
    class="ml-auto inline-block py-0.5 px-3 text-xs rounded-full"
    current-class="bg-gray-50"
    default-class="bg-gray-200 text-gray-600 group-hover:bg-gray-200"
  >
    5
  </span>
</a>

Will output:

<a
  href="/"
  class="px-3 py-2 text-sm font-medium rounded-md text-white bg-gray-900"
>
  Home
  <span
    class="ml-auto inline-block py-0.5 px-3 text-xs rounded-full bg-gray-50"
  >
    5
  </span>
</a>
Attributes
Name Value Description
current-class string The css classes to apply if the link matches the current url.
default-class string The css classes to apply if the link doesn't match the current url.
match Full (default) or Base The method to use when matching the link to the current url.

ValidationStatusTagHelper

The validation status tag helper will check the validation status of the model value passed to the asp-for attribute and apply one of two sets of css classes.

The default-class list will be applied if the field is valid, and the error-class list will be applied if the field is invalid.

<input
  type="email"
  asp-for="Input.Email"
  class="block w-full rounded-md pl-10 sm:text-sm transition"
  default-class="border-gray-300 focus:border-blue-400 focus:ring-blue-400"
  error-class="border-red-300 text-red-900 placeholder-red-300 focus:border-red-500 focus:outline-none focus:ring-red-500"
>

This tag helper target element is * so it can be used with any element, including web components and other tag helpers.

<heroicon-solid
  icon="AtSymbol"
  class="h-5 w-5"
  asp-for="Input.Email"
  default-class="text-gray-400"
  error-class="text-red-400"
/>
Attributes
Name Value Description
asp-for ModelExpression An expression to be evaluated against the current model.
default-class string The classes to apply when the form field doesn't have any errors.
error-class string default-class string The classes to apply when the form field doesn't have any errors.

Development

This project uses the run-script dotnet tool to manage its build and test scripts. To use this you'll need to run dotnet tool install and then dotnet r to see the available commands or look at the scripts section in the global.json.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp3.1 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETCoreApp 3.1

    • No dependencies.
  • net6.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.6.0 3,154 4/9/2024
0.5.0 6,045 2/24/2023
0.4.0 2,932 10/13/2022
0.3.0 417 10/12/2022
0.2.0 820 9/25/2022
0.1.0 1,649 12/19/2021
0.1.0-beta.50 168 8/21/2021
0.1.0-beta.10 165 2/19/2021