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

Save Automatically?

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

              
                @import url("https://fonts.googleapis.com/css?family=Nunito+Sans:400,800");

body {
  font-family: "Nunito Sans", sans-serif;
  background: #f9f9f9;
}

.Cart {
  max-width: 800px;
  display: block;
  margin: auto;
  font-family: "Nunito Sans", sans-serif;
  font-weight: 800;
}

.Cart ul {
  list-style-type: none;
}

.Cart li {
  max-width: 400px;
  padding-top: 10px;
  margin-bottom: 30px;
}

.Cart li, .Cart button {
  margin-right: 10px;
}

.Cart button {
  cursor: pointer;
  float: right;
  border: none;
  border-radius: 4px;
  background: #D8D8D8;
  box-shadow: 0px 2px 9px rgba(0, 0, 0, 0.2);
  padding: 5px;
  transform: translatey(-5px);
}

.cartTotal {
  position: fixed;
  right: 0;
  bottom: 0;
  color: #0e5c2c;
  font-family: "Nunito Sans", sans-serif;
  font-weight: 800;
  font-size: 48px;
  background: #a2bb98;
  border: 10px solid #0e5c2c;
  box-shadow: 0px 2px 9px rgba(0, 0, 0, 0.2);
  border-radius: 6px 0 0 0;
  padding: 10px 20px;
}

.cartColor {
  min-width: 90px;
  padding: 10px 20px;
  border-radius: 4px;
  box-shadow: 0px 2px 9px rgba(0, 0, 0, 0.2);
}

.cartColorQty,
.cartColorImport {
  width: 80px;
  float: right;
}

.wall {
  /* columns:4;
  column-gap: 1em; */
  max-width: 800px;
  display: block;
  margin: auto;
  padding-top: 60px;
}

.brick {
  margin: 20px;
  height: 180px;
  width: 150px;
  margin-bottom: 1em;
  clear: both;
  background: #fff;
  box-shadow: 0px 2px 9px rgba(0, 0, 0, 0.2);
  transition: all 0.5s;
  border-radius: 6px;
  display: inline-block;
}

.brick:hover {
  box-shadow: 0px 15px 30px rgba(0, 0, 0, 0.2) !important;
}

.color {
  grid-area: color;
  position: relative;
  height: 120px !important;
}

.content {
  height: 100%;
  display: grid;
  grid-template-columns: 10px auto 10px;
  grid-template-areas:
    ". color ."
    ". action .";
}

.action {
  /* border: 1px solid red; */
  grid-area: action;

  display: grid;
  grid-template-columns: auto auto;
}

.action p {
  /* padding: 0; */
  font-weight: 800;
  margin: auto;
}

