@blueprintjs/core
Version:
Core styles & components
55 lines • 2.49 kB
JavaScript
import { jsx as _jsx } from "react/jsx-runtime";
/*
* (c) Copyright 2026 Palantir Technologies Inc. All rights reserved.
*/
import { useMemo } from "react";
import { BlueprintThemeContext } from "./themeContext";
const THEME_API_VERSION = 1;
const themeTokenMap = {
borderRadius: "--bp-surface-border-radius",
fontFamily: "--bp-typography-family-default",
};
/**
* Supplies converted Blueprint tokens without adding DOM elements. Consumers apply them to their existing roots.
*
* Use `theme` when authoring overrides against the `ThemeAPI` type exported by your installed Blueprint version.
* Use `themesByVersion` when the receiving Blueprint instance's supported Theme API version is unknown,
* such as when a host forwards overrides to an independently deployed widget. Provide representations keyed
* by schema version; the receiving provider selects the version it supports.
* When `theme` is defined, it is used exclusively; an invalid value clears overrides. The matching
* `themesByVersion` entry is used only when `theme` is undefined. Omitting both clears overrides.
*/
export function ThemeProvider({ children, theme, themesByVersion }) {
const contextValue = useMemo(() => {
const selectedTheme = theme === undefined ? themesByVersion?.[THEME_API_VERSION] : theme;
const validTheme = isThemeValid(selectedTheme) ? selectedTheme : undefined;
return {
theme: validTheme,
tokens: validTheme == null ? undefined : customThemeToBlueprintTokens(validTheme),
};
}, [theme, themesByVersion]);
return _jsx(BlueprintThemeContext.Provider, { value: contextValue, children: children });
}
function customThemeToBlueprintTokens(theme) {
const tokens = {};
for (const key of Object.keys(themeTokenMap)) {
const value = theme[key];
if (value != null) {
tokens[themeTokenMap[key]] = value;
}
}
return tokens;
}
function isThemeValid(value) {
if (typeof value !== "object" || value === null || Array.isArray(value)) {
return false;
}
const theme = value;
return (theme.version === THEME_API_VERSION &&
(theme.borderRadius === undefined || isPixelString(theme.borderRadius)) &&
(theme.fontFamily === undefined || typeof theme.fontFamily === "string"));
}
function isPixelString(value) {
return typeof value === "string" && /^(?:0|[1-9]\d*)(?:\.\d+)?px$/.test(value);
}
//# sourceMappingURL=themeProvider.js.map