3.0.0 • Published 1 year ago

react-quizlet-flashcard v3.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

A simple and responsive quizlet-like flashcard component with a few additional options.

Front and back card accepts html strings and JSX elements!

react-quizlet-flashcardQuizlet's flashcard component
npm.ionpm.io

Installation

yarn add react-quizlet-flashcard
npm i react-quizlet-flashcard

NOTE: All basic card styles like padding, border radius, font, font size and flex alignment for card content has been removed from V3.0.0 to make it more customizable. You can add your own styles to the card using various style props in both <Flashcard /> and <FlashcardArray /> components or by defining the styles in cards array as mentioned below.

Basic Usage

import { FlashcardArray } from "react-quizlet-flashcard";

function App() {
  const cards = [
    {
      id: 1,
      front: "What is the capital of <u>Alaska</u>?",
      back: "Juneau",
      frontChild: <div>Hello there</div>,
      backChild: <p>This is a back child</p>,
    },
    {
      id: 2,
      front: "What is the capital of California?",
      back: "Sacramento",
    },
    {
      id: 3,
      front: "What is the capital of New York?",
      back: "Albany",
    },
    {
      id: 4,
      front: "What is the capital of Florida?",
      back: "Tallahassee",
    },
    {
      id: 5,
      front: "What is the capital of Texas?",
      back: "Austin",
    },
    {
      id: 6,
      front: "What is the capital of New Mexico?",
      back: "Santa Fe",
    },
    {
      id: 7,
      front: "What is the capital of Arizona?",
      back: "Phoenix",
    },
  ];
  return (
    <div>
      <FlashcardArray cards={cards} />
    </div>
  );
}

Available Props for <FlashcardArray />

PropTypedefault
*cardsarrayNone
controlsbooleantrue
forwardRefObj() => {}
showCountbooleantrue
frontCardStyleReact.CSSProperties{}
frontContentStyleReact.CSSProperties{}
backCardStyleReact.CSSProperties{}
backContentStyleReact.CSSProperties{}
FlashcardArrayStyleReact.CSSProperties{}
onCardChangefunc(id: any, index: number) => {}
onCardFlipfunc(id: any, index: number, state: boolean) => {}
currentCardFlipRefReact.MutableRefObjectNone
cyclebooleanfalse

Available props for <Flashcard />

PropTypedefault
*frontHTMLstring | JSX.ElementNone
frontCardStyleReact.CSSPropertiesNone
frontContentStyleReact.CSSPropertiesNone
backHTMLstring | JSX.ElementNone
backCardStyleReact.CSSPropertiesNone
backContentStyleReact.CSSPropertiesNone
classNamestring""
heightstringNone
widthstringNone
borderRadiusstring1rem
styleReact.CSSPropertiesNone
onCardFlip(state: boolean) => voidNone
manualFlipRefReact.MutableRefObject{ current: null }

Possible keys for each object in cards array

KeyType
*idnumber
*frontHTMLstring | JSX.Element
*backHTMLstring | JSX.Element
frontCardStyleReact.CSSProperties
frontContentStyleReact.CSSProperties
backCardStyleReact.CSSProperties
backContentStyleReact.CSSProperties
classNamestring
heightstring
widthstring
borderRadiusstring
styleReact.CSSProperties

Examples

Basic Flashcard:

import { Flashcard } from "react-quizlet-flashcard";

function App() {
  return (
    <div className="storyContainer">
      <Flashcard frontHTML="<h1>Front</h1>" backHTML={<h1>Back</h1>} />
    </div>
  );
}

Manual flip using ref:

You can use this when you want to add buttons or other intractable elements to flip the card content.

import { Flashcard } from "react-quizlet-flashcard";
import { useRef } from "react";

function App() {
  const flipRef = useRef();

  return (
    <div className="storyContainer">
      <Flashcard
        frontHTML="<h1>Front</h1>"
        backHTML={<h1>Back</h1>}
        manualFlipRef={flipRef}
      />
      <button onClick={() => flipRef.current()}>Flip</button>
    </div>
  );
}

Custom Styles for front and back content

import { Flashcard } from "react-quizlet-flashcard";

function App() {
  return (
    <div className="storyContainer">
      <Flashcard
        frontHTML={
          <>
            <span>1</span>
            <span>2</span>
            <span>3</span>
            <span>4</span>
            <span>5</span>
            <span>6</span>
            <span>7</span>
            <span>8</span>
            <span>9</span>
          </>
        }
        backHTML={<h1>Back</h1>}
        backContentStyle={{
          backgroundColor: "red",
          color: "white",
          padding: "10px",
          display: "flex",
          justifyContent: "center",
          alignItems: "center",
        }}
        frontContentStyle={{
          backgroundColor: "turquoise",
          color: "white",
          display: "grid",
          gridTemplateColumns: "repeat(3, 1fr)",
          gridTemplateRows: "repeat(3, 1fr)",
          fontSize: "2rem",
        }}
      />
    </div>
  );
}

