تمت إضافة أدوات جديدة! اكتشف الجديد

الرئيسية/المكونات/نموذج اتصال

نموذج اتصال examples

نموذج اتصال بتسميات مرئية وتحقق أصلي ورسائل أخطاء مضمّنة وحالة إرسال سهلة الوصول.

نموذج اتصال بتسميات مرئية ورسائل تحقق مضمّنة وحالة إرسال سهلة الوصول. كل حقل مرتبط بتسميته ارتباطًا صحيحًا، والأخطاء موصولة عبر aria-describedby لتُعلن لا لتُعرض فحسب.

تتحقق النسخة الأولى عند الإرسال وتعرض أخطاء لكل حقل. أما الباقي فشيفرة ثابتة تعتمد على التحقق الأصلي في المتصفح.

Contact form with inline validation

Name, email, and message with labels connected by id, per-field error messages wired through aria-describedby, and a submit button that guards against double sends.

Preview
"use client";

import { useState } from "react";

type Errors = Partial<Record<"name" | "email" | "message", string>>;

export default function ContactForm() {
  const [errors, setErrors] = useState<Errors>({});
  const [status, setStatus] = useState<"idle" | "sending" | "sent">("idle");

  async function handleSubmit(event: React.FormEvent<HTMLFormElement>) {
    event.preventDefault();

    const data = new FormData(event.currentTarget);
    const next: Errors = {};

    if (!String(data.get("name") ?? "").trim()) {
      next.name = "Enter your name.";
    }
    if (!String(data.get("email") ?? "").includes("@")) {
      next.email = "Enter a valid email address.";
    }
    if (!String(data.get("message") ?? "").trim()) {
      next.message = "Enter a message.";
    }

    setErrors(next);
    if (Object.keys(next).length > 0) return;

    setStatus("sending");
    // await fetch("/api/contact", { method: "POST", body: data });
    setStatus("sent");
  }

  const field =
    "mt-1.5 block w-full rounded-md border px-3 py-2 text-sm text-gray-900 focus:outline-none focus:ring-1";
  const ok = "border-gray-300 focus:border-gray-900 focus:ring-gray-900";
  const bad = "border-red-500 focus:border-red-500 focus:ring-red-500";

  return (
    <form onSubmit={handleSubmit} className="mx-auto max-w-lg space-y-5" noValidate>
      <div>
        <label htmlFor="name" className="block text-sm font-medium text-gray-900">
          Name
        </label>
        <input
          id="name"
          name="name"
          type="text"
          autoComplete="name"
          aria-invalid={Boolean(errors.name)}
          aria-describedby={errors.name ? "name-error" : undefined}
          className={field + " " + (errors.name ? bad : ok)}
        />
        {errors.name && (
          <p id="name-error" className="mt-1.5 text-xs text-red-600">
            {errors.name}
          </p>
        )}
      </div>

      <div>
        <label htmlFor="email" className="block text-sm font-medium text-gray-900">
          Email
        </label>
        <input
          id="email"
          name="email"
          type="email"
          autoComplete="email"
          aria-invalid={Boolean(errors.email)}
          aria-describedby={errors.email ? "email-error" : "email-hint"}
          className={field + " " + (errors.email ? bad : ok)}
        />
        {errors.email ? (
          <p id="email-error" className="mt-1.5 text-xs text-red-600">
            {errors.email}
          </p>
        ) : (
          <p id="email-hint" className="mt-1.5 text-xs text-gray-500">
            We will only use this to reply.
          </p>
        )}
      </div>

      <div>
        <label htmlFor="message" className="block text-sm font-medium text-gray-900">
          Message
        </label>
        <textarea
          id="message"
          name="message"
          rows={5}
          aria-invalid={Boolean(errors.message)}
          aria-describedby={errors.message ? "message-error" : undefined}
          className={field + " " + (errors.message ? bad : ok)}
        />
        {errors.message && (
          <p id="message-error" className="mt-1.5 text-xs text-red-600">
            {errors.message}
          </p>
        )}
      </div>

      <button
        type="submit"
        disabled={status === "sending"}
        className="w-full rounded-md bg-gray-900 px-4 py-2.5 text-sm font-medium text-white hover:bg-gray-700 disabled:opacity-60 focus:outline-none focus-visible:ring-2 focus-visible:ring-gray-900 focus-visible:ring-offset-2"
      >
        {status === "sending" ? "Sending..." : "Send message"}
      </button>

      <p aria-live="polite" className="text-sm text-gray-600">
        {status === "sent" ? "Thanks - we will reply shortly." : ""}
      </p>
    </form>
  );
}

