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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ 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="example"></div>
              
            
!

CSS

              
                body {
  font-family: "Helvetica Neue", Arial;
  font-weight: 200;
}

h1, h2, h3 {
  font-weight: 100;
}

a {
  color: hsl(200, 50%, 50%);
}

a.active {
  color: hsl(20, 50%, 50%);
}

.breadcrumbs a {
  text-decoration: none;
}
* {
  box-sizing: border-box;
}
body {
  font-family: Georgia, serif;
  padding: 50px;
  margin: 0; }
ul { padding: 0; }
li, button {
  border-radius: 3px;
  border: 1px solid grey;
  width: 100%;
  box-sizing: border-box;
  padding: 10px;
  cursor: pointer;
  list-style: none;
  text-align: center;
  margin-bottom: 30px;
}

input {
  font-size: 18px;
  font-family: Georgia, serif;
  padding: 10px;
  border-radius: 2px;
  border: 1px solid grey;
}
input:focus { outline:0; }

.Entries {
  margin: -5px;
}

.Entry {
  position: relative;
  cursor: pointer;
  display: inline-block;
  border: 1px solid #ccc;
  border-radius: 3px;
  padding: 10px;
  width: 140px;
  height: 100px;
  margin: 5px;
  overflow: hidden;
}
.Entry-name,
.Entry-phone {
  text-overflow: ellipsis;
  overflow: hidden;
}
.Entry-name {
  font-size: 18px;
  margin-bottom: 8px;
}
.Entry-phone {
  font-family: "Consolas", "Menlo", "Courier", monospace;
}
.Entry:hover {
  border-color: black;
}

.Entry-delete {
  position: absolute;
  width: 20px;
  height: 20px;
  top: 0;
  right: -5px;
}
.Entry-delete:hover { color: red; }

.NewEntry {
  width: 100%;
  border: 1px solid #ccc;
  margin-bottom: 10px;
}

.NewEntry-title {
  background: #444;
  color: #eee;
  font-size: 10px;
  padding: 3px;
}

.NewEntry-name,
.NewEntry-phone {
  box-sizing: border-box;
  border: none;
  width: 100%;
  font-size: 12px;
  margin-bottom: 5px;
}

.NewEntry-name {
  border-bottom: 1px solid #ccc;
}

.UndoBtn {
  position: relative;
  display: inline-block;
  font-size: 34px;
  text-decoration: none;
  margin-left: 15px;
}
.UndoBtn:after {
  position: absolute;
  bottom: -5px;
  left: 50%;
  transform: translateX(-50%);
  content: "undo";
  display: inline-block;
  font-size:10px;
}
              
            
!

JS

              
                const {fromJS} = Immutable;
const {component} = ElegantReact({debug: true});
 
const subedit = (edit, ...path) => transform => 
  edit(state => state.updateIn(path, transform));

const newBlankEntry = _ => fromJS({name: '', phone: ''});

const data = fromJS({
  newEntry: newBlankEntry(),
  entries: [
    { name: 'aria', phone: '3332221111'},
    { name: 'gilbox', phone: '6665552222'},
    { name: 'jimbo', phone: '9993332222'},
    { name: 'lebowski', phone: '7774442222'},
    { name: 'murphy', phone: '8884442222'},
    { name: 'vanderbilt', phone: '9993332222'},
  ]
});

const parseName = v => v && v.replace(/\d/g, '');

const identity = v => v;

const parsePhone = v => v && v.replace(/\D/g, '').substr(0,10);

const formatPhone = p => p &&
[p[0],p[1],p[2],'-',p[3],p[4],p[5],'-',p[6],p[7],p[8],p[9]]
  .join('')
  .replace(/-+$/,'');

const FormattedInput = component(function FormattedInput ({value, formatter, parser}, {edit}) {
  return (
    <input {...this.props}
      value={formatter(value)}
      onChange={event =>
        edit(value => parser(event.target.value))
        } />
  )
});

