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

Implement background image with gatsby-plugin-image #533

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
8 changes: 0 additions & 8 deletions gatsby-browser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,6 @@ export const wrapPageElement = ({ element }) => (
<WrapPageElementComponent>{element}</WrapPageElementComponent>
);

export const onClientEntry = () => {
// IntersectionObserver polyfill for gatsby-background-image (Safari, IE)
if (!('IntersectionObserver' in window)) {
import('intersection-observer');
console.log('# IntersectionObserver is polyfilled!!');
}
};

// Copied from https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby-plugin-google-gtag/src/gatsby-browser.js
export const onRouteUpdate = ({ location, prevLocation }) => {
if (
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
"embla-carousel": "^8.2.0",
"embla-carousel-react": "^8.2.0",
"gatsby": "^5.13.3",
"gatsby-background-image": "^1.6.0",
"gatsby-image": "^3.11.0",
"gatsby-omni-font-loader": "^2.0.2",
"gatsby-plugin-canonical-urls": "^5.13.1",
Expand Down
4 changes: 2 additions & 2 deletions src/components/AuthorBlogPostCard/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ interface AuthorBlogPostCardProps {
};
tags: { type: string; name: string }[];
authors: { id: string; name: string; role?: string; picture?: any }[];
hero: { alternativeText: string; localFile: any };
hero: { alternativeText?: string; localFile: any };
};
}

