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 style="display:flex;
  flex-flow:column wrap;
  align-content: center;">
  
 <!-- Mount calendar on root--> 
  <div id="root"></div>
  
 <!-- For Demo Purposes--> 
<span><input id="input-mode" type="checkbox" value="multiple"> <label>Select multiple</label></span>
<br/>
<textarea id="input-date" placeholder="MM/DD/YYYY"></textarea>
</div>

<div style="width:30em">
  <h3>new Calendar(selectCallback, mini, selectMulti)</h3>
  <h4>Parameters</h4>
  <u>selectCallback</u> function: Runs when a date is clicked on
  <br/>
  (e) => {
  console.log(e + "is the node that was clicked on!")
  }
  <br/>
  <u>[mini]</u> boolean: set true for mini appearance (WIP) Use short names and compact style<br/>
  <u>[selectMulti]</u> boolean: false by default, but if true can select multiple dates
  (demo--check the box to enable this)
  <br/>
</div>

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

CSS

              
                :root{
  --font1:'Nova Round', cursive;
  --font2: 'Margarine', cursive;
  --color1: rgb(68, 91, 222,1);
  --weekHeaderBGColor:rgb(68, 168, 222);
  --todayTextColor:rgba(234,43,187);
  --dayActiveColor:rgb(68, 91, 222,0.9);
  --dayHoverColor:rgba(222, 216, 68,0.3);
  --borderColor:rgba(0,0,0,0.1);
  --fullDayHeight:6rem;
  --transitionTime:300ms;
}

.calendar-wrapper{
  height:calc(var(--fullDayHeight)*7);
  width:70em;
  box-sizing:content-box;
  display:flex;
  position:relative;
  justify-content: center;
  overflow:hidden;
}

.calendar{
  position:absolute;
  transform:initial;
  font-family:var(--font2);
  display: inline-grid;
  grid-template:
      "M M M M M M M"
      "W W W W W W W"
      "Days Days Days Days Days Days Days"
}

/*Headings*/
.display.month{
  font-size: 2rem;
  margin: 0 auto;
}

.display.week div{
  padding:0 0.2em;
}

.display.week{
  font-size:1.2em;
  border-radius:6px 6px 0 0;
  grid-area: W;
}

/* Individual Units*/

.calendar .header{
  font-family:var(--font1);
  text-align:center;
}

.week{
  display: grid;
  justify-content:center;
  box-shadow: 0 2px 5px 0 rgba(0,0,0,0.3);
  grid-template-columns: repeat(7, 1fr);
}

.header.week{
  background-color:var(--weekHeaderBGColor);
  color:white;
}

.calendar .row{
  display:flex;
  flex-flow:row nowrap;
}

.days{
  width:100%;
  grid-area: Days;
  grid-auto-rows: var(--fullDayHeight);
}

.day{
  cursor:pointer;
  position:relative;
  border-collapse:collapse;
  border-top:1px solid var(--borderColor);
  padding:2px;
}

.day.today{
  color:var(--todayTextColor);
}

.day.today:before{
  content:"";
  position:absolute;
  background-color:var(--color1);
  filter:hue-rotate(40deg);
  height:5px;
  width:5px;
  border-radius:50%;
  top:4px;
  right:5px;
  z-index:-1;
}


.day:not(:nth-child(7n+0)){
  border-right:1px solid var(--borderColor);
}

.day.active {
  color:white  !important;
  background-color:var(--dayActiveColor)!important;
}

.blank.day{
  background-color: rgba(30,30,30, 0.2);
  color:hsl(240,20%,50%);
  filter:grayscale(0.5);
  opacity:0.4;
}

.day:hover, .day:active, .blank.day:hover, .blank.day:active{
  background-color:var(--dayHoverColor);
}

/* mini class styling*/
.mini .days{
  grid-auto-rows: min-content;
}

.calendar-wrapper.mini{
  width:20rem;
  height:15rem;
}

.mini .week{
  grid-auto-columns: fit-content(0.5rem);
  grid-auto-rows: fit-content(0.5rem);
}

.mini .day{
  text-align:center;
  font-size:1.2rem;
  padding:0;
}

/*UI Components*/

