Maintaining/accessing global state in Gatsby site











up vote
0
down vote

favorite












I am currently trying to implement a fairly simple feature in a Gatsby JS driven site, which was built by someone else and am facing the following problem:



The page currently darkens the background when a modal is opened, I would like to have the background blur instead though. In order to achieve the darker background, the component for the modal includes a div, with fixed position, which fills the window, the css of which is as follows:



.background_fader {
display: block;
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0,0,0,0.2);
cursor: pointer;
}


I have tried using filter: blur(20px) instead of the background-color, but to no avail. It appears, that even when I change the z-index of the div, I am not able to blur the background, but would rather have to add the filter property at the wrapper of the page, i.e. much higher up in the hierarchy.



I have some experience with react and almost none with Gatsby, so please excuse if this question is simple/ rooted in a fundamental misconception.
What I have dealt with so far, were pages which used Redux as a centralized state and the much wider question which arose in implementing the blurring is this:



How do I add a state to the root-component of the Gatsby page in order to pass relevant aspects down to child components and thereby rerender the page?



After doing some research, I came to the conclusion, that it should be possible to use the wrapRootElement function from gatsby-browser.js in order to define a state at the root of the app, but I am having trouble implementing this, because the only props I seem to be able to access in the "root" component, which is generated from layouts/index.js are the data retrieved from Graphql.



Currently the layouts/index.js looks like this:



import React from 'react';
import graphql from 'graphql';
import Helmet from 'react-helmet';
import classNames from 'classnames';

// import 'bootstrap/dist/css/bootstrap.min.css';
import './reset.css';
import '../components/styleguide/index.less';
import Feedback from '../components/feedback';
import Navbar from '../components/Nav';
import Footer from '../components/footer';
import Subscribe from '../components/Subscribe';
import './index.less';

export default ({
children, location, data: {
FooterSettings: { edges: [{ node: { frontmatter: footerData } }] },
NavbarSettings: { edges: [{ node: { frontmatter: navbarSettings } }] },
HomepageSettings: { edges: [{ node: { frontmatter: homepageSettings } }] }
}
}) => {
const wrapperClasses = classNames(
'page-wrapper',
{
'page-wrapper--custom': location.pathname.split('/')[1]
},
`page-wrapper--${location.pathname.split('/')[1]}`
);
return (
<div className={wrapperClasses}>
<Helmet>
<html lang="uk" />
</Helmet>
<Navbar location={location.pathname} className={location.pathname.split('/')[1]} {...navbarSettings} />
<Feedback
location={location}
email={homepageSettings.contactFormEmail}
title={homepageSettings.contactFormTitle}
/>
{children()}
<Subscribe email={homepageSettings.contactFormEmail} className={location.pathname.split('/')[1]} />
<Footer {...footerData} {...navbarSettings} className={location.pathname.split('/')[1]} />
</div>
);
};

export const pageQuery = // very long query which appears to be sound


So, returning to my question:



How do can I dynamically add or remove a class-name to wrapperClasses based on a state that I pass to the <Feedback component and where would I instantiate this state to reoad the entire page, when it changes.



Thanks in advance folks.










