Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(brand): Support additional bootstrap layers in defaults #11378

Merged
merged 3 commits into from
Nov 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 45 additions & 17 deletions src/core/sass/brand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,25 +202,37 @@ const brandColorLayer = (
};
};

type BootstrapDefaultsConfig = {
uses?: string;
functions?: string;
defaults?: Record<string, Record<string, string | boolean | number | null>>;
mixins?: string;
rules?: string;
};

const brandDefaultsBootstrapLayer = (
brand: Brand,
): SassLayer => {
// Bootstrap Variables from brand.defaults.bootstrap
const brandBootstrap = brand?.data?.defaults?.bootstrap as unknown as Record<
string,
Record<string, string | boolean | number | null>
>;
const brandBootstrap = brand?.data?.defaults
?.bootstrap as unknown as BootstrapDefaultsConfig;

const bsVariables: string[] = [
"/* Bootstrap variables from _brand.yml */",
'// quarto-scss-analysis-annotation { "action": "push", "origin": "_brand.yml defaults.bootstrap" }',
'// quarto-scss-analysis-annotation { "action": "push", "origin": "_brand.yml defaults.bootstrap.defaults" }',
];
for (const bsVar of Object.keys(brandBootstrap)) {
if (bsVar === "version") {
continue;
const bsDefaults = brandBootstrap.defaults || "";
if (typeof bsDefaults === "string") {
bsVariables.push(bsDefaults);
} else if (typeof bsDefaults === "object") {
for (const bsVar of Object.keys(bsDefaults)) {
bsVariables.push(`$${bsVar}: ${bsDefaults[bsVar]} !default;`);
}
bsVariables.push(
`$${bsVar}: ${brandBootstrap[bsVar]} !default;`,
} else {
throw new Error(
"Invalid bootstrap defaults in _brand.yml or `brand`. " +
"`defaults.bootstrap.defaults` expects a string or a dictionary " +
"mapping Sass variables to default values."
);
}
bsVariables.push('// quarto-scss-analysis-annotation { "action": "pop" }');
Expand Down Expand Up @@ -253,20 +265,36 @@ const brandDefaultsBootstrapLayer = (
continue;
}

bsColors.push(
`$${colorKey}: ${brand.getColor(colorKey)} !default;`,
);
bsColors.push(`$${colorKey}: ${brand.getColor(colorKey)} !default;`);
}
}

bsColors.push('// quarto-scss-analysis-annotation { "action": "pop" }');

const scssWithQuartoAnnotation = (
x: string | undefined,
origin: string
): string => {
if (!x) {
return "";
}

return [
`// quarto-scss-analysis-annotation { "action": "push", "origin": "_brand.yml defaults.bootstrap.${origin}" }`,
x,
'// quarto-scss-analysis-annotation { "action": "pop" }',
].join("\n");
};

return {
defaults: bsColors.join("\n") + "\n" + bsVariables.join("\n"),
uses: "",
functions: "",
mixins: "",
rules: "",
uses: scssWithQuartoAnnotation(brandBootstrap.uses, "uses"),
functions: scssWithQuartoAnnotation(
brandBootstrap.functions,
"functions"
),
mixins: scssWithQuartoAnnotation(brandBootstrap.mixins, "mixins"),
rules: scssWithQuartoAnnotation(brandBootstrap.rules, "rules"),
};
};

Expand Down
Loading