Modal.razor 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. @inject IJSRuntime js
  2. @code {
  3. [Parameter] public RenderFragment Title { get; set; }
  4. [Parameter] public RenderFragment Body { get; set; }
  5. [Parameter] public RenderFragment Footer { get; set; }
  6. [Parameter] public bool ShowCloseButton { get; set; } = true;
  7. [Parameter] public bool StaticBackDrop { get; set; }
  8. }
  9. <div class="modal fade" id="@ElementId" data-bs-backdrop="@(StaticBackDrop?"static":"true")">
  10. <div class="modal-dialog modal-dialog-centered modal-xl">
  11. <div class="modal-content">
  12. <div class="modal-header">
  13. <h5 class="modal-title">@Title</h5>
  14. @if (ShowCloseButton)
  15. {
  16. <button type="button" class="btn-close" @onclick="Hide"></button>
  17. }
  18. </div>
  19. <div class="modal-body">
  20. @Body
  21. </div>
  22. <div class="modal-footer">
  23. @if (Footer == null)
  24. {
  25. if (ShowCloseButton)
  26. {
  27. <button class="btn btn-primary" @onclick="Hide">Close</button>
  28. }
  29. }
  30. else
  31. {
  32. @Footer
  33. }
  34. </div>
  35. </div>
  36. </div>
  37. </div>
  38. @code {
  39. private const string HTML_ID_PREFIX = "select2-input-";
  40. private readonly string inputGuid = Guid.NewGuid().ToString();
  41. private string ElementId => HTML_ID_PREFIX + inputGuid;
  42. private ValueTask ShowInternal() => js.InvokeVoidAsync("BootstrapModal", ElementId, true);
  43. private ValueTask HideInternal() => js.InvokeVoidAsync("BootstrapModal", ElementId, false);
  44. private ValueTask ToggleInternal() => js.InvokeVoidAsync("BootstrapModal", ElementId);
  45. private ValueTask UpdateBackDropInternal() => js.InvokeVoidAsync("BootstrapModalSetBackDrop", ElementId, StaticBackDrop);
  46. protected override void OnAfterRender(bool firstRender)
  47. {
  48. base.OnAfterRender(firstRender);
  49. UpdateBackDropInternal();
  50. }
  51. private void CloseButton_Click() => HideInternal();
  52. public void Show() => ShowInternal();
  53. public void Hide() => HideInternal();
  54. public void Toggle() => ToggleInternal();
  55. }