@ai-growth/nextjs
Version:
Seamlessly integrate Sanity CMS with Next.js applications for automated blog routing and rendering
163 lines (162 loc) • 7.62 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.ThemeContext = void 0;
exports.ThemeProvider = ThemeProvider;
exports.useThemeContext = useThemeContext;
exports.useThemeAccessor = useThemeAccessor;
exports.useThemeValue = useThemeValue;
exports.CssOnlyThemeProvider = CssOnlyThemeProvider;
exports.withTheme = withTheme;
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = __importStar(require("react"));
const themeAdapters_1 = require("../utils/themeAdapters");
exports.ThemeContext = react_1.default.createContext(null);
/**
* Enhanced ThemeProvider component that injects CSS custom properties
* and provides theme accessor utilities
*/
function ThemeProvider({ theme, children, selector = ':root', cssPrefix = '--cms', customProperties = {}, adapterOptions = {}, inline = false, className, enableTransitions = false, }) {
// Create theme accessor for child components
const accessor = (0, react_1.useMemo)(() => {
return (0, themeAdapters_1.createThemeAccessor)(theme, cssPrefix);
}, [theme, cssPrefix]);
// Generate CSS custom properties from theme
const cssVariables = (0, react_1.useMemo)(() => {
const adapter = (0, themeAdapters_1.createCssVariablesAdapter)(theme, {
cssPrefix,
...adapterOptions,
});
const themeVars = adapter.toCssVariables();
return { ...themeVars, ...customProperties };
}, [theme, cssPrefix, customProperties, adapterOptions]);
// Generate CSS string for injection
const cssString = (0, react_1.useMemo)(() => {
const declarations = Object.entries(cssVariables)
.map(([property, value]) => ` ${property}: ${value};`)
.join('\n');
const transitionDeclaration = enableTransitions
? ' transition: color 0.2s ease, background-color 0.2s ease, border-color 0.2s ease, box-shadow 0.2s ease;\n'
: '';
return `${selector} {\n${transitionDeclaration}${declarations}\n}`;
}, [cssVariables, selector, enableTransitions]);
// Context value
const contextValue = (0, react_1.useMemo)(() => ({
theme,
accessor,
cssPrefix,
customProperties: cssVariables,
}), [theme, accessor, cssPrefix, cssVariables]);
// Inline styles for direct application
const inlineStyles = (0, react_1.useMemo)(() => {
if (!inline)
return undefined;
const styles = {};
Object.entries(cssVariables).forEach(([property, value]) => {
// Keep CSS custom properties as-is for inline styles
styles[property] = value;
});
if (enableTransitions) {
styles.transition = 'color 0.2s ease, background-color 0.2s ease, border-color 0.2s ease, box-shadow 0.2s ease';
}
return styles;
}, [cssVariables, inline, enableTransitions]);
return ((0, jsx_runtime_1.jsxs)(exports.ThemeContext.Provider, { value: contextValue, children: [!inline && ((0, jsx_runtime_1.jsx)("style", { "data-theme-provider": "true", children: cssString })), (0, jsx_runtime_1.jsx)("div", { className: className, style: inlineStyles, "data-theme-container": inline ? 'true' : undefined, children: children })] }));
}
/**
* Hook to access theme context with type safety
*/
function useThemeContext() {
const context = react_1.default.useContext(exports.ThemeContext);
if (!context) {
throw new Error('useThemeContext must be used within a ThemeProvider');
}
return context;
}
/**
* Hook to access theme accessor utilities
*/
function useThemeAccessor() {
const { accessor } = useThemeContext();
return accessor;
}
/**
* Hook to get theme values with built-in fallbacks
*/
function useThemeValue() {
const { accessor } = useThemeContext();
return {
color: (key, fallback) => accessor.color(key, fallback),
spacing: (key, fallback) => accessor.spacing(key, fallback),
typography: (category, key, fallback) => accessor.typography(category, key, fallback),
borderRadius: (key, fallback) => accessor.borderRadius(key, fallback),
shadow: (key, fallback) => accessor.shadow(key, fallback),
breakpoint: (key, fallback) => accessor.breakpoint(key, fallback),
cssVar: (category, key) => accessor.cssVar(category, key),
cssVarWithFallback: (category, key, fallback) => accessor.cssVarWithFallback(category, key, fallback),
};
}
/**
* Lightweight ThemeProvider for CSS-only applications
* (no React Context, just CSS injection)
*/
function CssOnlyThemeProvider({ theme, children, selector = ':root', cssPrefix = '--cms', customProperties = {}, adapterOptions = {}, enableTransitions = false, }) {
const cssString = (0, react_1.useMemo)(() => {
const adapter = (0, themeAdapters_1.createCssVariablesAdapter)(theme, {
cssPrefix,
...adapterOptions,
});
const themeVars = adapter.toCssVariables();
const allVars = { ...themeVars, ...customProperties };
const declarations = Object.entries(allVars)
.map(([property, value]) => ` ${property}: ${value};`)
.join('\n');
const transitionDeclaration = enableTransitions
? ' transition: color 0.2s ease, background-color 0.2s ease, border-color 0.2s ease, box-shadow 0.2s ease;\n'
: '';
return `${selector} {\n${transitionDeclaration}${declarations}\n}`;
}, [theme, selector, cssPrefix, customProperties, adapterOptions, enableTransitions]);
return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)("style", { "data-css-only-theme-provider": "true", children: cssString }), children] }));
}
/**
* Higher-order component to wrap components with ThemeProvider
*/
function withTheme(Component, themeProviderProps) {
const displayName = Component.displayName || Component.name || 'Component';
const WithThemeComponent = (props) => ((0, jsx_runtime_1.jsx)(ThemeProvider, { ...themeProviderProps, children: (0, jsx_runtime_1.jsx)(Component, { ...props }) }));
WithThemeComponent.displayName = `withTheme(${displayName})`;
return WithThemeComponent;
}
exports.default = ThemeProvider;