Skip to Content
dk-address

Styling AddressInput

For shadcn/ui, Tailwind, CSS modules or hand-written CSS. Style the headless parts and wrap them once in your own field component.

This guide is for apps that style everything themselves, with Tailwind, CSS modules or plain CSS. AddressInput is in the spirit of shadcn/ui: unstyled parts that already handle the behaviour (ARIA, keyboard, focus, popup positioning, the caret gaps and the KDS search), in your own component that you style and own. It ships no styles, no icons and no copy.

You'll build an AddressField like the one below. If you use shadcn/ui, the shadcn/ui block is a finished one, styled with your theme's tokens. If your component library has its own autocomplete, like Ant Design, see Component libraries instead.

Start with the street name.

Prefilled from a stored id.

Try both fields: pick an address, clear one and press Save to see the validation, and note that the billing address starts filled in from a stored id.

1. Pick a shape

There are two ways to wrap it. Start with the first unless product teams need to rearrange the parts.

A field component. One AddressField with your props (label, description, error), and the parts composed inside. App code can't get the anatomy wrong, and the design system owns every detail. The example above is this shape. Forward AddressInputProps so kind, onSelect, municipalityCodes and the rest still work:

export interface AddressFieldProps extends Omit<AddressInputProps, "children"> {
  label: string;
  description?: string;
  error?: string;
}

Styled parts. Re-export each part with your classes, shadcn-style, when product teams need to rearrange the anatomy. A part's className may be a string or a function of its state, so merge yours with the caller's using a small helper:

import { AddressInput, type AddressInputItemProps } from "@dk-address/react";

type ClassName<State> = string | ((state: State) => string | undefined) | undefined;

export function mergeClassName<State>(base: ClassName<State>, extra: ClassName<State>) {
  return (state: State) =>
    [
      typeof base === "function" ? base(state) : base,
      typeof extra === "function" ? extra(state) : extra,
    ]
      .filter(Boolean)
      .join(" ") || undefined;
}

export function AddressItem({ className, ...props }: AddressInputItemProps) {
  return (
    <AddressInput.Item
      {...props}
      className={mergeClassName(
        (state) => (state.highlighted ? "ds-item ds-item--active" : "ds-item"),
        className,
      )}
    />
  );
}

export const AddressRoot = AddressInput;

2. Style with your tokens

Every part takes className and style and forwards its element's props, so use whichever styling approach your design system already uses. AddressInput → Styling lists the state and attributes each part exposes.

Tailwind

<AddressInput.Item
  suggestion={suggestion}
  className="rounded-md px-2 py-1.5 data-[highlighted]:bg-accent data-[type=street]:font-medium"
/>

Plain CSS or CSS modules

.item[data-highlighted] {
  background: var(--ds-color-accent);
}
.item[data-type="street"]::after {
  content: "Street";
  color: var(--ds-color-muted);
}
.popup {
  width: var(--anchor-width);
  max-height: min(20rem, var(--available-height));
  overflow: auto;
}
.popup[data-starting-style],
.popup[data-ending-style] {
  opacity: 0;
  transform: translateY(-4px);
}

Placing the popup. Popup flips when there's no room and takes side, align, sideOffset, alignOffset and collisionPadding.

3. Show suggestions your way

List hands you each suggestion; render it however your design system renders list rows. suggestion.type tells you what it is, which is a good place for an icon and a hint:

typeIt isGood hint
streetNameA road name, no postcode yet"Road"
streetNameNumberRoad name and number across postcodes"Road and number"
streetA road in one postcode`${suggestion.accessAddressCount} numbers`
accessAddressA building / entrance"Building"
addressA full address with floor and door(none, it's the answer)
<AddressInput.List>
  {(suggestion) => (
    <AddressInput.Item suggestion={suggestion} className="ds-row">
      <Icon name={iconFor(suggestion.type)} aria-hidden />
      <span>{suggestion.label}</span>
    </AddressInput.Item>
  )}
</AddressInput.List>

4. Guide people through the steps

Picking a street or a building doesn't finish the field: the text is rewritten and the caret moves into a gap (Vesterbrogade |, 1620 København V). Help people notice:

  • Gap hints. useAddressInput().state.slot says what the gap wants (houseNumber or unit) and what's typed so far. The example's GapHint shows a line of help while the gap is empty, in an aria-live region.
  • Loading and empty states. Put Status in the popup with a visually hidden class so screen readers hear "Searching…" and result counts, and Empty for "No addresses found". Their children can be functions of { loading, count } and { empty, loading, query }.
  • Errors. state.status === "error" and state.error cover KDS being down; show your design system's inline error.

5. Wire it into forms

  • The text submits under name, like any input.
  • The id is what you usually want to store. Render a hidden input from useAddressInput(), empty until an address is picked, as the example's SelectedId does. Editing the text afterwards clears the selection, so a stale id never submits.
  • Requiring a real address. Treat an empty id as invalid ("Pick an address from the list."), not an empty text field. Pass the message to your error prop and point aria-describedby at it.
  • Edit forms. Prefill from a stored id with selectById(id) from useAddressInput(); the example's Prefill does it in an effect. It fills the text without opening the list or moving focus.
  • More than the label. onSelect receives the full details once loaded: id, floor, door, accessAddress.municipalityCode, postalCode, position (EPSG:25832) and the untouched raw KDS object.
  • Form libraries. Call your library's setValue from onSelect, or render your library's controller around the hidden id input.

6. Accessibility checklist

  • Always render Label, or give Input an aria-label. Agents and screen readers find the field by that name.
  • Give Clear an accessible name when it only shows an icon, e.g. aria-label="Clear delivery address".
  • Mark icons aria-hidden and keep text on every suggestion row.
  • Keep a visible focus style on Input (or on Control with :focus-within), and a visible highlight on items, which is the only cue for keyboard users since focus stays in the input.
  • Text needs 4.5:1 contrast and the highlight, borders and focus ring 3:1.
  • Don't add role attributes: the parts already set combobox, listbox and option roles.

7. Test your wrapper

Pass a client from @dk-address/client with a fake fetch, so tests never hit KDS:

import { createClient } from "@dk-address/client";

const client = createClient({ fetch: fakeFetch });

<AddressField label="Delivery address" client={client} debounceMs={0} />;

Record a few real KDS responses once (the client reference lists the endpoints) and have fakeFetch serve them by URL, so tests are fast and don't change when KDS's data does.

Run these tests in a real browser (Playwright or Vitest browser mode) rather than a DOM shim like jsdom. Caret placement, focus and the popup depend on real input events and layout, which a shim only approximates. Find the field by its role and name (combobox, "Delivery address"), as a screen reader would.