diff --git a/example/src/index.js b/example/src/index.js
index 6fcd4bf..15c4c5e 100644
--- a/example/src/index.js
+++ b/example/src/index.js
@@ -7,7 +7,7 @@ import Playground from "@agney/playground";
const App = () => {
const snippet = {
markup: `
`,
- css: ``,
+ css: `div { color: red }`,
javascript: `import { h, Component, render } from 'preact';
import htm from 'htm';
diff --git a/playground/src/Editor/index.tsx b/playground/src/Editor/index.tsx
index a73a86e..8c0624c 100644
--- a/playground/src/Editor/index.tsx
+++ b/playground/src/Editor/index.tsx
@@ -1,5 +1,5 @@
import React, { FC, useMemo } from "react";
-import {styled} from "goober";
+import { styled } from "goober";
import { IEditorTabs, ISnippet } from "../types";
import EditorSetup from "./EditorSetup";
import { ITabConfig } from "../types";
@@ -12,7 +12,7 @@ import {
} from "../TabStyles";
const TabContainer = styled(StyledTabs)`
- min-width: ${props => props.theme.container.minWidth};
+ min-width: ${(props) => props.theme.container.minWidth};
`;
interface IProps {
@@ -23,34 +23,44 @@ interface IProps {
}
const Editor: FC = ({ code, defaultTab, onChange, width }) => {
- const tabs: Readonly[]> = useMemo(
- () => [
- { name: "HTML", value: "markup" },
- { name: "CSS", value: "css" },
- { name: "JS", value: "javascript" },
- ],
- []
- );
+ const tabs: Readonly[]> = useMemo(() => {
+ const tabsArr = [];
+ if (code.markup) {
+ tabsArr.push({ name: "HTML", value: "markup", code: code.markup });
+ }
+ if (code.css) {
+ tabsArr.push({ name: "CSS", value: "css", code: code.css });
+ }
+ if (code.javascript) {
+ tabsArr.push({ name: "JS", value: "javascript", code: code.javascript });
+ }
+ return tabsArr;
+ }, []);
return (
tab.value === defaultTab)}
+ defaultIndex={tabs.findIndex(
+ (tab) => tab.code && tab.value === defaultTab
+ )}
style={{ width: width }}
>
- {tabs.map(tab => (
- {tab.name}
- ))}
+ {tabs.map(
+ (tab) => tab.code && {tab.name}
+ )}
- {tabs.map(tab => (
-
-
-
- ))}
+ {tabs.map(
+ (tab) =>
+ tab.code && (
+
+
+
+ )
+ )}
);
diff --git a/playground/src/Result/index.tsx b/playground/src/Result/index.tsx
index 60231c8..be79680 100644
--- a/playground/src/Result/index.tsx
+++ b/playground/src/Result/index.tsx
@@ -31,20 +31,20 @@ const Result: FC = ({
const [logs, setLogs] = useState([]);
const tabs: Readonly[]> = useMemo(
() => [
- { name: "Result", value: "result" },
- { name: "Console", value: "console" },
+ { name: "Result", value: "result" as IResultTabs },
+ { name: "Console", value: "console" as IResultTabs },
],
[]
);
useEffect(() => {
function waitForMessage() {
if (typeof window !== "undefined") {
- window.addEventListener("message", data => {
+ window.addEventListener("message", (data) => {
if (
data.data.source === `frame-${id}` &&
data.data.message.type === "log"
) {
- setLogs(prevLogs => [...prevLogs, ...data.data.message.data]);
+ setLogs((prevLogs) => [...prevLogs, ...data.data.message.data]);
}
});
}
@@ -53,11 +53,11 @@ const Result: FC = ({
}, [id]);
return (
tab.value === defaultTab)}
+ defaultIndex={tabs.findIndex((tab) => tab.value === defaultTab)}
style={{ width: width }}
>
- {tabs.map(tab => (
+ {tabs.map((tab) => (
{tab.name}
))}
diff --git a/playground/src/__tests__/Editor.test.tsx b/playground/src/__tests__/Editor.test.tsx
index 6e0a1f5..98c16ca 100644
--- a/playground/src/__tests__/Editor.test.tsx
+++ b/playground/src/__tests__/Editor.test.tsx
@@ -3,10 +3,24 @@ import { render } from "../../scripts/test-utils";
import Editor from "../Editor";
-const initialSnippet = {
- markup: ``,
+const emptySnippet = {
+ markup: ``,
css: ``,
- javascript: ``,
+ javascript: `import htm from 'htm'`,
+};
+
+const snippet = {
+ markup: ``,
+ css: `div {color: red;}`,
+ javascript: `import { h, Component, render } from 'preact';
+import htm from 'htm';
+
+const html = htm.bind(h);
+
+const app = html\`Hello World from Playground!
\`
+
+render(app, document.getElementById('app'));
+ `,
};
describe("Editor", () => {
@@ -15,7 +29,7 @@ describe("Editor", () => {
const { getByText } = render(
{}}
/>
@@ -23,4 +37,25 @@ describe("Editor", () => {
const button = getByText("CSS");
expect(button.getAttribute("data-selected")).toBe("");
});
+
+ it("should render only the tabs that provided code", () => {
+ const defaultTab = "markup";
+ const { getByText, queryByText } = render(
+ {}}
+ />
+ );
+
+ const htmlTab = getByText("HTML");
+ const javascriptTab = getByText("JS");
+
+ expect(htmlTab).toBeTruthy();
+ expect(javascriptTab).toBeTruthy();
+
+ const cssTab = queryByText("CSS");
+ expect(cssTab).toBeFalsy();
+ });
});
diff --git a/playground/src/types.ts b/playground/src/types.ts
index 49bb260..c33a329 100644
--- a/playground/src/types.ts
+++ b/playground/src/types.ts
@@ -8,6 +8,7 @@ export type IEditorTabs = "markup" | "css" | "javascript";
export type IResultTabs = "result" | "console";
export interface ITabConfig {
+ code?: string;
name: string;
value: T;
}