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

              
                <div class="min-h-screen p-32 bg-gray-100">


<div
	x-data="previewHandler()"
	x-init="setup"
	class="relative max-w-3xl mx-auto shadow"
	style="display:none;">
	<div class="flex justify-between bg-indigo-500 p-2 leading-none ">
		<div class="text-white font-extrabold">AlpineJs</div>
		<div class="divide-x-2 divide-indigo-600">
			<!-- todo: Update to use icons -->
			<button
				class="px-2 text-white hover:underline focus:outline-none"
				@click="$refs.pane.innerHTML = component.outerHTML">demo</button>
			<button
				class="px-2 text-white hover:underline focus:outline-none"
				@click="$refs.pane.innerHTML = snippet.outerHTML">source</button>
			<button
				class="px-2 text-white hover:underline focus:outline-none"
				@click.prevent="selectSnippet($event)"
				@copy.document.prevent="copySnippet($event)">copy source</button>
		</div>
	</div>
	<!-- todo: prevent back button when user is scrolling inside the pane -->
	<div x-ref="pane" class="relative flex items-center justify-center" style="min-height:300px;background:#d2d8e3">
		<pre>
			<div x-data="{ open: false }" class="relative">
				<button
					@click="open = !open"
					@click.away="open = false"
					x-text="open ? 'close' : 'open'"
					class="rounded w-24 py-2 bg-blue-500 hover:bg-blue-400 text-white font-bold focus:outline-none">
				</button>
				<div
					class="absolute mt-3 bg-white w-64 h-24 flex text-xl items-center justify-center rounded-lg shadow-lg"
					x-show.transition="open">
					Hey there!
				</div>
			</div>
		</pre>
	</div>
	<span
		class="italic text-sm absolute bottom-0 right-0 mr-2 mb-2"
		x-show.transition.opacity.duration.350ms="copied"
		x-cloak>Copied!</span>
</div>
	
	
</div>
              
            
!

CSS

              
                [x-cloak] { display: none; }
              
            
!

JS

              
                // TODO: accept parameters like height, width, theme, etc
function previewHandler() {
	return {
		component: null,
		snippet: null,
		copied: false,
		setup() {
			// TODO: Doing this here is too late as Alpine already has set it up
			this.component = this.$el.querySelector('[x-data]')
			this.buildSnippet()
			this.$refs.pane.innerHTML = this.component.outerHTML
			this.$el.style.setProperty('display', 'block')
		},
		buildSnippet() {
			this.snippet = document.createElement('pre')
			this.snippet.classList.add('absolute', 'inset-0', 'overflow-scroll', 'bg-gray-200', 'font-mono', 'p-4', 'pt-8')
			this.snippet.appendChild(this.component)
			let html = this.snippet.innerHTML
			let tokens = lolight.tok(html)

			tokens = tokens.map((token, index) => {

				// If it's a return or new line
				if (/\r|\n/.exec(token[1])) {
					
					// The return should be first, ie strip leading spaces
					token[1] = token[1].replace(/\s+[\n|\r]/, '')
					
					// Check the length of the \r + tabs
					const length = (/\r|\n/.exec(token[1]).input.length)
					
					// Only set the spaces the first time
					if (typeof spacesOffset === 'undefined') {
						// The first indent should always be 1 space, 
						// so with the \r that would mean a length of 2
						spacesOffset = 2 - length
					}
					token[1] = token[1].slice(0, (spacesOffset ? spacesOffset : length))
					
					// Replace tabs with 4 spaces
					token[1] = token[1].replace(/\t/g, '&nbsp;&nbsp;&nbsp;&nbsp;')
					
					return token[1]
				}
				// TODO: Might be nicer to put each directive on it's own line
				// But it will take some time to get the indentation right
				// Perhaps use the spaceOffset from above...
				// if (token[1] === '@') return '\n\t@'
				// if (token[1] === ':') return '\n\t:'
				// if (token[0] === 'key') {
				// 	if (tokens[index + 1] && tokens[index - 1][0] == 'pct') {
				// 		return token[1]
				// 	}
				// 	return '\n\t' + token[1]
				// }
				return token[1].replace(/[\u00A0-\u9999<>\&]/gim, i => '&#'+i.charCodeAt(0)+';')
			}).join('')
			this.snippet.innerHTML = tokens
			lolight.el(this.snippet) // https://larsjung.de/lolight/
		},
		selectSnippet(event) {
			const range = document.createRange()
			range.selectNode(event.target)
			window.getSelection().removeAllRanges()
			window.getSelection().addRange(range)
            document.execCommand('copy')
		},
		copySnippet(event) {
			if (this.copied) return
			window.getSelection().removeAllRanges()
			event.clipboardData.setData('text/plain', this.component.outerHTML)
			this.copied = true
			setTimeout(() => {
				this.copied = false
			}, 2000)
		}
	}
}


              
            
!
999px

Console