Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
50 changes: 50 additions & 0 deletions packages/theme-default/src/components/Banner/index.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
:root {
--rp-banner-background: linear-gradient(
279deg,
hsl(32.71, 100%, 50%) 35.21%,
hsl(6.91, 94.76%, 67%) 63.34%
);
}

.rp-banner {
display: flex;
justify-content: center;
align-items: center;
color: #fff;
position: sticky;
top: 0;
left: 0;
z-index: 999;
background: var(--rp-banner-background);
height: 36px;
}

.rp-banner__link {
color: #fff;
font-weight: 700;
text-decoration: none;
}

.rp-banner__link:hover {
text-decoration: underline;
opacity: 0.9;
}

.rp-banner__link:active {
opacity: 0.7;
}

.rp-banner__close {
right: 1.5rem;
font-size: 28px;
padding: 5px;
position: absolute;
cursor: pointer;
border-radius: 4px;
box-sizing: border-box;
color: inherit;
}

.rp-banner__close:hover {
background-color: rgba(255, 255, 255, 0.3);
}
121 changes: 121 additions & 0 deletions packages/theme-default/src/components/Banner/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import { Link, mergeRefs } from '@theme';
import CloseSvg from '@theme-assets/close';
import clsx from 'clsx';
import { forwardRef, type ReactNode, useState } from 'react';
import { SvgWrapper } from '../SvgWrapper';
import './index.scss';

export type BannerProps = {
/**
* @default true
*/
display?: boolean;
className?: string;
} & (
| {
/**
* @default 'localStorage'
*/
storage?: 'localStorage' | 'sessionStorage';
/**
* @default 'rp-banner-closed'
*/
storageKey?: string;
href: string;
message: string | ReactNode;
}
| {
customChildren: ReactNode;
}
);

/**
* @example
* // theme/index.tsx
* import { Layout as BasicLayout, Banner } from '@rspress/core/theme';
* const Layout = () => {
const lang = useLang();
return (
<BasicLayout
beforeNav={
<Banner
href="/"
message={
lang === 'en'
? '🚧 Rspress 2.0 document is under development'
: '🚧 Rspress 2.0 文档还在开发中'
}
localStorageKey="rspress-announcement-closed"
/>
}
/>
);
};
export { Layout }
*
*/
export const Banner = forwardRef<HTMLDivElement, BannerProps>(
(props, forwardedRef) => {
const {
href,
message,
display = true,
className,
storageKey = 'rp-banner-closed',
storage = 'localStorage',
customChildren,
} = props as {
href?: string;
message?: string | ReactNode;
display?: boolean;
className?: string;
storageKey: string;
storage?: 'localStorage' | 'sessionStorage';
customChildren?: ReactNode;
};

if (!display) {
return null;
}
const [height, setHeight] = useState(36);
const ref = mergeRefs(forwardedRef, ref => {
ref?.offsetHeight && setHeight(ref?.offsetHeight);
});
const [disable, setDisable] = useState(
typeof window === 'undefined'
? false
: storage
? (window[storage].getItem(storageKey) ?? false)
: false,
);

if (disable) {
return null;
}

return (
<>
<style>{`:root {--rp-banner-height: ${height}px;}`}</style>
<div className={clsx('rp-banner', className)} ref={ref}>
{customChildren ?? (
<>
<Link href={href} className="rp-banner__link">
{message}
</Link>
<SvgWrapper
icon={CloseSvg}
onClick={() => {
setDisable(true);
window.localStorage.setItem(storageKey, 'true');
}}
className="rp-banner__close"
/>
</>
)}
</div>
</>
);
},
);

Banner.displayName = 'Banner';
2 changes: 1 addition & 1 deletion packages/theme-default/src/components/Nav/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
align-items: center;

position: sticky;
top: 0;
top: var(--rp-banner-height, 0px);
left: 0;
z-index: var(--rp-z-index-nav);

