@yamada-ui/react
Version:
React UI components of the Yamada, by the Yamada, for the Yamada built with React and Emotion
168 lines (166 loc) • 6.99 kB
JavaScript
const require_utils_index = require('../../utils/index.cjs');
const require_breakpoint = require('./breakpoint.cjs');
const require_constant = require('../constant.cjs');
const require_layer = require('./layer.cjs');
const require_var = require('./var.cjs');
//#region src/core/system/create-system.ts
const tokenMap = {
primary: {
aspectRatios: {},
blurs: {},
borders: {},
colors: {},
durations: {},
easings: {},
fonts: {},
fontSizes: {},
fontWeights: {},
letterSpacings: {},
lineHeights: {},
radii: {},
sizes: {},
spaces: {},
zIndices: {}
},
secondary: {
gradients: {},
keyframes: { maxDepth: 1 },
shadows: {}
},
tertiary: { animations: { shouldProcess: (obj) => !obj.keyframes } }
};
const defaultSystem = {
breakpoints: require_breakpoint.createBreakpoints(),
config: {},
cssMap: {},
cssVars: {},
layers: require_layer.createLayers(),
utils: { getClassName: require_utils_index.utils_exports.bem }
};
function createSystem(theme, config = {}) {
const prefix = config.css?.varPrefix ?? require_constant.DEFAULT_VAR_PREFIX;
const breakpoints = require_breakpoint.createBreakpoints(theme.breakpoints, config.breakpoint);
const layers = require_layer.createLayers(config.css?.layers);
const shouldProcess = config.theme?.responsive ? (obj) => !breakpoints.isResponsive(obj, true) : () => true;
const primaryTokens = {
...createTokens(theme, "primary", shouldProcess),
...createColorSchemeTokens(theme, void 0, shouldProcess)
};
const secondaryTokens = createTokens(theme, "secondary", shouldProcess);
const tertiaryTokens = createTokens(theme, "tertiary", shouldProcess);
const { cssMap, cssVars } = require_var.mergeVars(require_var.createVars(prefix, theme, breakpoints)(primaryTokens), require_var.createVars(prefix, theme, breakpoints)(secondaryTokens), require_var.createVars(prefix, theme, breakpoints)(tertiaryTokens))();
const getClassName = (block, element, modifier) => {
if (!block) return "";
return `${prefix}-${(0, require_utils_index.utils_exports.bem)(block, element, modifier)}`;
};
if (theme.themeSchemes) {
const themeSchemeEntries = Object.entries(theme.themeSchemes);
for (const [themeScheme, nestedTheme] of themeSchemeEntries) {
const themeCondition = `[data-theme=${themeScheme}] &:not([data-theme]), &[data-theme=${themeScheme}]`;
const nestedPrimaryTokens = {
...createTokens(nestedTheme, "primary", shouldProcess),
...createColorSchemeTokens(theme, nestedTheme, shouldProcess)
};
const nestedSecondaryTokens = createTokens(nestedTheme, "secondary", shouldProcess);
const nestedTertiaryTokens = createTokens(nestedTheme, "tertiary", shouldProcess);
const { cssVars: nestedCSSVars } = require_var.mergeVars(require_var.createVars(prefix, theme, breakpoints)(nestedPrimaryTokens), require_var.createVars(prefix, theme, breakpoints)(nestedSecondaryTokens), require_var.createVars(prefix, theme, breakpoints)(nestedTertiaryTokens))({
...primaryTokens,
...secondaryTokens,
...tertiaryTokens
});
cssVars[themeCondition] = nestedCSSVars;
}
}
return {
breakpoints,
config,
cssMap,
cssVars,
layers,
utils: { getClassName }
};
}
function createColorSchemeTokens(theme, nestedTheme, shouldProcess) {
const colors = {
base: theme.colors ?? {},
nested: nestedTheme?.colors ?? {}
};
const semanticColors = {
base: theme.semanticTokens?.colors ?? {},
nested: nestedTheme?.semanticTokens?.colors ?? {}
};
const colorSchemeTokens = (0, require_utils_index.utils_exports.flattenObject)((nestedTheme ?? theme).semanticTokens?.colorSchemes ?? {}, { shouldProcess });
const results = {};
function insertToken(primaryKey, secondaryKey, value) {
if ((0, require_utils_index.utils_exports.isUndefined)(value)) return;
if (!secondaryKey || secondaryKey === "base") results[`colors.${primaryKey}`] = {
semantic: true,
value
};
else results[`colors.${primaryKey}.${secondaryKey}`] = {
semantic: true,
value
};
}
function processValue(primaryKey, colors$1, keyOrValue) {
const value = colors$1.nested[keyOrValue] ?? colors$1.base[keyOrValue];
if ((0, require_utils_index.utils_exports.isObject)(value)) {
const tokens = (0, require_utils_index.utils_exports.flattenObject)(value, { shouldProcess });
Object.keys(tokens).forEach((secondaryKey) => {
insertToken(primaryKey, secondaryKey, secondaryKey === "base" ? keyOrValue : `${keyOrValue}.${secondaryKey}`);
});
} else insertToken(primaryKey, void 0, keyOrValue);
}
function processColorModeValue(primaryKey, colors$1, keyOrValue) {
const [lightValue, darkValue] = keyOrValue;
const lightColors = colors$1.nested[lightValue] ?? colors$1.base[lightValue];
const darkColors = colors$1.nested[darkValue] ?? colors$1.base[darkValue];
if ((0, require_utils_index.utils_exports.isObject)(lightColors) && (0, require_utils_index.utils_exports.isObject)(darkColors)) {
const tokens = (0, require_utils_index.utils_exports.flattenObject)(lightColors, { shouldProcess });
Object.keys(tokens).forEach((secondaryKey) => {
insertToken(primaryKey, secondaryKey, [secondaryKey === "base" ? lightValue : `${lightValue}.${secondaryKey}`, secondaryKey === "base" ? darkValue : `${darkValue}.${secondaryKey}`]);
});
} else if (!(0, require_utils_index.utils_exports.isObject)(lightValue) && !(0, require_utils_index.utils_exports.isObject)(darkValue)) insertToken(primaryKey, void 0, [lightValue, darkValue]);
}
Object.entries(colorSchemeTokens).forEach(([primaryKey, value]) => {
if ((0, require_utils_index.utils_exports.isArray)(value)) {
processColorModeValue(primaryKey, colors, value);
processColorModeValue(primaryKey, semanticColors, value);
} else {
processValue(primaryKey, colors, value);
processValue(primaryKey, semanticColors, value);
}
});
return results;
}
function createTokens(theme, target, shouldProcess = () => true) {
const results = {};
Object.entries(tokenMap[target]).forEach(([primaryKey, { shouldProcess: shouldProcessProp,...rest }]) => {
const options = {
...rest,
shouldProcess: (obj) => shouldProcess(obj) && (!shouldProcessProp || shouldProcessProp(obj))
};
const tokens = (0, require_utils_index.utils_exports.flattenObject)(theme[primaryKey] ?? {}, options);
const semanticTokens = (0, require_utils_index.utils_exports.flattenObject)(theme.semanticTokens?.[primaryKey] ?? {}, options);
Object.entries(tokens).forEach(([secondaryKey, value]) => {
const token = `${primaryKey}.${secondaryKey}`;
results[token] = {
semantic: false,
value
};
});
Object.entries(semanticTokens).forEach(([secondaryKey, value]) => {
let token = `${primaryKey}.${secondaryKey}`;
if (token.endsWith(".base")) token = token.replace(".base", "");
results[token] = {
semantic: true,
value
};
});
});
return results;
}
//#endregion
exports.createSystem = createSystem;
exports.defaultSystem = defaultSystem;
//# sourceMappingURL=create-system.cjs.map