Skip to content

Commit

Permalink
integrated typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
minhphuc010194 committed Jan 16, 2023
1 parent e3a6ff5 commit d70b9dd
Show file tree
Hide file tree
Showing 9 changed files with 213 additions and 67 deletions.
11 changes: 11 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"files.exclude": {
"**/.git": true,
"**/.svn": true,
"**/.hg": true,
"**/CVS": true,
"**/.DS_Store": true,
"**/Thumbs.db": true,
"**/node_modules": true
}
}
9 changes: 8 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"web-vitals": "^3.0.0"
},
"scripts": {
"start": "react-scripts start",
"dev": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
Expand All @@ -39,5 +39,12 @@
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"@types/jest": "^29.2.5",
"@types/node": "^18.11.18",
"@types/react": "^18.0.26",
"@types/react-dom": "^18.0.10",
"typescript": "^4.9.4"
}
}
85 changes: 21 additions & 64 deletions src/components/MenuItems.jsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { useState, useEffect, useRef } from "react";
import { Link } from "react-router-dom";
import { Box } from "@chakra-ui/react";
import { Box, Link } from "@chakra-ui/react";
import Dropdown from "./Dropdown";
import { RANGE } from "utils";

const MenuItems = ({ items, depthLevel }) => {
const [dropdown, setDropdown] = useState(false);

let ref = useRef();
const ref = useRef();

useEffect(() => {
const handler = (event) => {
Expand All @@ -24,11 +23,11 @@ const MenuItems = ({ items, depthLevel }) => {
}, [dropdown]);

const onMouseEnter = () => {
window.innerWidth > 960 && setDropdown(true);
window.innerWidth > RANGE && setDropdown(true);
};

const onMouseLeave = () => {
window.innerWidth > 960 && setDropdown(false);
window.innerWidth > RANGE && setDropdown(false);
};

const closeDropdown = () => {
Expand All @@ -43,61 +42,23 @@ const MenuItems = ({ items, depthLevel }) => {
onMouseEnter={onMouseEnter}
onMouseLeave={onMouseLeave}
onClick={closeDropdown}
px={2}
px={13}
>
{items.url && items.submenu ? (
<>
<Box
as="button"
display="flex"
alignItems="center"
color="inherit"
fontSize="inherit"
border="none"
backgroundColor="transparent"
cursor="pointer"
width="100%"
onClick={() => setDropdown((prev) => !prev)}
_hover={{
bg: "#f2f2f2",
}}

// aria-haspopup="menu"
// aria-expanded={dropdown ? "true" : "false"}
>
{window.innerWidth < 960 && depthLevel === 0 ? (
<Box _hover={{ bg: "gray.300" }}>
{window.innerWidth < RANGE && depthLevel === 0 ? (
items.title
) : (
<Link
to={items.url}
style={{
display: "block",
fontSize: "inherit",
color: "inherit",
textDecoration: "none",
}}
>
{items.title}
</Link>
<Link to={items.url}>{items.title}</Link>
)}

{depthLevel > 0 &&
window.innerWidth < 960 ? null : depthLevel > 0 &&
window.innerWidth > 960 ? (
window.innerWidth < RANGE ? null : depthLevel > 0 &&
window.innerWidth > RANGE ? (
<span>&raquo;</span>
) : (
<Box
as="span"
_after={{
content: `""`,
display: "inline-block",
marginLeft: "0.28em",
verticalAlign: "0.09em",
borderTop: "0.42em solid",
borderRight: "0.32em solid transparent",
borderLeft: "0.32em solid transparent",
}}
/>
<Box />
)}
</Box>
<Dropdown
Expand All @@ -109,21 +70,15 @@ const MenuItems = ({ items, depthLevel }) => {
) : !items.url && items.submenu ? (
<>
<Box
as="button"
display="flex"
alignItems="center"
color="inherit"
fontSize="inherit"
border="none"
backgroundColor="transparent"
cursor="pointer"
width="100%"
_hover={{
bg: "#f2f2f2",
bg: "gray.300",
}}
onClick={() => setDropdown((prev) => !prev)}
// aria-expanded={dropdown ? "true" : "false"}
// aria-haspopup="menu"
>
{items.title}{" "}
{depthLevel > 0 ? (
Expand Down Expand Up @@ -151,12 +106,14 @@ const MenuItems = ({ items, depthLevel }) => {
</>
) : (
<Link
to={items.url}
style={{
display: "block",
fontSize: "inherit",
color: "inherit",
textDecoration: "none",
href={items.url}
display="block"
fontSize="inherit"
color="inherit"
textDecoration="none"
cursor="pointer"
_hover={{
bg: "gray.300",
}}
>
{items.title}
Expand Down
4 changes: 3 additions & 1 deletion src/index.js → src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ const colors = {
},
};
const theme = extendTheme({ colors });
const root = ReactDOM.createRoot(document.getElementById("root"));
const root = ReactDOM.createRoot(
document.getElementById("root") as HTMLAreaElement
);
root.render(
<ChakraProvider theme={theme}>
<BrowserRouter>
Expand Down
File renamed without changes.
1 change: 1 addition & 0 deletions src/utils/constant.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const RANGE = 960;
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./constant";
31 changes: 31 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"compilerOptions": {
"target": "ES6",
"lib": [
"DOM",
"ES6",
"DOM.Iterable",
"ScriptHost",
"ES2016.Array.Include"
],
"baseUrl": "./src",
"allowJs": true,
"strictNullChecks": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"noImplicitAny": false,
"jsx": "react-jsx"
},
"typeRoots": ["./node_modules/@types", "./index.d.ts"],
"include": ["src/**/*", "**/*.ts", "**/*.tsx"],
"exclude": ["node_modules"]
}
Loading

1 comment on commit d70b9dd

@vercel
Copy link

@vercel vercel bot commented on d70b9dd Jan 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.