You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Connected.Components/Components/Link/Link.razor.cs

92 lines
2.6 KiB

2 years ago
using Connected.Annotations;
using Connected.Extensions;
using Connected.Utilities;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
namespace Connected.Components;
public partial class Link : UIComponent
{
protected string Classname =>
new CssBuilder("mud-typography mud-link")
.AddClass($"mud-{Color.ToDescriptionString()}-text")
.AddClass($"mud-link-underline-{Underline.ToDescriptionString()}")
.AddClass($"mud-typography-{Typo.ToDescriptionString()}")
// When Href is empty, link's hover cursor is text "I beam" even when OnClick has a delegate.
// To change this for more expected look change hover cursor to a pointer:
.AddClass("cursor-pointer", Href == default && OnClick.HasDelegate && !Disabled)
.AddClass($"mud-link-disabled", Disabled)
.AddClass(Class)
.Build();
private Dictionary<string, object> Attributes
{
get => Disabled ? UserAttributes : new Dictionary<string, object>(UserAttributes)
{
{ "href", Href },
{ "target", Target }
};
}
/// <summary>
/// The color of the component. It supports the theme colors.
/// </summary>
[Parameter]
[Category(CategoryTypes.Link.Appearance)]
public ThemeColor Color { get; set; } = ThemeColor.Primary;
/// <summary>
/// Typography variant to use.
/// </summary>
[Parameter]
[Category(CategoryTypes.Link.Appearance)]
public Typo Typo { get; set; } = Typo.body1;
/// <summary>
/// Controls when the link should have an underline.
/// </summary>
[Parameter]
[Category(CategoryTypes.Link.Appearance)]
public Underline Underline { get; set; } = Underline.Hover;
/// <summary>
/// The URL, which is the actual link.
/// </summary>
[Parameter]
[Category(CategoryTypes.Link.Behavior)]
public string Href { get; set; }
/// <summary>
/// The target attribute specifies where to open the link, if Link is specified. Possible values: _blank | _self | _parent | _top | <i>framename</i>
/// </summary>
[Parameter]
[Category(CategoryTypes.Link.Behavior)]
public string Target { get; set; }
/// <summary>
/// Link click event.
/// </summary>
[Parameter] public EventCallback<MouseEventArgs> OnClick { get; set; }
protected async Task OnClickHandler(MouseEventArgs ev)
{
if (Disabled) return;
await OnClick.InvokeAsync(ev);
}
/// <summary>
/// Child content of component.
/// </summary>
[Parameter]
[Category(CategoryTypes.Link.Behavior)]
public RenderFragment ChildContent { get; set; }
/// <summary>
/// If true, the navlink will be disabled.
/// </summary>
[Parameter]
[Category(CategoryTypes.Link.Behavior)]
public bool Disabled { get; set; }
}