Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: scrollable container custom hook #230

Merged
merged 1 commit into from
Mar 16, 2023
Merged

Conversation

joohaem
Copy link
Member

@joohaem joohaem commented Mar 16, 2023

  • 브랜치명, 브랜치 알맞게 설정
  • Reviewer, Assignees, Label, Milestone, Issue(PR 작성 후에) 붙이기
  • PR이 승인된 경우 해당 브랜치는 삭제하기

📌 내용

  • x-drag를 데스크탑에서도 그래그 할 수 있게끔 기능을 추가합니다
  • 고마워 지피티!

📌 내가 알게 된 부분

  • x-drag 컨테이너가 데스크탑에서도 드래그 할 수 있도록 기능을 추가합니다
  • 고마워 지피티!
// 지피티가 짜준 코드
import { useState, useRef } from 'react';

function ScrollableContainer() {
  const containerRef = useRef(null);
  const [isDragging, setIsDragging] = useState(false);
  const [startX, setStartX] = useState(null);

  function handleMouseDown(event) {
    setIsDragging(true);
    setStartX(event.pageX);
  }

  function handleMouseMove(event) {
    if (!isDragging) return;
    const container = containerRef.current;
    container.scrollLeft = container.scrollLeft + startX - event.pageX;
    setStartX(event.pageX);
  }

  function handleMouseUp() {
    setIsDragging(false);
  }

  return (
    <div
      ref={containerRef}
      className="scrollable-container"
      onMouseDown={handleMouseDown}
      onMouseMove={handleMouseMove}
      onMouseUp={handleMouseUp}
      onMouseLeave={handleMouseUp}
    >
      {/* 스크롤 가능한 컨텐츠 요소 */}
    </div>
  );
}

@joohaem joohaem requested a review from NYeonK March 16, 2023 16:19
@joohaem joohaem self-assigned this Mar 16, 2023
@joohaem joohaem linked an issue Mar 16, 2023 that may be closed by this pull request
7 tasks
@joohaem joohaem merged commit 5d212c0 into release/1.3 Mar 16, 2023
@joohaem joohaem deleted the feat/#229-drag branch March 16, 2023 16:20
Copy link
Contributor

@NYeonK NYeonK left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

지피티 도랐네 진짜로...
그걸 스크롤에 이용할 생각한 당신은 천재✨🌟🍻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[ Main ] 마우스 드래그 기능 추가
2 participants