diff --git a/src/dzeio/Navbar/index.tsx b/src/dzeio/Navbar/index.tsx index 46a9c12..d366007 100644 --- a/src/dzeio/Navbar/index.tsx +++ b/src/dzeio/Navbar/index.tsx @@ -1,273 +1,274 @@ -import Router from 'next/router' - -import Image, { ImageProps } from 'next/image' -import React, { FC } from 'react' -import { ChevronDown, ChevronsRight, Menu, X } from 'lucide-react' -import Text from '../Text' -import Col from '../Col' -import Row from '../Row' -import Link from '../Link' -import { buildClassName } from '../Util' - -import css from './Navbar.module.styl' - -interface Props { - /** - * Type of Navbar - * _note: when in mobile it is not listened_ - */ - type: 'navbar' | 'sidebar' - - /** - * Logo to display - */ - logo?: ImageProps & {height: number, width: number} - /** - * Login URL - */ - loginUrl?: string - /** - * Login URL - */ - registerUrl?: string - /** - * User Informations if loggedin - */ - user?: { - /** - * Username - */ - name: string - /** - * User Short description - */ - description?: string - /** - * User Menu - */ - menu?: { - /** - * Menu links - */ - links: Array<{ - path: string - name: string - }> - /** - * Custom informations shown next to the links - */ - informations?: JSX.Element - } - } - /** - * Links to display - */ - items: Array<{ - path: string - icon?: FC - name: string - }> - /** - * Internal Use don't use it ! - */ - mobileMenu?: () => void -} - -interface State { - path?: string - short: boolean - isMobile: boolean - menuActive: boolean -} - -/** - * Navbar/Sidebar Component - * @version 1.0.3 - */ -export default class Navbar extends React.Component { - - public state: State = { - short: false, - isMobile: false, - menuActive: false - } - - public componentDidMount() { - this.setState({ - path: Router.asPath, - menuActive: !!this.props.mobileMenu - }) - Router.events.on('routeChangeComplete', () => { - this.setState({path: Router.asPath, menuActive: false}) - }) - Router.events.on('routeChangeError', () => { - this.setState({path: Router.asPath, menuActive: false}) - }) - if (!this.props.mobileMenu) { - window.addEventListener('resize', this.onResize) - this.onResize() - } - } - - public onResize = () => { - const isMobile = window.innerWidth <= 768 - if (this.state.isMobile !== isMobile) { - this.setState({isMobile}) - } - } - - public componentDidUpdate() { - if (!this.props.mobileMenu) { - if (this.state.short) { - document.body.classList.add(css.short) - } else { - document.body.classList.remove(css.short) - } - if (this.getType() === 'sidebar') { - document.body.classList.add(css['body-sidebar']) - document.body.classList.remove(css['body-navbar']) - } else { - document.body.classList.remove(css['body-sidebar']) - document.body.classList.add(css['body-navbar']) - } - } - - } - - public componentWillUnmount() { - if (!this.props.mobileMenu) { - document.body.classList.remove(css.short, css[`body-${this.getType()}`]) - window.removeEventListener('resize', this.onResize) - } - } - - public onSidebarButton = () => { - if (!this.props.mobileMenu) { - this.setState({short: !this.state.short, menuActive: false}) - } else { - this.props.mobileMenu() - } - } - - public menuCloseCallback = () => { - this.setState({menuActive: false}) - } - - public getType(): 'sidebar' | 'navbar' { - if (this.props.mobileMenu) { - return 'sidebar' - } - return this.state.isMobile ? 'navbar' : this.props.type - } - - public render = () => ( - <> - - - {!this.props.mobileMenu && this.state.isMobile && ( -
- -
- )} - - ) -} +import Router from 'next/router' + +import Image, { ImageProps } from 'next/image' +import React from 'react' +import { ChevronDown, ChevronsRight, Menu, X } from 'lucide-react' +import Text from '../Text' +import Col from '../Col' +import Row from '../Row' +import Link from '../Link' +import { buildClassName } from '../Util' + +import css from './Navbar.module.styl' +import { Icon } from '../interfaces' + +interface Props { + /** + * Type of Navbar + * _note: when in mobile it is not listened_ + */ + type: 'navbar' | 'sidebar' + + /** + * Logo to display + */ + logo?: ImageProps & {height: number, width: number} + /** + * Login URL + */ + loginUrl?: string + /** + * Login URL + */ + registerUrl?: string + /** + * User Informations if loggedin + */ + user?: { + /** + * Username + */ + name: string + /** + * User Short description + */ + description?: string + /** + * User Menu + */ + menu?: { + /** + * Menu links + */ + links: Array<{ + path: string + name: string + }> + /** + * Custom informations shown next to the links + */ + informations?: JSX.Element + } + } + /** + * Links to display + */ + items: Array<{ + path: string + icon?: Icon + name: string + }> + /** + * Internal Use don't use it ! + */ + mobileMenu?: () => void +} + +interface State { + path?: string + short: boolean + isMobile: boolean + menuActive: boolean +} + +/** + * Navbar/Sidebar Component + * @version 1.0.3 + */ +export default class Navbar extends React.Component { + + public state: State = { + short: false, + isMobile: false, + menuActive: false + } + + public componentDidMount() { + this.setState({ + path: Router.asPath, + menuActive: !!this.props.mobileMenu + }) + Router.events.on('routeChangeComplete', () => { + this.setState({path: Router.asPath, menuActive: false}) + }) + Router.events.on('routeChangeError', () => { + this.setState({path: Router.asPath, menuActive: false}) + }) + if (!this.props.mobileMenu) { + window.addEventListener('resize', this.onResize) + this.onResize() + } + } + + public onResize = () => { + const isMobile = window.innerWidth <= 768 + if (this.state.isMobile !== isMobile) { + this.setState({isMobile}) + } + } + + public componentDidUpdate() { + if (!this.props.mobileMenu) { + if (this.state.short) { + document.body.classList.add(css.short) + } else { + document.body.classList.remove(css.short) + } + if (this.getType() === 'sidebar') { + document.body.classList.add(css['body-sidebar']) + document.body.classList.remove(css['body-navbar']) + } else { + document.body.classList.remove(css['body-sidebar']) + document.body.classList.add(css['body-navbar']) + } + } + + } + + public componentWillUnmount() { + if (!this.props.mobileMenu) { + document.body.classList.remove(css.short, css[`body-${this.getType()}`]) + window.removeEventListener('resize', this.onResize) + } + } + + public onSidebarButton = () => { + if (!this.props.mobileMenu) { + this.setState({short: !this.state.short, menuActive: false}) + } else { + this.props.mobileMenu() + } + } + + public menuCloseCallback = () => { + this.setState({menuActive: false}) + } + + public getType(): 'sidebar' | 'navbar' { + if (this.props.mobileMenu) { + return 'sidebar' + } + return this.state.isMobile ? 'navbar' : this.props.type + } + + public render = () => ( + <> + + + {!this.props.mobileMenu && this.state.isMobile && ( +
+ +
+ )} + + ) +}