Input with prefix and suffix
An input with a fixed piece before or after it: a currency sign, a unit, a web address or a phone country code. The whole row is one well and takes the focus ring.
Project settings
$
USD
https://
.example.com
Options
Code
import { useId } from "react";
import "./input-affix.css";
/* An input with a fixed piece before or after it.
prefix and suffix are text; codes turns the prefix into a country code picker.
<AffixInput label="Budget" prefix="$" suffix="USD" defaultValue="4,500" /> */
export function AffixInput({ label, prefix, suffix, codes, code, onCode, ...props }) {
const id = useId();
return (
<div className="dh-affix">
<label htmlFor={id}>{label}</label>
<div>
{codes ? (
<select aria-label="Country code" value={code} onChange={(event) => onCode?.(event.target.value)}>
{codes.map((item) => (
<option key={item}>{item}</option>
))}
</select>
) : (
prefix && <b>{prefix}</b>
)}
<input id={id} type="text" {...props} />
{suffix && <b>{suffix}</b>}
</div>
</div>
);
}
export function Example() {
return (
<section className="dh-affix-card">
<h3>Project settings</h3>
<div className="dh-affix-stack">
<AffixInput label="Budget" prefix="$" suffix="USD" inputMode="decimal" defaultValue="4,500" />
<AffixInput label="Site address" prefix="https://" suffix=".example.com" placeholder="studio" />
<AffixInput label="Phone" codes={["+44", "+1", "+49", "+61"]} type="tel" placeholder="7700 900123" />
</div>
<footer>
<span>Prices are shown without tax.</span>
<button type="button">Save</button>
</footer>
</section>
);
}/* An input with a fixed piece before or after it: a currency sign, a unit, a
web address or a phone country code. The whole row is one well and takes the
focus ring. Shown here as three rows of a small settings card. */
.dh-affix {
--line: color-mix(in srgb, var(--dh-fg, #171717) 9%, transparent);
display: grid;
gap: 5px;
min-width: 0;
color: var(--dh-fg, #171717);
font-size: 13px;
line-height: 20px;
text-align: left;
}
.dh-affix > label {
color: var(--dh-muted, #5c5c5c);
font-size: 11px;
line-height: 16px;
}
.dh-affix > div {
display: flex;
align-items: center;
height: 32px;
border-radius: 7px;
background: var(--dh-well, #ededeb);
transition: box-shadow 150ms cubic-bezier(0.23, 1, 0.32, 1);
}
.dh-affix > div:has(input:focus-visible) {
box-shadow: 0 0 0 2px var(--dh-faint, #8a8a8a);
}
.dh-affix input {
flex: 1;
width: 0;
min-width: 0;
height: 100%;
padding: 0 10px;
border: 0;
background: none;
color: inherit;
font: inherit;
font-variant-numeric: tabular-nums;
outline: none;
}
.dh-affix input::placeholder {
color: var(--dh-faint, #8a8a8a);
}
/* The fixed pieces. One before the input reads as part of the value; one after
it is a quiet tag behind a hairline. */
.dh-affix b {
flex: none;
color: var(--dh-faint, #8a8a8a);
font-weight: inherit;
white-space: nowrap;
}
.dh-affix b:first-child {
padding-left: 10px;
}
.dh-affix b:first-child + input {
padding-left: 3px;
}
.dh-affix input + b {
margin: 6px 0;
padding: 0 10px;
border-left: 1px solid var(--line);
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 11px;
line-height: 20px;
}
/* The country code is a small raised select inside the well. */
.dh-affix select {
-webkit-appearance: none;
appearance: none;
flex: none;
height: 24px;
margin-left: 4px;
padding: 0 20px 0 8px;
border: 0;
border-radius: 5px;
background: var(--dh-raised, #fff) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 12 12' fill='none' stroke='%238a8a8a' stroke-width='1.4' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M3.5 4.75 6 7.25l2.5-2.5'/%3E%3C/svg%3E") right 5px center / 12px no-repeat;
color: inherit;
font: inherit;
font-variant-numeric: tabular-nums;
cursor: pointer;
outline: none;
box-shadow:
0 0 0 1px var(--line),
0 1px 2px color-mix(in srgb, var(--dh-fg, #171717) 10%, transparent);
}
.dh-affix select:focus-visible {
box-shadow: 0 0 0 2px var(--dh-faint, #8a8a8a);
}
.dh-affix select + input {
padding-left: 8px;
}
/* The card around them in the example. */
.dh-affix-card {
--accent: var(--dh-accent, #2563ff);
--line: color-mix(in srgb, var(--dh-fg, #171717) 9%, transparent);
width: 340px;
max-width: 100%;
overflow: hidden;
border-radius: 14px;
background: var(--dh-raised, #fff);
box-shadow:
0 0 0 1px var(--line),
0 1px 2px color-mix(in srgb, var(--dh-fg, #171717) 10%, transparent);
color: var(--dh-fg, #171717);
font-family: inherit;
font-size: 13px;
line-height: 20px;
text-align: left;
}
.dh-affix-card h3 {
margin: 0;
padding: 14px 16px 12px;
font-size: 14px;
font-weight: 500;
line-height: 20px;
letter-spacing: -0.015em;
}
.dh-affix-stack {
display: grid;
gap: 12px;
padding: 0 16px 16px;
}
.dh-affix-card footer {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
padding: 10px 10px 10px 16px;
border-top: 1px solid var(--line);
background: color-mix(in srgb, var(--dh-fg, #171717) 3%, var(--dh-raised, #fff));
color: var(--dh-muted, #5c5c5c);
font-size: 12px;
}
.dh-affix-card button {
flex: none;
height: 32px;
padding: 0 12px;
border: 0;
border-radius: 8px;
background: linear-gradient(color-mix(in srgb, var(--accent) 84%, #fff), var(--accent));
color: var(--dh-on-accent, #fff);
font: inherit;
font-size: 13px;
font-weight: 500;
letter-spacing: -0.01em;
cursor: pointer;
box-shadow:
inset 0 1px 0 rgb(255 255 255 / 0.3),
0 0 0 1px color-mix(in srgb, var(--accent) 78%, #000),
0 1px 2px color-mix(in srgb, var(--dh-fg, #171717) 22%, transparent);
transition: transform 150ms cubic-bezier(0.23, 1, 0.32, 1);
}
.dh-affix-card button:active {
transform: scale(0.97);
}
.dh-affix-card button:focus-visible {
outline: 2px solid var(--dh-faint, #8a8a8a);
outline-offset: 2px;
}
@media (pointer: coarse) {
.dh-affix > div {
height: 44px;
}
.dh-affix input {
font-size: 16px;
}
.dh-affix select {
height: 34px;
}
}
@media (prefers-reduced-motion: reduce) {
.dh-affix > div,
.dh-affix-card button {
transition: none;
}
}<section class="dh-affix-card">
<h3>Project settings</h3>
<div class="dh-affix-stack">
<div class="dh-affix">
<label for="dh-affix-budget">Budget</label>
<div>
<b>$</b>
<input id="dh-affix-budget" type="text" inputmode="decimal" value="4,500" />
<b>USD</b>
</div>
</div>
<div class="dh-affix">
<label for="dh-affix-site">Site address</label>
<div>
<b>https://</b>
<input id="dh-affix-site" type="text" placeholder="studio" />
<b>.example.com</b>
</div>
</div>
<div class="dh-affix">
<label for="dh-affix-phone">Phone</label>
<div>
<select aria-label="Country code">
<option>+44</option>
<option>+1</option>
<option>+49</option>
<option>+61</option>
</select>
<input id="dh-affix-phone" type="tel" placeholder="7700 900123" />
</div>
</div>
</div>
<footer>
<span>Prices are shown without tax.</span>
<button type="button">Save</button>
</footer>
</section>/* An input with a fixed piece before or after it: a currency sign, a unit, a
web address or a phone country code. The whole row is one well and takes the
focus ring. Shown here as three rows of a small settings card. */
.dh-affix {
--line: color-mix(in srgb, var(--dh-fg, #171717) 9%, transparent);
display: grid;
gap: 5px;
min-width: 0;
color: var(--dh-fg, #171717);
font-size: 13px;
line-height: 20px;
text-align: left;
}
.dh-affix > label {
color: var(--dh-muted, #5c5c5c);
font-size: 11px;
line-height: 16px;
}
.dh-affix > div {
display: flex;
align-items: center;
height: 32px;
border-radius: 7px;
background: var(--dh-well, #ededeb);
transition: box-shadow 150ms cubic-bezier(0.23, 1, 0.32, 1);
}
.dh-affix > div:has(input:focus-visible) {
box-shadow: 0 0 0 2px var(--dh-faint, #8a8a8a);
}
.dh-affix input {
flex: 1;
width: 0;
min-width: 0;
height: 100%;
padding: 0 10px;
border: 0;
background: none;
color: inherit;
font: inherit;
font-variant-numeric: tabular-nums;
outline: none;
}
.dh-affix input::placeholder {
color: var(--dh-faint, #8a8a8a);
}
/* The fixed pieces. One before the input reads as part of the value; one after
it is a quiet tag behind a hairline. */
.dh-affix b {
flex: none;
color: var(--dh-faint, #8a8a8a);
font-weight: inherit;
white-space: nowrap;
}
.dh-affix b:first-child {
padding-left: 10px;
}
.dh-affix b:first-child + input {
padding-left: 3px;
}
.dh-affix input + b {
margin: 6px 0;
padding: 0 10px;
border-left: 1px solid var(--line);
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 11px;
line-height: 20px;
}
/* The country code is a small raised select inside the well. */
.dh-affix select {
-webkit-appearance: none;
appearance: none;
flex: none;
height: 24px;
margin-left: 4px;
padding: 0 20px 0 8px;
border: 0;
border-radius: 5px;
background: var(--dh-raised, #fff) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 12 12' fill='none' stroke='%238a8a8a' stroke-width='1.4' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M3.5 4.75 6 7.25l2.5-2.5'/%3E%3C/svg%3E") right 5px center / 12px no-repeat;
color: inherit;
font: inherit;
font-variant-numeric: tabular-nums;
cursor: pointer;
outline: none;
box-shadow:
0 0 0 1px var(--line),
0 1px 2px color-mix(in srgb, var(--dh-fg, #171717) 10%, transparent);
}
.dh-affix select:focus-visible {
box-shadow: 0 0 0 2px var(--dh-faint, #8a8a8a);
}
.dh-affix select + input {
padding-left: 8px;
}
/* The card around them in the example. */
.dh-affix-card {
--accent: var(--dh-accent, #2563ff);
--line: color-mix(in srgb, var(--dh-fg, #171717) 9%, transparent);
width: 340px;
max-width: 100%;
overflow: hidden;
border-radius: 14px;
background: var(--dh-raised, #fff);
box-shadow:
0 0 0 1px var(--line),
0 1px 2px color-mix(in srgb, var(--dh-fg, #171717) 10%, transparent);
color: var(--dh-fg, #171717);
font-family: inherit;
font-size: 13px;
line-height: 20px;
text-align: left;
}
.dh-affix-card h3 {
margin: 0;
padding: 14px 16px 12px;
font-size: 14px;
font-weight: 500;
line-height: 20px;
letter-spacing: -0.015em;
}
.dh-affix-stack {
display: grid;
gap: 12px;
padding: 0 16px 16px;
}
.dh-affix-card footer {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
padding: 10px 10px 10px 16px;
border-top: 1px solid var(--line);
background: color-mix(in srgb, var(--dh-fg, #171717) 3%, var(--dh-raised, #fff));
color: var(--dh-muted, #5c5c5c);
font-size: 12px;
}
.dh-affix-card button {
flex: none;
height: 32px;
padding: 0 12px;
border: 0;
border-radius: 8px;
background: linear-gradient(color-mix(in srgb, var(--accent) 84%, #fff), var(--accent));
color: var(--dh-on-accent, #fff);
font: inherit;
font-size: 13px;
font-weight: 500;
letter-spacing: -0.01em;
cursor: pointer;
box-shadow:
inset 0 1px 0 rgb(255 255 255 / 0.3),
0 0 0 1px color-mix(in srgb, var(--accent) 78%, #000),
0 1px 2px color-mix(in srgb, var(--dh-fg, #171717) 22%, transparent);
transition: transform 150ms cubic-bezier(0.23, 1, 0.32, 1);
}
.dh-affix-card button:active {
transform: scale(0.97);
}
.dh-affix-card button:focus-visible {
outline: 2px solid var(--dh-faint, #8a8a8a);
outline-offset: 2px;
}
@media (pointer: coarse) {
.dh-affix > div {
height: 44px;
}
.dh-affix input {
font-size: 16px;
}
.dh-affix select {
height: 34px;
}
}
@media (prefers-reduced-motion: reduce) {
.dh-affix > div,
.dh-affix-card button {
transition: none;
}
}Input with prefix and suffix is not in the Figma file yet.
Use the React or HTML code for now.