Rich Text Editor
A Quill editor dressed as a Polaris field, with a label, disabled and error states, a character counter and a configurable toolbar.
Preview
Code
Example.jsx
import { useState } from 'react';
import { BlockStack, Box, Card, Icon, InlineGrid, Text } from '@shopify/polaris';
import { AlertCircleIcon } from '@shopify/polaris-icons';
const EDITOR_STYLES = `
.quill {
background-color: var(--p-color-input-bg-surface);
border: var(--p-border-width-0165) solid var(--p-color-input-border);
border-top-color: #898f94;
border-radius: var(--p-border-radius-200);
margin-bottom: 3px;
}
.quill .ql-toolbar.ql-snow {
border: none;
border-bottom: 1px solid #ccc;
}
.quill .ql-container.ql-snow {
border: none;
font-family: var(--p-font-family-sans);
}
.quill .ql-container.ql-snow .ql-editor {
min-height: 150px;
max-height: 400px;
}
.quill .ql-editor.ql-blank::before {
font-style: normal;
}
.quill--disabled {
color: var(--p-color-text-disabled);
background-color: #f2f2f2;
border: 1px solid #f2f2f2;
}
.quill--disabled .ql-toolbar .ql-stroke {
fill: none;
stroke: #aaa;
}
.quill--disabled .ql-toolbar .ql-fill {
fill: #aaa;
stroke: none;
}
.quill--disabled .ql-toolbar .ql-picker {
color: #aaa;
}
.quill--disabled .ql-editor.ql-blank::before {
color: var(--p-color-text-disabled);
}
.quill--error {
border: var(--p-border-width-0165) solid var(--p-color-border-critical-secondary);
background-color: var(--p-color-bg-surface-critical);
}
`;
const DEFAULT_TOOLBAR = [
['bold', 'italic', 'underline', 'blockquote'],
[{ list: 'ordered' }, { list: 'bullet' }, { indent: '-1' }, { indent: '+1' }],
['link'],
['clean'],
];
const EditorField = ({ label, value, onChange, placeholder, disabled, error, toolbar }) => {
const classNames = [disabled ? 'quill--disabled' : null, error ? 'quill--error' : null]
.filter(Boolean)
.join(' ');
return (
<Box>
<Box paddingBlockEnd="100">
<Text as="p" tone={disabled ? 'disabled' : 'base'}>
{label}
</Text>
</Box>
<ReactQuill
theme="snow"
className={classNames}
bounds=".quill"
readOnly={disabled}
placeholder={placeholder}
value={value}
onChange={onChange}
modules={{
toolbar: toolbar ?? DEFAULT_TOOLBAR,
clipboard: { matchVisual: false },
}}
/>
{error ? (
<InlineGrid alignItems="start" gap="150" columns="20px auto">
<Icon source={AlertCircleIcon} tone="textCritical" />
<Text as="p" tone="critical">
{error}
</Text>
</InlineGrid>
) : null}
</Box>
);
};
export const Example = () => {
const [standard, setStandard] = useState('');
const [disabled, setDisabled] = useState('');
const [invalid, setInvalid] = useState('');
const [counted, setCounted] = useState('');
const [customised, setCustomised] = useState('');
const [characters, setCharacters] = useState(0);
return (
<Card>
<style>{EDITOR_STYLES}</style>
<BlockStack gap="600">
<EditorField
label="Default editor"
placeholder="Enter text..."
value={standard}
onChange={setStandard}
/>
<EditorField
label="Disabled editor"
placeholder="Enter text..."
value={disabled}
onChange={setDisabled}
disabled
/>
<EditorField
label="Error state editor"
placeholder="Enter text..."
value={invalid}
onChange={setInvalid}
error="You must enter more than 30 characters"
/>
<Box>
<EditorField
label="Editor with character count"
placeholder="Enter text..."
value={counted}
onChange={(next, _delta, _source, editor) => {
setCharacters(editor.getText().trim().length);
setCounted(next);
}}
/>
<Text as="p">Character count: {characters}</Text>
</Box>
<EditorField
label="Editor with modified controls"
placeholder="Enter text..."
value={customised}
onChange={setCustomised}
toolbar={[
[{ header: [1, 2, 3, 4, false] }],
['bold', 'italic', 'underline', 'blockquote'],
[{ list: 'ordered' }, { list: 'bullet' }, { indent: '-1' }, { indent: '+1' }],
[{ color: [] }, { background: [] }],
['link'],
['clean'],
]}
/>
</BlockStack>
</Card>
);
};