Skip to content

lelouchB/gatsby-theme-cloudinary-gallery

Repository files navigation

Gatsby Theme Cloudinary Gallery

A Gatsby Theme for adding a Cloudinary Image Gallery to your Gatsby site.

screenshot

Deploy with Vercel

Installation

npm install --save gatsby-theme-cloudinary-gallery

Usage

// gatsby-config.js
module.exports = {
  plugins: [
    {
      resolve: "gatsby-theme-cloudinary-gallery",
      options: {
        cloudName: <YOUR-CLOUDINARY-CLOUDNAME>,
        apiKey: <YOUR-CLOUDINARY-API-KEY>,
        apiSecret: <YOUR-CLOUDIANRY-API-SECRET>,
      },
    },
  ],
};

This is the simplest usage of the gatsby-theme-cloudinary-gallery. You will need to pass your API Keys as props to the theme.

Securing your API Keys

To secure your Cloudianry API Keys, store them as envrionment variables. Run the following command to install dotenv package and create a file named .env in the root directory of your project.

npm install dotenv
touch .env

Add the API Keys to the .env file.

CLOUDINARY_CLOUDNAME = 
CLOUDINARY_API_KEY = 
CLOUDINARY_API_SECRET =

Update gatsby-config.js file like this.

// gatsby-config.js
require('dotenv').config();

module.exports = {
  plugins: [
    {
      resolve: "gatsby-theme-cloudinary-gallery",
      options: {
        cloudName: process.env.CLOUDINARY_CLOUDNAME,
        apiKey: process.env.CLOUDINARY_API_KEY,
        apiSecret: process.env.CLOUDINARY_API_SECRET,
      },
    },
  ],
};

Theme Options

You can pass the following options to the gatsby-theme-cloudinary-gallery theme.

Option Type Default Required Description
cloudName string N/A true Cloud name of your Cloudinary account, can be obtained from your Cloudinary console. This should be stored and retrieved as an environment variable.
apiKey string N/A true API Key of your Cloudinary account, can be obtained from your Cloudinary console. This should be stored and retrieved as an environment variable.
apiSecret string N/A true API Secret of your Cloudinary account, can be obtained from your Cloudinary console. This should be stored and retrieved as an environment variable.
basePath string / false URL for Gallery Page
type string false upload This is the storage type: upload, private, authenticated, facebook, twitter, gplus, instagram_name, gravatar, youtube, hulu, vimeo, animoto, or dailymotion.
maxResults integer false 10 Maximum number of assets to return (up to 500).
prefix string false '' Find all resources with a public ID that starts with the given prefix. The resources are sorted by public ID in the response.

Transformations

By default, the images are displayed using the Image component from the Cloudinary React SDK. Additional transformation is applied using the Transformation component.

<Image
  key={cloudinaryImage.id}
  cloudName={cloudinaryImage.cloud_name}
  publicId={cloudinaryImage.public_id}
  className="w-full block mx-auto"
>
  <Transformation
    fetchFormat="auto"
    width="800"
    height="800"
    loading="lazy"
    radius="20"
    gravity="face"
    crop="fill"
  />
</Image>;

Here are all the default transformations applied to the images.

Key Default Description
fetchFormat auto The format of the fetched image. Using auto, delivers the image in the most optimized format for each browser that requests it. For more details see image format conversion.
width 800 Width of the fetched image.
height 800 Height of the fetched image.
loading lazy Lazy Loading to delay loading images if they are not yet visible on the screen.
radius 20 Round the corner of the images.
gravity face Cloudinary supports built-in face-detection capabilities that allow you to intelligently crop your images. To automatically crop an image so that the detected face(s) is used as the center of the derived picture, set the gravity parameter to one of the following values: face - the region of the image that includes the single largest face. faces - the region of the image that includes all the faces detected.
crop fill Resizes the image to fill the specified dimensions without distortion. The image may be cropped as a result. For more details see.

<Gallery /> Component

You can use the Gallery component from the gatsby-theme-cloudinary-gallery to embed the Image Gallery in any page.

import { Gallery } from "gatsby-theme-cloudinary-gallery";

const Homepage = () => (
  <div>
    <Gallery />
  </div>
);

export default Homepage;

Shadowing

Gatsby themes introduce a concept called Shadowing. This feature allows users to replace a file in the src directory that is included in the webpack bundle with their own implementation. This works for React components, pages in src/pages, JSON files, TypeScript files, as well as any other imported file (such as .css) in your site.

Read more about Shadowing here.

Shadowing example

