GhostInput reference
Package: @ghostvue/themes. Status: experimental (stability policy).
Themed wrapper around headless GhostInput. Same behavioral props plus size and block. Importing it loads the default stylesheet and scopes styles to this instance. Hand-written until GHO-13 generates manifests.
Generated from the component manifest. Edit the manifest or Vue source, then rebuild the docs.
Props
| Name | Type | Default | Required | Description |
|---|---|---|---|---|
modelValue | string | — | No | Controlled value, forwarded to the unstyled GhostInput. Omit for uncontrolled mode. |
defaultValue | string | "" | No | Initial uncontrolled value, forwarded to the unstyled GhostInput. |
type | 'text' | 'search' | 'email' | 'password' | 'tel' | 'url' | "text" | No | Native text-like type, forwarded to the unstyled GhostInput. |
disabled | boolean | false | No | Forwards native disabled semantics to the unstyled GhostInput. |
readonly | boolean | false | No | Forwards native readonly semantics to the unstyled GhostInput. |
invalid | boolean | false | No | Forwards consumer-owned validity to the unstyled GhostInput. |
required | boolean | false | No | Forwards native required to the unstyled GhostInput. |
size | 'sm' | 'md' | 'lg' | "md" | No | Control size. The default omits data-size so CSS can treat a missing hook as md. |
block | boolean | false | No | Stretch to the available inline size via data-block. |
Emits
None declared.
Models
| Prop | Event | Type | Description |
|---|---|---|---|
modelValue | update:modelValue | string | v-model binding for the input string. |
Slots
None declared.
Exposed members
| Name | Type | Description |
|---|---|---|
element | HTMLInputElement | null | The native input after mount; null before mount and after unmount. Delegates to the unstyled GhostInput. |
focus | (options?: FocusOptions) => void | Focuses the native input when it is mounted and not disabled. Delegates to the unstyled GhostInput. |
blur | () => void | Removes focus from the native input when it is mounted. Delegates to the unstyled GhostInput. |
Parts and data attributes
| Attribute | Value | Description |
|---|---|---|
data-part | root | The native input inside the display:contents themed scope. |
data-state | invalid | Consumer-owned invalid appearance. Wins data-state when overlapping disabled or readonly. |
data-state | disabled | Unavailable appearance. Prefer the native disabled attribute when both apply. |
data-state | readonly | Read-only appearance. Omitted while invalid or disabled. |
CSS variables
| Name | Description |
|---|---|
--ghost-surface-bg | Resting field fill. |
--ghost-surface-muted-bg | Readonly field fill. |
--ghost-text-default | Typed text color. |
--ghost-text-muted | Placeholder text color. |
--ghost-border-default | Resting field border. |
--ghost-border-strong | Hover field border. |
--ghost-border-danger | Invalid field border. |
--ghost-border-hairline-width | Field border thickness. |
--ghost-status-danger-bg | Invalid field fill. |
--ghost-status-danger-fg | Invalid field text. |
--ghost-action-disabled-bg | Disabled field fill. |
--ghost-action-disabled-fg | Disabled field text. |
--ghost-action-disabled-border | Disabled field border. |
--ghost-action-radius | Field corner radius. |
--ghost-action-duration | Color and shadow transition duration. |
--ghost-focus-ring-color | Outer color of the two-color keyboard focus indicator. |
--ghost-focus-ring-contrast | Inner color of the two-color keyboard focus indicator. |
--ghost-focus-ring-offset | Gap between the field edge and the focus ring. |
--ghost-focus-ring-width | Thickness of the focus outline. |
--ghost-control-size-sm-min | Minimum block size for size sm. |
--ghost-control-size-md-min | Minimum block size for size md. |
--ghost-control-size-lg-min | Minimum block size for size lg. |
Accessibility
Role: textbox. APG pattern: https://www.w3.org/WAI/ARIA/apg/patterns/textfield/.
Required ARIA attributes: none.
Keyboard
| Key | Action | Automated browser test |
|---|---|---|
Tab | Move sequential focus into or out of the field when it is not disabled. | Not reviewed |
Enter | May submit an ancestor form for text and search types. | Not reviewed |
Verification
Each category is reviewed separately. Automated keyboard and axe results do not attest manual or assistive-technology review.
| Category | Status | Evidence |
|---|---|---|
| Automated keyboard | Not reviewed | No declared key is mapped to a browser test yet. |
| Manual interaction | Not reviewed | No review recorded. |
| Assistive technology | Not reviewed | No review recorded. |
Examples
input/account-details
Account form with labels, hints, live email validation, and readonly and disabled fields.
<script setup lang="ts">
import { computed, ref } from 'vue'
import {
GhostButton,
GhostCard,
GhostField,
GhostFieldControl,
GhostFieldDescription,
GhostFieldError,
GhostFieldLabel,
GhostInput,
} from '@ghostvue/themes'
/*
* Deterministic seed data. The work email starts without a top-level domain
* so the invalid state is visible on first render; fixing it clears the
* error without a submit.
*/
const firstName = ref('Ada')
const lastName = ref('Lovelace')
const email = ref('ada@analytical-engines')
const workspaceUrl =
'https://app.example.com/workspaces/analytical-engines-research-and-development'
const billingContact = 'finance@analytical-engines.example'
const saved = ref(false)
// A deliberately small check: the browser still runs native email validation.
const emailInvalid = computed(
() => !/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email.value),
)
const nameInvalid = computed(() => firstName.value.trim() === '')
function save(event: SubmitEvent): void {
saved.value = !emailInvalid.value && !nameInvalid.value
if (saved.value) return
// Move focus to the first field that needs attention.
const form = event.currentTarget
if (!(form instanceof HTMLFormElement)) return
form.querySelector<HTMLInputElement>('[aria-invalid="true"]')?.focus()
}
function resetSaved(): void {
saved.value = false
}
</script>
<template>
<form class="account" novalidate @submit.prevent="save" @input="resetSaved">
<GhostCard as="section" aria-labelledby="account-title">
<template #header>
<div class="account__header">
<h2 id="account-title" class="account__title">Account details</h2>
<p class="account__lead">
Shown on invoices, shared reports, and workspace invitations.
</p>
</div>
</template>
<div class="account__fields">
<div class="account__row">
<GhostField required :invalid="nameInvalid">
<GhostFieldLabel required>First name</GhostFieldLabel>
<GhostFieldControl v-slot="field">
<GhostInput
v-bind="field"
v-model="firstName"
name="given-name"
autocomplete="given-name"
block
/>
</GhostFieldControl>
<GhostFieldError v-if="nameInvalid">
Enter a first name.
</GhostFieldError>
</GhostField>
<GhostField>
<GhostFieldLabel>Last name</GhostFieldLabel>
<GhostFieldControl v-slot="field">
<GhostInput
v-bind="field"
v-model="lastName"
name="family-name"
autocomplete="family-name"
block
/>
</GhostFieldControl>
</GhostField>
</div>
<GhostField required :invalid="emailInvalid">
<GhostFieldLabel required>Work email</GhostFieldLabel>
<GhostFieldDescription>
Sign-in links and billing receipts go here.
</GhostFieldDescription>
<GhostFieldControl v-slot="field">
<GhostInput
v-bind="field"
v-model="email"
type="email"
name="email"
autocomplete="email"
block
/>
</GhostFieldControl>
<GhostFieldError v-if="emailInvalid">
Enter an address like name@company.com.
</GhostFieldError>
</GhostField>
<GhostField>
<GhostFieldLabel>Workspace URL</GhostFieldLabel>
<GhostFieldDescription>
Set by your workspace owner. Share it with teammates.
</GhostFieldDescription>
<GhostFieldControl v-slot="field">
<GhostInput
v-bind="field"
:model-value="workspaceUrl"
type="url"
name="workspace-url"
readonly
block
class="account__long-value"
/>
</GhostFieldControl>
</GhostField>
<GhostField disabled>
<GhostFieldLabel>Billing contact</GhostFieldLabel>
<GhostFieldDescription>
Managed by your organization's finance team.
</GhostFieldDescription>
<GhostFieldControl v-slot="field">
<GhostInput
v-bind="field"
:model-value="billingContact"
type="email"
name="billing-contact"
block
class="account__long-value"
/>
</GhostFieldControl>
</GhostField>
</div>
<template #footer>
<div class="account__actions">
<p class="account__status" role="status">
{{ saved ? 'Changes saved.' : '' }}
</p>
<GhostButton type="submit">Save changes</GhostButton>
</div>
</template>
</GhostCard>
</form>
</template>
<style scoped>
/*
* The form owns its width: 28rem keeps labels, hints, and fields at a
* readable measure and lets the card shrink with the preview on phones.
*/
.account {
max-inline-size: 28rem;
}
.account__header {
display: grid;
gap: var(--ghost-space-gap-xs);
padding-block-end: var(--ghost-space-stack-sm);
}
.account__title {
color: var(--ghost-text-default);
font-size: var(--ghost-type-size-lg);
font-weight: var(--ghost-type-weight-semibold);
line-height: var(--ghost-type-line-tight);
margin: 0;
}
.account__lead,
.account__status {
color: var(--ghost-text-muted);
font-size: var(--ghost-type-size-sm);
line-height: var(--ghost-type-line-snug);
margin: 0;
}
.account__fields {
display: grid;
gap: var(--ghost-space-stack-lg);
}
/* Two names share a row when each can keep about 11rem; otherwise they stack. */
.account__row {
display: grid;
gap: var(--ghost-space-stack-lg) var(--ghost-space-gap-md);
grid-template-columns: repeat(auto-fit, minmax(min(11rem, 100%), 1fr));
}
/* Long hints and errors wrap instead of widening the card. */
.account :deep([data-part='description']),
.account :deep([data-part='error']) {
overflow-wrap: anywhere;
}
/*
* Long single-line values stay on one line and end with an ellipsis at rest.
* `:deep()` because the input renders inside GhostFieldControl's slot.
*/
.account :deep(.account__long-value) {
text-overflow: ellipsis;
}
.account__actions {
align-items: center;
display: flex;
flex-wrap: wrap;
gap: var(--ghost-space-gap-sm) var(--ghost-space-gap-md);
justify-content: flex-end;
}
.account__status {
flex: 1 1 auto;
}
</style>
input/themed
Size props on the @ghostvue/themes wrapper, with aligned labels.
<script setup lang="ts">
import { GhostInput } from '@ghostvue/themes'
</script>
<template>
<div class="sizes">
<label class="sizes__row">
<span class="sizes__label">Small</span>
<GhostInput size="sm" name="team" placeholder="Team name" block />
</label>
<label class="sizes__row">
<span class="sizes__label">Medium</span>
<GhostInput name="project" placeholder="Project name" block />
</label>
<label class="sizes__row">
<span class="sizes__label">Large</span>
<GhostInput
size="lg"
name="workspace"
placeholder="Workspace name"
block
/>
</label>
</div>
</template>
<style scoped>
/* A bounded stack: label above field on phones, aligned columns when wider. */
.sizes {
display: grid;
gap: var(--ghost-space-stack-lg);
max-inline-size: 28rem;
}
.sizes__row {
display: grid;
gap: var(--ghost-space-stack-sm);
}
.sizes__label {
color: var(--ghost-text-default);
font-size: var(--ghost-type-size-sm);
font-weight: var(--ghost-type-weight-medium);
line-height: var(--ghost-type-line-snug);
}
/* The docs preview is a `ghost-demo` container; elsewhere the stack stays. */
@container ghost-demo (min-width: 26rem) {
.sizes__row {
align-items: center;
gap: var(--ghost-space-gap-md);
grid-template-columns: 5rem minmax(0, 1fr);
}
}
</style>
input/states
Disabled, readonly, invalid, and required, composed with GhostField.
Disabled: not focusable and not submitted.
Readonly: focusable and submitted, not editable.
Required before you can invite people.
<script setup lang="ts">
import {
GhostField,
GhostFieldControl,
GhostFieldDescription,
GhostFieldError,
GhostFieldLabel,
GhostInput,
} from '@ghostvue/themes'
</script>
<template>
<div class="states">
<!-- GhostField forwards disabled, invalid, and required to the input. -->
<GhostField disabled>
<GhostFieldLabel>Billing contact</GhostFieldLabel>
<GhostFieldDescription>
Disabled: not focusable and not submitted.
</GhostFieldDescription>
<GhostFieldControl v-slot="field">
<GhostInput
v-bind="field"
name="billing"
default-value="finance@analytical-engines.example"
block
/>
</GhostFieldControl>
</GhostField>
<GhostField>
<GhostFieldLabel>Account ID</GhostFieldLabel>
<GhostFieldDescription>
Readonly: focusable and submitted, not editable.
</GhostFieldDescription>
<GhostFieldControl v-slot="field">
<GhostInput
v-bind="field"
name="account-id"
default-value="acct_8F2K19QZ"
readonly
block
/>
</GhostFieldControl>
</GhostField>
<GhostField invalid>
<GhostFieldLabel>Username</GhostFieldLabel>
<GhostFieldControl v-slot="field">
<GhostInput
v-bind="field"
name="username"
default-value="ada lovelace"
block
/>
</GhostFieldControl>
<GhostFieldError>Use letters, numbers, and hyphens only.</GhostFieldError>
</GhostField>
<GhostField required>
<GhostFieldLabel required>Company</GhostFieldLabel>
<GhostFieldDescription
>Required before you can invite people.</GhostFieldDescription
>
<GhostFieldControl v-slot="field">
<GhostInput
v-bind="field"
name="company"
placeholder="Analytical Engines Ltd"
block
/>
</GhostFieldControl>
</GhostField>
</div>
</template>
<style scoped>
/*
* One column at a readable measure. Hints and errors differ in length, so a
* multi-column grid would misalign the fields.
*/
.states {
display: grid;
gap: var(--ghost-space-stack-lg);
max-inline-size: 28rem;
}
/* Long values end with an ellipsis at rest instead of a clipped glyph. */
.states :deep(.ghost-input) {
text-overflow: ellipsis;
}
.states :deep([data-part='description']),
.states :deep([data-part='error']) {
overflow-wrap: anywhere;
}
</style>
input/search
Controlled type=search filtering a local list of reports.
Overrides: none, showing the original example
4 of 4 reports
- Q3 revenue forecast
- Churn by region
- Forecast accuracy, trailing twelve months
- Seat expansion by plan
Original source. Playground overrides are not applied here; use Copy props for the current settings.
<script setup lang="ts">
import { computed, ref } from 'vue'
import { GhostInput, type GhostInputSize } from '@ghostvue/themes'
/*
* Optional so the sample runs as-is; the docs props playground passes them
* to try other states. The defaults (`md`, and Vue's `false` for absent
* booleans) are what this sample renders.
*/
const {
size = 'md',
disabled,
invalid,
} = defineProps<{
size?: GhostInputSize
disabled?: boolean
invalid?: boolean
}>()
// Deterministic local data; a real app would query its own source.
const reports = [
'Q3 revenue forecast',
'Churn by region',
'Forecast accuracy, trailing twelve months',
'Seat expansion by plan',
] as const
const query = ref('')
const matches = computed(() => {
const needle = query.value.trim().toLowerCase()
return needle === ''
? reports
: reports.filter((report) => report.toLowerCase().includes(needle))
})
</script>
<template>
<div class="search">
<label class="search__field">
<span class="search__label">Search reports</span>
<GhostInput
v-model="query"
type="search"
name="q"
placeholder="Forecast"
block
:size
:disabled
:invalid
/>
</label>
<p class="search__count" role="status">
{{ matches.length }} of {{ reports.length }} reports
</p>
<ul class="search__results" aria-label="Matching reports">
<li v-for="report in matches" :key="report">{{ report }}</li>
</ul>
</div>
</template>
<style scoped>
.search {
display: grid;
gap: var(--ghost-space-stack-md);
max-inline-size: 28rem;
}
.search__field {
display: grid;
gap: var(--ghost-space-stack-sm);
}
.search__label {
color: var(--ghost-text-default);
font-size: var(--ghost-type-size-sm);
font-weight: var(--ghost-type-weight-medium);
line-height: var(--ghost-type-line-snug);
}
.search__count {
color: var(--ghost-text-muted);
font-size: var(--ghost-type-size-sm);
line-height: var(--ghost-type-line-snug);
margin: 0;
}
/* Owns list spacing so docs prose rules do not decide it. */
.search__results {
color: var(--ghost-text-default);
display: grid;
font-size: var(--ghost-type-size-sm);
gap: var(--ghost-space-gap-xs);
line-height: var(--ghost-type-line-snug);
list-style: none;
margin: 0;
overflow-wrap: anywhere;
padding: 0;
}
</style>
input/unit-suffix
A visible unit beside the input, read as the first description and never submitted.
<script setup lang="ts">
import { computed, ref, useId } from 'vue'
import {
GhostButton,
GhostField,
GhostFieldControl,
GhostFieldDescription,
GhostFieldError,
GhostFieldLabel,
GhostInput,
} from '@ghostvue/themes'
const timeout = ref('30')
const submitted = ref('')
// One ID per instance, so sibling copies reference their own unit.
const unitId = `${useId()}-unit`
const invalid = computed(() => {
const seconds = Number(timeout.value)
return !/^\d+$/.test(timeout.value) || seconds < 1 || seconds > 120
})
/*
* GhostFieldControl passes its own aria-describedby without merging caller
* IDs, so join them here. The unit comes first so it qualifies the value
* before the hint is read.
*/
function describedBy(fieldIds: string | undefined): string {
return [unitId, fieldIds].filter(Boolean).join(' ')
}
function save(event: SubmitEvent): void {
if (invalid.value || !(event.currentTarget instanceof HTMLFormElement)) return
submitted.value = Array.from(
new FormData(event.currentTarget),
([name, value]) =>
`${name}=${typeof value === 'string' ? value : value.name}`,
).join(', ')
}
</script>
<template>
<form class="webhook-timeout" novalidate @submit.prevent="save">
<GhostField :invalid>
<GhostFieldLabel>Request timeout</GhostFieldLabel>
<GhostFieldDescription>Between 1 and 120.</GhostFieldDescription>
<GhostFieldControl v-slot="field">
<span class="webhook-timeout__group">
<GhostInput
v-bind="field"
v-model="timeout"
:aria-describedby="describedBy(field['aria-describedby'])"
name="timeoutSeconds"
inputmode="numeric"
autocomplete="off"
block
/>
<span :id="unitId" class="webhook-timeout__unit">seconds</span>
</span>
</GhostFieldControl>
<GhostFieldError v-if="invalid">
Enter a whole number from 1 to 120.
</GhostFieldError>
</GhostField>
<div class="webhook-timeout__actions">
<GhostButton type="submit">Save timeout</GhostButton>
<output aria-label="Submitted entries" class="webhook-timeout__result">
{{ submitted }}
</output>
</div>
</form>
</template>
<style scoped>
.webhook-timeout {
display: grid;
gap: var(--ghost-space-stack-md);
max-inline-size: 24rem;
min-inline-size: 0;
}
/*
* Grid tracks size the direct child without selecting the themed input's
* scope span. The input keeps at least half the width; a long unit wraps in
* its own track.
*/
.webhook-timeout__group {
align-items: center;
display: grid;
gap: var(--ghost-space-gap-sm);
grid-template-columns: minmax(50%, 1fr) minmax(0, auto);
}
.webhook-timeout__unit,
.webhook-timeout__result {
color: var(--ghost-text-muted);
font-size: var(--ghost-type-size-sm);
line-height: var(--ghost-type-line-snug);
overflow-wrap: anywhere;
}
.webhook-timeout__actions {
align-items: center;
display: flex;
flex-wrap: wrap;
gap: var(--ghost-space-gap-sm) var(--ghost-space-gap-md);
}
</style>
input/clearable-search
Report search with a decorative icon and a Clear button that returns focus to the field and never submits.
4 of 4 reports
- Q3 revenue forecast
- Churn by region
- Forecast accuracy, trailing twelve months
- Seat expansion by plan
Example settings
<script setup lang="ts">
import { computed, ref, useTemplateRef, watch } from 'vue'
import {
GhostButton,
GhostField,
GhostFieldControl,
GhostFieldLabel,
GhostInput,
type GhostInputExposed,
} from '@ghostvue/themes'
// Deterministic local data; a real app would query its own source.
const reports = [
'Q3 revenue forecast',
'Churn by region',
'Forecast accuracy, trailing twelve months',
'Seat expansion by plan',
] as const
const query = ref('')
const readonly = ref(false)
const disabled = ref(false)
const submitted = ref('')
const group = useTemplateRef<HTMLElement>('group')
const input = useTemplateRef<GhostInputExposed>('input')
// Clearing is an edit, so the button exists only while the field is editable.
const canClear = computed(
() => query.value !== '' && !disabled.value && !readonly.value,
)
const matches = computed(() => {
const needle = query.value.trim().toLowerCase()
return needle === ''
? reports
: reports.filter((report) => report.toLowerCase().includes(needle))
})
function clear(): void {
query.value = ''
// Focus before the button unmounts so focus never falls to <body>.
input.value?.focus()
}
/*
* The button can also disappear while it holds focus, for example when the
* field becomes read-only. It is the group's only other focusable part, so
* move focus to the input before the render removes it.
*/
watch(
canClear,
(next) => {
const active = document.activeElement
if (
!next &&
active !== input.value?.element &&
group.value?.contains(active)
)
input.value?.focus()
},
{ flush: 'pre' },
)
function search(event: SubmitEvent): void {
if (!(event.currentTarget instanceof HTMLFormElement)) return
submitted.value =
Array.from(
new FormData(event.currentTarget),
([name, value]) =>
`${name}=${typeof value === 'string' ? value : value.name}`,
).join(', ') || 'No search fields submitted.'
}
</script>
<template>
<div class="report-search">
<form
role="search"
aria-label="Reports"
class="report-search__form"
@submit.prevent="search"
@reset="submitted = ''"
>
<GhostField :disabled>
<GhostFieldLabel>Search reports</GhostFieldLabel>
<GhostFieldControl v-slot="field">
<span ref="group" class="report-search__group">
<svg
class="report-search__icon"
aria-hidden="true"
focusable="false"
viewBox="0 0 16 16"
width="16"
height="16"
>
<circle
cx="7"
cy="7"
r="4.5"
fill="none"
stroke="currentColor"
stroke-width="1.5"
/>
<path
d="m10.5 10.5 3.5 3.5"
stroke="currentColor"
stroke-width="1.5"
stroke-linecap="round"
/>
</svg>
<GhostInput
ref="input"
v-bind="field"
v-model="query"
type="search"
name="q"
placeholder="Forecast"
:readonly
block
/>
<GhostButton
v-if="canClear"
variant="ghost"
tone="neutral"
icon-only
aria-label="Clear search"
@click="clear"
>
<span data-part="icon" aria-hidden="true">×</span>
</GhostButton>
</span>
</GhostFieldControl>
</GhostField>
<div class="report-search__actions">
<GhostButton type="submit">Search</GhostButton>
<GhostButton type="reset" variant="ghost" tone="neutral">
Reset
</GhostButton>
</div>
</form>
<p class="report-search__meta" role="status">
{{ matches.length }} of {{ reports.length }} reports
</p>
<ul class="report-search__results" aria-label="Matching reports">
<li v-for="report in matches" :key="report">{{ report }}</li>
</ul>
<output aria-label="Submitted entries" class="report-search__meta">
{{ submitted }}
</output>
<details class="report-search__settings">
<summary>Example settings</summary>
<label><input v-model="readonly" type="checkbox" /> Read-only</label>
<label><input v-model="disabled" type="checkbox" /> Disabled</label>
</details>
</div>
</template>
<style scoped>
.report-search {
display: grid;
gap: var(--ghost-space-stack-md);
max-inline-size: 28rem;
min-inline-size: 0;
}
.report-search__form {
display: grid;
gap: var(--ghost-space-stack-sm);
}
/*
* Grid tracks size the direct child without selecting the themed input's
* scope span. The end track keeps the icon-only button's size so the input
* does not resize as the button appears.
*/
.report-search__group {
align-items: center;
display: grid;
gap: var(--ghost-space-gap-sm);
grid-template-columns: auto minmax(0, 1fr) var(--ghost-control-size-md-min);
}
.report-search__icon {
color: var(--ghost-text-muted);
}
/* One clear affordance: hide the UA cancel button inside this group only. */
.report-search__group :deep(.ghost-input::-webkit-search-cancel-button) {
display: none;
}
.report-search__actions {
display: flex;
flex-wrap: wrap;
gap: var(--ghost-space-gap-sm);
}
.report-search__meta {
color: var(--ghost-text-muted);
font-size: var(--ghost-type-size-sm);
line-height: var(--ghost-type-line-snug);
margin: 0;
overflow-wrap: anywhere;
}
/* Owns list spacing so docs prose rules do not decide it. */
.report-search__results {
color: var(--ghost-text-default);
display: grid;
font-size: var(--ghost-type-size-sm);
gap: var(--ghost-space-gap-xs);
line-height: var(--ghost-type-line-snug);
list-style: none;
margin: 0;
overflow-wrap: anywhere;
padding: 0;
}
.report-search__settings label {
align-items: baseline;
display: flex;
gap: var(--ghost-space-gap-xs);
}
</style>
input/duration
A minutes-and-seconds field that parses to whole seconds, shows a field error for out-of-range seconds, and drives a progress bar.
<script setup lang="ts">
import { computed, nextTick, ref } from 'vue'
import {
GhostButton,
GhostCard,
GhostField,
GhostFieldControl,
GhostFieldDescription,
GhostFieldError,
GhostFieldLabel,
GhostInput,
GhostProgress,
} from '@ghostvue/themes'
const MIN_SECONDS = 30
const MAX_SECONDS = 30 * 60
// Stand-in for the live canary's elapsed time, fixed so the sample is stable.
const ELAPSED_SECONDS = 130
type ParsedDuration = { seconds: number } | { error: string }
/*
* Parse `mm:ss` into whole seconds. Minutes take one or two digits so `5:00`
* is accepted; seconds always take two, so `5:0` is a typo rather than a
* guess. Each failure returns the message the field shows.
*/
function parseDuration(text: string): ParsedDuration {
const match = /^(\d{1,2}):(\d{2})$/.exec(text.trim())
if (!match) return { error: 'Enter minutes and seconds, like 05:00.' }
const minutes = Number(match[1])
const seconds = Number(match[2])
if (seconds > 59) return { error: 'Seconds must be from 00 to 59.' }
const total = minutes * 60 + seconds
if (total < MIN_SECONDS || total > MAX_SECONDS)
return { error: 'Enter a bake time from 00:30 to 30:00.' }
return { seconds: total }
}
/** Format whole seconds as zero-padded `mm:ss` for the field and the bar. */
function formatDuration(totalSeconds: number): string {
const minutes = Math.floor(totalSeconds / 60)
const seconds = totalSeconds % 60
return `${String(minutes).padStart(2, '0')}:${String(seconds).padStart(2, '0')}`
}
/*
* Screen readers read `02:10` digit by digit or as a clock time, so the bar's
* accessible value text spells the units out.
*/
function speakDuration(totalSeconds: number): string {
const minutes = Math.floor(totalSeconds / 60)
const seconds = totalSeconds % 60
const parts: string[] = []
if (minutes > 0) parts.push(`${String(minutes)} min`)
if (seconds > 0 || minutes === 0) parts.push(`${String(seconds)} s`)
return parts.join(' ')
}
// The saved value is seconds; the field holds the text the reader typed.
const bakeSeconds = ref(5 * 60)
const draft = ref(formatDuration(bakeSeconds.value))
// Errors wait for the first Save so a half-typed value is not flagged.
const attempted = ref(false)
const status = ref('')
const parsed = computed(() => parseDuration(draft.value))
const error = computed(() =>
attempted.value && 'error' in parsed.value ? parsed.value.error : '',
)
// A shorter bake than the time already elapsed reads as complete.
const elapsed = computed(() => Math.min(ELAPSED_SECONDS, bakeSeconds.value))
const progressText = computed(
() =>
`${formatDuration(elapsed.value)} of ${formatDuration(bakeSeconds.value)}`,
)
const progressValueText = computed(
() =>
`${speakDuration(elapsed.value)} of ${speakDuration(bakeSeconds.value)}`,
)
async function save(event: SubmitEvent): Promise<void> {
// Read the form now: `currentTarget` is cleared once the handler returns.
const form = event.currentTarget
attempted.value = true
const result = parsed.value
if ('error' in result) {
status.value = ''
// `aria-invalid` lands on the next render, so wait before looking for it.
await nextTick()
if (form instanceof HTMLFormElement)
form.querySelector<HTMLElement>('[aria-invalid="true"]')?.focus()
return
}
bakeSeconds.value = result.seconds
// Normalize `5:00` to `05:00` so the field matches what was stored.
draft.value = formatDuration(result.seconds)
attempted.value = false
status.value = `Saved ${String(result.seconds)} seconds.`
}
function clearStatus(): void {
status.value = ''
}
</script>
<template>
<form
class="canary-bake"
novalidate
@submit.prevent="save"
@input="clearStatus"
>
<GhostCard as="section" aria-labelledby="canary-bake-title">
<template #header>
<div class="canary-bake__header">
<h2 id="canary-bake-title" class="canary-bake__title">
checkout-api canary
</h2>
<p class="canary-bake__lead">
Tideline promotes the canary to production after it bakes without
errors.
</p>
</div>
</template>
<div class="canary-bake__body">
<GhostField :invalid="error !== ''">
<GhostFieldLabel>Canary bake time</GhostFieldLabel>
<GhostFieldDescription>
Minutes and seconds (mm:ss), from 00:30 to 30:00.
</GhostFieldDescription>
<GhostFieldControl v-slot="field">
<!--
Plain text, not inputmode="numeric": many phone number pads
have no colon key.
-->
<span class="canary-bake__control">
<GhostInput
v-bind="field"
v-model="draft"
name="bakeTime"
autocomplete="off"
spellcheck="false"
maxlength="5"
block
/>
</span>
</GhostFieldControl>
<GhostFieldError v-if="error">{{ error }}</GhostFieldError>
</GhostField>
<div class="canary-bake__progress">
<div class="canary-bake__row">
<span class="canary-bake__label">Baking in us-east</span>
<span class="canary-bake__value">{{ progressText }}</span>
</div>
<GhostProgress
label="Canary bake"
:value="elapsed"
:max="bakeSeconds"
:value-text="progressValueText"
/>
</div>
</div>
<template #footer>
<div class="canary-bake__actions">
<GhostButton type="submit">Save bake time</GhostButton>
<p class="canary-bake__status" role="status">{{ status }}</p>
</div>
</template>
</GhostCard>
</form>
</template>
<style scoped>
.canary-bake {
max-inline-size: 28rem;
min-inline-size: 0;
}
.canary-bake__header,
.canary-bake__progress {
display: grid;
gap: var(--ghost-space-gap-xs);
}
.canary-bake__title {
color: var(--ghost-text-default);
font-size: var(--ghost-type-size-lg);
font-weight: var(--ghost-type-weight-semibold);
line-height: var(--ghost-type-line-tight);
margin: 0;
}
.canary-bake__lead,
.canary-bake__status {
color: var(--ghost-text-muted);
font-size: var(--ghost-type-size-sm);
line-height: var(--ghost-type-line-snug);
margin: 0;
}
.canary-bake__body {
display: grid;
gap: var(--ghost-space-stack-md);
}
/*
* Room for `mm:ss` without stretching the field across the card. The themed
* input forwards classes to its inner element, outside this component's
* scope, so a wrapper sets the width.
*/
.canary-bake__control {
display: block;
inline-size: 8rem;
max-inline-size: 100%;
}
.canary-bake__row {
align-items: baseline;
display: flex;
flex-wrap: wrap;
gap: var(--ghost-space-gap-sm);
justify-content: space-between;
}
.canary-bake__label {
color: var(--ghost-text-default);
font-size: var(--ghost-type-size-sm);
font-weight: var(--ghost-type-weight-medium);
line-height: var(--ghost-type-line-snug);
}
.canary-bake__value {
color: var(--ghost-text-muted);
font-size: var(--ghost-type-size-xs);
font-variant-numeric: tabular-nums;
}
.canary-bake__actions {
align-items: center;
display: flex;
flex-wrap: wrap;
gap: var(--ghost-space-gap-sm) var(--ghost-space-gap-md);
}
</style>