Button Ghost
A transparent button with no border or background — suitable for icon actions, inline controls, and delete/close triggers.
The control is width: fit-content, matching button() — display: inline-flex alone is blockified by a flex or grid parent, so inside a stack() the ghost button stretched to the full column while the outline button next to it did not, although button({ variant: "ghost" }) documents the two as visually identical.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
color | ValueOrState<ThemeColor> | "neutral" | Text color tone. |
size | "small" | "medium" | "large" | "medium" | Button size preset — scales padding and font size via the density/size tokens. Also reachable via button({ variant: "ghost", size }). |
Customization
Must see the source of patch at the bottom of each patch page to understand the structure then code it still code as html native element.
There are four levels of customization, in increasing order of effort:
- Patch props. Each patch exposes a small, stable set of props—typically fewer than five. Lowest friction.
- Context attributes. Use
dataTone,dataSize, anddataDensityon a container to shift tone, size, or density for an entire subtree without touching individual elements. - Inline override. Native-wins merge strategy: any property set directly on the element overrides the patch value.
- Create a variant. Clone a similar patch and edit it. Use this only when you need a reusable custom version.
Formulas
Unit - U = fontSize / 4 - convert final values with themeSpacing(n).
Size - n = intrinsic text lines, w = wrapping level, d = density factor:
height = (n * 6 + 2 * d * w) * U
paddingBlock = d * w * U
paddingInline = ceil(3 / w) * d * w * U
radius = d * w * UBase density d = 1.5:
| U | w=0 | w=1 | w=2 | w=3 |
|---|---|---|---|---|
height (n = 1) | 6 | 9 | 12 | 15 |
| paddingBlock | 0 | 1.5 | 3 | 4.5 |
| paddingInline | 3 | 4.5 | 6 | 4.5 |
| radius | 0 | 1.5 | 3 | 4.5 |
Tone - K = N / 2 where N is the palette length. For N = 18, K = 9.
| Role | Shift | n=0 |
|---|---|---|
| Background | parent +/- n | 0 |
| Text | bg + K | 6 |
| Border | bg + K/2 | 3 |
| Hover | bg + 2K/3 | 4 |
| Selected / Focus | above +/- K/3 | 2-4 |
State shift range: K/3 <= delta <= 2K/3.
import { type PartialElement, toState, type ValueOrState } from "@domphy/core";
import {
type ThemeColor,
textToneOn,
themeColor,
themeDensity,
themeSize,
themeSpacing,
} from "@domphy/theme";
import { BUTTON_SIZE_FONT, type ButtonSize } from "../utils/buttonSize.js";
import { focusRing } from "../utils/focusRing.js";
const PADDING_STEPS: Record<ButtonSize, number> = {
small: 0.5,
medium: 1,
large: 1.5,
};
/**
* A transparent button with no border or background — suitable for icon
* actions, inline controls, and delete/close triggers. Apply to a `<button>`
* element.
*
* @hostTag button
* @param props.color - Text color tone. Optional `ValueOrState<ThemeColor>`, defaults to `"neutral"`.
* @param props.size - Button size preset. Optional `"small" | "medium" | "large"`, defaults to `"medium"`.
* @example { button: "×", $: [buttonGhost()] }
* @example { button: [{ span: "🗑", $: [icon()] }], $: [buttonGhost({ color: "error" })] }
*/
function buttonGhost(
props: { color?: ValueOrState<ThemeColor>; size?: ButtonSize } = {},
): PartialElement {
const color = toState(props.color ?? "neutral", "color");
const padding = PADDING_STEPS[props.size ?? "medium"];
const fontSize = BUTTON_SIZE_FONT[props.size ?? "medium"];
return {
// Native `type` on the host still wins (mergePartial: native over patch).
type: "button",
_onInsert: (node) => {
if (node.tagName !== "button") {
console.warn(`"buttonGhost" primitive patch must use button tag`);
}
},
style: {
appearance: "none",
fontSize: (listener) => themeSize(listener, fontSize),
paddingBlock: (listener) =>
themeSpacing(themeDensity(listener) * padding),
paddingInline: (listener) =>
themeSpacing(themeDensity(listener) * padding),
borderRadius: (listener) => themeSpacing(themeDensity(listener) * 1.5),
display: "inline-flex",
// Same as button(): without it a flex/grid parent stretches the control
// to the full column. Measured in Chromium inside a `stack()`: an
// outline button was 88px wide and the ghost button next to it 1232px,
// although `button({ variant: "ghost" })` documents the two as visually
// identical.
width: "fit-content",
justifyContent: "center",
alignItems: "center",
gap: (listener) => themeSpacing(themeDensity(listener) * 1),
userSelect: "none",
cursor: "pointer",
lineHeight: "inherit",
border: "none",
background: "none",
outline: "none",
transition:
"background-color 140ms ease, color 140ms ease, box-shadow 140ms ease",
// Resting text uses the "text" alias (shift-9) — NOT the semantic-zone
// shift-6 the patch previously used. A ghost button's label is essential
// interactive content, and shift-6 on an edge surface measures ~2.4:1,
// failing WCAG AA (axe color-contrast). shadcn's ghost variant likewise
// rests at full foreground; hover/active keep the same text color and
// differentiate via background only.
color: (listener) => themeColor(listener, "text", color.get(listener)),
// Hover/active paint a fill 2 steps off the surface, so the label moves
// with it (textToneOn) — a fixed "text" label measured 3.58:1 on the
// hover fill (light) / 4.37:1 (dark), below WCAG AA.
"&:hover:not([disabled]):not([aria-busy=true])": {
color: (listener) =>
themeColor(listener, textToneOn(2), color.get(listener)),
backgroundColor: (listener) =>
themeColor(listener, "hover", color.get(listener)),
},
"&:active:not([disabled]):not([aria-busy=true])": {
color: (listener) =>
themeColor(listener, textToneOn(2), color.get(listener)),
backgroundColor: (listener) =>
themeColor(listener, "increase-2", color.get(listener)),
},
"&:focus-visible": {
boxShadow: (listener) => focusRing(listener, color.get(listener)),
},
"&[disabled]": {
opacity: 0.7,
cursor: "not-allowed",
color: (listener) => themeColor(listener, "border-strong", "neutral"),
},
"&[aria-busy=true]": {
opacity: 0.7,
cursor: "wait",
pointerEvents: "none",
},
},
};
}
export { buttonGhost };