ホームページ ウェブフロントエンド jsチュートリアル shadcn/UI とマニフェストを使用してわずか数分でニュースレターの登録フォームを作成する方法

shadcn/UI とマニフェストを使用してわずか数分でニュースレターの登録フォームを作成する方法

Oct 09, 2024 am 06:19 AM

今日のデジタル世界では、MVP を構築する場合でも、スタートアップを立ち上げる場合でも、厳しい納期でプロジェクトを遂行する場合でも、アイデアを迅速にテストし、ユーザーと対話できることが非常に重要です。ニュースレター購読フォームの作成は、多くの場合、コンセプトを検証し、初期ユーザーを関与させ、関心のある人々のコミュニティを構築し、フィードバックを収集するために必要です。

ターンキー ソリューションは高価になる可能性がありますが、無料ツールの使用は依然として複雑で時間がかかります。

このチュートリアルでは、ニュースレター購読フォームを 20 分以内に作成する方法を説明します。複雑な設定や面倒な設定は必要ありません。完全に機能するサブスクリプション システムを備えた単なるフォームです!

使用スタック

  • shadcn/UI: フロントエンドを構築するための、すぐに使用できる美しくデザインされたコンポーネント。
  • マニフェスト: YAML ファイルに記入するだけで、完全なバックエンドを構築する最速かつ簡単な方法です。

このチュートリアルが終わるまでに、最初の購読者を集めるための完全に機能するフォームが完成します。準備ができて?行きましょう!

マニフェストとは何ですか?

Manifest は、オープンソースの Backend-as-a-Service (BaaS) です。これにより、アプリケーションの完全なバックエンドを作成できます。

単一の YAML ファイルに入力するだけで、データベース、API、および技術者以外の管理者にとって使いやすい管理パネルを備えたバックエンドが生成されます。

これにより、バックエンドの複雑さに対処するのではなく、製品の構築に集中できます。

本日の時点で、MVP をリリースしたばかりです。製品を正しい方向に進化させるためにコミュニティからのフィードバックを期待しています。

マニフェストは GitHub で入手できるので、プロジェクトが気に入ったらお気軽に ⭐ を付けてください!

インターフェースの計画

私たちの目標は、サブスクリプションフィールドと通知メッセージを表示する単一の画面です。シンプルかつ効果的で機能的です。得られるものは次のとおりです:

  • 購読者向けのフロントエンド
  • 管理者用の管理パネル

shadcn/UI を使用したフロントエンドの作成

フロントエンド、つまりニュースレター購読フォームのビジュアル部分を使用してプロジェクトを作成することから始めます。

Next.js で shadcn/UI を使用することにしました。ターミナルで次のコマンドを実行します:

npx shadcn@latest init -d
ログイン後にコピー

新しい Next.js プロジェクトを開始し、プロジェクトに名前を付けるように求められます。 「Y」と答えて、ニュースレター形式と名付けます。

プロジェクトが作成されたら、次のファイルを使用してフロントエンドを準備する必要があります:

How to create a newsletter signup form in just minutes with shadcn/UI and Manifest

npm run dev を実行して開発サーバーを起動します。

ターミナルで提供されたリンクをクリックします。デフォルトの Web ブラウザで https://localhost:3000 にある NextJS のようこそ画面が開きます。

How to create a newsletter signup form in just minutes with shadcn/UI and Manifest

静的フォームの作成

app/page.tsx を編集してフォームを作成しましょう。 shadcn は TailwindCSS と連携して動作するため、そのクラスを使用して目的のインターフェイスを構築します。次のコードをコピーします:

export default function Home() {
  return (
    <div className="w-full lg:grid lg:grid-cols-5 min-h-screen flex sm:items-center sm:justify-center sm:grid">
      <div className="flex items-center justify-center py-12 col-span-2 px-8">
        <div className="mx-auto grid max-w-[540px] gap-6">
          <div className="grid gap-2 text-left">
            <h1 className="text-3xl font-bold">Subscribe to our Newsletter! ?</h1>
            <p className="text-balance text-muted-foreground">
              Get the latest news, updates, and special offers delivered straight to your inbox.
            </p>
          </div>
          <form className="grid gap-4">{/* Newsletter form here */}</form>
        </div>
      </div>
      <div className="hidden bg-muted lg:block col-span-3 min-h-screen bg-gradient-to-t from-green-50 via-pink-100 to-purple-100"></div>
    </div>
  );
}
ログイン後にコピー

