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.
<div id="root"></div>
<ul>
<li>Focusable with tab</li>
<li>Open with space</li>
<li>Close with tab or escape</li>
<li>Toggle open/close with enter</li>
<li>Select using up/down arrows</li>
<li>Finding option by typing</li>
</ul>
$grey-background: #f5f5f5;
$grey-border: #ddd;
$grey-text: #898989;
$grey-dark: #444;
$main-light: #d9f2fb;
$main: #00a9e0;
$main-dark: #007da6;
$success: #7fd81e;
$error: #ff5100;
$warning: #f6c223;
$body: #081f2c;
*, *::before, *::after {
box-sizing: inherit;
}
body {
box-sizing: border-box;
margin: 10px;
color: $body;
font: 400 13px/20px 'Source Sans Pro', sans-serif;
}
svg {
display: block;
width: 1em;
height: 1em;
fill: currentColor;
}
.select {
position: relative;
display: inline-block;
width: 320px;
&:focus {
outline: 0;
& .selection {
box-shadow: 0 0 1px 1px $main;
}
}
}
.label {
display: block;
margin-bottom: 6px;
font-weight: 600;
}
.selection {
position: relative;
padding: 5px;
border: 1px solid $grey-border;
background: #fff;
}
.value {
position: relative;
display: inline-block;
padding: 5px 10px;
}
.multiple {
padding-right: 30px;
margin-right: 5px;
background: $main-light;
color: $main;
}
.delete {
position: absolute;
top: 0;
right: 0;
display: block;
padding: 10px;
font-size: 10px;
cursor: pointer;
}
.placeholder {
padding: 5px 10px;
color: $grey-text;
}
.arrow {
position: absolute;
top: 5px;
right: 5px;
display: block;
padding: 10px;
font-size: 10px;
color: $grey-text;
}
.options {
position: absolute;
top: 100%;
left: 0;
right: 0;
border: solid $grey-border;
border-width: 0 1px;
background: #fff;
}
.option {
padding: 10px 15px;
border-bottom: 1px solid $grey-border;
cursor: pointer;
&.selected {
border: 1px solid $main;
margin: -1px -1px 0;
background: $main-light;
}
&.focused {
background: $grey-background;
}
}
.checkbox {
content: '';
vertical-align: top;
display: inline-block;
width: 16px;
height: 16px;
padding: 2px;
border: 1px solid $grey-border;
border-radius: 2px;
margin: 2px 12px 0 0;
color: #fff;
font-size: 10px;
.selected & {
border-color: $main-dark;
background: $main;
}
}
const { Component, PropTypes } = React
const { string, arrayOf, shape } = PropTypes
class Select extends Component {
constructor(props) {
super(props)
this.state = {
values: [],
focusedValue: -1,
isFocused: false,
isOpen: false,
typed: ''
}
this.onFocus = ::this.onFocus
this.onBlur = ::this.onBlur
this.onKeyDown = ::this.onKeyDown
this.onClick = ::this.onClick
this.onDeleteOption = ::this.onDeleteOption
this.onHoverOption = ::this.onHoverOption
this.onClickOption = ::this.onClickOption
this.renderOption = ::this.renderOption
}
onFocus() {
this.setState({
isFocused: true
})
}
onBlur() {
const { options, multiple } = this.props
this.setState(prevState => {
const { values } = prevState
if (multiple) {
return {
focusedValue: -1,
isFocused: false,
isOpen: false
}
} else {
const value = values[0]
let focusedValue = -1
if (value) {
focusedValue = options.findIndex(option => option.value === value)
}
return {
focusedValue,
isFocused: false,
isOpen: false
}
}
})
}
onKeyDown(e) {
const { options, multiple } = this.props
const { isOpen } = this.state
switch (e.key) {
case ' ':
e.preventDefault()
if (isOpen) {
if (multiple) {
this.setState(prevState => {
const { focusedValue } = prevState
if (focusedValue !== -1) {
const [ ...values ] = prevState.values
const value = options[focusedValue].value
const index = values.indexOf(value)
if (index === -1) {
values.push(value)
} else {
values.splice(index, 1)
}
return { values }
}
})
}
} else {
this.setState({
isOpen: true
})
}
break
case 'Escape':
case 'Tab':
if (isOpen) {
e.preventDefault()
this.setState({
isOpen: false
})
}
break
case 'Enter':
this.setState(prevState => ({
isOpen: !prevState.isOpen
}))
break
case 'ArrowDown':
e.preventDefault()
this.setState(prevState => {
let { focusedValue } = prevState
if (focusedValue < options.length - 1) {
focusedValue++
if (multiple) {
return {
focusedValue
}
} else {
return {
values: [ options[focusedValue].value ],
focusedValue
}
}
}
})
break
case 'ArrowUp':
e.preventDefault()
this.setState(prevState => {
let { focusedValue } = prevState
if (focusedValue > 0) {
focusedValue--
if (multiple) {
return {
focusedValue
}
} else {
return {
values: [ options[focusedValue].value ],
focusedValue
}
}
}
})
break
default:
if (/^[a-z0-9]$/i.test(e.key)) {
const char = e.key
clearTimeout(this.timeout)
this.timeout = setTimeout(() => {
this.setState({
typed: ''
})
}, 1000)
this.setState(prevState => {
const typed = prevState.typed + char
const re = new RegExp(`^${typed}`, 'i')
const index = options.findIndex(option => re.test(option.value))
if (index === -1) {
return {
typed
}
}
if (multiple) {
return {
focusedValue: index,
typed
}
} else {
return {
values: [ options[index].value ],
focusedValue: index,
typed
}
}
})
}
break
}
}
onClick() {
this.setState(prevState => ({
isOpen: !prevState.isOpen
}))
}
onDeleteOption(e) {
const { value } = e.currentTarget.dataset
this.setState(prevState => {
const [ ...values ] = prevState.values
const index = values.indexOf(value)
values.splice(index, 1)
return { values }
})
}
onHoverOption(e) {
const { options } = this.props
const { value } = e.currentTarget.dataset
const index = options.findIndex(option => option.value === value)
this.setState({
focusedValue: index
})
}
onClickOption(e) {
const { multiple } = this.props
const { value } = e.currentTarget.dataset
this.setState(prevState => {
if (!multiple) {
return {
values: [ value ],
isOpen: false
}
}
const [ ...values ] = prevState.values
const index = values.indexOf(value)
if (index === -1) {
values.push(value)
} else {
values.splice(index, 1)
}
return { values }
});
}
stopPropagation(e) {
e.stopPropagation()
}
renderValues() {
const { placeholder, multiple } = this.props
const { values } = this.state
if (values.length === 0) {
return (
<div className="placeholder">
{ placeholder }
</div>
)
}
if (multiple) {
return values.map(value => {
return (
<span
key={ value }
onClick={ this.stopPropagation }
className="multiple value"
>
{ value }
<span
data-value={ value }
onClick={ this.onDeleteOption }
className="delete"
>
<X />
</span>
</span>
)
})
}
return (
<div className="value">
{ values[0] }
</div>
)
}
renderOptions() {
const { options } = this.props
const { isOpen } = this.state;
if (!isOpen) {
return null
}
return (
<div className="options">
{ options.map(this.renderOption) }
</div>
)
}
renderOption(option, index) {
const { multiple } = this.props
const { values, focusedValue } = this.state
const { value } = option
const selected = values.includes(value)
let className = "option"
if (selected) className += " selected"
if (index === focusedValue) className += " focused"
return (
<div
key={ value }
data-value={ value }
className={ className }
onMouseOver={ this.onHoverOption }
onClick={ this.onClickOption }
>
{ multiple ?
<span className="checkbox">
{ selected ? <Check /> : null }
</span> :
null
}
{ value }
</div>
)
}
render() {
const { label } = this.props
const { isOpen } = this.state
return (
<div
className="select"
tabIndex="0"
onFocus={ this.onFocus }
onBlur={ this.onBlur }
onKeyDown={ this.onKeyDown }
>
<label className="label">{ label }</label>
<div className="selection" onClick={ this.onClick }>
{ this.renderValues() }
<span className="arrow">
{ isOpen ? <ChevronUp /> : <ChevronDown /> }
</span>
</div>
{ this.renderOptions() }
</div>
)
}
}
const ChevronDown = () => (
<svg viewBox="0 0 10 7">
<path d="M2.08578644,6.5 C1.69526215,6.89052429 1.69526215,7.52368927 2.08578644,7.91421356 C2.47631073,8.30473785 3.10947571,8.30473785 3.5,7.91421356 L8.20710678,3.20710678 L3.5,-1.5 C3.10947571,-1.89052429 2.47631073,-1.89052429 2.08578644,-1.5 C1.69526215,-1.10947571 1.69526215,-0.476310729 2.08578644,-0.0857864376 L5.37867966,3.20710678 L2.08578644,6.5 Z" transform="translate(5.000000, 3.207107) rotate(90.000000) translate(-5.000000, -3.207107) " />
</svg>
)
const ChevronUp = () => (
<svg viewBox="0 0 10 8">
<path d="M2.08578644,7.29289322 C1.69526215,7.68341751 1.69526215,8.31658249 2.08578644,8.70710678 C2.47631073,9.09763107 3.10947571,9.09763107 3.5,8.70710678 L8.20710678,4 L3.5,-0.707106781 C3.10947571,-1.09763107 2.47631073,-1.09763107 2.08578644,-0.707106781 C1.69526215,-0.316582489 1.69526215,0.316582489 2.08578644,0.707106781 L5.37867966,4 L2.08578644,7.29289322 Z" transform="translate(5.000000, 4.000000) rotate(-90.000000) translate(-5.000000, -4.000000) " />
</svg>
)
const X = () => (
<svg viewBox="0 0 16 16">
<path d="M2 .594l-1.406 1.406.688.719 5.281 5.281-5.281 5.281-.688.719 1.406 1.406.719-.688 5.281-5.281 5.281 5.281.719.688 1.406-1.406-.688-.719-5.281-5.281 5.281-5.281.688-.719-1.406-1.406-.719.688-5.281 5.281-5.281-5.281-.719-.688z" />
</svg>
)
const Check = () => (
<svg viewBox="0 0 16 16">
<path d="M13 .156l-1.406 1.438-5.594 5.594-1.594-1.594-1.406-1.438-2.844 2.844 1.438 1.406 3 3 1.406 1.438 1.406-1.438 7-7 1.438-1.406-2.844-2.844z" transform="translate(0 1)" />
</svg>
)
ReactDOM.render(
<div>
<Select
label="React Select"
placeholder="Pick one"
options={[
{ value: 'Rock' },
{ value: 'Paper' },
{ value: 'Scissors' }
]}
/>
<span style={{ display: 'inline-block', width: 20 }} />
<Select
label="React Multiple Select"
placeholder="Pick some"
options={[
{ value: 'Rock' },
{ value: 'Paper' },
{ value: 'Scissors' }
]}
multiple
/>
<span style={{ display: 'inline-block', width: 20 }} />
<select>
<option>Rock</option>
<option>Paper</option>
<option>Scissors</option>
</select>
</div>,
document.getElementById('root')
)
Also see: Tab Triggers