---
import site from '@src/site.js' ;
interface Props {
name ?: string ;
showPhoneNumber ?: boolean ;
}
const { name = 'main' , showPhoneNumber = false } = Astro.props;
const menu = site.menus[name];
---
< div x-data = "{ isOpen: false }" class = "md:hidden" >
< button
@click = "isOpen = !isOpen"
: data-state = "isOpen ? 'open' : 'closed'"
class = "hamburger"
>
< span class = "bar" ></ span >
< span class = "bar" ></ span >
< span class = "bar" ></ span >
</ button >
< nav
class = "bg-white shadow-2xl flex flex-col fixed left-0 top-0 h-screen w-64 z-100 py-10 transform -translate-x-80 transition-transform duration-500 will-change-transform"
: class = "{ 'translate-x-0': isOpen, '-translate-x-80': !isOpen }"
>
< ul class = "text-lg" >
{
menu. map (( link , key ) => {
return (
< li >
< a
class = "block text-gray-700 font-bold py-4 px-8 no-underline"
href = {link.href}
>
{link.label}
</ a >
</ li >
);
})
}
</ ul >
{
showPhoneNumber && (
< div class = "mt-auto py-4 px-8" >
< p class = "uppercase text-gray-600 tracking-wide text-sm" >
Call Us
</ p >
< a
class = "text-xl font-bold tracking-tight no-underline"
href = { `tel:${ site . phone }` }
>
{site.phone}
</ a >
</ div >
)
}
</ nav >
</ div >
< style >
.hamburger {
position : relative ;
z-index : 9 ;
}
.hamburger .bar {
width : 35 px ;
height : 3 px ;
background-color : currentColor ;
margin : 6 px 0 ;
transition : 0.4 s ;
display : block ;
}
.hamburger [ data-state = 'open' ] .bar:nth-child ( 1 ) {
-webkit-transform : rotate ( -45 deg ) translate ( -4 px , 6 px );
transform : rotate ( -45 deg ) translate ( -4 px , 6 px );
}
.hamburger [ data-state = 'open' ] .bar:nth-child ( 2 ) {
opacity : 0 ;
}
.hamburger [ data-state = 'open' ] .bar:nth-child ( 3 ) {
-webkit-transform : rotate ( 45 deg ) translate ( -8 px , -8 px );
transform : rotate ( 45 deg ) translate ( -8 px , -8 px );
}
</ style >
The mobile menu is hidden at the md breakpoint. Resize your browser to see the preview.
This component is available with our Astro Starter Kit.
Usage
Before you use this component, be sure that you’ve added the menu to src/site.js in the following format:
export default {
... otherConfig,
/**
* Menus
*/
menus: {
main: [
{
label: 'Home' ,
href: '/' ,
},
{
label: 'About' ,
href: '/about' ,
},
{
label: 'Contact' ,
href: '/contact' ,
},
],
}
};
If you don’t specify a menu name, the component will look for a menu named main.
Now you can import the component into the page or component you need it and use it!
---
import MobileMenu from '@components/MobileMenu.astro' ;
---
< MobileMenu />
Props
name - The name of the menu to display. Defaults to main.
showPhoneNumber - Whether or not to show the phone number. Defaults to false.