
Product
Introducing Socket Firewall Enterprise: Flexible, Configurable Protection for Modern Package Ecosystems
Socket Firewall Enterprise is now available with flexible deployment, configurable policies, and expanded language support.
@stoplight/monaco
Advanced tools
Monaco Editor for React, with some light state management.
Supported in modern browsers.
# latest stable
yarn add @stoplight/monaco
# install dev deps
yarn add -D @stoplight/webpack
Note: there is some setup required to get this component to work.
A webpack-bundled version of monaco-editor is included and exported from the main module as a named export monaco, so you do not have to use monaco-editor-webpack-plugin, but you do need to statically serve the webworkers at some route. By default this route is / but can be configured at runtime like so:
import { setGetWorkerUrlFunc } from "@stoplight/monaco";
// something like this
setGetWorkerUrlFunc(
(fileName) => `${process.env.YOUR_CDN}/monaco-workers/${fileName}`
);
The webworkers that need to be served are in node_modules/@stoplight/monaco/monaco-editor/monaco-workers directory.
Storybook makes it easy to add some public static file directories. Add an -s flag when you launch it.
We can't control the route, so this serves it at the default /*.worker.js routes.
package.json
"scripts": {
- "storybook": "start-storybook -p 9001",
+ "storybook": "start-storybook -s ./node_modules/@stoplight/monaco/monaco-editor/monaco-workers -p 9001",
import * as React from "react";
import { MonacoCodeEditor, MonacoCodeStore } from "@stoplight/monaco";
const store = new MonacoCodeStore({
id: "a",
path: "file:///a.js",
value: "var y = true;"
});
store.onDidUpdateValue(val => {
console.log('latest value', val);
});
// The store value is synced with the MonacoCodeEditor internal value, but you can
// also treat it as a controlled component by calling setValue on the store directly (this will update the component).
const resetValue = () => store.setValue('');
export const Component = () => {
return (
<div>
<button onClick={resetValue}>Reset Value</div>
<MonacoCodeEditor store={store} />
</div>
);
};
Note that DiffEditor is not included in the default export to help with bundle size shaking.
import * as React from "react";
import { MonacoDiffStore } from "@stoplight/monaco";
import { MonacoDiffEditor } from "@stoplight/monaco/DiffEditor";
const store = new MonacoDiffStore({
id: "a",
lang: "js",
original: "var y = true;"
modified: "var y = false;"
});
store.modifiedStore.onDidUpdateValue(val => {
console.log('latest value', val);
});
// The diff store has a originalStore and modifiedStore that you can access to manipulate highlight, listen to events, etc.
const resetValue = () => store.modifiedStore.setValue('');
export const Component = () => {
return (
<div>
<button onClick={resetValue}>Reset Value</div>
<MonacoDiffEditor store={store} options={{ originalEditable: true }} />
</div>
);
};
In some cases you might need react to the underlying IStadaloneCodeEditor being changed.
import * as React from "react";
import { MonacoCodeStore, useCurrentCodeEditor } from "@stoplight/monaco";
const store = new MonacoCodeStore({
id: "a",
path: "file:///a.js",
value: "var y = true;"
});
// You can subscribe to this event
store.onEditorDidChange(editor => {
console.log('editor', editor);
});
// Or use the provided hook
export const Toolbar = () => {
const editor = useCurrentCodeEditor(store)
React.useEffect(() => {
// Do something with the editor, like register plugins
}, [editor]);
return <ToolbarButtons />;
};
To use the Stoplight monaco mocks, do the following in your test files or in jest.setup.ts:
require("@stoplight/monaco/mock").mock();
or if you're feeling importy
import { mock as mockMonaco } from "@stoplight/monaco/mock";
mockMonaco();
There isn't currently an unmock function or any variations of the mock, but if you need those, go ahead and add those to mock.ts. That's why I used a named export.
feature/{name}, chore/{name}, or fix/{name} branch.yarn.yarn test.prod.yarn commit. NOTE: Commits that don't follow the conventional format will be rejected. yarn commit creates this format for you, or you can put it together manually and then do a regular git commit.git push.develop branch.FAQs
Monaco Editor for React, with some light state management.
The npm package @stoplight/monaco receives a total of 101 weekly downloads. As such, @stoplight/monaco popularity was classified as not popular.
We found that @stoplight/monaco 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
Socket Firewall Enterprise is now available with flexible deployment, configurable policies, and expanded language support.

Security News
Open source dashboard CNAPulse tracks CVE Numbering Authorities’ publishing activity, highlighting trends and transparency across the CVE ecosystem.

Product
Detect malware, unsafe data flows, and license issues in GitHub Actions with Socket’s new workflow scanning support.