.calendar .mainheader{
  grid-area: M;
  display:flex;
  flex-flow:row nowrap;
  justify-content: center;
  text-transform:uppercase;
}

.calendar .arrow{
  transform:scale(1.2);
  border-radius:50%;
  background-color:var(--color1);
  height:1em;
  width:1em;
  color:white;
  line-height:0.8em;
  text-align:center;
  user-select: none;
  -moz-user-select: none;
  -webkit-user-select: none;
  -ms-user-select: none;
  cursor:pointer;
  margin:0.5em;
}

.calendar .arrow.left:after{
  content:"\02039";
}

.calendar .arrow.right:after{
  content:"\0203A";
}

/* Animations */

.anim-slide-left-in{
  animation: kfSlideLeftIn var(--transitionTime) linear ;
}

.anim-slide-left-out{
  animation: kfSlideLeftOut var(--transitionTime) linear ;
}

.anim-slide-right-in{
  animation: kfSlideLeftOut var(--transitionTime) linear reverse;
}

.anim-slide-right-out{
  animation: kfSlideLeftIn var(--transitionTime) linear reverse;
}

@keyframes kfSlideLeftIn{
  0%{
    opacity:0;
    transform:translate(100%);
  }
  100%{
    opacity:1;
    transform:translate(0);
  }
}

@keyframes kfSlideLeftOut{
  0%{
    opacity:1;
    transform:translate(0);
  }
  100%{
    opacity:0;
    transform:translate(-100%);
  }
}




/*For Demo formatting*/
body{
  display:flex;
  flex-flow:row wrap;
  justify-content:center;
  font-family:Trebuchet, sans-serif;
}

#input-date{
  height:10em;
}

