Skip to content

Commit cd0c039

Browse files
Merge pull request #1372 from cpinitiative/new-gatsby-img
New gatsby img
2 parents a048ce4 + 036eb57 commit cd0c039

File tree

6 files changed

+71
-32
lines changed

6 files changed

+71
-32
lines changed

gatsby-config.ts

+1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ const plugins = [
7171
name: `announcements`,
7272
},
7373
},
74+
`gatsby-plugin-image`,
7475
`gatsby-plugin-sharp`,
7576
{
7677
resolve: `gatsby-plugin-postcss`,

package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"author": "Nathan Wang <nathan.r.wang@gmail.com>",
77
"dependencies": {
88
"@babel/core": "^7.13.15",
9+
"@babel/plugin-syntax-typescript": "^7.12.13",
910
"@babel/preset-env": "^7.13.15",
1011
"@babel/preset-react": "^7.13.13",
1112
"@headlessui/react": "^0.3.2",
@@ -31,13 +32,14 @@
3132
"gatsby-plugin-create-client-paths": "^3.3.0",
3233
"gatsby-plugin-google-analytics": "^3.3.0",
3334
"gatsby-plugin-hotjar": "^1.1.1",
35+
"gatsby-plugin-image": "^1.3.1",
3436
"gatsby-plugin-manifest": "^3.3.0",
3537
"gatsby-plugin-meta-redirect": "^1.1.1",
3638
"gatsby-plugin-offline": "^4.3.0",
3739
"gatsby-plugin-postcss": "^4.3.0",
3840
"gatsby-plugin-react-helmet": "^4.3.0",
3941
"gatsby-plugin-sentry": "^1.0.1",
40-
"gatsby-plugin-sharp": "^3.3.0",
42+
"gatsby-plugin-sharp": "^3.3.1",
4143
"gatsby-plugin-sitemap": "^3.3.0",
4244
"gatsby-plugin-styled-components": "^4.3.0",
4345
"gatsby-remark-autolink-headers": "^4.0.0",

src/components/Dashboard/DailyStreak.tsx

+16-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { graphql, useStaticQuery } from 'gatsby';
2-
import Img from 'gatsby-image';
2+
import { GatsbyImage } from 'gatsby-plugin-image';
33
import * as React from 'react';
44
import { useContext, useState } from 'react';
55
import UserDataContext from '../../context/UserDataContext/UserDataContext';
@@ -37,22 +37,27 @@ const ComeBackTimer = ({ tomorrowMilliseconds }) => {
3737

3838
const PhotoCard = ({ img, day, tomorrowMilliseconds, hiddenOnDesktop }) => {
3939
return (
40-
<div className={'mb-8' + (hiddenOnDesktop ? ' lg:hidden' : '')}>
40+
<div
41+
className={
42+
'max-w-[592px] mx-auto mb-8' + (hiddenOnDesktop ? ' lg:hidden' : '')
43+
}
44+
>
4145
<div className="bg-white dark:bg-gray-900 shadow sm:rounded-lg overflow-hidden flex flex-col">
4246
<div className="px-4 pt-5 sm:px-6 sm:pt-6 pb-4">
4347
<h3 className="text-lg leading-6 font-medium text-gray-900 dark:text-dark-high-emphasis">
4448
Day {day} Photo
4549
</h3>
4650
</div>
47-
<div className="overflow-hidden relative">
51+
{/* We set text size to 0px because GatsbyImage is inline block. Without it, there's extra space after the image. */}
52+
<div className="overflow-hidden relative text-[0px]">
4853
{tomorrowMilliseconds >= 0 ? (
49-
<div className="absolute inset-0 text-center flex items-center justify-center text-black font-medium bg-white dark:bg-black dark:text-white bg-opacity-25 dark:bg-opacity-25 z-10 p-4">
54+
<div className="text-base absolute inset-0 text-center flex items-center justify-center text-black font-medium bg-white dark:bg-black dark:text-white bg-opacity-25 dark:bg-opacity-25 z-10 p-4">
5055
<ComeBackTimer tomorrowMilliseconds={tomorrowMilliseconds} />
5156
</div>
5257
) : null}
53-
<Img
58+
<GatsbyImage
59+
image={img}
5460
className="w-full object-cover"
55-
fluid={img}
5661
alt="Cow"
5762
style={tomorrowMilliseconds >= 0 ? { filter: 'blur(60px)' } : null}
5863
/>
@@ -64,17 +69,15 @@ const PhotoCard = ({ img, day, tomorrowMilliseconds, hiddenOnDesktop }) => {
6469

6570
export default function DailyStreak({ streak }) {
6671
const data = useStaticQuery(graphql`
67-
query {
72+
{
6873
allFile(
6974
filter: { relativePath: { regex: "/^cows/.*/" } }
7075
sort: { fields: name }
7176
) {
7277
edges {
7378
node {
7479
childImageSharp {
75-
fluid(maxWidth: 1000, quality: 100) {
76-
...GatsbyImageSharpFluid
77-
}
80+
gatsbyImageData(quality: 100, layout: CONSTRAINED, width: 592)
7881
}
7982
name
8083
}
@@ -84,7 +87,9 @@ export default function DailyStreak({ streak }) {
8487
`);
8588
// https://www.digitalocean.com/community/tutorials/react-usememo
8689
const cows = React.useMemo(() => {
87-
return data.allFile.edges.map(({ node }) => node.childImageSharp.fluid);
90+
return data.allFile.edges.map(
91+
({ node }) => node.childImageSharp.gatsbyImageData
92+
);
8893
}, []);
8994
const { lastVisitDate } = useContext(UserDataContext);
9095

src/components/Index/AuthorsSection.tsx

+11-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { graphql, useStaticQuery } from 'gatsby';
2-
import Img from 'gatsby-image';
2+
import { GatsbyImage } from 'gatsby-plugin-image';
33
import * as React from 'react';
44
import { Author, Authors } from '../../../content/authors/authors';
55

@@ -86,9 +86,9 @@ const AuthorCard = ({
8686
className={`inline-flex rounded-full border-2 border-white dark:border-gray-200`}
8787
>
8888
<div className="w-36 h-36 md:h-48 md:w-48 lg:h-36 lg:w-36 xl:h-48 xl:w-48">
89-
<Img
89+
<GatsbyImage
90+
image={gatsbyImage.gatsbyImageData}
9091
className="rounded-full"
91-
fixed={gatsbyImage.fixed}
9292
alt={author.name}
9393
style={{ width: '100%', height: '100%' }}
9494
/>
@@ -129,14 +129,18 @@ const AuthorCard = ({
129129

130130
export default function AuthorsSection() {
131131
const data = useStaticQuery(graphql`
132-
query {
132+
{
133133
allFile(filter: { relativePath: { regex: "/^authors/images/.*/" } }) {
134134
edges {
135135
node {
136136
childImageSharp {
137-
fixed(width: 192, height: 192, cropFocus: CENTER, quality: 100) {
138-
...GatsbyImageSharpFixed
139-
}
137+
gatsbyImageData(
138+
width: 192
139+
height: 192
140+
quality: 100
141+
transformOptions: { cropFocus: CENTER }
142+
layout: FIXED
143+
)
140144
}
141145
name
142146
}

src/components/Video.tsx

+8-9
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,21 @@
11
import { graphql, useStaticQuery } from 'gatsby';
2-
import Img from 'gatsby-image';
2+
import { GatsbyImage } from 'gatsby-plugin-image';
33
import * as React from 'react';
44
import Youtube from './markdown/Youtube';
55
export default function Video() {
66
const [showVideo, setShowVideo] = React.useState(false);
77
const data = useStaticQuery(graphql`
8-
query {
8+
{
99
thumbnail: file(relativePath: { eq: "index-video-thumbnail.jpg" }) {
1010
childImageSharp {
11-
fluid(maxWidth: 1200) {
12-
...GatsbyImageSharpFluid_withWebp
13-
}
11+
gatsbyImageData(layout: CONSTRAINED, width: 512, quality: 100)
1412
}
1513
}
1614
}
1715
`);
1816
return (
1917
<>
20-
<div className="relative mx-auto w-full rounded-lg shadow-lg lg:mx-4 xl:mx-8">
18+
<div className="relative mx-auto w-full rounded-lg shadow-lg lg:mx-4 xl:mx-8 max-w-[512px]">
2119
{showVideo && (
2220
<div className="relative block w-full min-h-32 lg:min-h-64 xl:min-h-80 rounded-lg overflow-hidden focus:outline-none focus:shadow-outline bg-gray-300">
2321
<Youtube id={'ueNT-w7Oluw'} autoplay />
@@ -27,11 +25,12 @@ export default function Video() {
2725
<button
2826
onClick={() => setShowVideo(true)}
2927
type="button"
30-
className="relative block w-full rounded-lg overflow-hidden focus:outline-none focus:shadow-outline"
28+
className="relative block w-full rounded-lg overflow-hidden focus:outline-none focus:shadow-outline text-[0px]"
3129
>
32-
<Img
30+
{/* We put 0px as text size above since GatsbyImage is inline block, so there's unwanted bottom spacing */}
31+
<GatsbyImage
32+
image={data.thumbnail.childImageSharp.gatsbyImageData}
3333
className="w-full"
34-
fluid={data.thumbnail.childImageSharp.fluid}
3534
alt="Video by William Lin"
3635
/>
3736

yarn.lock

+32-4
Original file line numberDiff line numberDiff line change
@@ -5773,6 +5773,11 @@ babel-jest@^26.6.3:
57735773
graceful-fs "^4.2.4"
57745774
slash "^3.0.0"
57755775

5776+
babel-jsx-utils@^1.1.0:
5777+
version "1.1.0"
5778+
resolved "https://registry.yarnpkg.com/babel-jsx-utils/-/babel-jsx-utils-1.1.0.tgz#304ce4fce0c86cbeee849551a45eb4ed1036381a"
5779+
integrity sha512-Mh1j/rw4xM9T3YICkw22aBQ78FhsHdsmlb9NEk4uVAFBOg+Ez9ZgXXHugoBPCZui3XLomk/7/JBBH4daJqTkQQ==
5780+
57765781
babel-loader@^8.2.2:
57775782
version "8.2.2"
57785783
resolved "https://registry.yarnpkg.com/babel-loader/-/babel-loader-8.2.2.tgz#9363ce84c10c9a40e6c753748e1441b60c8a0b81"
@@ -10881,6 +10886,24 @@ gatsby-plugin-hotjar@^1.1.1:
1088110886
dependencies:
1088210887
babel-runtime "^6.26.0"
1088310888

10889+
gatsby-plugin-image@^1.3.1:
10890+
version "1.3.1"
10891+
resolved "https://registry.yarnpkg.com/gatsby-plugin-image/-/gatsby-plugin-image-1.3.1.tgz#c3e1c39159fa32d02bd8737fb67c50bfc51efd23"
10892+
integrity sha512-ZSXBOFSYfbXGWxwc4JkFCoBMZjWLkj8vWkJ9Fu+4Gh5C4KdzveKTDJ0ypBJZU12z5qW+srnMDXVI2Ym+zL57Vw==
10893+
dependencies:
10894+
"@babel/code-frame" "^7.12.13"
10895+
"@babel/parser" "^7.12.5"
10896+
"@babel/traverse" "^7.12.5"
10897+
babel-jsx-utils "^1.1.0"
10898+
babel-plugin-remove-graphql-queries "^3.3.0"
10899+
camelcase "^5.3.1"
10900+
chokidar "^3.5.1"
10901+
common-tags "^1.8.0"
10902+
fs-extra "^8.1.0"
10903+
gatsby-core-utils "^2.3.0"
10904+
objectFitPolyfill "^2.3.0"
10905+
prop-types "^15.7.2"
10906+
1088410907
gatsby-plugin-manifest@^3.3.0:
1088510908
version "3.3.0"
1088610909
resolved "https://registry.yarnpkg.com/gatsby-plugin-manifest/-/gatsby-plugin-manifest-3.3.0.tgz#211b7ad6b0ff8dccd182a34bdb21a31c1fbd9377"
@@ -10948,10 +10971,10 @@ gatsby-plugin-sentry@^1.0.1:
1094810971
dependencies:
1094910972
"@sentry/browser" latest
1095010973

10951-
gatsby-plugin-sharp@^3.3.0:
10952-
version "3.3.0"
10953-
resolved "https://registry.yarnpkg.com/gatsby-plugin-sharp/-/gatsby-plugin-sharp-3.3.0.tgz#e6992c1a8175031edad3c2ea6c831e4a4de27771"
10954-
integrity sha512-56jRS02/Ir4t4AZ3sGNi+Ia+MVKui3IC7X9vSyrptf7jvOM8Ckc9JLqubjy2KG6+UYsXuOPcZVtn6l4mmTqnuQ==
10974+
gatsby-plugin-sharp@^3.3.1:
10975+
version "3.3.1"
10976+
resolved "https://registry.yarnpkg.com/gatsby-plugin-sharp/-/gatsby-plugin-sharp-3.3.1.tgz#103c967ba23789a522ffedbb42920f181834e6b0"
10977+
integrity sha512-mwY9L/9PiLQuNmKNAx2H1OaMHYqBhePxa80yI4gW3RzUl4gsKIDLS+UonmQVogVg+LFhaEMwZ8tiQ6tM+8RZjg==
1095510978
dependencies:
1095610979
"@babel/runtime" "^7.12.5"
1095710980
async "^3.2.0"
@@ -16353,6 +16376,11 @@ object.values@^1.1.0, object.values@^1.1.1, object.values@^1.1.3:
1635316376
es-abstract "^1.18.0-next.2"
1635416377
has "^1.0.3"
1635516378

16379+
objectFitPolyfill@^2.3.0:
16380+
version "2.3.5"
16381+
resolved "https://registry.yarnpkg.com/objectFitPolyfill/-/objectFitPolyfill-2.3.5.tgz#be8c83064aabfa4e88780f776c2013c48ce1f745"
16382+
integrity sha512-8Quz071ZmGi0QWEG4xB3Bv5Lpw6K0Uca87FLoLMKMWjB6qIq9IyBegP3b/VLNxv2WYvIMGoeUQ+c6ibUkNa8TA==
16383+
1635616384
objectorarray@^1.0.4:
1635716385
version "1.0.4"
1635816386
resolved "https://registry.yarnpkg.com/objectorarray/-/objectorarray-1.0.4.tgz#d69b2f0ff7dc2701903d308bb85882f4ddb49483"

0 commit comments

Comments
 (0)