Skip to content

Commit 03cfbec

Browse files
committed
Better click sim
1 parent 5648812 commit 03cfbec

File tree

3 files changed

+49
-13
lines changed

3 files changed

+49
-13
lines changed

packages/sim-user/src/index.tsx

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export { sim }
55
type ClickAction = {
66
type: "click"
77
selector: string
8+
offset?: number
89
}
910
type ScrollAction = {
1011
type: "scroll"
@@ -21,7 +22,8 @@ function scroll(action: ScrollAction, document: Document) {
2122
}
2223

2324
function click(action: ClickAction, document: Document) {
24-
const element = document.querySelector(action.selector)!
25+
const { selector, offset = 100 } = action
26+
const element = document.querySelector(selector)!
2527
// userEvent.click(element)
2628

2729
const elemRect = element.getBoundingClientRect()
@@ -32,8 +34,8 @@ function click(action: ClickAction, document: Document) {
3234
window.pageXOffset +
3335
(2 * elemRect.width) / 3
3436

35-
const startTop = offsetTop + 200
36-
const startLeft = offsetLeft + 200
37+
const startTop = offsetTop + offset
38+
const startLeft = offsetLeft + offset
3739
const cursor = createCursor()
3840
cursor.style.position = "absolute"
3941
cursor.style.top = startTop + "px"
@@ -47,20 +49,45 @@ function click(action: ClickAction, document: Document) {
4749
requestAnimationFrame(() =>
4850
requestAnimationFrame(() => {
4951
// console.log(element.offsetLeft)
50-
cursor.style.transform = `translate(${-200}px, ${-200}px) scale(1)`
52+
cursor.style.transform = `translate(${-offset}px, ${-offset}px) scale(1)`
5153
cursor.style.opacity = "1"
52-
cursor.style.scale = "1"
5354
})
5455
)
5556

5657
cursor.addEventListener(
5758
"transitionend",
5859
async () => {
5960
userEvent.click(element)
60-
cursor.style.opacity = "0"
61-
cursor.addEventListener("transitionend", () =>
62-
cursor.parentElement!.removeChild(cursor)
61+
62+
const circle = document.createElementNS(
63+
"http://www.w3.org/2000/svg",
64+
"circle"
6365
)
66+
circle.setAttribute("cx", "8")
67+
circle.setAttribute("cy", "5")
68+
circle.setAttribute("fill", "currentColor")
69+
circle.setAttribute("r", "3")
70+
circle.style.opacity = "0.99"
71+
circle.style.transformOrigin = "8px 5px"
72+
circle.style.transition =
73+
"transform 300ms, opacity 300ms"
74+
75+
const svg = cursor.firstChild as SVGElement
76+
77+
svg.innerHTML = ""
78+
svg.appendChild(circle)
79+
80+
requestAnimationFrame(() =>
81+
requestAnimationFrame(() => {
82+
circle.style.opacity = "0"
83+
circle.style.transform = `scale(10)`
84+
})
85+
)
86+
87+
circle.addEventListener("transitionend", () => {
88+
if (cursor.parentElement)
89+
cursor.parentElement.removeChild(cursor)
90+
})
6491
},
6592
{ once: true }
6693
)
@@ -82,7 +109,7 @@ function createCursor() {
82109
const div = document.createElement("div")
83110
div.style.color = "#7387c4"
84111
div.innerHTML = `<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
85-
style="display:block; filter: drop-shadow(0px 0px 1px rgba(0, 0, 0, .4))"
112+
style="display:block; overflow:visible; filter: drop-shadow(0px 0px 1px rgba(0, 0, 0, .4))"
86113
viewBox="8 5 28 28" enable-background="new 0 0 28 28" xml:space="preserve" height="28px" width="28px">
87114
<polygon fill="#FFFFFF" points="8.2,20.9 8.2,4.9 19.8,16.5 13,16.5 12.6,16.6 "/>
88115
<polygon fill="#FFFFFF" points="17.3,21.6 13.7,23.1 9,12 12.7,10.5 "/>

xmdx/demo/cake.mdx

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,21 @@
9191

9292
---
9393

94-
<Video src="000.mp4" start={0} end={5} />
94+
<Video src="001.mp4" start={0} end={8} />
9595
<Browser url="/07" zoom={0.9}>
9696
<Action on={2} type="click" selector="button" />
97-
<Action on={3} type="click" selector="button" />
98-
<Action on={4} type="click" selector="button" />
97+
<Action
98+
on={4}
99+
type="click"
100+
selector="button"
101+
offset={50}
102+
/>
103+
<Action
104+
on={6}
105+
type="click"
106+
selector="button"
107+
offset={50}
108+
/>
99109
</Browser>
100110
<Editor
101111
code="07.js"

xmdx/pages/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ function getStepsFromMDX(children) {
4040
children,
4141
child => child.props
4242
)
43-
console.log({ actions })
4443
// TODO fix production url
4544
return {
4645
url: "http://localhost:3000" + url,

0 commit comments

Comments
 (0)