-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(plugin-docs): Added announcement (#326)
- Loading branch information
Showing
4 changed files
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
55 changes: 55 additions & 0 deletions
55
packages/plugin-docs/client/theme-doc/components/Announcement.tsx
This file contains 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,55 @@ | ||
import cx from 'classnames'; | ||
import React, { useEffect, useState } from 'react'; | ||
import { useThemeContext } from '../context'; | ||
|
||
function Announcement() { | ||
const { themeConfig } = useThemeContext()!; | ||
|
||
if (!themeConfig.announcement) { | ||
return null; | ||
} | ||
|
||
const [closed, setClosed] = useState(false); | ||
|
||
useEffect(() => { | ||
if (!themeConfig.announcement) return; | ||
const item = localStorage.getItem('closed_announcements'); | ||
const closed: string[] = item ? JSON.parse(item) : []; | ||
if (closed.includes(themeConfig.announcement.title)) { | ||
setClosed(true); | ||
} | ||
}, []); | ||
|
||
function close(e: React.MouseEvent) { | ||
e.preventDefault(); | ||
if (!themeConfig.announcement) return; | ||
const item = localStorage.getItem('closed_announcements'); | ||
const closed: string[] = item ? JSON.parse(item) : []; | ||
closed.push(themeConfig.announcement.title); | ||
localStorage.setItem('closed_announcements', JSON.stringify(closed)); | ||
setClosed(true); | ||
} | ||
|
||
if (closed) return null; | ||
|
||
return ( | ||
<div className="w-full py-1 bg-blue-200 dark:bg-blue-900 animate-pulse"> | ||
<p | ||
className={cx( | ||
'text-center dark:text-white text-sm font-bold ', | ||
themeConfig.announcement.link && 'cursor-pointer', | ||
)} | ||
onClick={() => { | ||
window.open(themeConfig.announcement?.link, '_blank'); | ||
}} | ||
> | ||
{themeConfig.announcement.title} | ||
</p> | ||
<button className="absolute right-2 top-0 px-8" onClick={close}> | ||
x | ||
</button> | ||
</div> | ||
); | ||
} | ||
|
||
export default Announcement; |
This file contains 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 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