左側にフォーム用の領域、右側にグラデーション スペースがある分割画面が表示されます。

How to create a newsletter signup form in just minutes with shadcn/UI and Manifest

次に、フォームを追加しましょう。これには、次の shadcn コンポーネントが含まれます:

  • 入力
  • ボタン

次のコマンドを使用して、ターミナルからこれらのコンポーネントをインストールします。

npx shadcn@latest add input
npx shadcn@latest add button
ログイン後にコピー

次に、次のように page.tsx ファイルにコンポーネントをインポートします。

import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
ログイン後にコピー

これら 2 つのコンポーネントを使用するには、既存の

内に次のスニペットを追加します。タグ:

<form className="grid gap-4">
  <div className="flex w-full max-w-sm items-center space-x-2">
    <Input type="email" placeholder="Email" name="email" />
    <Button type="submit">Subscribe</Button>
  </div>
  <p className="text-sm text-muted-foreground">
    Sent out weekly on Mondays. Always free.
  </p>
</form>
ログイン後にコピー

フロントエンドに応答性の高いニュースレター フォームが必要です。あなたの作品をじっくりと鑑賞してください。リラックスしてください。20 分間の約束はまだ残っています!

How to create a newsletter signup form in just minutes with shadcn/UI and Manifest

マニフェストを使用してバックエンドを作成する

バックエンドを追加して購読者を保存し、管理者が管理パネルを介して購読者を管理できるようにしましょう。

次のコマンドを使用して、プロジェクトのルートにマニフェストをインストールします:

npx add-manifest
ログイン後にコピー

バックエンドがインストールされると、リポジトリに次のファイルが表示されます:

How to create a newsletter signup form in just minutes with shadcn/UI and Manifest

Now let’s define our data model. Open the backend.yml file and replace the existing code with this one:

name: Newsletter Form

entities:
  Subscriber:
    properties:
      - { name: email, type: email }
ログイン後にコピー

Run the following command to start your server:

npm run manifest
ログイン後にコピー

Once your backend is running, the terminal will provide two links:

  • ?️ Admin panel : http://localhost:1111,
  • ? API Doc: http://localhost:1111/api.

Launch the admin panel on your browser and log in with the pre-filled credentials. You should now see an empty list of subscribers.

How to create a newsletter signup form in just minutes with shadcn/UI and Manifest

There we have it! In under 3 minutes, We’ve built a full backend for our newsletter subscription form. ?

Connecting Manifest with our frontend

We’ll use Manifest’s SDK to connect the form and add emails directly to the subscribers list from the frontend.

From your project’s root, install the SDK with:

npm i @mnfst/sdk
ログイン後にコピー

Let’s add the newsletter subscription functionality to turn the static form into a dynamic one that stores emails using Manifest.

Next.js treats the files in the app directory as Server Components by default. To use interactive features (like React hooks), we need to mark our component as a Client Component.

Add "use client"; at the top of your Home.tsx file:

'use client';
ログイン後にコピー

Next, create the handleSubmit function to capture the email and send it to Manifest:

export default function Home() {
  const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => {
    e.preventDefault();

    const form = e.currentTarget as HTMLFormElement;
    const emailInput = form.querySelector('input[name="email"]') as HTMLInputElement;
    const email = emailInput?.value;

    if (!email) {
      alert('Please enter a valid email.');
      return;
    }

    const manifest = new Manifest();
    manifest
      .from('subscribers')
      .create({ email })
      .then(() => {
        form.reset();
        alert('Successfully subscribed!');
      })
      .catch((error) => {
        console.error('Error adding subscriber:', error);
        alert(`Failed to add subscriber: ${error.message || error}`);
      });
  };

  return (
    // ... Your existing code here>
  );
}
ログイン後にコピー

Now, add the onSubmit={handleSubmit} attribute to your tag:

<form onSubmit={handleSubmit} className="grid gap-4">
ログイン後にコピー

Testing the form

Time to see our form in action! Enter an email address and hit submit. You should get a confirmation message.

Check the admin panel, and voilà! this email is now in the subscriber list!

How to create a newsletter signup form in just minutes with shadcn/UI and Manifest

Enhancing user experience

Let’s add alerts to indicate whether the subscription was successful or not. We’ll use the ShadUI alert component.