share|improve this question


























    up vote
    0
    down vote

    favorite












    I am currently trying to implement a fairly simple feature in a Gatsby JS driven site, which was built by someone else and am facing the following problem:



    The page currently darkens the background when a modal is opened, I would like to have the background blur instead though. In order to achieve the darker background, the component for the modal includes a div, with fixed position, which fills the window, the css of which is as follows:



    .background_fader {
    display: block;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0,0,0,0.2);
    cursor: pointer;
    }


    I have tried using filter: blur(20px) instead of the background-color, but to no avail. It appears, that even when I change the z-index of the div, I am not able to blur the background, but would rather have to add the filter property at the wrapper of the page, i.e. much higher up in the hierarchy.



    I have some experience with react and almost none with Gatsby, so please excuse if this question is simple/ rooted in a fundamental misconception.
    What I have dealt with so far, were pages which used Redux as a centralized state and the much wider question which arose in implementing the blurring is this:



    How do I add a state to the root-component of the Gatsby page in order to pass relevant aspects down to child components and thereby rerender the page?



    After doing some research, I came to the conclusion, that it should be possible to use the wrapRootElement function from gatsby-browser.js in order to define a state at the root of the app, but I am having trouble implementing this, because the only props I seem to be able to access in the "root" component, which is generated from layouts/index.js are the data retrieved from Graphql.



    Currently the layouts/index.js looks like this:



    import React from 'react';
    import graphql from 'graphql';
    import Helmet from 'react-helmet';
    import classNames from 'classnames';

    // import 'bootstrap/dist/css/bootstrap.min.css';
    import './reset.css';
    import '../components/styleguide/index.less';
    import Feedback from '../components/feedback';
    import Navbar from '../components/Nav';
    import Footer from '../components/footer';
    import Subscribe from '../components/Subscribe';
    import './index.less';

    export default ({
    children, location, data: {
    FooterSettings: { edges: [{ node: { frontmatter: footerData } }] },
    NavbarSettings: { edges: [{ node: { frontmatter: navbarSettings } }] },
    HomepageSettings: { edges: [{ node: { frontmatter: homepageSettings } }] }
    }
    }) => {
    const wrapperClasses = classNames(
    'page-wrapper',
    {
    'page-wrapper--custom': location.pathname.split('/')[1]
    },
    `page-wrapper--${location.pathname.split('/')[1]}`
    );
    return (
    <div className={wrapperClasses}>
    <Helmet>
    <html lang="uk" />
    </Helmet>
    <Navbar location={location.pathname} className={location.pathname.split('/')[1]} {...navbarSettings} />
    <Feedback
    location={location}
    email={homepageSettings.contactFormEmail}
    title={homepageSettings.contactFormTitle}
    />
    {children()}
    <Subscribe email={homepageSettings.contactFormEmail} className={location.pathname.split('/')[1]} />
    <Footer {...footerData} {...navbarSettings} className={location.pathname.split('/')[1]} />
    </div>
    );
    };

    export const pageQuery = // very long query which appears to be sound


    So, returning to my question:



    How do can I dynamically add or remove a class-name to wrapperClasses based on a state that I pass to the <Feedback component and where would I instantiate this state to reoad the entire page, when it changes.



    Thanks in advance folks.










    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I am currently trying to implement a fairly simple feature in a Gatsby JS driven site, which was built by someone else and am facing the following problem:



      The page currently darkens the background when a modal is opened, I would like to have the background blur instead though. In order to achieve the darker background, the component for the modal includes a div, with fixed position, which fills the window, the css of which is as follows:



      .background_fader {
      display: block;
      position: fixed;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      background-color: rgba(0,0,0,0.2);
      cursor: pointer;
      }


      I have tried using filter: blur(20px) instead of the background-color, but to no avail. It appears, that even when I change the z-index of the div, I am not able to blur the background, but would rather have to add the filter property at the wrapper of the page, i.e. much higher up in the hierarchy.



      I have some experience with react and almost none with Gatsby, so please excuse if this question is simple/ rooted in a fundamental misconception.
      What I have dealt with so far, were pages which used Redux as a centralized state and the much wider question which arose in implementing the blurring is this:



      How do I add a state to the root-component of the Gatsby page in order to pass relevant aspects down to child components and thereby rerender the page?



      After doing some research, I came to the conclusion, that it should be possible to use the wrapRootElement function from gatsby-browser.js in order to define a state at the root of the app, but I am having trouble implementing this, because the only props I seem to be able to access in the "root" component, which is generated from layouts/index.js are the data retrieved from Graphql.



      Currently the layouts/index.js looks like this:



      import React from 'react';
      import graphql from 'graphql';
      import Helmet from 'react-helmet';
      import classNames from 'classnames';

      // import 'bootstrap/dist/css/bootstrap.min.css';
      import './reset.css';
      import '../components/styleguide/index.less';
      import Feedback from '../components/feedback';
      import Navbar from '../components/Nav';
      import Footer from '../components/footer';
      import Subscribe from '../components/Subscribe';
      import './index.less';

      export default ({
      children, location, data: {
      FooterSettings: { edges: [{ node: { frontmatter: footerData } }] },
      NavbarSettings: { edges: [{ node: { frontmatter: navbarSettings } }] },
      HomepageSettings: { edges: [{ node: { frontmatter: homepageSettings } }] }
      }
      }) => {
      const wrapperClasses = classNames(
      'page-wrapper',
      {
      'page-wrapper--custom': location.pathname.split('/')[1]
      },
      `page-wrapper--${location.pathname.split('/')[1]}`
      );
      return (
      <div className={wrapperClasses}>
      <Helmet>
      <html lang="uk" />
      </Helmet>
      <Navbar location={location.pathname} className={location.pathname.split('/')[1]} {...navbarSettings} />
      <Feedback
      location={location}
      email={homepageSettings.contactFormEmail}
      title={homepageSettings.contactFormTitle}
      />
      {children()}
      <Subscribe email={homepageSettings.contactFormEmail} className={location.pathname.split('/')[1]} />
      <Footer {...footerData} {...navbarSettings} className={location.pathname.split('/')[1]} />
      </div>
      );
      };

      export const pageQuery = // very long query which appears to be sound


      So, returning to my question:



      How do can I dynamically add or remove a class-name to wrapperClasses based on a state that I pass to the <Feedback component and where would I instantiate this state to reoad the entire page, when it changes.



      Thanks in advance folks.










      share|improve this question













      I am currently trying to implement a fairly simple feature in a Gatsby JS driven site, which was built by someone else and am facing the following problem:



      The page currently darkens the background when a modal is opened, I would like to have the background blur instead though. In order to achieve the darker background, the component for the modal includes a div, with fixed position, which fills the window, the css of which is as follows:



      .background_fader {
      display: block;
      position: fixed;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      background-color: rgba(0,0,0,0.2);
      cursor: pointer;
      }


      I have tried using filter: blur(20px) instead of the background-color, but to no avail. It appears, that even when I change the z-index of the div, I am not able to blur the background, but would rather have to add the filter property at the wrapper of the page, i.e. much higher up in the hierarchy.



      I have some experience with react and almost none with Gatsby, so please excuse if this question is simple/ rooted in a fundamental misconception.
      What I have dealt with so far, were pages which used Redux as a centralized state and the much wider question which arose in implementing the blurring is this:



      How do I add a state to the root-component of the Gatsby page in order to pass relevant aspects down to child components and thereby rerender the page?



      After doing some research, I came to the conclusion, that it should be possible to use the wrapRootElement function from gatsby-browser.js in order to define a state at the root of the app, but I am having trouble implementing this, because the only props I seem to be able to access in the "root" component, which is generated from layouts/index.js are the data retrieved from Graphql.



      Currently the layouts/index.js looks like this:



      import React from 'react';
      import graphql from 'graphql';
      import Helmet from 'react-helmet';
      import classNames from 'classnames';

      // import 'bootstrap/dist/css/bootstrap.min.css';
      import './reset.css';
      import '../components/styleguide/index.less';
      import Feedback from '../components/feedback';
      import Navbar from '../components/Nav';
      import Footer from '../components/footer';
      import Subscribe from '../components/Subscribe';
      import './index.less';

      export default ({
      children, location, data: {
      FooterSettings: { edges: [{ node: { frontmatter: footerData } }] },
      NavbarSettings: { edges: [{ node: { frontmatter: navbarSettings } }] },
      HomepageSettings: { edges: [{ node: { frontmatter: homepageSettings } }] }
      }
      }) => {
      const wrapperClasses = classNames(
      'page-wrapper',
      {
      'page-wrapper--custom': location.pathname.split('/')[1]
      },
      `page-wrapper--${location.pathname.split('/')[1]}`
      );
      return (
      <div className={wrapperClasses}>
      <Helmet>
      <html lang="uk" />
      </Helmet>
      <Navbar location={location.pathname} className={location.pathname.split('/')[1]} {...navbarSettings} />
      <Feedback
      location={location}
      email={homepageSettings.contactFormEmail}
      title={homepageSettings.contactFormTitle}
      />
      {children()}
      <Subscribe email={homepageSettings.contactFormEmail} className={location.pathname.split('/')[1]} />
      <Footer {...footerData} {...navbarSettings} className={location.pathname.split('/')[1]} />
      </div>
      );
      };

      export const pageQuery = // very long query which appears to be sound


      So, returning to my question:



      How do can I dynamically add or remove a class-name to wrapperClasses based on a state that I pass to the <Feedback component and where would I instantiate this state to reoad the entire page, when it changes.



      Thanks in advance folks.







      reactjs jsx gatsby






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 18 at 20:45









      MTTI

      18418




      18418





























          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53365278%2fmaintaining-accessing-global-state-in-gatsby-site%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53365278%2fmaintaining-accessing-global-state-in-gatsby-site%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Costa Masnaga

          Fotorealismo

          Sidney Franklin