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
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;| Prop | Type | Description |
|---|---|---|
color | ThemeColor | Theme colours. |
color.primary | string | Primary colour. |
color.text | string | Text colour. |
color.reverse_primary | string | The reverse counterpart of the primary colour. |
color.reverse_text | string | Text colour to use on top of the primary colour, such as on a primary-coloured button. |
color.is_dark | boolean | Whether the theme is dark. |
font | ThemeFont | undefined | The selected font. Can be missing. |
settings | ThemeSettings | The values of your theme settings. |
isRTL | boolean | true when the theme reads right to left. |
Gotchas
fontcan beundefined, so check it before readingfont.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_textfor the text so it stays readable. - For the direction of the current language,
useTranslationalso returnsdirection,isRTLandisLTR.
Next
- Theme data: every field of the object this hook returns, with example values.
useStorereads the store's data.- Styling shows how to apply colours with CSS.