Skip to content

Commit

Permalink
feat(codeblock): ✨ add finished codeblock to mdx components
Browse files Browse the repository at this point in the history
  • Loading branch information
navin-moorthy committed Jul 19, 2022
1 parent d6b385c commit 0e3660e
Show file tree
Hide file tree
Showing 15 changed files with 351 additions and 180 deletions.
19 changes: 10 additions & 9 deletions components/ClipBoardCopyButton.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
import { Button } from "@adaptui/react-tailwind";
import { useClipboard } from "@chakra-ui/hooks";

import { CopiedIcon, CopyIcon } from "./Icons";
import { CopyButton } from "./CopyButton";
import { Tooltip, TooltipProps } from "./Tooltip";

export type ClipboardCopyButtonProps = {
export type ClipboardCopyButtonProps = TooltipProps & {
content: string;
};

export const ClipboardCopyButton: React.FC<ClipboardCopyButtonProps> = ({
content,
...props
}) => {
const { hasCopied, onCopy } = useClipboard(content);

return (
<Button
size="sm"
onClick={onCopy}
iconOnly={hasCopied ? <CopiedIcon /> : <CopyIcon />}
<Tooltip
withArrow
placement="left"
anchor={<CopyButton hasCopied={hasCopied} onCopy={onCopy} />}
content={hasCopied ? "Copied!" : "Copy"}
{...props}
/>
);
};

export default ClipboardCopyButton;
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import ClipboardCopyButton from "../ClipBoardCopyButton";
import { ClipboardCopyButton } from "../ClipBoardCopyButton";

export type CopyCodeBlockButtonProps = {
code: string;
Expand All @@ -10,10 +10,8 @@ export const CopyCodeBlockButton: React.FC<
const { code } = props;

return (
<span className="absolute right-0 -top-2 -translate-x-2 translate-y-4 transform">
<span className="absolute right-2 top-2">
<ClipboardCopyButton content={code} />
</span>
);
};

export default CopyCodeBlockButton;
16 changes: 11 additions & 5 deletions components/Codeblock/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,21 @@ import lightTheme from "prism-react-renderer/themes/vsLight";

import { scope } from "../../utils";

import CopyCodeBlockButton from "./CopyCodeButton";
import { CopyCodeBlockButton } from "./CopyCodeBlockButton";
import { transformer } from "./transformer";

export type CodeblockProps = {
code: string;
noCopy?: boolean;
noInline?: boolean;
children?: React.ReactNode | any;
};

export const Codeblock = (props: CodeblockProps) => {
const { code, noCopy, noInline, ...rest } = props;
const { code, noCopy, noInline, children, ...rest } = props;
const _code = children?.props?.children?.props?.children ?? code;

const [editorCode, setEditorCode] = React.useState(code.trim());
const [editorCode, setEditorCode] = React.useState(_code.trim());
const handleChange = React.useCallback((code: string) => {
setEditorCode(code.trim());
}, []);
Expand All @@ -35,7 +38,10 @@ export const Codeblock = (props: CodeblockProps) => {

return (
<AdaptUIProvider>
<LiveProvider {...liveProviderProps}>
<LiveProvider
transformCode={rawCode => transformer(rawCode, noInline)}
{...liveProviderProps}
>
<div className="mt-6 rounded-md border border-[#ededed] bg-transparent">
<LivePreview className="rounded-t-md bg-white-900 p-6" />
<div className="relative">
Expand All @@ -52,4 +58,4 @@ export const Codeblock = (props: CodeblockProps) => {
);
};

export default Codeblock;
export * from "./CopyCodeBlockButton";
15 changes: 15 additions & 0 deletions components/Codeblock/transformer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export const transformer = (rawCode: any, noInline: any) => {
const code = rawCode
// remove imports
.replace(/((^|)import[^;]+[; ]+)+/gi, "")
// replace `export default => {*};` with `render(() => {*});`
.replace(/export default \(\) => {((.|\n)*)};/, "render(() => {$1});")
// replace `export default => (*);` with `render(*);`
.replace(/export default \(\) => \(((.|\n)*)\);/, "render($1);")
// replace `export default => *;` with `render(*);`
.replace(/export default \(\) => ((.|\n)*);/, "render($1);")
.replace(/export const App = ((.|\n)*);/, "render($1);")
.replace(/export default ((.|\n)*);/, "render($1);");

return !noInline ? `<>${code}</>` : code;
};
25 changes: 25 additions & 0 deletions components/CopyButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import React, { forwardRef } from "react";
import { Button, ButtonProps } from "@adaptui/react-tailwind";

import { CopiedIcon, CopyIcon } from "./Icons";

export type CopyButtonProps = ButtonProps & {
hasCopied: boolean;
onCopy: () => void;
};

export const CopyButton = forwardRef<HTMLButtonElement, CopyButtonProps>(
(props, ref) => {
const { onCopy, hasCopied, ...rest } = props;

return (
<Button
ref={ref}
size="sm"
onClick={onCopy}
iconOnly={hasCopied ? <CopiedIcon /> : <CopyIcon />}
{...rest}
/>
);
},
);
2 changes: 1 addition & 1 deletion components/InteractiveCodeblock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import lightTheme from "prism-react-renderer/themes/vsLight";
import { setup, tw } from "twind";
import * as colors from "twind/colors";

import CopyCodeBlockButton from "./Codeblock/CopyCodeButton";
import { CopyCodeBlockButton } from "./Codeblock/CopyCodeBlockButton";

setup({
preflight: false, // do not include base style reset (default: use tailwind preflight)
Expand Down
15 changes: 15 additions & 0 deletions components/Pre.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { CopyCodeBlockButton } from "./Codeblock/CopyCodeBlockButton";

export const Pre = (props: { children?: React.ReactNode }) => {
const { children, ...rest } = props;
console.log("%cprops", "color: #00ff88", props);

return (
<div className="relative">
<pre {...rest}>
<>{children}</>
</pre>
<CopyCodeBlockButton code={"copy"} />
</div>
);
};
16 changes: 16 additions & 0 deletions components/Tooltip.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import * as React from "react";
import {
Tooltip as AdaptTooltip,
TooltipProps as AdaptTooltipProps,
TooltipWrapper,
} from "@adaptui/react-tailwind";

export type TooltipProps = AdaptTooltipProps & {};

export const Tooltip: React.FC<TooltipProps> = props => {
return (
<AdaptTooltip {...props}>
<TooltipWrapper style={{ zIndex: 9999 }} />
</AdaptTooltip>
);
};
3 changes: 3 additions & 0 deletions components/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
export * from "./ClipBoardCopyButton";
export * from "./Codeblock";
export * from "./ComponentLinks";
export * from "./InteractiveCodeblock";
export * from "./Pre";
export * from "./PropsTable";
export * from "./RegionTable";
export * from "./Tooltip";
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
},
"dependencies": {
"@adaptui/react": "1.0.0-alpha.7",
"@adaptui/react-tailwind": "0.1.0-alpha.3",
"@adaptui/react-tailwind": "0.1.0-alpha.4",
"@chakra-ui/hooks": "2.0.4",
"@reach/skip-nav": "0.17.0",
"ariakit": "2.0.0-next.34",
Expand All @@ -83,6 +83,7 @@
"@babel/core": "7.18.9",
"@commitlint/cli": "17.0.3",
"@commitlint/config-conventional": "17.0.3",
"@mdx-js/react": "^2.1.2",
"@next/eslint-plugin-next": "12.2.2",
"@release-it/conventional-changelog": "5.0.0",
"@testing-library/dom": "8.16.0",
Expand Down
6 changes: 5 additions & 1 deletion pages/_app.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import type { ReactElement, ReactNode } from "react";
import { AdaptUIProvider } from "@adaptui/react-tailwind";
import { MDXProvider } from "@mdx-js/react";
import type { NextPage } from "next";
import type { AppProps } from "next/app";
import { mdxComponents } from "utils";

import "@/styles/global.css";
import "@/styles/nextra.css";
Expand All @@ -19,7 +21,9 @@ export default function MyApp({ Component, pageProps }: AppPropsWithLayout) {

return getLayout(
<AdaptUIProvider>
<Component {...pageProps} />
<MDXProvider components={mdxComponents}>
<Component {...pageProps} />
</MDXProvider>
</AdaptUIProvider>,
);
}
Loading

0 comments on commit 0e3660e

Please sign in to comment.