-
-
Notifications
You must be signed in to change notification settings - Fork 200
feat(theme): add Banner Component #2654
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
SoonIter marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }); | ||
| 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'); | ||
SoonIter marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }} | ||
| className="rp-banner__close" | ||
| /> | ||
| </> | ||
| )} | ||
| </div> | ||
| </> | ||
| ); | ||
| }, | ||
| ); | ||
|
|
||
| Banner.displayName = 'Banner'; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
SoonIter marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| }); | ||
| }; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.