Example.tsx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 = uiUrl;
  38. }
  39. let content = example;
  40. if (!fullpage) {
  41. content = `<main class="min-vh-100 ${vertical ? 'p-4' : 'py-4 px-4'}${
  42. centered
  43. ? ' d-flex justify-content-center align-items-center flex-wrap'
  44. : ''
  45. }${vcentered ? ' d-flex justify-content-center flex-column' : ''}">
  46. ${
  47. columns
  48. ? `<div class="mx-auto w-100" style="max-width: ${columns * 20}rem">`
  49. : ''
  50. }
  51. ${
  52. separated
  53. ? vertical
  54. ? '<div class="space-y">'
  55. : '<div class="space-x">'
  56. : ''
  57. }
  58. ${example}
  59. ${separated ? '</div>' : ''}
  60. ${columns ? '</div>' : ''}
  61. </main>`;
  62. }
  63. example = example
  64. .replace(/a href="[^"]+"/g, 'a href="javascript:void(0)"')
  65. .replace(/action="[^"]+"/g, 'action="javascript:void(0)"');
  66. return `<html lang="en">
  67. <head>
  68. <title>Example</title>
  69. <link rel="stylesheet" href="${assetsUrl}/dist/css/tabler.css">
  70. ${
  71. plugins
  72. ? plugins.map(
  73. (plugin) =>
  74. ` <link rel="stylesheet" href="${assetsUrl}/dist/css/tabler-${plugin}.css" />`
  75. )
  76. : ''
  77. }
  78. </head>
  79. <body class="h-100${background ? ` bg-${background}` : ''}${
  80. scrollable || fullpage ? ' auto-scroll' : ' no-scroll'
  81. }"${!background && ' style="--tblr-body-bg: #fbfcfd"'}>
  82. ${content}
  83. ${
  84. vendors
  85. ? `<link rel="stylesheet" href="${assetsUrl}/dist/css/tabler-vendors.css" />`
  86. : ''
  87. }
  88. <script src="${assetsUrl}/dist/js/tabler.js"></script>
  89. </body>
  90. </html>`;
  91. };
  92. export default function Example({
  93. height = 200,
  94. vertical = false,
  95. background = '',
  96. scrollable = false,
  97. columns = 0,
  98. centered = false,
  99. vcentered = false,
  100. separated = false,
  101. vendors = false,
  102. overview = false,
  103. fullpage = false,
  104. plugins = '',
  105. resizable = false,
  106. libs = '',
  107. html,
  108. }) {
  109. const [iframeHeight, setIframeHeight] = useState(height);
  110. const srcDoc = previewHtml(html, {
  111. vertical,
  112. background,
  113. scrollable,
  114. columns,
  115. centered,
  116. vcentered,
  117. separated,
  118. vendors,
  119. overview,
  120. fullpage,
  121. plugins: plugins ? plugins.split(',') : [],
  122. libs: libs ? libs.split(',') : [],
  123. });
  124. return (
  125. <div className="example">
  126. <iframe className={clsx('example-frame', {
  127. 'example-frame-resizable': resizable,
  128. })} srcDoc={srcDoc} style={{ height: iframeHeight }} />
  129. </div>
  130. );
  131. }