Skip to content

useDensity ​

The spacing preset. One class on <html> retunes --sa-space-unit, and every spacing token follows.

Open in demo
Open in demo

Import ​

ts
import { useDensity } from '@core/theme/useDensity'

Signature ​

ts
function useDensity(): {
  setDensity: (density: Density) => void
  currentDensity: Readonly<Ref<Density>>
}

Density is 'compact' | 'default' | 'comfortable'.

Returns ​

MemberTypeDescription
setDensity(density: Density) => voidSwaps the sa-density-* class on <html>.
currentDensityReadonly<Ref<Density>>The active preset.

Example ​

vue
<script setup lang="ts">
import { useDensity } from '@core/theme/useDensity'

const DENSITIES = ['compact', 'default', 'comfortable'] as const

const { setDensity, currentDensity } = useDensity()
</script>

<template>
  <div class="flex flex-wrap items-center gap-2">
    <button
      v-for="density in DENSITIES"
      :key="density"
      type="button"
      :aria-pressed="currentDensity === density"
      @click="setDensity(density)"
    >
      {{ density }}
    </button>
  </div>
</template>

Notes ​

  • The three classes are mutually exclusive. setDensity removes all of them before adding the one you asked for, so calling it twice leaves one class behind, not two.
  • It is a module-scope singleton, and it applies nothing on the first call. config.ui.density holds the stored value and the config wiring replays it at boot.
  • setDensity no-ops when there is no document.