HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
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.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
If the stylesheet you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
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.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
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.
Using packages here is powered by Skypack, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ES6 import
usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<div id="container">
<!-- This element's contents will be replaced with your component. -->
</div>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
background: #fff;
font: normal 13px/18px "Helvetica Neue", "Segoe UI", Helvetica, Arial,
"Lucida Grande", sans-serif;
}
#container {
width: 250px;
margin: 50px auto;
}
.hidden-input {
height: 0px;
width: 0px;
opacity: 0;
}
.datepicker-wrapper {
position: relative;
width: 250px;
}
.datepicker-input {
border-bottom: 1px solid #e4e0e9;
background: #fff;
font-size: 13px;
outline: none;
}
.is-focused {
border-bottom: 1px solid #188fff;
}
.is-invalid {
border-bottom: 1px solid red;
}
.datepicker-label {
color: #979ba7;
width: 64px;
line-height: 28px;
display: inline-block;
}
.datepicker-value {
padding: 0 8px;
margin-left: 8px;
line-height: 28px;
text-align: center;
border: none;
outline: none;
color: #26282a;
width: 100px;
font-size: 12px;
display: inline-block;
}
.datepicker-value.is-focused {
background-color: rgba(24, 143, 255, 0.16);
border-radius: 2px;
height: 28px;
}
.datepicker-value.is-invalid {
background-color: rgba(241, 88, 88, 0.16);
border-radius: 2px;
height: 28px;
}
.is-focused .arrow-down {
border-color: #188fff transparent;
}
.arrow-down {
border-color: #e0e4e9 transparent;
border-style: solid;
border-width: 5px 5px 0 5px;
float: right;
margin-top: 12px;
margin-right: 8px;
cursor: pointer;
}
.datepicker-calendar:after {
content: "";
position: absolute;
transform: rotate(45deg);
width: 12px;
height: 12px;
top: -7px;
left: 102px;
background: #fff;
}
.datepicker-calendar:before {
content: "";
position: absolute;
transform: rotate(45deg);
width: 12px;
height: 12px;
top: -7px;
left: 102px;
z-index: -1;
box-shadow: 0 0 6px rgba(0, 0, 0, 0.3);
}
.datepicker-calendar {
position: absolute;
background: #fff;
border-radius: 3px;
padding: 12px 16px 16px;
box-shadow: 0 0 6px rgba(0, 0, 0, 0.3);
top: 38px;
width: 230px;
left: 50%;
margin-left: -114px;
}
.datepicker-calendar.is-active {
border: 1px solid #188fff;
}
.datepicker-calendar.is-active:after {
border-top: 1px solid #188fff;
border-left: 1px solid #188fff;
}
.day-header-wrapper {
margin-top: 8px;
margin-bottom: 2px;
}
.month-header-wrapper {
font-size: 13px;
height: 20px;
border-bottom: 1px solid #e4e0e9;
}
.title-wrapper {
display: inline-block;
width: calc(100% - 40px);
color: #26282a;
font-weight: bold;
}
.year-title {
margin-left: 4px;
}
.days-wrapper {
margin-bottom: 2px;
}
.arrow {
color: #188fff;
width: 20px;
height: 20px;
text-align: center;
cursor: pointer;
font-weight: bold;
user-select: none;
}
.right {
float: right;
}
.day-header,
.day {
display: inline-block;
line-height: 20px;
width: 28px;
height: 20px;
font-size: 11px;
text-align: center;
}
.day-header {
color: #b9bdc5;
}
.day {
cursor: pointer;
}
.day.disabled {
color: #979ba7;
font-size: regular;
}
.day.selected {
background: #188fff;
border-radius: 3px;
color: white;
font-weight: bold;
}
.day:not(.selected):hover {
background: #e4e0e9;
border-radius: 3px;
}
.date-part {
margin: 0 4px;
text-align: center;
}
const daysToAdjust = {
"37": -1,
"38": -7,
"39": 1,
"40": 7
};
function cloneDate(date) {
return new Date(date.valueOf());
}
function isValidDate(value, format = "mm/dd/yyyy") {
let month, day, year;
const delimiter = /[^mdy]/.exec(format)[0];
const formatParts = format.split(delimiter);
const dateParts = value.split(delimiter);
for (let i = 0; i < dateParts.length; i++) {
if (/m/.test(formatParts[i])) month = dateParts[i];
if (/d/.test(formatParts[i])) day = dateParts[i];
if (/y/.test(formatParts[i])) year = dateParts[i];
}
return (
month > 0 &&
month < 13 &&
year &&
year.length === 4 &&
day > 0 &&
day <= new Date(year, month, 0).getDate()
);
}
function getFirstDayofWeek(date) {
const temp = cloneDate(date);
temp.setDate(temp.getDate() - temp.getDay());
return temp;
}
function getAllDaysInAWeek(date) {
const weekStartDate = getFirstDayofWeek(date);
const days = [weekStartDate];
for (let i = 1; i <= 6; i++) {
const next = cloneDate(weekStartDate);
next.setDate(next.getDate() + i);
days.push(next);
}
return days;
}
function WeekHeader() {
var header = ["S", "M", "T", "W", "T", "F", "S"].map(function(day, index) {
return (
<span key={day + index} className="day-header">
{day}
</span>
);
});
return header;
}
function buildWeeks(date) {
const tempDate = cloneDate(date);
tempDate.setDate(1);
const month = tempDate.getMonth();
const weekStartDate = getFirstDayofWeek(tempDate);
const currentDate = cloneDate(weekStartDate);
const weeks = [cloneDate(currentDate)];
currentDate.setDate(currentDate.getDate() + 7);
while (currentDate.getMonth() === month) {
weeks.push(cloneDate(currentDate));
currentDate.setDate(currentDate.getDate() + 7);
}
return weeks;
}
function isWeekend(date) {
const weekday = date.getDay();
return weekday === 0 || weekday === 6;
}
function Day({ day, month, selected, onSelect }) {
let className =
day.getMonth() !== month || isWeekend(day) ? "disabled" : "";
if (day.getTime() === selected.getTime()) {
className = "selected";
}
return (
<span
className={"day " + className}
onMouseUp={onSelect.bind(null, day)}
>
{day.getDate()}
</span>
);
}
function Week({ weekStart, selected, month, onSelect }) {
return (
<div className="days-wrapper">
{getAllDaysInAWeek(weekStart).map(function(day, i) {
return (
<Day
key={i}
month={month}
day={new Date(day)}
selected={selected}
onSelect={onSelect}
/>
);
})}
</div>
);
}
function Weeks({ date, month, selected, onSelect }) {
return (
<div>
{buildWeeks(date).map(
function(day, i) {
return (
<Week
key={i}
month={month}
weekStart={day}
selected={selected}
onSelect={onSelect}
/>
);
}.bind(this)
)}
</div>
);
}
const Calendar = React.createClass({
getInitialState: function() {
return {
selected: this.props.selectedDate
};
},
componentWillReceiveProps({ selectedDate }) {
this.setState({
selected: new Date(selectedDate)
});
},
setDate: function(selected) {
this.setState({ selected }, this.props.onMonthChange);
},
nextMonth: function() {
const clone = new Date(this.state.selected);
clone.setMonth(this.state.selected.getMonth() + 1);
this.setDate(clone);
},
previousMonth: function() {
const clone = new Date(this.state.selected);
clone.setMonth(this.state.selected.getMonth() - 1);
this.setDate(clone);
},
render: function() {
const { selected } = this.state;
const { onSelect, selectedDate } = this.props;
const year = selected.getFullYear();
const month = selected.toLocaleString("en-us", { month: "long" });
return (
<div>
<div className="month-header-wrapper">
<span className="title-wrapper">
<span className="month-title">{month}</span>
<span className="year-title ">{year}</span>
</span>
<span className="arrow right" onClick={this.nextMonth}>
>
</span>
<span className="arrow right" onClick={this.previousMonth}>
<
</span>
</div>
<div className="day-header-wrapper">{WeekHeader()}</div>
<Weeks
date={selected}
onSelect={onSelect}
selected={selectedDate}
month={selected.getMonth()}
/>
</div>
);
}
});
const DatePicker = React.createClass({
getInitialState: function() {
return {
isOpen: false,
isFocused: false,
currentSelected: new Date(),
// change to desired format
tempDate: new Date().toLocaleDateString()
};
},
componentDidMount: function() {
document.addEventListener("click", this.handleWindowMouseDown);
},
handleWindowMouseDown(event) {
const datepicker = ReactDOM.findDOMNode(this.datePicker);
const calendar = ReactDOM.findDOMNode(this.calendar);
if (!(calendar && datepicker)) {
return;
}
const eventIsOutside =
!calendar.contains(event.target) && calendar !== event.target;
const eventIsOnPopoverAnchor =
datepicker.contains(event.target) || datepicker === event.target;
if (eventIsOutside && !eventIsOnPopoverAnchor) {
this.setState({ isOpen: false });
}
},
close: function() {
this.setState({ isOpen: false, isFocused: false });
},
handleKeyDown: function(event) {
switch (event.keyCode) {
case 37:
case 38:
case 39:
case 40:
if (!event.shiftKey) {
return;
}
event.preventDefault();
const newDate = new Date(this.state.currentSelected);
newDate.setDate(
newDate.getDate() + daysToAdjust[event.keyCode]
);
this.setState({ currentSelected: newDate });
return;
case 13:
event.preventDefault();
const { isOpen, currentSelected } = this.state;
if (!isOpen) {
this.setState({ isOpen: true });
} else {
this.setState({
isOpen: false,
currentSelected,
tempDate: currentSelected.toLocaleDateString()
});
}
return;
case 9:
if (event.shiftKey || !this.state.isOpen) {
return;
}
this.close();
return;
case 27:
this.close();
return;
}
},
handleFocus: function() {
this.setState({ isFocused: true });
},
handleBlur: function() {
this.setState({ isFocused: false });
},
open: function() {
if (this.state.isOpen) {
return;
}
this.setState({ isOpen: true });
this.input.focus();
},
select: function(currentSelected) {
this.setState({
isOpen: false,
currentSelected,
invalidDate: false,
tempDate: currentSelected.toLocaleDateString()
});
this.input.focus();
},
updateDate({ target: { value } }) {
const newState = { tempDate: value, invalidDate: true };
if (isValidDate(value)) {
newState.invalidDate = false;
newState.currentSelected = new Date(value);
}
this.setState(newState);
},
render: function() {
const {
isOpen,
tempDate,
isFocused,
currentSelected,
invalidDate = false,
} = this.state;
const isInvalid = invalidDate ? " is-invalid" : "";
const focusClass =
!invalidDate && (isFocused || isOpen) ? " is-focused" : "";
return (
<div
className="datepicker-wrapper"
ref={ref => (this.datePicker = ref)}
>
<div
onClick={this.open}
className={"datepicker-input" + focusClass + isInvalid}
>
<span className="arrow-down" />
<label className="datepicker-label">From</label>
<input
type="text"
value={tempDate}
placeholder={"mm/dd/yyyy"}
onChange={this.updateDate}
onKeyDown={this.handleKeyDown}
ref={input => (this.input = input)}
className={"datepicker-value" + focusClass + isInvalid}
/>
</div>
{isOpen && (
<div
className="datepicker-calendar"
ref={ref => (this.calendar = ref)}
>
<Calendar
onSelect={this.select}
selectedDate={currentSelected}
onMonthChange={() => this.input.focus()}
/>
</div>
)}
</div>
);
}
});
ReactDOM.render(<DatePicker />, document.getElementById("container"));
Also see: Tab Triggers