Contact form with two-column fields

Name and email side by side from the sm breakpoint, stacked below it. Only pair fields that are genuinely related.

Preview
export default function TwoColumnContactForm() {
  return (
    <form className="mx-auto max-w-lg space-y-5" noValidate>
      <div className="grid gap-5 sm:grid-cols-2">
        <div>
          <label htmlFor="name" className="block text-sm font-medium text-gray-900">Name</label>
          <input id="name" name="name" type="text" required autoComplete="name" className="mt-1.5 block w-full rounded-md border border-gray-300 px-3 py-2 text-sm text-gray-900 placeholder:text-gray-400 focus:border-gray-900 focus:outline-none focus:ring-1 focus:ring-gray-900" />
        </div>
        <div>
          <label htmlFor="email" className="block text-sm font-medium text-gray-900">Email</label>
          <input id="email" name="email" type="email" required autoComplete="email" className="mt-1.5 block w-full rounded-md border border-gray-300 px-3 py-2 text-sm text-gray-900 placeholder:text-gray-400 focus:border-gray-900 focus:outline-none focus:ring-1 focus:ring-gray-900" />
        </div>
      </div>

      <div>
        <label htmlFor="message" className="block text-sm font-medium text-gray-900">Message</label>
        <textarea id="message" name="message" rows="5" required className="mt-1.5 block w-full rounded-md border border-gray-300 px-3 py-2 text-sm text-gray-900 placeholder:text-gray-400 focus:border-gray-900 focus:outline-none focus:ring-1 focus:ring-gray-900"></textarea>
      </div>

      <button type="submit" className="w-full rounded-md bg-gray-900 px-4 py-2.5 text-sm font-medium text-white hover:bg-gray-700 focus:outline-none focus-visible:ring-2 focus-visible:ring-gray-900 focus-visible:ring-offset-2">Send message</button>
    </form>
  );
}

Contact form with a subject select

A subject dropdown for routing enquiries to the right inbox. Keep the options short and concrete rather than naming internal teams.

Preview
export default function ContactFormWithSubject() {
  return (
    <form className="mx-auto max-w-lg space-y-5" noValidate>
      <div>
        <label htmlFor="name" className="block text-sm font-medium text-gray-900">Name</label>
        <input id="name" name="name" type="text" required autoComplete="name" className="mt-1.5 block w-full rounded-md border border-gray-300 px-3 py-2 text-sm text-gray-900 placeholder:text-gray-400 focus:border-gray-900 focus:outline-none focus:ring-1 focus:ring-gray-900" />
      </div>
      <div>
        <label htmlFor="email" className="block text-sm font-medium text-gray-900">Email</label>
        <input id="email" name="email" type="email" required autoComplete="email" className="mt-1.5 block w-full rounded-md border border-gray-300 px-3 py-2 text-sm text-gray-900 placeholder:text-gray-400 focus:border-gray-900 focus:outline-none focus:ring-1 focus:ring-gray-900" />
      </div>

      <div>
        <label htmlFor="subject" className="block text-sm font-medium text-gray-900">Subject</label>
        <select id="subject" name="subject" className="mt-1.5 block w-full rounded-md border border-gray-300 px-3 py-2 text-sm text-gray-900 placeholder:text-gray-400 focus:border-gray-900 focus:outline-none focus:ring-1 focus:ring-gray-900 bg-white">
          <option>General question</option>
          <option>Sales and pricing</option>
          <option>Technical support</option>
          <option>Press</option>
        </select>
      </div>

      <div>
        <label htmlFor="message" className="block text-sm font-medium text-gray-900">Message</label>
        <textarea id="message" name="message" rows="5" required className="mt-1.5 block w-full rounded-md border border-gray-300 px-3 py-2 text-sm text-gray-900 placeholder:text-gray-400 focus:border-gray-900 focus:outline-none focus:ring-1 focus:ring-gray-900"></textarea>
      </div>

      <button type="submit" className="w-full rounded-md bg-gray-900 px-4 py-2.5 text-sm font-medium text-white hover:bg-gray-700 focus:outline-none focus-visible:ring-2 focus-visible:ring-gray-900 focus-visible:ring-offset-2">Send message</button>
    </form>
  );
}