// input field that only allows 10 digits, and formats like XXX-XXX-XXXX
const PhoneInput = component(function PhoneInput ({value}, {editPhone}) {
  return (
    <FormattedInput {...this.props}
      placeholder="Phone Number"
      formatter={formatPhone}
      parser={parsePhone}
      value={value}
      statics={{ edit: editPhone }} />
  )
});

// input field that disallows digits
const NameInput = component(function NameInput ({value}, {editName}) {
  return (
    <FormattedInput {...this.props}
      placeholder="Name"
      formatter={identity}
      parser={parseName}
      value={value}
      statics={{ edit: editName }} />
  );
});

// a single phone book entry
const Entry = component(function Entry({entry}, {deleteEntry}) {
  return (
    <div className="Entry">
      <div onClick={deleteEntry} className="Entry-delete">×</div>
      <div className="Entry-name">{entry.get('name')}</div>
      <div className="Entry-phone">{formatPhone(entry.get('phone'))}</div>
    </div>
  );
});

// grid of phone book entries
const Entries = component(function Entries({entries}, {editEntries}) {
  return (
    <div className="Entries">
      {entries.map((entry, index) =>
          <Entry
            key={`${entry.get('phone')}${entry.get('name')}`}
            entry={entry}
            statics={{
              deleteEntry: _=> editEntries(entries => entries.delete(index) )
            }} />
      ).toArray()}
    </div>
  );
});

const handleEvent = handler => event => {
  handler(event);
  event.preventDefault();
};

// Form for creating a new phone book entry
const NewEntry = component(function NewEntry({data}, {edit, addNewEntry}) {
  return (
    <form className="NewEntry" onSubmit={handleEvent(addNewEntry)}>
      <div className="NewEntry-title">ADD NEW</div>
      <NameInput
        className="NewEntry-name"
        value={data.get('name')}
        statics={{
          editName: subedit(edit, 'name') }} />
      <PhoneInput
        className="NewEntry-phone"
        value={data.get('phone')}
        statics={{
          editPhone: subedit(edit, 'phone') }} />
      <button style={{display: 'none'}} />
    </form>
  );
});

const pushNewEntry = edit =>
  edit(data =>
    data.getIn(['newEntry', 'name']) &&
      data.update('entries',
          entries =>
            entries.push(data.get('newEntry'))
                   .sortBy(entry => entry.get('name')))
          .update('newEntry', newBlankEntry));

const App = component(function App ({data, historyLength}, {edit, undo}) {
  return (
    <div>
      <h1>Address Book Demo
        {historyLength ? <a href="#"
           onClick={handleEvent(undo)}
           className="UndoBtn">⎌</a> : null}
      </h1>
      <NewEntry
        data={data.get('newEntry')}
        statics={{
          edit: subedit(edit, 'newEntry'),
          addNewEntry: _=> pushNewEntry(edit) }} />
      <Entries
        entries={data.get('entries')}
        statics={{ editEntries: subedit(edit, 'entries') }} />
    </div>
  );
});

// the Renderer component manages the top-level app state.
// it also handles the undo history which takes a snapshot of
// the entire application state every time the 'entries' change
const rendererMixin = {
  history: [],
  getInitialState() {
    this.prevEntries = this.props.data.get('entries');
    this.history = [];

    return {data:this.props.data}
  },
  undo() {
    const lastEntries = this.history.pop();
    this.setState({data: this.state.data.set('entries', lastEntries) });
    this.prevEntries = lastEntries;
  },
  edit (transform) {
    const newData = transform(this.state.data);
    this.setState({data: newData});

    const entries = newData.get('entries');
    if (entries !== this.prevEntries) this.history.push(this.prevEntries);
    this.prevEntries = entries;
  }
};
const Renderer = component(rendererMixin, function Renderer() {
  return <App
    data={this.state.data}
    historyLength={this.history.length}
    statics={{ edit: this.edit, undo: this.undo }} />
});

React.render(<Renderer data={data} />, document.getElementById('example'));
              
            
!
999px

Console