Skip to content

Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.

1.9 KB
Raw Blame
@inherits ComponentBase
@inject IJSRuntime JsRuntime
@implements IDisposable

<div class="flex w-full">
    <input type="text" readonly value="@Value" onfocus="this.select()" class="min-w-0 flex-1 rounded-l-lg border border-r-0 border-gray-300 bg-gray-50 p-2.5 font-mono text-sm text-gray-900 focus:border-primary-600 focus:ring-primary-600 dark:border-gray-600 dark:bg-gray-700 dark:text-white">
    <button type="button" @onclick="CopyAsync" class="inline-flex shrink-0 items-center gap-2 rounded-r-lg border border-primary-600 bg-primary-600 px-4 text-sm font-medium text-white hover:bg-primary-700 focus:outline-none focus:ring-2 focus:ring-primary-300 dark:hover:bg-primary-700 dark:focus:ring-primary-800">
        @if (_copied)
        {
            <UiIcon Icon="UiIcons.Check" class="h-4 w-4" />
            <span>Copied</span>
        }
        else
        {
            <UiIcon Icon="UiIcons.Duplicate" class="h-4 w-4" />
            <span>Copy</span>
        }
    </button>
</div>

@code {
    private bool _copied;
    private CancellationTokenSource? _resetCts;

    /// <summary>
    /// Gets or sets the text that is shown and copied.
    /// </summary>
    [Parameter]
    public string Value { get; set; } = string.Empty;

    /// <inheritdoc/>
    public void Dispose()
    {
        _resetCts?.Cancel();
        _resetCts?.Dispose();
    }

    private async Task CopyAsync()
    {
        await JsRuntime.InvokeVoidAsync("clipboardCopy.copyText", Value);

        _resetCts?.Cancel();
        _resetCts?.Dispose();
        _resetCts = new CancellationTokenSource();
        var token = _resetCts.Token;

        _copied = true;
        StateHasChanged();

        try
        {
            await Task.Delay(2000, token);
            _copied = false;
            StateHasChanged();
        }
        catch (TaskCanceledException)
        {
            // A newer copy or disposal took over the reset.
        }
    }
}