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 class="ui-week">
  <div class="ui-day" data-day="sun">
    <div class="ui-day-box">
      <div class="ui-bg" data-flip-key="bg-sun"></div>
      <div class="ui-grab -left"></div>
      <div class="ui-day-name" data-flip-key="sun"></div>
      <div class="ui-grab -right"></div>
    </div>
  </div>
  <div class="ui-day" data-day="mon">
    <div class="ui-day-box">
      <div class="ui-bg" data-flip-key="bg-mon"></div>
      <div class="ui-grab -left"></div>
      <div class="ui-day-name" data-flip-key="mon"></div>
      <div class="ui-grab -right"></div>
    </div>
  </div>
  <div class="ui-day" data-day="tue">
    <div class="ui-day-box">
      <div class="ui-bg" data-flip-key="bg-tue"></div>
      <div class="ui-grab -left"></div>
      <div class="ui-day-name" data-flip-key="tue"></div>
      <div class="ui-grab -right"></div>
    </div>
  </div>
  <div class="ui-day" data-day="wed">
    <div class="ui-day-box">
      <div class="ui-bg" data-flip-key="bg-wed"></div>
      <div class="ui-grab -left"></div>
      <div class="ui-day-name" data-flip-key="wed"></div>
      <div class="ui-grab -right"></div>
    </div>
  </div>
  <div class="ui-day" data-day="thu">
    <div class="ui-day-box">
      <div class="ui-bg" data-flip-key="bg-thu"></div>
      <div class="ui-grab -left"></div>
      <div class="ui-day-name" data-flip-key="thu"></div>
      <div class="ui-grab -right"></div>
    </div>
  </div>
  <div class="ui-day" data-day="fri">
    <div class="ui-day-box">
      <div class="ui-bg" data-flip-key="bg-fri"></div>
      <div class="ui-grab -left"></div>
      <div class="ui-day-name" data-flip-key="fri"></div>
      <div class="ui-grab -right"></div>
    </div>
  </div>
  <div class="ui-day" data-day="sat">
    <div class="ui-day-box">
      <div class="ui-bg" data-flip-key="bg-sat"></div>
      <div class="ui-grab -left"></div>
      <div class="ui-day-name" data-flip-key="sat"></div>
      <div class="ui-grab -right"></div>
    </div>
  </div>
</div>

<div class="ui-alert"><span class="ui-alert-days"></span><span> days removed</span></div>
<div class="ui-trash">
  <div class="ui-trash-lid"></div>
  <div class="ui-ridges"></div>
  <div class="ui-sparks">
    <div class="ui-spark"></div>
    <div class="ui-spark"></div>
    <div class="ui-spark"></div>
  </div>
</div>

<div class="ui-selection"></div>
              
            
!

CSS

              
                $color-border: #9153FF;
$color-purple: #7960A7;
$color-box: #F7E3FF;
$color-text-dark: #7A61A7;

*, *:before, *:after {
  user-select: none;
}

body {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background-color: #F2F5F7;
  transform-style: preserve-3d;
  perspective: 1000px;
  perspective-origin: calc(var(--trash-x) * 1px) calc(var(--trash-y) * 1px);
  
  // &:before {
  //   content: attr(data-prev-state)' ➡ 'attr(data-state);
  //   position: absolute;
  //   top: 0;
  //   left: 0;
  // }
}

.ui-week {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  width: 70vw;
  height: 15vw;
  grid-column-gap: 1vw;
  flex-direction: row;
  justify-content: center;
  align-items: center;
  margin-bottom: 10vh;
}

%selected {
  color: $color-border;
  background: transparent;
  
  > .ui-bg {
    border-color: $color-border;
    background-color: $color-box;
  }
}

