|
| 1 | +# Touch Drag Support and Tablet Optimization |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +This document describes the touch drag support and tablet optimization features added to the Object UI Designer. |
| 6 | + |
| 7 | +## Problem Statement |
| 8 | + |
| 9 | +1. **Touch Device Support**: The designer's component palette did not support dragging on touch devices (tablets/mobile) because the HTML5 Drag and Drop API doesn't natively support touch events. |
| 10 | + |
| 11 | +2. **Tablet Layout**: The designer's fixed sidebar widths and spacing were not optimized for tablet screens (768px-1024px). |
| 12 | + |
| 13 | +## Solution |
| 14 | + |
| 15 | +### 1. Touch Drag Polyfill |
| 16 | + |
| 17 | +Created a comprehensive touch event polyfill (`touchDragPolyfill.ts`) that: |
| 18 | + |
| 19 | +- Converts touch events to drag events |
| 20 | +- Creates visual drag preview during touch interactions |
| 21 | +- Simulates the HTML5 Drag and Drop API for touch devices |
| 22 | +- Maintains compatibility with mouse-based interactions |
| 23 | + |
| 24 | +#### How It Works |
| 25 | + |
| 26 | +```typescript |
| 27 | +// Touch events flow |
| 28 | +touchstart → (100ms delay to distinguish from scroll) → dragstart |
| 29 | +touchmove → dragover on elements below touch point |
| 30 | +touchend → drop on target element |
| 31 | +touchcancel → cleanup |
| 32 | +``` |
| 33 | + |
| 34 | +#### Key Features |
| 35 | + |
| 36 | +- **Visual Feedback**: Creates a semi-transparent drag preview that follows the touch point |
| 37 | +- **Smart Detection**: 100ms delay to distinguish between scroll and drag intent |
| 38 | +- **Drop Target Detection**: Uses `document.elementFromPoint()` to find drop targets |
| 39 | +- **Event Simulation**: Dispatches proper drag events that existing Canvas handlers understand |
| 40 | +- **Cleanup**: Properly removes preview elements and event listeners |
| 41 | + |
| 42 | +### 2. Responsive Layout |
| 43 | + |
| 44 | +Updated the designer layout with Tailwind responsive classes: |
| 45 | + |
| 46 | +#### Sidebar Widths |
| 47 | +- **Mobile/Small Tablet**: `w-64` (256px) |
| 48 | +- **Desktop**: `md:w-72` (288px) for left, `md:w-80` (320px) for right |
| 49 | + |
| 50 | +#### Component Palette |
| 51 | +- **Grid**: Always 2 columns with responsive gaps (`gap-2 md:gap-2.5`) |
| 52 | +- **Item Heights**: `h-20` on mobile, `md:h-24` on desktop |
| 53 | +- **Spacing**: Reduced padding and margins on smaller screens |
| 54 | +- **Typography**: `text-[10px]` on mobile, `md:text-xs` on desktop |
| 55 | + |
| 56 | +#### Tab Labels |
| 57 | +- **Small Screens**: Icons only |
| 58 | +- **Desktop**: Icons + text using `hidden sm:inline` |
| 59 | + |
| 60 | +## Implementation Details |
| 61 | + |
| 62 | +### ComponentItem Component |
| 63 | + |
| 64 | +Each component in the palette is now a React component with: |
| 65 | + |
| 66 | +```tsx |
| 67 | +const ComponentItem: React.FC<ComponentItemProps> = ({ type, config, Icon, ... }) => { |
| 68 | + const itemRef = useRef<HTMLDivElement>(null); |
| 69 | + |
| 70 | + // Setup touch drag support |
| 71 | + useEffect(() => { |
| 72 | + if (!itemRef.current || !isTouchDevice()) return; |
| 73 | + |
| 74 | + const cleanup = enableTouchDrag(itemRef.current, { |
| 75 | + dragData: { componentType: type }, |
| 76 | + onDragStart: () => setDraggingType(type), |
| 77 | + onDragEnd: () => setDraggingType(null) |
| 78 | + }); |
| 79 | + |
| 80 | + return cleanup; |
| 81 | + }, [type]); |
| 82 | + |
| 83 | + return <div ref={itemRef} draggable ...>{/* component UI */}</div>; |
| 84 | +}; |
| 85 | +``` |
| 86 | + |
| 87 | +### Touch Detection |
| 88 | + |
| 89 | +```typescript |
| 90 | +export function isTouchDevice(): boolean { |
| 91 | + return ( |
| 92 | + 'ontouchstart' in window || |
| 93 | + navigator.maxTouchPoints > 0 || |
| 94 | + (navigator as any).msMaxTouchPoints > 0 |
| 95 | + ); |
| 96 | +} |
| 97 | +``` |
| 98 | + |
| 99 | +The polyfill only activates on touch-enabled devices, maintaining optimal performance on desktop. |
| 100 | + |
| 101 | +### Canvas Compatibility |
| 102 | + |
| 103 | +The existing Canvas component's drag handlers (`handleDragOver`, `handleDrop`) work seamlessly with the simulated drag events from the touch polyfill. No changes were needed to the Canvas component. |
| 104 | + |
| 105 | +## Usage |
| 106 | + |
| 107 | +### For Users |
| 108 | + |
| 109 | +**On Touch Devices (Tablet/Mobile):** |
| 110 | +1. Long press on a component in the palette (100ms) |
| 111 | +2. Drag your finger to the canvas |
| 112 | +3. Drop the component by lifting your finger |
| 113 | + |
| 114 | +**On Desktop:** |
| 115 | +- Standard drag and drop with mouse continues to work as before |
| 116 | + |
| 117 | +### For Developers |
| 118 | + |
| 119 | +To add touch drag support to any element: |
| 120 | + |
| 121 | +```typescript |
| 122 | +import { enableTouchDrag, isTouchDevice } from '../utils/touchDragPolyfill'; |
| 123 | + |
| 124 | +// In a component |
| 125 | +useEffect(() => { |
| 126 | + if (!elementRef.current || !isTouchDevice()) return; |
| 127 | + |
| 128 | + const cleanup = enableTouchDrag(elementRef.current, { |
| 129 | + dragData: { myData: 'value' }, |
| 130 | + onDragStart: (e, el) => console.log('Started dragging'), |
| 131 | + onDrag: (e, el) => console.log('Dragging...'), |
| 132 | + onDragEnd: (e, el) => console.log('Finished dragging') |
| 133 | + }); |
| 134 | + |
| 135 | + return cleanup; |
| 136 | +}, []); |
| 137 | +``` |
| 138 | + |
| 139 | +## Testing |
| 140 | + |
| 141 | +### Manual Testing |
| 142 | + |
| 143 | +To test on a real device: |
| 144 | +1. Open the designer in Chrome DevTools device mode |
| 145 | +2. Enable touch simulation |
| 146 | +3. Try dragging components from the palette to the canvas |
| 147 | +4. Verify the visual drag preview appears |
| 148 | +5. Verify components are added to the canvas on drop |
| 149 | + |
| 150 | +### Automated Testing |
| 151 | + |
| 152 | +Run the test suite: |
| 153 | +```bash |
| 154 | +pnpm --filter @object-ui/designer test |
| 155 | +``` |
| 156 | + |
| 157 | +The test file `touchDragPolyfill.test.ts` includes: |
| 158 | +- Touch device detection tests |
| 159 | +- Event listener setup/cleanup tests |
| 160 | +- Callback invocation tests |
| 161 | +- Edge case handling |
| 162 | + |
| 163 | +## Browser Compatibility |
| 164 | + |
| 165 | +The touch drag polyfill works on: |
| 166 | +- ✅ iOS Safari 12+ |
| 167 | +- ✅ Chrome Android 80+ |
| 168 | +- ✅ Firefox Mobile 68+ |
| 169 | +- ✅ Edge Mobile |
| 170 | +- ✅ Chrome Desktop (with touch screen) |
| 171 | +- ✅ All browsers with mouse (unchanged behavior) |
| 172 | + |
| 173 | +## Performance Considerations |
| 174 | + |
| 175 | +1. **Conditional Activation**: Polyfill only activates on touch devices via `isTouchDevice()` |
| 176 | +2. **Event Delegation**: Uses passive: false only where needed to prevent scrolling during drag |
| 177 | +3. **Memory Management**: Properly cleans up preview elements and event listeners |
| 178 | +4. **Efficient Updates**: Uses `useEffect` cleanup to remove listeners on unmount |
| 179 | + |
| 180 | +## Responsive Breakpoints |
| 181 | + |
| 182 | +The designer uses Tailwind's default breakpoints: |
| 183 | + |
| 184 | +- `sm`: 640px - Show tab labels |
| 185 | +- `md`: 768px - Increase sidebar widths, larger component items |
| 186 | +- Default: < 640px - Compact layout |
| 187 | + |
| 188 | +## Future Enhancements |
| 189 | + |
| 190 | +Potential improvements for future releases: |
| 191 | + |
| 192 | +- [ ] Add sidebar collapse/expand toggle for very small tablets |
| 193 | +- [ ] Add pinch-to-zoom support for canvas |
| 194 | +- [ ] Improve touch selection with long-press |
| 195 | +- [ ] Add touch-optimized context menu |
| 196 | +- [ ] Support multi-touch gestures |
| 197 | +- [ ] Add haptic feedback on supported devices |
| 198 | + |
| 199 | +## Migration Guide |
| 200 | + |
| 201 | +No breaking changes. Existing code continues to work: |
| 202 | +- Mouse-based dragging unchanged |
| 203 | +- All existing props and APIs remain the same |
| 204 | +- Desktop experience is identical |
| 205 | +- Touch support is additive |
| 206 | + |
| 207 | +## Known Limitations |
| 208 | + |
| 209 | +1. **Drag Preview Customization**: The touch drag preview is auto-generated and cannot be customized per-component (matches the original element's appearance) |
| 210 | +2. **Multi-Touch**: Currently only supports single-touch drag (multiple fingers are ignored) |
| 211 | +3. **Scroll During Drag**: Scrolling while dragging is prevented to avoid accidental drops |
| 212 | + |
| 213 | +## Support |
| 214 | + |
| 215 | +For issues or questions: |
| 216 | +- GitHub Issues: [objectui/issues](https://github.com/objectstack-ai/objectui/issues) |
| 217 | +- Documentation: [objectui.org/docs](https://www.objectui.org) |
0 commit comments