@stacksjs/stx 0.2.564 → 0.2.566
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{chunk-ezdsaxbb.js → chunk-01s01ten.js} +3 -3
- package/dist/chunk-04qvjcsr.js +2 -0
- package/dist/{chunk-7phshjwf.js → chunk-125rc958.js} +1 -1
- package/dist/{chunk-f1z9ekjh.js → chunk-18270qgd.js} +2 -2
- package/dist/{chunk-a9q8c4pm.js → chunk-1hrj0t0j.js} +8 -8
- package/dist/{chunk-wxkphrnt.js → chunk-2ernwctg.js} +3 -3
- package/dist/chunk-2yypassp.js +2 -0
- package/dist/{chunk-qyq7edq6.js → chunk-325p135t.js} +1 -1
- package/dist/{chunk-f5n26rq9.js → chunk-36aj0yzt.js} +1 -1
- package/dist/{chunk-m6826w3k.js → chunk-375skn32.js} +1 -1
- package/dist/{chunk-rqc62964.js → chunk-3hkv4e7t.js} +1 -1
- package/dist/chunk-4mmdpb46.js +2 -0
- package/dist/{chunk-ec8k75ep.js → chunk-4zb2ga7p.js} +2 -2
- package/dist/{chunk-4bhjt6xn.js → chunk-55hfvmvv.js} +5 -5
- package/dist/{chunk-0897e87r.js → chunk-5grx48s2.js} +1 -1
- package/dist/{chunk-4ct7a53d.js → chunk-5s555c1r.js} +3 -3
- package/dist/chunk-5ytg0wbc.js +2 -0
- package/dist/{chunk-97614ghv.js → chunk-6sf4v894.js} +2 -2
- package/dist/{chunk-zycahp89.js → chunk-6yn90mq9.js} +1 -1
- package/dist/chunk-7d2vyre0.js +2 -0
- package/dist/{chunk-vjx8gtfk.js → chunk-8afnq4qx.js} +2 -2
- package/dist/{chunk-658paz71.js → chunk-8cpqy9ea.js} +1506 -1506
- package/dist/chunk-8d788kvt.js +2 -0
- package/dist/{chunk-m6zmvcq2.js → chunk-8txqk9nq.js} +1 -1
- package/dist/{chunk-hb95ah88.js → chunk-90qywh24.js} +3 -3
- package/dist/chunk-bdhtq3vs.js +2 -0
- package/dist/chunk-brnapchc.js +2 -0
- package/dist/{chunk-7cfpb2n8.js → chunk-c765y72x.js} +2 -2
- package/dist/{chunk-p47kzj6s.js → chunk-d1mqedk0.js} +1 -1
- package/dist/{chunk-0fv4x8fn.js → chunk-da9avgd9.js} +1 -1
- package/dist/{chunk-8b6mp92r.js → chunk-dmf1jmge.js} +2 -2
- package/dist/chunk-e3njkecx.js +2 -0
- package/dist/{chunk-86cnpvf4.js → chunk-efzc1nw8.js} +1 -1
- package/dist/{chunk-ymcsz527.js → chunk-ev3qnr9q.js} +2 -2
- package/dist/{chunk-5zxb7dsf.js → chunk-fm4w1r9e.js} +1 -1
- package/dist/{chunk-m7h6wx3r.js → chunk-g1epk002.js} +1 -1
- package/dist/chunk-hmk7z0z6.js +2 -0
- package/dist/{chunk-q0xcf4zq.js → chunk-hn105xy3.js} +1 -1
- package/dist/chunk-jq079mrx.js +2 -0
- package/dist/{chunk-kj4fpfjj.js → chunk-jr149mmp.js} +1 -1
- package/dist/{chunk-4ga3g8xc.js → chunk-kwxy0e3r.js} +1 -1
- package/dist/chunk-kza3nxr0.js +2 -0
- package/dist/chunk-ma2yh8zc.js +63 -0
- package/dist/chunk-mahv855w.js +2 -0
- package/dist/{chunk-rxvnfge3.js → chunk-mb59b499.js} +4 -4
- package/dist/{chunk-w9157aaz.js → chunk-mm1vvk49.js} +1 -1
- package/dist/{chunk-d7w1n5jm.js → chunk-mt8sew7a.js} +5 -5
- package/dist/chunk-n2g9mj2h.js +2 -0
- package/dist/{chunk-m3drjrkh.js → chunk-n2nha538.js} +1 -1
- package/dist/{chunk-1dfd95ev.js → chunk-n6va14bt.js} +1 -1
- package/dist/chunk-nebw444s.js +2 -0
- package/dist/{chunk-hdswffnj.js → chunk-nm0y4qjg.js} +1 -1
- package/dist/chunk-nmt7kw0e.js +2 -0
- package/dist/{chunk-46195rpy.js → chunk-pzasxhwz.js} +1 -1
- package/dist/chunk-qajc13zq.js +2 -0
- package/dist/chunk-qtnqmw1n.js +2 -0
- package/dist/chunk-r3zdgeah.js +2 -0
- package/dist/chunk-r5htacg5.js +2 -0
- package/dist/{chunk-fh4vpewx.js → chunk-rf2rwddd.js} +1 -1
- package/dist/{chunk-9jvzz9pr.js → chunk-s1xxkzc9.js} +4 -4
- package/dist/chunk-s833fbvf.js +2 -0
- package/dist/chunk-sgvszh9s.js +2 -0
- package/dist/{chunk-et82hw8n.js → chunk-ss78553t.js} +1 -1
- package/dist/chunk-t5r3s1wj.js +2 -0
- package/dist/{chunk-jh9ajakd.js → chunk-tc01jkfc.js} +1 -1
- package/dist/{chunk-8sfv1h98.js → chunk-tfeenp4h.js} +4 -4
- package/dist/chunk-tjprg6kq.js +2 -0
- package/dist/{chunk-30wqqwbn.js → chunk-tsmbstj6.js} +1 -1
- package/dist/{chunk-vc6dn0jf.js → chunk-vtz9empe.js} +1 -1
- package/dist/{chunk-ztfvrpp6.js → chunk-wa6nzq5b.js} +1 -1
- package/dist/{chunk-tq28tg3k.js → chunk-wm11csdm.js} +1 -1
- package/dist/{chunk-pjgzqz07.js → chunk-wwn9dh6p.js} +1 -1
- package/dist/{chunk-4aw3whbc.js → chunk-xw8kzm6b.js} +2104 -2104
- package/dist/chunk-ytdzgctp.js +2 -0
- package/dist/{chunk-yje1skpw.js → chunk-z2n1rtj5.js} +1 -1
- package/dist/cli.js +11 -11
- package/dist/craft-entry.js +2 -2
- package/dist/craft.js +2 -2
- package/dist/database.js +2 -2
- package/dist/native/components/primitives.d.ts +20 -4
- package/dist/native/index.d.ts +3 -0
- package/dist/native/primitives/Map.stx +2 -1
- package/dist/native/prop-names.js +3 -0
- package/dist/native/runtime/craft-bridge.js +31 -12
- package/dist/native/runtime/craft.d.ts +7 -5
- package/dist/pwa.js +1 -1
- package/dist/ssg.js +2 -2
- package/dist/stx.d.ts +1 -1028
- package/dist/variable-extractor.js +2 -2
- package/package.json +6 -5
- package/src/composables/use-mouse.ts +438 -0
- package/stx.d.ts +2 -0
- package/dist/chunk-1g4t7df6.js +0 -63
- package/dist/chunk-1k38dc96.js +0 -2
- package/dist/chunk-4ss6b94k.js +0 -2
- package/dist/chunk-592xm851.js +0 -2
- package/dist/chunk-5adcz47v.js +0 -2
- package/dist/chunk-7bqkgjxv.js +0 -2
- package/dist/chunk-8218m54a.js +0 -2
- package/dist/chunk-b7yvq1kw.js +0 -2
- package/dist/chunk-bhyrzt2n.js +0 -2
- package/dist/chunk-d80xsgse.js +0 -2
- package/dist/chunk-e7ddshxz.js +0 -2
- package/dist/chunk-eh6n83az.js +0 -2
- package/dist/chunk-enb8dpkt.js +0 -2
- package/dist/chunk-fh8q5rnd.js +0 -2
- package/dist/chunk-fm5tm1b7.js +0 -2
- package/dist/chunk-gkq300a7.js +0 -2
- package/dist/chunk-h5jpbxda.js +0 -2
- package/dist/chunk-jzmtpjns.js +0 -2
- package/dist/chunk-m1bczftm.js +0 -2
- package/dist/chunk-nkarb6fg.js +0 -2
- package/dist/chunk-qc66nfp7.js +0 -2
- package/dist/chunk-sg2bn15n.js +0 -2
- package/dist/chunk-sn98t7ef.js +0 -2
- package/dist/chunk-sz243f3v.js +0 -2
- package/dist/chunk-t2ec3fek.js +0 -2
- package/dist/chunk-xnfjemzj.js +0 -2
- /package/dist/{chunk-nd7vkt4w.js → chunk-m8qgr307.js} +0 -0
|
@@ -669,14 +669,14 @@ export function convertToCommonJS(scriptContent, filePath) {
|
|
|
669
669
|
convertedLines.push(`useSeoMeta({ ${lifted.join(", ")} });`);
|
|
670
670
|
continue;
|
|
671
671
|
}
|
|
672
|
-
if (line.startsWith("import ") || line === "import" || line.startsWith("import{")) {
|
|
672
|
+
if (line.startsWith("import ") || line === "import" || line.startsWith("import{") || /^import['"]/.test(line)) {
|
|
673
673
|
let statement = line, consumed = 0;
|
|
674
674
|
while (!isCompleteImport(statement) && i + consumed + 1 < lines.length) {
|
|
675
675
|
consumed++;
|
|
676
676
|
statement = `${statement} ${lines[i + consumed].trim()}`;
|
|
677
677
|
}
|
|
678
678
|
i += consumed;
|
|
679
|
-
const defaultImportMatch = statement.match(/^import\s+(\w+)\s+from\s+['"](.+?)['"]/), namedImportMatch = statement.match(/^import\s+\{([^}]*)\}\s+from\s+['"](.+?)['"]/), sideEffectMatch = statement.match(/^import\s
|
|
679
|
+
const defaultImportMatch = statement.match(/^import\s+(\w+)\s+from\s+['"](.+?)['"]/), namedImportMatch = statement.match(/^import\s+\{([^}]*)\}\s+from\s+['"](.+?)['"]/), sideEffectMatch = statement.match(/^import\s*['"](.+?)['"]/), line_ = statement, resolveSource = (source) => {
|
|
680
680
|
if (source.startsWith("."))
|
|
681
681
|
return path.resolve(templateDir, source);
|
|
682
682
|
if (source.startsWith("~/") || source.startsWith("@/"))
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stacksjs/stx",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.2.
|
|
4
|
+
"version": "0.2.566",
|
|
5
5
|
"description": "A performant UI Framework. Powered by Bun.",
|
|
6
6
|
"author": "Chris Breuer <chris@stacksjs.org>",
|
|
7
7
|
"license": "MIT",
|
|
@@ -88,6 +88,7 @@
|
|
|
88
88
|
"LICENSE.md",
|
|
89
89
|
"README.md",
|
|
90
90
|
"dist",
|
|
91
|
+
"src/composables/use-mouse.ts",
|
|
91
92
|
"stx.d.ts",
|
|
92
93
|
"browser-auto-imports.d.ts",
|
|
93
94
|
"stx-module.d.ts"
|
|
@@ -123,12 +124,12 @@
|
|
|
123
124
|
"dependencies": {
|
|
124
125
|
"@stacksjs/ts-css": "^0.3.11",
|
|
125
126
|
"@stacksjs/clapp": "^0.2.16",
|
|
126
|
-
"@stacksjs/desktop": "0.2.
|
|
127
|
-
"@stacksjs/sanitizer": "0.2.
|
|
127
|
+
"@stacksjs/desktop": "0.2.566",
|
|
128
|
+
"@stacksjs/sanitizer": "0.2.566",
|
|
128
129
|
"@stacksjs/ts-i18n": "^0.1.11",
|
|
129
|
-
"bun-plugin-stx": "0.2.
|
|
130
|
+
"bun-plugin-stx": "0.2.566",
|
|
130
131
|
"bunfig": "^0.15.19",
|
|
131
|
-
"stx-router": "0.2.
|
|
132
|
+
"stx-router": "0.2.566",
|
|
132
133
|
"ts-broadcasting": "^0.0.11",
|
|
133
134
|
"ts-images": "^0.2.22",
|
|
134
135
|
"ts-syntax-highlighter": "^0.2.1"
|
|
@@ -0,0 +1,438 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* useMouse - Reactive mouse/pointer position tracking
|
|
3
|
+
*
|
|
4
|
+
* Track mouse position, movement, and button states.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```ts
|
|
8
|
+
* const mouse = useMouse()
|
|
9
|
+
* mouse.subscribe(state => {
|
|
10
|
+
* console.log(`Mouse at ${state.x}, ${state.y}`)
|
|
11
|
+
* })
|
|
12
|
+
*
|
|
13
|
+
* // Track within a specific element
|
|
14
|
+
* const elementMouse = useMouseInElement(myElement)
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
export interface MouseState {
|
|
19
|
+
x: number
|
|
20
|
+
y: number
|
|
21
|
+
pageX: number
|
|
22
|
+
pageY: number
|
|
23
|
+
clientX: number
|
|
24
|
+
clientY: number
|
|
25
|
+
movementX: number
|
|
26
|
+
movementY: number
|
|
27
|
+
buttons: number
|
|
28
|
+
isPressed: boolean
|
|
29
|
+
isInside: boolean
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface MouseOptions {
|
|
33
|
+
/** Target element (default: window) */
|
|
34
|
+
target?: Window | HTMLElement | null
|
|
35
|
+
/** Track touch events as mouse */
|
|
36
|
+
touch?: boolean
|
|
37
|
+
/** Reset to 0,0 when pointer leaves */
|
|
38
|
+
resetOnLeave?: boolean
|
|
39
|
+
/** Event type: 'page', 'client', 'screen', 'movement' */
|
|
40
|
+
type?: 'page' | 'client' | 'screen' | 'movement'
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export interface MouseRef {
|
|
44
|
+
get: () => MouseState
|
|
45
|
+
subscribe: (fn: (state: MouseState) => void) => () => void
|
|
46
|
+
stop: () => void
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export interface ElementMouseState extends MouseState {
|
|
50
|
+
elementX: number
|
|
51
|
+
elementY: number
|
|
52
|
+
elementWidth: number
|
|
53
|
+
elementHeight: number
|
|
54
|
+
isOutside: boolean
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export interface ElementMouseRef {
|
|
58
|
+
get: () => ElementMouseState
|
|
59
|
+
subscribe: (fn: (state: ElementMouseState) => void) => () => void
|
|
60
|
+
stop: () => void
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Track mouse position globally or within a target
|
|
65
|
+
*/
|
|
66
|
+
export function useMouse(options: MouseOptions = {}): MouseRef {
|
|
67
|
+
const {
|
|
68
|
+
target = typeof window !== 'undefined' ? window : null,
|
|
69
|
+
touch = true,
|
|
70
|
+
resetOnLeave = false,
|
|
71
|
+
} = options
|
|
72
|
+
|
|
73
|
+
let state: MouseState = {
|
|
74
|
+
x: 0,
|
|
75
|
+
y: 0,
|
|
76
|
+
pageX: 0,
|
|
77
|
+
pageY: 0,
|
|
78
|
+
clientX: 0,
|
|
79
|
+
clientY: 0,
|
|
80
|
+
movementX: 0,
|
|
81
|
+
movementY: 0,
|
|
82
|
+
buttons: 0,
|
|
83
|
+
isPressed: false,
|
|
84
|
+
isInside: false,
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
let listeners: Array<(state: MouseState) => void> = []
|
|
88
|
+
let cleanup: (() => void) | null = null
|
|
89
|
+
|
|
90
|
+
const notify = () => {
|
|
91
|
+
listeners.forEach(fn => fn(state))
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const handleMove = (event: MouseEvent | Touch, isTouch = false) => {
|
|
95
|
+
const e = event as MouseEvent
|
|
96
|
+
state = {
|
|
97
|
+
x: e.pageX ?? e.clientX,
|
|
98
|
+
y: e.pageY ?? e.clientY,
|
|
99
|
+
pageX: e.pageX ?? 0,
|
|
100
|
+
pageY: e.pageY ?? 0,
|
|
101
|
+
clientX: e.clientX ?? 0,
|
|
102
|
+
clientY: e.clientY ?? 0,
|
|
103
|
+
movementX: isTouch ? 0 : (e.movementX ?? 0),
|
|
104
|
+
movementY: isTouch ? 0 : (e.movementY ?? 0),
|
|
105
|
+
buttons: isTouch ? 1 : (e.buttons ?? 0),
|
|
106
|
+
isPressed: isTouch || (e.buttons ?? 0) > 0,
|
|
107
|
+
isInside: true,
|
|
108
|
+
}
|
|
109
|
+
notify()
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const handleMouseMove = (e: MouseEvent) => handleMove(e)
|
|
113
|
+
const handleTouchMove = (e: TouchEvent) => {
|
|
114
|
+
if (e.touches.length > 0) {
|
|
115
|
+
handleMove(e.touches[0], true)
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const handleMouseDown = (e: MouseEvent) => {
|
|
120
|
+
state = { ...state, buttons: e.buttons, isPressed: true }
|
|
121
|
+
notify()
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const handleMouseUp = (e: MouseEvent) => {
|
|
125
|
+
state = { ...state, buttons: e.buttons, isPressed: false }
|
|
126
|
+
notify()
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const handleEnter = () => {
|
|
130
|
+
state = { ...state, isInside: true }
|
|
131
|
+
notify()
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
const handleLeave = () => {
|
|
135
|
+
state = {
|
|
136
|
+
...state,
|
|
137
|
+
isInside: false,
|
|
138
|
+
...(resetOnLeave ? { x: 0, y: 0, pageX: 0, pageY: 0, clientX: 0, clientY: 0 } : {}),
|
|
139
|
+
}
|
|
140
|
+
notify()
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const setupListeners = () => {
|
|
144
|
+
if (!target) return
|
|
145
|
+
|
|
146
|
+
const t = target as EventTarget
|
|
147
|
+
|
|
148
|
+
t.addEventListener('mousemove', handleMouseMove as EventListener)
|
|
149
|
+
t.addEventListener('mousedown', handleMouseDown as EventListener)
|
|
150
|
+
t.addEventListener('mouseup', handleMouseUp as EventListener)
|
|
151
|
+
t.addEventListener('mouseenter', handleEnter)
|
|
152
|
+
t.addEventListener('mouseleave', handleLeave)
|
|
153
|
+
|
|
154
|
+
if (touch) {
|
|
155
|
+
t.addEventListener('touchmove', handleTouchMove as EventListener)
|
|
156
|
+
t.addEventListener('touchstart', handleEnter)
|
|
157
|
+
t.addEventListener('touchend', handleLeave)
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
cleanup = () => {
|
|
161
|
+
t.removeEventListener('mousemove', handleMouseMove as EventListener)
|
|
162
|
+
t.removeEventListener('mousedown', handleMouseDown as EventListener)
|
|
163
|
+
t.removeEventListener('mouseup', handleMouseUp as EventListener)
|
|
164
|
+
t.removeEventListener('mouseenter', handleEnter)
|
|
165
|
+
t.removeEventListener('mouseleave', handleLeave)
|
|
166
|
+
|
|
167
|
+
if (touch) {
|
|
168
|
+
t.removeEventListener('touchmove', handleTouchMove as EventListener)
|
|
169
|
+
t.removeEventListener('touchstart', handleEnter)
|
|
170
|
+
t.removeEventListener('touchend', handleLeave)
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
return {
|
|
176
|
+
get: () => state,
|
|
177
|
+
subscribe: (fn) => {
|
|
178
|
+
if (listeners.length === 0) {
|
|
179
|
+
setupListeners()
|
|
180
|
+
}
|
|
181
|
+
listeners.push(fn)
|
|
182
|
+
fn(state)
|
|
183
|
+
|
|
184
|
+
return () => {
|
|
185
|
+
listeners = listeners.filter(l => l !== fn)
|
|
186
|
+
if (listeners.length === 0 && cleanup) {
|
|
187
|
+
cleanup()
|
|
188
|
+
cleanup = null
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
},
|
|
192
|
+
stop: () => {
|
|
193
|
+
if (cleanup) {
|
|
194
|
+
cleanup()
|
|
195
|
+
cleanup = null
|
|
196
|
+
}
|
|
197
|
+
listeners = []
|
|
198
|
+
},
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Track mouse position relative to an element
|
|
204
|
+
*/
|
|
205
|
+
// eslint-disable-next-line pickier/no-unused-vars
|
|
206
|
+
export function useMouseInElement(
|
|
207
|
+
element: HTMLElement | (() => HTMLElement | null) | null,
|
|
208
|
+
options: Omit<MouseOptions, 'target'> = {}
|
|
209
|
+
): ElementMouseRef {
|
|
210
|
+
const { touch = true, resetOnLeave = true } = options
|
|
211
|
+
|
|
212
|
+
let state: ElementMouseState = {
|
|
213
|
+
x: 0,
|
|
214
|
+
y: 0,
|
|
215
|
+
pageX: 0,
|
|
216
|
+
pageY: 0,
|
|
217
|
+
clientX: 0,
|
|
218
|
+
clientY: 0,
|
|
219
|
+
movementX: 0,
|
|
220
|
+
movementY: 0,
|
|
221
|
+
buttons: 0,
|
|
222
|
+
isPressed: false,
|
|
223
|
+
isInside: false,
|
|
224
|
+
elementX: 0,
|
|
225
|
+
elementY: 0,
|
|
226
|
+
elementWidth: 0,
|
|
227
|
+
elementHeight: 0,
|
|
228
|
+
isOutside: true,
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
let listeners: Array<(state: ElementMouseState) => void> = []
|
|
232
|
+
let cleanup: (() => void) | null = null
|
|
233
|
+
|
|
234
|
+
const notify = () => {
|
|
235
|
+
listeners.forEach(fn => fn(state))
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
const getElement = (): HTMLElement | null => {
|
|
239
|
+
if (typeof element === 'function') return element()
|
|
240
|
+
return element
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
const updateElementPosition = (clientX: number, clientY: number) => {
|
|
244
|
+
const el = getElement()
|
|
245
|
+
if (!el) return
|
|
246
|
+
|
|
247
|
+
const rect = el.getBoundingClientRect()
|
|
248
|
+
const elementX = clientX - rect.left
|
|
249
|
+
const elementY = clientY - rect.top
|
|
250
|
+
const isOutside = elementX < 0 || elementY < 0 || elementX > rect.width || elementY > rect.height
|
|
251
|
+
|
|
252
|
+
state = {
|
|
253
|
+
...state,
|
|
254
|
+
elementX,
|
|
255
|
+
elementY,
|
|
256
|
+
elementWidth: rect.width,
|
|
257
|
+
elementHeight: rect.height,
|
|
258
|
+
isOutside,
|
|
259
|
+
isInside: !isOutside,
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
const handleMove = (event: MouseEvent | Touch, isTouch = false) => {
|
|
264
|
+
const e = event as MouseEvent
|
|
265
|
+
state = {
|
|
266
|
+
...state,
|
|
267
|
+
x: e.pageX ?? e.clientX,
|
|
268
|
+
y: e.pageY ?? e.clientY,
|
|
269
|
+
pageX: e.pageX ?? 0,
|
|
270
|
+
pageY: e.pageY ?? 0,
|
|
271
|
+
clientX: e.clientX ?? 0,
|
|
272
|
+
clientY: e.clientY ?? 0,
|
|
273
|
+
movementX: isTouch ? 0 : (e.movementX ?? 0),
|
|
274
|
+
movementY: isTouch ? 0 : (e.movementY ?? 0),
|
|
275
|
+
buttons: isTouch ? 1 : (e.buttons ?? 0),
|
|
276
|
+
isPressed: isTouch || (e.buttons ?? 0) > 0,
|
|
277
|
+
}
|
|
278
|
+
updateElementPosition(e.clientX, e.clientY)
|
|
279
|
+
notify()
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
const handleMouseMove = (e: MouseEvent) => handleMove(e)
|
|
283
|
+
const handleTouchMove = (e: TouchEvent) => {
|
|
284
|
+
if (e.touches.length > 0) {
|
|
285
|
+
handleMove(e.touches[0], true)
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
const handleLeave = () => {
|
|
290
|
+
state = {
|
|
291
|
+
...state,
|
|
292
|
+
isInside: false,
|
|
293
|
+
isOutside: true,
|
|
294
|
+
...(resetOnLeave ? { elementX: 0, elementY: 0 } : {}),
|
|
295
|
+
}
|
|
296
|
+
notify()
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
const setupListeners = () => {
|
|
300
|
+
if (typeof window === 'undefined') return
|
|
301
|
+
|
|
302
|
+
// Listen on window to track even outside element
|
|
303
|
+
window.addEventListener('mousemove', handleMouseMove)
|
|
304
|
+
|
|
305
|
+
if (touch) {
|
|
306
|
+
window.addEventListener('touchmove', handleTouchMove)
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
cleanup = () => {
|
|
310
|
+
window.removeEventListener('mousemove', handleMouseMove)
|
|
311
|
+
if (touch) {
|
|
312
|
+
window.removeEventListener('touchmove', handleTouchMove)
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
return {
|
|
318
|
+
get: () => state,
|
|
319
|
+
subscribe: (fn) => {
|
|
320
|
+
if (listeners.length === 0) {
|
|
321
|
+
setupListeners()
|
|
322
|
+
}
|
|
323
|
+
listeners.push(fn)
|
|
324
|
+
fn(state)
|
|
325
|
+
|
|
326
|
+
return () => {
|
|
327
|
+
listeners = listeners.filter(l => l !== fn)
|
|
328
|
+
if (listeners.length === 0 && cleanup) {
|
|
329
|
+
cleanup()
|
|
330
|
+
cleanup = null
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
},
|
|
334
|
+
stop: () => {
|
|
335
|
+
if (cleanup) {
|
|
336
|
+
cleanup()
|
|
337
|
+
cleanup = null
|
|
338
|
+
}
|
|
339
|
+
listeners = []
|
|
340
|
+
},
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
/**
|
|
345
|
+
* Track pointer (unified mouse + touch + pen)
|
|
346
|
+
*/
|
|
347
|
+
export function usePointer(options: MouseOptions = {}): MouseRef {
|
|
348
|
+
const { target = typeof window !== 'undefined' ? window : null } = options
|
|
349
|
+
|
|
350
|
+
let state: MouseState = {
|
|
351
|
+
x: 0,
|
|
352
|
+
y: 0,
|
|
353
|
+
pageX: 0,
|
|
354
|
+
pageY: 0,
|
|
355
|
+
clientX: 0,
|
|
356
|
+
clientY: 0,
|
|
357
|
+
movementX: 0,
|
|
358
|
+
movementY: 0,
|
|
359
|
+
buttons: 0,
|
|
360
|
+
isPressed: false,
|
|
361
|
+
isInside: false,
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
let listeners: Array<(state: MouseState) => void> = []
|
|
365
|
+
let cleanup: (() => void) | null = null
|
|
366
|
+
|
|
367
|
+
const notify = () => {
|
|
368
|
+
listeners.forEach(fn => fn(state))
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
const handlePointerMove = (e: PointerEvent) => {
|
|
372
|
+
state = {
|
|
373
|
+
x: e.pageX,
|
|
374
|
+
y: e.pageY,
|
|
375
|
+
pageX: e.pageX,
|
|
376
|
+
pageY: e.pageY,
|
|
377
|
+
clientX: e.clientX,
|
|
378
|
+
clientY: e.clientY,
|
|
379
|
+
movementX: e.movementX,
|
|
380
|
+
movementY: e.movementY,
|
|
381
|
+
buttons: e.buttons,
|
|
382
|
+
isPressed: e.buttons > 0 || e.pressure > 0,
|
|
383
|
+
isInside: true,
|
|
384
|
+
}
|
|
385
|
+
notify()
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
const handlePointerDown = (e: PointerEvent) => {
|
|
389
|
+
state = { ...state, buttons: e.buttons, isPressed: true }
|
|
390
|
+
notify()
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
const handlePointerUp = (e: PointerEvent) => {
|
|
394
|
+
state = { ...state, buttons: e.buttons, isPressed: false }
|
|
395
|
+
notify()
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
const setupListeners = () => {
|
|
399
|
+
if (!target) return
|
|
400
|
+
|
|
401
|
+
const t = target as EventTarget
|
|
402
|
+
t.addEventListener('pointermove', handlePointerMove as EventListener)
|
|
403
|
+
t.addEventListener('pointerdown', handlePointerDown as EventListener)
|
|
404
|
+
t.addEventListener('pointerup', handlePointerUp as EventListener)
|
|
405
|
+
|
|
406
|
+
cleanup = () => {
|
|
407
|
+
t.removeEventListener('pointermove', handlePointerMove as EventListener)
|
|
408
|
+
t.removeEventListener('pointerdown', handlePointerDown as EventListener)
|
|
409
|
+
t.removeEventListener('pointerup', handlePointerUp as EventListener)
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
return {
|
|
414
|
+
get: () => state,
|
|
415
|
+
subscribe: (fn) => {
|
|
416
|
+
if (listeners.length === 0) {
|
|
417
|
+
setupListeners()
|
|
418
|
+
}
|
|
419
|
+
listeners.push(fn)
|
|
420
|
+
fn(state)
|
|
421
|
+
|
|
422
|
+
return () => {
|
|
423
|
+
listeners = listeners.filter(l => l !== fn)
|
|
424
|
+
if (listeners.length === 0 && cleanup) {
|
|
425
|
+
cleanup()
|
|
426
|
+
cleanup = null
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
},
|
|
430
|
+
stop: () => {
|
|
431
|
+
if (cleanup) {
|
|
432
|
+
cleanup()
|
|
433
|
+
cleanup = null
|
|
434
|
+
}
|
|
435
|
+
listeners = []
|
|
436
|
+
},
|
|
437
|
+
}
|
|
438
|
+
}
|
package/stx.d.ts
CHANGED
|
@@ -638,6 +638,8 @@ declare function useDark(_options?: StxColorModeOptions): StxSignal<boolean> & {
|
|
|
638
638
|
readonly isDark: boolean
|
|
639
639
|
toggle: () => void
|
|
640
640
|
}
|
|
641
|
+
/** Demand-bundled from the actual pointer composable; keep its options and subscription state in one source. */
|
|
642
|
+
declare const useMouse: typeof import('./src/composables/use-mouse').useMouse
|
|
641
643
|
declare function useMediaQuery(query: string): StxSignal<boolean> & {
|
|
642
644
|
readonly matches: boolean
|
|
643
645
|
readonly value: boolean
|