Example.tsx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. if (!fullpage) {
  41. 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' : ''}">
  42. ${columns ? `<div class="mx-auto w-100" style="max-width: ${columns * 20}rem">` : ''}
  43. ${separated ? (vertical ? '<div class="space-y">' : '<div class="space-x">') : ''}
  44. ${example}
  45. ${separated ? '</div>' : ''}
  46. ${columns ? '</div>' : ''}
  47. </main>`;
  48. }
  49. example = example.replace(/a href="[^"]+"/g, 'a href="javascript:void(0)"').replace(/action="[^"]+"/g, 'action="javascript:void(0)"');
  50. return `<html lang="en">
  51. <head>
  52. <title>Example</title>
  53. <link rel="stylesheet" href="${assetsUrl}/dist/css/tabler.css">
  54. ${plugins ? plugins.map((plugin) => ` <link rel="stylesheet" href="${assetsUrl}/dist/css/tabler-${plugin}.css" />`) : ''}
  55. </head>
  56. <body class="h-100${background ? ` bg-${background}` : ''}${scrollable || fullpage ? ' auto-scroll' : ' no-scroll'}"${!background && ' style="--tblr-body-bg: #fbfcfd"'}>
  57. ${content}
  58. ${vendors ? `<link rel="stylesheet" href="${assetsUrl}/dist/css/tabler-vendors.css" />` : ''}
  59. <script src="${assetsUrl}/dist/js/tabler.js"></script>
  60. </body>
  61. </html>`;
  62. };
  63. export default function Example({
  64. height = 200,
  65. vertical = false,
  66. background = '',
  67. scrollable = false,
  68. columns = 0,
  69. centered = false,
  70. vcentered = false,
  71. separated = false,
  72. vendors = false,
  73. overview = false,
  74. fullpage = false,
  75. plugins = '',
  76. resizable = false,
  77. libs = '',
  78. html,
  79. }) {
  80. const [iframeHeight, setIframeHeight] = useState(height);
  81. const srcDoc = previewHtml(html, {
  82. vertical,
  83. background,
  84. scrollable,
  85. columns,
  86. centered,
  87. vcentered,
  88. separated,
  89. vendors,
  90. overview,
  91. fullpage,
  92. plugins: plugins ? plugins.split(',') : [],
  93. libs: libs ? libs.split(',') : [],
  94. });
  95. return (
  96. <div className="example">
  97. <iframe
  98. className={clsx('example-frame', {
  99. 'example-frame-resizable': resizable,
  100. })}
  101. srcDoc={srcDoc}
  102. style={{ height: iframeHeight }}
  103. />
  104. </div>
  105. );
  106. }