Skip to content

Commit

Permalink
[#2155] Make navigation sticky in desktop
Browse files Browse the repository at this point in the history
  • Loading branch information
julianguyen committed Jul 8, 2024
1 parent 17e71f7 commit a661828
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 9 deletions.
2 changes: 2 additions & 0 deletions app/assets/stylesheets/dashboard/dashboard_content.scss
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
padding: $size-0 25px 50px 25px;
display: flex;
flex-direction: row;
margin-top: 86px;

@media screen and (max-width: $medium) {
flex-direction: column;
padding: 0;
margin-top: 0;
}
}
3 changes: 2 additions & 1 deletion app/assets/stylesheets/static/static_content.scss
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
box-sizing: border-box;
padding: $size-40 34px;
scroll-behavior: smooth;
margin: 0 auto;
margin: 86px auto 0 auto;

@media screen and (max-width: $medium) {
padding: $size-20;
margin: 0 auto;
}

@media screen and (max-width: $small) {
Expand Down
4 changes: 2 additions & 2 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@
<%= react_component('SkipToContent', props: { id: 'mainContent' })%>
<%= react_component('Header', props: header_props) %>
<main>
<%= react_component('Toast', props: {
notice: notice.present? ? notice : '',
<%= react_component('Toast', props: {
notice: notice.present? ? notice : '',
alert: alert.present? ? alert : '' ,
appendDashboardClass: (user_signed_in? && !static_page?) || secret_share_path? }) %>
<% if user_signed_in? && !static_page? %>
Expand Down
11 changes: 11 additions & 0 deletions client/app/components/Header/Header.scss
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@
}
}

&Scroll {
background-color: $black-80;

@media screen and (max-width: $medium) {
background-color: unset;
}
}

&Mobile {
background: transparent;
position: static;
Expand Down Expand Up @@ -105,9 +113,12 @@
flex-direction: row;
justify-content: space-between;
align-items: center;
position: fixed;
z-index: $z-index-front;

@media screen and (max-width: $medium) {
padding: 15px $size-10;
position: static;
}

&Home {
Expand Down
23 changes: 21 additions & 2 deletions client/app/components/Header/index.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// @flow
import React, { useState, useRef, type Node } from 'react';
import React, {
useState, useRef, useEffect, type Node,
} from 'react';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { faBars, faTimes } from '@fortawesome/free-solid-svg-icons';
import renderHTML from 'react-render-html';
Expand All @@ -25,6 +27,7 @@ export const Header = ({
home, links, mobileOnly, profile,
}: Props): Node => {
const [mobileNavOpen, setMobileNavOpen] = useState(false);
const [scrolled, setScrolled] = useState(false);
const navigationRef = useRef(null);

useFocusTrap(navigationRef, mobileNavOpen);
Expand Down Expand Up @@ -73,7 +76,7 @@ export const Header = ({

const displayDesktop = (): Node => (
<div
className={css.headerDesktop}
className={`${css.headerDesktop} ${scrolled ? css.headerScroll : ''}`}
aria-label={I18n.t('navigation.main_menu')}
role="navigation"
>
Expand Down Expand Up @@ -109,6 +112,22 @@ export const Header = ({
</div>
);

useEffect(() => {
const handleScroll = (event) => {
if (event.currentTarget.scrollY > 0) {
setScrolled(true);
} else {
setScrolled(false);
}
};
if (!mobileNavOpen) {
window.addEventListener('scroll', handleScroll);
}
return () => {
window.removeEventListener('scroll', handleScroll);
};
}, [mobileNavOpen]);

return (
<header
id="header"
Expand Down
11 changes: 7 additions & 4 deletions client/app/components/Modal/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,13 @@ export const Modal = (props: Props): Node => {

useFocusTrap(modalEl, open);

useEffect(() => () => {
const documentBody = ((document.body: any): HTMLBodyElement);
documentBody.classList.remove('bodyModalOpen');
}, []);
useEffect(
() => () => {
const documentBody = ((document.body: any): HTMLBodyElement);
documentBody.classList.remove('bodyModalOpen');
},
[],
);

const handleKeyPress = (
event: SyntheticKeyboardEvent<HTMLDivElement>,
Expand Down

0 comments on commit a661828

Please sign in to comment.