/*Google fonts*/
/* latin-ext */
@font-face {
  font-family: 'Margarine';
  font-style: normal;
  font-weight: 400;
  font-display: swap;
  src: local('Margarine'), local('Margarine-Regular'), url(https://fonts.gstatic.com/s/margarine/v8/qkBXXvoE6trLT9Y7YLyu7Z5KoAU.woff2) format('woff2');
  unicode-range: U+0100-024F, U+0259, U+1E00-1EFF, U+2020, U+20A0-20AB, U+20AD-20CF, U+2113, U+2C60-2C7F, U+A720-A7FF;
}
/* latin */
@font-face {
  font-family: 'Margarine';
  font-style: normal;
  font-weight: 400;
  font-display: swap;
  src: local('Margarine'), local('Margarine-Regular'), url(https://fonts.gstatic.com/s/margarine/v8/qkBXXvoE6trLT9Y7YLyu455K.woff2) format('woff2');
  unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
}
/* latin */
@font-face {
  font-family: 'Nova Round';
  font-style: normal;
  font-weight: 400;
  font-display: swap;
  src: local('Nova Round'), local('NovaRound'), url(https://fonts.gstatic.com/s/novaround/v11/flU9Rqquw5UhEnlwTJYTUYDUeA.woff2) format('woff2');
  unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
}
              
            
!

JS

              
                "use strict"
class Calendar{
  constructor(selectCallback, mini, selectMulti){
    this.root = document.createElement("div");
    this.root.className = `calendar-wrapper${mini?" mini":""}`;
    this.mini = mini;
    selectMulti? this.mode = "multiple":this.mode = "single";
    this.currentDates = new Set() //
    this.calendarsRenderedLeft = [];
    this.calendarsRenderedRight = [];
    let today = new Date();
    this.onselect = selectCallback;
    this.renderCalendar(today.getMonth() + 1, today.getFullYear(), null, today.getDate());
    this.transitionTime = getComputedStyle(document.documentElement).getPropertyValue('--transitionTime').replace(/ms/g, "");
  }
  
  mount(eleement){
    eleement.appendChild(this.root);
  }
/*
* month integer is 1 based (1-12)
* From https://stackoverflow.com/questions/222309/calculate-last-day-of-month-in-javascript
*/
static getDaysInMonth(m, y) {
    return m===2 ? y & 3 || !(y%25) && y & 15 ? 28 : 29 : 30 + (m+(m>>3)&1);
}
/*
 Returns the day as 0 based integer (0-6, sunday-saturday)
*/
static getFirstDay(m, y){
  return new Date(y, m-1, 1).getDay();
}

static getLastDay(m, y){
  return new Date(y, m, 0).getDay();
}

static prevMonth(month){ if (month > 1) { return (month-1) } return 12};
static nextMonth(month){ if (month < 12){return month+1} return 1};
static prevYear(month, year){if (month > 1) {return year} return year-1};
static nextYear(month, year){if (month < 12){return year} return year+1};

static parseDate(MMDDYYYY){
  let arr = MMDDYYYY.split(/\//g);
  let n = (e) => {return Number.parseInt(e)};
  return {month: n(arr[0]), day: n(arr[1]), year: n(arr[2])}
}

static generateDaysLabels(mini){
  let a;
  let b = "";
  if (mini){a = Calendar.DAY["MINI"]} else {a = Calendar.DAY["FULL"]}
  for (let i = 0; i < 7; i++) {
    b += `<div>${a[i]}</div>`
  }
  return b;
}

  static get DAY(){
    return {
    FULL:["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"],
    MINI:["Su","Mo","Tu","We","Th","Fr","Sa"]
    }

    }
  static get MONTH(){
    return {   FULL: [
  "January" ,
  "February",
  "March",
  "April",
  "May",
  "June",
  "July",
  "August",
  "September",
  "October",
  "November",
  "December",
],
    MINI: [
  "Jan" ,
  "Feb",
  "Mar",
  "Apr",
  "May",
  "Jun",
  "Jul",
  "Aug",
  "Sep",
  "Oct",
  "Nov",
  "Dec",]}
  }

  get monthLabel(){
    return Calendar.MONTH[this.mini? "MINI":"FULL"];
  }
  
  static dayTemplate(num, month, year, blank, classes){
     return `<div class="${blank? "blank " : ""}day ${classes? classes:""}" data-month="${month}" data-year="${year}" data-day="${num}">${num}</div>`;
  }

  previous(month, year){
    return this.renderCalendar(Calendar.prevMonth(month), Calendar.prevYear(month, year), "right");
  }

  next(month, year){
    return this.renderCalendar(Calendar.nextMonth(month), Calendar.nextYear(month, year), "left");
  }

  generateCalendarHTML(month, year, direction, indicateToday) {
    let html = "";
    let daysInMonth = Calendar.getDaysInMonth(month, year);
    if(!direction) {
      direction = "out";
    }

    let firstDay = Calendar.getFirstDay(month, year);
    if (firstDay > 0){ //first day is not a Sunday
      let p = Calendar.getDaysInMonth(Calendar.prevMonth(month), year);
      for (let f = 0; f < firstDay; f++) {
        html = Calendar.dayTemplate(p, Calendar.prevMonth(month), Calendar.prevYear(month, year), true) + html;
        p--;
      }
    };
    for (let i = 1; i <= daysInMonth; i++) {
      if (indicateToday == i)
      {html += Calendar.dayTemplate(i, month, year, false, "today");}
      else
      {html += Calendar.dayTemplate(i, month, year);}
    }
    let lastDay = Calendar.getLastDay(month, year);
    if (lastDay < 6){ //last day is not a Saturday
      let u = 1;
      for (let l = 6; l > lastDay; l--) {
        html += Calendar.dayTemplate(u, Calendar.nextMonth(month), Calendar.nextYear(month, year), true);
        u++;
      }
    };

    return    `<div class="calendar anim-slide-${direction}-in">
        <div class="mainheader">
          <div class="arrow left"></div>
          <div class="display month header">
          ${this.monthLabel[month - 1]} ${year}
          </div>
          <div class="arrow right"></div>
        </div>
      
        <div class="display week header">
        ${Calendar.generateDaysLabels(this.mini)}
        </div>
      
        <div class="days week">
        ${html}
        </div>
      </div>
      `
  }

  selectDate(target){
    if (this.mode == "single") {
    this.clearSelectedDates();
    target.classList.add("active");
    this.currentDates.add(target);
    } else if (this.mode == "multiple") {
      if (this.currentDates.has(target)) {
        target.classList.remove("active");
        this.currentDates.delete(target);
      } else {
        target.classList.add("active");
        this.currentDates.add(target);
      }
    }
    this.onselect(target);
}

  selectDateTransition(month, year, transition, e){
    if (transition == "next") {
      this.next(month, year).then((res)=>{
        this.selectDate(res.querySelector(`[data-month="${Calendar.nextMonth(month)}"][data-day="${e.target.innerHTML}"]`));
      })
    } else if (transition == "prev") {
      this.previous(month, year).then((res)=>{
        this.selectDate(res.querySelector(`[data-month="${Calendar.prevMonth(month)}"][data-day="${e.target.innerHTML}"]`));
      })
    }

  }

clearSelectedDates(){
  this.currentDates.forEach(t => {
    t.classList.remove("active");
  })
  this.currentDates.clear();
}

get selectedDates(){
  let result = "";
    this.currentDates.forEach(d=> {
      result += `${d.dataset.month}/${d.innerHTML}/${d.dataset.year}` + `\n`;
    })

  return result;
}

  renderCalendar(month, year, direction, indicateToday){
    return new Promise((resolve, reject) => {
      let newCalendar;

if (this.calendarsRenderedLeft.length > 0 && direction == "right") {
    newCalendar = this.calendarsRenderedLeft.pop();
    newCalendar.classList.add(`anim-slide-${direction}-in`);
} else if (this.calendarsRenderedRight.length > 0 && direction == "left") {
  newCalendar = this.calendarsRenderedRight.pop();
  newCalendar.classList.add(`anim-slide-${direction}-in`);
} else {
  newCalendar = document.createRange().createContextualFragment(this.generateCalendarHTML(month, year, direction, indicateToday));
  newCalendar.querySelector(".arrow.left").addEventListener("click", ()=> {
    this.previous(month, year);
    });
  newCalendar.querySelector(".arrow.right").addEventListener("click", ()=> {
    this.next(month, year);
    });  
        
    let daysNodes = newCalendar.querySelectorAll(".day:not(.blank)");
    daysNodes.forEach((n) => {n.addEventListener("click", (e)=>this.selectDate(e.target))
    })
    daysNodes = newCalendar.querySelectorAll(`[data-month="${Calendar.prevMonth(month)}"][data-year="${Calendar.prevYear(month, year)}"]`);
    daysNodes.forEach((n) => {n.addEventListener("click", (e)=>this.selectDateTransition(month, year, "prev", e))
    })
    daysNodes = newCalendar.querySelectorAll(`[data-month="${Calendar.nextMonth(month)}"][data-year="${Calendar.nextYear(month, year)}"]`);
    daysNodes.forEach((n) => {n.addEventListener("click", (e)=>this.selectDateTransition(month, year, "next", e))
    })
}

    if (this.root.children.length > 0) {
      this.root.appendChild(newCalendar);
      this.root.children[0].classList.add(`anim-slide-${direction}-out`);
      setTimeout(()=>{
        if (direction == "right") {
          this.calendarsRenderedRight.push(this.root.children[0]);
        } else if (direction == "left") {
          this.calendarsRenderedLeft.push(this.root.children[0]);
        }
        this.root.children[0].className = "calendar";
        this.root.children[0].remove();
        this.root.children[0].className = "calendar";
        resolve(this.root.children[0]);
      }, this.transitionTime)
    } else {
      this.root.appendChild(newCalendar);
      this.root.children[0].className = "calendar";
      resolve(this.root.children[0]);
    }

    })
  }
}

/* end Class definition

******** Start main
*/

let inputDate = document.getElementById("input-date");
let inputMode = document.getElementById("input-mode");


let init = () => {
  let myCal = new Calendar(
    function(){
      inputDate.value = this.selectedDates
    }, true);
  myCal.mount(document.getElementById("root"));

  inputMode.addEventListener("change", (e)=>{
    if (e.target.checked) {
      myCal.clearSelectedDates();
      inputDate.value = "";
      myCal.mode = "multiple"
    } else {
      myCal.clearSelectedDates();
      inputDate.value = "";
      myCal.mode = "single"
    }

  })

}

init();
              
            
!
999px

Console