Skip to content
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

💄 DOP-4658 side nav now handles dark mode #1127

Merged
merged 8 commits into from
Jun 12, 2024
13 changes: 3 additions & 10 deletions src/components/Card/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import PropTypes from 'prop-types';
import { withPrefix, navigate } from 'gatsby';
import { navigate } from 'gatsby';
import styled from '@emotion/styled';
import { useDarkMode } from '@leafygreen-ui/leafygreen-provider';
import LeafyGreenCard from '@leafygreen-ui/card';
Expand All @@ -12,6 +12,7 @@ import ConditionalWrapper from '../ConditionalWrapper';
import Link from '../Link';
import Tag from '../Tag';
import { isRelativeUrl } from '../../utils/is-relative-url';
import { getSuitableIcon } from '../../utils/get-suitable-icon';

const cardBaseStyles = css`
display: flex;
Expand Down Expand Up @@ -157,15 +158,7 @@ const Card = ({
isLanding && !isLargeIconStyle ? landingStyles : '', // must come after other styles to override
];

let iconSrc;

if (icon) {
const isPath = icon.includes('/');
const getIcon = `${icon}${darkMode ? '_inverse' : ''}`;
const imageUrl = `https://webimages.mongodb.com/_com_assets/icons/${getIcon}.svg`;

iconSrc = isPath ? (darkMode && iconDark ? withPrefix(iconDark) : withPrefix(icon)) : imageUrl;
}
let iconSrc = getSuitableIcon(darkMode, iconDark, icon);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Can change this to a const now that it's a beautiful one-liner


return (
<LeafyGreenCard className={cx(styling)} onClick={url ? () => onCardClick(url) : undefined}>
Expand Down
9 changes: 6 additions & 3 deletions src/components/Sidenav/IALinkedData.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import React from 'react';
import { css, cx } from '@leafygreen-ui/emotion';
import { withPrefix } from 'gatsby';
import { useDarkMode } from '@leafygreen-ui/leafygreen-provider';
import { palette } from '@leafygreen-ui/palette';
import { SideNavItem } from '@leafygreen-ui/side-nav';
import Link from '../Link';
import { theme } from '../../theme/docsTheme';
import { getSuitableIcon } from '../../utils/get-suitable-icon';

const ulStyling = css`
display: grid;
Expand Down Expand Up @@ -62,12 +63,14 @@ const liStyling = css`
`;

const IALinkedData = ({ linkedData }) => {
const { darkMode } = useDarkMode();
return (
<ul className={cx(ulStyling)}>
{linkedData.map(({ headline, url, icon, 'icon-alt': iconAlt }, index) => {
{linkedData.map(({ headline, url, icon, 'icon-alt': iconAlt, 'icon-dark': iconDark }, index) => {
const iconSrc = getSuitableIcon(darkMode, iconDark, icon);
return (
<SideNavItem key={index} className={cx(liStyling)} as={Link} to={url}>
<img height={16} width={16} src={withPrefix(icon)} alt={iconAlt} />
<img height={16} width={16} src={iconSrc} alt={iconAlt} />
<span>{headline}</span>
</SideNavItem>
);
Expand Down
12 changes: 7 additions & 5 deletions src/components/Sidenav/Sidenav.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,6 @@ const Sidenav = ({ chapters, guides, page, pageTitle, repoBranches, siteTitle, s

const { darkMode } = useDarkMode();

const borderBottomStyle = darkMode ? palette.gray.dark2 : palette.gray.light2;

// CSS top property values for sticky side nav based on header height
const topValues = useStickyTopValues(eol);

Expand Down Expand Up @@ -240,10 +238,15 @@ const Sidenav = ({ chapters, guides, page, pageTitle, repoBranches, siteTitle, s
widthOverride={isMobile ? viewportSize.width : SIDENAV_WIDTH}
>
<IATransition back={back} hasIA={!!ia} slug={slug} isMobile={isMobile}>
<NavTopContainer style={{ '--background-color': darkMode ? palette.gray.dark4 : palette.gray.light3 }}>
<NavTopContainer
style={{
'--background-color': darkMode ? palette.gray.dark4 : palette.gray.light3,
'--border-bottom-color': darkMode ? palette.gray.dark2 : palette.gray.light2,
}}
>
<ArtificialPadding />
<DocsHomeButton darkMode={darkMode} />
<Border style={{ '--border-bottom-color': borderBottomStyle }} />
<Border />
{ia && (
<IA
header={!hideIaHeader ? <span className={cx([titleStyle])}>{formatText(pageTitle)}</span> : null}
Expand All @@ -259,7 +262,6 @@ const Sidenav = ({ chapters, guides, page, pageTitle, repoBranches, siteTitle, s
css={css`
margin-bottom: 0;
`}
style={{ '--border-bottom-color': borderBottomStyle }}
/>
)}
</NavTopContainer>
Expand Down
20 changes: 20 additions & 0 deletions src/utils/get-suitable-icon.js
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love this. Thanks!

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { withPrefix } from 'gatsby';

/**
*
* @param {boolean} isDarkMode
* @param {boolean} iconDark
* @param {string} icon
* @returns {string}
*/
export const getSuitableIcon = (isDarkMode, iconDark, icon) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks so much for adding this and putting it into the Card component as well!

one super miniscule nit is that I feel like the order of the parameters would make more sense as icon, iconDark, isDarkMode. Not super sure why, maybe just feel like the icon parameter should come first. Obviously this is really tiny, though, so do whatever you want!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mayaraman19 good call on that, icon would be the prominent param, which I can see your case for it coming first. I will make the change. Thank you for your feedback on that.

if (icon) {
const isPath = icon.startsWith('/');
const getIcon = `${icon}${isDarkMode ? '_inverse' : ''}`;
const imageUrl = `https://webimages.mongodb.com/_com_assets/icons/${getIcon}.svg`;

return isPath ? (isDarkMode && iconDark ? withPrefix(iconDark) : withPrefix(icon)) : imageUrl;
}

return '';
};
Loading