Skip to content

Commit

Permalink
Merge pull request #185 from rsksmart/feat/news-line
Browse files Browse the repository at this point in the history
News announcement bar
  • Loading branch information
owans authored Dec 4, 2024
2 parents 10e5c73 + 7fa0ebc commit 5ca8c67
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 5 deletions.
8 changes: 7 additions & 1 deletion docusaurus.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,13 @@ const config = {
description : 'Looking for information we haven’t covered? Fill out the form below to request a new article, and we’ll consider it in future updates.',
}
},
}
},
newsHighlight : [
{
title : '📣 Rootstock Hacktivator Program is Live! Contribute and Earn Rewards!',
url : '/resources/contribute/hacktivator-program/'
}
]
},
// GitHub pages deployment config.
// If you aren't using GitHub pages, you don't need these.
Expand Down
60 changes: 60 additions & 0 deletions src/components/NewsLine/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import React, { useState, useEffect, useLayoutEffect } from 'react'
import clsx from 'clsx'
import styles from './styles.module.scss'
import useDocusaurusContext from '@docusaurus/useDocusaurusContext'
import Link from '/src/components/Link'

export default function NewsLine () {
const { siteConfig } = useDocusaurusContext()
const news = siteConfig?.customFields?.newsHighlight || []
const [isClosed, setIsClosed] = useState(false)
const [isVisible, setIsVisible] = useState(true)
let lastScrollTop = 0

const handleClose = () => {
setIsClosed(true)
sessionStorage.setItem('DevportalNewsLineClosed', 'true')
}
const truncateTitle = (title, maxLength) => {
return title.length > maxLength ? `${title.substring(0, maxLength)}...` : title
}
useLayoutEffect(() => {
if (!window) return;
setIsClosed(sessionStorage.getItem('DevportalNewsLineClosed') === 'true')
const handleScroll = () => {
const scrollTop = window.pageYOffset || document.documentElement.scrollTop
if (scrollTop > lastScrollTop) {
setIsVisible(false)
} else {
setIsVisible(true)
}
lastScrollTop = scrollTop <= 0 ? 0 : scrollTop
}

window.addEventListener('scroll', handleScroll)
return () => {
window.removeEventListener('scroll', handleScroll)
}
}, [isClosed])

return (news?.length > 0 && !isClosed) && (
<div className={clsx(styles.NewsLineWrap, { [styles.hidden]: !isVisible })}>
<div className={clsx(`py-10`, styles.NewsLine)}>
<div className="container d-flex align-self-start align-items-md-center gap-8">
<div className="d-flex flex-column flex-wrap flex-md-row align-items-md-center justify-content-md-center gap-8 column-gap-lg-16 flex-grow-1">
{news.slice(0, 4).map((item, index) => (
<Link key={index} href={item.url} className={clsx(`d-flex fs-14 gap-16 align-items-center link-base`)}>
{truncateTitle(item.title, 150)}
</Link>
))}
</div>
<button className="btn-blank d-flex" onClick={handleClose} aria-label="Close">
<svg width="16" height="16">
<use xlinkHref="#icon-close-circle"/>
</svg>
</button>
</div>
</div>
</div>
)
}
38 changes: 38 additions & 0 deletions src/components/NewsLine/styles.module.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
.NewsLine {
background: var(--bs-body-bg);
border: 1px solid var(--bs-body-color);
border-width: 1px 0;

@media (max-width: 996.98px) {
border-width: 0 0 1px;
}
}

.NewsLineWrap {
position: sticky;
top: 88px;
z-index: calc(var(--ifm-z-index-fixed) - 1);
transition: transform 0.3s cubic-bezier(0.65, 0, 0.35, 1);

@media (max-width: 996.98px) {
top: 73px;
margin-top: -24px;
margin-bottom: 24px;
padding-left: 16px;
padding-right: 16px;

[class*="container"] {
padding-left: 0;
padding-right: 0;
}
}
@media (max-width: 767.98px) {
top: 54px;
margin-top: -16px;
margin-bottom: 24px;
}

&.hidden {
transform: translateY(-102%);
}
}
7 changes: 3 additions & 4 deletions src/theme/Navbar/Layout/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@ import React from 'react';
import clsx from 'clsx';
import {ErrorCauseBoundary, useThemeConfig} from '@docusaurus/theme-common';
import {
splitNavbarItems,
useHideableNavbar,
useNavbarMobileSidebar,
} from '@docusaurus/theme-common/internal';
import {translate} from '@docusaurus/Translate';
import NavbarMobileSidebar from '@theme/Navbar/MobileSidebar';
import styles from './styles.module.css';
import NavbarItem from "@theme/NavbarItem";
import MainNavDesktop from "../MainNavDesktop";
import useBrokenLinks from "@docusaurus/core/lib/client/exports/useBrokenLinks";
import NewsLine from '/src/components/NewsLine'

function NavbarBackdrop(props) {
return (
Expand All @@ -28,7 +26,7 @@ export default function NavbarLayout({children}) {
} = useThemeConfig();
const mobileSidebar = useNavbarMobileSidebar();
const {navbarRef, isNavbarVisible} = useHideableNavbar(hideOnScroll);

return (
<>
<nav
Expand All @@ -55,6 +53,7 @@ export default function NavbarLayout({children}) {
<NavbarBackdrop onClick={mobileSidebar.toggle} />
<NavbarMobileSidebar />
</nav>
<NewsLine />
<MainNavDesktop />
</>
);
Expand Down

0 comments on commit 5ca8c67

Please sign in to comment.