Skip to content

Commit d577a1f

Browse files
authored
fix: Fix preview on css variables defined on the current element (#4705)
## Description probably somehow related to #4448 ## Steps for reproduction define --color variable at :root and on current element. See background or color preview on the current element. ## Code Review - [ ] hi @kof, I need you to do - conceptual review (architecture, feature-correctness) - detailed review (read every line) - test it on preview ## Before requesting a review - [ ] made a self-review - [ ] added inline comments where things may be not obvious (the "why", not "what") ## Before merging - [ ] tested locally and on preview environment (preview dev login: 0000) - [ ] updated [test cases](https://github.com/webstudio-is/webstudio/blob/main/apps/builder/docs/test-cases.md) document - [ ] added tests - [ ] if any new env variables are added, added them to `.env` file
1 parent d07cef8 commit d577a1f

File tree

1 file changed

+29
-4
lines changed

1 file changed

+29
-4
lines changed

apps/builder/app/canvas/shared/styles.ts

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import {
3232
$instances,
3333
$props,
3434
$registeredComponentMetas,
35+
$selectedInstanceSelector,
3536
$selectedStyleState,
3637
$styleSourceSelections,
3738
$styles,
@@ -43,6 +44,7 @@ import { canvasApi } from "~/shared/canvas-api";
4344
import { $selectedInstance, $selectedPage } from "~/shared/awareness";
4445
import { findAllEditableInstanceSelector } from "~/shared/instance-utils";
4546
import type { InstanceSelector } from "~/shared/tree-utils";
47+
import { getVisibleElementsByInstanceSelector } from "~/shared/dom-utils";
4648

4749
const userSheet = createRegularStyleSheet({ name: "user-styles" });
4850
const stateSheet = createRegularStyleSheet({ name: "state-styles" });
@@ -580,18 +582,26 @@ const subscribeStateStyles = () => {
580582

581583
const subscribeEphemeralStyle = () => {
582584
// track custom properties added on previous ephemeral update
583-
const appliedEphemeralDeclarations = new Map<string, StyleDecl>();
585+
const appliedEphemeralDeclarations = new Map<
586+
string,
587+
[StyleDecl, HTMLElement[]]
588+
>();
584589

585590
return $ephemeralStyles.subscribe((ephemeralStyles) => {
586591
const instance = $selectedInstance.get();
587-
if (instance === undefined) {
592+
const instanceSelector = $selectedInstanceSelector.get();
593+
594+
if (instance === undefined || instanceSelector === undefined) {
588595
return;
589596
}
590597

591598
// reset ephemeral styles
592599
if (ephemeralStyles.length === 0) {
593600
canvasApi.resetInert();
594-
for (const styleDecl of appliedEphemeralDeclarations.values()) {
601+
for (const [
602+
styleDecl,
603+
elements,
604+
] of appliedEphemeralDeclarations.values()) {
595605
// prematurely apply last known ephemeral update to user stylesheet
596606
// to avoid lag because of delay between deleting ephemeral style
597607
// and sending style patch (and rendering)
@@ -606,6 +616,10 @@ const subscribeEphemeralStyle = () => {
606616
document.documentElement.style.removeProperty(
607617
getEphemeralProperty(styleDecl)
608618
);
619+
620+
for (const element of elements) {
621+
element.style.removeProperty(getEphemeralProperty(styleDecl));
622+
}
609623
}
610624
userSheet.setTransformer($transformValue.get());
611625
userSheet.render();
@@ -624,6 +638,17 @@ const subscribeEphemeralStyle = () => {
624638
getEphemeralProperty(styleDecl),
625639
toValue(styleDecl.value, $transformValue.get())
626640
);
641+
642+
// We need to apply the custom property to the selected element as well.
643+
// Otherwise, variables defined on it will not be visible on documentElement.
644+
const elements = getVisibleElementsByInstanceSelector(instanceSelector);
645+
for (const element of elements) {
646+
element.style.setProperty(
647+
getEphemeralProperty(styleDecl),
648+
toValue(styleDecl.value, $transformValue.get())
649+
);
650+
}
651+
627652
// render temporary rule for instance with var()
628653
// rendered with "all" breakpoint and without state
629654
// to reflect changes in canvas without user interaction
@@ -655,7 +680,7 @@ const subscribeEphemeralStyle = () => {
655680
});
656681
}
657682
}
658-
appliedEphemeralDeclarations.set(styleDeclKey, styleDecl);
683+
appliedEphemeralDeclarations.set(styleDeclKey, [styleDecl, elements]);
659684
}
660685
// avoid stylesheet rerendering on every ephemeral update
661686
if (ephemetalSheetUpdated) {

0 commit comments

Comments
 (0)