Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

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.

+ 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

              
                <!-- Your HTML here -->
<button class="toggle">
  <div class="name">John Wick</div>
</button>
<div class="chat" aria-hidden="true">
  <div class="window">
    <div class="bubble guest">
      Hi man! Lorem ipsum dolor sit amet consectetur adipisicing elit.
      Aspernatur esse commodi.
    </div>
    <div class="bubble own">
      Sounds great!
    </div>
  </div>
  <div class="input">
    <input type="text" class="field" />
    <button class="send">Send</button>
  </div>
</div>
              
            
!

CSS

              
                :root {
  --chat-width: 250px;
}
.chat {
  position: fixed;
  right: var(--space-4);
  bottom: var(--space-12);
  width: var(--chat-width);
  border: var(--border);
  border-radius: var(--radius);
  opacity: 0;
  overflow: hidden;
  transform: translateY(var(--space-8)) scale(0.9);
  transform-origin: right;
  pointer-events: none;
  transition: var(--transition);
  background-color: var(--color-white);
}
.chat.visible {
  opacity: 1;
  pointer-events: all;
  transform: translateY(0) scale(1);
}
.window {
  display: flex;
  flex-direction: column;
  height: var(--chat-width);
  padding: var(--space-3);
  overflow-y: scroll;
  background-color: var(--color-white);
}
.bubble {
  max-width: 80%;
  font-size: var(--text-sm);
  padding: var(--space-1) var(--space-2);
  margin-bottom: var(--space-1);
  align-self: flex-start;
  word-break: break-word;
  background-color: var(--color-gray-100);
  border-radius: var(--radius);
}
.bubble:last-child {
  margin-bottom: 0;
}
.own {
  align-self: flex-end;
  background-color: var(--color-blue-500);
  color: var(--color-white);
}
.own + .guest {
  margin-top: var(--space-1);
}
.guest + .own {
  margin-top: var(--space-1);
}
.input {
  display: flex;
  padding: var(--space-2);
  border-top: var(--border);
}
.field {
  font-size: var(--text-md);
  padding: var(--space-1);
  border: 0;
  outline: none;
}
.send {
  padding: var(--space-2) var(--space-3);
  /* Align properly on FireFox */
  margin-left: auto;
  border: 0;
  font-weight: bold;
  border-radius: var(--radius);
  background-color: var(--color-gray-100);
  color: var(--color-gray-700);
  transition: var(--transition);
  outline: none;
}
.send:focus {
  box-shadow: var(--shadow-focus);
}
.send.active {
  cursor: pointer;
  color: var(--color-white);
  background-color: var(--color-blue-500);
}
/* Toggle */
.toggle {
  font-size: var(--text-sm);
  position: fixed;
  bottom: var(--space-2);
  right: var(--space-4);
  min-width: var(--space-16);
  padding: var(--space-2) var(--space-3);
  border: var(--border);
  border-radius: var(--radius);
  outline: none;
  user-select: none;
  background-color: var(--color-white);
  color: var(--color-gray-900);
  transition: min-width var(--transition-speed) var(--transition-curve);
}
.toggle:hover,
.toggle:focus {
  cursor: pointer;
  border-color: var(--color-gray-300);
}
.toggle:active {
  border-color: var(--color-gray-400);
}
.name {
  display: flex;
  align-items: center;
}
.name:before {
  display: block;
  content: "";
  width: var(--space-2);
  height: var(--space-2);
  margin-right: var(--space-2);
  border: 1px solid var(--color-green-600);
  background-color: var(--color-green-400);
  border-radius: var(--round);
}

              
            
!

JS

              
                // Selectors
const chatContainer = document.querySelector(".chat");
const chatField = document.querySelector(".field");
const sendButton = document.querySelector(".send");
const chatWindow = document.querySelector(".window");
const chatToggle = document.querySelector(".toggle");
const keyword = "coffee";
let menuOpen = false;

// Get chat toggle width
const originalWidth = chatToggle.offsetWidth;
// Set that width to be the minimum width of the toggle
chatToggle.style.minWidth = `${originalWidth}px`;

const addMsg = () => {
  // Check if field has content (and not only spaces)
  if (chatField.value.length > 0 && chatField.value.trim()) {
    // Create a new chat bubble from the user (hence "own")
    createBubble(chatWindow, chatField.value, "own");
    // Check if message has certain keyword...
    if (chatField.value.includes(keyword)) {
      // ...and respond to it.
      addResponse();
    }
    // Reset text field value
    chatField.value = "";
    // Disable send button
    sendButton.classList.remove("active");
    // Focus text field again
    chatField.focus();
  }
};

const createBubble = (destination, message, owner = "own") => {
  const chatBubble = document.createElement("div");
  chatBubble.className = "bubble";
  // Add separate classes for own and friend's messages
  if (owner == "own") {
    chatBubble.classList.add("own");
  } else {
    chatBubble.classList.add("guest");
  }
  // Insert message within bubble (note SECURITY)
  chatBubble.innerText = message;
  // Append child to destination
  destination.appendChild(chatBubble);
  // Scroll to the bottom of the destination container (chat window)
  destination.scrollTop = destination.scrollHeight;
};

const resizeToggle = (sourceWidth) => {
  // If menu is not open, set the width of the toggle equal to the chat window
  if (!menuOpen) {
    chatToggle.style.minWidth = `${sourceWidth}px`;
  } else {
    chatToggle.style.minWidth = `${originalWidth}px`;
  }
};

const toggleChat = (event) => {
  // Get chat window width
  chatWidth = chatContainer.offsetWidth;
  // Resize chat toggle
  resizeToggle(chatWidth);
  // Show chat
  chatContainer.classList.toggle("visible");
  // Expand toggle
  event.currentTarget.classList.toggle("open");
  // Tell screen readers that the content is not hidden anymore
  chatContainer.setAttribute("aria-hidden", false);
  // Toggle menu open variable
  menuOpen = !menuOpen;
  // Prevent event from bubbling
  event.stopPropagation();
  // Focus on the text field
  chatField.focus();
};

const closeChat = () => {
  // Close chat
  chatContainer.classList.remove("visible");
  // Let screenreaders know that content is hidden
  chatContainer.setAttribute("aria-hidden", true);
  // Reset toggle to close state
  chatToggle.classList.remove("open");
  resizeToggle();
  menuOpen = false;
  // Take focus out of chat text field...
  chatField.blur();
  // ...and focus on the toggle instead
  chatToggle.focus();
};

// Add event listeners to chat toggle and send button
chatToggle.addEventListener("click", toggleChat);
sendButton.addEventListener("click", addMsg);

// Add keyboard event listeners
window.addEventListener("keydown", function (e) {
  if (e.key == "Enter") {
    addMsg();
  }
});

window.addEventListener("keydown", (e) => {
  if (e.key == "Escape" && menuOpen) {
    closeChat();
  }
});

// Detect click outside the chat container to close chat
window.addEventListener("click", (e) => {
  console.log("container:", chatContainer);
  console.log("event target:", e.target);
  if (menuOpen && !chatContainer.contains(e.target)) {
    closeChat();
  }
});

// Toggle send button state based on chatfield content
chatField.addEventListener("input", () => {
  if (chatField.value.length > 0 && chatField.value.trim()) {
    sendButton.classList.add("active");
  } else {
    sendButton.classList.remove("active");
  }
});

// Add response to a message
const addResponse = () => {
  setTimeout(() => {
    createBubble(chatWindow, "Coffee sounds great!", "");
  }, 1000);
};

              
            
!
999px

Console