If you’ve installed gatsby-theme-cloudinary-gallery you’ll notice that it renders a Heading component which is used in the index page. If you’d like to change the Heading component you can do so with the shadowing API.

Theme file structure

You can inspect gatsby-theme-cloudinary-gallery’s file structure to determine the file path for the file you want to shadow.

├── gatsby-browser.js
├── gatsby-config.js
├── gatsby-node.js
├── index.js
├── .gitignore
├── README.md
├── package.json
├── tailwind.config.js
└── src
    ├── components
    │   ├── Heading.js
    │   ├── Gallery.js
    ├── pages
    │   └── index.js
    └── styles
       └── global.css

Customizing the Heading component

In this case, the file to shadow is gatsby-theme-cloudinary-gallery/src/components/Heading.js.

The shadowing API uses a deterministic file structure to determine which component will be rendered. In order to override the Heading component in gatsby-theme-cloudinary-gallery, create a file named demo/src/gatsby-theme-cloudinary-gallery/components/Heading.js.

Any file that lives in the src/cloudinary-gallery directory of the demo’s site will be used instead of a file with the same name located in the theme’s src directory: gatsby-theme-cloudinary-gallery/src. This replaces the entire file: to re-use parts of the original file from the theme such as functionality or styling, check out the sections of this doc on extending and importing shadowed files.

This means that demo/src/gatsby-theme-cloudinary-gallery/components/Heading.js will be rendered in place of gatsby-theme-cloudinary-gallery/src/components/Heading.js:

For conflicting changes, your local shadowed settings take precedence. Here you are changing the name of the Image Gallery to Cute Puppies Gallery.

// src/gatsby-theme-cloudinary-gallery/components/Heading.js
import React from "react";

const Heading = () => {
  return (
    <h1 className="text-4xl md:text-5xl 
    text-center text-gray-800 font-sans font-medium">
      Cute Puppies Gallery
    </h1>
  );
};

export default Heading;

You can view this demo/src/gatsby-theme-cloudinary-gallery/components/Heading.js file here.

screenshot2

Available Scripts

yarn dev

This will run the demo app in development mode.

Navigate to http://localhost:8000 to view it in the browser.

yarn build

This will build the demo app for production.

Outputs to the demo/public folder.

🧐 What's inside?

A quick look at the top-level files and directories you'll see in a Gatsby project.

.
├── node_modules
├── src
├── .gitignore
├── .prettierrc
├── gatsby-browser.js
├── gatsby-config.js
├── gatsby-node.js
├── LICENSE
├── package-lock.json
├── package.json
└── README.md
  1. /node_modules: This directory contains all of the modules of code that your project depends on (npm packages) are automatically installed.

  2. /src: This directory will contain all of the code related to what you will see on the front-end of your site (what you see in the browser) such as your site header or a page template. src is a convention for “source code”.

  3. .gitignore: This file tells git which files it should not track / not maintain a version history for.

  4. .prettierrc: This is a configuration file for Prettier. Prettier is a tool to help keep the formatting of your code consistent.

  5. gatsby-browser.js: This file is where Gatsby expects to find any usage of the Gatsby browser APIs (if any). These allow customization/extension of default Gatsby settings affecting the browser.

  6. gatsby-config.js: This is the main configuration file for a Gatsby site. This is where you can specify information about your site (metadata) like the site title and description, which Gatsby plugins you’d like to include, etc. (Check out the config docs for more detail).

  7. gatsby-node.js: This file is where Gatsby expects to find any usage of the Gatsby Node APIs (if any). These allow customization/extension of default Gatsby settings affecting pieces of the site build process.

  8. LICENSE: This Gatsby starter is licensed under the 0BSD license. This means that you can see this file as a placeholder and replace it with your own license.

  9. package-lock.json (See package.json below, first). This is an automatically generated file based on the exact versions of your npm dependencies that were installed for your project. (You won’t change this file directly).

  10. package.json: A manifest file for Node.js projects, which includes things like metadata (the project’s name, author, etc). This manifest is how npm knows which packages to install for your project.

  11. README.md: A text file containing useful reference information about your project.

🎓 Learning Gatsby

Looking for more guidance? Full documentation for Gatsby lives on the website. Here are some places to start:

  • For most developers, we recommend starting with our in-depth tutorial for creating a site with Gatsby. It starts with zero assumptions about your level of ability and walks through every step of the process.

  • To dive straight into code samples, head to Gatsby documentation. In particular, check out the Guides, API Reference, and Advanced Tutorials sections in the sidebar.

Inspiration

This theme is inspired from the Gatsby Theme Gallery and gatsby-source-cloudinary plugin.