Expand Down
2 changes: 1 addition & 1 deletion packages/theme-default/src/components/NavScreen/index.scss
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.rp-nav-screen {
display: none;
position: fixed;
top: var(--rp-nav-height);
top: calc(var(--rp-nav-height) + var(--rp-banner-height, 0px));
right: 0;
bottom: 0;
left: 0;
Expand Down
2 changes: 2 additions & 0 deletions packages/theme-default/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// components
export { Aside } from './components/Aside/index';
export { Badge } from './components/Badge/index';
export { Banner, type BannerProps } from './components/Banner/index';
export { Button } from './components/Button/index';
export { Callout, type CalloutProps } from './components/Callout/index';
export {
Expand Down Expand Up @@ -78,6 +79,7 @@ export { DocLayout, type DocLayoutProps } from './layout/DocLayout';
export { HomeLayout } from './layout/HomeLayout/index';
export { Layout } from './layout/Layout/index';
export { NotFoundLayout } from './layout/NotFountLayout/index';
export { mergeRefs } from './logic/mergeRefs';
export { scrollToTarget, useSetup } from './logic/sideEffects';
export { useThemeState } from './logic/useAppearance';
export { useFullTextSearch } from './logic/useFullTextSearch';
Expand Down
18 changes: 11 additions & 7 deletions packages/theme-default/src/layout/DocLayout/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,10 @@
max-width: var(--rp-sidebar-width);
min-width: var(--rp-sidebar-width);

height: calc(100vh - var(--rp-nav-height));
max-height: calc(100vh - var(--rp-nav-height));
height: calc(100vh - var(--rp-nav-height) - var(--rp-banner-height, 0px));
max-height: calc(
100vh - var(--rp-nav-height) - var(--rp-banner-height, 0px)
);

padding: var(--rp-sidebar-padding);
border-right: 1px solid var(--rp-c-divider-light);
Expand All @@ -72,7 +74,7 @@
// postion
position: sticky;
margin-top: calc(-1 * var(--rp-sidebar-menu-height));
top: var(--rp-nav-height);
top: calc(var(--rp-nav-height) + var(--rp-banner-height, 0px));
left: 0;
bottom: 0;
z-index: var(--rp-z-index-sidebar);
Expand All @@ -89,21 +91,23 @@
width: var(--rp-aside-width);

position: sticky;
top: var(--rp-nav-height);
top: calc(var(--rp-nav-height) + var(--rp-banner-height, 0px));
margin-top: var(--rp-sidebar-menu-height);
right: 0;
z-index: var(--rp-z-index-aside);
padding-top: var(--rp-content-padding-y);

overflow: hidden scroll;
scrollbar-width: none;
max-height: calc(100vh - var(--rp-nav-height));
max-height: calc(
100vh - var(--rp-nav-height) - var(--rp-banner-height, 0px)
);

box-shadow: var(--rp-shadow-1);
}
&__menu {
position: sticky;
top: var(--rp-nav-height);
top: calc(var(--rp-nav-height) + var(--rp-banner-height, 0px));
left: 0;
z-index: var(--rp-z-index-sidebar-menu);

Expand Down Expand Up @@ -188,7 +192,7 @@
&__sidebar {
z-index: var(--rp-z-index-sidebar);
position: fixed;
top: var(--rp-nav-height);
top: calc(var(--rp-nav-height) + var(--rp-banner-height, 0px));

margin-top: 0px;
height: calc(100vh - var(--rp-nav-height));
Expand Down
11 changes: 11 additions & 0 deletions packages/theme-default/src/logic/mergeRefs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export function mergeRefs<T>(...refs: React.Ref<T>[]): React.RefCallback<T> {
return value => {
refs.forEach(ref => {
if (typeof ref === 'function') {
ref(value);
} else if (ref !== null) {
ref.current = value;
}
});
};
}
23 changes: 10 additions & 13 deletions website/theme/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
Banner,
HomeLayout as BasicHomeLayout,
Layout as BasicLayout,
getCustomMDXComponent as basicGetCustomMDXComponent,
Expand All @@ -13,12 +14,11 @@ import {
LlmsCopyButton,
LlmsViewOptions,
} from '@rspress/plugin-llms/runtime';
import { Announcement } from '@rstack-dev/doc-ui/announcement';
import { NavIcon } from '@rstack-dev/doc-ui/nav-icon';
import { ToolStack } from './components/ToolStack';

import './index.css';
import { NoSSR, useLang } from '@rspress/core/runtime';
import { useLang } from '@rspress/core/runtime';

function HomeLayout() {
return (
Expand All @@ -39,17 +39,14 @@ const Layout = () => {
<BasicLayout
beforeNavTitle={<NavIcon />}
beforeNav={
<NoSSR>
<Announcement
href="/"
message={
lang === 'en'
? '🚧 Rspress 2.0 document is under development'
: '🚧 Rspress 2.0 文档还在开发中'
}
localStorageKey="rspress-announcement-closed"
/>
</NoSSR>
<Banner
href="/"
message={
lang === 'en'
? '🚧 Rspress 2.0 document is under development'
: '🚧 Rspress 2.0 文档还在开发中'
}
/>
}
/>
);
Expand Down
Loading