Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ 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

              
                #app
              
            
!

CSS

              
                * {
  box-sizing: border-box;
  touch-action: none;
  font-family: monospace;
}

:root {
  --green-one: hsl(130, 64%, 76%);
  --green-two: hsl(136, 53%, 51%);
  --green-three: hsl(136, 54%, 41%);
  --green-four: hsl(139, 54%, 28%);
  --cell-radius: 2;
  --grey: hsl(216, 14%, 93%);
  --color: hsl(0, 0%, 40%);
  --shirt: var(--green-one);
  --shirt-shade: var(--green-two);
  --skin: hsl(0, 30%, 95%);
  --skin-shade: hsl(0, 20%, 80%);
  --stroke: hsl(0, 0%, 20%);
  --laptop: hsl(0, 0%, 78%);
  --laptop-main: #4d4d4c;
  --laptop-shade: rgba(58,58,59,0.5);
  --laptop-badge: #fff;
  --beard: #bf8040;
  --beard: #ae6113;
  --beard-light: #d9b38c;
  --beard-light: #ea943e;
  --eyes: var(--stroke);
  --strap: #bf4040;
  --strap: var(--green-four);
  --cheeks: #df9f9f;
  --cap: #404040;
  --sparks: rgba(255,255,153,0.85);
  --bulb: rgba(255,255,153,0.85);
  --filament: #000;
  --screw: #ccc;
  --bg: #6e7c91;
  --owl-1: #feec90;
  --owl-2: #c6e472;
  --owl-3: #f88a68;
  --owl-4: #80cbc5;
  --owl-5: #c793ec;
  --padding: 16px;
}

body {
  font-family: monospace;
  padding: 0;
  margin: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  color: hsl(0, 0%, 15%);
  background-color: hsl(0, 0%, 100%);
  min-height: 100vh;
  overflow: hidden;
}

ul {
  padding: 0 0 0 var(--padding);
  margin: 0;
}

ul > li + li {
  margin-top: var(--padding);
}

.submit-button {
  width: 100%;
  height: 44px;
  border-radius: 6px;
  background: var(--green-two);
  color: #fff;
  font-weight: bold;
  font-size: 1.25rem;
  border: none;
  transition: background 0.2s, box-shadow 0.2s, transform 0.2s;
  box-shadow: 4px 4px 0px -2px hsla(0, 0%, 0%, 0.25);

  &:hover {
    background: var(--green-three);
  }

  &:active {
    background: var(--green-four);
    box-shadow: 0px 0px 0px 0px hsla(0, 0%, 0%, 1);
    transform: translate(2px, 2px);
  }
}


#app {
  min-height: 100vh;
  width: 100vw;
  overflow: auto;
  display: flex;
  align-items: start;
  justify-content: center;
  flex-direction: column;

  @media(min-width: 768px) {
    align-items: center;
  }
}

button {
  cursor: pointer;
}

.canvas {
  padding: var(--padding);
}

.icon-button {
  height: 44px;
  width: 44px;
  border-radius: 50%;
  transition: background 0.2s;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 0;
  background: 0;

  &:hover {
    background: var(--grey);
  }

  &:active path {
    fill: var(--green-two);
  }

  path {
    fill: var(--green-four);
    transition: fill 0.2s;
  }

  svg {
    height: 24px;
    width: 24px;
  }

  &[disabled] {
    cursor: not-allowed;
    background: transparent;
    path {
      fill: var(--grey);
    }
  }
}

.audio-toggle {
  position: absolute;
  top: calc(44px + (2 * var(--padding)));
  left: var(--padding);
}

#audio {
  height: 0;
  width: 0;
  transform: scale(0);
}

select,
input {
  height: 44px;
  margin: 0;
  padding: 0 var(--padding);
}

select,
input,
button {
  outline: transparent;
}

select,
input {
  border: 2px solid var(--grey);
  border-radius: 6px;
  color: var(--color);
}

input::placeholder {
  color: hsl(0, 0%, 80%);
}

// Select styling
.select-wrapper {
  border: 2px solid var(--grey);
  border-radius: 6px;
  overflow: hidden;
  position: relative;

  &:after {
    content: "";
    height: 8px;
    width: 12px;
    background: var(--color);
    position: absolute;
    right: 9px;
    top: 50%;
    transform: translate(0, -30%);
    -webkit-clip-path: polygon(0 0, 100% 0, 50% 100%);
    clip-path: polygon(0 0, 100% 0, 50% 100%);
  }

  select {
    color: var(--color);
    -webkit-appearance: none;
    appearance: none;
    border: 0;
    padding: 0 30px 0 var(--padding);
    height: 40px;
  }
}

footer {
  padding: var(--padding);
}

a {
  color: var(--green-two);
}

a:active {
  color: var(--green-four);
}

.instructions-content > * + * {
  margin-top: var(--padding);
}

.toasts {
  position: fixed;
  bottom: var(--padding);
  right: var(--padding);
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  align-items: flex-end;
  z-index: 3;
}

.toasts > * + * {
  margin-top: var(--padding);
}