Install the alert component with:

npx shadcn@latest add alert
ログイン後にコピー

We can now add the alert function, and integrate it into our form. Here is the final page.tsx page:

'use client'

import { Alert, AlertDescription } from '@/components/ui/alert'
import { Button } from '@/components/ui/button'
import { Input } from '@/components/ui/input'
import Manifest from '@mnfst/sdk'
import { useState } from 'react'

export default function Home() {
  const [alertVisible, setAlertVisible] = useState(false) // State to manage alert visibility
  const [alertMessage, setAlertMessage] = useState('') // Message to display in the alert

  const handleSubmit = (e: React.FormEvent) => {
    e.preventDefault()

    // Retrieve email from the input field
    const form = e.currentTarget as HTMLFormElement
    const emailInput = form.querySelector(
      'input[name="email"]'
    ) as HTMLInputElement
    const email = emailInput?.value

    if (!email) {
      setAlertMessage('Please enter a valid email.')
      setAlertVisible(true)
      setTimeout(() => setAlertVisible(false), 3000) // Hide the alert after 3 seconds
      return
    }

    const manifest = new Manifest()
    manifest
      .from('subscribers')
      .create({ email })
      .then(() => {
        form.reset() // Reset the email input field after success
        setAlertMessage(
          'Successfully subscribed! We will contact you if you are selected.'
        )
        setAlertVisible(true) // Show success alert
        setTimeout(() => setAlertVisible(false), 3000) // Hide the alert after 3 seconds
      })
      .catch((error) => {
        setAlertMessage(`Failed to add subscriber: ${error.message || error}`)
        setAlertVisible(true) // Show error alert
        setTimeout(() => setAlertVisible(false), 3000) // Hide the alert after 3 seconds
      })
  }

  return (
    

Subscribe to our Newsletter! ?

Get the latest news, updates, and special offers delivered straight to your inbox.

<form onSubmit={handleSubmit} className="grid gap-4">

Sent out weekly on Mondays. Always free.

{/* Display the alert based on alertVisible state */} {alertVisible && ( {alertMessage} )}
) }
ログイン後にコピー

Let's try the form with the alert by entering a new valid email.

How to create a newsletter signup form in just minutes with shadcn/UI and Manifest

Congratulations! ? You’ve just built a fully functional newsletter subscription application in a flash! ⚡

Conclusion

By leveraging Manifest alongside your favorite frontend tools, you can rapidly create applications with minimal effort. Manifest has been instrumental in speeding up our development process, allowing us to set up a complete backend in just minutes.

I hope this guide was helpful and that you learned how to create a simple and effective newsletter subscription system for your future projects.

If you'd like to access the full project code, you can check out the repository here.

If you liked using Manifest, consider giving us a ⭐ on GitHub to support the project and stay updated!

以上がshadcn/UI とマニフェストを使用してわずか数分でニュースレターの登録フォームを作成する方法の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

Video Face Swap

Video Face Swap

完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

ホットツール

メモ帳++7.3.1

メモ帳++7.3.1

使いやすく無料のコードエディター

SublimeText3 中国語版

SublimeText3 中国語版

中国語版、とても使いやすい

ゼンドスタジオ 13.0.1

ゼンドスタジオ 13.0.1

強力な PHP 統合開発環境

ドリームウィーバー CS6

ドリームウィーバー CS6

ビジュアル Web 開発ツール

SublimeText3 Mac版

SublimeText3 Mac版

神レベルのコード編集ソフト(SublimeText3)

Python vs. JavaScript:学習曲線と使いやすさ Python vs. JavaScript:学習曲線と使いやすさ Apr 16, 2025 am 12:12 AM

Pythonは、スムーズな学習曲線と簡潔な構文を備えた初心者により適しています。 JavaScriptは、急な学習曲線と柔軟な構文を備えたフロントエンド開発に適しています。 1。Python構文は直感的で、データサイエンスやバックエンド開発に適しています。 2。JavaScriptは柔軟で、フロントエンドおよびサーバー側のプログラミングで広く使用されています。

C/CからJavaScriptへ:すべてがどのように機能するか C/CからJavaScriptへ:すべてがどのように機能するか Apr 14, 2025 am 12:05 AM

