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

              
                <fancy-table-add-form fancy-table="the-only-table">
  <form>
    <label>
      <span>Name</span>
      <input required type="text" name="name" placeholder="e.g. Charles Bronson">
    </label>
    <label>
      <span>Power</span>
      <input required type="number" name="power" step="100" min="0" max="9000" width="30" placeholder="e.g. 500">
    </label>
    <input type="submit" value="Add New Action Hero">
  </form>
</fancy-table-add-form>
<fancy-table-filter fancy-table="the-only-table">
  <form>
    <label>
      <span>Search</span>
      <input type="search" name="filter-terms">
    </label>
    <button>Search</button>
  </form>
</fancy-table-filter>

<fancy-table id="the-only-table">
  <table>
    <thead>
      <tr>
        <th>
          <fancy-table-sort-button>
            <button>
              Name
            </button>
          </fancy-table-sort-button>
        </th>
        <th>
          <fancy-table-sort-button>
            <button>
              Power
            </button>
          </fancy-table-sort-button>
        </th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>Chuck Norris</td>
        <td>Infinite</td>
      </tr>
      <tr>
        <td>Bruce Lee</td>
        <td>9001</td>
      </tr>
      <tr>
        <td>Jean-Claude</td>
        <td>8012</td>
      </tr>
      <tr>
        <td>Statham</td>
        <td>7654</td>
      </tr>
      <tr>
        <td>The Rock</td>
        <td>8888</td>
      </tr>
      <tr>
        <td>Jet Li</td>
        <td>8000</td>
      </tr>
      <tr>
        <td>Jackie Chan</td>
        <td>7000</td>
      </tr>
    </tbody>
  </table>
</fancy-table>
              
            
!

CSS

              
                /* Styles to make the <th> of the table
 * reflect the state of the sort and allow
 * sorting to happen
 */
fancy-table-sort-button button {
  /* Not needed for functionality, but
   * this makes the button that initiates the sort 
   * fill up the space */
  width: 100%;
  display: block;
  border: none;
  padding-left: 1rem;
  padding-right: 1rem;
  padding-top: 0.5rem;
  padding-bottom: 0.5rem;
  cursor: pointer;
  background-color: #004400;
  color: white;
  font-size: 1.25rem;
}
fancy-table-sort-button button:active {
  /* Since we removed the borders, we need
   * to provide visual feedback that the
   * button is being clicked */
  background-color: #006600;
}

/* These styles provide a visual indicator
 * of the sort direction using the arai-sort 
 * attributes */
fancy-table-sort-button button:after {
  content: " ";
}
th[aria-sort="ascending"] fancy-table-sort-button button:after {
  content: "\2191"; /* Up arrow */
}
th[aria-sort="descending"] fancy-table-sort-button button:after {
  content: "\2193"; /* Down arrow */
}

/* Styles purely for decoration and visual
 * appeal
 */
form {
  padding: 1rem;
}

fancy-table-add-form form {
  padding: 0.25rem;
  border: solid thin gray;
  border-radius: 0.125rem;
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  align-items: flex-end;
  width: 300px;
}

fancy-table-add-form form label {
  display: flex;
  gap: 0.25rem;
  align-items: baseline;
  width: 100%;
}

fancy-table-add-form input[type="text"],
fancy-table-add-form input[type="number"] {
  padding: 0.25rem;
  width: 100%;
  display: block;
}
fancy-table-add-form input[type="submit"] {
  padding-left: 1rem;
  padding-right: 1rem;
  padding-top: 0.5rem;
  padding-bottom: 0.5rem;
  border: solid thin black;
  border-radius: 0.5rem;
  background-color: #111111;
  color: #dddddd;
  cursor: pointer;
}
fancy-table-add-form input[type="submit"]:active {
  background-color: #441111;
  color: #ffffff;
}

/* Hide button from conventional browsers, but preserve its existence for screen readers */
form button {
  position: absolute;
  left: -10000px;
  top: auto;
  width: 1px;
  height: 1px;
  overflow: hidden;
}
table {
  padding: 1rem;
}
td {
  padding: 0.5rem;
}
xtbody tr:not([hidden]):nth-of-type(odd) {
  background-color: #dfffdf;
}
tr[data-stripe="odd"] {
  background-color: #dfffdf;
}

              
            
!

JS

              
                class BaseHTMLElement extends HTMLElement {
  connectedCallback() {
    this.#observeMutations();
    this.update();
  }
  #mutationObserver = null;

  #mutated = (records, observer) => {
    if (this.update) {
      this.#stopObservingMutations();
      this.update();
      this.#observeMutations();
    }
  };

  #observeMutations() {
    this.#stopObservingMutations();
    if (!this.#mutationObserver) {
      this.#mutationObserver = new MutationObserver(this.#mutated);
    }
    this.#mutationObserver.observe(this, { subtree: true, childList: true });
  }

  #stopObservingMutations() {
    if (this.#mutationObserver) {
      this.#mutationObserver.disconnect();
    }
  }
}
class FancyTable extends BaseHTMLElement {
  static tagName = "fancy-table";
  static observedAttributes = ["sort-column", "sort-direction", "filter-terms"];

  #sortColumn = NaN;
  #sortDirection = "ascending";
  #filterTerms = null;

  attributeChangedCallback(name, oldValue, newValue) {
    if (name == "sort-column") {
      this.#sortColumn = parseInt(newValue);
    } else if (name == "sort-direction") {
      this.#sortDirection = newValue;
    } else if (name == "filter-terms") {
      this.#filterTerms = newValue ? newValue.toLowerCase() : null;
    }
    this.update();
  }

