Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve sticky navbar #339

Merged
merged 1 commit into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions web/src/layout/navigation/Header.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,22 @@

/* Sticky nav */
.stickyNav {
position: sticky;
background-color: rgba(255, 255, 255, 0.9);
min-height: 49px;
z-index: 1100;
margin-top: -55px;
}

.stickyNav > div {
min-width: 28px;
}

.stickyLogo {
height: 35px;
max-width: 210px;
}

.mobileBtn {
width: 28px !important;
height: 28px !important;
Expand All @@ -92,11 +101,6 @@
max-width: 210px;
}

.stickyLogo {
height: 35px;
max-width: 210px;
}

.logoWrapper,
.searchWrapper {
width: calc(100% - 0.4rem);
Expand Down
185 changes: 95 additions & 90 deletions web/src/layout/navigation/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,112 +21,37 @@ interface Props {
items: BaseItem[];
}

const NAV_HEIGHT = 122;
const NAV_HEIGHT_STICKY = 50;
const NAV_HEIGHT = 121;
const MARGIN_TOP = -55;

const Header = (props: Props) => {
const location = useLocation();
const openMenu = useSetMobileTOCStatus();
const { point } = useBreakpointDetect();
const [sticky, setSticky] = createSignal<boolean>(false);
const [topPosition, setTopPosition] = createSignal<number>(MARGIN_TOP);
const scroll = useWindowScrollPosition();
const y = () => scroll.y;

createEffect(
on(y, () => {
if (!isUndefined(point()) && SMALL_DEVICES_BREAKPOINTS.includes(point()!)) {
const height = sticky() ? NAV_HEIGHT_STICKY : NAV_HEIGHT;
setSticky(y() > height);
const status = y() > NAV_HEIGHT;
if (status) {
const value = y() - NAV_HEIGHT + MARGIN_TOP;
setTopPosition(value < 0 ? value : 0);
} else {
setTopPosition(MARGIN_TOP);
}
}
})
);

return (
<header class="navbar navbar-expand border-bottom p-0 shadow-sm mb-2" classList={{ 'sticky-top': sticky() }}>
<>
<div
class="container-fluid d-flex flex-column flex-lg-row align-items-center p-3 p-lg-4 mainPadding"
classList={{ 'd-none': sticky() }}
>
<div class={`d-flex flex-row justify-content-between align-items-center ${styles.logoWrapper}`}>
<A href="/" class="me-4 me-xl-5">
<img
class={styles.logo}
src={import.meta.env.MODE === 'development' ? `../../static/${props.logo}` : `${props.logo}`}
alt="Landscape logo"
width="auto"
height={48}
/>
</A>
<div class="d-flex d-lg-none">
<MobileDropdown />
</div>
</div>

<Show
when={location.pathname !== '/screenshot'}
fallback={
<Show when={!isUndefined(window.baseDS.qr_code)}>
<img
class={styles.qr}
alt="QR code"
src={
import.meta.env.MODE === 'development'
? `../../static/${window.baseDS.qr_code}`
: window.baseDS.qr_code
}
/>
</Show>
}
>
<div class="d-none d-lg-flex align-items-center">
<A
class={`btn btn-link position-relative text-uppercase fw-bold text-decoration-none p-0 ${styles.link}`}
activeClass="activeLink"
href="/"
end
>
Explore
</A>

<Show when={!isUndefined(window.baseDS.guide_summary) && !isEmpty(window.baseDS.guide_summary)}>
<A
class={`btn btn-link position-relative text-uppercase fw-bold text-decoration-none p-0 ${styles.link}`}
href="/guide"
activeClass="activeLink"
state={{ from: 'header' }}
>
Guide
</A>
</Show>

<A
class={`btn btn-link position-relative text-uppercase fw-bold text-decoration-none p-0 ${styles.link}`}
activeClass="activeLink"
href="/stats"
>
Stats
</A>
</div>

<div class={`d-flex flex-row align-items-center ms-lg-auto mt-3 mt-md-4 mt-lg-0 ${styles.searchWrapper}`}>
<div class="position-relative me-0 me-lg-4 w-100">
<Searchbar items={props.items} />
</div>
<div class="d-none d-lg-flex align-items-center">
<DownloadDropdown />
<ExternalLink
class="btn btn-md text-dark fs-5 ms-2 ms-xl-4 px-0"
href="https://github.com/cncf/landscape2"
>
<SVGIcon kind={SVGIconKind.GitHub} class={`position-relative ${styles.githubIcon}`} />
</ExternalLink>
</div>
</div>
</Show>
</div>
<div
class={`d-flex d-lg-none flex-row align-items-center justify-content-between px-2 w-100 ${styles.stickyNav}`}
classList={{ 'd-none': !sticky() }}
class={`d-flex d-lg-none flex-row align-items-center justify-content-between border-bottom shadow-sm px-2 w-100 ${styles.stickyNav}`}
classList={{ 'd-none': topPosition() === MARGIN_TOP }}
style={{ top: `${topPosition()}px` }}
>
<div class="d-flex flex-row align-items-center">
<Show when={location.pathname !== '/stats'}>
Expand Down Expand Up @@ -154,7 +79,87 @@ const Header = (props: Props) => {
<MobileDropdown inSticky />
</div>
</div>
</header>
<header class="navbar navbar-expand p-0 mb-2 border-bottom shadow-sm">
<div class="container-fluid d-flex flex-column flex-lg-row align-items-center p-3 p-lg-4 mainPadding">
<div class={`d-flex flex-row justify-content-between align-items-center ${styles.logoWrapper}`}>
<A href="/" class="me-4 me-xl-5">
<img
class={styles.logo}
src={import.meta.env.MODE === 'development' ? `../../static/${props.logo}` : `${props.logo}`}
alt="Landscape logo"
width="auto"
height={48}
/>
</A>
<div class="d-flex d-lg-none">
<MobileDropdown />
</div>
</div>

<Show
when={location.pathname !== '/screenshot'}
fallback={
<Show when={!isUndefined(window.baseDS.qr_code)}>
<img
class={styles.qr}
alt="QR code"
src={
import.meta.env.MODE === 'development'
? `../../static/${window.baseDS.qr_code}`
: window.baseDS.qr_code
}
/>
</Show>
}
>
<div class="d-none d-lg-flex align-items-center">
<A
class={`btn btn-link position-relative text-uppercase fw-bold text-decoration-none p-0 ${styles.link}`}
activeClass="activeLink"
href="/"
end
>
Explore
</A>

<Show when={!isUndefined(window.baseDS.guide_summary) && !isEmpty(window.baseDS.guide_summary)}>
<A
class={`btn btn-link position-relative text-uppercase fw-bold text-decoration-none p-0 ${styles.link}`}
href="/guide"
activeClass="activeLink"
state={{ from: 'header' }}
>
Guide
</A>
</Show>

<A
class={`btn btn-link position-relative text-uppercase fw-bold text-decoration-none p-0 ${styles.link}`}
activeClass="activeLink"
href="/stats"
>
Stats
</A>
</div>

<div class={`d-flex flex-row align-items-center ms-lg-auto mt-3 mt-md-4 mt-lg-0 ${styles.searchWrapper}`}>
<div class="position-relative me-0 me-lg-4 w-100">
<Searchbar items={props.items} />
</div>
<div class="d-none d-lg-flex align-items-center">
<DownloadDropdown />
<ExternalLink
class="btn btn-md text-dark fs-5 ms-2 ms-xl-4 px-0"
href="https://github.com/cncf/landscape2"
>
<SVGIcon kind={SVGIconKind.GitHub} class={`position-relative ${styles.githubIcon}`} />
</ExternalLink>
</div>
</div>
</Show>
</div>
</header>
</>
);
};

Expand Down