HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
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.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
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.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
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.
Using packages here is powered by esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<!--
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>
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)
Also see: Tab Triggers