Inline newsletter signup form

A single email field with the button on the same row, for footers and banners where a full form is too heavy.

Preview
export default function NewsletterForm() {
  return (
    <form className="mx-auto max-w-md" noValidate>
      <label htmlFor="newsletter-email" className="block text-sm font-medium text-gray-900">Get product updates</label>
      <div className="mt-1.5 flex gap-2">
        <input
          id="newsletter-email"
          name="email"
          type="email"
          required
          autoComplete="email"
          placeholder="you@example.com"
          className="block w-full rounded-md border border-gray-300 px-3 py-2 text-sm text-gray-900 placeholder:text-gray-400 focus:border-gray-900 focus:outline-none focus:ring-1 focus:ring-gray-900"
        />
        <button type="submit" className="shrink-0 rounded-md bg-gray-900 px-4 py-2 text-sm font-medium text-white hover:bg-gray-700 focus:outline-none focus-visible:ring-2 focus-visible:ring-gray-900 focus-visible:ring-offset-2">Subscribe</button>
      </div>
      <p className="mt-2 text-xs text-gray-500">One email a month. Unsubscribe any time.</p>
    </form>
  );
}

Contact section with details beside the form

Contact details on the left and the form on the right at md, stacked on mobile. A complete contact-page section.

Preview
export default function ContactSection() {
  return (
    <section className="mx-auto grid max-w-5xl gap-10 px-4 py-12 md:grid-cols-2">
      <div className="bg-gray-900 p-4 rounded-lg">
        <h2 className="text-2xl font-bold text-gray-100">Get in touch</h2>
        <p className="mt-3 text-sm text-white">Tell us what you are working on and we will reply within one business day.</p>

        <dl className="mt-8 space-y-4 text-sm">
          <div>
            <dt className="font-medium text-gray-100">Email</dt>
            <dd className="text-white">hello@example.com</dd>
          </div>
          <div>
            <dt className="font-medium text-gray-100">Office hours</dt>
            <dd className="text-white">Monday to Friday, 9am to 5pm</dd>
          </div>
        </dl>
      </div>

      <form className="space-y-5" noValidate>
        <div>
          <label htmlFor="name" className="block text-sm font-medium text-gray-900">Name</label>
          <input id="name" name="name" type="text" required autoComplete="name" className="mt-1.5 block w-full rounded-md border border-gray-300 px-3 py-2 text-sm text-gray-900 placeholder:text-gray-400 focus:border-gray-900 focus:outline-none focus:ring-1 focus:ring-gray-900" />
        </div>
        <div>
          <label htmlFor="email" className="block text-sm font-medium text-gray-900">Email</label>
          <input id="email" name="email" type="email" required autoComplete="email" className="mt-1.5 block w-full rounded-md border border-gray-300 px-3 py-2 text-sm text-gray-900 placeholder:text-gray-400 focus:border-gray-900 focus:outline-none focus:ring-1 focus:ring-gray-900" />
        </div>
        <div>
          <label htmlFor="message" className="block text-sm font-medium text-gray-900">Message</label>
          <textarea id="message" name="message" rows="5" required className="mt-1.5 block w-full rounded-md border border-gray-300 px-3 py-2 text-sm text-gray-900 placeholder:text-gray-400 focus:border-gray-900 focus:outline-none focus:ring-1 focus:ring-gray-900"></textarea>
        </div>

        <button type="submit" className="w-full rounded-md bg-gray-900 px-4 py-2.5 text-sm font-medium text-white hover:bg-gray-700 focus:outline-none focus-visible:ring-2 focus-visible:ring-gray-900 focus-visible:ring-offset-2">Send message</button>
      </form>
    </section>
  );
}

