Tally UI

FontProvider

FontProvider resolves the stored ui.font setting to a type set and loads its faces through a loader the app supplies; useFontType reads the set, its faces and whether they have loaded.

Import

import {
  FontProvider, useFontType,
  type FontProviderProps, type FontState, type FontStatus,
} from '@tallyui/components';

Usage

import { FontProvider } from '@tallyui/components';
import { useAppSettings } from '@tallyui/pos';

const { settings } = useAppSettings(appSettingsCollection);

// loadBundledFonts is the app's own function that registers the faces of the
// families it is given, for example with expo-font's loadAsync.
<FontProvider font={settings['ui.font']} loadFonts={loadBundledFonts}>
  <App />
</FontProvider>

FontProvider resolves the font prop to a type set: system, instrument, grotesk, fraunces or manrope. One of those ids wins; auto, a missing value or any other value resolves to autoTypeSet, which defaults to system. No font files ship in the packages: the app bundles the OFL files and registers them in loadFonts. FONT_FACES in @tallyui/theme lists the files each family needs, with their names and weights. Register each family under exactly its ThemeFontFamily string (THEME_FONT_FAMILIES), as the expo-font key on native or the @font-face font-family on the web: Text asks for that bare name on native and for the quoted name with a system fallback on the web.

When the set names font families and loadFonts is given, the provider calls loadFonts with those families (body, numbers and display, without repeats) each time the set changes. Until the promise settles the status is loading. When it resolves the status is loaded; when it rejects the provider logs Failed to load fonts with console.warn and the status is failed. A result that arrives after the set has changed again is ignored. A new loadFonts function on a later render does not reload the set; going from no loader to a loader does. Switching back to a set whose earlier result is still held shows that result at once while it reloads.

The system set has no families, so it is loaded at once. Without loadFonts every set is loaded at once, and its faces are taken as already registered, for example by @font-face on the web.

The types in these props (TypeSetId, ThemeFontFamily, ThemeType) come from @tallyui/theme, not @tallyui/components.

The faces in context are the set's only while the status is loaded. While loading, and after a failure, they are the system faces. Text reads them: it draws the body face, or the numbers face (falling back to body) when its className has tabular-nums; a font-mono, font-sans or font-serif class keeps the class's own font.

Props

PropTypeDefaultDescription
fontstring | null---The stored ui.font setting; missing or unknown means Auto
autoTypeSetTypeSetId'system'The set Auto resolves to
loadFonts(families: ThemeFontFamily[]) => Promise<void>---Registers the bundled faces of these families; without it the faces are taken as already registered
childrenReactNode---The app

useFontType

function useFontType(): FontState;

Returns the nearest provider's state. It never throws: with no provider above it, it returns set system, the system faces and status loaded.

Types

FontState

interface FontState {
  setId: TypeSetId;  // 'system' | 'instrument' | 'grotesk' | 'fraunces' | 'manrope'
  type: ThemeType;   // the faces to draw: body, num, display and displayItalic, each optional
  status: FontStatus;
}

setId is the resolved set even while it loads; type is that set's faces only once status is loaded.

FontStatus

type FontStatus = 'loading' | 'loaded' | 'failed';

FontProviderProps

The props in the table above.