Skip to content

Commit

Permalink
feat(codeblock): 🐛 add interactive code editor & propstable
Browse files Browse the repository at this point in the history
  • Loading branch information
navin-moorthy committed Jan 4, 2022
1 parent 9ba1d9c commit 5010fe7
Show file tree
Hide file tree
Showing 16 changed files with 740 additions and 119 deletions.
19 changes: 19 additions & 0 deletions .yalc/nextra-renderlesskit-theme-docs/dist/codeblock.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import React from "react";
export declare type StaticCodeProps = {
className?: string;
highlight?: string;
noCopy?: boolean;
};
export declare const StaticCode: React.FC<StaticCodeProps>;
export declare type CopyButtonProps = {
code: string;
};
export declare const CopyButton: React.FC<CopyButtonProps>;
export declare type CodeBlockProps = {
className?: string;
live?: boolean;
render?: boolean;
noCopy?: boolean;
noInline?: boolean;
};
export declare const CodeBlock: React.FC<CodeBlockProps>;
175 changes: 175 additions & 0 deletions .yalc/nextra-renderlesskit-theme-docs/dist/codeblock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
var __defProp = Object.defineProperty;
var __defProps = Object.defineProperties;
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues = (a, b) => {
for (var prop in b || (b = {}))
if (__hasOwnProp.call(b, prop))
__defNormalProp(a, prop, b[prop]);
if (__getOwnPropSymbols)
for (var prop of __getOwnPropSymbols(b)) {
if (__propIsEnum.call(b, prop))
__defNormalProp(a, prop, b[prop]);
}
return a;
};
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
var __objRest = (source, exclude) => {
var target = {};
for (var prop in source)
if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
target[prop] = source[prop];
if (source != null && __getOwnPropSymbols)
for (var prop of __getOwnPropSymbols(source)) {
if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
target[prop] = source[prop];
}
return target;
};

