Skip to content

Commit 542806d

Browse files
committed
Move from polling to transition listeners approach for better performance (styles-removed bug remains)
1 parent 99f1688 commit 542806d

File tree

2 files changed

+49
-15
lines changed

2 files changed

+49
-15
lines changed

code-input.css

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ code-input {
2424
to keep low precedence and thus overridability
2525
The variables may change and are for internal use. */
2626
--code-input_highlight-text-color: black; /* Set by JS to be base text color of pre code element */
27-
color: var(--code-input_highlight-text-color, black);
27+
color: var(--code-input_highlight-text-color, black); /* Variable separate so can be overridden by CSS */
2828
--code-input_default-caret-color: black; /* Set by JS to be same as color property */
2929
caret-color: var(--code-input_default-caret-color, black); /* Set by JS to be equal to color property */
3030
background-color: white;
@@ -73,6 +73,12 @@ code-input textarea, code-input:not(.code-input_pre-element-styled) pre code, co
7373
code-input:not(.code-input_pre-element-styled) pre code, code-input.code-input_pre-element-styled pre {
7474
height: max-content;
7575
width: max-content;
76+
77+
78+
/* Allow colour change to reflect properly;
79+
transition-behavior: allow-discrete could be used but this is better supported and
80+
works with the color property. */
81+
transition: color 0.001s;
7682
}
7783

7884
code-input:not(.code-input_pre-element-styled) pre, code-input.code-input_pre-element-styled pre code {
@@ -169,6 +175,13 @@ code-input .code-input_dialog-container {
169175

170176
/* Dialog boxes' text is based on text-direction */
171177
text-align: inherit;
178+
179+
180+
/* Allow colour change to reflect properly;
181+
transition-behavior: allow-discrete could be used but this is better supported and
182+
works with the color property. */
183+
color: inherit;
184+
transition: color 0.001s;
172185
}
173186

174187
[dir=rtl] code-input .code-input_dialog-container, code-input[dir=rtl] .code-input_dialog-container {

code-input.js

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -506,18 +506,6 @@ var codeInput = {
506506
this.needsHighlight = false;
507507
}
508508

509-
// Synchronise colors
510-
if(this.textareaElement) {
511-
let color;
512-
if(this.templateObject.preElementStyled) {
513-
color = getComputedStyle(this.preElement).color;
514-
} else {
515-
color = getComputedStyle(this.codeElement).color;
516-
}
517-
this.style.setProperty("--code-input_highlight-text-color", color);
518-
}
519-
this.style.setProperty("--code-input_default-caret-color", getComputedStyle(this).color);
520-
521509
window.requestAnimationFrame(this.animateFrame.bind(this));
522510
}
523511

@@ -749,7 +737,6 @@ var codeInput = {
749737
this.codeElement = code;
750738
pre.append(code);
751739
this.append(pre);
752-
753740
if (this.templateObject.isCode) {
754741
if (lang != undefined && lang != "") {
755742
code.classList.add("language-" + lang.toLowerCase());
@@ -788,7 +775,41 @@ var codeInput = {
788775
// The only element that could be resized is this code-input element.
789776
this.syncSize();
790777
});
791-
resizeObserver.observe(this.textareaElement);
778+
resizeObserver.observe(this);
779+
780+
// Synchronise colors
781+
if(this.templateObject.preElementStyled) {
782+
this.preElement.addEventListener("transitionend", (evt) => {
783+
if(evt.propertyName == "color") {
784+
// So previous variable value does not affect new value:
785+
// (required to deal with color being no longer specified in CSS)
786+
this.style.removeProperty("--code-input_highlight-text-color");
787+
788+
console.log("pre", evt, getComputedStyle(this.preElement).color);
789+
this.style.setProperty("--code-input_highlight-text-color", getComputedStyle(this.preElement).color);
790+
}
791+
});
792+
} else {
793+
this.codeElement.addEventListener("transitionend", (evt) => {
794+
if(evt.propertyName == "color") {
795+
// So previous variable value does not affect new value:
796+
// (required to deal with color being no longer specified in CSS)
797+
this.style.removeProperty("--code-input_highlight-text-color");
798+
799+
console.log("code", evt, getComputedStyle(this.codeElement).color);
800+
this.style.setProperty("--code-input_highlight-text-color", getComputedStyle(this.codeElement).color);
801+
}
802+
});
803+
}
804+
// Not on this element so CSS transition property which must be set for
805+
// listener to work does not conflict with library-user transition property
806+
this.dialogContainerElement.addEventListener("transitionend", (evt) => {
807+
if(evt.propertyName == "color") {
808+
console.log("ci", evt, getComputedStyle(this).color);
809+
810+
this.style.setProperty("--code-input_default-caret-color", getComputedStyle(this).color);
811+
}
812+
});
792813

793814
this.classList.add("code-input_loaded");
794815
}

0 commit comments

Comments
 (0)