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. 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.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

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

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

              
                <main>
  <h1>Send a message</h1>
  <p>I am a summary element to show you that you’ve typed <strong aria-live="polite" aria-atomic="true" data-element="character-count">0 characters</strong>,
    because I am subscribed to state changes.</p>
  <form>
    <label for="message">Your message</label>
    <textarea data-element="textbox" rows="5" id="message"></textarea>
    <div data-element="message" aria-live="polite" aria-atomic="true"></div>
  </form>
</main>
              
            
!

CSS

              
                body {
  background: hsl(39,100%,97%);
  color: hsl(222,30%,16%);
  font-family: Arial, sans-serif;
  font-size: 1.2rem;
  height: 100vh;
  display: grid;
  place-items: center;
}

main {
  padding: 0 1.5rem;
  max-width: 30rem;
  margin: 0 auto;
}

h1 {
  font-size: 1.8rem;
  font-weight: 500;
}

textarea {
  font: inherit;
  border: 1px solid hsl(222,30%,20%);
  background: transparent;
  padding: 1rem;
  width: 100%;
  max-width: 30rem;
}

label {
  font-family: monospace;
  display: block;
  margin: 0 0 0.2rem 0;
  font-size: 1.8rem;
}

form {
  margin-top: 2rem;
}

.message {
  display: flex;
  align-items: center;
  margin-top: 0.5rem;
  color: hsl(220,23%,26%);
  line-height: 1;
}

.message svg {
  opacity: 0.8; 
  fill: currentcolor;
  margin-inline-end: 0.5rem;
}

.message[data-status="error"] {
  color: hsl(338,81%,41%);
  font-weight: bold;
}
              
            
!

JS

              
                window.subscribers = [];

const defaultState = {
  content: '',
  characterCount: 0
};

const state = new Proxy(defaultState, {
  set(state, key, value) {
    const oldState = {...state};

    state[key] = value;

    window.subscribers.forEach(callback => callback(state, oldState));

    return state;
  }
});

const characterLimit = 100;

// Grab the textbox and the result area which holds our character count result
const textboxElement = document.querySelector('[data-element="textbox"]');
const messageElement = document.querySelector('[data-element="message"]');
const characterCountElement = document.querySelector('[data-element="character-count"]');

// Every time a character is inputed (or removed), we update state
textboxElement.addEventListener('input', () => {
  state.content = textboxElement.value;
  state.characterCount = state.content.split('').length;
});

// Render an SVG based on the passed alias
const renderIcon = alias => {
  let path = '';

  switch (alias) {
    case 'alert':
      path =
        '<path d="M23 12c0-3.037-1.232-5.789-3.222-7.778s-4.741-3.222-7.778-3.222-5.789 1.232-7.778 3.222-3.222 4.741-3.222 7.778 1.232 5.789 3.222 7.778 4.741 3.222 7.778 3.222 5.789-1.232 7.778-3.222 3.222-4.741 3.222-7.778zM21 12c0 2.486-1.006 4.734-2.636 6.364s-3.878 2.636-6.364 2.636-4.734-1.006-6.364-2.636-2.636-3.878-2.636-6.364 1.006-4.734 2.636-6.364 3.878-2.636 6.364-2.636 4.734 1.006 6.364 2.636 2.636 3.878 2.636 6.364zM11 8v4c0 0.552 0.448 1 1 1s1-0.448 1-1v-4c0-0.552-0.448-1-1-1s-1 0.448-1 1zM12 17c0.552 0 1-0.448 1-1s-0.448-1-1-1-1 0.448-1 1 0.448 1 1 1z"></path>';
      break;
    case 'check':
      path =
        '<path d="M19.293 5.293l-10.293 10.293-4.293-4.293c-0.391-0.391-1.024-0.391-1.414 0s-0.391 1.024 0 1.414l5 5c0.391 0.391 1.024 0.391 1.414 0l11-11c0.391-0.391 0.391-1.024 0-1.414s-1.024-0.391-1.414 0z"></path>';
      break;
  }

  if (!path.length) {
    return '';
  }

  return `
    <svg aria-hidden="true" focusable="false" version="1.1" xmlns="http://www.w3.org/2000/svg" width="1em" height="1em" viewBox="0 0 24 24">
      ${path}
    </svg>
  `;
};

// Our render handler which renders how many characters we have left
// or how many characters we have gone over
const renderCounter = () => {
  let message = '';
  let icon = '';
  const diff = Math.abs(characterLimit - state.characterCount);
  const suffix = diff === 1 ? '' : 's';
  const status = state.characterCount <= characterLimit ? 'ok' : 'error';

  switch (status) {
    case 'ok':
      message = `You have ${diff} character${suffix} remaining`;
      icon = 'check';
      break;
    case 'error':
      message = `You have ${diff} character${suffix} too many`;
      icon = 'alert';
      break;
  }

  messageElement.innerHTML = `
    <div class="message" data-status="${status}">${renderIcon(
    icon
  )}${message}</div>
  `;
};


// Subscribe the render function to state changes
window.subscribers.push(renderCounter);

// The character count function updates the seperate summary element
const renderCharacterCount = (state, oldState) => {
  const suffix = state.characterCount === 1 ? '' : 's';
  
  characterCountElement.innerText = `${state.characterCount} character${suffix}`;
};

window.subscribers.push(renderCharacterCount);

// Render the default state
renderCounter(state);

              
            
!
999px

Console