Skip to content
This repository has been archived by the owner on Dec 6, 2022. It is now read-only.

Commit

Permalink
feat: disable save button by default (#9)
Browse files Browse the repository at this point in the history
Closes #4
Closes #5

BREAKING CHANGE: The Pinterest Save button isn't enabled by default anymore. Use the `saveButton` option to add it.
  • Loading branch information
MichaelDeBoey authored Mar 1, 2020
1 parent a86fa4d commit c932d06
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 15 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/gatsby-browser.js
/gatsby-node.js
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ package-lock.json
yarn.lock

/gatsby-browser.js
/gatsby-node.js
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ coverage
dist

/gatsby-browser.js
/gatsby-node.js
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

<!-- prettier-ignore-start -->
[![Build Status][build-badge]][build]
[![Code Coverage][coverage-badge]][coverage]
[![version][version-badge]][package]
[![downloads][downloads-badge]][npmtrends]
[![MIT License][license-badge]][license]
Expand Down Expand Up @@ -62,10 +61,13 @@ module.exports = {
{
resolve: `gatsby-plugin-pinterest`,
options: {
// Set to true to hide the text and display only a round P button
round: false, // default
// Set to true to display a bigger button
tall: true, // default
// If you just want to use the default, you can set this to `true`, defaults to `false`
saveButton: {
// Set to true to hide the text and display only a round P button
round: false, // default
// Set to true to display a bigger button
tall: true, // default
},
},
},

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"homepage": "https://github.com/robinmetral/gatsby-plugin-pinterest#readme",
"files": [
"gatsby-browser.js",
"gatsby-node.js",
"index.js"
],
"scripts": {
Expand Down
31 changes: 21 additions & 10 deletions src/gatsby-browser.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
const injectPinterestScript = (tall, round) => {
const injectPinterestScript = ({ saveButton = false }) => {
const addJS = () => {
const script = document.createElement("script");
script.async = true;
script.defer = true;
script.src = "https://assets.pinterest.com/js/pinit.js";

script.setAttribute("data-pin-hover", "true");
if (Boolean(saveButton)) {
script.setAttribute("data-pin-hover", "true");

if (tall) {
script.setAttribute("data-pin-tall", "true");
}
const { round = false, tall = true } =
typeof saveButton === "boolean" ? {} : saveButton;
if (round) {
script.setAttribute("data-pin-round", "true");
}

if (round) {
script.setAttribute("data-pin-round", "true");
if (tall) {
script.setAttribute("data-pin-tall", "true");
}
}

document.getElementsByTagName("head")[0].appendChild(script);
Expand All @@ -23,10 +27,17 @@ const injectPinterestScript = (tall, round) => {

let injectedPinterestScript = false;

exports.onRouteUpdate = (args, { tall = true, round = false } = {}) => {
if (document.querySelector("[data-pin-do]") !== null) {
exports.onRouteUpdate = (args, pluginOptions = {}) => {
const querySelectors = [
"[data-pin-do]",
Boolean(pluginOptions.saveButton) ? "img" : "",
]
.filter(Boolean)
.join(",");

if (document.querySelector(querySelectors) !== null) {
if (!injectedPinterestScript) {
injectPinterestScript(tall, round);
injectPinterestScript(pluginOptions);

injectedPinterestScript = true;
}
Expand Down
8 changes: 8 additions & 0 deletions src/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const deprecationWarning = `[gatsby-plugin-pinterest] From now on, you can use the 'saveButton' option to show Pinterest's save button on images.
See https://github.com/robinmetral/gatsby-plugin-pinterest#usage`;

exports.onPreInit = ({ reporter }, { round, tall } = {}) => {
if (round || tall) {
reporter.warn(deprecationWarning);
}
};

0 comments on commit c932d06

Please sign in to comment.