Last active
July 12, 2018 21:06
-
-
Save Qt-dev/556e2e0558892c99bd81 to your computer and use it in GitHub Desktop.
ReactJS - How to make a simple navbar menu
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var data = [ | |
{ | |
"text": "Link 1", | |
"url": "#" | |
}, | |
{ | |
"text": "Link 2", | |
"url": "#" | |
}, | |
{ | |
"text": "Link 3", | |
"url": "#", | |
"submenu": [ | |
{ | |
"text": "Sublink 1", | |
"url": "#", | |
"submenu": [ | |
{ | |
"text": "SubSublink 1", | |
"url": "#" | |
} | |
] | |
}, | |
{ | |
"text": "Sublink 2", | |
"url":"#", | |
"submenu": [ | |
{ | |
"text": "SubSublink 2", | |
"url": "#" | |
} | |
] | |
} | |
] | |
} | |
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title></title> | |
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/react/0.11.0/react.min.js"></script> | |
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/react/0.11.0/JSXTransformer.js"></script> | |
<script type="text/jsx" src="navbarLink.jsx"></script> | |
<script type="text/jsx" src="navbarItem.jsx"></script> | |
<script type="text/jsx" src="navbar.jsx"></script> | |
<script type="text/javascript" src="data.js"></script> | |
<link rel="stylesheet" type="text/css" href="style.css"> | |
</head> | |
<body> | |
<nav></nav> | |
<script type="text/jsx"> | |
/** @jsx React.DOM */ | |
window.onload = React.renderComponent( | |
<NavBar items={data} />, | |
document.querySelector('nav') | |
); | |
</script> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.menu { | |
list-style: none; | |
/* Important for sizing problems when you add/remove the submenus | |
Make it use 100% of the size of your container */ | |
width: 100%; | |
/* This prevents a lot of problems with the submenus. | |
UL have basic padding, keep that in mind */ | |
padding: 0; | |
} | |
.menu li { | |
/* I wanted mine horizontal. Feel free to change it to what you want */ | |
display:inline-block; | |
/* Float the items to the left, to make sure the disposition does not change when it shows the subitems */ | |
float:left; | |
/* The -1px on top and right make it so that our borders end up above one another | |
Not needed it you don't have borders... */ | |
margin:-1px -1px 0 0; | |
} | |
.menu li a { | |
display:block; | |
/* My basic styling. I'm just giving them a black text, with a black border, and center the text in the box. */ | |
text-decoration:none; | |
border: 1px solid black; | |
vertical-align: middle; | |
text-align: center; | |
/* Fixed size is also just me. | |
Not fixing it just make huge boxes when you have submenus */ | |
width:100px; | |
height:50px; | |
line-height: 50px; | |
} | |
.menu li > ul { | |
/****** THE INTERESTING PART ******/ | |
/* We hide the submenus by putting them somewhere on the left, with no opacity. */ | |
left:-9999px; | |
opacity: 0; | |
/* We make transitions on opacity */ | |
-webkit-transiton: opacity 0.3s; | |
-moz-transition: opacity 0.3s; | |
-ms-transition: opacity 0.3s; | |
-o-transition: opacity 0.3s; | |
transition: opacity 0.3s; | |
} | |
.menu li:hover > ul { | |
/* On hover, we get it back to its basic spot | |
left:0; | |
/* And change the opacity to a fully visible one */ | |
opacity: 1; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
excellent code