-
Notifications
You must be signed in to change notification settings - Fork 92
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
feat: navbar extra button from contentful #565
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,27 @@ | |
fill: var(--text); | ||
} | ||
|
||
.dui-navbar | ||
.dui-navbar-wrapper | ||
.item:not(.mobile).dui-menu-item.extra.has-background { | ||
height: calc(100% - 24px); | ||
padding: 12px; | ||
margin-top: 12px; | ||
} | ||
|
||
.item.mobile.dui-menu-item.extra.has-background { | ||
margin-top: 40px; | ||
padding: 20px; | ||
width: calc(100% - 50px - 40px); | ||
border-radius: 8px; | ||
justify-content: center; | ||
border-bottom: none; | ||
} | ||
|
||
.item.mobile.dui-menu-item.extra.has-background .dui-icon-container.centered { | ||
display: none; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This removes a div that is used to render icons, it's not used on the extra button, and it caused the text on the mobile extra button to not be centered, so I hide it. |
||
|
||
@media (max-width: 991px) { | ||
.item.dui-menu-item.mobile { | ||
cursor: pointer; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,8 +8,16 @@ import { MenuItemProps } from './MenuItem.types' | |
import './MenuItem.css' | ||
|
||
export const MenuItem = (props: MenuItemProps) => { | ||
const { activePage, section, title, onToggleShowSubMenu, isMobile, mainUrl } = | ||
props | ||
const { | ||
activePage, | ||
section, | ||
title, | ||
onToggleShowSubMenu, | ||
isMobile, | ||
mainUrl, | ||
textColor, | ||
backgroundColor | ||
} = props | ||
|
||
const mainRedirect = useCallback(() => { | ||
mainUrl && window.open(mainUrl, '_self') | ||
|
@@ -27,7 +35,13 @@ export const MenuItem = (props: MenuItemProps) => { | |
onMouseLeave={(e: React.MouseEvent) => | ||
!isMobile && onToggleShowSubMenu(e, false, section) | ||
} | ||
className={classNames('dui-menu-item', section, isMobile && 'mobile')} | ||
className={classNames( | ||
'dui-menu-item', | ||
section, | ||
isMobile && 'mobile', | ||
backgroundColor && 'has-background' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Applies a |
||
)} | ||
style={{ color: textColor, backgroundColor }} | ||
> | ||
{title} | ||
{isMobile && <ArrowIcon />} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,3 +10,52 @@ export const getNavbarPagesUrls = () => { | |
[NavbarPages.GOVERNANCE]: config.get('GOVERNANCE_URL') | ||
} | ||
} | ||
|
||
export type NavbarExtraButton = { | ||
text: string | ||
link: string | ||
visible: boolean | ||
textColor?: `#${string}` | ||
backgroundColor?: `#${string}` | ||
id?: string | ||
ttl: number | ||
} | ||
|
||
export type LocalStorageNavbarExtraButton = { | ||
button: NavbarExtraButton | ||
expiresAt: number | ||
} | ||
|
||
export const getExtraButton = async () => { | ||
const cachedExtraButton = localStorage.getItem('navbarExtraButton') | ||
if (cachedExtraButton) { | ||
try { | ||
const parsed = JSON.parse( | ||
cachedExtraButton | ||
) as LocalStorageNavbarExtraButton | ||
if (parsed.expiresAt > Date.now()) { | ||
return parsed.button | ||
} | ||
} catch (error) { | ||
// error parsing cached data, ignore and fetch from Contentful | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This above restores the data from a localStorage cache, to avoid hitting contentful every time. The TTL is configured form contentful itself. |
||
try { | ||
const SPACE_ID = config.get('CONTENTFUL_SPACE_ID') | ||
const ENV = config.get('CONTENTFUL_ENV') | ||
const ACCESS_TOKEN = config.get('CONTENTFUL_NAVBAR_ACCESS_TOKEN') | ||
const ENTRY_ID = config.get('CONTENTFUL_NAVBAR_ENTRY_ID') | ||
const CONTENTFUL_URL = `https://cdn.contentful.com/spaces/${SPACE_ID}/environments/${ENV}/entries/${ENTRY_ID}?access_token=${ACCESS_TOKEN}` | ||
const response = await fetch(CONTENTFUL_URL) | ||
const entry = await response.json() | ||
const button = entry.fields as NavbarExtraButton | ||
localStorage.setItem( | ||
'navbarExtraButton', | ||
JSON.stringify({ button, expiresAt: Date.now() + button.ttl * 1000 }) | ||
) | ||
return button | ||
} catch (error) { | ||
console.error(error) | ||
return null | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The styles above adjust the paddings/margins when the button has a background (
.has-background
) otherwise the default styles don't look okay with a background.