DEV Community

Kinga
Kinga

Posted on • Edited on

Get correct theme colors in SPFx workbench

If you are building a SPFx extension and try to use theme colors, you probably already noticed that when debugging, the getTheme().palette will always return default, blue, palette (when deployed, the result is correct).
This results in rather hilarious and questionable design:

const stackStyles: Partial<IStackStyles> = {
    root: {
        borderColor: theme.palette.themePrimary,
        backgroundColor: theme.palette.themeLight,
        ...
    },
}
Enter fullscreen mode Exit fullscreen mode

Image description

The trick is to get the theme from the window.__themeState__.theme as mentioned in here (scroll down to find Note section).

However... I never trust objects or methods whose names start with _ and since getTheme() works just fine in production, I also see no reason for this workaround to be used permanently.

The solution

I'm defining all the component styles in a separate file, ComponentStyles.ts. In here, I access the __themeState__ only if debugging

ComponentStyles.ts

let siteTheme: ITheme = null;

if (process.env.NODE_ENV !== 'production') {
    siteTheme = createTheme({
        // eslint-disable-next-line @typescript-eslint/no-explicit-any
        palette: (window as any).__themeState__.theme
    });
}
//if siteTheme is undefined, use the default theme
const theme = siteTheme ?? getTheme();

const stackStyles: Partial<IStackStyles> = {
    root: {
        borderColor: theme.palette.themePrimary,
        backgroundColor: theme.palette.themeLight,
        ...
    },
}

export { stackStyles };
Enter fullscreen mode Exit fullscreen mode

The solution components will only import the required styles, and the colors are correct in both, workbench and production environments.

DatePickerMenu.tsx

import { stackStyles } from "../../common/ComponentStyles";

const DatePickerMenu: React.FunctionComponent<IDatePickerMenuProps> = (props) => {
    ...
    return (
        <Stack
            horizontal={true}
            styles={stackStyles}
        >
        </Stack>
    );
};
}
Enter fullscreen mode Exit fullscreen mode

Image description

Much better, isn't it? Now I can style my components seeing the correct result.
And at the same time I don't have to remember to remove the __themeState__ before releasing the solution.

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay