QueryEditor
The full SQL editor of a database console: PostgreSQL highlighting, the project's tables and columns offered as you type, and Ctrl/⌘ + Enter to run the selection or the whole text. It has its own entry, @sypnia/ui/react/code-editor, so only the pages that use it load it.
import { QueryEditor } from '@sypnia/ui/react/code-editor'; import { QueryEditorSkeleton } from '@sypnia/ui/react'
A query with completion
Code
import { lazy, Suspense, useState } from 'react';
import { Field, QueryEditorSkeleton, toast, Toaster } from '@sypnia/ui/react';
// CodeMirror loads only when the editor is shown: the skeleton holds its place meanwhile.
const QueryEditor = lazy(() => import('@sypnia/ui/react/code-editor').then((m) => ({ default: m.QueryEditor })));
const schema = { orders: ['id', 'customer_id', 'total', 'created_at'], customers: ['id', 'name', 'country'] };
// Type «from o» to see the tables, select a line and press Ctrl/⌘ + Enter to run only that.
export default function Query() {
const [sql, setSql] = useState("select country, count(*)\nfrom orders\njoin customers on customers.id = orders.customer_id\nwhere created_at > now() - interval '7 days'\ngroup by 1\norder by 2 desc;");
return (
<>
<Field label="Query">
<Suspense fallback={<QueryEditorSkeleton rows={8} />}>
<QueryEditor label="Query" rows={8} value={sql} onChange={setSql} schema={schema} placeholder="select …"
onRun={(text) => toast(`Ran ${text.split('\n').length} lines`, { tone: 'ok' })} />
</Suspense>
</Field>
<Toaster />
</>
);
} Skeleton
Code
import { QueryEditorSkeleton } from '@sypnia/ui/react';
// While the editor's code arrives: the same box, the same height.
export default function Skeleton() {
return <QueryEditorSkeleton rows={6} />;
} When to use it
Use it when
- The SQL page of a console, where people write and run real queries.
- Any place where completing table and column names saves typing.
Do not use it when
- A few lines in a form: CodeEditor is lighter and loads at once.
- Code people should only read: CodeBlock.
For everyone
- It is a text box with a name and several lines; screen readers read it as one.
- Ctrl/⌘ + Enter runs; Escape closes the suggestions; Tab leaves the editor.
- Load it lazily and show QueryEditorSkeleton meanwhile: the page does not move when it arrives.
Props
| QueryEditor | Type | Default |
|---|---|---|
labelrequiredIts name for screen readers. | string | |
onChangerequired | (value: string) => void | |
valuerequired | string | |
className | string | |
onRunCtrl/⌘ + Enter: runs the selection when there is one, otherwise the whole text. | ((text: string) => void) | |
placeholder | string | |
readOnly | boolean | |
rowsVisible lines before it scrolls; it grows with its container when the container is taller. | number | 10 |
schemaTables and their columns, offered as the person types: `{ orders: ['id', 'total'] }`. | Record<string, string[]> | {} |
| QueryEditorSkeleton | Type | Default |
|---|---|---|
rows | number | 10 |