Example.tsx 2.9 KB

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