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

              
                <div class="ui-week" data-state="asdf">
  <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;
  
  &:before {
    content: attr(data-state);
    position: absolute;
    display: inline-block;
    top: 100%;
    left: 50%;
    transform: translateX(-50%);
    margin-top: 1rem;
    text-align: center;
    background: rgba(black, 0.5);
    padding: .5rem 1rem;
    border-radius: 2rem;
    color: white;
    font-size: 5vmin;
    font-weight: bold;
    pointer-events: none;
  }
}

%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 { assign } = XState;

const elWeek = document.querySelector('.ui-week');
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
});

const { fromEvent } = rxjs;
const { mapTo } = rxjs.operators;

const mouseDown$ = fromEvent( document.body,"mousedown");

const mouseUp$ = fromEvent( document.body,"mouseup");

const mouseMove$ = fromEvent( document.body,"mousemove");

const prepareDispose$ = fromEvent(trashEl, "mouseover").pipe(
  mapTo({ type: "prepareDispose" })
);
const dispose$ = fromEvent(trashEl, "mouseup").pipe(
  mapTo({ type: "dispose" })
);
const cancelDispose$ = fromEvent(trashEl, "mouseout").pipe(
  mapTo({ type: "cancelDispose" })
);

// because someone will inevitably resize their screen
const resize$ = fromEvent(window, 'resize')

// 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 = assign((s, e) => ({
  x1: event.clientX,
  y1: event.clientY,
  x2: event.clientX,
  y2: event.clientY,
  selected: [],
  foo: console.log(event.clientX)
}));

const updateSelection = assign((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 {
    ...s,
    mx1,
    mx2,
    my1,
    my2,
    x2: e.clientX,
    y2: e.clientY,
    selected: s.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 = assign((s, e) => ({
  drag: {
    type: e.type,
    index: e.index,
    x1: e.clientX,
    y1: e.clientY,
    x2: e.clientX,
    y2: e.clientY
  }
}));

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

const isNotTrashElement = (s, e) => e.target !== trashEl;
const initGrab = assign((s, e) => ({
  grabIndex: e.index,
  stretchIndex: e.index
}));
const updateGrab = assign((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 = assign({ grabIndex: undefined });

const machine = XState.Machine({
  initial: "start",
  context: {
    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)
  },
  invoke: [
    { src: mouseUp$ }, 
    { src: mouseMove$ },
    { src: dispose$ },
    { src: resize$ }
  ],
  states: {
    start: {
      invoke: {
        src: mouseDown$
      },
      on: {
        mousedown: {
          target: 'selecting',
          actions: startSelection 
        }
      }
    },
    selecting: {
      invoke: [{ src: mouseUp$ }, { src: mouseMove$ }],
      on: {
        mouseup: "selected",
        mousemove: {
          target: 'selecting',
          actions: updateSelection
        }
      }
    },
    selected: {
      invoke: [{ src: mouseDown$ }, { src: mouseMove$ }],
      on: {
        mousedown: {
          target: 'selecting',
          actions: startSelection
        },
        drag: {
          target: 'dragging',
          actions: startDrag
        }
      }
    },
    dragging: {
      on: {
        mousemove: {
          target: 'dragging',
          actions: drag
        },
        mouseup: [
          { target: "selected", cond: isNotTrashElement },
          { target: "disposed", actions: showDaysRemoved }
        ]
      }
    },
    disposed: {
      on: {
        startGrab: {
          target: 'grabbing',
          actions: initGrab
        }
      }
    },
    grabbing: {
      on: {
        mousemove: {
          target: 'grabbing',
          actions: updateGrab
        },
        mouseup: {
          target: 'disposed',
          actions: resetGrab
        }
      }
    }
  },
  on: {
    resize: {
      internal: true,
      actions: assign({
        centers: () => dayEls.map(center)
      })
    }
  }
});

const service = XState.interpret(machine);

service.subscribe(state => {
  elWeek.dataset.state = state.value; 
  document.body.setAttribute("data-state", state.value);
  const { mx1, my1, mx2, my2, drag, grabIndex, stretchIndex, selected } = state.context;

  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",
      selected.includes(i) ? "selected" : ""
    )
  );

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

service.start();

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.exec(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);

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

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

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





              
            
!
999px

Console