
Product
Introducing Supply Chain Attack Campaigns Tracking in the Socket Dashboard
Campaign-level threat intelligence in Socket now shows when active supply chain attacks affect your repositories and packages.
@sandpack-monaco/react
Advanced tools
Headless editor for React
yarn add @sandpack-monaco/react
import React from 'react'
import { Sandpack, PresetLayout } from '@sandpack-monaco/react'
import '@sandpack-monaco/react/dist/index.css'
const Editor = () => {
return (
<Sandpack
customSetup={{
files: {
'index.js': 'export {}',
},
}}
>
<PresetLayout />
</Sandpack>
)
}
this is a provider set of an editor instance, wrap this component in your App.
Every hook from @sandpack-monaco/react should call under this Component.
import React from 'react'
import { Sandpack } from '@sandpack-monaco/react'
import '@sandpack-monaco/react/dist/index.css'
const Root = () => {
return (
<Sandpack
customSetup={{
files: {
'index.js': 'export {}',
},
}}
>
<App />
</Sandpack>
)
}
type SandpackProps = {
customSetup: {
files?: SandpackFiles
}
activePath: string
openPaths: string[]
theme?: SandpackThemeProp | undefined
}
type SandpackFiles = Record<string, SandpackFile>
interface SandpackFile<T = never> {
code: string
/**
* any data you host in file
*/
data?: T
readonly?: boolean
}
React hooks that return all files relative api.
function useSandpack(): {
openPaths: string[]
activePath: string
history: string[]
files: SandpackFiles
updateFile: (path: string, updateObj: Partial<SandpackFile>) => void
updateCurrentFile: (updateObj: Partial<SandpackFile>) => void
openFile: (path: string) => void
closeFile: (path: string) => void
updateFileState: (path: string, state: SandpackFile['state']) => void
setActiveFile: (path: string) => void
reopenClosedFile: () => void
}
React hook that return active code. Alias of
const { code, updateCode } = useSandpack().
function useActiveCode(): {
code: string
updateCode: (newCode: string) => void
}
React hook that return active file. Alias of
const { path, file, updateFile } = useSandpack().
function useActiveFile(): {
path: string
file: SandpackFile
updateFile: (updateObj: Partial<SandpackFile>) => void
}
React hook that return the file history stack. Alias of
const { history } = useSandpack().
function useHistory(): string[]
Use for custom editor action, which can register both context-menu action and monaco-eidtor action.
function useSandpackAction(desc?: ISpandpackActionDescriptor | undefined): void
type ISpandpackActionDescriptor = Omit<
Editor.IActionDescriptor,
'contextMenuGroupId' | 'contextMenuOrder' | 'run'
> &
IExtraActionOptions
example
This example implements an action for reopening a file.
import { useMemo, useRef } from 'react'
import { KeyCode, KeyMod } from 'monaco-editor/esm/vs/editor/editor.api'
import {
useSandpack,
ISpandpackActionDescriptor,
useSandpackAction,
} from '@sandpack-monaco/react'
export const useReopenClosedFileAction = () => {
const { reopenClosedFile } = useSandpack()
const ref = useRef(reopenClosedFile)
ref.current = reopenClosedFile
const descriptor = useMemo<ISpandpackActionDescriptor>(
() => ({
id: 'view:reopen-closed-editor',
label: 'View: Reopen Closed Editor',
keybindings: [KeyMod.Shift | KeyMod.Alt | KeyCode.KeyT],
run: () => {
ref.current()
},
}),
[],
)
useSandpackAction(descriptor)
}
Use for custom contextMenu, return an useContextMenuTrigger hook.
return the theme Object from Sandpack.
function useSandpackTheme(): {
theme: SandpackTheme
themeId: string
}
React hook that return the monaco-editor instance.
function useEdtiorInstance(): {
editor: editor.IStandaloneCodeEditor | null
setEditor: (editor: editor.IStandaloneCodeEditor) => void
}
return the monaco instance, reexport from @monaco-editor/react
function useMonaco(): typeof monaco
return the @monaco-editor/loader, reexport from @monaco-editor/react
PrsetLayout which contains sidebar, tabs, code editor by default.
import React from 'react'
import { Sandpack, PresetLayout, SandpackFiles } from '@sandpack-monaco/react'
import '@sandpack-monaco/react/dist/index.css'
export const Preset = () => {
return (
<div>
<Sandpack
customSetup={{
files: setupFiles,
}}
>
<PresetLayout></PresetLayout>
</Sandpack>
</div>
)
}
Since you have imported the preset ui, remember to import css from
@sandpack-monaco/react/dist/index.css
export type PresetLayoutProps = {
initalSplitSizes?: [number, number]
height?: Property.Height<string | number>
width?: Property.Width<string | number>
CustomSidebar?: React.ComponentType<any>
CustomFileTab?: React.ComponentType<any>
CustomToolbar?: React.ComponentType<any>
}
FAQs
Headless editor for React
The npm package @sandpack-monaco/react receives a total of 0 weekly downloads. As such, @sandpack-monaco/react popularity was classified as not popular.
We found that @sandpack-monaco/react demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Product
Campaign-level threat intelligence in Socket now shows when active supply chain attacks affect your repositories and packages.

Research
Malicious PyPI package sympy-dev targets SymPy users, a Python symbolic math library with 85 million monthly downloads.

Security News
Node.js 25.4.0 makes require(esm) stable, formalizing CommonJS and ESM compatibility across supported Node versions.