summaryrefslogtreecommitdiffhomepage
path: root/desktop/packages/mullvad-vpn/src/renderer/components/NavigationContainer.tsx
blob: 946a66c05f3b12f705402c383feffce5dc01327a (plain)
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
import React from 'react';

import { IScrollEvent } from './CustomScrollbars';

interface NavigationContainerProps {
  children?: React.ReactNode;
}

interface NavigationContainerState {
  showsBarTitle: boolean;
}

export const NavigationScrollContext = React.createContext<{
  showsBarTitle: boolean;
  onScroll: (event: IScrollEvent) => void;
}>({
  showsBarTitle: false,
  onScroll: () => {
    throw new Error('NavigationScrollContext provider is missing.');
  },
});

export class NavigationContainer extends React.Component<
  NavigationContainerProps,
  NavigationContainerState
> {
  state: NavigationContainerState = {
    showsBarTitle: false,
  };

  componentDidMount() {
    this.updateBarAppearance({ scrollLeft: 0, scrollTop: 0 });
  }

  render() {
    const { children } = this.props;
    const { showsBarTitle } = this.state;

    return (
      <NavigationScrollContext.Provider
        value={{
          showsBarTitle,
          onScroll: this.onScroll,
        }}>
        {children}
      </NavigationScrollContext.Provider>
    );
  }

  private onScroll = (event: IScrollEvent) => {
    this.updateBarAppearance(event);
  };

  private updateBarAppearance = ({ scrollTop }: IScrollEvent) => {
    // Show the bar title when user scrolls past page title
    const showsBarTitle = scrollTop > 20;

    if (this.state.showsBarTitle !== showsBarTitle) {
      this.setState({ showsBarTitle });
    }
  };
}