import Editor, { loader, EditorProps, Monaco } from '@monaco-editor/react' import { CSS } from '@stitches/react' import type monaco from 'monaco-editor' import { useTheme } from 'next-themes' import { FC, MutableRefObject, ReactNode } from 'react' import { Flex } from '.' import dark from '../theme/editor/amy.json' import light from '../theme/editor/xcode_default.json' export type MonacoProps = EditorProps & { id?: string rootProps?: { css: CSS } & Record overlay?: ReactNode editorRef?: MutableRefObject monacoRef?: MutableRefObject } loader.config({ paths: { vs: 'https://cdn.jsdelivr.net/npm/monaco-editor@0.30.1/min/vs' } }) const Monaco: FC = ({ id, path = `file:///${id}`, className = id, language = 'json', overlay, editorRef, monacoRef, beforeMount, rootProps, ...rest }) => { const { theme } = useTheme() const setTheme = (monaco: Monaco) => { monaco.editor.defineTheme('dark', dark as any) monaco.editor.defineTheme('light', light as any) } return ( { beforeMount?.(monaco) setTheme(monaco) }} theme={theme === 'dark' ? 'dark' : 'light'} {...rest} /> {overlay && ( {overlay} )} ) } export default Monaco