summaryrefslogtreecommitdiff
path: root/Pages/Component/Dialog.razor
blob: e71ddc67d4f1fbd270f74a9e1cfb1faaa6e00d0f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
@inject IDialogService dialogService
@implements IDialog
@implements IDisposable

<div @onclick=Bump style="@(style)">
    @if(Title is not null) {
        <p>@Title</p>
        <hr/>
    }
    @ChildContent
</div>

@code {
    [Parameter]
    public string? Title { get; set; }

    [Parameter]
    public RenderFragment ChildContent { get; set; }

    [Parameter]
    public int HeightPixels  { set => height = $"{value}px"; }
    [Parameter]
    public int HeightPercent { set => height = $"{value}%"; }

    public bool Visible {
        get => visible;
        set {
            visible = value;
            if(value)
                Bump();
            StateHasChanged();
        }
    }

    public int? ZIndex {
        get => zIndex;
        set {
            zIndex = value;
            StateHasChanged();
        }
    }

    private bool visible = false;

    private int? zIndex;

    private string? height;

    protected override void OnInitialized() =>
       dialogService.Register(this);

    public void Show() => Visible = true;
    public void Hide() => Visible = false;

    private void Bump() =>
        dialogService.BumpZIndex(this);

    private string style =>
        zIndexStyle +
        heightStyle +
        visiblilityStyle;

    private string zIndexStyle =>
        zIndex is null ? "display:none;" : $"z-index:{zIndex};";

    private string heightStyle =>
        $"{(height is null ? "" : $"max-height:{height};")}";

    private string visiblilityStyle =>
        Visible ? "opacity:1;visibility:visible;" : "opacity:0;visibility:hidden;";

    public void Dispose() =>
        dialogService.Unregister(this);
}