2.0.7 • Published 2 years ago

react-buzzfeed-quiz v2.0.7

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

MIT License

Check out the demo page here!

Installation

react-buzzfeed-quiz is available through Yarn:

yarn add react-buzzfeed-quiz

or through npm:

npm install react-buzzfeed-quiz

Once it's installed, you can import the BuzzFeedQuiz component as follows:

import { BuzzFeedQuiz } from "react-buzzfeed-quiz";

Custom Styles

If you'd like to import the default react-buzzfeed-quiz CSS styles, you can import them like so:

import "react-buzzfeed-quiz/lib/styles.css";

Fonts

react-buzzfeed-quiz does not provide any fonts, but some recommended fonts that are often found on BuzzFeed quizzes are:

Example

Additional example can be found in the example directory. The package can be used as follows:

import ProfilePhoto from "./path/to/ProfilePhoto.jpg";
import BackgroundImage from "./path/to/BackgroundImage.jpg";
import FirstResultImage from "./path/to/FirstResultImage.jpg";
import SecondResultImage from "./path/to/SecondResultImage.jpg";
import ResponseImage from "./path/to/ResponseImage.jpg";
import { BuzzFeedQuiz } from "react-buzzfeed-quiz";
import "react-buzzfeed-quiz/lib/styles.css";

const App = () => {
  return (
    <BuzzFeedQuiz
      title={"Your title goes here."}
      description={"Your description goes here."}
      byline={true}
      bylineAuthor={"Your name"}
      bylineAuthorLink={"www.yourlink.com"}
      bylineAuthorLinkOpenInNewTab={true}
      bylineAuthorTagline={"Your author tagline goes here"}
      bylineAvatarImageSrc={ProfilePhoto}
      autoScroll={true}
      onRestart={() => alert("This alert was triggered by the onRestart prop!")}
      onResult={() => alert("The onResult prop triggered this alert!")}
      onAnswerSelection={(questionIndex, answerIndex, resultID) =>
        console.log({
          questionIndex,
          answerIndex,
          resultID,
        })
      }
      facebookShareButton={true}
      facebookShareLink={"www.yourlink.com"}
      twitterShareButton={true}
      twitterShareLink={"www.yourlink.com"}
      copyShareButton={true}
      copyShareLink={"This text was copied using the copyShareLink prop."}
      questions={[
        {
          question: "Here's a default question",
          answers: [
            {
              answer: "Answer one",
              resultID: 0,
            },
            {
              answer: "Answer two",
              resultID: 1,
            },
            {
              answer: "Answer three",
              resultID: 2,
            },
          ],
        },
        {
          question: "Let's add some background and font colors",
          backgroundColor: "rgb(211, 211, 211)",
          fontColor: "#000",
          answers: [
            {
              answer: "First answer",
              backgroundColor: "red",
              fontColor: "rgb(215, 215, 215)",
              resultID: 0,
            },
            {
              answer: "Second answer",
              backgroundColor: "orange",
              fontColor: "green",
              resultID: 1,
            },
            {
              answer: "Third answer",
              backgroundColor: "yellow",
              fontColor: "#000",
              resultID: 2,
              revealResponse: {
                 title: "Here's an answer response!",
                 description: "Here's a description for the answer response!",
                 image: ResponseImage,
                 imageAttribution: "Answer response image attribution text goes here",
              },
            },
          ],
        },
        {
          question: "Here is some overlapping image text",
          questionRelativeToImage: "overlap",
          imageAttribution: "Question image attribution text goes here",
          answerArrangement: "tile",
          backgroundImageSrc: BackgroundImage,
          answers: [
            {
              answer: "This answer has a background photo",
              resultID: 0,
              backgroundImageSrc: BackgroundImage,
              imageAttribution: "Answer photo attribution text goes here",
            },
            {
              answer: "Not this one, though",
              resultID: 1,
              backgroundColor: "rgb(238,243,247)",
              fontColor: "rgb(53,51,48)",
            },
            {
              answer: "Not this one, either",
              resultID: 2,
              backgroundColor: "rgb(238,243,247)",
              fontColor: "rgb(53,51,48)",
            },
          ],
        },
        {
          question: "Here is some adjacent image text",
          questionRelativeToImage: "adjacent",
          answerArrangement: "row",
          backgroundImageSrc: BackgroundImage,
          answers: [
            {
              answer: "Answer one",
              resultID: 0,
            },
            {
              answer: "Answer two",
              resultID: 1,
            },
            {
              answer: "Answer three",
              resultID: 2,
            },
          ],
        },
        {
          question: "Answers can also trigger a callback function",
          answers: [
            {
              answer: "This one does not trigger a function",
              resultID: 0,
            },
            {
              answer: "Click for answer function",
              onAnswerSelection: (questionIndex, answerIndex, resultID) =>
                alert("This alert is caused by an answer selection!"),
              resultID: 1,
            },
          ],
        },
      ]}
      results={[
        {
          title: "Your first result title goes here",
          description: "Your first result description goes here",
          resultImageSrc: FirstResultImage,
          imageAttribution: "Your photo attribution text goes here",
          resultID: 0,
        },
        {
          title: "Your second result title goes here",
          description: "Your second result description goes here",
          resultImageSrc: SecondResultImage,
          resultID: 1,
        },
        {
          title: "Your third result title goes here",
          description: "Result images and attributions are optional!",
          resultID: 2,
        },
      ]}
    />
  );
};