C/CからJavaScriptへのシフトには、動的なタイピング、ゴミ収集、非同期プログラミングへの適応が必要です。 1)C/Cは、手動メモリ管理を必要とする静的に型付けられた言語であり、JavaScriptは動的に型付けされ、ごみ収集が自動的に処理されます。 2)C/Cはマシンコードにコンパイルする必要がありますが、JavaScriptは解釈言語です。 3)JavaScriptは、閉鎖、プロトタイプチェーン、約束などの概念を導入します。これにより、柔軟性と非同期プログラミング機能が向上します。

JavaScriptとWeb:コア機能とユースケース JavaScriptとWeb:コア機能とユースケース Apr 18, 2025 am 12:19 AM

Web開発におけるJavaScriptの主な用途には、クライアントの相互作用、フォーム検証、非同期通信が含まれます。 1)DOM操作による動的なコンテンツの更新とユーザーインタラクション。 2)ユーザーエクスペリエンスを改善するためにデータを提出する前に、クライアントの検証が実行されます。 3)サーバーとのリフレッシュレス通信は、AJAXテクノロジーを通じて達成されます。

JavaScript in Action:実際の例とプロジェクト JavaScript in Action:実際の例とプロジェクト Apr 19, 2025 am 12:13 AM

現実世界でのJavaScriptのアプリケーションには、フロントエンドとバックエンドの開発が含まれます。 1)DOM操作とイベント処理を含むTODOリストアプリケーションを構築して、フロントエンドアプリケーションを表示します。 2)node.jsを介してRestfulapiを構築し、バックエンドアプリケーションをデモンストレーションします。

JavaScriptエンジンの理解:実装の詳細 JavaScriptエンジンの理解:実装の詳細 Apr 17, 2025 am 12:05 AM

JavaScriptエンジンが内部的にどのように機能するかを理解することは、開発者にとってより効率的なコードの作成とパフォーマンスのボトルネックと最適化戦略の理解に役立つためです。 1)エンジンのワークフローには、3つの段階が含まれます。解析、コンパイル、実行。 2)実行プロセス中、エンジンはインラインキャッシュや非表示クラスなどの動的最適化を実行します。 3)ベストプラクティスには、グローバル変数の避け、ループの最適化、constとletsの使用、閉鎖の過度の使用の回避が含まれます。

Python vs. JavaScript:コミュニティ、ライブラリ、リソース Python vs. JavaScript:コミュニティ、ライブラリ、リソース Apr 15, 2025 am 12:16 AM

PythonとJavaScriptには、コミュニティ、ライブラリ、リソースの観点から、独自の利点と短所があります。 1)Pythonコミュニティはフレンドリーで初心者に適していますが、フロントエンドの開発リソースはJavaScriptほど豊富ではありません。 2)Pythonはデータサイエンスおよび機械学習ライブラリで強力ですが、JavaScriptはフロントエンド開発ライブラリとフレームワークで優れています。 3)どちらも豊富な学習リソースを持っていますが、Pythonは公式文書から始めるのに適していますが、JavaScriptはMDNWebDocsにより優れています。選択は、プロジェクトのニーズと個人的な関心に基づいている必要があります。

Python vs. JavaScript:開発環境とツール Python vs. JavaScript:開発環境とツール Apr 26, 2025 am 12:09 AM

開発環境におけるPythonとJavaScriptの両方の選択が重要です。 1)Pythonの開発環境には、Pycharm、Jupyternotebook、Anacondaが含まれます。これらは、データサイエンスと迅速なプロトタイピングに適しています。 2)JavaScriptの開発環境には、フロントエンドおよびバックエンド開発に適したnode.js、vscode、およびwebpackが含まれます。プロジェクトのニーズに応じて適切なツールを選択すると、開発効率とプロジェクトの成功率が向上する可能性があります。

JavaScript通訳者とコンパイラにおけるC/Cの役割 JavaScript通訳者とコンパイラにおけるC/Cの役割 Apr 20, 2025 am 12:01 AM

CとCは、主に通訳者とJITコンパイラを実装するために使用されるJavaScriptエンジンで重要な役割を果たします。 1)cは、JavaScriptソースコードを解析し、抽象的な構文ツリーを生成するために使用されます。 2)Cは、Bytecodeの生成と実行を担当します。 3)Cは、JITコンパイラを実装し、実行時にホットスポットコードを最適化およびコンパイルし、JavaScriptの実行効率を大幅に改善します。

See all articles