Skip to Content
Hooks

useKivaHostTheme / useKivaHostLocale

Follow the Kiva shell's colour mode and language from inside an embedded app, so a theme or language switch in the shell reaches the app it is hosting.

An app embedded in the Kiva shell runs in its own iframe, so it does not inherit the shell's theme or language. Without these hooks the user switches the shell to dark mode or to Turkish and every embedded app stays light and English.

Both hooks are no-ops outside an embedded context, so they are safe to mount unconditionally in an app that also runs standalone.

Installation

pnpm dlx @kivacore/ui-cli add @kivacore/hooks-use-kiva-host-appearance

Usage

The hooks only report the host's value — you apply it, so the library stays independent of which theming or i18n package you use.

import { useTheme } from '@kivacore/theme-provider';
import { useTranslation } from 'react-i18next';

import {
  useKivaHostTheme,
  useKivaHostLocale
} from '@/hooks/use-kiva-host-appearance';

export function useShellAppearance() {
  const { setTheme } = useTheme();
  const { i18n } = useTranslation();

  useKivaHostTheme(setTheme);
  useKivaHostLocale(locale => void i18n.changeLanguage(locale));
}

Mount it once, near the root:

function App() {
  useShellAppearance();

  return <Routes>{/* … */}</Routes>;
}

Keeping component copy in step

If you also mount a translation catalog, swap it in the same handler so Kiva component copy follows the shell too:

useKivaHostLocale(async (locale) => {
  void i18n.changeLanguage(locale);
  setMessages((await import(`@/locales/kiva/${locale}.json`)).default);
});

How the shell delivers it

Two channels, by design:

CarriesWhy
?theme= / ?lang= on the iframe URLthe value the app boots withthe first paint is already correct — no flash of the wrong theme
theme-change / locale-change postMessageevery later switchchanging the URL instead would reload the iframe and lose the app's state

The hooks read the query params on mount and listen for the messages afterwards, so you only wire the handler once.

API Reference

useKivaHostTheme(onThemeChange, options?)

ParameterTypeDescription
onThemeChange(mode: KivaHostThemeMode) => voidCalled with the host's colour mode — on mount when the URL carries one, and on every later change
optionsKivaHostAppearanceOptionsOrigin policy overrides

useKivaHostLocale(onLocaleChange, options?)

ParameterTypeDescription
onLocaleChange(locale: string) => voidCalled with the host's active language code
optionsKivaHostAppearanceOptionsOrigin policy overrides

isKivaHostEmbedded()

Returns true when the document is inside an iframe or a React Native WebView. Both hooks use it internally; it is exported so you can branch on it yourself (hiding an in-app theme switcher when the shell owns the theme, for example).

Type Reference

KivaHostThemeMode

type KivaHostThemeMode = 'light' | 'dark' | 'system';

KivaHostAppearanceOptions

FieldTypeDefaultDescription
allowedOriginsstring[][]Extra origins to trust, beyond the pattern — a local shell or a staging host
allowedOriginPatternRegExp/^https:\/\/[^/]+\.(?:kiva|docyrus)\.app$/Host origins to accept messages from

The default origin policy is the same one @kivacore/signin applies to auth messages. Widen it only for an origin you control — a message from an untrusted frame would otherwise be able to drive your app's theme and language.

On this page