화면에 존재하는 각 패널(예: 동영상 목록, 추천 영상, 댓글) 등을 드래그로 자유롭게 이동하고, 다른 패널 가장자리에 스냅되며, 자동 정렬되는 유튜브 스타일의 레이아웃을 만들기 위해 필요한 주요 기능은 다음과 같습니다:
✅ 요구사항 정리
- React + TypeScript 기반 UI
- 사용자 패널(예: 추천, 댓글...)을 drag-and-drop으로 자유롭게 이동
- 다른 패널의 edge에 snap
- 화면 전체에서 panel끼리 자동 정렬 (Layout Auto-Ajust)
- 모듈성 좋은 설계
🧰 사용 기술 스택
| 라이브러리 | 용도 |
|---|
React + TypeScript | SPA 작성 |
react-draggable | 드래그 이동 지원 |
react-snap-grid (또는 custom snap logic) | 패널끼리 스냅 |
| CSS Grid / Flexbox | Layout |
📦 설치할 라이브러리
bash
npm install react-draggable
🧩 1. 전체 구성 설명
우리는 다음과 같은 구조를 가정합니다:
App.tsx: 전체 레이아웃의 최상위 컴포넌트
DraggablePanel.tsx: 드래그 가능한 개별 패널
- 각 패널은
position, size, id를 가짐
📁 디렉토리 구성
code
src/
├── components/
│ └── DraggablePanel.tsx
├── App.tsx
└── types.d.ts
🧾 types.d.ts
ts
export interface PanelConfig {
id: string
title: string
x: number
y: number
width: number
height: number
}
💠 DraggablePanel.tsx
tsx
// components/DraggablePanel.tsx
import React from 'react';
import Draggable from 'react-draggable';
import './DraggablePanel.css';
import { PanelConfig } from '../types';
interface DraggablePanelProps {
panel: PanelConfig;
onDragStop: (id: string, x: number, y: number) => void;
}
const DraggablePanel: React.FC<DraggablePanelProps> = ({ panel, onDragStop }) => {
return (
<Draggable
position={{ x: panel.x, y: panel.y }}
onStop={(_, data) => onDragStop(panel.id, data.x, data.y)}
grid={[20, 20]} // grid snap
bounds="parent"
>
<div className="panel" style={{ width: panel.width, height: panel.height }}>
<div className="panel-header">{panel.title}</div>
<div className="panel-content">내용 삽입 위치</div>
</div>
</Draggable>
);
};
export default DraggablePanel;
🎨 DraggablePanel.css
css
.panel {
background-color: #ffffff;
border: 1px solid #ccc;
border-radius: 8px;
position: absolute;
box-shadow: 0 1px 8px rgba(0,0,0,0.1);
overflow: hidden;
user-select: none;
}
.panel-header {
background-color: #f1f1f1;
padding: 8px 12px;
cursor: move;
font-weight: bold;
}
.panel-content {
padding: 12px;
height: calc(100% - 40px);
}
🌐 App.tsx
tsx
// App.tsx
import React, { useState } from 'react';
import DraggablePanel from './components/DraggablePanel';
import { PanelConfig } from './types';
import './App.css';
const initialPanels: PanelConfig[] = [
{ id: 'video', title: '메인 영상', x: 40, y: 40, width: 480, height: 270 },
{ id: 'recommend', title: '추천 영상', x: 540, y: 40, width: 240, height: 450 },
{ id: 'comments', title: '댓글', x: 40, y: 330, width: 480, height: 200 },
];
function App() {
const [panels, setPanels] = useState<PanelConfig[]>(initialPanels);
const handleDragStop = (id: string, x: number, y: number) => {
setPanels(prevPanels =>
prevPanels.map(panel =>
panel.id === id ? { ...panel, x, y } : panel
)
);
};
return (
<div className="app-container">
{panels.map(panel => (
<DraggablePanel
key={panel.id}
panel={panel}
onDragStop={handleDragStop}
/>
))}
</div>
);
}
export default App;
🖼️ App.css
css
.app-container {
position: relative;
width: 100vw;
height: 100vh;
background-color: #fafafa;
overflow: hidden;
}
✨ Snap 기능 설명
현재 snap 기능은 grid=[20,20] 옵션으로 기본 제공되고 있습니다.
좀 더 정밀한 edge snap을 원하면, 아래처럼 마우스를 놓을 때 가장 가까운 edge로 정렬하는 로직을 넣을 수 있습니다.
📌 스냅 로직 커스터마이징 예 (onDragStop 수정)
tsx
const SNAP_DISTANCE = 30;
const snapToNearby = (x: number, y: number, target: PanelConfig, others: PanelConfig[]) => {
for (const other of others) {
if (other.id === target.id) continue;
// Snap X
if (Math.abs(x - (other.x + other.width)) < SNAP_DISTANCE) {
x = other.x + other.width;
} else if (Math.abs(x + target.width - other.x) < SNAP_DISTANCE) {
x = other.x - target.width;
}
// Snap Y
if (Math.abs(y - (other.y + other.height)) < SNAP_DISTANCE) {
y = other.y + other.height;
} else if (Math.abs(y + target.height - other.y) < SNAP_DISTANCE) {
y = other.y - target.height;
}
}
return { x, y };
};
const handleDragStop = (id: string, x: number, y: number) => {
const target = panels.find(panel => panel.id === id)!;
const others = panels.filter(panel => panel.id !== id);
const adjusted = snapToNearby(x, y, target, others);
setPanels(prev =>
prev.map(panel =>
panel.id === id ? { ...panel, x: adjusted.x, y: adjusted.y } : panel
)
);
};
📽️ 결과 예시
메인 영상을 드래그하여 추천 영상 옆에 붙이기
댓글 패널을 메인 영상 아래로 위치 이동
- 이동 시 자동
snap됨
✅ 향후 확장 포인트
- 패널 닫기/최소화, 크기 조절 추가
- 패널 저장 (localStorage로 배치 상태 불러오기)
- snap line 시각화 (
react-rnd 등 라이브러리 활용 가능)
- 모바일 터치 지원
📌 마무리
이 코드는 YouTube UI 스타일의 구성 요소를 자유롭게 이동 가능하도록 만든 React+TS 기반 예제입니다.
간단한 edge snap을 포함하고 있으며, 사용자 편의성을 고려해 확장 가능한 구조로 설계되었습니다.
필요하면 해당 UI를 전체 유튜브 클론에 연결 or 더 정교한 레이아웃 관리자 UI로 확장 가능합니다!
원하는 기능이 더 있다면 말씀해주세요 👍