Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CLAUDE.md

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions web/src/lib/components/SearchField.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ A search field that reports every change of its text (the list waits for changes
once. In a FilterBar it takes the width the other filters leave (at least 200px).
-->
<script lang="ts">
import { controlClasses } from './field.ts';
import { controlClasses, inFieldButtonClasses } from './field.ts';

type Props = { value: string; label: string; onchange: (value: string, now: boolean) => void };

Expand All @@ -30,7 +30,7 @@ once. In a FilterBar it takes the width the other filters leave (at least 200px)
{#if value}
<button
type="button"
class="text-base absolute top-1/2 right-1.5 inline-flex size-6 -translate-y-1/2 cursor-pointer items-center justify-center rounded-full leading-none text-muted focus-ring hover:bg-hover hover:text-text"
class={inFieldButtonClasses}
title="Clear"
aria-label="Clear {label}"
onclick={() => {
Expand Down
34 changes: 29 additions & 5 deletions web/src/lib/components/TextField.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ secret, its mask in the placeholder and in a description for screen readers (a p
reliably read, and goes once typing starts); the field may stay empty to keep it. `action`: a
button beside the field (Verify, Find chat ID). `variant="hero"`: the one large, centered field
of a page, its label only for screen readers; `variant="code"`: a short code (a confirmation
code), narrow and monospace.
code), narrow and monospace. `type="password"` has an eye inside the field that shows the value.
-->
<script lang="ts">
import type { Snippet } from 'svelte';
import type { HTMLInputAttributes } from 'svelte/elements';
import { fieldClasses, heroFieldClasses } from './field.ts';
import { fieldClasses, heroFieldClasses, inFieldButtonClasses } from './field.ts';
import Hint from './Hint.svelte';
import Icon from './Icon.svelte';

type Props = {
label: string;
Expand Down Expand Up @@ -64,6 +65,8 @@ code), narrow and monospace.
}: Props = $props();

const hero = $derived(variant === 'hero');
const secret = $derived(type === 'password');
let revealed = $state(false);

function focus(input: HTMLInputElement) {
if (autofocus) input.focus();
Expand All @@ -78,11 +81,11 @@ code), narrow and monospace.
);
</script>

{#snippet input()}
{#snippet field()}
<input
{id}
{name}
{type}
type={secret && revealed ? 'text' : type}
bind:value
{readonly}
required={required && !saved}
Expand All @@ -103,11 +106,32 @@ code), narrow and monospace.
? 'w-[140px] font-mono text-sm tracking-[.15em]'
: 'w-full min-w-0 text-sm'
],
'bg-surface'
'bg-surface',
secret && 'pr-8'
]}
/>
{/snippet}

{#snippet input()}
{#if secret}
<span class="relative flex min-w-0 flex-1">
{@render field()}
<button
type="button"
class={inFieldButtonClasses}
title={revealed ? 'Hide' : 'Show'}
aria-label="Show {label}"
aria-pressed={revealed}
onclick={() => (revealed = !revealed)}
>
<Icon name={revealed ? 'hide' : 'view'} size={16} />
</button>
</span>
{:else}
{@render field()}
{/if}
{/snippet}

<div class="flex flex-col gap-1">
<span class={hero ? 'sr-only' : 'flex items-center gap-1.25 text-label text-muted'}>
<label for={id}>{label}</label>
Expand Down
25 changes: 24 additions & 1 deletion web/src/lib/components/TextField.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { render, screen } from '@testing-library/svelte';
import { fireEvent, render, screen } from '@testing-library/svelte';
import { describe, expect, it } from 'vitest';
import TextField from './TextField.svelte';

Expand Down Expand Up @@ -28,4 +28,27 @@ describe('TextField', () => {
expect(input.className).toContain('tracking-[.15em]');
expect(input.className).not.toContain('w-full');
});

it('shows and hides a password with the eye inside the field', async () => {
render(TextField, { label: 'Password', name: 'password', type: 'password' });

const input = screen.getByLabelText<HTMLInputElement>('Password');
const eye = screen.getByRole('button', { name: 'Show Password' });

expect(input.type).toBe('password');
expect(eye.getAttribute('aria-pressed')).toBe('false');

await fireEvent.click(eye);
expect(input.type).toBe('text');
expect(eye.getAttribute('aria-pressed')).toBe('true');

await fireEvent.click(eye);
expect(input.type).toBe('password');
});

it('has no eye in a field that is not a password', () => {
render(TextField, { label: 'Name', name: 'name' });

expect(screen.queryByRole('button')).toBeNull();
});
});
5 changes: 5 additions & 0 deletions web/src/lib/components/field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ export const fieldFrame = `rounded-control border border-border px-2.5 text-text
/** The frame of a field. */
export const fieldClasses = `h-9 ${fieldFrame}`;

/** A small round button inside a field, at its right end (SearchField's ×, a password's eye);
* the field leaves room for it with pr-8. */
export const inFieldButtonClasses =
'absolute top-1/2 right-1.5 inline-flex size-6 -translate-y-1/2 cursor-pointer items-center justify-center rounded-full text-base leading-none text-muted focus-ring hover:bg-hover hover:text-text';

/** The one large, centered field of a page (TextField's hero). */
export const heroFieldClasses = `w-full rounded-popup border border-border px-4 py-3 text-center text-lg text-text ${focusGlow} ${invalidGlow}`;

Expand Down
1 change: 1 addition & 0 deletions web/src/lib/design-system/DesignSystem.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,7 @@ Every component with its variants, on sample data. Served only by the dev server
note="optional: the email before @ when empty"
/>
<TextField label="Email" name="email" value="developer@example.com" readonly />
<TextField label="Password" name="password" type="password" value="root-password" />
<TextField label="Bot token" name="bot_token" type="password" saved="1234…wxyz" />
<TextField
label="Connection name"
Expand Down
Loading