Using Stylex with Theme hook #147
-
How can we use theme variables in stylex when we get our theme using a hook like, useTheme(). ` |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 9 replies
-
Please use the StyleX theme APIs. Short explanations. I'll use a theme of just a few colors as an example. First define the "default theme".Define all the variables you need along with default values in a file that ends with import * as stylex from '@stylexjs/stylex'
export const colors = stylex.defineVars({
primaryText: 'black',
secondaryText: 'rgb(60,60,60)',
primaryBg: 'white',
secondaryBg: 'rgb(200,200,200)',
}); Define a bunch of themes in a separate file:import * as stylex from '@stylexjs/stylex';
import {colors} from './vars.stylex';
export const dark = stylex.createTheme(colors, {
primaryText: 'white',
secondaryText: 'rgb(200,200,200)',
primaryBg: 'rgb(60,60,60)',
secondaryBg: 'black',
});
export const rainbow = stylex.createTheme(colors, {...});
export const blue = stylex.createTheme(colors, {...});
// ... Import and use variables directly from the
|
Beta Was this translation helpful? Give feedback.
-
Thought I'd share how we're handling this, based on what @nmn has shared in this thread: // hooks/useTheme.ts
import { useState } from "react";
export enum Theme {
LIGHT = "light",
DARK = "dark",
}
const useTheme = () => {
const [currentTheme, setCurrentTheme] = useState<Theme | null>(null);
// ...theme logic
return currentTheme;
};
export default useTheme; // utils/styles.ts
import * as stylex from "@stylexjs/stylex";
import { Theme } from "hooks/useTheme";
export const stylexThemed = <T extends Record<string, any>>(
key: keyof T,
styles: T,
darkStyles: T,
theme: Theme | null,
) => stylex.props(styles[key], theme === Theme.DARK ? darkStyles[key] : null); // components/MyComponent.tsx
import useTheme from "hooks/useTheme";
import { border } from "../../styles/border.stylex";
import { colors } from "../../styles/colors.stylex";
const styles = stylex.create({
container: {
// ...other styles
backgroundColor: colors.grey25,
borderColor: border.color,
},
});
const darkStyles = stylex.create({
container: {
// ...other styles
backgroundColor: colors.grey725,
borderColor: border.darkColor,
},
});
const MyComponent = () => {
const theme = useTheme();
return (
<div {...stylexThemed("container", styles, darkStyles, theme)}>
</div>
)
};
export default MyComponent; This is "semi" type safe as if Open to suggestions on ways to improve this (I prefer how stylex colocates the styles with |
Beta Was this translation helpful? Give feedback.
-
FYI, please see #464 to see the discussion about supporting Once you switch to using |
Beta Was this translation helpful? Give feedback.
-
@nmn is it possible to dynamically define variables or create a new theme? Basically we have a default colour that we use as the background colour for all elements (buttons, labels etc). Based on different user, we will need to replace the default background colour with the value returned from an API. Is there a way to achieve this? |
Beta Was this translation helpful? Give feedback.
Please use the StyleX theme APIs.
Short explanations. I'll use a theme of just a few colors as an example.
First define the "default theme".
Define all the variables you need along with default values in a file that ends with
.stylex.ts
.Define a bunch of themes in a separate file: