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 id="app"></div>
              
            
!

CSS

              
                @import url('https://fonts.googleapis.com/css?family=Lato&display=swap');

.calendar {
  width: 350px;
  height: 480px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-family: 'Lato', sans-serif;
  box-shadow: 0px 0px 15px 0px rgba(0,0,0,0.5);
  
  .header {
    background: #009688;
    color: white;
    padding: 20px 15px;
    font-size: 2rem;
    
    div:first-child {
      font-size: 1.2rem;
      opacity: 0.8;
    }
  }
  
  .selector {
    margin: 20px;
    position: relative;
    font-size: 1.5rem;
    
    span {
      font-size: 1.3rem;
      position: absolute;
      left: 50%;
      transform: translateX(-50%);
    }
    
    i {
      cursor: pointer;
      &.fa-chevron-right {
        float: right;
      }
    }
  }
  
  .days {
    margin: 20px;
    font-size: 1.2rem;
    
    > * {
      display: grid;
      justify-items: center;
      grid-template-columns: repeat(7, 1fr);
      margin: 20px 0px;
    }
    
    .days-in-week {
      font-weight: bold;
      opacity: 0.4;
    }
    
    .days-in-month {
      span {
        width: 50%;
        text-align: center;
        border-radius: 100%;
        cursor: pointer;
        padding: 8px;
        
        &.today {
          background: #009688;
          color: white;
        }
        
        &.selected {
          background: rgba(00, 96, 88, 0.4);
        }
      
        &.different-month {
          opacity: 0.4;
        }
      }
    }
  }
}
              
            
!

JS

              
                const { observable } = mobx;
const { observer } = mobxReact;
const { Component } = React;

const store = observable({
  today: moment().startOf('day'),
  selectedDay: moment().startOf('day').add(1, "days"),
  selectedMonth: moment().startOf('month'),
  daysInWeek: ['S', 'M', 'T', 'W', 'T', 'F', 'S'],
  
  changeMonth: function(num) {
		this.selectedMonth = moment(this.selectedMonth.add(num, "months"));
	},
  
  changeDay: function(day) {
		this.selectedDay = day;
	},
});
                         
const App = observer(
  class App extends Component {
    render() {
      const { store } = this.props;
      return (
        <div className="calendar">
          <Header selectedDay={store.selectedDay}/>
          <Selector store={store}/>
          <DaysGrid store={store}/>
        </div>
      );
    }
  }
);

const Header = observer(
  class Header extends Component {
    render() {
      return (
        <div className="header">
          <div>{this.props.selectedDay.format("YYYY")}</div>
          <div>{this.props.selectedDay.format("dddd, MMM DD")}</div>
        </div>
      );
    }
  }
);

const Selector = observer(
  class Selector extends Component {
    handleAddMonth = () => {
      this.props.store.changeMonth(1);
    }

    handleSubtractMonth = () => {
      this.props.store.changeMonth(-1);
    }

    render() {
      return (
        <div className="selector">
          <i className="fas fa-chevron-left" onClick={this.handleSubtractMonth}></i>
          <span>{this.props.store.selectedMonth.format("MMM YYYY")}</span>
          <i className="fas fa-chevron-right" onClick={this.handleAddMonth}></i>
        </div>
      );
    }
  }
);

const DaysGrid = observer(
  class DaysGrid extends Component {
    
    getDaysInMonth = () => {
      const {selectedMonth} = this.props.store;
      const totalDays = new Array();
      const totalLimit = 42; // 6 lines x 7 days
  
      // Add days of current month
      for (let i = 0; i < selectedMonth.daysInMonth(); i++) {
        totalDays.push(moment(selectedMonth).add(i, "days"));
      }

      // Add days of previous month if first day of current month isn't Sunday
      if (moment(selectedMonth).day() !== 0) {
        const previousMonth = moment(selectedMonth).subtract(1, 'months').endOf("month").startOf("day");
        for (let i = 0; i < moment(selectedMonth).day(); i++) {
          totalDays.unshift(moment(previousMonth).subtract(i, "days"));
        }
      }
  
      // Add days of next month if total days is no enough
      if (totalDays.length < totalLimit) {
        const nextMonth = moment(selectedMonth).add(1, 'months');
        const daysToFill = totalLimit - totalDays.length;
        for (let i = 0; i < daysToFill; i++) {
          totalDays.push(moment(nextMonth).add(i, "days"));
        }
      }
  
      return totalDays;
    }

    render() {
      const {daysInWeek, selectedDay, selectedMonth, today} = this.props.store;
      return (
        <div className="days">
          <div className="days-in-week">
            {daysInWeek.map(d => <span>{d}</span> )}
          </div>
          <div className="days-in-month">
            {this.getDaysInMonth().map(d => <Day day={d} store={this.props.store}/>)}
          </div>
        </div>
      );
    }
  }
);

const Day = observer(
  class Day extends Component {
    
    getClassName = () => {
      const {day} = this.props;
      const {daysInWeek, selectedDay, selectedMonth, today} = this.props.store;

      if (day.diff(today, "days") === 0) {
        return "today"
      } else if (day.diff(selectedDay, "days") === 0) {
        return "selected"
      } else if (day.month() != selectedMonth.month()) {
        return "different-month"
      } else {
        return ""
      }
    }
    
    handleChangeDay = () => {
      this.props.store.changeDay(this.props.day);
    }

    render() {
      return (
        <span className={this.getClassName()} onClick={this.handleChangeDay}>
          {this.props.day.date()}
        </span>
      )      
    }
  }
);

ReactDOM.render(
  <App store={store} />,
  document.getElementById("app")
);

              
            
!
999px

Console