Drag & Drop List Example Fix
Problem
The Drag & Drop List example had two issues:
drag-drop-list.mjs when dragging itemsRoot Cause
The example was passing a simple string array instead of an array of objects:
// โ WRONG - String array
'items': '["Item 1", "Item 2", "Item 3"]'
But the component expects items to be objects with an id property:
// From component documentation (line 4):
// - items: JSON array of items with { id, content } structure
Why It Failed
Looking at the component code:
Line 154-156 - Tries to manipulate items array:const movedItem = this.items[oldIndex]; // Works for strings
this.items.splice(oldIndex, 1); // Works for strings
this.items.splice(newIndex, 0, movedItem); // Works for strings
Line 158-165 - Publishes PAN message:
this.pc.publish({
topic: `${this.topic}.reorder`,
data: {
items: this.items, // Array of strings instead of objects
from: oldIndex,
to: newIndex
}
});
Line 262 - Rendering tries to access .id:
<div class="list-item" data-id="${item.id}" data-index="${index}">
^^^^^^^^ undefined for strings!
Line 266 - Tries to use id for slot name:
<slot name="item-${item.id}">
^^^^^^^^ undefined for strings!
Line 267 - Falls back to content property:
${item.content || item.label || item.title || JSON.stringify(item)}
^^^^^^^^^^^^ undefined for strings, eventually calls JSON.stringify("Item 1") = '"Item 1"'
The Error
When items are strings:
item.id is undefineddata-id="undefined" in HTMLslot name="item-undefined"Solution
Changed the example to use properly formatted item objects with id and content:
{
id: 'drag-drop',
name: 'Drag & Drop List',
description: 'Reorderable drag and drop list',
components: [
{
name: 'drag-drop-list',
attributes: {
'topic': 'list',
'items': JSON.stringify([
{ id: 1, content: '๐ Task 1: Review code' },
{ id: 2, content: '๐จ Task 2: Update designs' },
{ id: 3, content: '๐ Task 3: Deploy to production' },
{ id: 4, content: '๐ Task 4: Analyze metrics' }
])
}
}
]
}
Enhancements Made
id and contentlist.reordered to list (more standard)Item Object Format
The component supports flexible item formats:
Required
id(number or string) - Unique identifier for the item
Content (one of these):
content- Main display content (preferred)label- Alternative to contenttitle- Alternative to content- Any other properties - Will be JSON.stringify'd
Example Formats
Basic:{ id: 1, content: 'Task 1' }
With emoji:
{ id: 'task-1', content: '๐ Review code' }
With label:
{ id: 'item-1', label: 'First Item' }
With title:
{ id: 1, title: 'Important Task' }
Complex object (auto-stringified):
{ id: 1, name: 'Task', priority: 'high', completed: false }
// Displays: {"name":"Task","priority":"high","completed":false}
Testing
Before Fix
1. Load "Drag & Drop List" example
2. See: Three items ("Item 1", "Item 2", "Item 3")
3. Drag items to reorder
4. Open PAN Monitor
5. See: No messages appear โ
6. Check console
7. See: Error at line 155 or undefined attribute values โ
After Fix
1. Load "Drag & Drop List" example
2. See: Four tasks with emojis โ
3. Open PAN Monitor
4. Drag "Task 3" above "Task 1"
5. See: PAN message appears โ
Topic: list.reorder
Data: {
items: [{id: 1, content: '๐ Task 1: Review code'}, ...],
from: 2,
to: 0
}
6. Check console
7. See: No errors โ
PAN Messages
Reorder Event
When you drag and drop an item:Topic: {topic}.reorder
Data: {
items: [
{ id: 1, content: '๐ Task 1: Review code' },
{ id: 2, content: '๐จ Task 2: Update designs' },
{ id: 3, content: '๐ Task 3: Deploy to production' },
{ id: 4, content: '๐ Task 4: Analyze metrics' }
],
from: 2, // Original index
to: 0 // New index
}
Drop Event
Published after drag completes:Topic: {topic}.drop
Data: {
item: { id: 3, content: '๐ Task 3: Deploy to production' },
index: 0
}
PAN Subscriptions
The component also listens to control topics:
Set Items
Replace entire list:pc.publish({
topic: 'list.setItems',
data: {
items: [
{ id: 1, content: 'New item 1' },
{ id: 2, content: 'New item 2' }
]
}
});
Add Item
Insert item at position:pc.publish({
topic: 'list.addItem',
data: {
item: { id: 5, content: 'New task' },
index: 2 // Optional, defaults to end
}
});
Remove Item
Remove by ID:pc.publish({
topic: 'list.removeItem',
data: {
id: 3 // Remove item with id: 3
}
});
Component Features
Drag Handle
Theโฎโฎ icon indicates items are draggable. You can specify a custom handle:
attributes: {
'handle': '.drag-handle',
'items': JSON.stringify([
{ id: 1, content: '<span class="drag-handle">โก</span> Item 1' }
])
}
Disabled State
Disable dragging:attributes: {
'disabled': 'true',
'items': '...'
}
Empty State
Shows when no items:<div class="empty-state">
<slot name="empty">No items to display</slot>
</div>
Custom Slots
Use slots for custom content:// In your HTML:
<drag-drop-list items='[{"id":1,"content":"Item 1"}]'>
<div slot="item-1">
<strong>Custom content for item 1</strong>
</div>
</drag-drop-list>
Example Variations
Task List with Priorities
'items': JSON.stringify([
{ id: 1, content: '๐ด High: Fix critical bug' },
{ id: 2, content: '๐ก Medium: Update docs' },
{ id: 3, content: '๐ข Low: Refactor code' }
])
Shopping List
'items': JSON.stringify([
{ id: 1, content: '๐ฅ Bread' },
{ id: 2, content: '๐ฅ Milk' },
{ id: 3, content: '๐ Apples' },
{ id: 4, content: '๐ง Cheese' }
])
File Manager
'items': JSON.stringify([
{ id: 1, content: '๐ Documents' },
{ id: 2, content: '๐ Pictures' },
{ id: 3, content: '๐ Downloads' },
{ id: 4, content: '๐ README.md' }
])
Playlist
'items': JSON.stringify([
{ id: 1, content: '๐ต Song 1 - Artist A' },
{ id: 2, content: '๐ต Song 2 - Artist B' },
{ id: 3, content: '๐ต Song 3 - Artist C' }
])
Files Changed
/Users/cdr/Projects/larc-repos/playground/examples.mjs
Lines 382-400: Changed from string array to object array with proper structure
// Before:
'items': '["Item 1", "Item 2", "Item 3"]'
// After:
'items': JSON.stringify([
{ id: 1, content: '๐ Task 1: Review code' },
{ id: 2, content: '๐จ Task 2: Update designs' },
{ id: 3, content: '๐ Task 3: Deploy to production' },
{ id: 4, content: '๐ Task 4: Analyze metrics' }
])
Also changed topic from list.reordered to list (more standard).
Pattern Recognition
This is now the third example with the same issue:
{label, value}, got strings{id, content}, got stringsCommon Pattern
Components that accept JSON array attributes usually expect objects with specific properties, not primitive values (strings/numbers).Rule of Thumb
When a component has anitems attribute:
{id, content})Prevention
For new examples with array attributes:
- โ Check component documentation first
- โ Use objects with required properties
- โ Include IDs when needed
- โ Test with PAN Monitor open
- โ Don't use simple string/number arrays
Summary
The drag & drop list was fixed by changing the items from a simple string array to an array of properly formatted objects with id and content properties. Now:
- โ Items display correctly with emojis
- โ Drag and drop works smoothly
- โ PAN messages appear in monitor
- โ No console errors
- โ Reorder data includes full item objects