Skip to content

Commit

Permalink
feat(landing): add examples section (#160)
Browse files Browse the repository at this point in the history
  • Loading branch information
danilowoz authored Nov 23, 2021
1 parent 7d5706f commit 5a0b279
Show file tree
Hide file tree
Showing 24 changed files with 1,407 additions and 226 deletions.
11 changes: 10 additions & 1 deletion sandpack-react/src/components/CodeEditor/CodeMirror.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,11 @@ interface CodeMirrorProps {
decorators?: Decorators;
}

export const CodeMirror = React.forwardRef<HTMLElement, CodeMirrorProps>(
export interface CodeMirrorRef {
getCodemirror: () => EditorView | undefined;
}

export const CodeMirror = React.forwardRef<CodeMirrorRef, CodeMirrorProps>(
(
{
code,
Expand All @@ -90,6 +94,10 @@ export const CodeMirror = React.forwardRef<HTMLElement, CodeMirrorProps>(
const { listen } = useSandpack();
const ariaId = React.useRef<string>(generateRandomId());

React.useImperativeHandle(ref, () => ({
getCodemirror: () => cmView.current,
}));

React.useEffect(() => {
if (!wrapper.current) {
return () => {
Expand Down Expand Up @@ -200,6 +208,7 @@ export const CodeMirror = React.forwardRef<HTMLElement, CodeMirrorProps>(
"aria-describedby",
`exit-instructions-${ariaId.current}`
);
view.contentDOM.setAttribute("data-gramm", "false");
}

cmView.current = view;
Expand Down
83 changes: 47 additions & 36 deletions sandpack-react/src/components/CodeEditor/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import { useSandpack } from "../../hooks/useSandpack";
import { FileTabs } from "../FileTabs";

import { CodeMirror } from "./CodeMirror";
import type { CodeMirrorRef } from "./CodeMirror";

export type CodeEditorRef = CodeMirrorRef;
export interface CodeEditorProps {
customStyle?: React.CSSProperties;
showTabs?: boolean;
Expand All @@ -21,44 +23,53 @@ export interface CodeEditorProps {

export { CodeMirror as CodeEditor };

export const SandpackCodeEditor: React.FC<CodeEditorProps> = ({
customStyle,
showTabs,
showLineNumbers = false,
showInlineErrors = false,
showRunButton = true,
wrapContent = false,
closableTabs = false,
}) => {
const { sandpack } = useSandpack();
const { code, updateCode } = useActiveCode();
const { activePath, status, editorState } = sandpack;
const shouldShowTabs = showTabs ?? sandpack.openPaths.length > 1;
export const SandpackCodeEditor = React.forwardRef<
CodeMirrorRef,
CodeEditorProps
>(
(
{
customStyle,
showTabs,
showLineNumbers = false,
showInlineErrors = false,
showRunButton = true,
wrapContent = false,
closableTabs = false,
},
ref
) => {
const { sandpack } = useSandpack();
const { code, updateCode } = useActiveCode();
const { activePath, status, editorState } = sandpack;
const shouldShowTabs = showTabs ?? sandpack.openPaths.length > 1;

const c = useClasser("sp");
const c = useClasser("sp");

const handleCodeUpdate = (newCode: string) => {
updateCode(newCode);
};
const handleCodeUpdate = (newCode: string) => {
updateCode(newCode);
};

return (
<SandpackStack customStyle={customStyle}>
{shouldShowTabs ? <FileTabs closableTabs={closableTabs} /> : null}
return (
<SandpackStack customStyle={customStyle}>
{shouldShowTabs ? <FileTabs closableTabs={closableTabs} /> : null}

<div className={c("code-editor")}>
<CodeMirror
key={activePath}
code={code}
editorState={editorState}
filePath={activePath}
onCodeUpdate={handleCodeUpdate}
showInlineErrors={showInlineErrors}
showLineNumbers={showLineNumbers}
wrapContent={wrapContent}
/>
<div className={c("code-editor")}>
<CodeMirror
key={activePath}
ref={ref}
code={code}
editorState={editorState}
filePath={activePath}
onCodeUpdate={handleCodeUpdate}
showInlineErrors={showInlineErrors}
showLineNumbers={showLineNumbers}
wrapContent={wrapContent}
/>

{showRunButton && status === "idle" ? <RunButton /> : null}
</div>
</SandpackStack>
);
};
{showRunButton && status === "idle" ? <RunButton /> : null}
</div>
</SandpackStack>
);
}
);
3 changes: 2 additions & 1 deletion sandpack-react/src/components/CodeViewer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { SandpackStack } from "../../common/Stack";
import { useActiveCode } from "../../hooks/useActiveCode";
import { useSandpack } from "../../hooks/useSandpack";
import { CodeEditor } from "../CodeEditor";
import type { CodeEditorRef } from "../CodeEditor";
import type { Decorators } from "../CodeEditor/CodeMirror";
import { FileTabs } from "../FileTabs";

Expand All @@ -16,7 +17,7 @@ export interface CodeViewerProps {
code?: string;
}

export const SandpackCodeViewer = forwardRef<HTMLDivElement, CodeViewerProps>(
export const SandpackCodeViewer = forwardRef<CodeEditorRef, CodeViewerProps>(
({ showTabs, showLineNumbers, decorators, code: propCode }, ref) => {
const { sandpack } = useSandpack();
const { code } = useActiveCode();
Expand Down
1 change: 0 additions & 1 deletion sandpack-react/src/presets/CustomSandpack.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import {
useSandpackNavigation,
SandpackStack,
} from "../index";
import { getThemeStyleSheet } from "../themes";

export default {
title: "presets/Custom Sandpack",
Expand Down
1 change: 0 additions & 1 deletion website/docs/src/CustomSandpack.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { SandpackTheme } from "@codesandbox/sandpack-react";
import {
Sandpack as SandpackDefault,
SandpackLayout as SandpackLayoutDefault,
Expand Down
176 changes: 176 additions & 0 deletions website/landing/components/Intro/Examples.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
import {
ClasserProvider,
SandpackLayout,
SandpackPreview,
SandpackProvider,
SandpackThemeProvider,
} from "@codesandbox/sandpack-react";
import { motion, useTransform, useViewportScroll } from "framer-motion";
import { useLayoutEffect, useRef, useState } from "react";

import { Box, List } from "../common";
import { useBreakpoint } from "../common/useBreakpoint";

import { SandpackExample } from "./SandpackExample";
import { CustomExample } from "./Sections/Custom";
import { EditorExample } from "./Sections/Editor";
import { LayoutExample } from "./Sections/Layout";
import { useLayoutExampleContext } from "./Sections/LayoutContext";
import { TemplateExample } from "./Sections/Template";
import { ThemeExample } from "./Sections/Theme";

export const Examples: React.FC = () => {
const { layoutFiles, visibility } = useLayoutExampleContext();

const sandpackSection = useRef<HTMLDivElement>(null);
const { scrollY } = useViewportScroll();
const isLarge = useBreakpoint("2260");

const [sandpackSectionTop, setSandpackSectionTop] = useState(0);
const [sandpackSectionHeight, setSandpackSectionHeight] = useState(0);

useLayoutEffect(() => {
if (!sandpackSection.current) return;

const onResize = () => {
if (!sandpackSection.current) return;

setSandpackSectionTop(sandpackSection.current?.offsetTop);
setSandpackSectionHeight(sandpackSection.current?.offsetHeight);
};

onResize();
window.addEventListener("resize", onResize);

return () => window.removeEventListener("resize", onResize);
}, [sandpackSection]);

const scrollRangeX = [
sandpackSectionTop * 0.7,
(sandpackSectionTop + sandpackSectionHeight) * 0.8,
];

const progressRangeX = ["0vw", isLarge ? "0vw" : "30vw"];
const x = useTransform(scrollY, scrollRangeX, progressRangeX);

return (
<>
<motion.div
ref={sandpackSection}
style={{ x, position: "sticky", top: "calc(50vh - 25%)" }}
>
<Box
css={{
right: 0,
display: "none",
position: "relative",

"@bp2": {
display: "block",
},

"*": {
transition: ".2s ease background, .2s ease color",
},
}}
>
<motion.div
animate={{ opacity: visibility ? 0 : 1 }}
initial={{ opacity: 0 }}
style={{ position: "relative", zIndex: 1 }}
>
<SandpackExample />
</motion.div>

<Box
css={{
position: "absolute",
top: 0,
zIndex: visibility ? 2 : 0,

".custom-wrapper": {
"--sp-border-radius": "10px",
},

".custom-layout": {
width: "342px",
height: "512px",
border: 0,

"@bp1": {
width: "384px",
height: "608px",
},

"@bp2": {
height: "448px",
width: "996px",
},
},

".custom-stack": {
"@bp2": {
height: "100% !important",
width: "100% !important",
},
},
}}
>
<motion.div
animate={{ opacity: visibility ? 1 : 0 }}
initial={{ opacity: 0 }}
>
<SandpackProvider
customSetup={{
files: layoutFiles,
dependencies: { "@codesandbox/sandpack-react": "latest" },
}}
template="react"
>
<ClasserProvider
classes={{
"sp-layout": "custom-layout",
"sp-stack": "custom-stack",
"sp-wrapper": "custom-wrapper",
}}
>
<SandpackThemeProvider>
<SandpackLayout>
<SandpackPreview />
</SandpackLayout>
</SandpackThemeProvider>
</ClasserProvider>
</SandpackProvider>
</motion.div>
</Box>
</Box>
</motion.div>

<List
css={{
display: "flex",
flexDirection: "column",
gap: "148px",

"@bp2": { gap: "0" },
}}
>
<TemplateExample />

<SandpackProvider>
<CustomExample />
</SandpackProvider>

<SandpackProvider>
<EditorExample />
</SandpackProvider>

<ThemeExample />

<SandpackProvider template="react">
<LayoutExample />
</SandpackProvider>
</List>
</>
);
};
50 changes: 50 additions & 0 deletions website/landing/components/Intro/Header.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import content from "../../website.config.json";
import { Box, CodeBlock, Text } from "../common";

export const Header: React.FC = () => {
const { commands, intro } = content;

return (
<Box
css={{
alignItems: "center",
display: "flex",
flexDirection: "column",
gap: "64px",
marginBottom: "100px",
overflow: "hidden",
}}
>
<Text
as="h2"
css={{
fontSize: "36px",
fontWeight: "$semiBold",
letterSpacing: "-0.05em",
lineHeight: "1",
textAlign: "center",

"@bp1": {
fontSize: "72px",
},

"@bp2": {
fontSize: "96px",
},

"@bp3": {
fontSize: "144px",
},
}}
dangerouslySetInnerHTML={{ __html: intro.title }}
/>
<Box
css={{
maxWidth: "calc(100vw - 16px)",
}}
>
<CodeBlock oneLiner>{commands.import}</CodeBlock>
</Box>
</Box>
);
};
Loading

0 comments on commit 5a0b279

Please sign in to comment.