UNPKG

@blueprintjs/core

Version:

Core styles & components

58 lines • 2.64 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ThemeProvider = ThemeProvider; const jsx_runtime_1 = require("react/jsx-runtime"); /* * (c) Copyright 2026 Palantir Technologies Inc. All rights reserved. */ const react_1 = require("react"); const themeContext_1 = require("./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. */ function ThemeProvider({ children, theme, themesByVersion }) { const contextValue = (0, react_1.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 (0, jsx_runtime_1.jsx)(themeContext_1.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