在本文中,我们将探索 SandPack(CodeSandbox 的一个流行的 Playground 框架),并讨论如何使用它为用户创建一个更加动态和交互的环境。
本文涵盖了您需要了解的有关 SandPack 的几乎所有基本知识。但是,我的博客上详细讨论了更高级的功能,例如挂钩和自定义组件以及很酷的自定义选项。
查看本文详细版本
什么是沙包
SandPack 是一个组件工具包,用于为博客和技术文档构建实时代码编辑器。在本文中,我们将重点关注 sandpack-react 而不是 sandpack-client,它是一个轻量级的 JavaScript 捆绑器。
SandPack 的脱颖而出之处在于提供广泛的定制选项。另外,它真的很容易上手。 sandpack-react 最有用的功能包括:
- 针对流行语言和框架的预构建模板
- 为编辑器提供大量预构建主题以及创建自定义主题的选项。
- 支持所有 npm 依赖项和主要 JavaScript 框架。
- 自定义 UI 和游乐场几乎各个方面的选项。
- 您可以使用预构建的可组合组件创建一个完全自定义的 Playground。
- 提供者和自定义挂钩可用于创建自定义组件。
游乐场概览
要开始使用 sandpack-react,请运行此 npm 或纱线命令:
npm 我@codesandbox/sandpack-react
或
纱线添加@codesandbox/sandpack-react
接下来,导入 Sandpack 游乐场并使用以下代码渲染它:
import { Sandpack } from "@codesandbox/sandpack-react"; export default function App() { return <sandpack></sandpack> }
>组件设置了一个空的游乐场供您直接跳进去。默认情况下,游乐场包含一个基本的 React 模板。让我们看看自定义模板、主题等的基本道具:
- template:此属性接受预定义的模板列表。默认情况下,设置为 vanilla。
- files:这是一个非常有用的属性。您可以使用自定义代码创建多个文件,类似于常规文件夹结构。文件对象包含一个值(相对文件路径)和键(文件内容)。然后,该对象中的文件也会自动显示在选项卡中。
-
选项:您可以使用选项对象自定义多个功能。您可以在此处查看完整列表。一些最有用的包括:
- showLineNumbers:切换行号的可见性。
- showTabs:切换选项卡的可见性。
- 类:您可以将自定义类名称分配给现有模板类以进行进一步自定义。
- 依赖项:依赖项对象可以包含应用程序所需的任何 NPM 包。格式和语法与 package.json 文件类似。
- 主题:您可以选择预先构建的主题或分配完全自定义的主题。
定制游乐场
让我们调整默认的 Playground 以适应我们的风格,并创建一个有趣的示例来玩。自定义编辑器以匹配您的网站主题可以使其无缝融合,而不是感觉像第三方嵌入。首先,让我们使用 files 属性创建一个简单的计数器按钮。除了 App.js 文件之外,我们还将创建 App.css 文件。
看看下面的示例和代码:
在此示例中,计数器组件在 Playground 中渲染。文件对象包含 App.js 和 App.css 的代码。我们从前面提到的预构建列表中选择了一个主题,该主题源自沙包主题,添加了一丝风格。行号也已设置为 true。
此外,您可以轻松自定义游乐场的布局。这可以通过应用自定义类或利用 SandPack 提供的预构建选项来完成。例如,您可以使用这样的自定义类:
<sandpack theme="{theme}" template="react" options="{{" classes:></sandpack>
然后,您可以使用 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.
Customizing the Output
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:></sandpack>;
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: resizablepanels:></sandpack>
The result will look somewhat like this:
Conclusion
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中文網其他相關文章!

JavaScript字符串替換方法詳解及常見問題解答 本文將探討兩種在JavaScript中替換字符串字符的方法:在JavaScript代碼內部替換和在網頁HTML內部替換。 在JavaScript代碼內部替換字符串 最直接的方法是使用replace()方法: str = str.replace("find","replace"); 該方法僅替換第一個匹配項。要替換所有匹配項,需使用正則表達式並添加全局標誌g: str = str.replace(/fi

本教程向您展示瞭如何將自定義的Google搜索API集成到您的博客或網站中,提供了比標準WordPress主題搜索功能更精緻的搜索體驗。 令人驚訝的是簡單!您將能夠將搜索限制為Y

本文系列在2017年中期進行了最新信息和新示例。 在此JSON示例中,我們將研究如何使用JSON格式將簡單值存儲在文件中。 使用鍵值對符號,我們可以存儲任何類型的

因此,在這裡,您準備好了解所有稱為Ajax的東西。但是,到底是什麼? AJAX一詞是指用於創建動態,交互式Web內容的一系列寬鬆的技術。 Ajax一詞,最初由Jesse J創造

增強您的代碼演示文稿:10個語法熒光筆針對開發人員在您的網站或博客上共享代碼段的開發人員是開發人員的常見實踐。 選擇合適的語法熒光筆可以顯著提高可讀性和視覺吸引力。 t

利用輕鬆的網頁佈局:8 ESTISSEL插件jQuery大大簡化了網頁佈局。 本文重點介紹了簡化該過程的八個功能強大的JQuery插件,對於手動網站創建特別有用

核心要點 JavaScript 中的 this 通常指代“擁有”該方法的對象,但具體取決於函數的調用方式。 沒有當前對象時,this 指代全局對象。在 Web 瀏覽器中,它由 window 表示。 調用函數時,this 保持全局對象;但調用對象構造函數或其任何方法時,this 指代對象的實例。 可以使用 call()、apply() 和 bind() 等方法更改 this 的上下文。這些方法使用給定的 this 值和參數調用函數。 JavaScript 是一門優秀的編程語言。幾年前,這句話可

本文介紹了關於JavaScript和JQuery模型視圖控制器(MVC)框架的10多個教程的精選選擇,非常適合在新的一年中提高您的網絡開發技能。 這些教程涵蓋了來自Foundatio的一系列主題


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

Atom編輯器mac版下載
最受歡迎的的開源編輯器