Design Systems & Component Libraries · درس

بناء مكونات متعددة الأشكال

أتقن نمط 'as' المتقدم متعدد الأشكال، الذي يتيح لمكوّن واحد العرض كعناصر مختلفة مع الحفاظ على تنسيقه وسلوكه.

الدرس 4 من 413 خطوة

بناء مكونات متعددة الأشكال درس مجاني في Design Systems & Component Libraries على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Design Systems & Component Libraries، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Design Systems & Component Libraries 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

One Component, Many Elements

Sometimes a button should render as a real <button>, sometimes as an <a> link, sometimes as a router link - but always look identical.

Polymorphic components solve this elegantly. This lesson covers the advanced as pattern.

The Problem With Duplicates

Without polymorphism you end up with Button, LinkButton, ButtonLink - near-identical copies that drift apart over time.

Each must be styled, tested, and maintained separately. Polymorphism collapses them into one source of truth.

The 'as' Prop

A polymorphic component accepts an as prop naming the element or component to render. The styling stays the same; only the tag changes.

The snippet shows the core idea in plain JS.

function Box(as, content) {
  const tag = as || 'div';
  return '<' + tag + ' class="box">' + content + '</' + tag + '>';
}

console.log(Box('button', 'Click me'));
console.log(Box('a', 'A link'));
console.log(Box(null, 'Default div'));

Forwarding Props Correctly

When rendering as an <a>, the component must accept href; as a <button>, it accepts type.

Polymorphic components spread the remaining props onto the underlying element so the right attributes flow through automatically.

Preserving Accessibility

Rendering a button as a <div> loses keyboard and screen-reader behavior. Polymorphism is powerful but can be misused.

Guide consumers toward semantic elements, and add role and tabindex fallbacks when a non-semantic element is unavoidable.

Typing the 'as' Prop

In typed codebases, polymorphic components are notoriously tricky. The props must change based on the as value - an href only makes sense for an anchor.

Advanced generic typing infers the correct prop set per element, giving consumers accurate autocomplete and errors.

Integrating Routers

The real payoff: pass a router Link as as and your styled Button becomes a client-side navigation link with zero new code.

This lets design system components work seamlessly with any app's routing library.

A Polymorphic Base Layer

Many systems build a single low-level Box primitive that is polymorphic, then compose higher components on top of it.

This concentrates the tricky polymorphic logic in one place, keeping every other component simpler.

When Not to Use It

Polymorphism adds complexity. If a component only ever renders one element, do not add an as prop speculatively.

Reserve it for genuinely interchangeable cases like buttons/links. Otherwise you pay typing and cognitive cost for nothing.

Documenting the Pattern

Polymorphic components surprise people. Document which components support as, the allowed values, and accessibility caveats.

Clear docs prevent misuse like rendering interactive controls as non-interactive elements.

Power With Responsibility

The polymorphic pattern eliminates duplicate components and integrates cleanly with any host app - but it demands careful prop forwarding, typing, and accessibility.

Used judiciously, it is one of the most elegant tools in advanced component development.

Quick Check

Test your grasp of polymorphic components.

Recap

You learned the advanced polymorphic component pattern:

  • The as prop swaps the rendered element while keeping styling.
  • Forward props correctly and preserve accessibility semantics.
  • Type the prop set per element and integrate with routers.
  • Use it only for genuinely interchangeable cases and document it.

Polymorphism removes duplication when applied with care.

البدء مجانًا

تعلم Design Systems & Component Libraries مع معلم ذكاء اصطناعي — مجانًا

اكتب وقم بتشغيل أكوادك الفعلية في المتصفح، واحصل على مساعدة فورية من معلم ذكاء اصطناعي متاح 24/7، واستمر من حيث توقفت على الويب أو في التطبيق.

الدورات
12
الدروس
48

الأسئلة الشائعة

هل درس «بناء مكونات متعددة الأشكال» مجاني؟

نعم — نص درس «بناء مكونات متعددة الأشكال» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Design Systems & Component Libraries، انتقل إلى CoddyKit PRO. تتضمن دورة Design Systems & Component Libraries 4 دروس في المجموع.

ماذا ستتعلم في «بناء مكونات متعددة الأشكال»؟

أتقن نمط 'as' المتقدم متعدد الأشكال، الذي يتيح لمكوّن واحد العرض كعناصر مختلفة مع الحفاظ على تنسيقه وسلوكه. تتمرن على Design Systems & Component Libraries مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Design Systems & Component Libraries؟

لا تُشترط خبرة سابقة. Design Systems & Component Libraries على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.

كم من الوقت يستغرق درس «بناء مكونات متعددة الأشكال»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Design Systems & Component Libraries هذا؟

نعم. كل درس في Design Systems & Component Libraries يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. تخصيص السمات والعلامة البيضاء
  2. التدويل (i18n)
  3. تحسين الأداء
  4. بناء مكونات متعددة الأشكال
← العودة إلى Design Systems & Component Libraries