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 is required to process package imports. If you need a different preprocessor remove all packages first.

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

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

              
                <!--
TODO: 
* styling
* add month selector
* add year selector
-->
<div id="app">
  <p class="text-center">Selected Date: {{ formattedDate }}</p>
  <calendar v-model="curr" />
</div>


<template id="calendar">
  <div class="calendar">
  <header class="header">
    <button @click="previousMonth">&lt;&lt;</button>
    <span>{{ currentMonthLabel }} {{ currentYear }}</span>
    <button @click="nextMonth">&gt;&gt;</button>
  </header>
  <div class="headings" v-for="dayLabel in dayLabels">
    {{ dayLabel }}
  </div>
  <div v-for="(day, index) in dates"
       class="day"
       :class="dayClassObj(day)">
    <button @click="setSelectedDate(day)">
      {{ day.date | formatDateToDay }}
    </button>
  </div>
</div>
</template>
              
            
!

CSS

              
                :root {
  --white: hsl(0, 0%, 100%);
  --blue-grey-400: hsl(210, 38%, 95%);
  --blue-grey-100: hsl(210, 38%, 80%);
  --black-400: hsl(0, 0%, 96%);
  --black-300: hsl(0, 0%, 85%);
  --black-200: hsl(0, 0%, 75%);
  --black-100: hsl(0, 0%, 20%);
}

html {
  box-sizing: border-box;
  font-size: 16px;
}

*,
*:before,
*:after {
  box-sizing: inherit;
  font-family: inherit;
}

html,
body {
  height: 100%;
  width: 100%;
  overflow: hidden;
}

body {
  font-family: 'Inconsolata', serif;
  background: var(--blue-grey-400);
  color: var(--black-100);
  display: flex;
  justify-content: center;
}

.calendar {
  background-color: var(--white);
  border-radius: 3px;
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  width: 300px;
  padding: .25rem .5rem 1rem .5rem;
  box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
  
  > .top {
    grid-column: 1 / span 7;
  }

  > .header {
    padding: .75rem;
    font-size: 1.25rem;
    grid-column: 1 / span 7;
    
    >span {
      flex: 1;
      text-align: center;
    }
    
    button {
      border: none;
      background: var(--white);
      margin: 0 1rem;
      padding: .25rem .5rem;
      
      &:hover {
        cursor: pointer;
        background: var(--black-400);
        transition: background 150ms;
      }
    }
  }

  > * {
    align-items: center;
    display: flex;
    justify-content: center;
  }

  > .day {
    color: var(--black-200);
    font-size: 1rem;
    border-radius: 2px;
    
    &.selected {
      border: 1px solid var(--blue-grey-100);
    }
    
    &.current {
      color: var(--black-100);
    }

    &::before {
      content: "";
      display: inline-block;
      height: 0;
      padding-bottom: 100%;
      width: 1px;
    }
    
    button {
      color: inherit;
      background: transparent;
      border: none;
      height: 100%;
      width: 100%;
      &:hover {
        background: var(--black-400);
        transition: background 150ms;
      }
    }
  }

  > .today {
    background: var(--black-400);
    border-radius: 2px;
  }
}



.text-center {
  text-align: center;
}
              
            
!

JS

              
                const DAY_LABELS = ['S', 'M', 'T', 'W', 'Th', 'F', 'S'];
const MONTH_LABELS = [
        "January", "February", "March",
        "April", "May", "June",
        "July", "August", "September",
        "October", "November", "December"];

const Calendar = Vue.component('calendar', {
  template: "#calendar",
  data() {
    return {
      today: null,
      selectedDate: null,
      currDateCursor: null,
      dayLabels: null,
    };
  },
  created() {
    this.dayLabels = DAY_LABELS.slice();
    this.today = new Date();
    this.selectedDate = this.today;
    this.currDateCursor = this.today;
  },
  props: {
    startDate: {
      required: false,
      type: Date,
    }
  },
  computed: {
    currentMonth() {
      return this.currDateCursor.getMonth();
    },
    currentYear() {
      return this.currDateCursor.getFullYear();
    },
    currentMonthLabel() {
      return MONTH_LABELS[this.currentMonth];
    },
    dates() {
      const cursorDate = this.currDateCursor;
      let startDate = dateFns.startOfMonth(cursorDate),
          endDate = dateFns.lastDayOfMonth(cursorDate);
      const daysNeededForLastMonth = dateFns.getDay(startDate),
            daysNeededForNextMonth = (7 - (dateFns.getDay(endDate) + 1)) > 6 ? 0 : (7 - dateFns.getDay(endDate)) - 1;
      startDate = dateFns.addDays(startDate, -daysNeededForLastMonth);
      endDate = dateFns.addDays(endDate, daysNeededForNextMonth);
      
      return dateFns.eachDay(startDate, endDate).map(date => ({
        date,
        isCurrentMonth:  dateFns.isSameMonth(cursorDate, date),
        isToday: dateFns.isToday(date),
        isSelected: dateFns.isSameDay(this.selectedDate, date)  
      }));
    },
  },
  mounted() {
    if (this.startDate) {
      this.currDateCursor = this.startDate;
      this.selectedDate = this.startDate;
    }
  },
  methods: {
    dayClassObj(day) {
      return {
        'today' : day.isToday,
        'current': day.isCurrentMonth,
        'selected': day.isSelected,
      };
    },
    nextMonth() {
      this.currDateCursor = dateFns.addMonths(this.currDateCursor, 1);
    },
    previousMonth() {
      this.currDateCursor = dateFns.addMonths(this.currDateCursor, -1);
    },
    setSelectedDate(day) {
      this.selectedDate = day.date;
      this.$emit('input', this.selectedDate);
      // change calendar to correct month if they select previous or next month's days
      if (!day.isCurrentMonth) {
        const selectedMonth = dateFns.getMonth(this.selectedDate);
        this.currDateCursor = dateFns.setMonth(this.currDateCursor, selectedMonth);
      }
    }
  },
  filters: {
    formatDateToDay(val) {
      return dateFns.format(val, 'D');
    }
  },
});

new Vue({
  el: '#app',
  components: {
    Calendar,
  },
  data: {
    curr: new Date(),
  },
  computed: {
    formattedDate() {
      return dateFns.format(this.curr, 'MM/DD/YYYY');
    }
  },
});
              
            
!
999px

Console