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

Fix CSS custom property precedence issue #6907

Closed
Closed
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "Fix CSS custom property precedence issue",
"packageName": "@microsoft/fast-element",
"email": "7282195+m-akinc@users.noreply.github.com",
"dependentChangeType": "prerelease"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "Fix CSS custom property precedence issue",
"packageName": "@microsoft/fast-foundation",
"email": "7282195+m-akinc@users.noreply.github.com",
"dependentChangeType": "prerelease"
}
3 changes: 3 additions & 0 deletions packages/web-components/fast-element/docs/api-report.md
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,9 @@ export interface PartialHTMLDirectiveDefinition {
aspected?: boolean;
}

// @public
export const prependToAdoptedStyleSheetsSymbol: unique symbol;

// @public
export class PropertyChangeNotifier implements Notifier {
constructor(subject: any);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -645,8 +645,27 @@ export class StyleElementStrategy implements StyleStrategy {
}
}

/**
* A Symbol that can be added to a CSSStyleSheet to cause it to be prepended (rather than appended) to adoptedStyleSheets.
* @public
*/
export const prependToAdoptedStyleSheetsSymbol = Symbol("prependToAdoptedStyleSheets");

function separateSheetsToPrepend(sheets: CSSStyleSheet[]): {
prepend: CSSStyleSheet[];
append: CSSStyleSheet[];
} {
const prepend: CSSStyleSheet[] = [];
const append: CSSStyleSheet[] = [];
sheets.forEach(x =>
(x[prependToAdoptedStyleSheetsSymbol] ? prepend : append).push(x)
);
return { prepend, append };
}

let addAdoptedStyleSheets = (target: Required<StyleTarget>, sheets: CSSStyleSheet[]) => {
target.adoptedStyleSheets = [...target.adoptedStyleSheets!, ...sheets];
const { prepend, append } = separateSheetsToPrepend(sheets);
target.adoptedStyleSheets = [...prepend, ...target.adoptedStyleSheets!, ...append];
};
let removeAdoptedStyleSheets = (
target: Required<StyleTarget>,
Expand All @@ -666,7 +685,9 @@ if (ElementStyles.supportsAdoptedStyleSheets) {
(document as any).adoptedStyleSheets.push();
(document as any).adoptedStyleSheets.splice();
addAdoptedStyleSheets = (target, sheets) => {
target.adoptedStyleSheets.push(...sheets);
const { prepend, append } = separateSheetsToPrepend(sheets);
target.adoptedStyleSheets.splice(0, 0, ...prepend);
target.adoptedStyleSheets.push(...append);
};
removeAdoptedStyleSheets = (target, sheets) => {
for (const sheet of sheets) {
Expand Down
1 change: 1 addition & 0 deletions packages/web-components/fast-element/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,5 @@ export {
export {
ElementController,
ElementControllerStrategy,
prependToAdoptedStyleSheetsSymbol,
} from "./components/element-controller.js";
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ import type {
ElementController,
FASTElement,
} from "@microsoft/fast-element";
import { ElementStyles, observable, Observable, Updates } from "@microsoft/fast-element";
import {
ElementStyles,
observable,
Observable,
prependToAdoptedStyleSheetsSymbol,
Updates,
} from "@microsoft/fast-element";

/**
* A target that can have key/value pairs set and removed.
Expand Down Expand Up @@ -33,6 +39,7 @@ class ConstructableStyleSheetTarget extends QueuedStyleSheetTarget {
super();

const sheet = new CSSStyleSheet();
sheet[prependToAdoptedStyleSheetsSymbol] = true;
this.target = (sheet.cssRules[sheet.insertRule(":host{}")] as CSSStyleRule).style;
source.$fastController.addStyles(new ElementStyles([sheet]));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1061,6 +1061,25 @@ test.describe("A DesignToken", () => {
})
).toBe("12px");
});
test("should allow stylesheet to override token's CSS custom property", async () => {
expect(
await page.evaluate(async () => {
const target = addElement();
const token = DesignToken.create<number>(uniqueTokenName());
const styles = css`
:host {
${token.cssCustomProperty}: 34;
width: calc(${token} * 1px);
}
`;
target.$fastController.addStyles(styles);
token.setValueFor(target, 12);

await Updates.next();
return window.getComputedStyle(target).getPropertyValue("width");
})
).toBe("34px");
});
});

test.describe("with a default value set", () => {
Expand Down
Loading