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

Cursor disappear when using TextInput #35

Open
jcubic opened this issue Dec 10, 2023 · 1 comment
Open

Cursor disappear when using TextInput #35

jcubic opened this issue Dec 10, 2023 · 1 comment

Comments

@jcubic
Copy link

jcubic commented Dec 10, 2023

When using a TextInput element the cursor disappears when inside color Text.

The cursor only appears when it's at the end of the line.

this is my code:

const Editor = ({ initValue, style, onChange = () => {} }: EditorProps) => {
  const [value, setValue] = useState<string>(initValue);

  const changeHandler = (text: string) => {
    setValue(text);
    onChange(text);
  };
  const ast = parse(value);

  const headers: HeaderNode[] = ast.filter(isHeaderNode);

  console.log(JSON.stringify(ast, null, 4));

  return (
    <TextInput
      style={[styles.editor, style]}
      multiline
      autoComplete="off"
      autoCorrect={false}
      autoFocus={true}
      onChangeText={changeHandler}>
      <Text>
        {ast.map((node, index) => {
          const style = styles[node.type];
          if (node.type !== 'link') {
            const { content } = node;
            const key = `${index}-${content}`;
            if (node.type === 'code') {
               const { language } = node;
               if (!language) {
                 return (
                   <Text key={key} style={style}>
                     ```{ content }{'\n'}```
                   </Text>
                 );
               } else {
                 return (
                   <Fragment key={key}>
                     <Text>```{language}</Text>
                     <SyntaxHighlighter
                       language={language}
                       style={atomOneLight}
                       fontSize={fontSize}
                       PreTag={Text}
                       CodeTag={Text}
                       highlighter={'hljs'}
                     >
                       { content }
                     </SyntaxHighlighter>
                     <Text>```</Text>
                   </Fragment>
                 );
               }
            }
            return (
              <Text key={key} style={style}>{ content }</Text>
            );
          }
        })}
      </Text>
    </TextInput>
  );
};

Peek 2023-12-10 18-40

@jcubic
Copy link
Author

jcubic commented Dec 28, 2023

I've found a workaround, it seems that this is caused by background on Text created for Code or Pre tags.

Adding this Dummy Text component as Pre and Code solves the issue:

import { type FC, type ReactNode } from 'react';
import { Text } from 'react-native';

const DummyText: FC<{children: ReactNode}> = ({ children }) => {
  return <Text>{children}</Text>;
};

const Component = () => {
  return (
    <SyntaxHighlighter
      language={language}
      style={atomOneLight}
      fontSize={fontSize}
      PreTag={DummyText}
      CodeTag={DummyText}
      highlighter={'hljs'}
    >
      { content }
    </SyntaxHighlighter>
  );
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant