Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div id="root"></div>
              
            
!

CSS

              
                /**
 * Credit
 * counter desgin is from dribbble (https://dribbble.com/shots/5539678-Stepper-VI)
 * chevron icons are from font-awesome (https://fontawesome.com/icons/chevron-down?style=solid)
**/
html,
body {
  margin: 0;
  padding: 0;
  height: 100%;
  width: 100%;
  background-color: #f26072;
  display: flex;
  align-items: center;
  justify-content: center;
}

.container {
  display: flex;
  align-items: center;
  flex-direction: column;
  min-width: 200px;
}

.number {
  font-size: 72px;
  color: #fbf5f5;
  font-family: Verdana;
  line-height: 1.75em;
  text-align: center;
  user-select: none;
}

.chevron {
  background: transparent center center;
  background-repeat: no-repeat;
  cursor: pointer;
  width: 70px;
  height: 70px;
  transition: background-image 0.3s;
}

.chevron-up {
  background-image: url("data:image/svg+xml, %3Csvg aria-hidden='true' focusable='false' role='img' xmlns='http://www.w3.org/2000/svg' viewBox='0 0 448 512'%3E%3Cpath fill='%23f6909c' d='M240.971 130.524l194.343 194.343c9.373 9.373 9.373 24.569 0 33.941l-22.667 22.667c-9.357 9.357-24.522 9.375-33.901.04L224 227.495 69.255 381.516c-9.379 9.335-24.544 9.317-33.901-.04l-22.667-22.667c-9.373-9.373-9.373-24.569 0-33.941L207.03 130.525c9.372-9.373 24.568-9.373 33.941-.001z'%3E%3C/path%3E%3C/svg%3E");
}

.chevron-up:hover {
  background-image: url("data:image/svg+xml, %3Csvg aria-hidden='true' focusable='false' role='img' xmlns='http://www.w3.org/2000/svg' viewBox='0 0 448 512'%3E%3Cpath fill='%23fbf5f5' d='M240.971 130.524l194.343 194.343c9.373 9.373 9.373 24.569 0 33.941l-22.667 22.667c-9.357 9.357-24.522 9.375-33.901.04L224 227.495 69.255 381.516c-9.379 9.335-24.544 9.317-33.901-.04l-22.667-22.667c-9.373-9.373-9.373-24.569 0-33.941L207.03 130.525c9.372-9.373 24.568-9.373 33.941-.001z'%3E%3C/path%3E%3C/svg%3E");
}

.chevron-down {
  background-image: url("data:image/svg+xml,%3Csvg aria-hidden='true' focusable='false' xmlns='http://www.w3.org/2000/svg' viewBox='0 0 448 512'%3E%3Cpath fill='%23f6909c' d='M207.029 381.476L12.686 187.132c-9.373-9.373-9.373-24.569 0-33.941l22.667-22.667c9.357-9.357 24.522-9.375 33.901-.04L224 284.505l154.745-154.021c9.379-9.335 24.544-9.317 33.901.04l22.667 22.667c9.373 9.373 9.373 24.569 0 33.941L240.971 381.476c-9.373 9.372-24.569 9.372-33.942 0z'%3E%3C/path%3E%3C/svg%3E");
}

.chevron-down:hover {
  background-image: url("data:image/svg+xml,%3Csvg aria-hidden='true' focusable='false' xmlns='http://www.w3.org/2000/svg' viewBox='0 0 448 512'%3E%3Cpath fill='%23fbf5f5' d='M207.029 381.476L12.686 187.132c-9.373-9.373-9.373-24.569 0-33.941l22.667-22.667c9.357-9.357 24.522-9.375 33.901-.04L224 284.505l154.745-154.021c9.379-9.335 24.544-9.317 33.901.04l22.667 22.667c9.373 9.373 9.373 24.569 0 33.941L240.971 381.476c-9.373 9.372-24.569 9.372-33.942 0z'%3E%3C/path%3E%3C/svg%3E");
}

              
            
!

JS

              
                const { useState } = React;

const counters = Array.from({ length: 14 }, (_, index) => index);

// STEP 2:透過 props 取得外層傳入的資料
const Counter = props => {
  // STEP 3: 使用解構賦值把需要的資料從 props 中取出
  const { startingValue, minNumber, maxNumber } = props;
  
  // STEP 4: 把 startingValue 當作 count 的預設值放入 useState 的參數中
  const [count, setCount] = useState(startingValue);

  const handleIncrement = () => setCount(count + 1);
  const handleDecrement = () => setCount(count - 1);

  return (
    <div className="container">
      <div
        className="chevron chevron-up"
        onClick={handleIncrement}
        // STEP 5: 把 maxNumber 帶入判斷式
        style={{
          visibility: count >= maxNumber && "hidden"
        }}
      />

      <div className="number">{count}</div>

      <div
        className="chevron chevron-down"
        onClick={handleDecrement}
        // STEP 6: 把 minNumber 帶入判斷式
        style={{
          visibility: count <= minNumber && "hidden"
        }}
      />
    </div>
  );
};

ReactDOM.render(
  <div
    style={{
      display: "flex",
      flexWrap: "wrap"
    }}
  >
    {/* counters.map(item => <Counter />) */}
    {/* STEP 1:將需要的資料透過 props 傳入 */}
    <Counter startingValue={5} minNumber={1} maxNumber={10} />
    <Counter startingValue={15} minNumber={11} maxNumber={20} />
    <Counter startingValue={25} minNumber={21} maxNumber={30} />
    <Counter startingValue={35} minNumber={31} maxNumber={40} />
    <Counter startingValue={45} minNumber={41} maxNumber={50} />
  </div>,
  document.getElementById("root")
);

              
            
!
999px

Console