.buyButton {
  cursor: pointer;

  font-weight: 800;
  color: #fff;

  width: 68px;
  height: 25px;
  background: linear-gradient(270deg, #ff3d00 -13.97%, #ffa800 105.15%);
  box-shadow: 0px 2px 9px rgba(0, 0, 0, 0.2);
  border-radius: 6px;
  border: none;

  position: relative;
  top: 50%;
  left: 15%;
  transform: translateY(-50%);
  transition: box-shadow 0.2s;
}

.color h1 {
  position: absolute;
  z-index: 2;

  font-size: 46px;

  top: 50%;
  left: 50%;
  transform: translate(-40%, -100%);
}

.palette {
  /* border: 1px solid red; */
  position: absolute;
  height: 140px;
  width: 140px;
  left: 5px;
  top: -10px;

  background: #fdff8d;
  box-shadow: 0px 2px 9px rgba(0, 0, 0, 0.2);
  border-radius: 6px;
}

button:focus {
  outline: 0;
}

button:active {
  box-shadow: none;
}

.navBar {
  position: fixed;

  font-family: "Nunito Sans", sans-serif;
  font-weight: 800;
  text-align: right;
  right: 0;
  top: 0;
  background: #fff;
  z-index: 999;

  box-shadow: 0px 15px 30px rgba(0, 0, 0, 0.2);
  border-radius: 0 0 0 6px;
}

.navBar li {
  position: relative;
  display: inline-block;
  padding-right: 50px;
  cursor: pointer;
}

.navBar li:hover {
  font-style: italic;
}

.cartQuantity {
  padding: 0px 5px;
  border-radius: 6px;
  color: #000;
  background: linear-gradient(270deg, #ff3d00 -13.97%, #ffa800 105.15%);
}

              
            
!

JS

              
                const { Component } = React;

// Empty bazar store is initialized on first import.
const { register, poke } = bazar;

const colors = [
  { name: "Yellow", hex: "#FDFF8D", text: "black", price: 615 },
  { name: "Wine", hex: "#4D1310", text: "white", price: 258 },
  { name: "Punch", hex: "#EF5678", text: "black", price: 493 },
  { name: "Blue", hex: "#4342B6", text: "white", price: 807 },
  { name: "Green", hex: "#3CAF49", text: "black", price: 105 },
  { name: "Peach", hex: "#F99484", text: "black", price: 752 },
  { name: "Olive", hex: "#95BE67", text: "black", price: 60 },
  { name: "Rose", hex: "#DF3034", text: "white", price: 810 },
  { name: "Mint", hex: "#97ECC2", text: "black", price: 739 },
  { name: "Lilac", hex: "#B560CA", text: "white", price: 974 },
  { name: "Oil", hex: "#0D0702", text: "white", price: 153 },
  { name: "Teal", hex: "#4DA8AB", text: "white", price: 156 },
  { name: "Slate", hex: "#757A86", text: "white", price: 244 },
  { name: "Blush", hex: "#FEC4E4", text: "black", price: 981 },
  { name: "Sky", hex: "#67C3D8", text: "white", price: 239 },
  { name: "Wood", hex: "#413323", text: "white", price: 343 },
  { name: "Iris", hex: "#9865C2", text: "white", price: 56 },
  { name: "Smoke", hex: "#5A525E", text: "black", price: 902 },
  { name: "Azure", hex: "#2920A2", text: "white", price: 433 },
  { name: "Basil", hex: "#356132", text: "white", price: 743 }
];

const Item = props => {
  const { name, price, hex, text } = props.color;

  const buy = () =>
    poke("App", {
      name,
      type: "increment"
    });

  return (
    <div className="brick">
      <div className="content">
        <div className="color" style={{ color: text }}>
          <div className="palette" style={{ background: hex }} />
          <h1>{name}</h1>
        </div>
        <div className="action">
          <p>$ {price}</p>
          <button className="buyButton" onClick={() => buy()}>
            Buy
          </button>
        </div>
      </div>
    </div>
  );
};

const Shop = props => {
  const items = () => {
    return props.colors.map((color, index) => {
      return <Item color={color} key={index} />;
    });
  };

  return <div className="wall">{items()}</div>;
};

const Cart = props => {
  let total = 0;
  const purchases = props.items.map((item, index) => {
    const { name, hex, price, quantity } = item;
    const currentImport = quantity * price;
    total += currentImport;
    return (
      <li key={index}>
        <span className="cartColor" style={{ background: hex }}>{name}</span>
        <span className="cartColorQty"> x{item.quantity}</span>
        <button onClick={() => poke("App", { name, type: "increment" })}>
          ➕
        </button>
        <button onClick={() => poke("App", { name, type: "decrement" })}>
          ➖
        </button>
        <span className="cartColorImport"> ${currentImport}</span>
        <button onClick={() => poke("App", { name, type: "remove" })}>🗑</button>
      </li>
    );
  });

  return (
    <div className="Cart">
      <ul>{purchases.length === 0 ? "empty (:" : purchases}</ul>
      <div className="cartTotal">$ {total}</div>
    </div>
  );
};

const Navbar = props => (
  <div className="navBar">
    <ul>
      <li onClick={() => props.changeView("shop")}>
        <span role="img" aria-label="shop">
          🏪{" "}
        </span>
        <span>Shop</span>
      </li>
      <li onClick={() => props.changeView("cart")}>
        <span role="img" aria-label="shop">
          🛒{" "}
        </span>
        <span>Cart </span>
        <span className="cartQuantity">{props.quantity}</span>
      </li>
    </ul>
  </div>
);

class App extends Component {
  constructor(props) {
    super(props);
    this.state = {
      colors: colors.map(e => ({
        ...e,
        quantity: 0
      }))
    };

    register({
      id: "App",
      sync: () => this.state,
      onPoke: arg => {
        const { name, type } = arg;
        console.log('poked', name);
        const { colors } = this.state;
        this.setState({
          colors: colors.map(e => {
            const { quantity } = e;
            return e.name === name
              ? {
                  ...e,
                  quantity: (() => {
                    if (type === "increment") return quantity + 1;
                    else if (type === "remove") return 0;
                    return quantity - 1;
                  })()
                }
              : e;
          })
        });
      }
    });

    this.changeView = this.changeView.bind(this);
  }

  changeView = where => this.setState({ view: where });

  render() {
    const { view = "shop", colors } = this.state;

    const quantity = colors.map(e => e.quantity).reduce((a, b) => a + b);

    const inCart = colors.filter(e => e.quantity > 0);

    return (
      <div className="App">
        <Navbar changeView={this.changeView} quantity={quantity} />
        {view === "shop" ? <Shop colors={colors} /> : <Cart items={inCart} />}
      </div>
    );
  }
}

ReactDOM.render(<App />, document.getElementById("root"));

              
            
!
999px

Console