Card flip callback

import { Flashcard } from "react-quizlet-flashcard";

function App() {
  return (
    <div className="storyContainer">
      <Flashcard
        frontHTML="<h1>Check console</h1>"
        backHTML={<h1>Back</h1>}
        onCardFlip={(state) => {
          if (state) console.log("Card is flipped");
          else console.log("Card is not flipped");
        }}
      />
    </div>
  );
}

Custom Card Size

import { Flashcard } from "react-quizlet-flashcard";

function App() {
  return (
    <div className="storyContainer">
      <Flashcard
        frontHTML="<h1>Front</h1>"
        backHTML={<h1>Back</h1>}
        style={{ width: "300px", height: "300px" }}
      />
    </div>
  );
}

Basic FlashcardArray:

import { FlashcardArray } from "react-quizlet-flashcard";

function App() {
  const cards = [...]
  return (
    <div className="storyContainer">
      <FlashcardArray cards={cards} />
    </div>
  );
}

Cards with custom controls(Using forwardRef prop):

import { FlashcardArray } from "react-quizlet-flashcard";
import { useRef } from "react";

function App() {
  const controlRef = useRef({}); // {} should definitely be passed to useRef for it to work
  const currentCardFlipRef = useRef(); // nothing should be passed to useRef for it to work
  const [currentCard, setCurrentCard] = useState(1);

  return (
    <div className="storyContainer">
      <FlashcardArray
        cards={deck.cards}
        controls={false}
        showCount={false}
        forwardRef={controlRef}
        currentCardFlipRef={currentCardFlipRef}
        onCardChange={(id, index) => {
          setCurrentCard(index);
        }}
      />
      <p>
        {currentCard} / {deck.cards.length}
      </p>
      <button onClick={() => controlRef.current.prevCard()}>Prev</button>
      <button onClick={() => controlRef.current.resetArray()}>Reset</button>
      <button onClick={() => controlRef.current.nextCard()}>Next</button>
      <button onClick={() => currentCardFlipRef.current()}>Flip</button>
    </div>
  );
}

Custom styles for all cards in the array:

import { FlashcardArray } from "react-quizlet-flashcard";

function App() {
  cards = [...]
  return (
    <div className="storyContainer">
      <FlashcardArray
        cards={cards}
        frontContentStyle={{
          backgroundColor: "lightgoldenrodyellow",
          color: "black",
        }}
        backContentStyle={{
          backgroundColor: "turquoise",
        }}
      />
    </div>
  );
}

Custom style for each card:

You can set style for each card through the card object. Refer to prop list of Card object above. Instead, you can also pass another react component with custom style into cards.

import { FlashcardArray } from "react-quizlet-flashcard";

function App() {
  return (
    <div className="storyContainer">
      <FlashcardArray
        cards={[
          {
            id: 1,
            frontHTML: (
              <>
                <span style={{ backgroundColor: "lawngreen" }}>Option 1</span>
                <span style={{ backgroundColor: "lawngreen" }}>Option 2</span>
                <span style={{ backgroundColor: "lawngreen" }}>Option 3</span>
              </>
            ),
            backHTML: "Juneau",
            options: ["Juneau", "Anchorage", "Fairbanks"],
            frontContentStyle: {
              backgroundColor: "lightgoldenrodyellow",
              color: "black",
              display: "grid",
              gridTemplateColumns: "1fr 1fr 1fr",
              gridTemplateRows: "1fr",
              gap: "10px",
              padding: "10px",
            },
          },
          {
            id: 2,
            frontHTML: (
              <>
                <span style={{ backgroundColor: "pink" }}>Option 1</span>
                <span style={{ backgroundColor: "pink" }}>Option 2</span>
                <span style={{ backgroundColor: "pink" }}>Option 3</span>
              </>
            ),
            backHTML: "Sacramento",
            options: ["Sacramento", "Los Angeles", "San Francisco"],
            frontContentStyle: {
              backgroundColor: "lightgoldenrodyellow",
              color: "black",
              display: "grid",
              gridTemplateColumns: "1fr",
              gridTemplateRows: "1fr 1fr 1fr",
              gap: "10px",
              padding: "10px",
            },
          },
        ]}
      />
    </div>
  );
}

🤝 Contributing

Contributions, issues and feature requests are welcome! Feel free to check issues page.

Give a ⭐️ if this project helped you!

To-Do:

  • Write the component with typescript.
  • Write Unit tests.
  • More finer control.
  • Write the styles with Sass
3.0.0

1 year ago

1.2.0

2 years ago

1.2.1

2 years ago

2.0.1

2 years ago

2.0.0

2 years ago

1.1.3

2 years ago

1.1.2

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago