From b3117816f4c325c4734c5ea6031995f45522dc8c Mon Sep 17 00:00:00 2001 From: alexcss Date: Wed, 4 Dec 2024 11:29:32 +0200 Subject: [PATCH] News line show/hide --- docusaurus.config.js | 9 +-- src/components/NewsLine/index.js | 75 ++++++++++++++++------ src/components/NewsLine/styles.module.scss | 37 ++++++++++- 3 files changed, 94 insertions(+), 27 deletions(-) diff --git a/docusaurus.config.js b/docusaurus.config.js index e5c407a2..2bfacf9d 100644 --- a/docusaurus.config.js +++ b/docusaurus.config.js @@ -53,10 +53,11 @@ const config = { } }, }, - news: [ - {title: 'Rootstock Hacktivator Program is Live! Contribute and Earn Rewards!', url: 'https://dev.rootstock.io/resources/contribute/hacktivator-program/'}, - {title: 'Contribute and Earn Rewards!', url: '/changelog'}, - {title: 'Rootstock News', url: '/changelog'}, + newsHighlight : [ + { + title : '📣 Rootstock Hacktivator Program is Live! Contribute and Earn Rewards!', + url : '/resources/contribute/hacktivator-program/' + } ] }, // GitHub pages deployment config. diff --git a/src/components/NewsLine/index.js b/src/components/NewsLine/index.js index 4bcb6b6a..29e7ad05 100644 --- a/src/components/NewsLine/index.js +++ b/src/components/NewsLine/index.js @@ -1,27 +1,60 @@ -import React from "react"; -import clsx from "clsx"; -import styles from './styles.module.scss'; -import useDocusaurusContext from '@docusaurus/useDocusaurusContext'; +import React, { useState, useEffect } 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?.news || []; + const { siteConfig } = useDocusaurusContext() + const news = siteConfig?.customFields?.newsHighlight || [] + const [isClosed, setIsClosed] = useState(() => { + return sessionStorage.getItem('DevportalNewsLineClosed') === 'true' + }) + const [isVisible, setIsVisible] = useState(true) + let lastScrollTop = 0 - return news?.length > 0 &&
-
- {news.map((item, index) => ( - - {item.title} - - ))} + const handleClose = () => { + setIsClosed(true) + sessionStorage.setItem('DevportalNewsLineClosed', 'true') + } + const truncateTitle = (title, maxLength) => { + return title.length > maxLength ? `${title.substring(0, maxLength)}...` : title + } + useEffect(() => { + 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) + } + }, []) + + return (news?.length > 0 && !isClosed) && ( +
+
+
+
+ {news.slice(0, 4).map((item, index) => ( + + {truncateTitle(item.title, 150)} + + ))} +
+ +
+
- -
+ ) } diff --git a/src/components/NewsLine/styles.module.scss b/src/components/NewsLine/styles.module.scss index b2474de4..f8dd0d14 100644 --- a/src/components/NewsLine/styles.module.scss +++ b/src/components/NewsLine/styles.module.scss @@ -1,5 +1,38 @@ -.NewsLine{ +.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: var(--ifm-z-index-fixed); + 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: 16px; + } + + &.hidden { + transform: translateY(-102%); + } }