All files / src/components/layouts Banner.tsx

79.41% Statements 27/34
0% Branches 0/2
75% Functions 3/4
78.12% Lines 25/32

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 1821x 1x 1x 1x 1x 1x   1x 1x 1x 1x   1x 1x                       1x 1x         1x 1x                                                                         1x 1x   1x                       1x 1x                 1x 1x 14x   1x                                                                                                                                                                      
import { KeyboardDoubleArrowDown as KeyboardDoubleArrowDownIcon } from '@mui/icons-material';
import { Box, Container, styled, Typography } from '@mui/material';
import React from 'react';
import { useInView } from 'react-intersection-observer';
import Particles from 'react-tsparticles';
import Typed from 'react-typed';
 
import imageBanner from '@/assets/banner.webp';
import { SectionFadeIn } from '@/components/elements';
import { meta, sections, styles } from '@/constants';
import { stringFormat } from '@/utils/strings';
 
const Image = styled(Box)(({ theme }) =>
  theme.unstable_sx({
    position: 'absolute',
    left: 0,
    right: 0,
    top: 0,
    bottom: 0,
    backgroundSize: 'cover',
    backgroundRepeat: 'no-repeat',
    backgroundPosition: 'center center',
  })
);
 
const FreezableParticles = React.memo(function _({ inView }: { inView: boolean }) {
  Iif (!inView) {
    console.debug('particles is hidden.');
    return null;
  }
 
  console.debug('particles rendered');
  return (
    <Particles
      options={{
        style: {
          position: 'absolute',
        },
        particles: {
          color: {
            value: '#ff0000',
            animation: { h: { enable: true, speed: 100 } },
          },
          opacity: {
            value: 0.7,
          },
          size: {
            value: { min: 0.1, max: 3 },
          },
          number: {
            value: 70,
            density: { enable: true },
          },
          move: {
            enable: true,
            speed: 3,
          },
          links: {
            enable: true,
            opacity: 0.7,
            color: { value: '#ffffff' },
          },
        },
      }}
      data-testid="Banner__Particles"
    />
  );
});
 
export const Banner = () => {
  const { ref, inView } = useInView(styles.intersectionOptions);
 
  const handleScrollClick = React.useCallback(() => {
    const element = document.getElementById(sections.products);
    Iif (element) {
      const offset = -20;
      const elementPosition = element.getBoundingClientRect().top + window.scrollY;
      window.scrollTo({
        top: elementPosition + offset,
        behavior: 'smooth',
      });
    }
  }, []);
 
  const bannerTexts = [`I'm ${meta.author}.`, 'Welcome to my portfolio site.'];
  const bannerFormats = [
    'print("{0}")',
    'cat("{0}")',
    'NSLog(@"{0}")',
    'DISPLAY "{0}"',
    'puts "{0}"',
    'write("{0}")',
    'ECHO {0}',
  ];
  const strings = [];
  for (let i = 0; i < bannerFormats.length * bannerTexts.length; i++)
    strings.push(stringFormat(bannerFormats[i % bannerFormats.length], bannerTexts[i % bannerTexts.length]));
 
  return (
    <SectionFadeIn in={inView} ref={ref}>
      <Box>
        {
          // ------------------------------------------------------------------------------
          // イメージ
          // ------------------------------------------------------------------------------
        }
        <Image sx={{ backgroundImage: `url(${imageBanner})` }} data-testid="Banner__Image" />
 
        {
          // ------------------------------------------------------------------------------
          // パーティクル
          // ------------------------------------------------------------------------------
        }
        <FreezableParticles inView={inView} />
 
        {
          // ------------------------------------------------------------------------------
          // テキストエフェクト
          // ------------------------------------------------------------------------------
        }
        <Box
          sx={{
            display: 'flex',
            alignItems: 'center',
            height: '100vh',
          }}
        >
          <Container sx={{ zIndex: 1 }}>
            <Box
              sx={{
                color: 'common.white',
                bgcolor: '#111d',
                borderRadius: 2,
                boxShadow: 1,
                p: { xs: 2, sm: 4 },
                mb: 8, // 若干上寄りに配置する
              }}
            >
              <Typography sx={{ fontSize: 32, fontWeight: 'bold' }} data-testid="Banner__Text">
                <Typed strings={strings} typeSpeed={50} backSpeed={20} backDelay={1800} loop />
              </Typography>
            </Box>
          </Container>
        </Box>
 
        {
          // ------------------------------------------------------------------------------
          // スクロールアロー
          // ------------------------------------------------------------------------------
        }
        <Box
          sx={{
            position: 'absolute',
            bottom: '20vh',
            left: '50%',
            transform: 'translateX(-50%)',
          }}
        >
          <style>
            {`@keyframes move-y {
              from { transform: translateY(0); }
              to { transform: translateY(10px); }
            }`}
          </style>
          <KeyboardDoubleArrowDownIcon
            onClick={handleScrollClick}
            sx={{
              width: 40,
              height: 40,
              cursor: 'pointer',
              color: 'common.white',
              // アニメーションを増やすと煩いので一旦オフ
              // animation: 'move-y .75s infinite alternate ease-in-out',
            }}
            data-testid="Banner__ScrollArrow"
          />
        </Box>
      </Box>
    </SectionFadeIn>
  );
};