File size: 2,225 Bytes
1daa44a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
class Navbar {
    constructor(UIManager) {
        this.uiManager = UIManager;
        this.menu = this.uiManager.menu;
        this.hamburger = this.uiManager.hamburger;
        this.container = this.uiManager.container;
        this.state = true; // true = open, false = closed
        this.menuWidth = 0;
        this.containerWidth = this.container.clientWidth;
        this.NAV_AREA_LARGE = 20; // % of window width
        this.NAV_AREA_MEDIUM = 25; // % of window width
        this.NAV_AREA_SMALL = 60; // % of window width
        this.ANIMATION_STEP = 5; // pixels per frame
    }

    calculateNavArea() {
        if (window.innerWidth > 785) {
            return window.innerWidth < 1100 
                ? window.innerWidth * (this.NAV_AREA_MEDIUM / 100)
                : window.innerWidth * (this.NAV_AREA_LARGE / 100);
        }
        return window.innerWidth * (this.NAV_AREA_SMALL / 100);
    }

    toggleState() {
        if (this.state) {
            this.nav.close();
        } else {
            this.nav.open();
        }
        this.state = !this.state;
    }

    animateNav(action) {
        if (action === 'open' && this.menuWidth < this.navArea) {
            this.menuWidth += this.ANIMATION_STEP;
        } else if (action === 'close' && this.menuWidth > 0) {
            this.menuWidth -= this.ANIMATION_STEP;
        } else {
            return; // Stop animation
        }

        this.menu.style.width = `${this.menuWidth}px`;
        if(window.innerWidth>775){
            this.container.style.width = `${this.containerWidth - this.menuWidth}px`;
            this.container.style.left = `${this.menuWidth}px`;
        }
        requestAnimationFrame(() => this.animateNav(action));
    }

    run() {
        this.navArea = this.calculateNavArea();
        this.nav = {
            open: () => this.animateNav('open'),
            close: () => this.animateNav('close'),
        };

        if (window.innerWidth <= 785) {
            this.state = false;
        } else {
            this.nav.open();
        }

        this.hamburger.addEventListener('click', () => this.toggleState());
    }
}

export default Navbar