Passed
Pull Request — dev (#45)
by James
03:39
created

Sidebar.jsx ➔ handleResize   A

Complexity

Conditions 3

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 2
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
import React from "react";
2
import { Link, NavLink } from "react-router-dom";
3
import { links } from "../data/data";
4
import { ReactComponent as LogoSpark } from "../assets/LogoSpark.svg";
5
import { useState, useEffect } from "react";
6
const activeLink = "bg-sidebarActive rounded-xl text-blue-300";
7
const normalLink = "text-white";
8
9
const SidebarItems = () => {
10
  return links.map((item) => {
11
    return (
12
      <NavLink
13
        key={item.title}
14
        to={`/${item.title}`}
15
        className={({ isActive }) => (isActive ? activeLink : normalLink)}
16
      >
17
        <div
18
          className="
19
          flex items-center p-2 font-normal text-2xl py-3
20
         hover:bg-sidebarHover rounded-xl transition-colors"
21
        >
22
          <span className="px-3">{item.icon}</span>
23
          <span className="capitalize">{item.title}</span>
24
        </div>
25
      </NavLink>
26
    );
27
  });
28
};
29
30
const compactSidebarItem = () => {
31
  return links.map((item) => {
32
    return (
33
      <NavLink
34
        key={item.title}
35
        to={`/${item.title}`}
36
        className={({ isActive }) => (isActive ? activeLink : normalLink)}
37
      >
38
        <div
39
          className="
40
          flex items-center p-2 font-normal text-2xl py-3
41
         hover:bg-sidebarHover rounded-xl transition-colors"
42
        >
43
          <span className="px-3">{item.icon}</span>
44
        </div>
45
      </NavLink>
46
    );
47
  });
48
};
49
50
const Sidebar = () => {
51
  //const [displaySidebar, setDisplaySidebar] = useState(true);
52
  const [width, setwidth] = useState(window.innerWidth);
53
  useEffect(() => {
54
    function handleResize() {
55
      setwidth(window.innerWidth);
56
    }
57
    window.addEventListener("resize", handleResize);
58
59
    return () => {
60
      window.removeEventListener("resize", handleResize);
61
    };
62
  }, []);
63
64
  if (width < 1536) {
65
    return (
66
      <div className="relative mr-4 w-20 z-10">
67
        <nav
68
          className="
69
            h-screen overflow-auto flex flex-col bg-sidebarBlue p-3 fixed"
70
        >
71
          {/* <div className="pt-2 pb-5">
72
            <LogoSpark />
73
          </div> */}
74
          {compactSidebarItem()}
75
        </nav>
76
      </div>
77
    );
78
  }
79
80
  return (
81
    <div className="relative min-w-80 w-80 mr-4 z-10">
82
      <nav
83
        className="
84
          h-screen overflow-auto flex flex-col
85
          w-72 bg-sidebarBlue p-3 fixed"
86
      >
87
        <div className="pt-2 pb-5">
88
          <LogoSpark />
89
        </div>
90
        {SidebarItems()}
91
      </nav>
92
    </div>
93
  );
94
};
95
96
export default Sidebar;
97