.toast {
  --blue: hsl(199, 94%, 87%);
  --blue-border: hsl(199, 94%, 67%);
  --blue-stroke: hsl(199, 94%, 37%);
  --green: hsl(130, 65%, 87%);
  --green-border: hsl(130, 65%, 57%);
  --green-stroke: hsl(130, 65%, 37%);
  --red: hsl(3, 70%, 89%);
  --red-border: hsl(3, 70%, 59%);
  --red-stroke: hsl(3, 70%, 39%);
  --button-size: 44px;
  border-radius: calc(var(--cell-radius) * 1px);
  border-width: calc(var(--cell-radius) * 1px);
  border-style: solid;
  border-color: var(--border);
  padding: var(--padding);
  padding-right: var(--button-size);
  position: relative;
  background: var(--bg);
  color: var(--stroke);

  &--SUCCESS {
    --bg: var(--green);
    --border: var(--green-border);
    --stroke: var(--green-stroke);
  }
  &--INFO {
    --bg: var(--blue);
    --border: var(--blue-border);
    --stroke: var(--blue-stroke);
  }
  &--ERROR {
    --bg: var(--red);
    --border: var(--red-border);
    --stroke: var(--red-stroke);
  }

  button {
    height: var(--button-size);
    width: var(--button-size);
    position: absolute;
    top: 50%;
    right: 0;
    padding: 0;
    transform: translate(0, -50%);
    border: 0;
    background: 0;
  }

  svg {
    stroke: var(--stroke);
    fill: var(--stroke);
  }
}
              
            
!

JS

              
                import T from 'https://cdn.skypack.dev/prop-types'
import React, { Fragment, useEffect, useState } from 'https://cdn.skypack.dev/react'
import { render } from 'https://cdn.skypack.dev/react-dom'
import { DateTime } from 'https://cdn.skypack.dev/luxon'
import gsap from 'https://cdn.skypack.dev/gsap'


const Mirror = (values) =>
  Object.freeze(values.reduce((keys, key) => ({ ...keys, [key]: key }), {}))

const TOASTS = Mirror(['INFO', 'SUCCESS', 'ERROR'])

const Close = () => <svg xmlns="http://www.w3.org/2000/svg" xmlnsXlink="http://www.w3.org/1999/xlink" version="1.1" width={20} height={20} viewBox="0 0 24 24"><path d="M19,6.41L17.59,5L12,10.59L6.41,5L5,6.41L10.59,12L5,17.59L6.41,19L12,13.41L17.59,19L19,17.59L13.41,12L19,6.41Z" /></svg>

const Toast = ({ created, message, type, onDismiss, autoDismiss }) => {
  useEffect(() => {
    let timer
    if (autoDismiss) {
      timer = setTimeout(onDismiss, autoDismiss)
    }
    return () => clearTimeout(timer)
  }, [autoDismiss, onDismiss])
  return (
    <div data-toast={created} className={`toast toast--${type}`}>
      {message}
      <button title="Dismiss" onClick={onDismiss}>
        <Close />
      </button>
    </div>
  )
}

Toast.propTypes = {
  created: T.string,
  message: T.string,
  type: T.oneOf([TOASTS.INFO, TOASTS.ERROR, TOASTS.SUCCESS]),
  onDismiss: T.func,
  autoDismiss: T.number,
}

const DISMISS = 3000

const Toasts = ({ toast: toastToAdd }) => {
  const [toast, setToast] = useState(null)
  const [remove, setRemove] = useState(null)
  const [toasts, setToasts] = useState([])
  const onDismiss = (toastTime) => () => {
    gsap.to(`[data-toast="${toastTime}"]`, {
      xPercent: 100,
      opacity: 0,
      onComplete: () => {
        setRemove(toastTime)
      },
    })
  }

  useEffect(() => {
    const createToast = (message, type, life = DISMISS) => {
      const TOAST_TIME = DateTime.local().toISO({ includeOffset: true })
      const TOAST = {
        created: TOAST_TIME,
        type,
        message,
        autoDismiss: life,
        onDismiss: onDismiss(TOAST_TIME),
      }
      return TOAST
    }
    if (toastToAdd) {
      const NEW_TOAST = createToast(
        toastToAdd.message,
        toastToAdd.type,
        toastToAdd.life
      )
      setToast(NEW_TOAST)
    }
  }, [toastToAdd])

  useEffect(() => {
    if (
      toast &&
      toasts.filter((t) => t.created === toast.created).length === 0
    ) {
      // Adds toasts into pile
      setToasts([...toasts, toast])
    }
  }, [toast, toasts])

  useEffect(() => {
    if (
      toasts.length &&
      toast &&
      toasts.filter((t) => t.created === toast.created).length !== 0
    ) {
      gsap.from(`[data-toast="${toast.created}"]`, {
        xPercent: 100,
        opacity: 0,
        onStart: () => setToast(null),
      })
    }
  }, [toast, toasts])

  useEffect(() => {
    if (remove) {
      setToasts([...toasts.filter((t) => t.created !== remove)])
      setRemove(null)
    }
  }, [remove, toasts])

  return (
    <div className="toasts">
      {toasts.map((t) => (
        <Toast
          key={t.created}
          onDismiss={t.onDismiss}
          message={t.message}
          autoDismiss={t.autoDismiss}
          created={t.created}
          type={t.type}
        />
      ))}
    </div>
  )
}

Toasts.propTypes = {
  toast: T.shape({
    created: T.string,
    message: T.string,
    type: T.oneOf(['ERROR', 'INFO', 'SUCCESS']),
  }),
}


const App = () => {
  const [toast, setToast] = useState({
            type: TOASTS.ERROR,
            message: 'There was an Error',
            life: 0,
          })
  const fireToast = () => {
    
    setToast({
      type: ['ERROR', 'INFO', 'SUCCESS'][Math.floor(Math.random() * 3)],
      message: 'Generic Message',
      life: Math.random() * 5000
    })
  }
return <Fragment><button onClick={fireToast}>Fire Random Toast</button><Toasts toast={toast} /></Fragment>
  
}

render(<App/>, document.querySelector('#app'))
              
            
!
999px

Console