/// hooks
useDensity
Reads the active UI density — "default" or "compact" — from the nearest PdsProvider. Use it to make a component’s behaviour density-aware where CSS alone cannot reach.
Signature
function useDensity(): "default" | "compact";Returns
| Field | Type | Description |
|---|---|---|
(return) | "default" | "compact" | The active density from the nearest provider. |
When to use
- Adjust non-visual behaviour by density — e.g. how many rows a virtualised table shows per page, or whether a label collapses to an icon.
- Choose a matching component
sizeprop programmatically when composing controls inside a compact region. - You do not need it for spacing or control heights — those already respond to
data-densitythrough the token layer. Reach for the hook only when the decision lives in React.
Example
import { useDensity } from "@protocore/pds";
function useRowsPerPage() {
const density = useDensity();
return density === "compact" ? 25 : 15;
}Note
useDensity requires an ancestor <PdsProvider>. Set the density on the provider (or via data-density) — the hook reads it, it does not set it.