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

feat(Tooltip): make the Top as the default position to show tooltip #2298

Merged
merged 2 commits into from
Dec 27, 2024
Merged
Changes from 1 commit
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
38 changes: 29 additions & 9 deletions src/Masa.Blazor/Components/Tooltip/MTooltip.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,22 @@ public MTooltip()
private static Block _block = new("m-tooltip");
private ModifierBuilder _modifierBuilder = _block.CreateModifierBuilder();
private ModifierBuilder _contentModifierBuilder = _block.Element("content").CreateModifierBuilder();

private Position ComputedPosition
{
get
{
if (Right) return Position.Right;
if (Bottom) return Position.Bottom;
if (Left) return Position.Left;
return Position.Top;
}
}

protected override IEnumerable<string> BuildComponentClass()
{
yield return _modifierBuilder
.Add(Top, Right, Bottom, Left)
.Add(ComputedPosition.ToString())
.Add("attached", Attach is not { AsT1: true })
.Build();
}
Expand All @@ -52,18 +63,18 @@ protected double CalculatedLeft
var activator = Dimensions.Activator;
var content = Dimensions.Content;
if (activator == null || content == null) return 0;

var unknown = !Bottom && !Left && !Top && !Right;

var activatorLeft = !IsDefaultAttach ? activator.OffsetLeft : activator.Left;
double left = 0;

if (Top || Bottom || unknown)
if (ComputedPosition is Position.Top or Position.Bottom)
{
left = activatorLeft + (activator.Width / 2) - (content.Width / 2);
}
else if (Left || Right)
else if (ComputedPosition is Position.Left or Position.Right)
{
left = activatorLeft + (Right ? activator.Width : -content.Width) + (Right ? 10 : -10);
var right = ComputedPosition == Position.Right;
left = activatorLeft + (right ? activator.Width : -content.Width) + (right ? 10 : -10);
}

if (NudgeLeft != null)
Expand Down Expand Up @@ -93,11 +104,12 @@ protected double CalculatedTop
var activatorTop = !IsDefaultAttach ? activator.OffsetTop : activator.Top;
double top = 0;

if (Top || Bottom)
if (ComputedPosition is Position.Top or Position.Bottom)
{
top = activatorTop + (Bottom ? activator.Height : -content.Height) + (Bottom ? 10 : -10);
var bottom = ComputedPosition == Position.Bottom;
top = activatorTop + (bottom ? activator.Height : -content.Height) + (bottom ? 10 : -10);
}
else if (Left || Right)
else if (ComputedPosition is Position.Left or Position.Right)
{
top = activatorTop + (activator.Height / 2) - (content.Height / 2);
}
Expand All @@ -123,4 +135,12 @@ protected double CalculatedTop
}
}
}

internal enum Position
{
Top,
Right,
Bottom,
Left
}
}
Loading