Disclaimer

react-buzzfeed-quiz is not affiliated, associated, authorized, endorsed by, or in any way officially connected with BuzzFeed, Inc., or any of its subsidiaries or its affiliates. The official BuzzFeed, Inc. website can be found at https://www.buzzfeed.com.

The name BuzzFeed as well as related names, marks, emblems/logos, and images are registered trademarks of BuzzFeed, Inc.

User Guide

API

The BuzzFeedQuiz component accepts the following props:

NameTypeDefaultDescription
titlestring""The main title header for the quiz.
descriptionstring""The sub-heading description for the quiz.
bylinebooleantrueWhether or not to render the author information sub-header.
bylineAuthorstring""The name of the quiz creator. Only takes effect when byline prop is set to true.
bylineAuthorLinkstring""URL to redirect to when byline author name is clicked. Only takes effect when byline prop is set to true.
bylineAuthorLinkOpenInNewTabbooleanfalseWhether or not to open a new tab when redirecting to the byline author link URL upon author name click. Only takes effect when byline prop is set to true.
bylineAuthorTaglinestring""Additional text to render under the author name in the byline. Only takes effect when byline prop is set to true.
bylineAvatarImageSrcstringtrueURL or local filename to be included in the byline's circular avatar image's src attribute. Only takes effect when byline prop is set to true.
generalBackgroundColorstring""Applies a given color to every question container and every answer container's background. If no color or invalid color is supplied, the default background color is black.
generalFontColorstring""Applies a given color to every question container and every answer container's font. If no color or invalid color is supplied, the default font color is white.
autoScrollbooleantrueWhether or not to automatically smooth-scroll the quiz to the next available question upon answer selection, to the result when the quiz is finished, and to the top of the quiz when the quiz is restarted.
facebookShareButtonbooleantrueWhether or not to include the Facebook share button in the result container.
facebookShareLinkstring""URL to share to Facebook upon clicking the Facebook share button.
twitterShareButtonbooleantrueWhether or not to include the Twitter share button in the result container.
twitterShareLinkstring""URL to share to Twitter upon clicking the Twitter share button.
twitterShareTextstring""Text to pre-fill shared tweet (keep in mind Twitter's current 280 character limit).
twitterShareHashtagsArraystring[]Array of strings that should be marked as hashtags of the shared tweet.
copyShareButtonbooleantrueWhether or not to include the "Copy Link" (or 🔗) button in the result container.
copyShareLinkstring""The text that should be copied to the clipboard upon clicking the "Copy Link" (or 🔗) button.
onResultFunctionNOOPFunction called when the quiz is completed and a result is available.
onAnswerSelectionFunctionNOOPFunction called when any answer option is selected. The question's index, the answer's index, and the answer's associated result ID are available as parameters.
onRestartFunctionNOOPFunction called when the "Retake Quiz" button is clicked in the result container.
questionsArrayObject[]An array of question objects (see Question Object below).
resultsArrayObject[]An array of result objects (see Result Object below).

Question Object

BuzzFeedQuiz's questions prop accepts an array of question objects with key-value pairs formatted in the following manner:

Key NameValue TypeValue Description
questionstringThe text value of the question being asked.
questionRelativeToImagestringDetermines whether the question text's position overlaps a supplied image or whether the question text is displayed directly above the image (adjacently). Provide a value of "adjacent" to this key for the latter option, otherwise the text will overlap by default.
answerArrangementstringDetermines the spacial orientation of the answer choices. Assign this key a value of "row" for each answer text to be on a separate line (Note: only answer text will show - no background images or image attributions) or a value of "tile" for Buzzfeed's default tile configuration.
backgroundColorstringApplies a given color to only the specific question container's background. This value supersedes the generalBackgroundColor prop if supplied to ReactBuzzFeedQuiz. If the generalBackgroundColor prop is supplied and this value is empty, then the color in generalBackgroundColor will be applied. Otherwise, if no color or invalid color is supplied, the default background color is black.
fontColorstringApplies a given color to only the specific question container's font. This value supersedes the generalFontColor prop if supplied to ReactBuzzFeedQuiz. If the generalFontColor prop is supplied and this value is empty, then the color in generalFontColor will be applied. Otherwise, if no color or invalid color is supplied, the default font color is white.
backgroundImageSrcstringURL or local filename to be included as the background image of the question container. Automatically sets the font color to white and adds a text stroke. Takes precedence over any theme, if supplied.
imageAttributionstringIf background image is supplied, this will add attribution text below the image giving credit to its original source.
answersArrayObjectAn array of answer objects (see Answer Object below).

Answer Object

The value of the answers key of the Question Object accepts an array of answer objects with key-value pairs formatted in the following manner:

Key NameValue TypeValue Description
answerstringThe text value of the answer choice.
backgroundColorstringApplies a given color to only the specific answer container's background. This value supersedes the generalBackgroundColor prop if supplied to ReactBuzzFeedQuiz. If the generalBackgroundColor prop is supplied and this value is empty, then the color in generalBackgroundColor will be applied. Otherwise, if no color or invalid color is supplied, the default background color is black.
fontColorstringApplies a given color to only the specific answer container's font. This value supersedes the generalFontColor prop if supplied to ReactBuzzFeedQuiz. If the generalFontColor prop is supplied and this value is empty, then the color in generalFontColor will be applied. Otherwise, if no color or invalid color is supplied, the default font color is white.
backgroundImageSrcstringURL or local filename to be included as the background image of the answer container. Automatically sets the font color to white and adds a text stroke. Takes precedence over any theme, if supplied.
imageAttributionstringIf background image is supplied, this will add attribution text below the image giving credit to its original source.
onAnswerSelectionFunctionFunction called when this specific answer option is selected. The question's index, the answer's index, and the answer's associated result ID are available as parameters. This function value supersedes ReactBuzzFeedQuiz's onAnswerSelection prop, if supplied, for this specific answer choice.
resultIDnumberThe numerical ID of the answer's associated Result Object (see below) found in ReactBuzzFeedQuiz's results prop.
revealResponseObjectTriggers a response message for this specific answer below the associated question and its answer options (see Response Object below).

Response Object

BuzzFeedQuiz's Answer Objects accept an optional revealResponse key object with key-value pairs formatted in the following manner:

Key NameValue TypeValue Description
titlestring or JSX elementThe heading value of the associated answer response.
descriptionstring or JSX elementThe lower sub-heading value of the associated answer response.
imagestringURL or local filename to be included in the answer response image's src attribute.
imageAttributionstringThis will add attribution text below the answer response image giving credit to the image's original source.

Result Object

BuzzFeedQuiz's results prop accepts an array of result objects with key-value pairs formatted in the following manner:

Key NameValue TypeValue Description
titlestringThe heading text value of the associated result.
descriptionstringThe text value of the sub-heading result description.
resultImageSrcstringURL or local filename to be included in the result container image's src attribute.
imageAttributionstringThis will add attribution text below the result description giving credit to the result container image's original source.
onResultFunctionFunction called when the quiz is completed and the outcome is this specific result. This function value supersedes ReactBuzzFeedQuiz's onResult prop, if supplied, for this specific result outcome.
resultIDnumberThe numerical ID of the Result Object. Used to associate Result Objects to Answer Objects.

Contributing

Contributions are welcome!

  1. Fork the project.
  2. Create your feature branch (git checkout -b feature/MyFeature).
  3. Commit your changes (git commit -m 'Add my feature').
  4. Push to the branch (git push origin feature/MyFeature).
  5. Open a pull request.

License

Distributed under the MIT License. See LICENSE.txt for more information.

2.0.7

2 years ago

2.0.6

2 years ago

2.0.5

2 years ago

2.0.4

3 years ago

2.0.3

3 years ago

2.0.2

3 years ago

2.0.1

3 years ago

2.0.0

3 years ago

1.0.11

3 years ago

1.0.10

3 years ago

1.0.9

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago