在本文中,我们将探索 SandPack(CodeSandbox 的一个流行的 Playground 框架),并讨论如何使用它为用户创建一个更加动态和交互的环境。
本文涵盖了您需要了解的有关 SandPack 的几乎所有基本知识。但是,我的博客上详细讨论了更高级的功能,例如挂钩和自定义组件以及很酷的自定义选项。
查看本文详细版本
SandPack 是一个组件工具包,用于为博客和技术文档构建实时代码编辑器。在本文中,我们将重点关注 sandpack-react 而不是 sandpack-client,它是一个轻量级的 JavaScript 捆绑器。
SandPack 的脱颖而出之处在于提供广泛的定制选项。另外,它真的很容易上手。 sandpack-react 最有用的功能包括:
要开始使用 sandpack-react,请运行此 npm 或纱线命令:
npm 我@codesandbox/sandpack-react
或
纱线添加@codesandbox/sandpack-react
接下来,导入 Sandpack 游乐场并使用以下代码渲染它:
import { Sandpack } from "@codesandbox/sandpack-react"; export default function App() { return <Sandpack /> }
>组件设置了一个空的游乐场供您直接跳进去。默认情况下,游乐场包含一个基本的 React 模板。让我们看看自定义模板、主题等的基本道具:
让我们调整默认的 Playground 以适应我们的风格,并创建一个有趣的示例来玩。自定义编辑器以匹配您的网站主题可以使其无缝融合,而不是感觉像第三方嵌入。首先,让我们使用 files 属性创建一个简单的计数器按钮。除了 App.js 文件之外,我们还将创建 App.css 文件。
看看下面的示例和代码:
在此示例中,计数器组件在 Playground 中渲染。文件对象包含 App.js 和 App.css 的代码。我们从前面提到的预构建列表中选择了一个主题,该主题源自沙包主题,添加了一丝风格。行号也已设置为 true。
此外,您可以轻松自定义游乐场的布局。这可以通过应用自定义类或利用 SandPack 提供的预构建选项来完成。例如,您可以使用这样的自定义类:
<Sandpack theme={theme} template="react" options={{ classes: { "sp-wrapper": "custom-wrapper", "sp-layout": "custom-layout", "sp-tab-button": "custom-tab", }, }} />
然后,您可以使用 CSS 调整外观和布局,从而更好地控制视觉设计。
Another useful feature is the ability to switch between different layout modes. SandPack offers three modes: preview, tests, and console. The default mode is preview, while the tests mode provides a suite for running tests and the console mode renders a terminal/console component instead of a preview window. The console mode is useful for displaying outputs of server side logic. You can also switch the layout direction using the rtl (Right to left layout) option.
Besides the editor itself, the output display can be customized as well. For instance, you can choose to show or hide the console, change the layout, or even modify the appearance of the preview window. Pretty cool right!. Code editors often have heavily customized editing windows, but the actual output is not paid attention to as much.
The console displays all sorts of errors and console logs. Depending on the type of code snippet being showcased, you'd either want to show or hide the console. You can also toggle the visibility of the show console button. By default, the console is hidden. As with all the SandPack components, the styling can be individually modified using custom CSS classes.
<Sandpack template="react" files={files} theme={nightOwl} options={{ showConsole: true, showConsoleButton: true, }} />;
Besides the console, the display window itself can be customized as well. For example, you can turn the navigator bar on or off with the showNavigator option and decide if you want the panels to be resizable with the resizablePanels option.
<Sandpack template="react" files={files} theme={nightOwl} options={{ showLineNumbers: true, showNavigator: true, resizablePanels: true, }} />
The result will look somewhat like this:
Sandpack isn't just easy to use—it's also super customizable. This makes it a great choice for blogs, documentation, or any platform where live code editing adds value, while still allowing developers to customize it based on their sites.
You can check the detailed version of this article here
Thanks for reading!
以上是使用 React 和 SandPack 建立一體化程式碼編輯器的詳細內容。更多資訊請關注PHP中文網其他相關文章!