Expand Down Expand Up @@ -59,7 +59,7 @@ const AuthorBlogPostCard = ({ data: blogPost }: AuthorBlogPostCardProps) => {
<Link to={`/${blogPost.slug}`} className={container}>
<GatsbyImage
image={blogPostImage}
alt={blogPost.hero.alternativeText}
alt={blogPost.hero.alternativeText ?? 'Blog post image'}
className={popularArticlesImage}
/>
<div className={articleCardHeader}>
Expand Down
12 changes: 12 additions & 0 deletions src/components/BackgroundImages/BackgroundWrapper.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import React from 'react';
import { wrapper } from './styles.module.less';

interface BackgroundWrapperProps {
children: React.ReactNode;
}

const BackgroundWrapper = ({ children }: BackgroundWrapperProps) => {
return <div className={wrapper}>{children}</div>;
};

export default BackgroundWrapper;
40 changes: 15 additions & 25 deletions src/components/BackgroundImages/BigImageBackground.tsx
Original file line number Diff line number Diff line change
@@ -1,34 +1,24 @@
import { graphql, useStaticQuery } from 'gatsby';
import React from 'react';
import BackgroundImage from 'gatsby-background-image';
import { backgroundIndexWrapper } from './styles.module.less';
import { StaticImage } from 'gatsby-plugin-image';
import clsx from 'clsx';
import { backgroundImage, bigVector } from './styles.module.less';
import BackgroundWrapper from './BackgroundWrapper';

const BigImageBackground = ({
children,
...rest
className,
}: React.BaseHTMLAttributes<HTMLDivElement>) => {
const imageData = useStaticQuery(graphql`
query {
background: file(relativePath: { eq: "blogs-bg.png" }) {
childImageSharp {
fluid(quality: 90, maxWidth: 1920) {
...GatsbyImageSharpFluid_withWebp_noBase64
}
}
}
}
`);

return (
<BackgroundImage
fadeIn={false}
critical
className={backgroundIndexWrapper}
fluid={imageData.background.childImageSharp.fluid}
{...{ ...{ children, rest }, children: undefined }}
>
{children}
</BackgroundImage>
<BackgroundWrapper>
<StaticImage
alt=""
src="../../images/blogs-bg.png"
className={clsx(backgroundImage, bigVector)}
placeholder="blurred"
quality={100}
/>
<div className={className}>{children}</div>
</BackgroundWrapper>
);
};

Expand Down
28 changes: 28 additions & 0 deletions src/components/BackgroundImages/BubblesCircleBackground.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import React from 'react';
import { StaticImage } from 'gatsby-plugin-image';
import clsx from 'clsx';
import {
backgroundImage,
bubbleCircleBackgroundImage,
} from './styles.module.less';
import BackgroundWrapper from './BackgroundWrapper';

const BubblesCircleBackground = ({
children,
className,
}: React.BaseHTMLAttributes<HTMLDivElement>) => {
return (
<BackgroundWrapper>
<StaticImage
alt=""
src="../../images/lp-connector/take-a-tour/background.png"
className={clsx(backgroundImage, bubbleCircleBackgroundImage)}
placeholder="blurred"
quality={100}
/>
<div className={className}>{children}</div>
</BackgroundWrapper>
);
};

export default BubblesCircleBackground;
25 changes: 25 additions & 0 deletions src/components/BackgroundImages/CdcBackground.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import React from 'react';
import { StaticImage } from 'gatsby-plugin-image';
import clsx from 'clsx';
import { backgroundImage, cdcBackgroundImage } from './styles.module.less';
import BackgroundWrapper from './BackgroundWrapper';

const CdcBackground = ({
children,
className,
}: React.BaseHTMLAttributes<HTMLDivElement>) => {
return (
<BackgroundWrapper>
<StaticImage
alt=""
src="../../images/lp-connector/change-data/background.png"
className={clsx(backgroundImage, cdcBackgroundImage)}
placeholder="blurred"
quality={100}
/>
<div className={className}>{children}</div>
</BackgroundWrapper>
);
};

export default CdcBackground;
24 changes: 24 additions & 0 deletions src/components/BackgroundImages/CirclesBackground.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import React from 'react';
import { StaticImage } from 'gatsby-plugin-image';
import clsx from 'clsx';
import { fullWidth, backgroundImage, fullHeight } from './styles.module.less';

const CirclesBackground = ({
children,
className,
}: React.BaseHTMLAttributes<HTMLDivElement>) => {
return (
<div className={fullWidth}>
<StaticImage
alt=""
src="../../images/circles-background.png"
className={clsx(backgroundImage, fullHeight)}
placeholder="blurred"
quality={100}
/>
<div className={className}>{children}</div>
</div>
Comment on lines +11 to +20
Copy link
Member

Choose a reason for hiding this comment

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

All of the BackgroundImages/* components should probably share a single root component since they are all the same except for the src setting.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The StaticImage component does not accept to receive prop values as variables. They should be constants. So we can't create a separate component for it, but I created a separate component for the wrapper div element.

);
};

export default CirclesBackground;
Original file line number Diff line number Diff line change
@@ -1,46 +1,27 @@
import { graphql, useStaticQuery } from 'gatsby';
import React from 'react';

import BackgroundImage, {
IBackgroundImageProps,
} from 'gatsby-background-image';

import clsx from 'clsx';
import { sectionTopBottomPadding } from '../../globalStyles/sections.module.less';
import { commonBackgroundImageProps } from './utils';

type DarkSwoopingLinesLeftDirectionBackgroundProps = IBackgroundImageProps & {
children?: React.ReactNode;
} & React.HTMLAttributes<HTMLDivElement>;
import { StaticImage } from 'gatsby-plugin-image';
import { backgroundImage, fullHeight } from './styles.module.less';
import { backgroundColors } from './shared';
import BackgroundWrapper from './BackgroundWrapper';

const DarkSwoopingLinesLeftDirectionBackground = ({
children,
className,
...rest
}: DarkSwoopingLinesLeftDirectionBackgroundProps) => {
const swoopingLines = useStaticQuery(graphql`
query {
imageData: file(relativePath: { eq: "overlay-vector-left.png" }) {
childImageSharp {
fluid(quality: 90, maxWidth: 2170) {
...GatsbyImageSharpFluid_withWebp_noBase64
}
}
}
}
`);

}: React.HTMLAttributes<HTMLDivElement>) => {
return (
<BackgroundImage
Tag="section"
fluid={swoopingLines.imageData.childImageSharp.fluid}
backgroundColor="#04192b"
className={clsx(sectionTopBottomPadding, className)}
{...commonBackgroundImageProps}
{...rest}
>
{children}
</BackgroundImage>
<BackgroundWrapper>
<StaticImage
alt=""
src="../../images/overlay-vector-left.png"
className={clsx(backgroundImage, fullHeight)}
placeholder="blurred"
quality={100}
backgroundColor={backgroundColors.dark}
/>
<div className={className}>{children}</div>
</BackgroundWrapper>
);
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,48 +1,26 @@
import { graphql, useStaticQuery } from 'gatsby';
import React from 'react';

import BackgroundImage, {
IBackgroundImageProps,
} from 'gatsby-background-image';

import { StaticImage } from 'gatsby-plugin-image';
import clsx from 'clsx';
import { sectionTopBottomPadding } from '../../globalStyles/sections.module.less';
import { commonBackgroundImageProps } from './utils';

type LightSwoopingLinesRightDirectionBackgroundProps = IBackgroundImageProps & {
children?: React.ReactNode;
} & React.HTMLAttributes<HTMLDivElement>;
import { backgroundImage, fullHeight } from './styles.module.less';
import { backgroundColors } from './shared';
import BackgroundWrapper from './BackgroundWrapper';

const LightSwoopingLinesRightDirectionBackground = ({
children,
className,
...rest
}: LightSwoopingLinesRightDirectionBackgroundProps) => {
const swoopingLines = useStaticQuery(graphql`
query {
imageData: file(
relativePath: { eq: "lp-connector/hero/Vector.png" }
) {
childImageSharp {
fluid(quality: 90, maxWidth: 2170) {
...GatsbyImageSharpFluid_withWebp_noBase64
}
}
}
}
`);

}: React.HTMLAttributes<HTMLDivElement>) => {
return (
<BackgroundImage
Tag="section"
fluid={swoopingLines.imageData.childImageSharp.fluid}
backgroundColor="#f9fafc"
className={clsx(sectionTopBottomPadding, className)}
{...commonBackgroundImageProps}
{...rest}
>
{children}
</BackgroundImage>
<BackgroundWrapper>
<StaticImage
alt=""
src="../../images/swooping-lines-to-the-right.png"
className={clsx(backgroundImage, fullHeight)}
placeholder="blurred"
quality={100}
backgroundColor={backgroundColors.light}
/>
<div className={className}>{children}</div>
</BackgroundWrapper>
);
};

Expand Down
53 changes: 18 additions & 35 deletions src/components/BackgroundImages/StraightLinesBackground.tsx
Original file line number Diff line number Diff line change
@@ -1,43 +1,26 @@
import { graphql, useStaticQuery } from 'gatsby';
import React from 'react';

import BackgroundImage, {
IBackgroundImageProps,
} from 'gatsby-background-image';

import { commonBackgroundImageProps } from './utils';

type StraightLinesBackgroundProps = IBackgroundImageProps & {
children?: React.ReactNode;
} & React.HTMLAttributes<HTMLDivElement>;
import clsx from 'clsx';
import { StaticImage } from 'gatsby-plugin-image';
import { backgroundImage, fullHeight } from './styles.module.less';
import { backgroundColors } from './shared';
import BackgroundWrapper from './BackgroundWrapper';

const StraightLinesBackground = ({
children,
...rest
}: StraightLinesBackgroundProps) => {
const swoopingLines = useStaticQuery(graphql`
query {
imageData: file(
relativePath: { eq: "lp-connector/real-time/Vector.png" }
) {
childImageSharp {
fluid(quality: 90, maxWidth: 1356) {
...GatsbyImageSharpFluid_withWebp_noBase64
}
}
}
}
`);

className,
}: React.HTMLAttributes<HTMLDivElement>) => {
return (
<BackgroundImage
fluid={swoopingLines.imageData.childImageSharp.fluid}
backgroundColor="#04192b"
{...commonBackgroundImageProps}
{...rest}
>
{children}
</BackgroundImage>
<BackgroundWrapper>
<StaticImage
alt=""
src="../../images/lp-connector/real-time/Vector.png"
className={clsx(backgroundImage, fullHeight)}
placeholder="blurred"
quality={100}
backgroundColor={backgroundColors.dark}
/>
<div className={className}>{children}</div>
</BackgroundWrapper>
);
};

Expand Down
Loading
Loading