Skip to content

useTypography ​

The type scale. Three presets, each rewriting the seven --sa-text-* variables the whole app sizes text with.

Open in demo
Open in demo

Import ​

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

Signature ​

ts
function useTypography(): {
  setScale: (preset: TypographyPreset) => void
  setToken: (name: TextSize, value: string) => void
  resetScale: () => void
  currentPreset: Readonly<Ref<TypographyPreset>>
}

TypographyPreset is 'compact' | 'default' | 'comfortable'. TextSize is 'xs' | 'sm' | 'base' | 'lg' | 'xl' | '2xl' | '3xl'.

Returns ​

MemberTypeDescription
setScale(preset) => voidWrites all seven sizes and records the preset.
setToken(name, value) => voidOverrides one size, e.g. setToken('base', '1.0625rem').
resetScale() => voidBack to default.
currentPresetReadonly<Ref<TypographyPreset>>The active preset.

The presets ​

Each preset is a base size in pixels and a factor. The seven sizes derive as base × factor ** offset, with base at offset 0, xs two steps down and 3xl four steps up.

PresetBaseFactor
compact13px1.2
default16px1.2
comfortable16px1.333

Because base is offset 0, two presets sharing a base leave --sa-text-base where it is and move everything around it.

Example ​

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

const { setScale, setToken, resetScale, currentPreset } = useTypography()
</script>

<template>
  <div class="flex flex-wrap items-center gap-3">
    <button type="button" @click="setScale('compact')">
      Compact
    </button>
    <button type="button" @click="setScale('comfortable')">
      Comfortable
    </button>
    <button type="button" @click="setToken('base', '1.0625rem')">
      Nudge the base size
    </button>
    <button type="button" @click="resetScale()">
      Reset
    </button>
    <span>{{ currentPreset }}</span>
  </div>
</template>

Notes ​

  • Nothing is applied on the first call. The preset is stored in config.typographyScale and the config wiring replays it at boot, so this composable does not race it.
  • setToken accepts the seven known size keys only. A typo would write a --sa-text-* variable nothing reads and fail with no error, so the type is the guard.
  • setToken writes an inline value on :root, which outranks whatever setScale wrote. A later setScale overwrites it again.
  • Both writers no-op when there is no document.