in packages/docusaurus-theme-common/src/contexts/announcementBar.tsx [46:102]
function useContextValue(): ContextValue {
const {announcementBar} = useThemeConfig();
const isBrowser = useIsBrowser();
const [isClosed, setClosed] = useState(() =>
isBrowser
? // On client navigation: init with local storage value
isDismissedInStorage()
: // On server/hydration: always visible to prevent layout shifts (will be hidden with css if needed)
false,
);
// Update state after hydration
useEffect(() => {
setClosed(isDismissedInStorage());
}, []);
const handleClose = useCallback(() => {
setDismissedInStorage(true);
setClosed(true);
}, []);
useEffect(() => {
if (!announcementBar) {
return;
}
const {id} = announcementBar;
let viewedId = IdStorage.get();
// retrocompatibility due to spelling mistake of default id
// see https://github.com/facebook/docusaurus/issues/3338
// cSpell:ignore annoucement
if (viewedId === 'annoucement-bar') {
viewedId = 'announcement-bar';
}
const isNewAnnouncement = id !== viewedId;
IdStorage.set(id);
if (isNewAnnouncement) {
setDismissedInStorage(false);
}
if (isNewAnnouncement || !isDismissedInStorage()) {
setClosed(false);
}
}, [announcementBar]);
return useMemo(
() => ({
isActive: !!announcementBar && !isClosed,
close: handleClose,
}),
[announcementBar, isClosed, handleClose],
);
}