Skip to content

Commit

Permalink
fix: revert changes made to placeholderColor & add handling to startC…
Browse files Browse the repository at this point in the history
…omposition
  • Loading branch information
BartoszGrajdek committed Mar 6, 2024
1 parent 7721417 commit fbd6e00
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
19 changes: 15 additions & 4 deletions src/MarkdownTextInput.web.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -258,13 +258,20 @@ const MarkdownTextInput = React.forwardRef<TextInput, MarkdownTextInputProps>(
return e;
}, []);

// Placeholder text color logic
const updateTextColor = useCallback((node: HTMLDivElement, text: string) => {
// eslint-disable-next-line no-param-reassign -- we need to change the style of the node, so we need to modify it
node.style.color = String(placeholder && (text === '' || text === '\n') ? placeholderTextColor : flattenedStyle.color || 'black');
}, []);

const handleOnChangeText = useCallback(
(e: SyntheticEvent<HTMLDivElement>) => {
if (!divRef.current || !(e.target instanceof HTMLElement)) {
return;
}

if (compositionRef.current) {
updateTextColor(divRef.current, e.target.innerText);
compositionRef.current = false;
return;
}
Expand All @@ -281,6 +288,7 @@ const MarkdownTextInput = React.forwardRef<TextInput, MarkdownTextInputProps>(
default:
text = parseText(divRef.current, e.target.innerText, processedMarkdownStyle).text;
}
updateTextColor(divRef.current, e.target.innerText);

if (onChange) {
const event = e as unknown as NativeSyntheticEvent<any>;
Expand Down Expand Up @@ -437,7 +445,13 @@ const MarkdownTextInput = React.forwardRef<TextInput, MarkdownTextInputProps>(
(r as unknown as TextInput).isFocused = () => document.activeElement === r;
(r as unknown as TextInput).clear = () => {
r.innerText = '';
updateTextColor(r, '');
};

if (value === '' || value === undefined) {
// update to placeholder color when value is empty
updateTextColor(r, r.innerText);
}
}

if (ref) {
Expand All @@ -464,6 +478,7 @@ const MarkdownTextInput = React.forwardRef<TextInput, MarkdownTextInputProps>(

const text = processedValue !== undefined ? processedValue : '';
parseText(divRef.current, text, processedMarkdownStyle, text.length);
updateTextColor(divRef.current, value);
},
[multiline, processedMarkdownStyle, processedValue],
);
Expand Down Expand Up @@ -506,10 +521,6 @@ const MarkdownTextInput = React.forwardRef<TextInput, MarkdownTextInputProps>(
divRef.current.focus();
}, []);

useEffect(() => {
document.documentElement.style.setProperty('--placeholder-color', placeholderTextColor as string);
}, [placeholderTextColor]);

const startComposition = useCallback(() => {
compositionRef.current = true;
}, []);
Expand Down
1 change: 0 additions & 1 deletion src/web/MarkdownTextInput.css
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,4 @@
pointer-events: none;
display: block; /* For Firefox */
content: attr(placeholder);
color: var(--placeholder-color);
}

0 comments on commit fbd6e00

Please sign in to comment.