Last active
October 10, 2025 20:46
-
-
Save KonnorRogers/ce25361b278148e107d16501d6740f37 to your computer and use it in GitHub Desktop.
draggable...fun times.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1"> | |
<title></title> | |
</head> | |
<body> | |
<script data-fa-kit-code="38c11e3f20" type="module" src="https://early.webawesome.com/[email protected]/dist/webawesome.loader.js"></script> | |
<link rel="stylesheet" href="https://early.webawesome.com/[email protected]/dist/styles/webawesome.css"> | |
<script type="module"> | |
const dialog = document.querySelector("#draggable-dialog") | |
// not supported in Chrome 140, but supported in FF 143 | |
// const getInnerDialog = () => dialog.shadowRoot.querySelector("::part(dialog)") | |
const getInnerDialog = () => dialog.shadowRoot.querySelector("[part~='dialog']") | |
let pointerId = null | |
dialog.addEventListener("pointerdown", startDrag) | |
dialog.addEventListener("pointerup", stopDrag) | |
dialog.addEventListener("pointermove", drag) | |
let innerDialog = null | |
let isDragging = false; | |
let offsetX = null | |
let offsetY = null | |
function startDrag(e) { | |
isDragging = true; | |
innerDialog = getInnerDialog() | |
const clientRect = innerDialog.getBoundingClientRect() | |
console.log(offsetX, offsetY) | |
offsetX = e.clientX - clientRect.x; | |
offsetY = e.clientY - clientRect.y; | |
const newX = e.clientX - offsetX; | |
const newY = e.clientY - offsetY; | |
dialog.style.setProperty("--left", `${newX}px`); | |
dialog.style.setProperty("--right", `unset`) | |
dialog.style.setProperty("--top", `${newY}px`); | |
dialog.style.setProperty("--bottom", `unset`); | |
dialog.setPointerCapture(e.pointerId) | |
} | |
function stopDrag(e) { | |
isDragging = false | |
dialog.releasePointerCapture(e.pointerId) | |
} | |
function drag(e) { | |
if (!isDragging) { return } | |
console.log("dragging") | |
const newX = e.clientX - offsetX; | |
const newY = e.clientY - offsetY; | |
dialog.style.setProperty("--left", `${newX}px`); | |
dialog.style.setProperty("--top", `${newY}px`); | |
} | |
</script> | |
<style> | |
#draggable-dialog { | |
position: absolute; | |
&::part(dialog) { | |
margin: 0; | |
bottom: var(--bottom, 0); | |
right: var(--right, 0); | |
top: var(--top, unset); | |
left: var(--left, unset); | |
} | |
} | |
</style> | |
<main style="padding: 4rem;"> | |
<wa-button data-dialog="open draggable-dialog">Open Draggable Window</wa-button> | |
</main> | |
<!-- set draggable="true" and break everything. --> | |
<wa-dialog | |
id="draggable-dialog" | |
> | |
<wa-input label="To"></wa-input> | |
<wa-divider></wa-divider> | |
<wa-input label="cc"></wa-input> | |
<wa-divider></wa-divider> | |
<wa-textarea label="Message"></wa-textarea> | |
</wa-dialog> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment