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 URLs added here will be added as <link>
s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.
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.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and 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 esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM 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.
html
head
body
span(style='position: relative; display: block; float: left; height: 24px; line-height: 24px; margin-right: 10px;')
| Find me:
a.github(style='position: relative; display: inline-block; width: 24px; height: 24px; margin-right: 10px;', href='https://github.com/LucaColonnello', title='Luca Colonnello on GitHub')
svg(viewBox="0 0 1024 1024", xmlns='http://www.w3.org/2000/svg')
path(d='M512 0C229.252 0 0 229.25199999999995 0 512c0 226.251 146.688 418.126 350.155 485.813 25.593 4.686 34.937-11.125 34.937-24.626 0-12.188-0.469-52.562-0.718-95.314-128.708 23.46-161.707-31.541-172.469-60.373-5.525-14.809-30.407-60.249-52.398-72.263-17.988-9.828-43.26-33.237-0.917-33.735 40.434-0.476 69.348 37.308 78.471 52.75 45.938 77.749 119.876 55.627 148.999 42.5 4.654-32.999 17.902-55.627 32.501-68.373-113.657-12.939-233.22-56.875-233.22-253.063 0-55.94 19.968-101.561 52.658-137.404-5.22-12.999-22.844-65.095 5.063-135.563 0 0 42.937-13.749 140.811 52.501 40.811-11.406 84.594-17.031 128.124-17.22 43.499 0.188 87.314 5.874 128.188 17.28 97.689-66.311 140.686-52.501 140.686-52.501 28 70.532 10.375 122.564 5.124 135.499 32.811 35.844 52.626 81.468 52.626 137.404 0 196.686-119.751 240-233.813 252.686 18.439 15.876 34.748 47.001 34.748 94.748 0 68.437-0.686 123.627-0.686 140.501 0 13.625 9.312 29.561 35.25 24.562C877.436 929.998 1024 738.126 1024 512 1024 229.25199999999995 794.748 0 512 0z')
a.linkedin(style='position: relative; display: inline-block; width: 24px; height: 24px;', href='https://it.linkedin.com/pub/luca-colonnello/26/545/774', title='Luca Colonnello on LinkedIn')
svg(xmlns='http://www.w3.org/2000/svg', xmlns:xlink='http://www.w3.org/1999/xlink', viewbox='0 0 512 512', fill='#000000')
g#svgGrid.svgGrid
line(stroke-width='1', stroke='#B35047', x1='32', x2='32', y1='0', y2='512')
line(stroke-width='1', stroke='#B35047', x1='64', x2='64', y1='0', y2='512')
line(stroke-width='1', stroke='#B35047', x1='96', x2='96', y1='0', y2='512')
line(stroke-width='1', stroke='#B35047', x1='128', x2='128', y1='0', y2='512')
line(stroke-width='1', stroke='#B35047', x1='160', x2='160', y1='0', y2='512')
line(stroke-width='1', stroke='#B35047', x1='192', x2='192', y1='0', y2='512')
line(stroke-width='1', stroke='#B35047', x1='224', x2='224', y1='0', y2='512')
line(stroke-width='1', stroke='#B35047', x1='256', x2='256', y1='0', y2='512')
line(stroke-width='1', stroke='#B35047', x1='288', x2='288', y1='0', y2='512')
line(stroke-width='1', stroke='#B35047', x1='320', x2='320', y1='0', y2='512')
line(stroke-width='1', stroke='#B35047', x1='352', x2='352', y1='0', y2='512')
line(stroke-width='1', stroke='#B35047', x1='384', x2='384', y1='0', y2='512')
line(stroke-width='1', stroke='#B35047', x1='416', x2='416', y1='0', y2='512')
line(stroke-width='1', stroke='#B35047', x1='448', x2='448', y1='0', y2='512')
line(stroke-width='1', stroke='#B35047', x1='480', x2='480', y1='0', y2='512')
line(stroke-width='1', stroke='#B35047', x1='0', x2='512', y1='32', y2='32')
line(stroke-width='1', stroke='#B35047', x1='0', x2='512', y1='64', y2='64')
line(stroke-width='1', stroke='#B35047', x1='0', x2='512', y1='96', y2='96')
line(stroke-width='1', stroke='#B35047', x1='0', x2='512', y1='128', y2='128')
line(stroke-width='1', stroke='#B35047', x1='0', x2='512', y1='160', y2='160')
line(stroke-width='1', stroke='#B35047', x1='0', x2='512', y1='192', y2='192')
line(stroke-width='1', stroke='#B35047', x1='0', x2='512', y1='224', y2='224')
line(stroke-width='1', stroke='#B35047', x1='0', x2='512', y1='256', y2='256')
line(stroke-width='1', stroke='#B35047', x1='0', x2='512', y1='288', y2='288')
line(stroke-width='1', stroke='#B35047', x1='0', x2='512', y1='320', y2='320')
line(stroke-width='1', stroke='#B35047', x1='0', x2='512', y1='352', y2='352')
line(stroke-width='1', stroke='#B35047', x1='0', x2='512', y1='384', y2='384')
line(stroke-width='1', stroke='#B35047', x1='0', x2='512', y1='416', y2='416')
line(stroke-width='1', stroke='#B35047', x1='0', x2='512', y1='448', y2='448')
line(stroke-width='1', stroke='#B35047', x1='0', x2='512', y1='480', y2='480')
rect(x='0', y='0', width='512', height='512', fill='none', stroke-width='1', stroke='#B35047')
path(d='M 426.00,0.00L 86.00,0.00 C 38.70,0.00,0.00,38.70,0.00,86.00l0.00,340.00 c0.00,47.30, 38.70,86.00, 86.00,86.00l 340.00,0.00 c 47.30,0.00, 86.00-38.70, 86.00-86.00L 512.00,86.00 C 512.00,38.70, 473.30,0.00, 426.00,0.00z M 192.00,416.00l-64.00,0.00 L 128.00,192.00 l 64.00,0.00 L 192.00,416.00 z M 160.00,160.00c-17.673,0.00-32.00-14.327-32.00-32.00s 14.327-32.00, 32.00-32.00s 32.00,14.327, 32.00,32.00S 177.673,160.00, 160.00,160.00z M 416.00,416.00l-64.00,0.00 L 352.00,288.00 c0.00-17.673-14.327-32.00-32.00-32.00s-32.00,14.327-32.00,32.00l0.00,128.00 l-64.00,0.00 L 224.00,192.00 l 64.00,0.00 l0.00,39.736 C 301.199,213.604, 321.376,192.00, 344.00,192.00c 39.765,0.00, 72.00,35.817, 72.00,80.00 L 416.00,416.00 z', fill='#000000')
#body
// settings
// general
$bodyBackground: #cfcfcf;
$loaderColor: #777;
// autocomplete
$autocompleteInputBorderColor: #999;
$autocompleteListShadow: 0px 2px 4px #bbb;
$autocompleteListBorderColor: #999;
$autocompleteListBgColor: #fff;
$autocompleteListHoverColor: #ccc;
// ---------------------------------
// animations
@keyframes cssload-rotate-one {
0% {
//If you use compass, instead of the line below you could use + transform($scale, $rotate, $transx, $transy, $skewx, $skewy, $originx, $originy)
transform: rotateX(35deg) rotateY(-45deg) rotateZ(0deg);
}
100% {
//If you use compass, instead of the line below you could use + transform($scale, $rotate, $transx, $transy, $skewx, $skewy, $originx, $originy)
transform: rotateX(35deg) rotateY(-45deg) rotateZ(360deg);
}
}
@keyframes cssload-rotate-two {
0% {
//If you use compass, instead of the line below you could use + transform($scale, $rotate, $transx, $transy, $skewx, $skewy, $originx, $originy)
transform: rotateX(50deg) rotateY(10deg) rotateZ(0deg);
}
100% {
//If you use compass, instead of the line below you could use + transform($scale, $rotate, $transx, $transy, $skewx, $skewy, $originx, $originy)
transform: rotateX(50deg) rotateY(10deg) rotateZ(360deg);
}
}
@keyframes cssload-rotate-three {
0% {
//If you use compass, instead of the line below you could use + transform($scale, $rotate, $transx, $transy, $skewx, $skewy, $originx, $originy)
transform: rotateX(35deg) rotateY(55deg) rotateZ(0deg);
}
100% {
//If you use compass, instead of the line below you could use + transform($scale, $rotate, $transx, $transy, $skewx, $skewy, $originx, $originy)
transform: rotateX(35deg) rotateY(55deg) rotateZ(360deg);
}
}
// fadeIn animation
@-webkit-keyframes fadeIn { from { opacity:0; } to { opacity:1; } }
@-moz-keyframes fadeIn { from { opacity:0; } to { opacity:1; } }
@keyframes fadeIn { from { opacity:0; } to { opacity:1; } }
// ----------------------------------------------------------------
// default styles
body {
background: $bodyBackground;
font-size: 100%;
font-family: Arial;
}
// spinner
.cssload-loader {
position: relative;
display: inline-block;
width: 24px;
height: 24px;
//If you use compass, instead of the line below you could use + border-radius($radius, $vertical-radius)
border-radius: 50%;
//If you use compass, instead of the line below you could use + perspective($p)
perspective: 780px;
}
.cssload-inner {
position: absolute;
width: 100%;
height: 100%;
//If you use compass, instead of the line below you could use + box-sizing($bs)
box-sizing: border-box;
//If you use compass, instead of the line below you could use + border-radius($radius, $vertical-radius)
border-radius: 50%;
&.cssload-one {
left: 0;
top: 0;
animation: cssload-rotate-one 1.15s linear infinite;
border-bottom: 3px solid $loaderColor;
}
&.cssload-two {
right: 0;
top: 0;
animation: cssload-rotate-two 1.15s linear infinite;
border-right: 3px solid $loaderColor;
}
&.cssload-three {
right: 0;
bottom: 0;
animation: cssload-rotate-three 1.15s linear infinite;
border-top: 3px solid $loaderColor;
}
}
// wrapper element
.wrapper {
position: relative;
width: 600px;
margin: auto;
}
// selected element
.selected {
position: relative;
font-size: 16px;
}
// autocomplete container
.autocomplete_container {
position: relative;
margin-top: 10px;
}
// autocomplete component style
.autocomplete {
input {
position: relative;
float: left;
padding: 10px;
width: 100%;
border-radius: 3px;
border: 1px solid $autocompleteInputBorderColor;
font-size: 14px;
box-sizing: border-box;
outline: 0;
}
}
.autocomplete-loading {
position: relative;
float: left;
margin-left: -34px;
top: 7px;
}
.autocomplete-list {
position: absolute;
border: 1px solid $autocompleteListBorderColor;
background: $autocompleteListBgColor;
min-width: 100%;
top: 40px;
box-shadow: $autocompleteListShadow;
box-sizing: border-box;
animation: fadeIn ease-in 1 forwards 100ms;
}
.autocomplete__item {
padding: 8px;
&.hover,
&:hover {
background: $autocompleteListHoverColor;
}
}
class AutocompleteContainer extends React.Component {
constructor ( props, context ) {
super( props, context );
this.data = [
"London",
"Berlin",
"Valencia",
"Amsterdam",
"Madrid",
"Manchester",
"Hamburgo",
"New York",
"Rome",
"Rio de Janeiro",
"Singapore",
"Saint Petersburg",
"Los Angeles",
"Sydney",
"Liverpool",
];
this.state = {
results: [ ],
loading: false,
selected: ''
};
}
search ( value ) {
return this.data.filter( function( item ) {
return ( new RegExp(value, 'i') ).test( item );
} );
}
onSearch( value ) {
var time = this.time = (new Date( )).getTime( );
this.state.results = [ ];
value = value.trim( );
if( value == "" ) {
this.state.loading = false;
this.setState( this.state );
return;
}
this.state.loading = true;
this.setState( this.state );
setTimeout( function( ) {
if( this.time != time ) return;
this.state.loading = false;
this.state.results = this.search( value ) || [ ];
this.setState( this.state );
}.bind( this ), 1000 );
}
onSelect( result, index ) {
console.log( "selected", result, index );
this.state.selected = result;
this.state.results = [ ];
this.setState( this.state );
}
render( ) {
return (
<div className="wrapper">
<h2>Autocomplete ReactJS <br />(Made with <3 by Luca Colonnello)</h2>
<div className="selected">
<strong>Selected: {( this.state.selected ) ? this.state.selected : 'Select a city'}</strong><br />
(Examples: Rome, New York, London, Rio de Janeiro, ...)
</div>
<div className="autocomplete_container">
<Autocomplete
onSearch={this.onSearch.bind( this )}
onSelect={this.onSelect.bind( this )}
results={this.state.results}
loading={this.state.loading}>
{item => {
return <div key={item}>{item}</div>;
}}
</Autocomplete>
</div>
<br /><br />
<p>
A simple autocomplete test component made with Reactjs.
</p>
<h3>Goals:</h3>
<ul>
<li>Make an agnostic component, independent from any kind of Flux implementation</li>
<li>Use a container to interface with the Flux implementation and gain control of the autocomplete</li>
<li>Use a function as children in order to let developers to gain more control on the list items</li>
<li>Simulate async loading and give feedback to the users</li>
</ul>
</div>
);
}
}
class Autocomplete extends React.Component {
constructor ( props, context ) {
super( props, context );
this.state = {
selected: false,
hover: false,
results: this.props.results,
loading: this.props.loading
};
}
componentWillReceiveProps( nextProps ) {
this.setState( {
selected: false,
hover: false,
results: nextProps.results,
loading: nextProps.loading
} );
}
onChange( e ) {
this.props.onSearch( e.target.value );
}
onKeyUp( e ) {
var
keyCode = e.keyCode || e.which
, hover = false
, selected = false
, state = {
results: this.state.results,
loading: this.state.loading
}
;
if( this.state.results.length ) {
switch( keyCode ) {
case 38: // up
if(
this.state.hover === false ||
( this.state.hover - 1 ) < 0
) {
hover = this.state.results.length - 1;
} else {
hover = this.state.hover - 1;
}
break;
case 40: // down
if(
this.state.hover === false ||
( this.state.hover + 1 ) > ( this.state.results.length - 1 )
) {
hover = 0;
} else {
hover = this.state.hover + 1;
}
break;
case 13: // enter
if( this.state.hover !== false )
selected = this.state.hover;
break;
}
}
if( selected === false ) {
state.selected = false;
state.hover = hover;
this.setState( state );
} else {
this.select( selected );
}
}
onBlur( e ) {
setTimeout( function(){
var state = { };
state.selected = false;
state.hover = false;
state.results = [ ];
this.setState( state );
}.bind( this ), 250 );
}
select( index ) {
var state = { };
state.selected = index;
state.hover = false;
React.findDOMNode( this.refs.input ).value = "";
// call the callback
this.props.onSelect( this.state.results[ state.selected ], state.selected );
state.results = [ ];
this.setState( state );
}
render( ) {
return (
<div className="autocomplete">
<input ref="input" onBlur={this.onBlur.bind(this)} onKeyUp={this.onKeyUp.bind( this )} onChange={this.onChange.bind( this )} placeholder="Search a city..." />
{function( ){
if( this.state.loading ) {
return (
<div className="autocomplete-loading">
<div className="cssload-loader">
<div className="cssload-inner cssload-one"></div>
<div className="cssload-inner cssload-two"></div>
<div className="cssload-inner cssload-three"></div>
</div>
</div>
);
}
return "";
}.bind( this )( )}
<div style={{clear: "both"}} />
{function(){
if( this.state.results.length ) {
return <div className="autocomplete-list">{( !this.state.selected ) ? this.state.results.map(function( item, index ){
return React.cloneElement( this.props.children( item ), {
className: "autocomplete__item " + ( ( this.state.hover !== false && this.state.hover == index ) ? 'hover' : '' ),
onClick: this.select.bind( this, index )
} );
}.bind( this )) : ''}</div>
} else return "";
}.bind(this)()}
</div>
);
}
}
Autocomplete.contextTypes = {
children: React.PropTypes.func
};
React.render( <AutocompleteContainer />, document.getElementById( 'body' ) );
Also see: Tab Triggers