npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@sandpack-monaco/react

v0.4.7

Published

Headless editor for React

Downloads

1

Readme

@sandpack-monaco/react

Headless editor for React

Install

yarn add @sandpack-monaco/react

useage

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>
  )
}

API Reference

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>
  )
}

SandpackProps

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
}

useSandpack

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
}

useActiveCode

React hook that return active code. Alias of const { code, updateCode } = useSandpack().

function useActiveCode(): {
  code: string
  updateCode: (newCode: string) => void
}

useActiveFile

React hook that return active file. Alias of const { path, file, updateFile } = useSandpack().

function useActiveFile(): {
  path: string
  file: SandpackFile
  updateFile: (updateObj: Partial<SandpackFile>) => void
}

useHistory

React hook that return the file history stack. Alias of const { history } = useSandpack().

function useHistory(): string[]

useSanpackAction

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)
}

useSandpackContextMenu

Use for custom contextMenu, return an useContextMenuTrigger hook.

useSandpackTheme

return the theme Object from Sandpack.

function useSandpackTheme(): {
  theme: SandpackTheme
  themeId: string
}

useEditorInstance

React hook that return the monaco-editor instance.

function useEdtiorInstance(): {
  editor: editor.IStandaloneCodeEditor | null
  setEditor: (editor: editor.IStandaloneCodeEditor) => void
}

useMonaco

return the monaco instance, reexport from @monaco-editor/react

function useMonaco(): typeof monaco

loader

return the @monaco-editor/loader, reexport from @monaco-editor/react

PresetLayout

PrsetLayout which contains sidebar, tabs, code editor by default.

example

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

PresetLayoutProps

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>
}

UI Components

FileExplorer

Pane

PresetPane

ModuleList

FileTabs

Toolbar

ToolbarButton

CodeEditor