useTheme

Read the theme's colours, font, settings and text direction.

Use useTheme when a component needs the merchant's theme choices: brand colours, font, your own theme settings or the text direction.

Example

app/components/common/Badge.tsx
import { useTheme } from '@salla.sa/twilight-theme-engine/hooks/useTheme';

export function Badge({ children }: { children: React.ReactNode }) {
  const { color, isRTL } = useTheme();
  return (
    <span
      dir={isRTL ? 'rtl' : 'ltr'}
      style={{ background: color.primary, color: color.reverse_text }}
    >
      {children}
    </span>
  );
}

API

function useTheme(): UseThemeResult;
PropTypeDescription
colorThemeColorTheme colours.
color.primarystringPrimary colour.
color.textstringText colour.
color.reverse_primarystringThe reverse counterpart of the primary colour.
color.reverse_textstringText colour to use on top of the primary colour, such as on a primary-coloured button.
color.is_darkbooleanWhether the theme is dark.
fontThemeFont | undefinedThe selected font. Can be missing.
settingsThemeSettingsThe values of your theme settings.
isRTLbooleantrue when the theme reads right to left.

Gotchas

  • font can be undefined, so check it before reading font.name.
  • Works during server rendering and in the browser. The data comes from the provider at the root of the app.
  • On a primary-coloured background, use reverse_text for the text so it stays readable.
  • For the direction of the current language, useTranslation also returns direction, isRTL and isLTR.

Next

  • Theme data: every field of the object this hook returns, with example values.
  • useStore reads the store's data.
  • Styling shows how to apply colours with CSS.

On this page