preview-editor.tsx 1.6 KB

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