Skip to content

Commit

Permalink
add popover options
Browse files Browse the repository at this point in the history
  • Loading branch information
erinnmclaughlin committed May 19, 2024
1 parent ab45aa7 commit 90548bb
Show file tree
Hide file tree
Showing 49 changed files with 61 additions and 11,037 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<HBDropdownItemLink class="dropdown-item" href="#" OnClickPreventDefault="true" OnClickStopPropagation="true">Link (Prevent Default and Stop Propagation)</HBDropdownItemLink>
</HBDropdownItems>
</HBDropdown>
<HBDropdown class="dropdown d-inline-block">
<HBDropdown class="dropdown d-inline-block" Alignment="HBPopoverAlignment.End">
<HBDropdownTrigger class="btn btn-primary dropdown-toggle">Click Me</HBDropdownTrigger>
<HBDropdownItems class="@(context.IsOpen ? "dropdown-menu show" : "dropdown-menu")">
<HBDropdownItem class="dropdown-item">Default</HBDropdownItem>
Expand Down
11 changes: 11 additions & 0 deletions src/HeadlessBlazor.Core/Behaviors/HBPopoverAlignment.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Text.Json.Serialization;

namespace HeadlessBlazor.Behaviors;

[JsonConverter(typeof(JsonStringEnumConverter))]
public enum HBPopoverAlignment
{
Start,
Center,
End
}
16 changes: 11 additions & 5 deletions src/HeadlessBlazor.Core/Behaviors/HBPopoverBehavior.razor
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
@implements IAsyncDisposable
@code {
private DotNetObjectReference<HBPopoverBehavior>? dotNetObjRef;
private IJSObjectReference? popover;

[Inject]
private IJSRuntime JS { get; set; } = default!;

[Parameter]
public HBPopoverAlignment? Alignment { get; set; }

[Parameter]
public IReferenceable Container { get; set; } = default!;

[Parameter]
public HBPopoverSide? Side { get; set; }

public async ValueTask DisposeAsync()
{
if (popover is not null)
Expand All @@ -17,18 +22,19 @@
await popover.DisposeAsync();
popover = null;
}

dotNetObjRef?.Dispose();
}

protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
dotNetObjRef ??= DotNetObjectReference.Create(this);
var moduleName = nameof(HBPopoverBehavior);
var module = await JS.InvokeAsync<IJSObjectReference>("import", $"./_content/HeadlessBlazor.Core/Behaviors/{moduleName}.razor.js");
popover = await module.InvokeAsync<IJSObjectReference>($"{moduleName}.createInstance", Container.ElementReference, dotNetObjRef);
popover = await module.InvokeAsync<IJSObjectReference>($"{moduleName}.createInstance", Container.ElementReference, new HBPopoverOptions
{
Alignment = Alignment,
Side = Side
});
await module.DisposeAsync();
}
}
Expand Down
22 changes: 10 additions & 12 deletions src/HeadlessBlazor.Core/Behaviors/HBPopoverBehavior.razor.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,36 @@
import {
autoUpdate,
computePosition,
flip
} from '../@floating-ui/dom/dist/floating-ui.dom.browser.min.mjs';
import { autoUpdate, computePosition, flip } from '../floating-ui.dom.browser.min.mjs';

export class HBPopoverBehavior {

/**
* @static
* @param {Element} container
* @param {any} dotNetRef
* @param {any} options
* @returns {HBPopoverBehavior}
*/
static createInstance(container) {
return new HBPopoverBehavior(container);
static createInstance(container, options) {
return new HBPopoverBehavior(container, options);
}

/**
* Creates a new instance of HBPopoverBehavior.
* @constructor
* @param {Element} container
* @param {any} dotNetRef
* @param {any} options
*/
constructor(container) {
constructor(container, options) {
const anchor = container.querySelector('[hb-popover-anchor]');
const floater = container.querySelector('[hb-popover]');

const side = (options && options.Side) || 'bottom';
const align = (options && options.Alignment) || 'start';

this.dispose = autoUpdate(anchor, floater, () => {
computePosition(anchor, floater, {
placement: 'bottom-start',
placement: `${side}-${align}`,
middleware: [flip()]
}).then(({ x, y }) => {
Object.assign(floater.style, {
display: `block`,
left: `${x}px`,
top: `${y}px`,
});
Expand Down
7 changes: 7 additions & 0 deletions src/HeadlessBlazor.Core/Behaviors/HBPopoverOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace HeadlessBlazor.Behaviors;

public sealed record HBPopoverOptions
{
public HBPopoverAlignment? Alignment { get; init; }
public HBPopoverSide? Side { get; init; }
}
12 changes: 12 additions & 0 deletions src/HeadlessBlazor.Core/Behaviors/HBPopoverSide.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Text.Json.Serialization;

namespace HeadlessBlazor.Behaviors;

[JsonConverter(typeof(JsonStringEnumConverter))]
public enum HBPopoverSide
{
Top,
Right,
Bottom,
Left
}
20 changes: 0 additions & 20 deletions src/HeadlessBlazor.Core/wwwroot/@floating-ui/core/LICENSE

This file was deleted.

4 changes: 0 additions & 4 deletions src/HeadlessBlazor.Core/wwwroot/@floating-ui/core/README.md

This file was deleted.

Loading

0 comments on commit 90548bb

Please sign in to comment.