Skip to content

refactor(Editor): generalize editor component to be reusable #51

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it customizable by users?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in LiveEdit for example I'm passing an extension (vscode theme)
it is customizable to "light" | "dark" by users but not allowing (yet, you had a good point) to pass their own theme