-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(Nav): Use RouterLink instead of custom implementation
- Loading branch information
Showing
4 changed files
with
127 additions
and
104 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import React from 'react' | ||
import PropTypes from 'prop-types' | ||
|
||
import { useI18n } from 'cozy-ui/transpiled/react' | ||
|
||
import { | ||
NavItem as UINavItem, | ||
NavIcon, | ||
NavText | ||
} from 'cozy-ui/transpiled/react/Nav' | ||
|
||
import { NavLink } from 'drive/web/modules/navigation/NavLink' | ||
|
||
const NavItem = ({ to, icon, label, rx, clickState }) => { | ||
const { t } = useI18n() | ||
|
||
return ( | ||
<UINavItem> | ||
<NavLink to={to} rx={rx} clickState={clickState}> | ||
<NavIcon icon={icon} /> | ||
<NavText>{t(`Nav.item_${label}`)}</NavText> | ||
</NavLink> | ||
</UINavItem> | ||
) | ||
} | ||
|
||
NavItem.propTypes = { | ||
to: PropTypes.string.isRequired, | ||
icon: PropTypes.string.isRequired, | ||
label: PropTypes.string.isRequired, | ||
rx: PropTypes.shape(RegExp) | ||
} | ||
|
||
export { NavItem } |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import React from 'react' | ||
import cx from 'classnames' | ||
import { useLocation } from 'react-router-dom' | ||
import PropTypes from 'prop-types' | ||
|
||
import { NavLink as UINavLink } from 'cozy-ui/transpiled/react/Nav' | ||
|
||
import { navLinkMatch } from 'drive/web/modules/navigation/helpers' | ||
|
||
/** | ||
* Like react-router NavLink but sets the lastClicked state (passed in props) | ||
* to have a faster change of active (not waiting for the route to completely | ||
* change). | ||
*/ | ||
const NavLink = ({ | ||
children, | ||
to, | ||
rx, | ||
clickState: [lastClicked, setLastClicked] | ||
}) => { | ||
const location = useLocation() | ||
const pathname = lastClicked ? lastClicked : location.pathname | ||
const isActive = navLinkMatch(rx, to, pathname) | ||
return ( | ||
<a | ||
style={{ outline: 'none' }} | ||
onClick={() => setLastClicked(to)} | ||
href={`#${to}`} | ||
className={cx( | ||
UINavLink.className, | ||
isActive ? UINavLink.activeClassName : null | ||
)} | ||
> | ||
{children} | ||
</a> | ||
) | ||
} | ||
|
||
NavLink.propTypes = { | ||
children: PropTypes.node.isRequired, | ||
to: PropTypes.string.isRequired, | ||
rx: PropTypes.shape(RegExp) | ||
} | ||
|
||
export { NavLink } |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/** | ||
* Returns true if `to` and `pathname` match | ||
* Supports `rx` for regex matches. | ||
*/ | ||
export const navLinkMatch = (rx, to, pathname) => { | ||
return rx ? rx.test(pathname) : pathname.slice(1) === to | ||
} |