All files / src/components/layouts Header.tsx

100% Statements 46/46
100% Branches 6/6
100% Functions 17/17
100% Lines 39/39

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 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 2391x 1x                         1x   1x 1x 1x 1x 1x   1x 43x     43x 43x 43x 43x   4x 4x 4x 4x         43x                               86x     4x                     1x 19x   19x 19x 19x   19x 19x                                                                                                                                                                                   57x     2x 2x                         1x 14x   14x 14x                                       42x   1x                               1x 14x                      
import { Close as CloseIcon, Menu as MenuIcon, Translate as TranslateIcon } from '@mui/icons-material';
import {
  AppBar,
  Box,
  Button,
  Container,
  Fade,
  IconButton,
  Link,
  Menu,
  MenuItem,
  Toolbar,
  Tooltip,
} from '@mui/material';
import React from 'react';
 
import { HideOnScroll } from '@/components/elements';
import { meta, sections } from '@/constants';
import { configKeys, getConfig, setConfig } from '@/utils/config';
import { scrollToElement, scrollToTop } from '@/utils/elements';
import { changeLocale, LocaleCodes, localeCodes, localeNames } from '@/utils/localization';
 
const CultureMenu = (props: { parent: 'NarrowMenu' | 'WideMenu' }) => {
  const [localeCode, setLocaleCode] = React.useState<LocaleCodes>(
    (getConfig(configKeys.locale) ?? localeCodes.jaJp) as LocaleCodes
  );
  const [anchorEl, setAnchorEl] = React.useState<null | HTMLElement>();
  const onMenuClick = React.useCallback((e: React.MouseEvent<HTMLButtonElement>) => setAnchorEl(e.currentTarget), []);
  const onMenuClose = React.useCallback(() => setAnchorEl(null), []);
  const onSelected = React.useCallback(
    (locale: LocaleCodes) => {
      setLocaleCode(locale);
      changeLocale(locale);
      setConfig(configKeys.locale, locale);
      onMenuClose();
    },
    [setLocaleCode, onMenuClose]
  );
 
  return (
    <>
      <Tooltip title="Select Language">
        <IconButton onClick={onMenuClick} data-testid={`Header__${props.parent}__CultureList`}>
          <TranslateIcon />
        </IconButton>
      </Tooltip>
      <Menu
        open={Boolean(anchorEl)}
        onClose={onMenuClose}
        anchorEl={anchorEl}
        anchorOrigin={{ vertical: 'bottom', horizontal: 'left' }}
        transformOrigin={{ vertical: 'top', horizontal: 'left' }}
        keepMounted
      >
        {Object.keys(localeNames).map((locale) => (
          <MenuItem
            key={`Header__${props.parent}__CultureList--${locale}`}
            selected={locale === localeCode}
            onClick={() => onSelected(locale as LocaleCodes)}
            data-testid={`Header__${props.parent}__CultureList--${locale}`}
          >
            {localeNames[locale]}
          </MenuItem>
        ))}
      </Menu>
    </>
  );
};
 
