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

              
                <!-- 
  Resize the preview pane until you see the scroll navigation. (Change View > Editor Layout > first layout is best)
  
  This is the stock HTML for the Polaris data-table component example pasted in here, with some Polaris-DataTable__Pip elements added, one for each column found.
  
  On top of the stock HTML, I added the necessary data- attributes to make the table-scroll Stimulus controller work. Just search the code for instances of "table-scroll" and you'll see the important Stimulus bits. The Stimulus controller picks that up (not just on load but on DOM update) and coordinates the interactivity.
  -->
  
  <div style="--top-bar-background: #00848e; --top-bar-background-lighter: #1d9ba4; --top-bar-color: #f9fafb; --p-frame-offset: 0px;">
    <div class="Polaris-Page">
      <div class="Polaris-Page-Header Polaris-Page-Header--mobileView">
        <div class="Polaris-Page-Header__MainContent">
          <div class="Polaris-Page-Header__TitleActionMenuWrapper">
            <div class="Polaris-Page-Header__TitleWrapper">
              <div>
                <div class="Polaris-Header-Title__TitleAndSubtitleWrapper">
                  <div class="Polaris-Header-Title">
                    <h1 class="Polaris-DisplayText Polaris-DisplayText--sizeLarge"><span class="Polaris-TextStyle--variationStrong">Sales by product</span></h1>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
      <div class="Polaris-Page__Content">
        <div class="Polaris-Card">
          <div data-controller="table-scroll"
               data-table-scroll-nav-shown-class="Polaris-DataTable--condensed"
               data-table-scroll-nav-hidden-class="Polaris-DataTable"
               data-table-scroll-button-disabled-class="Polaris-Button--disabled"
               data-table-scroll-indicator-visible-class="Polaris-DataTable__Pip--visible"
          >
            <div class="Polaris-DataTable"
                 data-table-scroll-target="navBar"
            >
              <div class="Polaris-DataTable__Navigation" style="justify-content: center;">
                <button     
                        type="button"
                        class="Polaris-Button Polaris-Button--disabled Polaris-Button--plain Polaris-Button--iconOnly"
                        disabled=""
                        aria-label="Scroll table left one column" 
                        data-table-scroll-target="leftButton"
                        data-action="table-scroll#scrollLeft"
                ><span class="Polaris-Button__Content"><span class="Polaris-Button__Icon"><span class="Polaris-Icon"><svg viewBox="0 0 20 20" class="Polaris-Icon__Svg" focusable="false" aria-hidden="true">
                          <path d="M12 16a.997.997 0 0 1-.707-.293l-5-5a.999.999 0 0 1 0-1.414l5-5a.999.999 0 1 1 1.414 1.414L8.414 10l4.293 4.293A.999.999 0 0 1 12 16z"></path>
                        </svg></span></span></span>
                </button>

                <div class="Polaris-DataTable__Pip"
                     data-table-scroll-target="columnVisibilityIndicator"></div>
                <div class="Polaris-DataTable__Pip"
                     data-table-scroll-target="columnVisibilityIndicator"></div>
                <div class="Polaris-DataTable__Pip"
                     data-table-scroll-target="columnVisibilityIndicator"></div>
                <div class="Polaris-DataTable__Pip"
                     data-table-scroll-target="columnVisibilityIndicator"></div>
                <div class="Polaris-DataTable__Pip"
                     data-table-scroll-target="columnVisibilityIndicator"></div>

                <button
                        type="button"
                        class="Polaris-Button Polaris-Button--plain Polaris-Button--iconOnly"
                        aria-label="Scroll table right one column"
                        data-table-scroll-target="rightButton"
                        data-action="table-scroll#scrollRight"
                ><span class="Polaris-Button__Content"><span class="Polaris-Button__Icon"><span class="Polaris-Icon"><svg viewBox="0 0 20 20" class="Polaris-Icon__Svg" focusable="false" aria-hidden="true">
                          <path d="M8 16a.999.999 0 0 1-.707-1.707L11.586 10 7.293 5.707a.999.999 0 1 1 1.414-1.414l5 5a.999.999 0 0 1 0 1.414l-5 5A.997.997 0 0 1 8 16z"></path>
                        </svg></span></span></span>
                </button>
              </div>
              <div class="Polaris-DataTable">
                <div class="Polaris-DataTable__ScrollContainer" data-table-scroll-target="scrollArea">
                  <table class="Polaris-DataTable__Table">
                    <thead>
                      <tr>
                        <th data-polaris-header-cell="true" class="Polaris-DataTable__Cell Polaris-DataTable__Cell--verticalAlignTop Polaris-DataTable__Cell--firstColumn Polaris-DataTable__Cell--header" scope="col" data-table-scroll-target="column">Product</th>
                        <th data-polaris-header-cell="true" class="Polaris-DataTable__Cell Polaris-DataTable__Cell--verticalAlignTop Polaris-DataTable__Cell--header Polaris-DataTable__Cell--numeric" scope="col" data-table-scroll-target="column">Price</th>
                        <th data-polaris-header-cell="true" class="Polaris-DataTable__Cell Polaris-DataTable__Cell--verticalAlignTop Polaris-DataTable__Cell--header Polaris-DataTable__Cell--numeric" scope="col" data-table-scroll-target="column">SKU Number</th>
                        <th data-polaris-header-cell="true" class="Polaris-DataTable__Cell Polaris-DataTable__Cell--verticalAlignTop Polaris-DataTable__Cell--header Polaris-DataTable__Cell--numeric" scope="col" data-table-scroll-target="column">Net quantity</th>
                        <th data-polaris-header-cell="true" class="Polaris-DataTable__Cell Polaris-DataTable__Cell--verticalAlignTop Polaris-DataTable__Cell--header Polaris-DataTable__Cell--numeric" scope="col" data-table-scroll-target="column">Net sales</th>
                      </tr>
                      <tr>
                        <th class="Polaris-DataTable__Cell Polaris-DataTable__Cell--verticalAlignTop Polaris-DataTable__Cell--firstColumn Polaris-DataTable__Cell--total" scope="row">Totals</th>
                        <td class="Polaris-DataTable__Cell Polaris-DataTable__Cell--verticalAlignTop Polaris-DataTable__Cell--total"></td>
                        <td class="Polaris-DataTable__Cell Polaris-DataTable__Cell--verticalAlignTop Polaris-DataTable__Cell--total"></td>
                        <td class="Polaris-DataTable__Cell Polaris-DataTable__Cell--verticalAlignTop Polaris-DataTable__Cell--total Polaris-DataTable__Cell--numeric">255</td>
                        <td class="Polaris-DataTable__Cell Polaris-DataTable__Cell--verticalAlignTop Polaris-DataTable__Cell--total Polaris-DataTable__Cell--numeric">$155,830.00</td>
                      </tr>
                    </thead>
                    <tbody>
                      <tr class="Polaris-DataTable__TableRow">
                        <th class="Polaris-DataTable__Cell Polaris-DataTable__Cell--verticalAlignTop Polaris-DataTable__Cell--firstColumn" scope="row">Emerald Silk Gown</th>
                        <td class="Polaris-DataTable__Cell Polaris-DataTable__Cell--verticalAlignTop Polaris-DataTable__Cell--numeric">$875.00</td>
                        <td class="Polaris-DataTable__Cell Polaris-DataTable__Cell--verticalAlignTop Polaris-DataTable__Cell--numeric">124689</td>
                        <td class="Polaris-DataTable__Cell Polaris-DataTable__Cell--verticalAlignTop Polaris-DataTable__Cell--numeric">140</td>
                        <td class="Polaris-DataTable__Cell Polaris-DataTable__Cell--verticalAlignTop Polaris-DataTable__Cell--numeric">$122,500.00</td>
                      </tr>
                      <tr class="Polaris-DataTable__TableRow">
                        <th class="Polaris-DataTable__Cell Polaris-DataTable__Cell--verticalAlignTop Polaris-DataTable__Cell--firstColumn" scope="row">Mauve Cashmere Scarf</th>
                        <td class="Polaris-DataTable__Cell Polaris-DataTable__Cell--verticalAlignTop Polaris-DataTable__Cell--numeric">$230.00</td>
                        <td class="Polaris-DataTable__Cell Polaris-DataTable__Cell--verticalAlignTop Polaris-DataTable__Cell--numeric">124533</td>
                        <td class="Polaris-DataTable__Cell Polaris-DataTable__Cell--verticalAlignTop Polaris-DataTable__Cell--numeric">83</td>
                        <td class="Polaris-DataTable__Cell Polaris-DataTable__Cell--verticalAlignTop Polaris-DataTable__Cell--numeric">$19,090.00</td>
                      </tr>
                      <tr class="Polaris-DataTable__TableRow">
                        <th class="Polaris-DataTable__Cell Polaris-DataTable__Cell--verticalAlignTop Polaris-DataTable__Cell--firstColumn" scope="row">Navy Merino Wool Blazer with khaki chinos and yellow belt</th>
                        <td class="Polaris-DataTable__Cell Polaris-DataTable__Cell--verticalAlignTop Polaris-DataTable__Cell--numeric">$445.00</td>
                        <td class="Polaris-DataTable__Cell Polaris-DataTable__Cell--verticalAlignTop Polaris-DataTable__Cell--numeric">124518</td>
                        <td class="Polaris-DataTable__Cell Polaris-DataTable__Cell--verticalAlignTop Polaris-DataTable__Cell--numeric">32</td>
                        <td class="Polaris-DataTable__Cell Polaris-DataTable__Cell--verticalAlignTop Polaris-DataTable__Cell--numeric">$14,240.00</td>
                      </tr>
                    </tbody>
                  </table>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
              
            
