Skip to content

Commit ba8e9a9

Browse files
committed
topcoder-platform#95 New Nav: When the screen is resized, second level selection moves
1 parent f284bd4 commit ba8e9a9

File tree

3 files changed

+46
-13
lines changed

3 files changed

+46
-13
lines changed

src/components/IconSelect/index.js

+15-4
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,26 @@ import React from 'react'
22
import PropTypes from 'prop-types'
33
import styles from './styles.module.scss'
44

5-
const IconSelect = ({ show, x }) => (
6-
<span
5+
const IconSelect = ({ show, x, isResize }) => {
6+
let styleObj = {
7+
transform: `translateX(calc(${x}px - 50%))`
8+
9+
}
10+
// is window is on resize, stop animation
11+
if (isResize) {
12+
styleObj['transition'] = 'auto'
13+
styleObj['-webkit-transition'] = 'auto'
14+
}
15+
return <span
716
className={styles.iconSelect}
8-
style={{ transform: `translateX(calc(${x}px - 50%))` }}
17+
style={styleObj}
918
hidden={!show}
1019
/>
11-
)
20+
}
1221

1322
IconSelect.propTypes = {
23+
// screen is changing size
24+
isResize: PropTypes.bool,
1425
/** Show or hide the icon */
1526
show: PropTypes.bool,
1627
/** The x position of the arrow. Generally this will be the center of the target */

src/components/TopNav/SubNav.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const SubNav = ({
1010
open,
1111
menu,
1212
isSecondaryMenu,
13+
isResize,
1314
activeChildId,
1415
showIndicator,
1516
indicatorX,
@@ -32,14 +33,14 @@ const SubNav = ({
3233
</Link>
3334
)
3435
})}
35-
<IconSelect show={showIndicator} x={indicatorX} />
36-
</div>
36+
<IconSelect isResize={isResize} show={showIndicator} x={indicatorX} /> </div>
3737
</div>
3838
)
3939

4040
SubNav.propTypes = {
4141
open: PropTypes.bool,
4242
menu: PropTypes.object,
43+
isResize: PropTypes.bool,
4344
isSecondaryMenu: PropTypes.bool,
4445
activeChildId: PropTypes.any,
4546
showIndicator: PropTypes.bool,

src/components/TopNav/index.js

+28-7
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ const TopNav = ({
7070
const [activeLevel1Id, setActiveLevel1Id] = useState()
7171
const [activeLevel2Id, setActiveLevel2Id] = useState()
7272
const [activeLevel3Id, setActiveLevel3Id] = useState()
73+
const [isResize, setResize] = useState(false)
7374
const [showLevel3, setShowLevel3] = useState(false)
7475
const [forceHideLevel3, setforceHideLevel3] = useState(false)
7576

@@ -334,7 +335,7 @@ const TopNav = ({
334335
const onResize = _.debounce(() => {
335336
regenerateMoreMenu([])
336337
// tick to update menu (reposition arrow)
337-
setChosenArrowTick(x => x + 1)
338+
// setChosenArrowTick(x => x + 1)
338339
}, 100)
339340
window.addEventListener('resize', onResize)
340341
return () => window.removeEventListener('resize', onResize)
@@ -344,7 +345,7 @@ const TopNav = ({
344345
let found = { m1: null, m2: null, m3: null }
345346

346347
// If haven't a path just return
347-
if(!path_) return found
348+
if (!path_) return found
348349

349350
menuWithId_.forEach(level1 => {
350351
if (level1.href && path_.indexOf(level1.href) > -1) found = { m1: level1.id, m2: null }
@@ -357,21 +358,40 @@ const TopNav = ({
357358
} else {
358359
found = { m1: level1.id, m2: level2.id, m3: level3.id }
359360
}
360-
if(!activeLevel3Id && level3.collapsed) setforceHideLevel3(true)
361+
if (!activeLevel3Id && level3.collapsed) setforceHideLevel3(true)
361362
}
362363
})
363364
})
364365
level1.secondaryMenu && level1.secondaryMenu.forEach(level3 => {
365366
if (level3.href) {
366367
// Check if path have parameters
367-
const href = level3.href.indexOf("?") > -1 ? level3.href.split("?")[0] : level3.href;
368+
const href = level3.href.indexOf('?') > -1 ? level3.href.split('?')[0] : level3.href
368369
if (path_.indexOf(href) > -1) found = { m1: level1.id, m3: level3.id }
369370
}
370371
})
371372
})
372373
return found
373374
}
374375

376+
let timeId = 0
377+
useEffect(() => {
378+
// when scren change size, keep green indicator keep static
379+
const onResize = _.debounce(() => {
380+
if (timeId) { clearTimeout(timeId) }
381+
const { m3 } = getMenuIdsFromPath(menuWithId, path)
382+
activeLevel2Id && setChosenArrowPos(activeLevel2Id)
383+
setIconSelectPos(m3)
384+
setResize(true)
385+
timeId = setTimeout(() => {
386+
setResize(false)
387+
timeId = 0
388+
}, 1000)
389+
}, 50)
390+
391+
window.addEventListener('resize', onResize)
392+
return () => window.removeEventListener('resize', onResize)
393+
}, [getMenuIdsFromPath])
394+
375395
useEffect(() => {
376396
if (!path || !menuWithId[0]) return
377397
setLeftNav(menuWithId)
@@ -390,13 +410,13 @@ const TopNav = ({
390410
forceExpand = true
391411
forceM2 = getMenuIdsFromPath(menuWithId, '/challenges').m2
392412
}
393-
} else if (path.indexOf('/my-dashboard') > -1 || path.indexOf('/members/'+profileHandle) > -1) {
413+
} else if (path.indexOf('/my-dashboard') > -1 || path.indexOf('/members/' + profileHandle) > -1) {
394414
// If My Dashboard and My Profile page
395415
setShowLevel3(true)
396416
} else if (path.indexOf('/community/learn') > -1 || path.indexOf('/thrive/tracks') > -1) {
397417
// Show 3rd level menu to Community [ Overview - How It Works ]
398-
forceM2 = getMenuIdsFromPath(menuWithId, '/community').m2;
399-
} else if(!m2) {
418+
forceM2 = getMenuIdsFromPath(menuWithId, '/community').m2
419+
} else if (!m2) {
400420
setShowLevel3(false)
401421
setforceHideLevel3(true)
402422
}
@@ -459,6 +479,7 @@ const TopNav = ({
459479
<SubNav
460480
open={forceHideLevel3 ? false : showLevel3}
461481
menu={activeMenu2 || activeMenu1}
482+
isResize={isResize}
462483
isSecondaryMenu={!activeMenu2}
463484
activeChildId={activeLevel3Id}
464485
showIndicator={showIconSelect}

0 commit comments

Comments
 (0)