0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

figma make で作成したモックをnextに組み込む

0
Posted at

figma make とは

プロンプトを与えるのみでデザインを行ってくれるツールです。

figma make の技術スタック

  • ビルド環境: Viteベース
  • フレームワーク: React + Tailwind CSS
  • 出力形式: SPA構成(index.html + hashed JS/CSS)

PJではNext.js + Chakra UIを技術スタックとして採用しているため、このままではfigma make で作成したモックを組み込むことはできません。
そのため、vite buildで生成したhtml、js、cssファイルをpublicに配置し、配信する必要があります。
その後下記のようなmock専用のコンポーネントを作成し、next側に組み込むことに成功しました。

import { headers } from "next/headers";

import fs from "node:fs";
import path from "node:path";

interface MockHtmlRendererProps {
  mockHtmlDirName: string;
}

export default async function MockHtmlRenderer({ mockHtmlDirName }: MockHtmlRendererProps) {
  const headersList = await headers();

  // 不要な文字列を削除してディレクトリ名を正規化
  const normalizedDirName = mockHtmlDirName
    .replace(/^public\/mock\/?/, "") // 先頭のpublic/mockを削除
    .replace(/\/index\.html$/, "") // 末尾の/index.htmlを削除
    .replace(/^\/+|\/+$/g, ""); // 先頭と末尾のスラッシュを削除

  const htmlPath = path.join(process.cwd(), `public/mock/${normalizedDirName}/index.html`);
  const html = fs.readFileSync(htmlPath, "utf8");

  return <div suppressHydrationWarning dangerouslySetInnerHTML={{ __html: html }} />;
}


ソースコードについて一部補足を入れておきます。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?