!

CSS

              
                
              
            
!

JS

              
                function supportsIntersectionObserver() {
  return (
    "IntersectionObserver" in window ||
    "IntersectionObserverEntry" in window ||
    "intersectionRatio" in window.IntersectionObserverEntry.prototype
  );
}

class TableScrollController extends Stimulus.Controller {
  static targets = [
    "navBar",
    "scrollArea",
    "column",
    "leftButton",
    "rightButton",
    "columnVisibilityIndicator",
  ];
  static classes = [
    "navShown",
    "navHidden",
    "buttonDisabled",
    "indicatorVisible",
  ];

  connect() {
    this.startObservingColumnVisibility();
  }

  disconnect() {
    this.stopObservingColumnVisibility();
  }

  startObservingColumnVisibility() {
    if (!supportsIntersectionObserver()) {
      console.warn(`This browser doesn't support IntersectionObserver`);
      return;
    }
    
    this.intersectionObserver = new IntersectionObserver(
      this.updateScrollNavigation.bind(this),
      {
        root: this.scrollAreaTarget,
        threshold: 0.99, // otherwise, the right-most column sometimes won't be considered visible in some browsers, rounding errors, etc.
      }
    );

    this.columnTargets.forEach((headingEl) => {
      this.intersectionObserver.observe(headingEl);
    });
  }

