Skip to content

Commit

Permalink
refactor(Editor): generalize editor component to be reusable
Browse files Browse the repository at this point in the history
  • Loading branch information
YossiSaadi committed Apr 3, 2024
1 parent 54917e6 commit 9e93a40
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 16 deletions.
4 changes: 2 additions & 2 deletions src/components/Addons/Panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ const Panel: React.FC<Addon_RenderOptions> = ({ active }) => {
<Suspense fallback={"Loading Editor..."}>
<Editor
loading={!hasInitialCodeLoaded}
type={selectedTab}
placeholder={`Insert your ${selectedTab.toUpperCase()} code here`}
code={code[selectedTab]}
theme={theme}
extensions={extensions[selectedTab]}
fontSize={fontSize}
style={{ fontSize }}
onChange={updateCode}
/>
</Suspense>
Expand Down
38 changes: 25 additions & 13 deletions src/components/Editor/Editor.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
import React, { forwardRef, lazy } from "react";
import { Extension, ReactCodeMirrorRef } from "@uiw/react-codemirror";
import { Code, EditorTheme, PlaygroundState } from "@/types";
import {
Extension,
ReactCodeMirrorRef,
BasicSetupOptions,
} from "@uiw/react-codemirror";
import { Loader } from "@storybook/components";
const CodeMirror = lazy(() => import("@uiw/react-codemirror"));
import "./Editor.module.css";

interface EditorProps {
loading: boolean;
type: PlaygroundState["selectedTab"];
code: Code["jsx"] | Code["css"];
theme?: EditorTheme;
fontSize: PlaygroundState["fontSize"];
extensions: Extension[];
onChange: (newVal: Code["jsx"] | Code["css"]) => void;
code: string;
onChange: (newVal: string) => void;
placeholder?: string;
loading?: boolean;
theme?: "light" | "dark" | Extension;
style?: React.CSSProperties;
extensions?: Extension[];
setup?: BasicSetupOptions;
}

type EditorComponent = React.ForwardRefExoticComponent<
Expand All @@ -21,24 +25,32 @@ type EditorComponent = React.ForwardRefExoticComponent<

const Editor: EditorComponent = forwardRef(
(
{ loading, type, code, theme = "light", fontSize, extensions, onChange },
{
code,
onChange,
placeholder,
loading,
theme = "light",
style,
extensions,
setup,
},
ref
) => {
const placeholder = `Insert your ${type.toUpperCase()} code here`;

return (
<>
{loading ? (
<Loader />
) : (
<CodeMirror
style={{ fontSize }}
ref={ref}
style={style}
theme={theme}
value={code}
extensions={extensions}
onChange={onChange}
placeholder={placeholder}
basicSetup={setup}
/>
)}
</>
Expand Down
3 changes: 2 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from "react";
import { Extension } from "@uiw/react-codemirror";

export interface PlaygroundParameters {
storyId: string;
Expand Down Expand Up @@ -39,4 +40,4 @@ export type Code = { jsx: string; css: string };

export type Tab = "jsx" | "css";

export type EditorTheme = "light" | "dark";
export type EditorTheme = "light" | "dark" | Extension;

0 comments on commit 9e93a40

Please sign in to comment.