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

Auto Save

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

              
                <h1>Santa's Naughty or Nice List</h1>

<ul class="santas-list">
  <li>
    <list-item>
      <span slot="name">Priscilla Lloyd</span>
    </list-item>
  </li>
  <li>
    <list-item>
      <span slot="name">Reece Zavala</span>
    </list-item>
  </li>
  <li>
    <list-item>
      <span slot="name">Ann Wiggins</span>
    </list-item>
  </li>
  <li>
    <list-item>
      <span slot="name">Bentley Lloyd</span>
    </list-item>
  </li>
  <li>
    <list-item>
      <span slot="name">Ahmad Yang</span>
    </list-item>
  </li>
  <li>
    <list-item>
      <span slot="name">Callum Robertson</span>
    </list-item>
  </li>
  <li>
    <list-item>
      <span slot="name">Kailee Kidd</span>
    </list-item>
  </li>
  <li>
    <list-item>
      <span slot="name">Monica Kaufman</span>
    </list-item>
  </li>
  <li>
    <list-item>
      <span slot="name">Jeremiah Anthony</span>
    </list-item>
  </li>
  <li>
    <list-item>
      <span slot="name">Gracie Blankenship</span>
    </list-item>
  </li>
  <li>
    <list-item>
      <span slot="name">Heidi Tapia</span>
    </list-item>
  </li>
  <li>
    <list-item>
      <span slot="name">Kian Patel</span>
    </list-item>
  </li>
</ul>

<template id="list-item">
  <style>
    :host {
      display: grid;
      align-items: center;
      padding: 0.5rem;
      row-gap: 0.5rem;
      background-color: white;
      border-radius: 0.5rem;
      box-shadow: 0 0 4px -1px hsl(0 0% 35%);
    }

    @container list (inline-size > 400px) {
      :host {
        grid-template-columns: subgrid;
        grid-column: 1 / -1;
      }
    }

    .actions {
      display: flex;
      flex-wrap: wrap;
      gap: 1rem;
    }

    button {
      cursor: pointer;
      display: inline-flex;
      align-items: center;
      justify-content: center;
      font: inherit;
      gap: 0.25em;
      border: 1px solid black;
      border-radius: 0.25em;
      padding: 0.25em 0.5em;
      text-align: center;
    }

    button:focus-visible {
      outline: 2px solid black;
      outline-offset: 2px;
    }

    [aria-pressed="false"] {
      min-width: calc-size(max-content, size + 1em);
    }

    [aria-pressed="true"] {
      background-color: green;
      color: white;

      &#naughty {
        background-color: firebrick;
      }
    }

    [aria-pressed="true"]::before {
      content: "✓";
    }
  </style>

  <span id="name" class="name">
    <slot name="name"></slot>
  </span>
  <div aria-labelledby="name" role="group" class="actions">
    <button id="nice" aria-pressed="true">Nice</button>
    <button id="naughty" aria-pressed="false">Naughty</button>
  </div>
</template>
              
            
!

CSS

              
                * {
  box-sizing: border-box;
  margin: 0;
}

body {
  font-family: system-ui;
  background-color: whitesmoke;
}

h1 {
  font-family: cursive;
  text-align: center;
  padding: 1rem;
  text-wrap: pretty;
  font-weight: bold;
  color: firebrick;
  font-size: clamp(1.75rem, 8cqi, 4rem);
  line-height: 1.2;
}

.santas-list {
  list-style: none;
  padding: 0;
  inline-size: min(80ch, 100% - 2rem);
  margin: 5vh auto;
  display: grid;
  grid-template-columns: 1fr auto;
  container: list / inline-size;
  gap: 1rem;

  li {
    display: grid;
    grid-template-columns: subgrid;
    grid-column: 1 / -1;
  }
}

              
            
!

JS

              
                const santasList = new BroadcastChannel("santasList");

customElements.define(
  "list-item",
  class extends HTMLElement {
    constructor() {
      super();
      const template = document.getElementById("list-item");
      const listItem = template.content;

      const shadowRoot = this.attachShadow({ mode: "open" }).appendChild(
        listItem.cloneNode(true)
      );
    }

    connectedCallback() {
      // Get child's name
      const nameSlot = this.querySelector('[slot="name"]');
      const name = nameSlot?.textContent;

      /*
       * Updates button state from both in-page button presses and broadcasts
       *
       * id: 'naughty'|'nice' - presssed button id
       * pressed: boolean - pressed button state
       * postUpdate: boolean - true if triggered from in-page button
       */
      const handleListUpdate = (id, pressed, postUpdate) => {
        const button = this.shadowRoot.getElementById(id);
        const otherButtonId = id === "nice" ? "naughty" : "nice";

        button.setAttribute("aria-pressed", `${pressed}`);
        this.shadowRoot
          .getElementById(otherButtonId)
          .setAttribute("aria-pressed", `${!pressed}`);

        if (postUpdate) {
          // Post message with updated list item button state
          santasList.postMessage({
            name,
            id,
            pressed
          });
        }
      };

      // Handle list item button clicks
      this.shadowRoot.addEventListener("click", (e) => {
        if (!e.target.closest("button")) return;

        const button = e.target;
        const ariaPressed =
          button.getAttribute("aria-pressed") === "true" ? false : true;

        handleListUpdate(button.id, ariaPressed, true);
      });

      // Respond to custom event containing broadcast message data
      document.addEventListener("update-list", ({ detail }) => {
        if (detail.name !== name) return;

        // Pass off to update list item button state
        handleListUpdate(detail.id, detail.pressed);
      });
    }
  }
);

// Custom event to share broadcast message data
const updateListEvent = (detail) =>
  document.dispatchEvent(new CustomEvent("update-list", { detail }));

// BroadcastChannel message event listener
santasList.addEventListener("message", ({ data }) => {
  const { name, id, pressed } = data;

  // Trigger custom event
  updateListEvent({ name, id, pressed });
});

              
            
!
999px

Console