λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°

πŸ‘©‍πŸ’» 개발 곡뢀

[React/Typescript] ν™”λ©΄ μ „ν™˜ 효과 / νŽ˜μ΄λ“œμΈ μ• λ‹ˆλ©”μ΄μ…˜ / Fade μ»΄ν¬λ„ŒνŠΈ

728x90

 

 

μ’…μ„€ 개발 쀑에 ν™”λ©΄ μ „ν™˜ νš¨κ³Όκ°€ 있으면 쒋을 것 κ°™μ•„ λ„£μ–΄λ³Έ fade 효과 μ»΄ν¬λ„ŒνŠΈμž…λ‹ˆλ‹€.

duration λ³€μˆ˜λ‘œ fade in μ‹œκ°„ 쑰절 κ°€λŠ₯ν•˜κ³ ,

μ „ν™˜ μ• λ‹ˆλ©”μ΄μ…˜ μƒνƒœ 속성은 μ•„λž˜ μ°Έκ³ .

 

entering: μ»΄ν¬λ„ŒνŠΈκ°€ 화면에 μ§„μž…ν•˜λŠ” μ• λ‹ˆλ©”μ΄μ…˜μ„ μˆ˜ν–‰ 쀑인 μƒνƒœλ₯Ό λ‚˜νƒ€λƒ…λ‹ˆλ‹€.
entered: μ»΄ν¬λ„ŒνŠΈκ°€ 화면에 μ™„μ „νžˆ μ§„μž…ν•œ μƒνƒœλ₯Ό λ‚˜νƒ€λƒ…λ‹ˆλ‹€.
exiting: μ»΄ν¬λ„ŒνŠΈκ°€ ν™”λ©΄μ—μ„œ μ‚¬λΌμ§€λŠ” μ• λ‹ˆλ©”μ΄μ…˜μ„ μˆ˜ν–‰ 쀑인 μƒνƒœλ₯Ό λ‚˜νƒ€λƒ…λ‹ˆλ‹€.
exited: μ»΄ν¬λ„ŒνŠΈκ°€ ν™”λ©΄μ—μ„œ μ™„μ „νžˆ 사라진 μƒνƒœλ₯Ό λ‚˜νƒ€λƒ…λ‹ˆλ‹€.

 

ν˜Ήμ‹œ λ‚˜μ€‘μ— 써먹을 일 μƒκΈΈκΉŒλ΄ & ν•„μš”ν•˜μ‹  λΆ„ μžˆμ„κΉŒλ΄ 곡유!

 

μ•„ 참고둜 reactμ—μ„œ transition μ“°λ €λ©΄ μ„€μΉ˜ λ¨Όμ € ν•΄μ€˜μ•Όν•œλ‹€.

 

Javascript : 

npm install react-transition-group

 

Typescript : 

npm i --save-dev @types/react-transition-group

 

Fade in/Fade out/λΆ€λ“œλŸ¬μš΄ ν™”λ©΄μ „ν™˜ μ½”λ“œ

import React, { ReactNode } from "react";
import { Transition } from "react-transition-group";

interface IProps {
  children: React.ReactNode;
}

const duration = 200;

const defaultStyle = {
  transition: `opacity ${duration}ms ease-in-out`,
  opacity: 0,
};

const transitionStyles: { [key: string]: any } = {
  entering: { opacity: 1 },
  entered: { opacity: 1 },
  exiting: { opacity: 0 },
  exited: { opacity: 0 },
};

const Fade = ({ children }: IProps) => {
  const [inProp, setInProp] = React.useState(false);

  React.useEffect(() => {
    setInProp(true);
    return () => setInProp(false);
  }, []);

  return (
    <Transition in={inProp} timeout={duration}>
      {(state) => (
        <div
          style={{
            ...defaultStyle,
            ...transitionStyles[state],
          }}
        >
          {children}
        </div>
      )}
    </Transition>
  );
};

export default Fade;

 

μ‚¬μš©ν•  λ•ŒλŠ” μ•„λž˜μ™€ 같이 return 내뢀에 fade 효과λ₯Ό 주고싢은 μš”μ†Œλ“€μ„ 감싸면 λ©λ‹ˆλ‹€.

 

 

728x90