Skip to content

Offline, privacy-first grammar checker. Fast, open-source, Rust-powered

feat(chrome-ext): add loading state to report buttons (#4462)

Elijah Potter26c0d429891e
diff --git a/packages/chrome-plugin/src/popup/DomainReview.svelte b/packages/chrome-plugin/src/popup/DomainReview.svelte
index 7ef5f302a..3e57df15d 100644
--- a/packages/chrome-plugin/src/popup/DomainReview.svelte
+++ b/packages/chrome-plugin/src/popup/DomainReview.svelte
@@ -10,6 +10,7 @@ let {
}: { domain: string; works: boolean; feedback: string; onSubmit: () => void } = $props();
let submitting = $state(false);
+let successful = $state(false);
async function handleSubmit(event: SubmitEvent) {
event.preventDefault();
@@ -29,6 +30,7 @@ async function handleSubmit(event: SubmitEvent) {
if (success) {
onSubmit();
+ successful = true;
}
}
</script>
@@ -72,7 +74,7 @@ async function handleSubmit(event: SubmitEvent) {
/>
<div class="flex items-center justify-between pt-2">
- <Button type="submit" disabled={submitting}>Submit</Button>
+ <Button state={submitting ? (successful ? "success" : "loading") : "idle"} type="submit" disabled={submitting}>Submit</Button>
</div>
</div>
</form>
diff --git a/packages/chrome-plugin/src/popup/ReportProblematicLint.svelte b/packages/chrome-plugin/src/popup/ReportProblematicLint.svelte
index eb9950127..4add8f715 100644
--- a/packages/chrome-plugin/src/popup/ReportProblematicLint.svelte
+++ b/packages/chrome-plugin/src/popup/ReportProblematicLint.svelte
@@ -10,6 +10,7 @@ let {
}: { rule_id: string; feedback: string; example: string; onSubmit: () => void } = $props();
let submitting = $state(false);
+let successful = $state(false);
async function handleSubmit(event: SubmitEvent) {
event.preventDefault();
@@ -30,6 +31,7 @@ async function handleSubmit(event: SubmitEvent) {
if (success) {
onSubmit();
+ successful = true;
}
}
</script>
@@ -74,7 +76,7 @@ async function handleSubmit(event: SubmitEvent) {
/>
<div class="flex items-center justify-between pt-2">
- <Button type="submit" disabled={submitting}>Submit</Button>
+ <Button state={submitting ? (successful ? "success" : "loading") : "idle"} type="submit" disabled={submitting}>Submit</Button>
</div>
</div>
</form>
diff --git a/packages/components/src/lib/Button.svelte b/packages/components/src/lib/Button.svelte
index 1e8c0a126..32d6f4265 100644
--- a/packages/components/src/lib/Button.svelte
+++ b/packages/components/src/lib/Button.svelte
@@ -1,10 +1,12 @@
<script lang="ts">
import { createEventDispatcher } from 'svelte';
-import type { HTMLAnchorAttributes, HTMLButtonAttributes } from 'svelte/elements';
-import Link from './Link.svelte';
+import type { HTMLAnchorAttributes, HTMLAttributes, HTMLButtonAttributes } from 'svelte/elements';
+import CheckIcon from './icons/CheckIcon.svelte';
type ButtonSize = 'xs' | 'sm' | 'md' | 'lg';
type ButtonColor = 'primary' | 'light' | 'gray' | 'white' | 'dark';
+/** Mutually exclusive feedback states, controlled by the caller. */
+type ButtonState = 'idle' | 'loading' | 'success';
export let size: ButtonSize = 'md';
export let color: ButtonColor | string = 'primary';
@@ -15,12 +17,14 @@ export let target: HTMLAnchorAttributes['target'] = undefined;
export let rel: HTMLAnchorAttributes['rel'] = undefined;
export let type: HTMLButtonAttributes['type'] = 'button';
export let disabled: boolean | undefined = undefined;
+/** Loading blocks activation; success remains interactive. The caller controls transitions and resets. */
+export let state: ButtonState = 'idle';
export let unstyled = false;
// Alias for the `class` attribute since `class` is a reserved TS keyword
export let className: string | undefined = undefined;
let restClass: string | undefined;
-let restProps: Record<string, unknown> = {};
+let restProps: HTMLAttributes<HTMLElement> = {};
const dispatch = createEventDispatcher<{ click: Event; dblclick: Event }>();
const sizeClasses: Record<ButtonSize, string> = {
@@ -43,22 +47,25 @@ const colorClasses: Record<ButtonColor, string> = {
const baseClasses =
'cursor-pointer inline-flex items-center gap-2 justify-center font-medium text-center transition-colors focus:outline-none focus:ring-4 disabled:opacity-50 disabled:cursor-not-allowed';
-$: toneClass = colorClasses[color as ButtonColor] ?? colorClasses.primary;
-$: shapeClass = pill ? 'rounded-full' : 'rounded-lg';
-$: sizeClass = sizeClasses[size] ?? sizeClasses.md;
+$: effectiveDisabled = disabled || state === 'loading';
+$: resolvedRel = target === '_blank' && !rel ? 'noreferrer noopener' : rel;
+$: toneClass = colorClasses[color as ButtonColor];
$: ({ class: restClass, ...restProps } = $$restProps);
$: classes = [
+ // Retain the Link component's styling for existing href callers.
+ href && 'hover:underline text-primary dark:text-white',
!unstyled && baseClasses,
- !unstyled && shapeClass,
- !unstyled && sizeClass,
- !unstyled && toneClass,
+ !unstyled && (pill ? 'rounded-full' : 'rounded-lg'),
+ !unstyled && (sizeClasses[size] ?? sizeClasses.md),
+ !unstyled && (toneClass ?? colorClasses.primary),
+ !unstyled && href && effectiveDisabled && 'opacity-50 cursor-not-allowed',
restClass,
className,
]
.filter(Boolean)
.join(' ');
-$: colorOverride = !unstyled && colorClasses[color as ButtonColor] == null ? color : undefined;
+$: colorOverride = !unstyled && toneClass == null ? color : undefined;
$: inlineStyle =
colorOverride || textColor
? [
@@ -69,43 +76,44 @@ $: inlineStyle =
.join(' ')
: undefined;
-function handleClick(event: Event) {
- if (disabled) {
+/** Guard native button/link activation before forwarding the existing Svelte component events. */
+function handleActivation(event: Event) {
+ if (effectiveDisabled) {
event.preventDefault();
event.stopPropagation();
return;
}
- dispatch('click', event);
+ dispatch(event.type === 'dblclick' ? 'dblclick' : 'click', event);
}
</script>
-{#if href}
- <Link
- class={classes}
- style={inlineStyle}
- href={disabled ? undefined : href}
- aria-disabled={disabled}
- role={disabled ? 'link' : undefined}
- tabindex={disabled ? -1 : undefined}
- rel={rel}
- target={target}
- on:click={handleClick}
- on:dblclick={(event) => dispatch('dblclick', event)}
- {...restProps}
- >
- <slot />
- </Link>
-{:else}
- <button
- class={classes}
- type={type}
- {disabled}
- {...restProps}
- style={inlineStyle}
- on:click={handleClick}
- on:dblclick={(event) => dispatch('dblclick', event)}
- >
- <slot />
- </button>
-{/if}
+<svelte:element
+ this={href ? 'a' : 'button'}
+ {...restProps}
+ class={classes}
+ style={href && restProps.style !== undefined ? restProps.style : inlineStyle}
+ type={href ? undefined : type}
+ disabled={href ? undefined : effectiveDisabled}
+ href={effectiveDisabled ? undefined : href}
+ target={href ? target : undefined}
+ rel={href ? resolvedRel : undefined}
+ aria-busy={state === 'loading' ? true : restProps['aria-busy']}
+ aria-disabled={href && effectiveDisabled ? true : restProps['aria-disabled']}
+ role={href && effectiveDisabled ? 'link' : restProps.role}
+ tabindex={href && effectiveDisabled ? -1 : restProps.tabindex}
+ on:click={handleActivation}
+ on:dblclick={handleActivation}
+>
+ {#if state === 'loading'}
+ <svg class="size-4 shrink-0 animate-spin motion-reduce:animate-none" viewBox="0 0 20 20" fill="none" aria-hidden="true">
+ <circle cx="10" cy="10" r="8" stroke="currentColor" stroke-width="2" opacity="0.25" />
+ <path d="M10 2a8 8 0 0 1 8 8" stroke="currentColor" stroke-width="2" stroke-linecap="round" />
+ </svg>
+ {:else if state === 'success'}
+ <CheckIcon className="size-4 shrink-0" />
+ {/if}
+ <slot />
+</svelte:element>
+<!-- Outside the busy control so loading announcements are not deferred. -->
+<span class="sr-only" role="status">{state === 'loading' ? 'Loading' : state === 'success' ? 'Success' : ''}</span>