Example.tsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. 'use client';
  2. import { useState } from 'react';
  3. import clsx from 'clsx';
  4. import { uiCdnUrl, uiUrl } from '@/config/site';
  5. import libsSrc from '../../src/pages/_data/libs.json';
  6. const previewHtml = (
  7. example,
  8. {
  9. background = '',
  10. vertical = false,
  11. scrollable = false,
  12. columns = 0,
  13. centered = false,
  14. vcentered = false,
  15. separated = false,
  16. vendors = false,
  17. overview = false,
  18. fullpage = false,
  19. plugins = [],
  20. libs = [],
  21. }: {
  22. vertical?: boolean;
  23. background?: string;
  24. scrollable?: boolean;
  25. columns?: number;
  26. centered?: boolean;
  27. vcentered?: boolean;
  28. separated?: boolean;
  29. vendors?: boolean;
  30. overview?: boolean;
  31. fullpage?: boolean;
  32. plugins?: string[];
  33. libs?: string[];
  34. },
  35. ) => {
  36. let assetsUrl = uiCdnUrl;
  37. if (process.env.TABLER_LOCAL) {
  38. assetsUrl = 'http://localhost:3000';
  39. }
  40. let content = example;
  41. example = example
  42. .replace(/ href="[^"]+"/g, ' href="javascript:void(0)"')
  43. .replace(/action="[^"]+"/g, 'action="javascript:void(0)"');
  44. if (!fullpage) {
  45. content = `<main class="min-vh-100 ${vertical ? 'p-4' : 'py-4 px-4'}${centered ? ' d-flex justify-content-center align-items-center flex-wrap' : ''}${vcentered ? ' d-flex justify-content-center flex-column' : ''}">
  46. ${columns ? `<div class="mx-auto w-100" style="max-width: ${columns * 20}rem">` : ''}
  47. ${separated ? (vertical ? '<div class="space-y">' : '<div class="space-x">') : ''}
  48. ${example}
  49. ${separated ? '</div>' : ''}
  50. ${columns ? '</div>' : ''}
  51. </main>`;
  52. }
  53. return `<html lang="en">
  54. <head>
  55. <title>Example</title>
  56. <link rel="stylesheet" href="${assetsUrl}/dist/css/tabler.css">
  57. ${plugins ? plugins.map((plugin) => ` <link rel="stylesheet" href="${assetsUrl}/dist/css/tabler-${plugin}.css" />`) : ''}
  58. ${vendors ? `<link rel="stylesheet" href="${assetsUrl}/dist/css/tabler-vendors.css" />` : ''}
  59. ${libs ? libs.map((lib) => (libsSrc.css[lib] ? `<link rel="stylesheet" href="${assetsUrl}/dist/libs/${libsSrc.css[lib]}" />` : '')) : ''}
  60. </head>
  61. <body class="h-100${background ? ` bg-${background}` : ''}${scrollable || fullpage ? ' auto-scroll' : ' no-scroll'}"${!background && ' style="--tblr-body-bg: #fbfcfd"'}>
  62. ${content}
  63. <script src="${assetsUrl}/dist/js/tabler.js"></script>
  64. ${libs ? libs.map((lib) => (libsSrc.js[lib] ? `<script src="${assetsUrl}/dist/libs/${libsSrc.js[lib]}"></script>` : '')) : ''}
  65. </body>
  66. </html>`;
  67. };
  68. export default function Example({
  69. height = 200,
  70. vertical = false,
  71. background = '',
  72. scrollable = false,
  73. columns = 0,
  74. centered = false,
  75. vcentered = false,
  76. separated = false,
  77. vendors = false,
  78. overview = false,
  79. fullpage = false,
  80. plugins = '',
  81. resizable = false,
  82. libs = '',
  83. html,
  84. }) {
  85. const [iframeHeight, setIframeHeight] = useState(height);
  86. const srcDoc = previewHtml(html, {
  87. vertical,
  88. background,
  89. scrollable,
  90. columns,
  91. centered,
  92. vcentered,
  93. separated,
  94. vendors,
  95. overview,
  96. fullpage,
  97. plugins: plugins ? plugins.split(',') : [],
  98. libs: libs ? libs.split(',') : [],
  99. });
  100. return (
  101. <>
  102. <div className="example">
  103. <iframe
  104. className={clsx('example-frame', {
  105. 'example-frame-resizable': resizable,
  106. })}
  107. srcDoc={srcDoc}
  108. style={{ height: iframeHeight }}
  109. />
  110. </div>
  111. </>
  112. );
  113. }