  update() {
    this.#sortTable();
    this.#filter();
    this.#stripe();
  }

  #stripe() {
    let index = 0;
    this.querySelectorAll("tbody tr").forEach((tr) => {
      if (!tr.getAttribute("hidden")) {
        tr.setAttribute("data-stripe", index % 2 == 0 ? "even" : "odd");
        index++;
      } else {
        tr.removeAttribute("data-stripe");
      }
    });
  }

  #sortTable() {
    const tbody = this.querySelector("table tbody");
    if (!tbody) {
      return;
    }
    const rows = Array.from(tbody.querySelectorAll("tr"));
    rows.sort((a, b) => {
      let sortColumnA = a.querySelectorAll("td")[this.#sortColumn];
      let sortColumnB = b.querySelectorAll("td")[this.#sortColumn];

      if (this.#sortDirection == "descending") {
        const swap = sortColumnA;

        sortColumnA = sortColumnB;
        sortColumnB = swap;
      }
      if (sortColumnA) {
        if (sortColumnB) {
          return sortColumnA.textContent.localeCompare(sortColumnB.textContent);
        } else {
          return 1;
        }
      } else if (sortColumnB) {
        return -1;
      } else {
        return 0;
      }
    });
    rows.forEach((row) => tbody.appendChild(row));
  }
  #filter() {
    let indexShown = 0;
    this.querySelectorAll("tbody tr").forEach((tr) => {
      if (this.#filterTerms) {
        tr.setAttribute("hidden", true);
        tr.querySelectorAll("td").forEach((td) => {
          if (td.textContent.toLowerCase().indexOf(this.#filterTerms) != -1) {
            tr.removeAttribute("hidden");
          }
        });
      } else {
        tr.removeAttribute("hidden");
      }
    });
  }
}

class FancyTableFilter extends BaseHTMLElement {
  static tagName = "fancy-table-filter";
  static observedAttributes = ["fancy-table"];

  #tableId = null;

  attributeChangedCallback(name, oldValue, newValue) {
    if (name == "fancy-table") {
      this.#tableId = newValue;
    }
  }

  update() {
    const form = this.querySelector("form");
    if (form) {
      form.addEventListener("submit", this.#formSubmitted);
    }
  }

  #formSubmitted = (event) => {
    event.preventDefault();
    const fancyTable = document.getElementById(this.#tableId);
    if (!fancyTable) {
      if (this.#tableId) {
        console.warn(
          "fancy-table was %s, however no object with that id exists",
          this.#tableId
        );
      }
      return;
    }
    const formData = new FormData(event.target);
    const filterTerms = formData.get("filter-terms");
    if (filterTerms) {
      fancyTable.setAttribute("filter-terms", filterTerms);
    } else {
      fancyTable.removeAttribute("filter-terms");
    }
  };
}

class FancyTableSortButton extends BaseHTMLElement {
  static tagName = "fancy-table-sort-button";

  update() {
    const button = this.querySelector("button");
    if (!button) {
      return;
    }

    button.addEventListener("click", this.#sort);
  }

  #sort = (event) => {
    const fancyTable = this.closest(FancyTable.tagName);
    if (!fancyTable) {
      return;
    }

    const th = this.closest("th");
    if (!th) {
      return;
    }

    const tr = th.closest("tr");
    if (!tr) {
      return;
    }

    const direction = th.getAttribute("aria-sort");
    let myIndex = -1;
    tr.querySelectorAll("th").forEach((th, index) => {
      if (th.querySelector(FancyTableSortButton.tagName) == this) {
        myIndex = index;
      }
      th.removeAttribute("aria-sort");
    });
    if (myIndex == -1) {
      return;
    }
    const newDirection = direction == "ascending" ? "descending" : "ascending";

    th.setAttribute("aria-sort", newDirection);
    fancyTable.setAttribute("sort-direction", newDirection);
    fancyTable.setAttribute("sort-column", myIndex);
  };
}

class FancyTableAddForm extends BaseHTMLElement {
  static tagName = "fancy-table-add-form";

  static observedAttributes = ["fancy-table"];

  #tableId = null;

  attributeChangedCallback(name, oldValue, newValue) {
    if (name == "fancy-table") {
      this.#tableId = newValue;
    }
  }

  #addHero = (event) => {
    event.preventDefault();
    const fancyTable = document.getElementById(this.#tableId);
    if (!fancyTable) {
      if (this.#tableId) {
        console.warn(
          "fancy-table was %s, however no object with that id exists",
          this.#tableId
        );
      }
      return;
    }

    const tbody = fancyTable.querySelector("tbody");
    if (!tbody) {
      return;
    }

    const formData = new FormData(event.target);
    const name = formData.get("name");
    const power = formData.get("power");

    const tr = document.createElement("tr");
    tr.innerHTML = `<td>${name}</td><td>${power}</td>`;
    tbody.appendChild(tr);
    event.target.reset();
  };

  update() {
    const form = this.querySelector("form");
    if (form) {
      form.addEventListener("submit", this.#addHero);
    }
  }
}

window.customElements.define(FancyTable.tagName, FancyTable);
window.customElements.define(FancyTableFilter.tagName, FancyTableFilter);
window.customElements.define(
  FancyTableSortButton.tagName,
  FancyTableSortButton
);
window.customElements.define(FancyTableAddForm.tagName, FancyTableAddForm);

              
            
!
999px

Console