preview-editor.tsx 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /**
  2. * Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
  3. * SPDX-License-Identifier: MIT
  4. */
  5. import { useMemo } from 'react';
  6. import { useDark } from '@rspress/core/runtime';
  7. import {
  8. SandpackProvider,
  9. SandpackLayout,
  10. SandpackCodeEditor,
  11. SandpackFiles,
  12. // SandpackPreview,
  13. } from '@codesandbox/sandpack-react';
  14. export const PreviewEditor = ({
  15. files,
  16. children,
  17. previewStyle,
  18. dependencies,
  19. editorStyle,
  20. codeInRight,
  21. }: {
  22. files: SandpackFiles;
  23. children: JSX.Element;
  24. previewStyle?: React.CSSProperties;
  25. dependencies?: Record<string, string>;
  26. editorStyle?: React.CSSProperties;
  27. codeInRight?: boolean;
  28. }) => {
  29. const dark = useDark();
  30. const theme = useMemo(() => (dark ? 'dark' : 'light'), [dark]);
  31. const content = codeInRight ? (
  32. <>
  33. <SandpackLayout style={{ width: '100%', display: 'flex' }}>
  34. <div className="light-mode preview-ediitor" style={previewStyle}>
  35. {children}
  36. </div>
  37. <SandpackCodeEditor style={editorStyle} readOnly />
  38. </SandpackLayout>
  39. </>
  40. ) : (
  41. <>
  42. <SandpackLayout style={previewStyle}>
  43. <div className="light-mode preview-ediitor">{children}</div>
  44. {/* <SandpackPreview /> */}
  45. </SandpackLayout>
  46. <SandpackLayout>
  47. <SandpackCodeEditor style={editorStyle} readOnly />
  48. </SandpackLayout>
  49. </>
  50. );
  51. return (
  52. <SandpackProvider
  53. template="react"
  54. theme={theme}
  55. customSetup={{
  56. dependencies,
  57. }}
  58. files={{
  59. ...files,
  60. '/App.js': {
  61. code: '',
  62. hidden: true,
  63. },
  64. }}
  65. onChange={(v) => {
  66. console.log('debugger', v);
  67. }}
  68. >
  69. {content}
  70. </SandpackProvider>
  71. );
  72. };