Pen Settings



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


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


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.


Save Automatically?

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.


                <div class="min-h-screen justify-center flex p-16 bg-blue-200">
<!-- This is a revised version. See the original clunky version here:	 -->

<p class="mb-10 text-center">Drag & drop or press the menu icon button <br>(or use your tab key)</p>

<div class="pt-6 pb-4 bg-indigo-500 rounded-lg shadow-xl max-w-sm">
<h1 id="agenda-title" class="text-white font-extrabold text-lg p-6 pt-0">What's the agenda for today?</h1>
	x-title="Sorting Demo"
	x-data="dragAndSortHandler(items)""usedKeyboard = true"
	@dragleave="dropcheck--;dropcheck || rePositionPlaceholder()"
	<template x-for="(item, index) in items" :key="index">
			@dragend="$'draggable', false)"
			class="border-b border-transparent"
				'opacity-25': indexBeingDragged == index,
			<!-- Pointer events are disabled while dragging, otherwise drag events fire on child elements -->
			<div class="bg-indigo-300 p-6 flex justify-between"
				 :class="{'pointer-events-none': indexBeingDragged}">
				<p x-text=""></p>
				<div class="relative" aria-haspopup="true">
					<!-- Lots of events are here as it combines click drag, click, and keyboard events -->
						aria-label="Sorting menu"
						:class="{'focus:outline-none': !usedKeyboard}">
							class="block w-6 text-indigo-500"
							viewBox="0 0 20 20"
								 d="M3 5a1 1 0 011-1h12a1 1 0 110 2H4a1 1 0 01-1-1zM3 10a1 1 0 011-1h12a1 1 0 110 2H4a1 1 0 01-1-1zM3 15a1 1 0 011-1h12a1 1 0 110 2H4a1 1 0 01-1-1z"
						:aria-expanded="(openedContextMenu == index).toString()"
						x-show="openedContextMenu == index"
						x-transition:enter="transition ease-in duration-100"
						x-transition:enter-start="transform opacity-75 -translate-y-1"
						x-transition:leave-end="transform -translate-y-1 opacity-0"
						class="absolute w-32 bg-indigo-500 py-2 -mt-3 left-0 transform -translate-x-12 z-50 shadow-lg rounded text-sm">
						<li role="menuitem">
								@click="index && move(index, index - 1)"
								class="text-left w-full pl-4 hover:bg-indigo-400"
								:class="{'focus:outline-none': !usedKeyboard}"
								Move up
						<!-- hard coded for two options. If you need more then you need a new method -->
						<li role="menuitem">
								@click="(index + 1 < items.length) && move(index + 1, index)"
								class="text-left w-full pl-4 hover:bg-indigo-400"
								:class="{'focus:outline-none': !usedKeyboard}">
								Move down


<!-- Dev tools -->


                [x-cloak] { display: none; }


                const items = [
		name: 'Learn about the draggable API',
		name: 'Practice guitar',
		name: 'Read a novel for 25 minutes',
		name: 'Practice Thai vocabulary',
		name: 'Sleep',
function dragAndSortHandler(items) {
	return {
		// Keeps track of when we leave the dropzone
		// Otherwise child events will trigger @dragloave
		dropcheck: 0,
		usedKeyboard: false,
		originalIndexBeingDragged: null,
		indexBeingDragged: null,
		indexBeingDraggedOver: null,
		openedContextMenu: null,
		items: items,
		preDragOrder: items,
		dragstart(event) {
			if (this.openedContextMenu) {
				// Without this the drag will show the context menu
				return this.closeContextMenu()	
			// Store a copy for when we drag out of range
			this.preDragOrder = [...this.items]
			// The index is continuously updated to reorder live and also keep a placeholder
			this.indexBeingDragged ='x-ref')
			// The original is needed for then the drag leaves the container
			this.originalIndexBeingDragged ='x-ref')
			// Not entirely sure this is needed but moz recommended it (?)
			event.dataTransfer.dropEffect = "copy"
		updateListOrder(event) {
			// This fires every time you drag over another list item
			// It reorders the items array but maintains the placeholder 
			if (this.indexBeingDragged) {
				this.indexBeingDraggedOver ='x-ref')
				let from = this.indexBeingDragged
				let to = this.indexBeingDraggedOver
				if (this.indexBeingDragged == to) return
				if (from == to) return

				this.move(from, to)
				this.indexBeingDragged = to
		// These are needed for the handle effect
		setParentDraggable(event) {'li').setAttribute('draggable', true)
		setParentNotDraggable(event) {'li').setAttribute('draggable', false)
		resetState() {
			this.dropcheck = 0
			this.indexBeingDragged = null
			this.preDragOrder = [...this.items]
			this.indexBeingDraggedOver = null
			this.originalIndexBeingDragged = null
		// This acts as a cancelled event, when the item is dropped outside the container
		revertState() {
			this.items = this.preDragOrder.length ? this.preDragOrder : this.items
		// Just repositions the placeholder when we move out of range of the container
		rePositionPlaceholder() {
			this.items = [...this.preDragOrder]
			this.indexBeingDragged = this.originalIndexBeingDragged
		move(from, to) {
			let items = this.items
    		if (to >= items.length) {
        		let k = to - items.length + 1
				while (k--) {
			items.splice(to, 0, items.splice(from, 1)[0])
			this.items = items
		// THe rest are just for adding better UX to the context menu
		openContextMenu(event) {
			this.openedContextMenu ='li').__x_for_key
		closeAllContextMenus() {	
			this.openedContextMenu = null
		highlightFirstContextButton($event) {'button').focus()
		highlightNextContextMenuItem(event) {'li').nextElementSibling.querySelector('button').focus()
		highlightPreviousContextMenuItem(event) {'li').previousElementSibling.querySelector('button').focus()