  stopObservingColumnVisibility() {
    if (this.intersectionObserver) {
      this.intersectionObserver.disconnect();
    }
  }

  updateScrollNavigation(observerRecords) {
    observerRecords.forEach((record) => {
      record.target.dataset.isVisible = record.isIntersecting;
    });
    
    this.toggleScrollNavigationVisibility();
    this.updateColumnVisibilityIndicators();
    this.updateLeftRightButtonAffordance();
  }

  toggleScrollNavigationVisibility() {
    const allColumnsVisible =
      this.columnTargets.length > 0 &&
      this.columnTargets[0].dataset.isVisible === "true" &&
      this.columnTargets[this.columnTargets.length - 1].dataset.isVisible ===
        "true";
    
    if (allColumnsVisible) {
      this.navBarTarget.classList.remove(this.navShownClass);
      this.navBarTarget.classList.add(this.navHiddenClass);
    } else {
      this.navBarTarget.classList.add(this.navShownClass);
      this.navBarTarget.classList.remove(this.navHiddenClass);
    }
  }

  updateColumnVisibilityIndicators() {
    this.columnTargets.forEach((headingEl, index) => {
      const indicator = this.columnVisibilityIndicatorTargets[index];

      if (indicator) {
        indicator.classList.toggle(
          this.indicatorVisibleClass,
          headingEl.dataset.isVisible === "true"
        );
      }
    });
  }

  updateLeftRightButtonAffordance() {
    const firstColumnHeading = this.columnTargets[0];
    const lastColumnHeading = this.columnTargets[this.columnTargets.length - 1];

    this.updateButtonAffordance(
      this.leftButtonTarget,
      firstColumnHeading.dataset.isVisible === "true"
    );
    this.updateButtonAffordance(
      this.rightButtonTarget,
      lastColumnHeading.dataset.isVisible === "true"
    );
  }

  updateButtonAffordance(button, isDisabled) {
    if (isDisabled) {
      button.setAttribute("disabled", "");
      button.classList.add(this.buttonDisabledClass);
    } else {
      button.removeAttribute("disabled");
      button.classList.remove(this.buttonDisabledClass);
    }
  }

  scrollLeft() {
    // scroll to make visible the first non-fully-visible column to the left of the scroll area
    let columnToScrollTo = null;
    for (let i = 0; i < this.columnTargets.length; i++) {
      const column = this.columnTargets[i];
      if (columnToScrollTo !== null && column.dataset.isVisible === "true") {
        break;
      }
      if (column.dataset.isVisible === "false") {
        columnToScrollTo = column;
      }
    }

    this.scrollAreaTarget.scroll(columnToScrollTo.offsetLeft, 0);
  }

  scrollRight() {
    // scroll to make visible the first non-fully-visible column to the right of the scroll area
    let columnToScrollTo = null;
    for (let i = this.columnTargets.length - 1; i >= 0; i--) {
      // right to left
      const column = this.columnTargets[i];
      if (columnToScrollTo !== null && column.dataset.isVisible === "true") {
        break;
      }
      if (column.dataset.isVisible === "false") {
        columnToScrollTo = column;
      }
    }

    this.scrollAreaTarget.scroll(columnToScrollTo.offsetLeft, 0);
  }
}

const application = Stimulus.Application.start()
application.register('table-scroll', TableScrollController)



              
            
!
999px

Console