[TIL/React] 2023/08/21

์›๋ฏผ๊ด€ยท2023๋…„ 8์›” 21์ผ
0

[TIL]

๋ชฉ๋ก ๋ณด๊ธฐ
101/159

event page ๊ด€๋ จ ๐ŸŸ 

์ •์œก๊ฐ์˜ event page๋ฅผ ๊ตฌํ˜„ํ•˜๊ณ ์ž ํ•œ๋‹ค. ๋งˆ์šฐ์Šค๋ฅผ 'press'ํ•˜๊ณ (click๊ณผ ๊ตฌ๋ถ„ํ•  ๊ฒƒ) ์šฐ์ธก์—์„œ ์ขŒ์ธก์œผ๋กœ ๋„˜๊ธฐ๋ฉด, ์ง„ํ–‰์ค‘์ธ event๋“ค์ด oveflow๋˜๋Š” ๋ชจ์Šต์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. best practice๋ฅผ ์ดํ•ดํ•˜๋Š” ๋ฐฉ์‹์„ ํƒํ–ˆ๋‹ค.

best practice ๐Ÿ”ต

import React, { useState } from "react";
import styled from "styled-components";

const CardContainer = styled.div`
  display: flex;
  overflow-x: scroll;
  width: 100%;
  height: 200px;
  background-color: #f0f0f0;
  cursor: grab;
`;

const Card = styled.div`
  flex: 0 0 200px;
  height: 100%;
  background-color: #fff;
  border: 1px solid #ccc;
  margin-right: 10px;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 20px;
`;

const App = () => {
  const [isMousePressed, setIsMousePressed] = useState(false);
  const [containerScrollLeft, setContainerScrollLeft] = useState(0);
  const [startMouseX, setStartMouseX] = useState(0);

  const handleMousePress = (e) => {
    setIsMousePressed(true);
    setStartMouseX(e.pageX - e.currentTarget.offsetLeft);
    setContainerScrollLeft(e.currentTarget.scrollLeft);
  };

  const handleMouseMove = (e) => {
    if (!isMousePressed) return;
    e.preventDefault();
    const currentMouseX = e.pageX - e.currentTarget.offsetLeft;
    const mouseMovement = currentMouseX - startMouseX;
    console.log(mouseMovement);
    console.log("2", containerScrollLeft);
    e.currentTarget.scrollLeft = containerScrollLeft - mouseMovement;
  };

  const handleMouseRelease = () => {
    setIsMousePressed(false);
  };

  return (
    <CardContainer
      // ๋ˆ„๋ฅด๋Š” ์ˆœ๊ฐ„
      onMouseDown={handleMousePress}
      onMouseMove={handleMouseMove}
      onMouseUp={handleMouseRelease}
      onMouseLeave={handleMouseRelease}
    >
      {/* ์นด๋“œ ์ปดํฌ๋„ŒํŠธ๋“ค */}

      <Card>Card 1</Card>
      <Card>Card 2</Card>
      <Card>Card 3</Card>
      <Card>Card 4</Card>
      <Card>Card 5</Card>
      <Card>Card 1</Card>
      <Card>Card 2</Card>
      <Card>Card 3</Card>
      <Card>Card 4</Card>
      <Card>Card 5</Card>
    </CardContainer>
  );
};

export default App;

card container์— onMouseDown, onMouseMove, onMouseUp, onMouseLeave๊ฐ€ ์กด์žฌํ•œ๋‹ค.

onClick์€ 'ํด๋ฆญ'ํ•  ๋•Œ ํŠน์ • event ๊ฐ์ฒด๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. ์œ„ ์š”์†Œ๋“ค๋„ ๋งˆ์ฐฌ๊ฐ€์ง€์ด๋‹ค.

onMouseDown๋Š” ๋งˆ์šฐ์Šค๋กœ pressํ•  ๋•Œ, onMouseMove๋Š” ๋งˆ์šฐ์Šค๋กœ ๋”์—์„œ ์›€์ง์ผ ๋•Œ, onMouseUp๋Š” ๋งˆ์šฐ์Šค๋ฅผ ๋—„ ๋•Œ, onMouseLeave๋Š” ํŠน์ • element์—์„œ ๋งˆ์šฐ์Šค๊ฐ€ ๋– ๋‚ฌ์„ ๋•Œ event ๊ฐ์ฒด๋ฅผ ๋ฐœ์ƒ์‹œํ‚จ๋‹ค.

  1. onMouseDown