What is a نموذج اتصال?

نموذج الاتصال هو الطريقة الأكثر شيوعًا التي يصل بها الزائر إلى صاحب الموقع، وهو من الأماكن القليلة التي يكلّف فيها خطأ تنفيذ صغير رسالة فعلًا. فكل حقل مطلوب إضافي، وكل خطأ غير واضح، وكل حقل لا يتعرف عليه الملء التلقائي يضيف احتكاكًا في اللحظة نفسها التي قرر فيها شخص التواصل.

الأساسيات غير لامعة وكثيرًا ما تُتجاهل. كل حقل يحتاج إلى عنصر label حقيقي مرتبط بـ id، لا نص بديل placeholder يقوم مقامه - فالنصوص البديلة تختفي حين تبدأ الكتابة، فتترك من يفقدون مكانهم بلا طريقة لاستعادة معنى الحقل، ولا تعلنها قارئات الشاشة بشكل موثوق. وسمات الإكمال التلقائي مهمة لسبب عملي مماثل: فمعها يملأ المتصفح الاسم والبريد بنقرة واحدة، ودونها يكتب الشخص كليهما يدويًا على لوحة مفاتيح هاتف.

التحقق هو المكان الذي تصبح فيه معظم النماذج مزعجة. فالتحقق عند كل ضغطة مفتاح يعني إخبار شخص بأن بريده غير صالح وهو لا يزال عند الحرف الثالث. والتحقق عند الإرسال فقط ثم تمييز الحقول بالأحمر دون أي شرح يتركه يخمّن. والحل الوسط الذي ينجح هو التحقق عند الإرسال برسالة محددة لكل حقل، ثم إعادة التحقق من ذلك الحقل حين يُصحَّح ليزول الخطأ عند إصلاحه.

حالة الإرسال أهم مما تبدو. فالزر الذي لا يعطي تغذية راجعة يدعو إلى نقرة ثانية، والنقرة الثانية على نموذج غير محمي ترسل رسالة مكررة. وتعطيل الزر أثناء الإرسال وتغيير تسميته يعالجان ذلك، وتحمل منطقة حية مهذبة التأكيد النهائي إلى من لا يستطيعون رؤية الرسالة وهي تظهر.

Anatomy

مجموعة الحقل
تسمية وعنصر تحكمها وتلميحها أو رسالة خطئها، مغلّفة معًا لتبقى المسافات بين الثلاثة متسقة عبر الحقول.
التسمية
عنصر label حقيقي بسمة for تطابق id الحقل. وهذا ما يجعل التسمية قابلة للنقر وما يمنح الحقل اسمًا متاحًا.
عنصر التحكم
الحقل input أو textarea، يحمل name لبيانات النموذج وtype للوحة مفاتيح الجوال الصحيحة ورمز autocomplete لملء المتصفح.
التلميح
نص داعم اختياري موصول بـ aria-describedby، يُستخدم لكل ما كان سيوضع في النص البديل.
رسالة الخطأ
تحل محل التلميح حين يفشل الحقل. وهي موصولة أيضًا بـ aria-describedby ومقترنة بـ aria-invalid على عنصر التحكم.
زر الإرسال
زر submit حقيقي ليرسل Enter النموذج. معطّل أثناء الإرسال، بتسمية تعكس الحالة الراهنة.
منطقة الحالة
منطقة حية مهذبة تحمل رسالة النجاح، ليُعلن التأكيد لا أن يظهر على الشاشة فحسب.

How to Use

  1. 1

    انسخ النموذج

    خذ نسخة HTML للاعتماد على التحقق الأصلي في المتصفح، أو نسخة React لرسائل مخصصة وحالة إرسال متحكَّم بها.

  2. 2

    اربط نقطة النهاية

    استبدل استدعاء fetch المعلَّق بمسار API أو خدمة النماذج أو إجراء الخادم لديك. بيانات النموذج مجمّعة أصلًا ككائن FormData.

  3. 3

    عدّل الحقول

    أضف مجموعات حقول أو احذفها حسب الحاجة. كل واحدة تحتاج إلى زوج label for وinput id متطابقين - فذلك الارتباط هو ما يجعل الحقل قابلًا للاستخدام.

  4. 4

    اضبط رموز الإكمال التلقائي

    استخدم القيم القياسية: name وemail وtel وorganization. فهي ما يتيح للمتصفح ملء النموذج بنقرة واحدة على الهاتف.

  5. 5

    عالج الاستجابة

    اضبط الحالة على sent عند النجاح وأظهر رسالة محددة عند الفشل. فالإرسال الفاشل الذي يبدو مطابقًا لإرسال ناجح أسوأ من الخطأ.

