Skip to content

Commit

Permalink
Merge pull request #4 from abidhkm/feat/draggable-border
Browse files Browse the repository at this point in the history
draggable border
  • Loading branch information
agneym authored Dec 16, 2019
2 parents b1fbc1c + 2680ee9 commit 7049e07
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 13 deletions.
5 changes: 3 additions & 2 deletions playground/src/Editor/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React, { FC, useMemo } from "react";
import styled from "styled-components";

import { IEditorTabs, ISnippet } from "../types";
import EditorSetup from "./EditorSetup";
import { ITabConfig } from "../types";
Expand All @@ -17,12 +16,13 @@ const TabContainer = styled(StyledTabs)`
`;

interface IProps {
width: number;
code: ISnippet;
defaultTab: IEditorTabs;
onChange: (changed: string, type: IEditorTabs) => void;
}

const Editor: FC<IProps> = ({ code, defaultTab, onChange }) => {
const Editor: FC<IProps> = ({ code, defaultTab, onChange, width }) => {
const tabs: Readonly<ITabConfig<IEditorTabs>[]> = useMemo(
() => [
{ name: "HTML", value: "markup" },
Expand All @@ -34,6 +34,7 @@ const Editor: FC<IProps> = ({ code, defaultTab, onChange }) => {
return (
<TabContainer
defaultIndex={tabs.findIndex(tab => tab.value === defaultTab)}
style={{ width: width }}
>
<StyledTabList>
{tabs.map(tab => (
Expand Down
59 changes: 49 additions & 10 deletions playground/src/Playground.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { FC, useState } from "react";
import React, { FC, useState, useCallback, useEffect, useRef } from "react";
import styled, { ThemeProvider, DefaultTheme } from "styled-components";
import { useId } from "@reach/auto-id";

Expand All @@ -16,6 +16,11 @@ const Container = styled.div`
${media.phone`
flex-direction: column;
`}
.divider {
border: 1px solid transparent;
cursor: col-resize;
}
`;

interface IProps {
Expand All @@ -38,8 +43,10 @@ const Playground: FC<IProps> = ({
theme = ourTheme,
}) => {
const [snippet, setSnippet] = useState<ISnippet>(initialSnippet);

const [width, setWidth] = useState<number>(0);
const [containerWidth, setContainerWidth] = useState(0);
const id = useId(userId);
const ref = useRef<HTMLDivElement>(null);

const onSnippetChange = (changed: string, type: IEditorTabs) => {
setSnippet(snippet => ({
Expand All @@ -48,22 +55,54 @@ const Playground: FC<IProps> = ({
}));
};

useEffect(() => {
if (ref.current) {
setContainerWidth(ref.current.clientWidth);
setWidth(ref.current.clientWidth / 2);
}
}, []);

const resize = useCallback(
(e: any) => {
const movedInPx = e.clientX - 10;
setWidth(movedInPx);
},
[setWidth]
);

const handleAddListener = useCallback(() => {
document.addEventListener("mousemove", resize, false);
}, []);

const handleRemoveListener = useCallback(() => {
document.removeEventListener("mousemove", resize, false);
}, []);

return (
<ThemeProvider theme={theme}>
<Container>
<Container ref={ref}>
<Editor
width={width}
code={snippet}
defaultTab={defaultEditorTab}
onChange={onSnippetChange}
/>
{id && (
<Result
id={id}
snippet={snippet}
defaultTab={defaultResultTab}
transformJs={transformJs}
presets={presets}
/>
<>
<div
onMouseDown={handleAddListener}
onMouseUp={handleRemoveListener}
className="divider"
></div>
<Result
width={containerWidth - width}
id={id}
snippet={snippet}
defaultTab={defaultResultTab}
transformJs={transformJs}
presets={presets}
/>
</>
)}
</Container>
</ThemeProvider>
Expand Down
7 changes: 6 additions & 1 deletion playground/src/Result/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ interface IProps {
defaultTab: IResultTabs;
transformJs: boolean;
presets: string[];
width: number;
}

const Result: FC<IProps> = ({
Expand All @@ -25,6 +26,7 @@ const Result: FC<IProps> = ({
presets,
defaultTab,
transformJs,
width,
}) => {
const [logs, setLogs] = useState<unknown[]>([]);
const tabs: Readonly<ITabConfig<IResultTabs>[]> = useMemo(
Expand All @@ -50,7 +52,10 @@ const Result: FC<IProps> = ({
waitForMessage();
}, [id]);
return (
<StyledTabs defaultIndex={tabs.findIndex(tab => tab.value === defaultTab)}>
<StyledTabs
defaultIndex={tabs.findIndex(tab => tab.value === defaultTab)}
style={{ width: width }}
>
<StyledTabList>
{tabs.map(tab => (
<StyledTab key={tab.value}>{tab.name}</StyledTab>
Expand Down

0 comments on commit 7049e07

Please sign in to comment.