crashwap
crashwap

Reputation: 3068

How turn off spell check in QuillJS (React)

How do you turn off spellcheck in quill.js in React?

I found this GitHub page that shows how disable quill's spellchecker in normal JavaScript:

const quill = new Quill('#editor-container')
quill.root.setAttribute('spellcheck', false)

However, I can't see how to implement this with a React component.

My React component (Preact actually):

import { h, FunctionalComponent } from 'preact';
import './contentEditor.scss';
import Quill from 'react-quill';
import 'react-quill/dist/quill.snow.css';

interface ContentEditorProps {
  content: string | undefined;
  onChange(value: string): void;
}

const modules = {
  toolbar: [
    [{ header: [1, 2, 3, 4, false] }],
    ['bold', 'italic', 'underline', 'blockquote'],
    [{ color: [] }],
    [{ align: [] }],
    [{ list: 'ordered' }, { list: 'bullet' }],
    [{ indent: '-1' }, { indent: '+1' }],
    ['link', 'image'],
  ],
};

const formats = [
  'header',
  'bold',
  'color',
  'italic',
  'underline',
  'strike',
  'blockquote',
  'list',
  'bullet',
  'align',
  'indent',
  'link',
  'image',
];

export const ContentEditor: FunctionalComponent<ContentEditorProps> = ({
  content,
  onChange,
}) => {
  return (
    <Quill
      theme='snow'
      value={content}
      onChange={onChange}
      modules={modules}
      formats={formats}
    />
  );
};

Upvotes: 5

Views: 4575

Answers (3)

Jay Maniya
Jay Maniya

Reputation: 1

For typescript next.js

React.useEffect(() => {
  if (quillRef.current) {
    const editor = quillRef.current?.editor;

    if (editor) {
      editor.root.setAttribute("spellcheck", "false");
    }
  }
}, []);

Upvotes: 0

Victor Eke
Victor Eke

Reputation: 545

Here's a simple alterntaive without the need for a useRef hook

<div spellCheck={false}>
  <ReactQuill
    value={content}
    modules={toolbar}
    onChange={handleOnChange}
    theme="snow"
  />
</div>

Since the spell-check attribute is inheritable, it will be disabled in the quill editor as well.

Upvotes: 1

tmhao2005
tmhao2005

Reputation: 17524

In order to access to Quill editor, you have to create an ref to the <Quill /> component, then use it to set the attribute. Here is the snippet code:


// For typings
import Quill from "react-quill";
import QuillEditor from "quill"

export const ContentEditor = ({ content, onChange }) => {
  const ref = React.useRef<Quill & { editor: QuillEditor }>(null);

  // Disable spellcheck as component is mounted
  React.useEffect(() => {
    ref.current?.editor.root.setAttribute("spellcheck", "false");
  }, []);

  return (
    <Quill
      // set the ref to access to quill editor
      ref={ref}
      theme="snow"
      value={content}
      onChange={onChange}
      modules={modules}
      formats={formats}
    />
  );
};

I have also made an sample as well: https://codesandbox.io/s/stoic-mendel-4g3jw?file=/src/App.js

Upvotes: 10

Related Questions