.ui-day {
  justify-self: stretch;
  align-self: stretch;
  background-color: #EAEDF4;
  border-radius: .25rem;
  grid-row: 1 / 2;
  
  @for $i from 1 through 7 {
    &:nth-child(#{$i}) {
      grid-column: var(--start, #{$i}) / var(--end, span 1);
    }
  }
  
  .ui-day-name {
    color: $color-purple;
    &:before {
      content: attr(data-flip-key);
    }
  }
  
  &[data-state="selected"] > .ui-day-box,
  [data-state="disposed"] &:hover .ui-day-box {
    @extend %selected;
  }
  
  &[data-state="selected"] > .ui-day-box {
    animation: selected .6s cubic-bezier(.5, 0, .5, 1);

    @keyframes selected {
      50% {
        transform: rotate(2deg);
      }
    }
  }
  
  [data-state="dragging"] &[data-state="selected"],
  [data-state="disposed"] &[data-state="selected"] {    
    > .ui-day-box {
      z-index: 2;
      transition-duration: 0s;
      transform:
        translateX(calc((var(--drag-x2) - var(--drag-x1)) * 1px))
        translateY(calc((var(--drag-y2) - var(--drag-y1)) * 1px + (3px - var(--yc) * 3px)));
    }
  }
  
  [data-state="disposed"] &[data-state="selected"] > .ui-day-box {
    transition-duration: .3s;
    transform:
        translateX(calc((var(--drag-x2) - var(--drag-x1)) * 1px))
        translateY(calc((var(--drag-y2) - var(--drag-y1)) * 1px))
      translateZ(-1000px);
  }
}

.ui-day-box {
  display: flex;
  justify-content: center;
  align-items: center;
  text-transform: uppercase;
  font-weight: bold;
  border-radius: inherit;
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  transition-duration: .3s;
  z-index: 1;
  pointer-events: none;
  
  > .ui-bg {
    background-color: #fff;
    border: 4px solid white;
    border-radius: inherit;
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    transition-duration: .3s;
    transition-property: border-color, background-color;
    
    &:before {
      content: '';
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      box-shadow: 0 .5rem 1.5rem rgba($color-border, 0.2);
      z-index: -1;
    }
  }
}

[data-state="disposed"],
[data-state="grabbing"] {
  .ui-day[data-state="selected"] > .ui-day-box {
    opacity: 0;
    pointer-events: none;
  }
  
  .ui-day[data-state="grabbed"] > .ui-day-box {
    @extend %selected;
  }
  
  .ui-day-box {
    pointer-events: auto;
  }
}

.ui-grab {
  position: absolute;
  top: 0;
  height: 100%;
  width: 1vw;
  
  &.-left {
    left: 0;
  }
  
  &.-right {
    right: 0;
  }
}

.ui-grab, [data-state="grabbing"] {
  cursor: ew-resize;
}

.ui-selection {
  color: #7DDAFC;
  background: rgba(#7DDAFC, 0.3);
  border: 2px solid currentColor;
  position: absolute;
  left: calc(var(--mx1) * 1px);
  top: calc(var(--my1) * 1px);
  width: calc((var(--mx2) - var(--mx1)) * 1px);
  height: calc((var(--my2) - var(--my1)) * 1px);
  transition: opacity .3s ease-in-out;
  pointer-events: none;
  z-index: 3;
  
  [data-state]:not([data-state="selecting"]) & {
    opacity: 0;
  }
}

.ui-alert {
  color: #FF62A3;
  text-transform: uppercase;
  font-weight: bold;
  font-size: 1rem;
  white-space: nowrap;
  text-align: center;
  opacity: 0;
  
  [data-state="disposed"][data-prev-state="dragging"] &  {
    animation: alert 1s cubic-bezier(0, .5, 0, 1) both;
  }
  @keyframes alert {
    from {
      opacity: 0;
    }
    50% {
      opacity: 1;
    }
    to {
      transform: translateY(-200%);
      opacity: 0;
    }
  }
}

.ui-trash {
  width: 5vw;
  height: 6vw;
  border: 2px solid $color-purple;
  border-bottom: none;
  border-top: none;
  background-image: linear-gradient(to right, white, white 60%, lighten($color-purple, 45%) 61%, lighten($color-purple, 45%));
  opacity: 0;
  transform: translateY(1vw);
  
  &, > * {
    transition-property: opacity, transform;
    transition-duration: .6s;
    trasition-timing-function: cubic-bezier(.5, 0, .5, 1);
  }
  
  z-index: 10;
  
  [data-state="dragging"] & {
    opacity: 1;
    transform: none;
    
    &:hover {
      transform: rotate(-10deg);
      
      > .ui-trash-lid {
        transform: rotate(25deg);
      }
    }
  }
  
  [data-state="disposed"] & {
    transition-duration: 1s;
    opacity: 0;
  }
  
  &:before, &:after {
    content: '';
    position: absolute;
    width: calc(100% + 4px);
    left: -2px;
    height: 1vw;
    border: 2px solid $color-purple;
    border-bottom-left-radius: 100% 100%;
    border-bottom-right-radius: 100% 100%;
    background-image: inherit;
  }
  
  &:before {
    top: 100%;
    border-top: none;
    border-bottom-left-radius: 100% 100%;
    border-bottom-right-radius: 100% 100%;
  }
  &:after {
    bottom: calc(100% - .5vw);
    border-top-left-radius: 100% 100%;
    border-top-right-radius: 100% 100%;
  }
  
  > .ui-ridges {
    height: 70%;
    width: 2px;
    background-color: $color-purple;
    position: absolute;
    left: calc(50% - 1px);
    top: 25%;
    pointer-events: none;
    
    &:before, &:after {
      content: '';
      height: 100%;
      width: 100%;
      position: absolute;
      background-color: inherit;
    }
    &:before {
      left: -1.5vw;
      transform: rotate(-2deg);
    }
    &:after {
      left: 1.5vw;
      transform: rotate(2deg);
    }
  }
  
  .ui-spark {
    height: 6px;
    width: 6px;
    border-radius: 50%;
    background-color: $color-purple;
    position: absolute;
    top: -1vw;
    left: -1vw;
    opacity: 0;
    transition: transform 1s, opacity .1s;
    
    [data-state="dragging"] & {
      opacity: 0;
    }
    [data-state="disposed"] & {
      opacity: 1;
      transform: translate(-2vw, -1vw) scale(0);
      
      &:nth-child(2) {
        transform: translate(-2vw, -3vw) scale(0);
      }
      &:nth-child(3) {
        transform: translate(2vw, -1vw) scale(0);
      }
    }
    
    &:nth-child(2) {
      top: -1vw;
      left: 1vw;
    }
    &:nth-child(3) {
      top: -.5vw;
      left: 5vw;
    }
  }
}

.ui-trash-lid {
  position: absolute;
  bottom: calc(100% - .5vw);
  height: 1vw;
  width: 120%;
  left: -10%;
  border: 2px solid $color-purple;
  border-bottom-left-radius: 100% 100%;
  border-bottom-right-radius: 100% 100%;
  border-top-left-radius: 100% 100%;
  border-top-right-radius: 100% 100%;
  background-color: white;
  z-index: 1;
  transform-origin: 120% center;
  
  &:before, &:after {
    content: '';
    position: absolute;
    border: inherit;
    background-color: inherit;
  }
  
  &:before {
    width: 80%;
    height: 1vw;
    left: 10%;
    bottom: calc(100% - .4vw);
    border-radius: inherit;
  }
  
  &:after {
    width: 50%;
    left: 25%;
    height: 1.2vw;
    border-top-left-radius: 50% 100%;
    border-top-right-radius: 50% 100%;
    bottom: 107%;
    border-bottom: none;
    background-color: transparent;
  }
}

html, body {
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 0;
}

*, *:before, *:after {
  box-sizing: border-box;
  position: relative;
}
              
            
!

JS

              
                console.clear();

// safarIE
if (!('animate' in Element.prototype)) {
  Element.prototype.animate = () => {};
}

const dayEls = Array.from(document.querySelectorAll(".ui-day"));
const trashEl = document.querySelector(".ui-trash");
const rect = el => el.getBoundingClientRect();
const trashRect = rect(trashEl);
const center = el => {
  const elRect = rect(el);
  return [elRect.left + elRect.width / 2, elRect.top + elRect.height / 2];
};
const styleVars = (vars, el = document.documentElement) => {
  Object.keys(vars).forEach(key => {
    el.style.setProperty(`--${key}`, vars[key]);
  });
};

styleVars({
  "trash-x": trashRect.left,
  "trash-y": trashRect.top
});

// pretend it's redux, okay?
const extState = {
  selected: [],
  x1: 0,
  y1: 0,
  x2: 0,
  y2: 0,
  drag: {
    x1: 0,
    y1: 0,
    x2: 0,
    y2: 0
  },
  grabIndex: undefined,
  stretch: {},
  centers: dayEls.map(center)
};

const startSelection = (s, e) => ({
  x1: event.clientX,
  y1: event.clientY,
  x2: event.clientX,
  y2: event.clientY,
  selected: []
});

const updateSelection = (s, e) => {
  const mx1 = Math.min(s.x1, e.clientX);
  const mx2 = Math.max(s.x1, e.clientX);
  const my1 = Math.min(s.y1, e.clientY);
  const my2 = Math.max(s.y1, e.clientY);

  return {
    mx1,
    mx2,
    my1,
    my2,
    x2: e.clientX,
    y2: e.clientY,
    selected: extState.centers
      .map((center, i) => ({
        center,
        i
      }))
      .filter(({ center: [x, y], i }) => {
        return x > mx1 && x < mx2 && y > my1 && y < my2;
      })
      .map(({ i }) => i)
  };
};
 
const startDrag = (s, e) => ({
  drag: {
    type: e.type,
    index: e.index,
    x1: e.clientX,
    y1: e.clientY,
    x2: e.clientX,
    y2: e.clientY
  }
});

const drag = (s, e) => ({
  drag: {
    ...s.drag,
    x2: e.clientX,
    y2: e.clientY
  }
});

const isNotTrashElement = (s, e) => e.target !== trashEl;
const initGrab = (s, e) => ({
  grabIndex: e.index,
  stretchIndex: e.index
});
const updateGrab = (s, e) => {
  const { clientX } = e;
  const stretchIndexes = s.centers
    .map((c, i) => ({ c, index: i }))
    .filter(({ c: [x] }, i) => {
      return (
        s.selected.includes(i) &&
        ((s.grabIndex < i && clientX > x) ||
          (s.grabIndex > i && clientX < x)) &&
        Object.keys(s.stretch).every(key => {
          if (+key === +s.grabIndex) return true;

          const [min, max] = s.stretch[key];
          return key < s.grabIndex ? i > max : i < min;
        })
      );
    })
    .map(stretch => stretch.index);

  const start = Math.min(s.grabIndex, ...stretchIndexes);
  const end = Math.max(s.grabIndex, ...stretchIndexes);

  const prevRange = s.stretch[s.grabIndex];

  if (prevRange && prevRange[0] === start && prevRange[1] === end) {
    return;
  }

  flippingStyleVars(
    {
      start: start + 1,
      end: end + 2
    },
    dayEls[s.grabIndex]
  );

  return {
    stretch: {
      ...s.stretch,
      [s.grabIndex]: [start, end]
    }
  };
};
const showDaysRemoved = (s, e) => {
  const {selected} = s;
  const numDays = selected.length;
  
  document.querySelector('.ui-alert-days').innerHTML = numDays;
}

const resetGrab = () => ({ grabIndex: undefined });

const machine = xstate.Machine({
  initial: "start",
  states: {
    start: {
      on: {
        mousedown: {
          selecting: {
            actions: [startSelection]
          }
        }
      }
    },
    selecting: {
      on: {
        mouseup: "selected",
        mousemove: {
          selecting: {
            actions: [updateSelection]
          }
        }
      }
    },
    selected: {
      on: {
        mousedown: { selecting: { actions: [startSelection] } },
        drag: {
          dragging: {
            actions: [startDrag]
          }
        }
      }
    },
    dragging: {
      on: {
        mousemove: {
          dragging: {
            actions: [drag]
          }
        },
        mouseup: [
          { target: "selected", cond: isNotTrashElement },
          { target: "disposed", actions: [showDaysRemoved] }
        ]
      }
    },
    disposed: {
      on: {
        startGrab: {
          grabbing: {
            actions: [initGrab]
          }
        }
      }
    },
    grabbing: {
      on: {
        mousemove: {
          grabbing: {
            actions: [updateGrab]
          }
        },
        mouseup: {
          disposed: {
            actions: [resetGrab]
          }
        }
      }
    }
  }
});

let currentState = machine.initialState;

const flipping = new Flipping({
  activeSelector: el => el.matches('[data-state="grabbed"] *')
});

const flippingStyleVars = flipping.wrap(styleVars);

const send = event => {
  const state = machine.transition(currentState, event, extState);
  
  if (state.value !== currentState.value) {
    document.body.setAttribute("data-prev-state", currentState.value);
  }
  
  currentState = state;

  if (state.actions) {
    state.actions.reduce((extState, action) => {
      Object.assign(extState, action(extState, event) || {});
      return extState;
    }, extState);
  }

  document.body.setAttribute("data-state", currentState.value);
  const { mx1, my1, mx2, my2, drag, grabIndex, stretchIndex } = extState;

  styleVars({
    mx1,
    my1,
    mx2,
    my2,
    "drag-index": drag.index,
    "drag-x1": drag.x1,
    "drag-y1": drag.y1,
    "drag-x2": drag.x2,
    "drag-y2": drag.y2
  });

  dayEls.forEach((el, i) =>
    el.setAttribute(
      "data-state",
      extState.selected.includes(i) ? "selected" : ""
    )
  );

  if (extState.grabIndex !== undefined) {
    dayEls[extState.grabIndex].setAttribute("data-state", "grabbed");
  }
};

document.body.setAttribute("data-state", machine.initial);

document.body.addEventListener("mousedown", e => {
  send(e);
});

document.body.addEventListener("mouseup", e => {
  send(e);
});

document.body.addEventListener("mousemove", e => {
  send(e);
});

dayEls.forEach((el, i) => {
  styleVars({ yc: Math.abs(3 - i) }, el);
  el.addEventListener("mousedown", e => {
    if (e.target.matches('[data-state="selected"]')) {
      send({
        type: "drag",
        index: i,
        clientX: e.clientX,
        clientY: e.clientY
      });
    }
  });

  el.addEventListener("mouseover", e => {
    send({
      type: "mouseover",
      index: i
    });
  });

  Array.from(el.querySelectorAll(".ui-grab")).forEach(grabEl => {
    grabEl.addEventListener("mousedown", e => {
      send({
        type: "startGrab",
        index: i
      });
    });
  });
});

trashEl.addEventListener("mouseover", e => {
  send({ type: "prepareDispose" });
});
trashEl.addEventListener("mouseup", e => {
  send("dispose");
});
trashEl.addEventListener("mouseout", e => {
  send("cancelDispose");
});

// because someone will inevitably resize their screen
window.addEventListener('resize', e => {
  extState.centers = dayEls.map(center);
});

              
            
!
999px

Console