Skip to content

Input group

An input with text, icons or buttons inside its border.

View as Markdown

Example

Sizes

xs, sm, md (default) and lg: set size="sm" on it, or data-size="sm" on any container, as here. See Theming.

€
xs
€
sm
€
md
€
lg

Installation

npx fizzy-ui add input-group

Copies input-group.ts into src/app/ui/ and installs what it needs. Run init once first.

Accessibility

No axe-core violations axe-core 4.14.0 checks this example, closed and with its panels open, on every build; the site is not published otherwise.

  • The inner input is the control: label it with fzField, a <label for>, or aria-label.
  • Addon text is not read with the input: repeat units or prefixes that matter in the label.

Source

The exact file add copies. It is yours to edit.

input-group.ts
import { Directive, input } from '@angular/core';

type FzSize = 'xs' | 'sm' | 'md' | 'lg';

/**
 * An input with text, icons or buttons inside its border:
 * `<div fzInputGroup><span fzInputGroupAddon>https://</span><input fzInputGroupInput aria-label="Site" /></div>`.
 * Addons go before or after the input. Text in an addon is not read with the input: put units or
 * prefixes that matter in the label too. Works inside `fzField` (the inner input is the control).
 */
@Directive({
  selector: '[fzInputGroup]',
  host: {
    '[attr.data-size]': 'size() ?? null',
    class:
      'flex h-(--fz-h) w-full min-w-0 items-center rounded-md border bg-background text-(length:--fz-text) shadow-xs transition-colors ' +
      'focus-within:ring-2 focus-within:ring-ring has-[[aria-invalid=true]]:border-destructive has-[input:disabled]:opacity-50',
  },
})
export class FzInputGroup {
  /** xs, sm, md, lg. Unset, it takes the size of the nearest `data-size` container, else md. */
  readonly size = input<FzSize>();
}

@Directive({
  selector: '[fzInputGroupAddon]',
  host: { class: 'flex h-full shrink-0 items-center gap-1 px-(--fz-field-px) text-muted-foreground has-[>button]:px-1 [&>svg]:size-4' },
})
export class FzInputGroupAddon {}

@Directive({
  selector: 'input[fzInputGroupInput]',
  host: {
    class:
      'h-full min-w-0 flex-1 bg-transparent px-(--fz-field-px) outline-none placeholder:text-muted-foreground not-first:ps-0 not-last:pe-0 ' +
      'disabled:cursor-not-allowed',
  },
})
export class FzInputGroupInput {}

export const FZ_INPUT_GROUP = [FzInputGroup, FzInputGroupAddon, FzInputGroupInput] as const;