onMouseDown์—๋Š” handleMousePress ํ•จ์ˆ˜๊ฐ€ ์ „๋‹ฌ๋œ๋‹ค. isMousePressed๋ผ๋Š” ์ƒํƒœ๋ฅผ true๋กœ ๋ณ€๊ฒฝํ•œ๋‹ค. ์ด์–ด์„œ startMouseX๋ผ๋Š” ์ƒํƒœ๋ฅผ, 'press'ํ•˜๋Š” ์ˆœ๊ฐ„ page์˜ x์ขŒํ‘œ์—์„œ card container์˜ offsetLeft๋ฅผ ๋บ€ ๊ฐ’์œผ๋กœ updateํ•œ๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ containerScrollLeft๋ผ๋Š” ์ƒํƒœ๋ฅผ e.currentTarget.scrollLeft๋กœ updateํ•œ๋‹ค

  1. onMouseMove

pressํ•œ ์ƒํƒœ๋ผ๋ฉด if๋ฌธ์„ ํ†ต๊ณผํ•œ ๋’ค preventDefault ํ•จ์ˆ˜๊ฐ€ ์‹คํ–‰๋œ๋‹ค. ์ดํ›„ currentMouseX ์ƒ์ˆ˜์— page์˜ x์ขŒํ‘œ์—์„œ offsetLeft๋ฅผ ๋บ€ ๊ฐ’์„ ํ• ๋‹นํ•œ๋‹ค. ์›€์ง์ธ ์ดํ›„์˜ x์ขŒํ‘œ๋ฅผ ์˜๋ฏธํ•˜๊ธฐ์— startMouseX์˜ x์ขŒํ‘œ์™€๋Š” ๊ตฌ๋ถ„๋œ๋‹ค๋Š” ์ ์„ ์ธ์ง€ํ•ด์•ผ ํ•œ๋‹ค.

mouseMovement ์ƒ์ˆ˜๋Š” currrent page์˜ x์ขŒํ‘œ์—์„œ start page์˜ x์ขŒํ‘œ๋ฅผ ๋บ€ ๊ฐ’์ด ํ• ๋‹น๋œ๋‹ค.

preventDefault, currentMouseX ํ• ๋‹น, mouseMovement ํ• ๋‹น์ด ์ข…๋ฃŒ๋˜๋ฉด, ๋งˆ์ง€๋ง‰์œผ๋กœ e.currentTarget.scrollLeft ๊ฐ’์„ containerScrollLeft์—์„œ mouseMovement๋ฅผ ๋บ€ ๊ฐ’์œผ๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค.

  1. onMouseUp, onMouseLeave

isMousePressed๋ฅผ false๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๊ธฐ๋Šฅ๋งŒ์„ ์ˆ˜ํ–‰ํ•˜๋Š” ํ•จ์ˆ˜์ด๋‹ค.

ํšŒ๊ณ  ๐Ÿ”ด

containerScrollLeft ๋ถ€๋ถ„์ด ์ดํ•ด๊ฐ€ ๋˜์ง€ ์•Š์•˜๋‹ค. ๋‚ด์ผ์€ containerScrollLeft๊นŒ์ง€ ๋ช…ํ™•ํ•˜๊ฒŒ ์ดํ•ดํ•ด์„œ ํ”„๋กœ์ ํŠธ์— ์ ์šฉํ•˜๋Š” ๋ฐ ์„ฑ๊ณตํ•˜๊ธธ ๋ฐ”๋ž€๋‹ค. ์ดํ•ดํ•˜์ง€ ๋ชปํ•˜๊ณ  ์ž๋ ค๋‹ˆ๊นŒ ๊ต‰์žฅํžˆ ์ฐ์ฐํ•œ๋ฐ, ์ฒ˜์Œ ์‹œ๋„ํ•ด ๋ณด๋Š” ๊ฑฐ๋‹ˆ๊นŒ ์ฒซ ์ˆ ์— ๋ฐฐ๋ถ€๋ฅด๋ ค ์š•์‹ฌ๋‚ด์ง€ ๋ง์ž. ๋ฐ”๋ณด ๊ฐ์ž ์ฃผ์ œ์— ํ•œ ํ์— ์ดํ•ด๋˜๊ธธ ๋ฐ”๋ผ๋Š” ๊ฑด '์˜ค๋ฒ„'๋‹ค.

profile
Write a little every day, without hope, without despair โœ๏ธ

0๊ฐœ์˜ ๋Œ“๊ธ€