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

              
                <main>
	<h1>Prototype: Adaptive Web Component</h1>
	<x-component>
		<h2 slot="heading">Example Heading</h2>
		<p slot="description">Try resizing the window to see this component's children change order. For example, on desktop the heading is above this text but on mobile it drops below.</p>
	</x-component>
</main>

              
            
!

CSS

              
                html {
	box-sizing: border-box;
}

*,
::after,
::before {
	box-sizing: inherit;
}

body {
	margin: 1rem auto 3rem;
	line-height: 1.4;
	tab-size: 4;
}

main {
	max-width: 42rem;
	margin: 0 auto;
	padding-inline: 1rem;
}

pre {
	padding: 1rem;
	overflow: auto;
	border: 1px solid #ccc;
}

/* ul li { margin: 1rem 0; } */
/* ul li li { margin: 0.5rem 0; } */

/* Colors */
.colors > *:nth-child(7n + 1) { background-color: red; }
.colors > *:nth-child(7n + 2) { background-color: orange; }
.colors > *:nth-child(7n + 3) { background-color: yellow; }
.colors > *:nth-child(7n + 4) { background-color: green; }
.colors > *:nth-child(7n + 5) { background-color: blue; }
.colors > *:nth-child(7n + 6) { background-color: indigo; }
.colors > *:nth-child(7n + 7) { background-color: violet; }

              
            
!

JS

              
                const template = document.createElement('template');
template.innerHTML = `
	<style>
		.container {
			padding: 2rem;
			border-radius: 1rem;
			background: lightgray;
		}

		.top ::slotted(*) {
			margin-top: 0;
		}

		.bottom ::slotted(*) {
			margin-bottom: 0;
		}
	</style>
	<div class="container"></div>
`;

class XComponent extends HTMLElement {
	constructor() {
		super();

		// Init shadow DOM with template.
		this.attachShadow({ mode: 'open' });
		this.shadowRoot.appendChild(template.content.cloneNode(true));

		// Save reference to container element.
		this._container = this.shadowRoot.querySelector('div');

		// Track container size and breakpoints.
		// TODO: Make breakpoints configurable.
		this._width = this._container.getBoundingClientRect().width;
		this._breakpoints = [{ name: 'small', max: 499 }, { name: 'large' }];
		this._breakpoint = this.getBreakpoint();

		// Render once (to avoid infinite resize loops).
		this._render();

		// Declare resize observer.
		this._resizeObserver = new ResizeObserver(this.onResize.bind(this));
	}
	
	connectedCallback() {
		// On load, init resize observer.
		this._resizeObserver.observe(this._container);
	}

	getBreakpoint() {
		return this._breakpoints.find((bp) => {
			// Min & Max.
			if (bp.min && bp.max) {
				return this._width >= bp.min && this._width <= bp.max;
			}

			// Min only.
			if (bp.min && !bp.max) {
				return this._width >= bp.min;
			}

			// Max only.
			if (!bp.min && bp.max) {
				return this._width <= bp.max;
			}

			// Default.
			return true;
		});
	}

	onResize(entries) {
		// Assume first entry is container element.
		const entry = entries[0];

		// Has the container width changed?
		const { width } = entry.contentRect;

		if (this._width === width) {
			return;
		}

		this._width = width;

		// Is this a new breakpoint?
		const breakpoint = this.getBreakpoint();

		if (this._breakpoint === breakpoint.name) {
			return;
		}
		
		this._breakpoint = breakpoint.name;
		
		// Render the component.
		this._render();
	}
	
	_render() {
		console.log(this._width, this._breakpoint);

		if (this._breakpoint === 'small') {
			// Small
			this._container.innerHTML = `
				<div class="top"><slot name="description"></slot></div>
				<div class="bottom"><slot name="heading"></slot></div>
			`;
		} else {
			// Large
			this._container.innerHTML = `
				<div class="top"><slot name="heading"></slot></div>
				<div class="bottom"><slot name="description"></slot></div>
			`;
		}
	}
}

customElements.define('x-component', XComponent);

              
            
!
999px

Console