AddressInput
Headless compound components for the address field. They render no styles; you style every part.
AddressInput and its parts render the address field: the input, the popup and the suggestions. Try it below with either search kind, and watch the state change as you type and pick.
{
"value": "",
"caret": null,
"slot": null,
"status": "idle",
"error": null,
"open": false,
"suggestions": {
"count": 0,
"types": {}
},
"selection": null
}Anatomy
import { AddressInput } from "@dk-address/react";
<AddressInput onSelect={(selection) => save(selection.details)}>
<AddressInput.Label>Address</AddressInput.Label>
<AddressInput.Control>
<AddressInput.Input placeholder="Vesterbrogade 28…" />
<AddressInput.Clear aria-label="Clear">×</AddressInput.Clear>
</AddressInput.Control>
<AddressInput.Popup>
<AddressInput.Status>{(status) => (status.loading ? "Searching…" : null)}</AddressInput.Status>
<AddressInput.Empty>
{(empty) => (empty.empty && !empty.loading ? "No matches" : null)}
</AddressInput.Empty>
<AddressInput.List>
{(suggestion) => <AddressInput.Item suggestion={suggestion} />}
</AddressInput.List>
</AddressInput.Popup>
</AddressInput>;| Part | Renders | Notes |
|---|---|---|
AddressInput | nothing | Takes every core option plus name, required, disabled, readOnly, autoHighlight and loop |
.Label | label | Names the input |
.Control | div | Optional box around the input and clear button, which the popup lines up with; data-open, data-loading |
.Input | input | The combobox; data-open |
.Clear | button | Empties the field and keeps focus in it |
.Popup | div | Positioned against Control, or the input without one; side, align, sideOffset, alignOffset, collisionPadding, anchor |
.Status | div | Announces loading and counts; children can be a function of { loading, count } |
.Empty | div | Shows when nothing matched; children can be a function of { empty, loading, query } |
.List | div | The listbox; children can be a function of each suggestion, and with no children it renders one Item per suggestion |
.Item | div | One suggestion; shows its label unless given children; data-type, data-highlighted |
Styling
Nothing is styled. Every part takes className and style, plus its element's props, so plain CSS, CSS modules, inline styles and Tailwind all work:
<AddressInput.Item
suggestion={suggestion}
className={(state) => (state.highlighted ? "item item--active" : "item")}
style={{ padding: "0.25rem 0.5rem" }}
/>className and style can be functions of the part's state, and the same state shows as attributes for CSS:
| Part | className / style function gets | Attributes |
|---|---|---|
Control | (plain div) | data-open, data-loading |
Input | { open, disabled, readOnly, required } | data-open, data-disabled |
Clear | { disabled } | data-disabled |
Popup | { open, status } | data-open, data-side, data-align, data-starting-style, data-ending-style |
Item | { highlighted, disabled } | data-highlighted, data-disabled, data-type |
The popup lines up with Control when you render one, so an icon or padding inside the box doesn't shift it, and with the input otherwise. Pass anchor (an element or a CSS selector) to line it up with something else. It sets --anchor-width (the width of what it lines up with) and --available-height (room to the viewport edge), e.g. w-(--anchor-width) max-h-(--available-height). Animate it with data-starting-style and data-ending-style; it waits for your transition before unmounting. The Styling AddressInput guide shows full Tailwind and CSS examples, and the shadcn/ui block is a complete styled version.
To read the state or the selection from inside <AddressInput>, use useAddressInput().
Server Components
AddressInput and its parts render from a React Server Component, like a page in the Next.js App Router. On its own there, the field submits the text under its name:
import { AddressInput } from "@dk-address/react";
export default function Page() {
return (
<form action="/order">
<AddressInput name="address" required>
<AddressInput.Label>Address</AddressInput.Label>
<AddressInput.Input />
<AddressInput.Popup>
<AddressInput.List />
</AddressInput.Popup>
</AddressInput>
</form>
);
}A Server Component can't pass functions to the field: onSelect, a custom client, a function as children, or className and style as functions of state. A field that needs one of them goes in your own component, in a file that starts with "use client", and your page renders that component:
"use client";
import { AddressInput } from "@dk-address/react";
export function AddressField() {
return (
<AddressInput onSelect={(selection) => console.log(selection.details)}>
<AddressInput.Label>Address</AddressInput.Label>
<AddressInput.Input />
<AddressInput.Popup>
<AddressInput.List />
</AddressInput.Popup>
</AddressInput>
);
}useAddressInput() and useAddressAutosuggest() are hooks, so they also need a client component.
Keyboard
| Key | Does |
|---|---|
| ↓ / ↑ | Open the list and move the highlight |
| Enter | Pick the highlighted suggestion |
| Escape | Close the list |
The combobox follows the ARIA combobox pattern with list autocomplete. Focus stays in the input while the highlight moves through the list.
If your component library has its own autocomplete, like Ant Design or MUI, skip AddressInput and drive that autocomplete with useAddressAutosuggest().