Skip to content

Commit 99f1688

Browse files
committed
Make caret and placeholder use highlighted text's base colour or preferably color CSS of code-input element
1 parent f4d4067 commit 99f1688

2 files changed

Lines changed: 26 additions & 9 deletions

File tree

code-input.css

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,13 @@ code-input {
2020
top: 0;
2121
left: 0;
2222

23-
color: black;
23+
/* CSS variables rather than inline styles used for values synced from JavaScript
24+
to keep low precedence and thus overridability
25+
The variables may change and are for internal use. */
26+
--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);
28+
--code-input_default-caret-color: black; /* Set by JS to be same as color property */
29+
caret-color: var(--code-input_default-caret-color, black); /* Set by JS to be equal to color property */
2430
background-color: white;
2531

2632
/* Normal inline styles */
@@ -36,7 +42,6 @@ code-input {
3642
text-align: start;
3743
line-height: 1.5; /* Inherited to child elements */
3844
tab-size: 2;
39-
caret-color: darkgrey;
4045
white-space: pre;
4146
padding: 0!important; /* Use --padding to set the code-input element's padding */
4247
display: grid;
@@ -118,12 +123,13 @@ code-input pre {
118123
/* Make textarea almost completely transparent, except for caret and placeholder */
119124

120125
code-input textarea:not([data-code-input-fallback]) {
121-
color: transparent;
122126
background: transparent;
123-
caret-color: inherit!important; /* Or choose your favourite color */
127+
color: transparent;
128+
caret-color: inherit; /* Or choose your favourite color */
124129
}
125-
code-input textarea::placeholder {
126-
color: lightgrey;
130+
code-input textarea:not([data-code-input-fallback]):placeholder-shown {
131+
/* Show placeholder */
132+
color: inherit;
127133
}
128134

129135
/* Can be scrolled */
@@ -252,6 +258,8 @@ code-input:not(.code-input_loaded) pre, code-input:not(.code-input_loaded) texta
252258
code-input:has(textarea[data-code-input-fallback]) {
253259
padding: 0!important; /* Padding now in the textarea */
254260
box-sizing: content-box;
261+
262+
caret-color: revert; /* JS not setting the colour since no highlighting */
255263
}
256264
code-input textarea[data-code-input-fallback] {
257265
overflow: auto;

code-input.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,18 @@ 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+
509521
window.requestAnimationFrame(this.animateFrame.bind(this));
510522
}
511523

@@ -536,11 +548,9 @@ var codeInput = {
536548
syncSize() {
537549
// Synchronise the size of the pre/code and textarea elements
538550
if(this.templateObject.preElementStyled) {
539-
this.style.backgroundColor = getComputedStyle(this.preElement).backgroundColor;
540551
this.textareaElement.style.height = getComputedStyle(this.preElement).height;
541552
this.textareaElement.style.width = getComputedStyle(this.preElement).width;
542553
} else {
543-
this.style.backgroundColor = getComputedStyle(this.codeElement).backgroundColor;
544554
this.textareaElement.style.height = getComputedStyle(this.codeElement).height;
545555
this.textareaElement.style.width = getComputedStyle(this.codeElement).width;
546556
}
@@ -765,7 +775,6 @@ var codeInput = {
765775
// Update with fallback textarea's state so can keep editing
766776
// if loaded slowly
767777
if(fallbackSelectionStart !== undefined) {
768-
console.log("sel", fallbackSelectionStart, fallbackSelectionEnd, fallbackSelectionDirection, "scr", fallbackScrollTop, fallbackScrollLeft, "foc", fallbackFocused);
769778
textarea.setSelectionRange(fallbackSelectionStart, fallbackSelectionEnd, fallbackSelectionDirection);
770779
textarea.scrollTo(fallbackScrollTop, fallbackScrollLeft);
771780
}

0 commit comments

Comments
 (0)