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="app"></div>
.todos-list-group .list-group-item.disabled
text-decoration: line-through
font-style: italic
background-color: #efefef
.welcome-text
height: 0
overflow: hidden
transition: height .25s ease-in-out
text-align: center
color: #444
.welcome-text--visible
height: 50px
//
// -- State management
//
// Settings reducer
const settingsInitialState = { name: 'redux-todo-app' }
const settingsReducer = (state = settingsInitialState, action) => state
// Todos reducer
const todosInitialState = []
const todosReducer = (state = todosInitialState, action) => {
switch (action.type) {
case 'add-todo': return [ ...state, action.todo ]
case 'toggle-todo': return [ ...state.map(todo => {
if (todo.id === action.todoId) {
return {
...todo,
status: !todo.status
}
}
return todo
}) ]
case 'clear-done-todos': return [ ...state.filter(todo => !todo.status) ]
default: return state
}
}
const reducers = Redux.combineReducers({
settings: settingsReducer,
todos: todosReducer,
})
const appInitialState = {
settings: { name: 'TodosDemo' },
todos: [
// { id: 1, status: false, content: 'Buy milk' },
// { id: 2, status: true, content: 'Clean home' },
],
}
// compose the store with the debug tool if available
const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || Redux.compose
const enhancers = composeEnhancers(Redux.applyMiddleware(ReduxThunk.default))
const store = Redux.createStore(reducers, appInitialState, enhancers)
//
// -- Service Layer (thunks)
//
const addTodo = content => dispatch => {
dispatch({
type: 'add-todo',
todo: {
id: Date.now(),
status: false,
content,
}
})
}
//
// -- Dumb Components
//
const getTodoClass = status => {
const classes = [ 'list-group-item' ]
if (status) {
classes.push('disabled')
}
return classes.join(' ')
}
const TodoItem = ({ id, status, content, onToggle }) => (
<li className={getTodoClass(status)} onClick={() => onToggle(id)}>
{content}
</li>
)
const TodosList = ({ items, onToggle }) => (
<ul className={'list-group todos-list-group'}>
{items.map(item =>
<TodoItem
{...item}
key={item.id}
onToggle={onToggle}
/>
)}
</ul>
)
const TodosListView = ({ todos, toggleTodo, clearDone }) => {
if (!todos.length) {
return null
}
return (
<div>
<hr />
<TodosList items={todos} onToggle={toggleTodo} />
<hr />
<button
className={'btn btn-link'}
onClick={clearDone}
children={'Clear stuff I have done'}
/>
</div>
)
}
// Stateful component that handle the form typing and local validation
class NewItemForm extends React.Component {
constructor (props) {
super(props)
this.state = { text: '' }
}
onTextUpdate (text) {
this.setState({ text })
}
onFormSubmit (e) {
e.preventDefault()
if (!this.state.text) {
return
}
this.props.onNewValue(this.state.text)
this.setState({ text: '' })
}
render () {
return (
<form onSubmit={(e) => this.onFormSubmit(e)}>
<div className={'form-row'}>
<div className={'col'}>
<input
type={'text'}
className={'form-control'}
placeholder={'remember the milk...'}
value={this.state.text}
onChange={e => this.onTextUpdate(e.target.value)}
/>
</div>
<div className={'col-xs-1'}>
<button type={'submit'} className={'btn btn-primary'}>{'Add'}</button>
</div>
</div>
</form>
)
}
}
NewItemForm.defaultProps = {
onNewValue: () => {}
}
const getWelcomeTextClass = show => {
const classes = [ 'welcome-text' ]
if (show) {
classes.push('welcome-text--visible')
}
return classes.join(' ')
}
const WelcomeText = ({ show }) => (
<div className={getWelcomeTextClass(show)}>
<p className={'lead'}>{'What are you going to do next?'}</p>
</div>
)
// Many incoming props can be listed vertically
const AppComponent = ({
name,
todos,
showWelcome,
addTodo,
toggleTodo,
clearDone
}) => (
<div className={'container'}>
<h3>{name}</h3>
<hr />
<WelcomeText show={showWelcome} />
<NewItemForm onNewValue={addTodo} />
<TodosListView { ...({todos, toggleTodo, clearDone}) } />
</div>
)
//
// -- Container
//
const mapStateToProps = ({ settings, todos }) => ({
name: settings.name,
showWelcome: todos.length === 0,
todos,
})
const mapDispatchToProps = {
addTodo, // services
toggleTodo: todoId => ({ type: 'toggle-todo', todoId }),
clearDone: () => ({ type: 'clear-done-todos' }),
}
const AppContainer = ReactRedux.connect(mapStateToProps, mapDispatchToProps)(AppComponent)
//
// -- Render the App
//
const renderApp = <AppContainer store={store} />
const renderTarget = document.getElementById('app')
ReactDOM.render(renderApp, renderTarget)
Also see: Tab Triggers