Improve this code:
- Place the arrow to the right of the ul elements and at the same bottom line height but just if it has li elements inside of it.
- Open and close the acordion only if the ul is clicked and has li elements inside of it.
- Don´t show the arrow-icon at the end of the transtion.
Make whatever other changes to improve the code.
Give the whole code in the reply.
:root {
--black-color: #11101d;
--white-color: #fff;
--sky-color: #E4E9F7;
--light-black: #1d1b31;
}
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
}
.sidebar {
width: 200px;
height: 100vh;
background-color: #333;
position: fixed;
top: 0;
left: 0;
transition: width 0.3s;
overflow: hidden;
}
.logo_line {
display: flex;
justify-content: space-between;
align-items: center;
}
.arrow-icon {
margin-right: 10px;
position: absolute;
left: 0;
/* Modify this line */
top: 50%;
/* Add this line */
transform: translateY(-50%);
/* Add this line */
}
.menu ul li ul {
display: none;
max-height: 0;
overflow: hidden;
transition: max-height 0.3s ease-out;
}
.menu ul li:hover ul {
display: block;
}
.menu ul li ul li {
display: none;
}
.menu ul li:hover ul li {
display: block;
}
.sidebar.collapsed {
width: 30px;
}
.sidebar.collapsed .menu ul {
visibility: hidden;
}
.sidebar:not(.collapsed) .menu ul {
visibility: visible;
}
.sidebar.expanded .menu ul {
visibility: visible;
}
.logo {
height: 40px;
color: #fff;
cursor: pointer;
}
.logo img {
width: 20px;
height: 20px;
margin-right: 10px;
}
.arrow img {
width: 20px;
height: 20px;
margin-right: 10px;
}
.menu {
margin-top: 40px;
padding-left: 20px;
}
.menu ul {
list-style-type: none;
padding: 0;
margin: 0;
}
.menu ul li {
position: relative;
/* Add this line */
padding-left: 30px;
/* Add this line */
}
.menu ul li a {
color: #ffffff;
text-decoration: none;
}
.menu ul li ul {
margin-left: 20px;
display: none;
}
</style>
logo.addEventListener('click', () => {
sidebar.classList.toggle('collapsed');
});
arrow.addEventListener('click', () => {
sidebar.classList.toggle('collapsed');
});
const menuItems = document.querySelectorAll('.menu ul li');
menuItems.forEach(item => {
const arrowIcon = item.querySelector('.arrow-icon');
item.addEventListener('click', () => {
item.classList.toggle('open');
const isOpen = item.classList.contains('open');
arrowIcon.innerHTML = isOpen ? '▲' : '▼';
// Add transition effect to the opening and closing of the accordion
item.querySelector('ul').style.transition = 'max-height 0.3s ease-out';
item.querySelector('ul').style.maxHeight = isOpen ? `${item.querySelector('ul').scrollHeight}px` : '0px';
});
});
</script>