- Accordion
- Alert
- Alert Dialog
- Aspect Ratio
- Attachment
- Autocomplete
- Avatar
- Badge
- Breadcrumb
- Bubble
- Button
- Button Group
- Calendar
- Card
- Carousel
- Chart
- Checkbox
- Code Block
- Collapsible
- Combobox
- Command
- Context Menu
- Data Table
- Date Picker
- Dialog
- Drawer
- Dropdown Menu
- Empty
- Field
- Hover Card
- Input Group
- Input OTP
- Input
- Item
- Kbd
- Label
- Marker
- Menubar
- Message
- Message Scroller
- Native Select
- Navigation Menu
- Pagination
- Popover
- Progress
- Questionnaire
- Radio Group
- Resizable
- Scroll Area
- Select
- Separator
- Sheet
- Sidebar
- Skeleton
- Slider
- Sonner (Toast)
- Spinner
- Switch
- Table
- Tabs
- Textarea
- Toggle
- Toggle Group
- Tooltip
- Trace
Field
Combine labels, controls, and help text to compose accessible form fields and grouped inputs.
import { Component } from '@angular/core';
import { HlmButtonImports } from '@spartan-ng/helm/button';
import { HlmCheckboxImports } from '@spartan-ng/helm/checkbox';
import { HlmFieldImports } from '@spartan-ng/helm/field';
import { HlmInputImports } from '@spartan-ng/helm/input';
import { HlmSelectImports } from '@spartan-ng/helm/select';
import { HlmTextareaImports } from '@spartan-ng/helm/textarea';
@Component({
selector: 'spartan-field-preview',
imports: [
HlmCheckboxImports,
HlmTextareaImports,
HlmButtonImports,
HlmInputImports,
HlmFieldImports,
HlmSelectImports,
],
host: {
class: 'w-full max-w-md',
},
template: `
<form>
<div hlmFieldGroup>
<fieldset hlmFieldSet>
<legend hlmFieldLegend>Payment Method</legend>
<p hlmFieldDescription>All transactions are secure and encrypted</p>
<div hlmFieldGroup>
<div hlmField>
<label hlmFieldLabel for="field-preview-name-on-card">Name on card</label>
<input hlmInput placeholder="John Doe" id="field-preview-name-on-card" />
</div>
<div hlmField class="col-span-2">
<label hlmFieldLabel for="field-preview-card-number">Card number</label>
<input hlmInput placeholder="1234 1234 1234 1234" id="field-preview-card-number" />
<p hlmFieldDescription>Enter your 16-digit card number</p>
</div>
<div class="grid grid-cols-3 gap-4">
<div hlmField>
<label hlmFieldLabel for="field-exp-month--trigger">Month</label>
<hlm-select id="field-exp-month" class="inline-block">
<hlm-select-trigger class="w-full">
<hlm-select-value />
</hlm-select-trigger>
<hlm-select-content *hlmSelectPortal>
<hlm-select-group>
<hlm-select-item value="01">01</hlm-select-item>
<hlm-select-item value="02">02</hlm-select-item>
<hlm-select-item value="03">03</hlm-select-item>
<hlm-select-item value="04">04</hlm-select-item>
<hlm-select-item value="05">05</hlm-select-item>
<hlm-select-item value="06">06</hlm-select-item>
<hlm-select-item value="07">07</hlm-select-item>
<hlm-select-item value="08">08</hlm-select-item>
<hlm-select-item value="09">09</hlm-select-item>
<hlm-select-item value="10">10</hlm-select-item>
<hlm-select-item value="11">11</hlm-select-item>
<hlm-select-item value="12">12</hlm-select-item>
</hlm-select-group>
</hlm-select-content>
</hlm-select>
</div>
<div hlmField>
<label hlmFieldLabel for="field-exp-year--trigger">Year</label>
<hlm-select id="field-exp-year" class="inline-block">
<hlm-select-trigger class="w-full">
<hlm-select-value placeholder="YYYY" />
</hlm-select-trigger>
<hlm-select-content *hlmSelectPortal>
<hlm-select-group>
<hlm-select-item value="2024">2024</hlm-select-item>
<hlm-select-item value="2025">2025</hlm-select-item>
<hlm-select-item value="2026">2026</hlm-select-item>
<hlm-select-item value="2027">2027</hlm-select-item>
<hlm-select-item value="2028">2028</hlm-select-item>
<hlm-select-item value="2029">2029</hlm-select-item>
</hlm-select-group>
</hlm-select-content>
</hlm-select>
</div>
<div hlmField>
<label hlmFieldLabel for="field-preview-cvv">CVV</label>
<input hlmInput placeholder="123" id="field-preview-cvv" />
</div>
</div>
</div>
</fieldset>
<hlm-field-separator />
<fieldset hlmFieldSet>
<legend hlmFieldLegend>Billing Address</legend>
<p hlmFieldDescription>The billing address associated with your payment method</p>
<div hlmFieldGroup>
<div hlmField orientation="horizontal">
<hlm-checkbox inputId="field-preview-billing-address" [checked]="true" />
<label hlmFieldLabel for="field-preview-billing-address">Same as shipping address.</label>
</div>
</div>
</fieldset>
<fieldset hlmFieldSet>
<div hlmFieldGroup>
<div hlmField>
<label hlmFieldLabel for="field-preview-comments">Comments</label>
<textarea hlmTextarea class="resize-none" id="field-preview-comments"></textarea>
</div>
</div>
</fieldset>
<div hlmField orientation="horizontal">
<button hlmBtn>Submit</button>
<button hlmBtn variant="outline">Cancel</button>
</div>
</div>
</form>
`,
})
export class FieldPreview {}
export const anatomyCode = `
<div hlmField>
<label hlmFieldLabel for="field-preview-fullname">Label</label>
<!-- Input, Select, Switch, etc. -->
<p hlmFieldDescription>Optional helper text.</p>
<hlm-field-error>Validation message.</hlm-field-error>
</div>
`;
export const validationAndErrorCode = `
<div hlmField data-invalid="true">
<label hlmFieldLabel for="validation-error-email">Email</label>
<input hlmInput id="validation-error-email" type="email" aria-invalid="true" />
<hlm-field-error>Enter a valid email address.</hlm-field-error>
</div>
`;Installation
ng g @spartan-ng/cli:ui fieldnx g @spartan-ng/cli:ui fieldUsage
import { HlmFieldImports } from "@spartan-ng/helm/field";<fieldset hlmFieldSet>
<legend hlmFieldLegend>Profile</legend>
<p hlmFieldDescription>This appears on invoices and emails.</p>
<div hlmFieldGroup>
<div hlmField>
<label hlmFieldLabel for="field-preview-fullname">Full name</label>
<input hlmInput id="field-preview-fullname" type="text" placeholder="John Doe" />
<p hlmFieldDescription>This appears on invoices and emails.</p>
</div>
<div hlmField>
<label hlmFieldLabel for="field-preview-username">Username</label>
<input hlmInput id="field-preview-username" type="text" placeholder="john_doe_99" />
<hlm-field-error>Choose another username.</hlm-field-error>
</div>
<div hlmField orientation="horizontal">
<hlm-switch inputId="field-preview-subscribe-newsletter" />
<label hlmFieldLabel for="field-preview-subscribe-newsletter">Subscribe to the newsletter</label>
</div>
</div>
</fieldset>Anatomy
The Field family is designed for composing accessible forms. A typical field is structured as follows:
<div hlmField>
<label hlmFieldLabel for="field-preview-fullname">Label</label>
<!-- Input, Select, Switch, etc. -->
<p hlmFieldDescription>Optional helper text.</p>
<hlm-field-error>Validation message.</hlm-field-error>
</div>HlmFieldis the core wrapper for a single field.HlmFieldContentis a flex column that groups label and description. Not required if you have no description.- Wrap related fields with
HlmFieldGroup, and useHlmFieldSetwithHlmFieldLegendfor semantic grouping.
Examples
Input
import { Component } from '@angular/core';
import { HlmFieldImports } from '@spartan-ng/helm/field';
import { HlmInputImports } from '@spartan-ng/helm/input';
@Component({
selector: 'spartan-field-input-preview',
imports: [HlmFieldImports, HlmInputImports],
host: {
class: 'w-full max-w-md',
},
template: `
<fieldset hlmFieldSet>
<div hlmFieldGroup>
<div hlmField>
<label hlmFieldLabel for="field-input-preview-firstname">Username</label>
<input hlmInput id="field-input-preview-firstname" type="text" placeholder="Alex Cooper" />
<p hlmFieldDescription>Choose a unique username for your account.</p>
</div>
<div hlmField>
<label hlmFieldLabel for="field-input-preview-password">Password</label>
<p hlmFieldDescription>Must be at least 8 characters long.</p>
<input hlmInput id="field-input-preview-password" type="password" placeholder="••••••••" />
</div>
</div>
</fieldset>
`,
})
export class FieldInputPreview {}Textarea
import { Component } from '@angular/core';
import { HlmFieldImports } from '@spartan-ng/helm/field';
import { HlmTextareaImports } from '@spartan-ng/helm/textarea';
@Component({
selector: 'spartan-field-textarea-preview',
imports: [HlmFieldImports, HlmTextareaImports],
host: {
class: 'w-full max-w-md',
},
template: `
<fieldset hlmFieldSet>
<div hlmFieldGroup>
<div hlmField>
<label hlmFieldLabel for="field-textarea-preview-firstname">Feedback</label>
<textarea
hlmTextarea
id="field-textarea-preview-firstname"
placeholder="Your feedback helps us improve..."
></textarea>
<p hlmFieldDescription>Share your thoughts about our service.</p>
</div>
</div>
</fieldset>
`,
})
export class FieldTextareaPreview {}Select
import { Component } from '@angular/core';
import { HlmFieldImports } from '@spartan-ng/helm/field';
import { HlmSelectImports } from '@spartan-ng/helm/select';
@Component({
selector: 'spartan-field-select-preview',
imports: [HlmFieldImports, HlmSelectImports],
host: {
class: 'w-full max-w-md',
},
template: `
<fieldset hlmFieldSet>
<div hlmFieldGroup>
<div hlmField>
<label hlmFieldLabel for="field-select-department">Department</label>
<hlm-select [itemToString]="itemToString">
<hlm-select-trigger class="w-full" buttonId="field-select-department">
<hlm-select-value placeholder="Choose a department" />
</hlm-select-trigger>
<hlm-select-content *hlmSelectPortal>
<hlm-select-group>
@for (department of departments; track department.value) {
<hlm-select-item [value]="department.value">
{{ department.label }}
</hlm-select-item>
}
</hlm-select-group>
</hlm-select-content>
</hlm-select>
<p hlmFieldDescription>Select your department or area of work.</p>
</div>
</div>
</fieldset>
`,
})
export class FieldSelectPreview {
public readonly departments = [
{ value: 'engineering', label: 'Engineering' },
{ value: 'design', label: 'Design' },
{ value: 'marketing', label: 'Marketing' },
{ value: 'sales', label: 'Sales' },
{ value: 'support', label: 'Customer Support' },
{ value: 'hr', label: 'Human Resources' },
{ value: 'finance', label: 'Finance' },
{ value: 'operations', label: 'Operations' },
];
public readonly itemToString = (value: string) => this.departments.find((d) => d.value === value)?.label ?? '';
}Native Select
import { Component } from '@angular/core';
import { HlmFieldImports } from '@spartan-ng/helm/field';
import { HlmNativeSelectImports } from '@spartan-ng/helm/native-select';
@Component({
selector: 'spartan-field-native-select-preview',
imports: [HlmFieldImports, HlmNativeSelectImports],
host: {
class: 'w-full max-w-md',
},
template: `
<fieldset hlmFieldSet>
<hlm-field-group>
<hlm-field>
<label hlmFieldLabel for="field-native-select-department">Department</label>
<hlm-native-select selectId="field-native-select-department">
<option hlmNativeSelectOption value="">Choose department</option>
<option hlmNativeSelectOption value="engineering">Engineering</option>
<option hlmNativeSelectOption value="design">Design</option>
<option hlmNativeSelectOption value="marketing">Marketing</option>
<option hlmNativeSelectOption value="sales">Sales</option>
<option hlmNativeSelectOption value="support">Customer Support</option>
<option hlmNativeSelectOption value="hr">Human Resources</option>
<option hlmNativeSelectOption value="finance">Finance</option>
<option hlmNativeSelectOption value="operations">Operations</option>
</hlm-native-select>
<hlm-field-description>Select your department or area of work.</hlm-field-description>
</hlm-field>
</hlm-field-group>
</fieldset>
`,
})
export class FieldNativeSelectPreview {}Date Picker
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { HlmButtonImports } from '@spartan-ng/helm/button';
import { HlmDatePickerImports } from '@spartan-ng/helm/date-picker';
import { HlmFieldImports } from '@spartan-ng/helm/field';
@Component({
selector: 'spartan-field-date-picker-preview',
imports: [HlmButtonImports, HlmDatePickerImports, HlmFieldImports],
changeDetection: ChangeDetectionStrategy.OnPush,
host: {
class: 'w-full max-w-xs',
},
template: `
<fieldset hlmFieldSet>
<hlm-field-group>
<hlm-field>
<label hlmFieldLabel for="date-range">Date of birth</label>
<hlm-date-picker buttonId="date-range" captionLayout="dropdown">
<span>Enter your date of birth</span>
</hlm-date-picker>
</hlm-field>
</hlm-field-group>
</fieldset>
`,
})
export class FieldDatePickerPreview {}Slider
import { Component, signal } from '@angular/core';
import { HlmField, HlmFieldDescription, HlmFieldGroup, HlmFieldLabel, HlmFieldSet } from '@spartan-ng/helm/field';
import { HlmSliderImports } from '@spartan-ng/helm/slider';
@Component({
selector: 'spartan-field-slider-preview',
imports: [HlmFieldSet, HlmFieldGroup, HlmField, HlmFieldLabel, HlmFieldDescription, HlmSliderImports],
host: {
class: 'w-full max-w-md',
},
template: `
<fieldset hlmFieldSet>
<div hlmFieldGroup>
<div hlmField>
<label hlmFieldLabel>Price Range</label>
<p hlmFieldDescription>Set your budget range ($0 - {{ value() }}).</p>
<hlm-slider [max]="1000" [step]="10" [(value)]="value" />
</div>
</div>
</fieldset>
`,
})
export class FieldSliderPreview {
public readonly value = signal([500]);
}Fieldset
import { Component } from '@angular/core';
import { HlmFieldImports } from '@spartan-ng/helm/field';
import { HlmInputImports } from '@spartan-ng/helm/input';
@Component({
selector: 'spartan-field-set-preview',
imports: [HlmFieldImports, HlmInputImports],
host: {
class: 'w-full max-w-md space-y-6',
},
template: `
<fieldset hlmFieldSet>
<legend hlmFieldLegend>Address Information</legend>
<p hlmFieldDescription>Choose a unique username for your account.</p>
<div hlmFieldGroup>
<div hlmField>
<label hlmFieldLabel for="field-set-street-address">Street Address</label>
<input hlmInput id="field-set-street-address" type="text" placeholder="123 Main St" />
</div>
<div class="grid grid-cols-2 gap-4">
<div hlmField>
<label hlmFieldLabel for="field-set-city">City</label>
<input hlmInput id="field-set-city" type="text" placeholder="Los Angeles" />
</div>
<div hlmField>
<label hlmFieldLabel for="field-set-postal-code">Postal Code</label>
<input hlmInput id="field-set-postal-code" type="text" placeholder="90502" />
</div>
</div>
</div>
</fieldset>
`,
})
export class FieldSetPreview {}Checkbox
Your Desktop & Documents folders are being synced with iCloud Drive. You can access them from other devices.
import { Component } from '@angular/core';
import { HlmCheckboxImports } from '@spartan-ng/helm/checkbox';
import { HlmFieldImports } from '@spartan-ng/helm/field';
@Component({
selector: 'spartan-field-checkbox-preview',
imports: [HlmFieldImports, HlmCheckboxImports],
host: {
class: 'w-full max-w-md',
},
template: `
<div hlmFieldGroup>
<fieldset hlmFieldSet>
<legend hlmFieldLegend variant="label">Show these items on the desktop</legend>
<p hlmFieldDescription>Select the items you want to show on the desktop.</p>
<div hlmFieldGroup class="gap-3">
<div hlmField orientation="horizontal">
<hlm-checkbox inputId="field-hard-disks" />
<label hlmFieldLabel for="field-hard-disks" class="font-normal">Hard disks</label>
</div>
<div hlmField orientation="horizontal">
<hlm-checkbox inputId="field-external-disks" />
<label hlmFieldLabel for="field-external-disks" class="font-normal">External disks</label>
</div>
<div hlmField orientation="horizontal">
<hlm-checkbox inputId="field-cds-dvds-ipods" />
<label hlmFieldLabel for="field-cds-dvds-ipods" class="font-normal">CDs, DVDs, and IPods</label>
</div>
<div hlmField orientation="horizontal">
<hlm-checkbox inputId="field-connected-servers" />
<label hlmFieldLabel for="field-connected-servers" class="font-normal">Connected servers</label>
</div>
</div>
</fieldset>
<hlm-field-separator />
<div hlmField orientation="horizontal">
<hlm-checkbox inputId="field-sync-desktop-documents" [checked]="true" />
<div hlmFieldContent>
<label hlmFieldLabel for="field-sync-desktop-documents">Sync Desktop & Documents folders</label>
<p hlmFieldDescription>
Your Desktop & Documents folders are being synced with iCloud Drive. You can access them from other devices.
</p>
</div>
</div>
</div>
`,
})
export class FieldCheckboxPreview {}Radio
import { Component } from '@angular/core';
import { HlmFieldImports } from '@spartan-ng/helm/field';
import { HlmRadioGroupImports } from '@spartan-ng/helm/radio-group';
@Component({
selector: 'spartan-field-radio-preview',
imports: [HlmFieldImports, HlmRadioGroupImports],
host: {
class: 'w-full max-w-md',
},
template: `
<fieldset hlmFieldSet>
<label hlmFieldLabel>Subscription Plan</label>
<p hlmFieldDescription>Yearly and lifetime plans offer significant savings.</p>
<hlm-radio-group value="monthly">
<div hlmField orientation="horizontal">
<hlm-radio value="monthly" inputId="plan-monthly">
<hlm-radio-indicator indicator />
</hlm-radio>
<label hlmFieldLabel for="plan-monthly" class="font-normal">Monthly ($9.99/month)</label>
</div>
<div hlmField orientation="horizontal">
<hlm-radio value="yearly" inputId="plan-yearly">
<hlm-radio-indicator indicator />
</hlm-radio>
<label hlmFieldLabel for="plan-yearly" class="font-normal">Yearly ($99.99/year)</label>
</div>
<div hlmField orientation="horizontal">
<hlm-radio value="lifetime" inputId="plan-lifetime">
<hlm-radio-indicator indicator />
</hlm-radio>
<label hlmFieldLabel for="plan-lifetime" class="font-normal">Lifetime ($299.99/lifetime)</label>
</div>
</hlm-radio-group>
</fieldset>
`,
})
export class FieldRadioPreview {}Switch
Enable multi-factor authentication. If you do not have a two-factor device, you can use a one-time code sent to your email.
import { Component } from '@angular/core';
import { HlmFieldImports } from '@spartan-ng/helm/field';
import { HlmSwitchImports } from '@spartan-ng/helm/switch';
@Component({
selector: 'spartan-field-switch-preview',
imports: [HlmFieldImports, HlmSwitchImports],
host: {
class: 'w-full max-w-md',
},
template: `
<div hlmField orientation="horizontal">
<div hlmFieldContent>
<label hlmFieldLabel for="field-2fa">Multi-factor authentication</label>
<p hlmFieldDescription>
Enable multi-factor authentication. If you do not have a two-factor device, you can use a one-time code sent
to your email.
</p>
</div>
<hlm-switch inputId="field-2fa" />
</div>
`,
})
export class FieldSwitchPreview {}Choice Card
Wrap HlmField directives inside HlmFieldLabel to create selectable field groups. This works with RadioItem , Checkbox and Switch components.
import { Component } from '@angular/core';
import { HlmFieldImports } from '@spartan-ng/helm/field';
import { HlmRadioGroupImports } from '@spartan-ng/helm/radio-group';
@Component({
selector: 'spartan-field-choice-card-preview',
imports: [HlmFieldImports, HlmRadioGroupImports],
host: {
class: 'w-full max-w-md',
},
template: `
<div hlmFieldGroup>
<fieldset hlmFieldSet>
<label hlmFieldLabel>Compute Environment</label>
<p hlmFieldDescription>Select the compute environment for your cluster.</p>
<hlm-radio-group value="kubernetes">
<label hlmFieldLabel for="kubernetes">
<div hlmField orientation="horizontal">
<div hlmFieldContent>
<div hlmFieldTitle>Kubernetes</div>
<div hlmFieldDescription>Run GPU workloads on a K8s configured cluster.</div>
</div>
<hlm-radio value="kubernetes" inputId="kubernetes">
<hlm-radio-indicator indicator />
</hlm-radio>
</div>
</label>
<label hlmFieldLabel for="virtual-machine">
<div hlmField orientation="horizontal">
<div hlmFieldContent>
<div hlmFieldTitle>Virtual Machine</div>
<div hlmFieldDescription>Access a VM configured cluster to run GPU workloads.</div>
</div>
<hlm-radio value="virtual-machine" inputId="virtual-machine">
<hlm-radio-indicator indicator />
</hlm-radio>
</div>
</label>
</hlm-radio-group>
</fieldset>
</div>
`,
})
export class FieldChoiceCardPreview {}Field Group
Stack HlmField directives with HlmFieldGroup . Add HlmFieldSeparator to divide them.
import { Component } from '@angular/core';
import { HlmCheckboxImports } from '@spartan-ng/helm/checkbox';
import { HlmFieldImports } from '@spartan-ng/helm/field';
@Component({
selector: 'spartan-field-group-preview',
imports: [HlmFieldImports, HlmCheckboxImports],
host: {
class: 'w-full max-w-md',
},
template: `
<div hlmFieldGroup>
<fieldset hlmFieldSet>
<label hlmFieldLabel>Response</label>
<p hlmFieldDescription>
Get notified when ChatGPT responds to requests that take time, like research or image generation.
</p>
<div hlmFieldGroup data-slot="checkbox-group">
<div hlmField orientation="horizontal">
<hlm-checkbox inputId="field-group-push-notifications" disabled [checked]="true" />
<label hlmFieldLabel for="field-group-push-notifications" class="font-normal">Push notifications</label>
</div>
</div>
</fieldset>
<hlm-field-separator />
<fieldset hlmFieldSet>
<label hlmFieldLabel>Tasks</label>
<p hlmFieldDescription>
Get notified when tasks you've created have updates.
<a href="#">Manage tasks</a>
</p>
<div hlmFieldGroup data-slot="checkbox-group">
<div hlmField orientation="horizontal">
<hlm-checkbox inputId="field-group-push-task" />
<label hlmFieldLabel for="field-group-push-task" class="font-normal">Push notifications</label>
</div>
<div hlmField orientation="horizontal">
<hlm-checkbox inputId="field-group-email-task" />
<label hlmFieldLabel for="field-group-email-task" class="font-normal">Email notifications</label>
</div>
</div>
</fieldset>
</div>
`,
})
export class FieldGroupPreview {}Field Separator
Visual divider to separate sections inside a HlmFieldGroup . Accepts optional inline content.
Don't have an account? Sign up
import { Component } from '@angular/core';
import { RouterLink } from '@angular/router';
import { NgIcon, provideIcons } from '@ng-icons/core';
import { remixGithubFill } from '@ng-icons/remixicon';
import { HlmButtonImports } from '@spartan-ng/helm/button';
import { HlmFieldImports } from '@spartan-ng/helm/field';
import { HlmInputImports } from '@spartan-ng/helm/input';
@Component({
selector: 'spartan-field-separator-preview',
imports: [HlmFieldImports, HlmButtonImports, HlmInputImports, NgIcon, RouterLink],
providers: [provideIcons({ remixGithubFill })],
host: {
class: 'w-full max-w-xs',
},
template: `
<div hlmFieldGroup>
<div hlmField>
<label hlmFieldLabel for="field-separator-email">Email</label>
<input hlmInput id="field-separator-email" type="email" placeholder="m@example.com" />
</div>
<div hlmField>
<button hlmBtn type="submit">Login</button>
</div>
<hlm-field-separator>Or continue with</hlm-field-separator>
<div hlmField>
<button hlmBtn variant="outline" type="button">
<ng-icon name="remixGithubFill" class="text-xl" />
Login with GitHub
</button>
<p hlmFieldDescription class="text-center">
Don't have an account?
<a routerLink=".">Sign up</a>
</p>
</div>
</div>
`,
})
export class FieldSeparatorPreview {}RTL
To enable RTL support in spartan-ng, see the RTL configuration guide.
import { Component, computed, inject } from '@angular/core';
import { TranslateService, Translations } from '@spartan-ng/app/app/shared/translate.service';
import { HlmButtonImports } from '@spartan-ng/helm/button';
import { HlmCheckboxImports } from '@spartan-ng/helm/checkbox';
import { HlmFieldImports } from '@spartan-ng/helm/field';
import { HlmInputImports } from '@spartan-ng/helm/input';
import { HlmSelectImports } from '@spartan-ng/helm/select';
import { HlmTextareaImports } from '@spartan-ng/helm/textarea';
@Component({
selector: 'spartan-field-rtl',
imports: [
HlmCheckboxImports,
HlmTextareaImports,
HlmButtonImports,
HlmInputImports,
HlmFieldImports,
HlmSelectImports,
],
host: {
class: 'w-full max-w-md',
'[attr.dir]': '_dir()',
},
template: `
<form>
<div hlmFieldGroup>
<fieldset hlmFieldSet>
<legend hlmFieldLegend>{{ _t()['paymentMethod'] }}</legend>
<p hlmFieldDescription>{{ _t()['secureTransactions'] }}</p>
<div hlmFieldGroup>
<div hlmField>
<label hlmFieldLabel for="field-preview-name-on-card">{{ _t()['nameOnCard'] }}</label>
<input hlmInput placeholder="John Doe" id="field-preview-name-on-card" />
</div>
<div hlmField class="col-span-2">
<label hlmFieldLabel for="field-preview-card-number">{{ _t()['cardNumber'] }}</label>
<input hlmInput placeholder="1234 1234 1234 1234" id="field-preview-card-number" />
<p hlmFieldDescription>{{ _t()['cardNumberDescription'] }}</p>
</div>
<div class="grid grid-cols-3 gap-4">
<div hlmField>
<label hlmFieldLabel for="field-exp-month--trigger">{{ _t()['month'] }}</label>
<hlm-select id="field-exp-month" class="inline-block" [itemToString]="_monthToString">
<hlm-select-trigger class="w-full">
<hlm-select-value />
</hlm-select-trigger>
<hlm-select-content *hlmSelectPortal>
<hlm-select-group>
@for (month of _months(); track month.value) {
<hlm-select-item [value]="month.value">{{ month.label }}</hlm-select-item>
}
</hlm-select-group>
</hlm-select-content>
</hlm-select>
</div>
<div hlmField>
<label hlmFieldLabel for="field-exp-year--trigger">{{ _t()['year'] }}</label>
<hlm-select id="field-exp-year" class="inline-block">
<hlm-select-trigger class="w-full">
<hlm-select-value />
</hlm-select-trigger>
<hlm-select-content *hlmSelectPortal>
<hlm-select-group>
@for (year of years; track year.value) {
<hlm-select-item [value]="year.value">{{ year.label }}</hlm-select-item>
}
</hlm-select-group>
</hlm-select-content>
</hlm-select>
</div>
<div hlmField>
<label hlmFieldLabel for="field-preview-cvv">{{ _t()['cvv'] }}</label>
<input hlmInput placeholder="123" id="field-preview-cvv" />
</div>
</div>
</div>
</fieldset>
<hlm-field-separator />
<fieldset hlmFieldSet>
<legend hlmFieldLegend>{{ _t()['billingAddress'] }}</legend>
<p hlmFieldDescription>{{ _t()['billingAddressDescription'] }}</p>
<div hlmFieldGroup>
<div hlmField orientation="horizontal">
<hlm-checkbox inputId="field-preview-billing-address" [checked]="true" />
<label hlmFieldLabel for="field-preview-billing-address">{{ _t()['sameAsShipping'] }}</label>
</div>
</div>
</fieldset>
<fieldset hlmFieldSet>
<div hlmFieldGroup>
<div hlmField>
<label hlmFieldLabel for="field-preview-comments">{{ _t()['comments'] }}</label>
<textarea
hlmTextarea
class="resize-none"
id="field-preview-comments"
[placeholder]="_t()['commentsPlaceholder']"
></textarea>
</div>
</div>
</fieldset>
<div hlmField orientation="horizontal">
<button hlmBtn>{{ _t()['submit'] }}</button>
<button hlmBtn variant="outline">{{ _t()['cancel'] }}</button>
</div>
</div>
</form>
`,
})
export class FieldRtl {
protected readonly _months = computed(() => {
const t = this._t();
return ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12'].map((m) => ({
label: t[`month${m}` as keyof typeof t],
value: m,
}));
});
protected readonly _monthToString = (value: string) => this._months().find((m) => m.value === value)?.label ?? value;
public readonly years = [
{ label: 'YYYY', value: null },
{ label: '2024', value: '2024' },
{ label: '2025', value: '2025' },
{ label: '2026', value: '2026' },
{ label: '2027', value: '2027' },
{ label: '2028', value: '2028' },
{ label: '2029', value: '2029' },
];
private readonly _language = inject(TranslateService).language;
private readonly _translations: Translations = {
en: {
dir: 'ltr',
values: {
paymentMethod: 'Payment Method',
secureTransactions: 'All transactions are secure and encrypted',
nameOnCard: 'Name on Card',
cardNumber: 'Card Number',
cardNumberDescription: 'Enter your 16-digit card number',
month: 'Month',
year: 'Year',
cvv: 'CVV',
monthPlaceholder: 'MM',
month01: '01',
month02: '02',
month03: '03',
month04: '04',
month05: '05',
month06: '06',
month07: '07',
month08: '08',
month09: '09',
month10: '10',
month11: '11',
month12: '12',
billingAddress: 'Billing Address',
billingAddressDescription: 'The billing address associated with your payment method',
sameAsShipping: 'Same as shipping address',
comments: 'Comments',
commentsPlaceholder: 'Add any additional comments',
submit: 'Submit',
cancel: 'Cancel',
},
},
ar: {
dir: 'rtl',
values: {
paymentMethod: 'طريقة الدفع',
secureTransactions: 'جميع المعاملات آمنة ومشفرة',
nameOnCard: 'الاسم على البطاقة',
cardNumber: 'رقم البطاقة',
cardNumberDescription: 'أدخل رقم البطاقة المكون من 16 رقمًا',
month: 'الشهر',
year: 'السنة',
cvv: 'CVV',
monthPlaceholder: 'ش.ش',
month01: '٠١',
month02: '٠٢',
month03: '٠٣',
month04: '٠٤',
month05: '٠٥',
month06: '٠٦',
month07: '٠٧',
month08: '٠٨',
month09: '٠٩',
month10: '١٠',
month11: '١١',
month12: '١٢',
billingAddress: 'عنوان الفوترة',
billingAddressDescription: 'عنوان الفوترة المرتبط بطريقة الدفع الخاصة بك',
sameAsShipping: 'نفس عنوان الشحن',
comments: 'تعليقات',
commentsPlaceholder: 'أضف أي تعليقات إضافية',
submit: 'إرسال',
cancel: 'إلغاء',
},
},
he: {
dir: 'rtl',
values: {
paymentMethod: 'אמצעי תשלום',
secureTransactions: 'כל העסקאות מאובטחות ומוצפנות',
nameOnCard: 'שם על הכרטיס',
cardNumber: 'מספר כרטיס',
cardNumberDescription: 'הזן את מספר הכרטיס בן 16 הספרות שלך',
month: 'חודש',
year: 'שנה',
cvv: 'CVV',
monthPlaceholder: 'MM',
month01: '01',
month02: '02',
month03: '03',
month04: '04',
month05: '05',
month06: '06',
month07: '07',
month08: '08',
month09: '09',
month10: '10',
month11: '11',
month12: '12',
billingAddress: 'כתובת חיוב',
billingAddressDescription: 'כתובת החיוב המשויכת לאמצעי התשלום שלך',
sameAsShipping: 'זהה לכתובת המשלוח',
comments: 'הערות',
commentsPlaceholder: 'הוסף הערות נוספות',
submit: 'שלח',
cancel: 'בטל',
},
},
};
private readonly _translation = computed(() => this._translations[this._language()]);
protected readonly _t = computed(() => this._translation().values);
protected readonly _dir = computed(() => this._translation().dir);
}Responsive Layout
- Vertical fields: Default orientation stacks label, control, and helper text—ideal for mobile-first layouts.
- Horizontal fields: Set
orientation="horizontal"onHlmFieldto align the label and control side-by-side. Pair withHlmFieldContentto keep descriptions aligned. - Responsive fields: Set
orientation="responsive"for automatic column layouts inside container-aware parents. Apply@container/field-groupclasses onHlmFieldGroupto switch orientations at specific breakpoints.
import { Component } from '@angular/core';
import { HlmButtonImports } from '@spartan-ng/helm/button';
import { HlmFieldImports } from '@spartan-ng/helm/field';
import { HlmInputImports } from '@spartan-ng/helm/input';
import { HlmTextareaImports } from '@spartan-ng/helm/textarea';
@Component({
selector: 'spartan-field-responsive-layout-preview',
imports: [HlmFieldImports, HlmInputImports, HlmTextareaImports, HlmButtonImports],
host: {
class: 'w-full max-w-4xl',
},
template: `
<form>
<fieldset hlmFieldSet>
<legend hlmFieldLegend>Profile</legend>
<p hlmFieldDescription>Fill in your profile information.</p>
<hlm-field-separator />
<div hlmFieldGroup>
<div hlmField orientation="responsive">
<div hlmFieldContent>
<label hlmFieldLabel for="field-responsive-name">Name</label>
<p hlmFieldDescription>Provide your full name for identification</p>
</div>
<input hlmInput id="field-responsive-name" type="text" placeholder="Alex Cooper" />
</div>
<hlm-field-separator />
<div hlmField orientation="responsive">
<div hlmFieldContent>
<label hlmFieldLabel for="field-responsive-message">Message</label>
<p hlmFieldDescription>
You can write your message here. Keep it short, preferably under 100 characters.
</p>
</div>
<textarea
hlmTextarea
id="field-responsive-message"
placeholder="Hello, World!"
class="min-h-[100px] resize-none sm:min-w-[300px]"
></textarea>
</div>
<hlm-field-separator />
<div hlmField orientation="responsive">
<button hlmBtn type="submit">Submit</button>
<button hlmBtn type="button" variant="outline">Cancel</button>
</div>
</div>
</fieldset>
</form>
`,
})
export class FieldResponsiveLayoutPreview {}Validation and Errors
- Add
data-invalidtoHlmFieldto switch the entire block into an error state. - Add
aria-invalidon the input itself for assistive technologies. - Render
FieldErrorimmediately after the control or insideFieldContentto keep error messages aligned with the field. - For checkbox or radio groups wrapped in a
fieldset, renderFieldErroras a sibling of thefieldset, not inside it. BecauseFieldGroupis a container-query context, an error nested in thefieldsettriggers a Chrome layout bug that collapses the controls on toggle.
<div hlmField data-invalid="true">
<label hlmFieldLabel for="validation-error-email">Email</label>
<input hlmInput id="validation-error-email" type="email" aria-invalid="true" />
<hlm-field-error>Enter a valid email address.</hlm-field-error>
</div>Accessibility
HlmFieldSetandHlmFieldLegendkeep related controls grouped for keyboard and assistive tech users.HlmFieldoutputsrole="group"so nested controls inherit labeling fromHlmFieldLabelandHlmFieldLegendwhen combined.- When a control and a
HlmFieldLabelare placed inside the sameHlmField, the label'sforattribute is automatically set to the control'sid. There is no need to setformanually. - Apply
HlmFieldSeparatorsparingly to ensure screen readers encounter clear section boundaries.
Brain API
BrnFieldControlDescribedBy
Selector: [brnFieldControlDescribedBy]
ExportAs: brnFieldControlDescribedBy
Inputs
| Prop | Type | Default | Description |
|---|---|---|---|
| aria-describedby | string | null | null | - |
BrnField
Selector: [brnField],brn-field
ExportAs: brnField
Inputs
| Prop | Type | Default | Description |
|---|---|---|---|
| data-invalid | boolean | false | Whether the field is invalid. Overrides the `data-invalid` attribute. |
| forceInvalid | boolean | false | Whether to force the field into an invalid state, regardless of the form control's state. Overrides both the `data-invalid` and `data-matches-spartan-invalid` attributes. |
Helm API
HlmFieldContent
Selector: [hlmFieldContent],hlm-field-content
ExportAs: hlmFieldContent
HlmFieldDescription
Selector: [hlmFieldDescription],hlm-field-description
ExportAs: hlmFieldDescription
Inputs
| Prop | Type | Default | Description |
|---|---|---|---|
| id | string | `hlm-field-description-${HlmFieldDescription._id++}` | - |
HlmFieldError
Selector: hlm-field-error
ExportAs: hlmFieldError
Inputs
| Prop | Type | Default | Description |
|---|---|---|---|
| id | string | `hlm-field-error-${HlmFieldError._id++}` | The unique ID for the field error. If none is supplied, it will be auto-generated. |
| validator | string | - | The name of the specific validator error key to match (e.g. 'required'). When omitted, the error is shown if any validation error is present. |
| forceShow | boolean | false | Forces the error message to be visible regardless of the control's validation state. |
HlmFieldGroup
Selector: [hlmFieldGroup],hlm-field-group
ExportAs: hlmFieldGroup
HlmFieldLabel
Selector: [hlmFieldLabel],hlm-field-label
ExportAs: hlmFieldLabel
HlmFieldLegend
Selector: legend[hlmFieldLegend]
ExportAs: hlmFieldLegend
Inputs
| Prop | Type | Default | Description |
|---|---|---|---|
| variant | 'label' | 'legend' | 'legend' | - |
HlmFieldSeparator
Selector: hlm-field-separator
ExportAs: hlmFieldSeparator
HlmFieldSet
Selector: fieldset[hlmFieldSet]
ExportAs: hlmFieldSet
HlmFieldTitle
Selector: [hlmFieldTitle],hlm-field-title
ExportAs: hlmFieldTitle
HlmField
Selector: [hlmField],hlm-field
ExportAs: hlmField
Inputs
| Prop | Type | Default | Description |
|---|---|---|---|
| orientation | FieldVariants['orientation'] | 'vertical' | - |
| data-invalid | boolean | false | Whether the field is invalid. Overrides the `data-invalid` attribute. |
| forceInvalid | boolean | false | Whether to force the field into an invalid state, regardless of the form control's state. Overrides both the `data-invalid` and `data-matches-spartan-invalid` attributes. |
On This Page