Files
webstudio/packages/react-sdk/src/css/css.ts
T
Bogdan Chadkin 7e440df235 Move css generation from project to sdk (#1354)
Generation of css text relies on components and not related to project
management so better move to sdk.

Also need to fix in saas
(https://github.com/webstudio-is/webstudio-saas/issues/126)
2023-04-03 19:30:45 +07:00

58 lines
1.8 KiB
TypeScript

import { createCssEngine } from "@webstudio-is/css-engine";
import type { Asset } from "@webstudio-is/asset-uploader";
import type { Build } from "@webstudio-is/project-build";
import { getComponentNames } from "../components/components-utils";
import { getComponentMeta } from "../components";
import { componentAttribute, idAttribute } from "../tree";
import { addGlobalRules } from "./global-rules";
import { getStyleRules } from "./style-rules";
type Data = {
assets: Asset[];
breakpoints?: Build["breakpoints"];
styles?: Build["styles"];
styleSourceSelections?: Build["styleSourceSelections"];
};
export const generateCssText = (data: Data) => {
const assets = new Map<Asset["id"], Asset>(
data.assets.map((asset) => [asset.id, asset])
);
const breakpoints = new Map(data.breakpoints);
const styles = new Map(data.styles);
const styleSourceSelections = new Map(data.styleSourceSelections);
const engine = createCssEngine({ name: "ssr" });
addGlobalRules(engine, { assets });
for (const breakpoint of breakpoints.values()) {
engine.addMediaRule(breakpoint.id, breakpoint);
}
for (const component of getComponentNames()) {
const meta = getComponentMeta(component);
const presetStyle = meta?.presetStyle;
if (presetStyle !== undefined) {
for (const [tag, style] of Object.entries(presetStyle)) {
engine.addStyleRule(
`${tag}:where([${componentAttribute}=${component}])`,
{
style,
}
);
}
}
}
const styleRules = getStyleRules(styles, styleSourceSelections);
for (const { breakpointId, instanceId, style } of styleRules) {
engine.addStyleRule(`[${idAttribute}="${instanceId}"]`, {
breakpoint: breakpointId,
style,
});
}
return engine.cssText;
};