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

Add useEffect and state to conditionally render footer width/logo #1640

Merged
merged 1 commit into from
Sep 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions src/@newrelic/gatsby-theme-newrelic/components/Logo.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 10 additions & 14 deletions src/layouts/QuickStartLayout.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { useEffect, useState } from 'react';
import {
Layout,
GlobalHeader,
Expand All @@ -8,19 +8,15 @@ import PropTypes from 'prop-types';
import { css } from '@emotion/react';
import '../components/styles.scss';

const getSidebarWidth = () => {
switch (true) {
case !window.location:
return 0;
case window.location.pathname === '/instant-observability/':
// this value matches '--sidebar-width' variable defined in instant-observability.js
return 300;
default:
return 0;
}
};

const QuickStartLayout = ({ children }) => {
const [sidebarWidth, setSidebarWidth] = useState(0);

useEffect(() => {
if (window.location.pathname === '/instant-observability/') {
setSidebarWidth(300);
} else setSidebarWidth(0);
}, [children]);
Copy link
Contributor

Choose a reason for hiding this comment

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

Out of curiosity, do we need to add children to the dependency array? This might cause unnecessary calls to this function (whenever anything inside this component changes, this will get called).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

because this is in the layout it won't fire the effect again (it didn't seem) when switching to the details view since the layout doesn't change. using the children of the layout to determine if it fires was the only way i found to re-trigger it. very open to other solutions though! i just haven't found them 😭


return (
<>
<GlobalHeader activeSite={NR_SITES.IO} />
Expand All @@ -43,7 +39,7 @@ const QuickStartLayout = ({ children }) => {
</Layout.Main>
<Layout.Footer
css={css`
--sidebar-offset: ${getSidebarWidth()}px;
--sidebar-offset: ${sidebarWidth}px;

max-width: calc(var(--site-max-width) - var(--sidebar-offset));

Expand Down