11import styled from 'styled-components'
22import { NavLink } from 'react-router-dom'
3- import { useState } from 'react' ;
3+ import { useState , useEffect } from 'react' ;
44
5- const SidebarContainer = styled . div `
6- width: 200px;
7- //background-color: rgba(0, 0, 0, 0.1);
8- padding: 16px;
5+ const SidebarContainer = styled . div < { $isExpanded : boolean } > `
6+ width: ${ props => props . $isExpanded ? '180px' : '20px' } ;
7+ padding: ${ props => props . $isExpanded ? '16px' : '1px' } ;
98 border-radius: 2px;
109 border: 1px solid #0050a1;
1110 margin-right: 5px;
11+ transition: width 0.2s ease;
1212
1313 &:focus {outline:0;}
1414`
1515
16- const NavItem = styled ( NavLink ) `
16+ const NavItem = styled ( NavLink ) < { $isExpanded : boolean } > `
1717 display: block;
18- padding: 8px;
18+ padding: ${ props => props . $isExpanded ? ' 8px' : '1px' } ;
1919 margin: 4px 0;
2020 color: #e04bff;
2121 text-decoration: none;
22+ white-space: nowrap;
23+ overflow: hidden;
24+ text-overflow: ellipsis;
2225
2326 &:hover {
27+ translate: ${ props => ! props . $isExpanded ? "''" : "-8px" } ;
2428 text-decoration: none;
2529 }
2630
2731 &:hover:after {
2832 color: #535bf2;
29- content: '< ';
33+ content: '] ';
3034 }
3135
32- &.hovered:after {
36+ &:hover:before {
3337 color: #535bf2;
34- content: '<' ;
38+ content: ${ props => ! props . $isExpanded ? "''" : "'['" } ;
3539 }
3640
37- &.active:before {
41+ &.hovered {
42+ translate: -8px;
43+
44+ }
45+
46+ &.hovered:after {
47+ color: #535bf2;
48+ content: ']';
49+ }
50+ &.hovered:before {
3851 color: #535bf2;
39- content: '> ';
52+ content: '[ ';
4053 }
4154`
4255
43-
4456export const Sidebar = ( ) => {
4557 const [ focusedIndex , setFocusedIndex ] = useState ( - 1 ) ;
58+ const [ isExpanded , setIsExpanded ] = useState ( false ) ;
59+ const [ isMobile , setIsMobile ] = useState ( window . innerWidth < 768 ) ;
60+
4661 const navItems = [
47- { to : "/" , label : "Home" } ,
62+ { to : "/" , label : "Home" } ,
4863 { to : "/accounts" , label : "Accounts" } ,
4964 { to : "/agents" , label : "Agents" } ,
5065 { to : "/transfers" , label : "Transfers" } ,
5166 { to : "/blocks" , label : "Blocks" } ,
52- { to : "/extrinsics" , label : "Extrinsics" } ,
53- { to : "/events" , label : "Events" }
67+ { to : "/extrinsics" , label : "Extrinsics" } ,
68+ { to : "/events" , label : "Events" }
5469 ] ;
55-
70+ // Add mobile detection
71+ useEffect ( ( ) => {
72+ const handleResize = ( ) => {
73+ setIsMobile ( window . innerWidth < 768 ) ;
74+ if ( window . innerWidth >= 768 ) {
75+ setIsExpanded ( true ) ;
76+ }
77+ } ;
78+
79+ window . addEventListener ( 'resize' , handleResize ) ;
80+ return ( ) => window . removeEventListener ( 'resize' , handleResize ) ;
81+ } , [ ] ) ;
82+
5683 const handleKeyDown = ( e : React . KeyboardEvent ) => {
57- if ( e . key === 'ArrowDown' ) {
58- setFocusedIndex ( prev => ( prev + 1 ) % navItems . length ) ;
59- } else if ( e . key === 'ArrowUp' ) {
60- setFocusedIndex ( prev => ( prev - 1 + navItems . length ) % navItems . length ) ;
61- } else if ( e . key === 'Enter' ) {
62- // setFocusedIndex(-1);
63- const link = document . getElementById ( `nav-item-${ focusedIndex } ` ) ;
64- link ?. click ( ) ;
65- }
84+ if ( e . key === 'ArrowDown' ) {
85+ setFocusedIndex ( prev => ( prev + 1 ) % navItems . length ) ;
86+ } else if ( e . key === 'ArrowUp' ) {
87+ setFocusedIndex ( prev => ( prev - 1 + navItems . length ) % navItems . length ) ;
88+ } else if ( e . key === 'Enter' ) {
89+ // setFocusedIndex(-1);
90+ const link = document . getElementById ( `nav-item-${ focusedIndex } ` ) ;
91+ link ?. click ( ) ;
92+ }
6693 } ;
67-
94+
6895 return (
69- < SidebarContainer onKeyDown = { handleKeyDown } tabIndex = { 0 } >
70- { navItems . map ( ( item , index ) => (
71- < NavItem
72- key = { item . to }
73- to = { item . to }
74- id = { `nav-item-${ index } ` }
75- className = { focusedIndex === index ? `hovered` : '' }
76- >
77- { item . label }
78- </ NavItem >
79- ) ) }
80- </ SidebarContainer >
96+ < SidebarContainer
97+ onKeyDown = { handleKeyDown }
98+ tabIndex = { 0 }
99+ $isExpanded = { isExpanded || ! isMobile }
100+ onClick = { ( ) => isMobile && setIsExpanded ( ! isExpanded ) }
101+ >
102+ { navItems . map ( ( item , index ) => (
103+ < NavItem
104+ key = { item . to }
105+ to = { item . to }
106+ id = { `nav-item-${ index } ` }
107+ className = { focusedIndex === index ? `hovered` : '' }
108+ $isExpanded = { isExpanded || ! isMobile }
109+ >
110+ { isMobile && ! isExpanded ? item . label [ 0 ] : item . label }
111+ </ NavItem >
112+ ) ) }
113+ </ SidebarContainer >
81114 ) ;
82- }
115+ }
0 commit comments