Features

تسميات حقيقية على كل حقل

كل تسمية مرتبطة بـ id، فتصبح قابلة للنقر وتمنح الحقل اسمًا متاحًا - لا نصًا بديلًا يؤدي المهمة بشكل سيئ.

أخطاء موصولة بحقولها

الرسائل موصولة عبر aria-describedby مع aria-invalid على عنصر التحكم، فيُعلن الخطأ حين يُركَّز الحقل لا أن يكون مرئيًا فقط.

دعم الإكمال التلقائي

تتيح الرموز القياسية على كل حقل للمتصفحات ومديري كلمات المرور ملء النموذج بإجراء واحد، وهذا هو الفرق بين نقرة وفقرة من الكتابة بالإبهام.

إرسال محمي

يتعطل الزر أثناء الإرسال وتتغير تسميته، مما يمنع النقرة المزدوجة التي ترسل رسائل مكررة.

تأكيد معلن

تقع رسالة النجاح في منطقة حية مهذبة، فتصل إلى مستخدمي قارئات الشاشة بدل أن تظهر بصمت.

لوحات مفاتيح جوال صحيحة

تُضبط أنواع الإدخال لكل حقل، فيُظهر حقل البريد لوحة مفاتيح فيها علامة @ بدل أن يبحث الناس عنها.

Common Uses

صفحات الاتصال بالموقع

نموذج الاسم والبريد والرسالة القياسي للاستفسارات العامة، حيث الهدف أقل عدد من الحقول يتيح لك الرد بشكل مفيد.

طلبات المبيعات والعروض التجريبية

يُوسَّع بحقل شركة وقائمة اختيار للموضوع، فيوجّه الاستفسارات دون أن يُترك الزائر يخمّن أي بريد يكتب إليه.

طلبات الدعم

مقترن بقائمة اختيار فئة ومرجع اختياري للطلب أو الحساب، ليكون الرد الأول مفيدًا لا طلبًا للتفاصيل.

الملاحظات وبلاغات الأخطاء

حقل رسالة مع بريد اختياري، حيث يزيد جعل البريد اختياريًا عدد البلاغات الواردة زيادة ملموسة.

Accessibility

Customization

Props

PropTypeDefaultDescription
onSubmit(data: FormData) => Promise<void>-تُستدعى ببيانات النموذج بعد اجتياز التحقق. أبقِ استدعاء الشبكة خارج المكوّن ليتمكن النموذج نفسه من الإرسال إلى نقاط نهاية مختلفة.
successMessagestring"شكرًا - سنرد عليك قريبًا."تُعلن عبر منطقة حية مهذبة بعد الإرسال الناجح، ليصل التأكيد إلى مستخدمي قارئات الشاشة كما يصل إلى المبصرين.

Why Use These نموذج اتصالs?

يصيب الأجزاء غير اللامعة. فالتسميات المرتبطة بـ id ورموز الإكمال التلقائي وaria-describedby على الأخطاء والإرسال المحمي هي الفرق بين نموذج يعمل للجميع ونموذج يعمل لمن بناه.

يعمل التحقق عند الإرسال ويزول لكل حقل، بدل الإشارة إلى أن بريدًا غير صالح وهو لا يزال قيد الكتابة - وهو السلوك الذي يجعل نماذج كثيرة تبدو عدائية.

لا مكتبة نماذج. فلثلاثة حقول، يمثل مدقق المخطط وحزمة حالة النموذج إعدادًا أكثر من النموذج نفسه، ولا يزالان يتركان ربط إمكانية الوصول عليك.

Tips

Frequently Asked Questions