const NarrowMenu = () => {
  const [anchorEl, setAnchorEl] = React.useState<null | HTMLElement>();
 
  const onTitleClick = React.useCallback(() => scrollToTop(), []);
  const onMenuClick = React.useCallback((e: React.MouseEvent<HTMLButtonElement>) => setAnchorEl(e.currentTarget), []);
  const onMenuClose = React.useCallback(() => setAnchorEl(null), [setAnchorEl]);
 
  const display = { xs: 'flex', sm: 'none' };
  return (
    <>
      <Link
        component="button"
        variant="h6"
        color="inherit"
        underline="none"
        sx={{ display, cursor: 'pointer' }}
        onClick={onTitleClick}
        data-testid="Header__NarrowMenu__Title"
      >
        {meta.title}
      </Link>
      <Box sx={{ display, flexGrow: 1 }} />
      <Box sx={{ display, m: 0 }}>
        {/* 言語設定 */}
        <CultureMenu parent="NarrowMenu" />
 
        {/* ナビゲーション */}
        <IconButton onClick={onMenuClick} data-testid="Header__NarrowMenu__Hamburger">
          {
            // NOTE: CSS で中央寄せレイアウトを構築する
            //   - display: flex
            //       要素を Flexbox とする
            //       Flexbox は指定の方向に沿った柔軟な要素の配置を実現
            //         ┌───────────────────────┐
            //         │                       │
            //         │                       │
            //         │                       │
            //         │                       │
            //         │                       │
            //         └───────────────────────┘
            //   - flex-direction: row (default) | column | ...
            //       Flexbox の基準軸
            //       子要素を配置する方向を指定
            //       row: 水平 (左から右), column: 垂直 (上から下)
            //         ⹀⹀⹀⹀⹀⹀⹀⹀⹀⹀⹀⹀⹀⹀⹀⹀⹀⹀⹀⹀⹀⹀⹀⹀⹀⹀⹀⹀>
            //         ┌───────────────────────┐   ┌───────────────────────┐ ‖
            //         │                       │   │                       │ ‖
            //         │                       │   │                       │ ‖
            //         │                       │   │                       │ ‖
            //         │                       │   │                       │ ‖
            //         │                       │   │                       │ ‖
            //         └───────────────────────┘   └───────────────────────┘ ∨
            //   - justify-content: flex-start (default) | flex-end | center | space-between |...
            //       基準軸の方向に対する子要素の位置
            //       flex-start: 先頭寄せ, flex-end: 末尾寄せ, center: 中央寄せ, space-between: 等間隔
            //         ⹀⹀⹀⹀⹀⹀⹀⹀⹀⹀⹀⹀⹀⹀⹀⹀⹀⹀⹀⹀⹀⹀⹀⹀⹀⹀⹀⹀>
            //         ┌───────────────────────┐   ┌───────────────────────┐ ‖
            //         │                       │   │         elem1         │ ‖
            //         │                       │   │         elem2         │ ‖
            //         │elem1 elem2 elem3      │   │         elem3         │ ‖
            //         │                       │   │                       │ ‖
            //         │                       │   │                       │ ‖
            //         └───────────────────────┘   └───────────────────────┘ ∨
            //   - align-items: flex-start | flex-end | center | stretch (default) | ...
            //       基準軸に交差する方向に対する子要素の配置 (水平軸であれば垂直方向、垂直軸であれば水平方向)
            //       flex-start: 先頭寄せ, flex-end: 末尾寄せ, center: 中央寄せ, stretch: 領域全体
            //         ⹀⹀⹀⹀⹀⹀⹀⹀⹀⹀⹀⹀⹀⹀⹀⹀⹀⹀⹀⹀⹀⹀⹀⹀⹀⹀⹀⹀>
            //         ┌───────────────────────┐   ┌───────────────────────┐ ‖
            //         │   elem1 elem2 elem3   │   │                       │ ‖
            //         │                       │   │elem1                  │ ‖
            //         │                       │   │elem2                  │ ‖
            //         │                       │   │elem3                  │ ‖
            //         │                       │   │                       │ ‖
            //         └───────────────────────┘   └───────────────────────┘ ∨
          }
          <Box sx={{ display: 'flex', justifyContent: 'center', alignItems: 'center', width: 30, height: 30 }}>
            {!anchorEl && (
              <Fade in={!anchorEl} unmountOnExit>
                <MenuIcon />
              </Fade>
            )}
            {!!anchorEl && (
              <Fade in={!!anchorEl} unmountOnExit>
                <CloseIcon />
              </Fade>
            )}
          </Box>
        </IconButton>
        <Menu
          open={Boolean(anchorEl)}
          onClose={onMenuClose}
          anchorEl={anchorEl}
          anchorOrigin={{ vertical: 'bottom', horizontal: 'left' }}
          transformOrigin={{ vertical: 'top', horizontal: 'left' }}
          sx={{ display: { xs: 'block', sm: 'none' } }}
          keepMounted
        >
          {Object.values(sections).map((section) => (
            <MenuItem
              key={`Header__NarrowMenu__${section}`}
              onClick={() => {
                scrollToElement(section, -20);
                onMenuClose();
              }}
              data-testid={`Header__NarrowMenu__${section}`}
            >
              {section}
            </MenuItem>
          ))}
        </Menu>
      </Box>
    </>
  );
};
 
const WideMenu = () => {
  const onTitleClick = React.useCallback(() => scrollToTop(), []);
 
  const display = { xs: 'none', sm: 'flex' };
  return (
    <>
      <Link
        component="button"
        variant="h6"
        color="inherit"
        underline="none"
        sx={{ display, cursor: 'pointer' }}
        onClick={onTitleClick}
        data-testid="Header__WideMenu__Title"
      >
        {meta.title}
      </Link>
      <Box sx={{ display, flexGrow: 1 }} />
      <Box sx={{ display }}>
        {/* 言語設定 */}
        <CultureMenu parent="WideMenu" />
 
        {/* ナビゲーション */}
        {Object.values(sections).map((section) => (
          <Button
            key={`Header__WideMenu__${section}`}
            onClick={() => scrollToElement(section, -60)}
            sx={{
              margin: '0 8px',
              padding: '8px',
              color: 'common.white',
            }}
            data-testid={`Header__WideMenu__${section}`}
          >
            {section}
          </Button>
        ))}
      </Box>
    </>
  );
};
 
export const Header = () => (
  <HideOnScroll>
    <AppBar component="header" position="fixed">
      <Container maxWidth="xl">
        <Toolbar disableGutters sx={{ minHeight: 0 }}>
          <NarrowMenu />
          <WideMenu />
        </Toolbar>
      </Container>
    </AppBar>
  </HideOnScroll>
);