11import PropTypes from "prop-types" ;
22import { Component } from "react" ;
33
4- import "./Dropdown.scss" ;
5-
64export default class Dropdown extends Component {
75 static propTypes = {
86 className : PropTypes . string ,
@@ -14,56 +12,53 @@ export default class Dropdown extends Component {
1412 } ;
1513
1614 componentDidMount ( ) {
17- this . _boundCloseOnEsc = this . _closeDropdownOnEsc . bind ( this ) ;
18- this . _boundCloseLostFocus = this . _closeDropdownIfFocusLost . bind ( this ) ;
19-
20- document . addEventListener ( "keyup" , this . _boundCloseOnEsc , true ) ;
21- document . addEventListener ( "focus" , this . _boundCloseLostFocus , true ) ;
22- document . addEventListener ( "click" , this . _boundCloseLostFocus , true ) ;
15+ document . addEventListener ( "keyup" , this . _closeDropdownOnEsc , true ) ;
16+ document . addEventListener ( "focus" , this . _closeDropdownIfFocusLost , true ) ;
17+ document . addEventListener ( "click" , this . _closeDropdownIfFocusLost , true ) ;
2318 }
2419
2520 componentWillUnmount ( ) {
26- document . removeEventListener ( "keyup" , this . _boundCloseOnEsc , true ) ;
27- document . removeEventListener ( "focus" , this . _boundCloseLostFocus , true ) ;
28- document . removeEventListener ( "click" , this . _boundCloseLostFocus , true ) ;
21+ document . removeEventListener ( "keyup" , this . _closeDropdownOnEsc , true ) ;
22+ document . removeEventListener ( "focus" , this . _closeDropdownIfFocusLost , true ) ;
23+ document . removeEventListener ( "click" , this . _closeDropdownIfFocusLost , true ) ;
2924 }
3025
31- _closeDropdownOnEsc ( event ) {
26+ _closeDropdownOnEsc = ( event ) => {
3227 if ( event . key === "Escape" && this . state . active ) {
3328 this . setState ( { active : false } , ( ) => {
3429 this . dropdownButton . focus ( ) ;
3530 } ) ;
3631 }
37- }
32+ } ;
3833
39- _closeDropdownIfFocusLost ( event ) {
34+ _closeDropdownIfFocusLost = ( event ) => {
4035 if ( this . state . active && ! this . dropdown . contains ( event . target ) ) {
4136 this . setState ( { active : false } ) ;
4237 }
43- }
38+ } ;
4439
4540 render ( ) {
4641 const { className = "" , items = [ ] } = this . props ;
47- const activeMod = this . state . active ? "dropdown__list--active" : "" ;
4842
4943 return (
5044 < nav
51- className = { `dropdown ${ className } ` }
45+ className = { `relative ${ className } ` }
5246 ref = { ( el ) => ( this . dropdown = el ) }
53- onMouseOver = { this . _toggle . bind ( this , true ) }
54- onMouseLeave = { this . _toggle . bind ( this , false ) }
47+ onMouseOver = { this . _handleMouseOver }
48+ onMouseLeave = { this . _handleMouseLeave }
5549 >
5650 < button
5751 ref = { ( el ) => ( this . dropdownButton = el ) }
5852 aria-haspopup = "true"
5953 aria-expanded = { String ( this . state . active ) }
6054 aria-label = "Select language"
61- onClick = { this . _handleClick . bind ( this ) }
55+ onClick = { this . _handleClick }
56+ className = "cursor-pointer text-white border-none bg-transparent m-0 p-0 text-[length:inherit] flex items-center"
6257 >
6358 < svg
6459 viewBox = "0 0 610 560"
6560 xmlns = "http://www.w3.org/2000/svg"
66- className = "dropdown__language text-gray-100 hover:text-blue-200 transition-colors duration-200"
61+ className = "w-20 h-20 align-middle text-gray-100 hover:text-blue-200 transition-colors duration-200"
6762 >
6863 < path d = "m304.8 99.2-162.5-57.3v353.6l162.5-52.6z" />
6964 < path
@@ -91,25 +86,32 @@ export default class Dropdown extends Component {
9186 </ svg >
9287 { /* Commented out until media breakpoints are in place
9388 <span>{ items[0].title }</span> */ }
94- < i aria-hidden = "true" className = "dropdown__arrow " />
89+ < i aria-hidden = "true" className = "leading-none before:content-['▾'] " />
9590 </ button >
96- < div className = { `dropdown__list ${ activeMod } ` } >
97- < ul >
91+ < div
92+ className = { `${
93+ this . state . active ? "block" : "hidden"
94+ } absolute top-full right-0 text-[13px] bg-[#526b78] rounded-md shadow-[0_4px_12px_rgba(0,0,0,0.4)] z-[9999]`}
95+ >
96+ < ul className = "pt-1" >
9897 { items . map ( ( item , i ) => (
99- < li key = { item . title } >
98+ < li
99+ key = { item . title }
100+ className = "py-1 px-2 list-none text-white transition-all duration-[250ms] hover:bg-[#175d96]"
101+ >
100102 < a
101- onKeyDown = { this . _handleArrowKeys . bind (
102- this ,
103- i ,
104- items . length - 1 ,
105- ) }
103+ onKeyDown = { ( event ) =>
104+ this . _handleArrowKeys ( i , items . length - 1 , event )
105+ }
106106 ref = { ( node ) =>
107107 this . links ? this . links . push ( node ) : ( this . links = [ node ] )
108108 }
109109 href = { item . url }
110- className = "px-5 block"
110+ className = "px-5 block text-white visited:text-white hover:text-white "
111111 >
112- < span lang = { item . lang } > { item . title } </ span >
112+ < span lang = { item . lang } className = "align-top text-left" >
113+ { item . title }
114+ </ span >
113115 </ a >
114116 </ li >
115117 ) ) }
@@ -119,7 +121,7 @@ export default class Dropdown extends Component {
119121 ) ;
120122 }
121123
122- _handleArrowKeys ( currentIndex , lastIndex , event ) {
124+ _handleArrowKeys = ( currentIndex , lastIndex , event ) => {
123125 if ( [ "ArrowDown" , "ArrowUp" ] . includes ( event . key ) ) {
124126 event . preventDefault ( ) ;
125127 }
@@ -140,24 +142,21 @@ export default class Dropdown extends Component {
140142 }
141143
142144 this . links [ newIndex ] . focus ( ) ;
143- }
145+ } ;
144146
145- _handleClick ( ) {
147+ _handleClick = ( ) => {
146148 this . setState ( { active : ! this . state . active } , ( ) => {
147149 if ( this . state . active ) {
148150 this . links [ 0 ] . focus ( ) ;
149151 }
150152 } ) ;
151- }
153+ } ;
152154
153- /**
154- * Toggle visibility of dropdown items
155- *
156- * @param {boolean } state - Whether to display or hide the items
157- */
158- _toggle ( state = false ) {
159- this . setState ( {
160- active : state ,
161- } ) ;
162- }
155+ _handleMouseOver = ( ) => {
156+ this . setState ( { active : true } ) ;
157+ } ;
158+
159+ _handleMouseLeave = ( ) => {
160+ this . setState ( { active : false } ) ;
161+ } ;
163162}
0 commit comments