Explore / Programming add the nav items into a cool animated box with awesome ui "import React, { useState,… Save Share Download ▾ Reportadd the nav items into a cool animated box with awesome ui "import React, { useState, useEffect, useRef } from 'react';
import { Helmet } from 'react-helmet';
import { Link } from 'gatsby';
import styled from 'styled-components';
import { navLinks } from '@config';
import { KEY_CODES } from '@utils';
import { useOnClickOutside } from '@hooks';
const StyledMenu = styled.div`
display: none;
@media (max-width: 768px) {
display: block;
}
`;
const StyledHamburgerButton = styled.button`
display: none;
@media (max-width: 768px) {
${({ theme }) => theme.mixins.flexCenter};
position: relative;
z-index: 10;
margin-right: -15px;
padding: 15px;
border: 0;
background-color: transparent;
color: inherit;
text-transform: none;
transition-timing-function: linear;
transition-duration: 0.15s;
transition-property: opacity, filter;
}
.ham-box {
display: inline-block;
position: relative;
width: var(--hamburger-width);
height: 24px;
}
.ham-box-inner {
position: absolute;
top: 50%;
right: 0;
width: var(--hamburger-width);
height: 2px;
border-radius: var(--border-radius);
background-color: var(--green);
transition-duration: 0.22s;
transition-property: transform;
transition-delay: p r o p s = > ( p r o p s . m e n u O p e n ? ‘ 0.12 s ‘ : ‘ 0 s ‘ ) ; t r a n s f o r m : r o t a t e ( {props => (props.menuOpen ? `0.12s` : `0s`)};
transform: rotate( p r {props => (props.menuOpen ? : )});
transition-timing-function: cubic-bezier(
{props => (props.menuOpen ? : )};
top: {props => (props.menuOpen ? 0 : 1)};
transition: {props => (props.menuOpen ? : )};
bottom: {props => (props.menuOpen ? : )});
transition: ${({ menuOpen }) => (menuOpen ? 'var(--ham-after-active)' : 'var(--ham-after)')};
}
}
`;
Show full message · 280 lines Assistant2024-11-12 18:37 UTC Copy response
Copy first prompt
Details
Provider OpenAI
Model provenance As recorded by source dataset
Turns 1 (2 messages)
Language en
Conversation date Nov 12, 2024
Added to archive Nov 17, 2026
ID 01m7h427xwqm4sn2yx7x8bhyepSource & license Imported from WildChat-4.8M (AI2) (source ). Imported records earn no Research Points.
Upstream record: b0921dd1dddf7ed458e629b607edef78
Zhao et al., WildChat: 1M ChatGPT Interaction Logs in the Wild (ICLR 2024). Contains information from WildChat-4.8M, made available under the ODC Attribution License.
License: ODC-BY-1.0
o
p
s
=>
(
p
r
o
p
s
.
m
e
n
u
O
p
e
n
?
‘0.12
s
‘
:
‘0
s
‘
)
;
t
r
an
s
f
or
m
:
r o t a t e (
225deg
0deg
{props => (props.menuOpen ? `0.215, 0.61, 0.355, 1` : `0.55, 0.055, 0.675, 0.19`)}
);
&:before,
&:after {
content: '';
display: block;
position: absolute;
left: auto;
right: 0;
width: var(--hamburger-width);
height: 2px;
border-radius: 4px;
background-color: var(--green);
transition-timing-function: ease;
transition-duration: 0.15s;
transition-property: transform;
}
&:before {
width:
100%
120%
p r o p s = > ( p r o p s . m e n u O p e n ? ‘ 0 ‘ : ‘ − 10 p x ‘ ) ; o p a c i t y : {props => (props.menuOpen ? `0` : `-10px`)};
opacity: p r o p s => ( p r o p s . m e n u O p e n ? ‘0‘ : ‘ − 10 p x ‘ ) ; o p a c i t y : {({ menuOpen }) =>
menuOpen ? 'var(--ham-before-active)' : 'var(--ham-before)'};
}
&:after {
width:
100%
80%
p r o p s = > ( p r o p s . m e n u O p e n ? ‘ 0 ‘ : ‘ − 10 p x ‘ ) ; t r a n s f o r m : r o t a t e ( {props => (props.menuOpen ? `0` : `-10px`)};
transform: rotate( p r o p s => ( p r o p s . m e n u O p e n ? ‘0‘ : ‘ − 10 p x ‘ ) ; t r an s f or m : r o t a t e ( -90deg
0
const StyledSidebar = styled.aside`
display: none;
@media (max-width: 768px) {
( t h e m e ) = > t h e m e . m i x i n s . f l e x C e n t e r ; p o s i t i o n : f i x e d ; t o p : 0 ; b o t t o m : 0 ; r i g h t : 0 ; p a d d i n g : 50 p x 10 p x ; w i d t h : m i n ( 75 v w , 400 p x ) ; h e i g h t : 100 v h ; o u t l i n e : 0 ; b a c k g r o u n d − c o l o r : v a r ( − − l i g h t − n a v y ) ; b o x − s h a d o w : − 10 p x 0 p x 30 p x − 15 p x v a r ( − − n a v y − s h a d o w ) ; z − i n d e x : 9 ; t r a n s f o r m : t r a n s l a t e X ( {({ theme }) => theme.mixins.flexCenter};
position: fixed;
top: 0;
bottom: 0;
right: 0;
padding: 50px 10px;
width: min(75vw, 400px);
height: 100vh;
outline: 0;
background-color: var(--light-navy);
box-shadow: -10px 0px 30px -15px var(--navy-shadow);
z-index: 9;
transform: translateX( ( t h e m e ) => t h e m e . mi x in s . f l e x C e n t er ; p os i t i o n : f i x e d ; t o p : {props => (props.menuOpen ? 0 : 100)}vw);
visibility: ${props => (props.menuOpen ? 'visible' : 'hidden')};
transition: var(--transition);
}
nav {
${({ theme }) => theme.mixins.flexBetween};
width: 100%;
flex-direction: column;
color: var(--lightest-slate);
font-family: var(--font-mono);
text-align: center;
}
ol {
padding: 0;
margin: 0;
list-style: none;
width: 100%;
code Copy code
li {
position: relative;
margin: 0 auto 20px;
counter-increment: item 1;
font-size: clamp(var(--fz-sm), 4vw, var(--fz-lg));
@media (max-width: 600px) {
margin: 0 auto 10px;
}
&:before {
content:;
display: block;
margin-bottom: 5px;
color: var(--green);
font-size: var(--fz-sm);
}
}
a {
${({ theme }) => theme.mixins.link};
width: 100%;
padding: 3px 20px 20px;
}
.resume-link {
${({ theme }) => theme.mixins.bigButton};
padding: 18px 50px;
margin: 10% auto 0;
width: max-content;
}
`;
const Menu = () => {
const [menuOpen, setMenuOpen] = useState(false);
const toggleMenu = () => setMenuOpen(!menuOpen);
const buttonRef = useRef(null);
const navRef = useRef(null);
let menuFocusables;
let firstFocusableEl;
let lastFocusableEl;
const setFocusables = () => {
menuFocusables = [buttonRef.current, ...Array.from(navRef.current.querySelectorAll('a'))];
firstFocusableEl = menuFocusables[0];
lastFocusableEl = menuFocusables[menuFocusables.length - 1];
};
const handleBackwardTab = e => {
if (document.activeElement === firstFocusableEl) {
e.preventDefault();
lastFocusableEl.focus();
}
};
const handleForwardTab = e => {
if (document.activeElement === lastFocusableEl) {
e.preventDefault();
firstFocusableEl.focus();
}
};
const onKeyDown = e => {
switch (e.key) {
case KEY_CODES.ESCAPE:
case KEY_CODES.ESCAPE_IE11: {
setMenuOpen(false);
break;
}
code Copy code
case KEY_CODES.TAB: {
if (menuFocusables && menuFocusables.length === 1) {
e.preventDefault();
break;
}
if (e.shiftKey) {
handleBackwardTab(e);
} else {
handleForwardTab(e);
}
break;
}
default: {
break;
}
}
const onResize = e => {
if (e.currentTarget.innerWidth > 768) {
setMenuOpen(false);
}
};
useEffect(() => {
document.addEventListener('keydown', onKeyDown);
window.addEventListener('resize', onResize);
code Copy code
setFocusables();
return () => {
document.removeEventListener('keydown', onKeyDown);
window.removeEventListener('resize', onResize);
};
const wrapperRef = useRef();
useOnClickOutside(wrapperRef, () => setMenuOpen(false));
return (
<body className={menuOpen ? 'blur' : ''} />
code Copy code
<div ref={wrapperRef}>
<StyledHamburgerButton
onClick={toggleMenu}
menuOpen={menuOpen}
ref={buttonRef}
aria-label="Menu">
<div className="ham-box">
<div className="ham-box-inner" />
</div>
</StyledHamburgerButton>
<StyledSidebar menuOpen={menuOpen} aria-hidden={!menuOpen} tabIndex={menuOpen ? 1 : -1}>
<nav ref={navRef}>
{navLinks && (
<ol>
{navLinks.map(({ url, name }, i) => (
<li key={i}>
<Link to={url} onClick={() => setMenuOpen(false)}>
{name}
</Link>
</li>
))}
</ol>
)}
<a href="https://github.com/ai-apex-dev" className="resume-link">
Resume
</a>
</nav>
</StyledSidebar>
</div>
</StyledMenu>
Absolutely! Let's enhance your navigation menu with a more dynamic and visually appealing animated sidebar. We'll introduce smooth slide-in animations, staggered fade-in effects for the navigation links, and sleek hover effects to elevate the UI experience.
Here's the updated code with detailed explanations:
jsx Copy code
import React, { useState, useEffect, useRef } from 'react';
import { Helmet } from 'react-helmet';
import { Link } from 'gatsby';
import styled, { keyframes } from 'styled-components';
import { navLinks } from '@config';
import { KEY_CODES } from '@utils';
import { useOnClickOutside } from '@hooks';
// Keyframes for sidebar slide-in animation
const slideIn = keyframes`
from {
transform: translateX(100%);
opacity: 0;
}
to {
transform: translateX(0);
opacity: 1;
}
`;
// Keyframes for staggered fade-in of nav items
const fadeIn = keyframes`
from {
opacity: 0;
transform: translateY(20px);
}
to {
opacity: 1;
transform: translateY(0);
}
`;
// Styled component for the hamburger menu
const StyledMenu = styled.div`
display: none;
@media (max-width: 768px) {
display: block;
}
`;
// Styled component for the hamburger button with enhanced animation
const StyledHamburgerButton = styled.button`
display: none;
@media (max-width: 768px) {
${({ theme }) => theme.mixins.flexCenter};
position: relative;
z-index: 20;
margin-right: -15px;
padding: 15px;
border: none;
background: transparent;
cursor: pointer;
transition: transform 0.3s ease;
&:hover {
transform: scale(1.1);
}
}
.ham-box {
display: inline-block;
position: relative;
width: var(--hamburger-width);
height: 24px;
}
.ham-box-inner {
position: absolute;
top: 50%;
right: 0;
width: var(--hamburger-width);
height: 2px;
border-radius: var(--border-radius);
background-color: var(--green);
transition: transform 0.3s ease, background-color 0.3s ease;
transform: rotate(${props => (props.menuOpen ? '225deg' : '0deg')});
background-color: ${props => (props.menuOpen ? 'var(--orange)' : 'var(--green)')};
&:before,
&:after {
content: '';
display: block;
position: absolute;
left: 0;
width: var(--hamburger-width);
height: 2px;
border-radius: var(--border-radius);
background-color: var(--green);
transition: transform 0.3s ease, opacity 0.3s ease, background-color 0.3s ease;
}
&:before {
top: ${props => (props.menuOpen ? '0' : '-8px')};
transform: rotate(${props => (props.menuOpen ? '90deg' : '0deg')});
opacity: ${props => (props.menuOpen ? '0' : '1')};
background-color: ${props => (props.menuOpen ? 'var(--orange)' : 'var(--green)')};
}
&:after {
top: ${props => (props.menuOpen ? '0' : '8px')};
transform: rotate(${props => (props.menuOpen ? '-90deg' : '0deg')};
background-color: ${props => (props.menuOpen ? 'var(--orange)' : 'var(--green)')};
}
}
`;
// Styled component for the animated sidebar
const StyledSidebar = styled.aside`
display: none;
@media (max-width: 768px) {
${({ theme }) => theme.mixins.flexCenter};
position: fixed;
top: 0;
bottom: 0;
right: 0;
padding: 50px 20px;
width: min(80vw, 400px);
height: 100vh;
background-color: var(--dark-navy);
box-shadow: -5px 0px 25px rgba(0, 0, 0, 0.5);
z-index: 15;
transform: translateX(${props => (props.menuOpen ? '0' : '100%')});
visibility: ${props => (props.menuOpen ? 'visible' : 'hidden')};
opacity: ${props => (props.menuOpen ? '1' : '0')};
transition: transform 0.4s ease, opacity 0.4s ease, visibility 0.4s;
animation: ${props => (props.menuOpen ? slideIn : 'none')} 0.4s forwards;
nav {
width: 100%;
display: flex;
flex-direction: column;
align-items: center;
color: var(--light-slate);
font-family: var(--font-mono);
text-align: center;
}
ol {
padding: 0;
margin: 0;
list-style: none;
width: 100%;
li {
margin: 15px 0;
font-size: clamp(1rem, 2vw, 1.5rem);
opacity: 0;
transform: translateY(20px);
animation: ${fadeIn} 0.5s forwards;
animation-delay: ${props => props.index * 0.1 + 0.3}s; /* Staggered animation */
@media (max-width: 600px) {
margin: 10px 0;
}
a {
position: relative;
display: inline-block;
color: var(--light-slate);
text-decoration: none;
transition: color 0.3s ease;
&:hover,
&:focus {
color: var(--green);
}
&:after {
content: '';
position: absolute;
width: 0%;
height: 2px;
bottom: -5px;
left: 0;
background-color: var(--green);
transition: width 0.3s ease;
}
&:hover:after,
&:focus:after {
width: 100%;
}
}
}
}
.resume-link {
${({ theme }) => theme.mixins.bigButton};
padding: 15px 40px;
margin-top: 30px;
background-color: var(--green);
color: var(--light-navy);
border-radius: var(--border-radius);
transition: background-color 0.3s ease, transform 0.3s ease;
&:hover,
&:focus {
background-color: var(--light-green);
transform: translateY(-3px);
}
}
}
`;
const Menu = () => {
const [menuOpen, setMenuOpen] = useState(false);
const toggleMenu = () => setMenuOpen(prev => !prev);
const buttonRef = useRef(null);
const navRef = useRef(null);
let menuFocusables;
let firstFocusableEl;
let lastFocusableEl;
const setFocusables = () => {
menuFocusables = [buttonRef.current, ...Array.from(navRef.current.querySelectorAll('a'))];
firstFocusableEl = menuFocusables[0];
lastFocusableEl = menuFocusables[menuFocusables.length - 1];
};
const handleBackwardTab = e => {
if (document.activeElement === firstFocusableEl) {
e.preventDefault();
lastFocusableEl.focus();
}
};
const handleForwardTab = e => {
if (document.activeElement === lastFocusableEl) {
e.preventDefault();
firstFocusableEl.focus();
}
};
const onKeyDown = e => {
switch (e.key) {
case KEY_CODES.ESCAPE:
case KEY_CODES.ESCAPE_IE11: {
setMenuOpen(false);
break;
}
case KEY_CODES.TAB: {
if (menuFocusables && menuFocusables.length === 1) {
e.preventDefault();
break;
}
if (e.shiftKey) {
handleBackwardTab(e);
} else {
handleForwardTab(e);
}
break;
}
default: {
break;
}
}
};
const onResize = e => {
if (e.currentTarget.innerWidth > 768) {
setMenuOpen(false);
}
};
useEffect(() => {
document.addEventListener('keydown', onKeyDown);
window.addEventListener('resize', onResize);
setFocusables();
return () => {
document.removeEventListener('keydown', onKeyDown);
window.removeEventListener('resize', onResize);
};
}, []);
const wrapperRef = useRef();
useOnClickOutside(wrapperRef, () => setMenuOpen(false));
return (
<StyledMenu>
<Helmet>
<body className={menuOpen ? 'blur' : ''} />
</Helmet>
<div ref={wrapperRef}>
<StyledHamburgerButton
onClick={toggleMenu}
menuOpen={menuOpen}
ref={buttonRef}
aria-label="Menu"
aria-expanded={menuOpen}
aria-controls="navigation"
>
<div className="ham-box">
<div className="ham-box-inner" />
</div>
</StyledHamburgerButton>
<StyledSidebar menuOpen={menuOpen}>
<nav id="navigation" ref={navRef} aria-hidden={!menuOpen}>
{navLinks && (
<ol>
{navLinks.map(({ url, name }, i) => (
<li key={i} style={{ animationDelay: `${i * 0.1 + 0.3}s` }}>
<Link to={url} onClick={() => setMenuOpen(false)}>
{name}
</Link>
</li>
))}
</ol>
)}
<a href="https://github.com/ai-apex-dev" className="resume-link" target="_blank" rel="noopener noreferrer">
Resume
</a>
</nav>
</StyledSidebar>
</div>
</StyledMenu>
);
};
export default Menu;
Enhancements Breakdown
Sidebar Slide-In Animation:
Keyframes slideIn: Defines the sliding animation from right to left with an opacity transition.
StyledSidebar: Applies the slideIn animation when menuOpen is true. The sidebar smoothly transitions into view with both translation and opacity changes.
Staggered Fade-In for Navigation Items:
Keyframes fadeIn: Each navigation link will fade in and move slightly upward for a dynamic entrance.
ol li: Each list item (li) uses the fadeIn animation with a staggered delay based on its index, creating a cascading effect.
Inline Style for animationDelay: Ensures that each nav item starts its animation slightly after the previous one.
Enhanced Hamburger Button Animation:
Transformations & Color Transitions: The hamburger icon transitions into a cross (X) when the menu is open, with color changes to indicate active state.
Hover Effect: Slight scaling on hover to provide feedback.
Sleek Hover Effects on Links:
Underline Animation: Links have an underline that animates from left to right on hover, providing a smooth and modern visual cue.
Color Transitions: Link colors transition on hover for better interactivity.
Resume Button Styling:
Button Styling: The resume link is styled as a prominent button with hover effects that change the background color and slightly elevate the button, making it stand out.
Accessibility Enhancements:
ARIA Attributes: Added aria-expanded and aria-controls to the hamburger button for better accessibility.
Focus Management: Ensured focusable elements are handled correctly for keyboard navigation.
Additional Recommendations
CSS Variables: Ensure that the following CSS variables are defined in your global styles for consistent theming:
css Copy code
:root {
--hamburger-width: 24px;
--border-radius: 4px;
--green: #28a745;
--orange: #fd7e14;
--dark-navy: #001f3f;
--light-navy: #007bff;
--light-slate: #c0c0c0;
--light-green: #28a745cc;
--font-mono: 'Courier New', Courier, monospace;
--fz-sm: 0.875rem;
--fz-lg: 1.25rem;
}
Responsive Adjustments: Feel free to tweak the media queries and animation durations to better suit your design preferences and ensure optimal performance across devices.
Animation Performance: For smoother animations, especially on mobile devices, consider using transform and opacity properties, which are GPU-accelerated, ensuring better performance.
By implementing these enhancements, your navigation menu will provide a more engaging and user-friendly experience with polished animations and a modern UI design.
Show full message · 389 lines Copy first response
Not yet included in a published release.
0
;
b
o
tt
o
m
:
0 ; r i g h t :
0 ; p a dd in g :
50 p x 10 p x ; w i d t h :
min ( 75 v w , 400 p x ) ; h e i g h t :
100 v h ; o u tl in e :
0 ; ba c k g r o u n d −
co l or :
v a r ( − −
l i g h t −
na v y ) ; b o x −
s ha d o w :
− 10 p x 0 p x 30 p x −
15 p xv a r ( − −
na v y −
s ha d o w ) ; z −
in d e x :
9 ; t r an s f or m :
t r an s l a t e X (