// src/codeblock.tsx
import React from "react";
import { LiveEditor, LiveError, LivePreview, LiveProvider } from "react-live";
import * as Renderlesskit from "@renderlesskit/react-tailwind";
import { RenderlesskitProvider } from "@renderlesskit/react-tailwind";
import { useClipboard } from "@chakra-ui/hooks";
import Highlight, {
defaultProps
} from "prism-react-renderer";
import theme from "prism-react-renderer/themes/palenight";
var THEME = {
plain: {
backgroundColor: "transparent"
},
styles: [
{
types: ["keyword", "builtin"],
style: {
color: "#ff0078",
fontWeight: "bold"
}
},
{
types: ["comment"],
style: {
color: "#999",
fontStyle: "italic"
}
},
{
types: ["variable", "language-javascript"],
style: {
color: "#0076ff"
}
},
{
types: ["attr-name"],
style: {
color: "#d9931e",
fontStyle: "normal"
}
},
{
types: ["boolean", "regex"],
style: {
color: "#d9931e"
}
}
]
};
var StaticCode = (props) => {
const _a = props, { children, className, highlight, noCopy } = _a, rest = __objRest(_a, ["children", "className", "highlight", "noCopy"]);
if (!className)
return /* @__PURE__ */ React.createElement("code", __spreadValues({}, rest), children);
const highlightedLines = highlight ? highlight.split(",").map(Number) : [];
const language = className.replace(/language-/, "");
return /* @__PURE__ */ React.createElement("div", {
className: "relative"
}, /* @__PURE__ */ React.createElement(Highlight, __spreadProps(__spreadValues({}, defaultProps), {
code: children.trim(),
language,
theme: THEME
}), ({ className: className2, style, tokens, getLineProps, getTokenProps }) => /* @__PURE__ */ React.createElement("code", {
className: className2,
style: __spreadValues({}, style)
}, tokens.map((line, i) => /* @__PURE__ */ React.createElement("div", __spreadProps(__spreadValues({
key: i
}, getLineProps({ line, key: i })), {
style: highlightedLines.includes(i + 1) ? {
background: "var(--c-highlight)",
margin: "0 -1rem",
padding: "0 1rem"
} : {}
}), line.map((token, key) => /* @__PURE__ */ React.createElement("span", __spreadValues({
key
}, getTokenProps({ token, key })))))))), !noCopy && /* @__PURE__ */ React.createElement(CopyButton, {
code: children.trim()
}));
};
var CopyButton = ({ code }) => {
const { hasCopied, onCopy } = useClipboard(code);
return /* @__PURE__ */ React.createElement("button", {
className: "absolute right-0 px-4 py-1 text-xs text-gray-800 transform -translate-x-2 translate-y-4 bg-white rounded-md -top-2",
onClick: onCopy
}, hasCopied ? "COPIED!" : "COPY");
};
var transformer = (rawCode, language, noInline) => {
const code = rawCode.replace(/((^|)import[^;]+[; ]+)+/gi, "").replace(/export default \(\) => {((.|\n)*)};/, "render(() => {$1});").replace(/export default \(\) => \(((.|\n)*)\);/, "render($1);").replace(/export default \(\) => ((.|\n)*);/, "render($1);").replace(/export default ((.|\n)*);/, "render($1);");
return language === "jsx" && !noInline ? `<>${code}</>` : code;
};
var CodeBlock = (props) => {
const _a = props, { code, className, live, render, noCopy, noInline } = _a, rest = __objRest(_a, ["code", "className", "live", "render", "noCopy", "noInline"]);
const language = className == null ? void 0 : className.replace(/language-/, "");
const [editorCode, setEditorCode] = React.useState(code.trim());
React.useEffect(() => {
console.log("%ccode", "color: #e57373", code);
if (code)
setEditorCode(code.trim());
}, [code]);
console.log("%ceditorCode", "color: #f200e2", editorCode);
const scope = __spreadValues({
React
}, Renderlesskit);
const liveProviderProps = __spreadValues({
theme,
language,
code: editorCode,
scope,
noInline
}, rest);
console.log("%cliveProviderProps", "color: #731d1d", liveProviderProps);
if (live) {
return /* @__PURE__ */ React.createElement(RenderlesskitProvider, null, /* @__PURE__ */ React.createElement(LiveProvider, __spreadValues({
transformCode: (rawCode) => transformer(rawCode, language, props.noInline)
}, liveProviderProps), /* @__PURE__ */ React.createElement(LivePreview, {
className: "p-6 bg-white border border-gray-600 rounded-md rounded-b-none"
}), /* @__PURE__ */ React.createElement("div", {
className: "relative"
}, /* @__PURE__ */ React.createElement(LiveEditor, {
className: "font-mono text-sm rounded-md rounded-t-none"
}), /* @__PURE__ */ React.createElement(CopyButton, {
code: editorCode
})), /* @__PURE__ */ React.createElement(LiveError, {
className: "mt-0 text-xs text-red-500 bg-red-100 rounded-md rounded-t-none"
})));
}
if (render) {
return /* @__PURE__ */ React.createElement("div", null, /* @__PURE__ */ React.createElement(RenderlesskitProvider, null, /* @__PURE__ */ React.createElement(LiveProvider, __spreadValues({
transformCode: (rawCode) => transformer(rawCode, language, props.noInline)
}, liveProviderProps), /* @__PURE__ */ React.createElement(LivePreview, {
style: { fontFamily: "'Inter', sans-serif" }
}))));
}
return /* @__PURE__ */ React.createElement(StaticCode, __spreadValues({
noCopy,
className
}, props), editorCode);
};
export {
CodeBlock,
CopyButton,
StaticCode
};
28 changes: 14 additions & 14 deletions .yalc/nextra-renderlesskit-theme-docs/dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ var defaultTheme = {
logo: /* @__PURE__ */ React2.createElement(React2.Fragment, null, /* @__PURE__ */ React2.createElement("span", {
className: "mr-2 font-extrabold hidden md:inline"
}, "Nextra"), /* @__PURE__ */ React2.createElement("span", {
className: "text-slate-600 font-normal hidden md:inline"
className: "text-oldGray-600 font-normal hidden md:inline"
}, "The Next Docs Builder")),
head: /* @__PURE__ */ React2.createElement(React2.Fragment, null, /* @__PURE__ */ React2.createElement("meta", {
name: "msapplication-TileColor",
Expand Down Expand Up @@ -876,7 +876,7 @@ var NextLink = ({ route, title, isRTL }) => {
return /* @__PURE__ */ React7.createElement(Link2, {
href: route
}, /* @__PURE__ */ React7.createElement("a", {
className: cn("text-lg font-medium p-4 -m-4 no-underline text-slate-600 hover:text-blue-600 flex items-center", { "ml-2": !isRTL, "mr-2": isRTL }),
className: cn("text-lg font-medium p-4 -m-4 no-underline text-oldGray-600 hover:text-blue-600 flex items-center", { "ml-2": !isRTL, "mr-2": isRTL }),
title
}, title, /* @__PURE__ */ React7.createElement(arrow_right_default, {
className: cn("transform inline flex-shrink-0", {
Expand All @@ -889,7 +889,7 @@ var PrevLink = ({ route, title, isRTL }) => {
return /* @__PURE__ */ React7.createElement(Link2, {
href: route
}, /* @__PURE__ */ React7.createElement("a", {
className: cn("text-lg font-medium p-4 -m-4 no-underline text-slate-600 hover:text-blue-600 flex items-center", { "mr-2": !isRTL, "ml-2": isRTL }),
className: cn("text-lg font-medium p-4 -m-4 no-underline text-oldGray-600 hover:text-blue-600 flex items-center", { "mr-2": !isRTL, "ml-2": isRTL }),
title
}, /* @__PURE__ */ React7.createElement(arrow_right_default, {
className: cn("transform inline flex-shrink-0", {
Expand Down Expand Up @@ -957,7 +957,7 @@ var Footer = ({
}, children, /* @__PURE__ */ React7.createElement("hr", null), config.footer ? /* @__PURE__ */ React7.createElement("div", {
className: "mt-24 flex justify-between flex-col-reverse md:flex-row items-center md:items-end"
}, /* @__PURE__ */ React7.createElement("span", {
className: "text-slate-600"
className: "text-oldGray-600"
}, render_component_default(config.footerText, { locale })), /* @__PURE__ */ React7.createElement("div", {
className: "mt-6"
}), config.footerEditLink ? /* @__PURE__ */ React7.createElement(EditPageLink, {
Expand Down Expand Up @@ -1067,16 +1067,16 @@ function LocaleSwitch({ options, isRTL }) {
})), /* @__PURE__ */ React11.createElement("span", {
className: "sr-only"
}, "Languages")), mounted ? /* @__PURE__ */ React11.createElement("ul", {
className: cn2("locale-dropdown absolute block bg-white dark:bg-dark border dark:border-slate-700 py-1 rounded shadow-lg", { "right-0": !isRTL, "left-0": isRTL })
className: cn2("locale-dropdown absolute block bg-white dark:bg-dark border dark:border-oldGray-700 py-1 rounded shadow-lg", { "right-0": !isRTL, "left-0": isRTL })
}, Array.isArray(options) && options.map((l) => /* @__PURE__ */ React11.createElement("li", {
key: l.locale
}, /* @__PURE__ */ React11.createElement(Link3, {
href: asPath,
locale: l.locale
}, /* @__PURE__ */ React11.createElement("a", {
className: cn2("block no-underline text-current py-2 px-4 hover:bg-slate-200 dark:hover:bg-slate-800 whitespace-nowrap", {
className: cn2("block no-underline text-current py-2 px-4 hover:bg-oldGray-200 dark:hover:bg-oldGray-800 whitespace-nowrap", {
"font-semibold": locale === l.locale,
"bg-slate-100 dark:bg-slate-900": locale === l.locale
"bg-oldGray-100 dark:bg-oldGray-900": locale === l.locale
})
}, l.text))))) : null);
}
Expand Down Expand Up @@ -1182,7 +1182,7 @@ var Search = ({ directories = [] }) => {
}), show ? null : /* @__PURE__ */ React12.createElement("div", {
className: "hidden sm:flex absolute inset-y-0 right-0 py-1.5 pr-1.5"
}, /* @__PURE__ */ React12.createElement("kbd", {
className: "inline-flex items-center px-2 font-sans text-sm font-medium text-slate-400 dark:text-slate-800 dark:border-slate-800 border rounded"
className: "inline-flex items-center px-2 font-sans text-sm font-medium text-oldGray-400 dark:text-oldGray-800 dark:border-oldGray-800 border rounded"
}, "/"))), renderList && /* @__PURE__ */ React12.createElement("ul", {
className: "absolute left-0 z-20 w-full p-0 m-0 mt-1 list-none border divide-y rounded shadow-md md:right-0 top-100 md:w-auto"
}, results.map((res, i) => {
Expand Down Expand Up @@ -1242,7 +1242,7 @@ var Item2 = ({ title, active, href, onMouseOver, excerpt }) => {
}, /* @__PURE__ */ React13.createElement("span", {
className: "font-semibold"
}, title), excerpt ? /* @__PURE__ */ React13.createElement("div", {
className: "text-slate-600"
className: "text-oldGray-600"
}, /* @__PURE__ */ React13.createElement(TextWithHighlights, {
content: excerpt.text,
ranges: excerpt.highlight_ranges
Expand Down Expand Up @@ -1368,7 +1368,7 @@ function Search2() {
}), show ? null : /* @__PURE__ */ React13.createElement("div", {
className: "hidden sm:flex absolute inset-y-0 right-0 py-1.5 pr-1.5"
}, /* @__PURE__ */ React13.createElement("kbd", {
className: "inline-flex items-center px-2 font-sans text-sm font-medium text-slate-400 dark:text-slate-800 dark:border-slate-800 border rounded"
className: "inline-flex items-center px-2 font-sans text-sm font-medium text-oldGray-400 dark:text-oldGray-800 dark:border-oldGray-800 border rounded"
}, "/"))), renderList && /* @__PURE__ */ React13.createElement("ul", {
className: "absolute z-20 p-0 m-0 mt-1 divide-y top-full"
}, results.map((res, i) => {
Expand Down Expand Up @@ -1450,7 +1450,7 @@ function Navbar({
const activeRoute = getFSRoute(asPath, locale).split("#")[0];
const { menu, setMenu } = useMenuContext();
return /* @__PURE__ */ React15.createElement("nav", {
className: "flex items-center bg-white z-20 fixed top-0 left-0 right-0 h-16 border-b border-slate-200 px-6 dark:bg-dark dark:border-slate-900 bg-opacity-[.97] dark:bg-opacity-100"
className: "flex items-center bg-white z-20 fixed top-0 left-0 right-0 h-16 border-b border-oldGray-200 px-6 dark:bg-dark dark:border-oldGray-900 bg-opacity-[.97] dark:bg-opacity-100"
}, /* @__PURE__ */ React15.createElement("div", {
className: "w-full flex items-center mr-2"
}, /* @__PURE__ */ React15.createElement(Link6, {
Expand All @@ -1469,7 +1469,7 @@ function Navbar({
href,
key: page.route
}, /* @__PURE__ */ React15.createElement("a", {
className: cn5("no-underline whitespace-nowrap mr-4 hidden md:inline-block", page.route === activeRoute || activeRoute.startsWith(page.route + "/") ? "text-current" : "text-slate-500")
className: cn5("no-underline whitespace-nowrap mr-4 hidden md:inline-block", page.route === activeRoute || activeRoute.startsWith(page.route + "/") ? "text-current" : "text-oldGray-500")
}, page.title));
}) : null, /* @__PURE__ */ React15.createElement("div", {
className: "flex-1"
Expand Down Expand Up @@ -1662,7 +1662,7 @@ function Sidebar({
height: "calc(100vh - 4rem)"
}
}, /* @__PURE__ */ React16.createElement("div", {
className: "sidebar border-slate-200 dark:border-slate-900 w-full p-4 pb-40 md:pb-16 h-full overflow-y-auto"
className: "sidebar border-oldGray-200 dark:border-oldGray-900 w-full p-4 pb-40 md:pb-16 h-full overflow-y-auto"
}, /* @__PURE__ */ React16.createElement("div", {
className: "mb-4 block md:hidden"
}, config.customSearch || (config.search ? config.unstable_stork ? /* @__PURE__ */ React16.createElement(Search2, null) : /* @__PURE__ */ React16.createElement(search_default, {
Expand Down Expand Up @@ -1716,7 +1716,7 @@ function ToC({
style: indent(heading.depth)
}, /* @__PURE__ */ React17.createElement("a", {
href: `#${slug}`,
className: cn7("no-underline hover:text-slate-900 dark:hover:text-slate-100", state && state.isActive ? "text-slate-900 dark:text-slate-100 font-semibold" : "text-slate-600")
className: cn7("no-underline hover:text-oldGray-900 dark:hover:text-oldGray-100", state && state.isActive ? "text-oldGray-900 dark:text-oldGray-100 font-semibold" : "text-oldGray-600")
}, text));
})) : null);
}
Expand Down
20 changes: 0 additions & 20 deletions .yalc/nextra-renderlesskit-theme-docs/dist/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,4 @@
/// <reference types="react" />
import { Heading } from "mdast";
export interface PageMapItem {
name: string;
route: string;
locale?: string;
children?: PageMapItem[];
frontMatter?: Record<string, any>;
meta?: Record<string, any>;
active?: boolean;
}
export interface PageOpt {
filename: string;
route: string;
meta: Record<string, any>;
pageMap: PageMapItem[];
titleText: string | null;
headings?: Heading[];
hasH1: boolean;
}
export { Heading };
export interface DocsThemeConfig {
docsRepositoryBase?: string;
titleSuffix?: string | React.FC<{
Expand Down
8 changes: 7 additions & 1 deletion .yalc/nextra-renderlesskit-theme-docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
},
"./bleed": {
"import": "./dist/bleed.js"
},
"./codeblock": {
"import": "./dist/codeblock.js"
}
},
"main": "dist/index.js",
Expand Down Expand Up @@ -70,8 +73,10 @@
]
},
"dependencies": {
"@chakra-ui/hooks": "1.7.1",
"@mdx-js/react": "2.0.0-rc.2",
"@reach/skip-nav": "0.16.0",
"@renderlesskit/react-tailwind": "0.0.1-alpha.37",
"classnames": "2.3.1",
"focus-visible": "5.2.0",
"github-slugger": "1.4.0",
Expand All @@ -81,6 +86,7 @@
"next-themes": "0.0.15",
"parse-git-url": "1.0.1",
"prism-react-renderer": "1.2.1",
"react-live": "2.3.0",
"title": "3.4.3"
},
"peerDependencies": {
Expand All @@ -94,5 +100,5 @@
"emoji": "emoji",
"editor": false
},
"yalcSig": "ecdf36723a243c305d9261fd73b2c6b3"
"yalcSig": "0a6245f7e065a391412a9662a582e25b"
}
Loading

0 comments on commit 5010fe7

Please sign in to comment.