-
Notifications
You must be signed in to change notification settings - Fork 2
Navbar dropdown component #17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,10 @@ | ||
| import Link from 'next/link' | ||
| import { useState } from 'react' | ||
| import styled, { css, FontType, useTheme } from 'styled-components' | ||
| import { fontTypeCss } from '@/styles/sample-rhdevs-website/index.styled' | ||
|
|
||
| import { dropdownTitles } from '@/texts/common/dropdownTitles' | ||
|
|
||
| const activeCss = css` | ||
| color: ${(props) => props.theme.palette.primary}; | ||
| //css solution for styled underline | ||
|
|
@@ -22,13 +25,28 @@ const inactiveCss = css` | |
| font-size: 130%; | ||
| } | ||
| ` | ||
| const DropdownDiv = styled.div` | ||
| display: flex; | ||
| flex-direction: column; | ||
| height: auto; | ||
| width: 90px; | ||
| background-position: 50% calc(50% + 12px); | ||
| background-color: black; | ||
| justify-content: center; | ||
| align-items: center; | ||
| ` | ||
|
|
||
| const StyledNavItem = styled(Link)<{ isActive?: boolean; fontType: FontType }>` | ||
| height: 100%; | ||
| const StyledNavItem = styled(Link)<{ | ||
| isActive?: boolean | ||
| fontType: FontType | ||
| }>` | ||
| height: auto; | ||
| width: fit-content; | ||
| display: flex; | ||
| flex-direction: column; | ||
| justify-content: center; | ||
| align-items: center; | ||
| margin-left: 2rem; | ||
| cursor: pointer; | ||
|
|
||
| // overwrite default link styles | ||
|
|
@@ -38,29 +56,92 @@ const StyledNavItem = styled(Link)<{ isActive?: boolean; fontType: FontType }>` | |
| ${fontTypeCss} | ||
| transition: font-size 0.3s; | ||
| text-transform: capitalize; | ||
| line-height: 1; | ||
| //line-height: 1; | ||
|
|
||
| ${(props) => (props.isActive ? activeCss : inactiveCss)} | ||
|
|
||
| @media (max-width: 650px) { | ||
| font-size: 12px; | ||
| } | ||
| ` | ||
| const ColumnNavItemContainer = styled(StyledNavItem)` | ||
| margin-left: 0; | ||
|
|
||
| a + a { | ||
| margin-top: 1rem; | ||
| margin-left: 0; | ||
| } | ||
| ` | ||
|
|
||
| type NavItemProps = { | ||
| text: string | ||
| href?: string | ||
| isActive?: boolean | ||
| isDropdown?: boolean | ||
| isInColumn?: boolean | ||
| } | ||
|
|
||
| const defaultProps = { | ||
| href: '', | ||
| isActive: false, | ||
| isDropdown: false, | ||
| isInColumn: false, | ||
| } | ||
|
|
||
| export default function NavItem({ text, href, isActive }: NavItemProps) { | ||
| export default function NavItem({ text, href, isActive, isDropdown, isInColumn }: NavItemProps) { | ||
| const theme = useTheme() | ||
| const [dropdown, setDropdown] = useState(false) | ||
| const { h3 } = { ...theme.typography.fontSize } | ||
| const onMouseEnter = () => { | ||
| if (window.innerWidth < 960) { | ||
| setDropdown(false) | ||
| } else { | ||
| setDropdown(true) | ||
| } | ||
| } | ||
|
|
||
| const onMouseLeave = () => { | ||
| if (window.innerWidth < 960) { | ||
| setDropdown(false) | ||
| } else { | ||
| setDropdown(false) | ||
| } | ||
| } | ||
|
Comment on lines
+103
to
+109
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. both is the same?????????? |
||
|
|
||
| if (isDropdown) { | ||
| return ( | ||
| <StyledNavItem | ||
| href={href ?? ''} | ||
| isActive={isActive} | ||
| fontType={h3} | ||
| onMouseEnter={onMouseEnter} | ||
| onMouseLeave={onMouseLeave} | ||
| > | ||
| <div>{text}</div> | ||
|
|
||
| {dropdown && ( | ||
| <DropdownDiv> | ||
| {dropdownTitles.map((item, index) => ( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice attempt to abstract the dropdown titles! should work for now
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. but think what if we want to have multiple tabs on the navbar to have different dropdown menus? and how you store information such as which URL will each dropdown option direct to |
||
| <NavItem | ||
| key={index} | ||
| text={item} | ||
| isDropdown={item.toLowerCase() === 'about us'} | ||
| href={item === 'Home' ? '/' : `/${item.toLowerCase()}`} | ||
| isInColumn={isInColumn} | ||
| /> | ||
|
Comment on lines
+125
to
+131
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm you should create a new component for dropdown item instead of just reusing the navbar NavItem component! |
||
| ))} | ||
| </DropdownDiv> | ||
| )} | ||
| </StyledNavItem> | ||
| ) | ||
| } | ||
| if (isInColumn) { | ||
| return ( | ||
| <ColumnNavItemContainer href={href ?? ''} isActive={isActive} fontType={h3}> | ||
| {text} | ||
| </ColumnNavItemContainer> | ||
| ) | ||
| } | ||
|
Comment on lines
+111
to
+144
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of putting rerturns within if else statement what you could do is:
|
||
| return ( | ||
| <StyledNavItem href={href ?? ''} isActive={isActive} fontType={h3}> | ||
| {text} | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok acceptable for now