Dropdown Component - Quick Usage Guide
Dynamic dropdown component for Astro MDX that switches content based on user selection. Perfect for language translations, code examples, and documentation.
📦 Import the Components
import Dropdown, { DropdownOption, DropdownRef } from "@/react/Dropdown/Dropdown.jsx";// or use backward compatible importimport Dropdown, { Option, DropdownRef } from "@/react/Dropdown/Dropdown.jsx";
or with relative path:
import Dropdown, { DropdownOption, DropdownRef } from "../../react/Dropdown/Dropdown.jsx";
Important: Use
.mdx
extension (not.md
) when working with React components.
🚀 Basic Usage
Simple Dropdown
<Dropdown defaultValue="english" placeholder="Choose your language..." client:load> <DropdownOption value="english" label="🇬🇧 English"> ## Welcome! Your content here... </DropdownOption> <DropdownOption value="spanish" label="🇪🇸 Español"> ## ¡Bienvenido! Tu contenido aquí... </DropdownOption></Dropdown>
Key Points:
- Always include
client:load
for interactivity - Each option needs unique
value
andlabel
- Full Markdown support inside options
⚙️ Component Props
Dropdown Props
Prop | Type | Default | Description |
---|---|---|---|
defaultValue | string | First option | Which option to show initially |
placeholder | string | ”Select an option…” | Text shown before selection |
stripCss | boolean | false | Remove background styling |
client:load | directive | - | Required for Astro |
DropdownOption Props
Prop | Type | Required | Description |
---|---|---|---|
value | string | Yes | Unique identifier |
label | string | No | Display text (auto-generated if omitted) |
children | content | Yes | Content to display |
🎯 Features & Examples
1. Default Value
Set which option shows first:
<Dropdown defaultValue="python" client:load> <DropdownOption value="python" label="Python"> Content... </DropdownOption></Dropdown>
2. Custom Placeholder
<Dropdown placeholder="Pick your favorite framework..." client:load> <DropdownOption value="react" label="⚛️ React"> Content... </DropdownOption></Dropdown>
3. Auto-Generated Labels
When you omit the label
prop, options get automatic names:
<Dropdown client:load> <DropdownOption value="opt1"> Content here... (becomes "Option 1") </DropdownOption> <DropdownOption value="opt2"> Content here... (becomes "Option 2") </DropdownOption></Dropdown>
When you omit both label
& value
props, options get automatic names:
<Dropdown client:load> <DropdownOption> Content here... (becomes "Option 1") </DropdownOption> <DropdownOption> Content here... (becomes "Option 2") </DropdownOption></Dropdown>
Note: Only use auto-generated labels for prototyping. Always provide meaningful labels in production.
4. Strip CSS for Clean Look
Use stripCss
to remove background styling - great for nesting components:
<Dropdown stripCss client:load> <DropdownOption value="example" label="Example"> Content without background box styling </DropdownOption></Dropdown>
🔗 Reference Dropdown (DropdownRef)
For lengthy content, keep it organized by using references:
Basic Reference Example
How to Use References
Step 1: Define dropdown with reference pattern :::drop <id>
<Dropdown defaultValue=":::drop javascript-ref" client:load> <DropdownOption value=":::drop javascript-ref" label="JavaScript" /> <DropdownOption value=":::drop python-ref" label="Python" /></Dropdown>
Step 2: Define content separately with <DropdownRef>
<DropdownRef type="drop" id="javascript-ref"> ## Your lengthy content here Multiple sections, code blocks, etc.</DropdownRef>
<DropdownRef type="drop" id="python-ref"> ## More lengthy content More sections, code blocks, etc.</DropdownRef>
Benefits:
- ✅ Better organization for long content
- ✅ Easier to maintain and edit
- ✅ Cleaner dropdown definitions
- ✅ Perfect for extensive documentation
⚠️ Important Limitations
React Components Inside Dropdown
React components (like Tabs) may not work reliably inside dropdowns due to how content is rendered.
What Works:
- ✅ Markdown (text, lists, tables)
- ✅ Code blocks with syntax highlighting
- ✅ Images, links, quotes
- ✅ Static HTML content
What May Not Work:
- ⚠️ React Tabs component
- ⚠️ Custom React components with state
- ⚠️ Interactive elements requiring event handlers
- ⚠️ Any component using
client:load
inside dropdown
Recommendation: Use dropdowns primarily for static Markdown content and code examples. For complex interactive content, consider alternative layouts.
💡 Best Practices
1. When to Use Basic Dropdown
// ✅ Good for short content (< 30 lines)<Dropdown client:load> <DropdownOption value="opt1" label="Quick Example"> Short content here... </DropdownOption></Dropdown>
2. When to Use Reference Dropdown
// ✅ Good for long content (> 30 lines)<Dropdown client:load> <DropdownOption value=":::drop long-content" label="Documentation" /></Dropdown>
<DropdownRef type="drop" id="long-content"> Extensive documentation here... Multiple sections... Many code blocks...</DropdownRef>
3. Provide Meaningful Labels
// ❌ Bad<DropdownOption value="a" label="A">
// ✅ Good<DropdownOption value="javascript" label="🟨 JavaScript">
4. Use stripCss for Nested Content
// ✅ Good for clean integration<Dropdown stripCss client:load> <DropdownOption value="opt1" label="Clean Look"> Content blends with page... </DropdownOption></Dropdown>
📝 Quick Reference
Basic Dropdown Template
<Dropdown defaultValue="option1" placeholder="Select..." client:load> <DropdownOption value="option1" label="Option 1"> Content for option 1 </DropdownOption> <DropdownOption value="option2" label="Option 2"> Content for option 2 </DropdownOption></Dropdown>
Reference Dropdown Template
<Dropdown defaultValue=":::drop ref1" placeholder="Select..." client:load> <DropdownOption value=":::drop ref1" label="First" /> <DropdownOption value=":::drop ref2" label="Second" /></Dropdown>
<DropdownRef type="drop" id="ref1"> Content for first option</DropdownRef>
<DropdownRef type="drop" id="ref2"> Content for second option</DropdownRef>
🎯 Summary
- ✅ Perfect for language translations and code examples
- ✅ Full Markdown and syntax highlighting support
- ✅ Auto theme switching (light/dark)
- ✅ Reference system for organizing long content
- ✅
stripCss
prop for clean integration - ⚠️ Use for static content only (React components may not work)
Start using dropdowns to create dynamic, multilingual content in your Astro blog! 🚀