@solid-primitives/mouse
Advanced tools
Comparing version 2.0.8-beta.0 to 2.0.8
@@ -1,5 +0,5 @@ | ||
import { makeEventListenerStack } from "@solid-primitives/event-listener"; | ||
import { clamp, createStaticStore } from "@solid-primitives/utils"; | ||
import { createSharedRoot } from "@solid-primitives/rootless"; | ||
import { createEffect, createComputed, onMount } from "solid-js"; | ||
import { makeEventListenerStack } from '@solid-primitives/event-listener'; | ||
import { clamp, createStaticStore } from '@solid-primitives/utils'; | ||
import { createSharedRoot } from '@solid-primitives/rootless'; | ||
import { createEffect, createComputed, onMount } from 'solid-js'; | ||
@@ -12,3 +12,3 @@ // src/common.ts | ||
isInside: false, | ||
sourceType: null, | ||
sourceType: null | ||
}; | ||
@@ -22,3 +22,3 @@ var DEFAULT_RELATIVE_ELEMENT_POSITION = { | ||
height: 0, | ||
isInside: false, | ||
isInside: false | ||
}; | ||
@@ -28,7 +28,7 @@ function makeMousePositionListener(target = window, callback, options = {}) { | ||
const [listen, clear] = makeEventListenerStack(target, PASSIVE); | ||
const handleMouse = e => callback({ x: e.pageX, y: e.pageY, sourceType: "mouse" }); | ||
const handleMouse = (e) => callback({ x: e.pageX, y: e.pageY, sourceType: "mouse" }); | ||
listen("mousemove", handleMouse); | ||
listen("dragover", handleMouse); | ||
if (touch) { | ||
const handleTouch = e => { | ||
const handleTouch = (e) => { | ||
if (e.touches.length) | ||
@@ -38,3 +38,4 @@ callback({ x: e.touches[0].clientX, y: e.touches[0].clientY, sourceType: "touch" }); | ||
listen("touchstart", handleTouch); | ||
if (followTouch) listen("touchmove", handleTouch); | ||
if (followTouch) | ||
listen("touchmove", handleTouch); | ||
} | ||
@@ -49,3 +50,3 @@ return clear; | ||
function handleChange(isInside) { | ||
this === "mouse" ? (mouseIn = isInside) : (touchIn = isInside); | ||
this === "mouse" ? mouseIn = isInside : touchIn = isInside; | ||
callback(mouseIn || touchIn); | ||
@@ -63,8 +64,3 @@ } | ||
var getPositionToElement = (pageX, pageY, el) => { | ||
const bounds = el.getBoundingClientRect(), | ||
top = bounds.top + window.scrollY, | ||
left = bounds.left + window.scrollX, | ||
x = pageX - left, | ||
y = pageY - top, | ||
{ width, height } = bounds; | ||
const bounds = el.getBoundingClientRect(), top = bounds.top + window.scrollY, left = bounds.left + window.scrollX, x = pageX - left, y = pageY - top, { width, height } = bounds; | ||
return { | ||
@@ -77,3 +73,3 @@ x, | ||
height, | ||
isInside: x >= 0 && y >= 0 && x <= width && y <= height, | ||
isInside: x >= 0 && y >= 0 && x <= width && y <= height | ||
}; | ||
@@ -86,3 +82,3 @@ }; | ||
x: clamp(relative.x, 0, relative.width), | ||
y: clamp(relative.y, 0, relative.height), | ||
y: clamp(relative.y, 0, relative.height) | ||
}; | ||
@@ -92,3 +88,3 @@ }; | ||
x: pageX - window.scrollX, | ||
y: pageY - window.screenY, | ||
y: pageY - window.screenY | ||
}); | ||
@@ -98,14 +94,16 @@ function createMousePosition(target, options = {}) { | ||
...DEFAULT_MOUSE_POSITION, | ||
...options.initialValue, | ||
...options.initialValue | ||
}); | ||
const attachListeners = el => { | ||
const attachListeners = (el) => { | ||
makeMousePositionListener(el, setState, options); | ||
makeMouseInsideListener(el, setState.bind(void 0, "isInside"), options); | ||
}; | ||
if (typeof target !== "function") attachListeners(target); | ||
else createEffect(() => attachListeners(target())); | ||
if (typeof target !== "function") | ||
attachListeners(target); | ||
else | ||
createEffect(() => attachListeners(target())); | ||
return state; | ||
} | ||
var useMousePosition = /* @__PURE__ */ createSharedRoot( | ||
createMousePosition.bind(void 0, void 0, void 0), | ||
createMousePosition.bind(void 0, void 0, void 0) | ||
); | ||
@@ -115,30 +113,19 @@ function createPositionToElement(element, pos, options = {}) { | ||
...DEFAULT_RELATIVE_ELEMENT_POSITION, | ||
...options.initialValue, | ||
...options.initialValue | ||
}; | ||
const calcState = el => { | ||
const calcState = (el) => { | ||
const { x, y } = pos(); | ||
return getPositionToElement(x, y, el); | ||
}; | ||
const getState = | ||
typeof element === "function" | ||
? () => { | ||
const el = element(); | ||
return el ? calcState(el) : fallback; | ||
} | ||
: calcState.bind(void 0, element); | ||
const getState = typeof element === "function" ? () => { | ||
const el = element(); | ||
return el ? calcState(el) : fallback; | ||
} : calcState.bind(void 0, element); | ||
const [state, setState] = createStaticStore(fallback); | ||
createComputed(() => setState(getState())); | ||
if (typeof element === "function") onMount(() => setState(getState())); | ||
if (typeof element === "function") | ||
onMount(() => setState(getState())); | ||
return state; | ||
} | ||
export { | ||
createMousePosition, | ||
createPositionToElement, | ||
getPositionInElement, | ||
getPositionToElement, | ||
getPositionToScreen, | ||
makeMouseInsideListener, | ||
makeMousePositionListener, | ||
useMousePosition, | ||
}; | ||
export { createMousePosition, createPositionToElement, getPositionInElement, getPositionToElement, getPositionToScreen, makeMouseInsideListener, makeMousePositionListener, useMousePosition }; |
@@ -1,31 +0,31 @@ | ||
import { Position, MaybeAccessor } from "@solid-primitives/utils"; | ||
import { Accessor } from "solid-js"; | ||
import { Position, MaybeAccessor } from '@solid-primitives/utils'; | ||
import { Accessor } from 'solid-js'; | ||
type MouseSourceType = "mouse" | "touch" | null; | ||
type MousePosition = Position & { | ||
sourceType: MouseSourceType; | ||
sourceType: MouseSourceType; | ||
}; | ||
type MousePositionInside = MousePosition & { | ||
isInside: boolean; | ||
isInside: boolean; | ||
}; | ||
interface PositionRelativeToElement extends Position { | ||
top: number; | ||
left: number; | ||
width: number; | ||
height: number; | ||
isInside: boolean; | ||
top: number; | ||
left: number; | ||
width: number; | ||
height: number; | ||
isInside: boolean; | ||
} | ||
interface UseTouchOptions { | ||
/** | ||
* Listen to touch events. If enabled, position will be updated on `touchstart` event. | ||
* @default true | ||
*/ | ||
touch?: boolean; | ||
/** | ||
* Listen to touch events. If enabled, position will be updated on `touchstart` event. | ||
* @default true | ||
*/ | ||
touch?: boolean; | ||
} | ||
interface FollowTouchOptions { | ||
/** | ||
* If enabled, position will be updated on `touchmove` event. | ||
* @default true | ||
*/ | ||
followTouch?: boolean; | ||
/** | ||
* If enabled, position will be updated on `touchmove` event. | ||
* @default true | ||
*/ | ||
followTouch?: boolean; | ||
} | ||
@@ -43,7 +43,3 @@ | ||
*/ | ||
declare function makeMousePositionListener( | ||
target: HTMLElement | Window | Document | undefined, | ||
callback: (position: MousePosition) => void, | ||
options?: UseTouchOptions & FollowTouchOptions, | ||
): VoidFunction; | ||
declare function makeMousePositionListener(target: HTMLElement | Window | Document | undefined, callback: (position: MousePosition) => void, options?: UseTouchOptions & FollowTouchOptions): VoidFunction; | ||
/** | ||
@@ -59,23 +55,11 @@ * Attaches event listeners to provided targat, listening for mouse/touch entering/leaving the element. | ||
*/ | ||
declare function makeMouseInsideListener( | ||
target: HTMLElement | Window | Document | undefined, | ||
callback: (isInside: boolean) => void, | ||
options?: UseTouchOptions, | ||
): VoidFunction; | ||
declare function makeMouseInsideListener(target: HTMLElement | Window | Document | undefined, callback: (isInside: boolean) => void, options?: UseTouchOptions): VoidFunction; | ||
/** | ||
* Turn position relative to the page, into position relative to an element. | ||
*/ | ||
declare const getPositionToElement: ( | ||
pageX: number, | ||
pageY: number, | ||
el: Element, | ||
) => PositionRelativeToElement; | ||
declare const getPositionToElement: (pageX: number, pageY: number, el: Element) => PositionRelativeToElement; | ||
/** | ||
* Turn position relative to the page, into position relative to an element. Clamped to the element bounds. | ||
*/ | ||
declare const getPositionInElement: ( | ||
pageX: number, | ||
pageY: number, | ||
el: Element, | ||
) => PositionRelativeToElement; | ||
declare const getPositionInElement: (pageX: number, pageY: number, el: Element) => PositionRelativeToElement; | ||
/** | ||
@@ -87,14 +71,14 @@ * Turn position relative to the page, into position relative to the screen. | ||
interface MousePositionOptions extends UseTouchOptions, FollowTouchOptions { | ||
/** | ||
* Initial values | ||
* @default { x: 0, y: 0, isInside: false, sourceType: null } | ||
*/ | ||
initialValue?: Partial<MousePositionInside>; | ||
/** | ||
* Initial values | ||
* @default { x: 0, y: 0, isInside: false, sourceType: null } | ||
*/ | ||
initialValue?: Partial<MousePositionInside>; | ||
} | ||
interface PositionToElementOptions extends UseTouchOptions, FollowTouchOptions { | ||
/** | ||
* Initial value | ||
* @default { x: 0, y: 0, top: 0, left: 0, width: 0, height: 0, isInside: false } | ||
*/ | ||
initialValue?: Partial<PositionRelativeToElement>; | ||
/** | ||
* Initial value | ||
* @default { x: 0, y: 0, top: 0, left: 0, width: 0, height: 0, isInside: false } | ||
*/ | ||
initialValue?: Partial<PositionRelativeToElement>; | ||
} | ||
@@ -117,6 +101,3 @@ /** | ||
*/ | ||
declare function createMousePosition( | ||
target?: MaybeAccessor<Window | Document | HTMLElement>, | ||
options?: MousePositionOptions, | ||
): MousePositionInside; | ||
declare function createMousePosition(target?: MaybeAccessor<Window | Document | HTMLElement>, options?: MousePositionOptions): MousePositionInside; | ||
/** | ||
@@ -156,25 +137,4 @@ * Attaches event listeners to `window` to provide a reactive object of current mouse position on the page. | ||
*/ | ||
declare function createPositionToElement( | ||
element: Element | Accessor<Element | undefined>, | ||
pos: Accessor<Position>, | ||
options?: PositionToElementOptions, | ||
): PositionRelativeToElement; | ||
declare function createPositionToElement(element: Element | Accessor<Element | undefined>, pos: Accessor<Position>, options?: PositionToElementOptions): PositionRelativeToElement; | ||
export { | ||
FollowTouchOptions, | ||
MousePosition, | ||
MousePositionInside, | ||
MousePositionOptions, | ||
MouseSourceType, | ||
PositionRelativeToElement, | ||
PositionToElementOptions, | ||
UseTouchOptions, | ||
createMousePosition, | ||
createPositionToElement, | ||
getPositionInElement, | ||
getPositionToElement, | ||
getPositionToScreen, | ||
makeMouseInsideListener, | ||
makeMousePositionListener, | ||
useMousePosition, | ||
}; | ||
export { FollowTouchOptions, MousePosition, MousePositionInside, MousePositionOptions, MouseSourceType, PositionRelativeToElement, PositionToElementOptions, UseTouchOptions, createMousePosition, createPositionToElement, getPositionInElement, getPositionToElement, getPositionToScreen, makeMouseInsideListener, makeMousePositionListener, useMousePosition }; |
@@ -1,5 +0,5 @@ | ||
import { makeEventListenerStack } from "@solid-primitives/event-listener"; | ||
import { clamp, createStaticStore } from "@solid-primitives/utils"; | ||
import { createSharedRoot } from "@solid-primitives/rootless"; | ||
import { createEffect, createComputed, onMount } from "solid-js"; | ||
import { makeEventListenerStack } from '@solid-primitives/event-listener'; | ||
import { clamp, createStaticStore } from '@solid-primitives/utils'; | ||
import { createSharedRoot } from '@solid-primitives/rootless'; | ||
import { createEffect, createComputed, onMount } from 'solid-js'; | ||
@@ -12,3 +12,3 @@ // src/common.ts | ||
isInside: false, | ||
sourceType: null, | ||
sourceType: null | ||
}; | ||
@@ -22,3 +22,3 @@ var DEFAULT_RELATIVE_ELEMENT_POSITION = { | ||
height: 0, | ||
isInside: false, | ||
isInside: false | ||
}; | ||
@@ -28,7 +28,7 @@ function makeMousePositionListener(target = window, callback, options = {}) { | ||
const [listen, clear] = makeEventListenerStack(target, PASSIVE); | ||
const handleMouse = e => callback({ x: e.pageX, y: e.pageY, sourceType: "mouse" }); | ||
const handleMouse = (e) => callback({ x: e.pageX, y: e.pageY, sourceType: "mouse" }); | ||
listen("mousemove", handleMouse); | ||
listen("dragover", handleMouse); | ||
if (touch) { | ||
const handleTouch = e => { | ||
const handleTouch = (e) => { | ||
if (e.touches.length) | ||
@@ -38,3 +38,4 @@ callback({ x: e.touches[0].clientX, y: e.touches[0].clientY, sourceType: "touch" }); | ||
listen("touchstart", handleTouch); | ||
if (followTouch) listen("touchmove", handleTouch); | ||
if (followTouch) | ||
listen("touchmove", handleTouch); | ||
} | ||
@@ -49,3 +50,3 @@ return clear; | ||
function handleChange(isInside) { | ||
this === "mouse" ? (mouseIn = isInside) : (touchIn = isInside); | ||
this === "mouse" ? mouseIn = isInside : touchIn = isInside; | ||
callback(mouseIn || touchIn); | ||
@@ -63,8 +64,3 @@ } | ||
var getPositionToElement = (pageX, pageY, el) => { | ||
const bounds = el.getBoundingClientRect(), | ||
top = bounds.top + window.scrollY, | ||
left = bounds.left + window.scrollX, | ||
x = pageX - left, | ||
y = pageY - top, | ||
{ width, height } = bounds; | ||
const bounds = el.getBoundingClientRect(), top = bounds.top + window.scrollY, left = bounds.left + window.scrollX, x = pageX - left, y = pageY - top, { width, height } = bounds; | ||
return { | ||
@@ -77,3 +73,3 @@ x, | ||
height, | ||
isInside: x >= 0 && y >= 0 && x <= width && y <= height, | ||
isInside: x >= 0 && y >= 0 && x <= width && y <= height | ||
}; | ||
@@ -86,3 +82,3 @@ }; | ||
x: clamp(relative.x, 0, relative.width), | ||
y: clamp(relative.y, 0, relative.height), | ||
y: clamp(relative.y, 0, relative.height) | ||
}; | ||
@@ -92,3 +88,3 @@ }; | ||
x: pageX - window.scrollX, | ||
y: pageY - window.screenY, | ||
y: pageY - window.screenY | ||
}); | ||
@@ -98,14 +94,16 @@ function createMousePosition(target, options = {}) { | ||
...DEFAULT_MOUSE_POSITION, | ||
...options.initialValue, | ||
...options.initialValue | ||
}); | ||
const attachListeners = el => { | ||
const attachListeners = (el) => { | ||
makeMousePositionListener(el, setState, options); | ||
makeMouseInsideListener(el, setState.bind(void 0, "isInside"), options); | ||
}; | ||
if (typeof target !== "function") attachListeners(target); | ||
else createEffect(() => attachListeners(target())); | ||
if (typeof target !== "function") | ||
attachListeners(target); | ||
else | ||
createEffect(() => attachListeners(target())); | ||
return state; | ||
} | ||
var useMousePosition = /* @__PURE__ */ createSharedRoot( | ||
createMousePosition.bind(void 0, void 0, void 0), | ||
createMousePosition.bind(void 0, void 0, void 0) | ||
); | ||
@@ -115,30 +113,19 @@ function createPositionToElement(element, pos, options = {}) { | ||
...DEFAULT_RELATIVE_ELEMENT_POSITION, | ||
...options.initialValue, | ||
...options.initialValue | ||
}; | ||
const calcState = el => { | ||
const calcState = (el) => { | ||
const { x, y } = pos(); | ||
return getPositionToElement(x, y, el); | ||
}; | ||
const getState = | ||
typeof element === "function" | ||
? () => { | ||
const el = element(); | ||
return el ? calcState(el) : fallback; | ||
} | ||
: calcState.bind(void 0, element); | ||
const getState = typeof element === "function" ? () => { | ||
const el = element(); | ||
return el ? calcState(el) : fallback; | ||
} : calcState.bind(void 0, element); | ||
const [state, setState] = createStaticStore(fallback); | ||
createComputed(() => setState(getState())); | ||
if (typeof element === "function") onMount(() => setState(getState())); | ||
if (typeof element === "function") | ||
onMount(() => setState(getState())); | ||
return state; | ||
} | ||
export { | ||
createMousePosition, | ||
createPositionToElement, | ||
getPositionInElement, | ||
getPositionToElement, | ||
getPositionToScreen, | ||
makeMouseInsideListener, | ||
makeMousePositionListener, | ||
useMousePosition, | ||
}; | ||
export { createMousePosition, createPositionToElement, getPositionInElement, getPositionToElement, getPositionToScreen, makeMouseInsideListener, makeMousePositionListener, useMousePosition }; |
@@ -1,5 +0,5 @@ | ||
import "@solid-primitives/event-listener"; | ||
import { noop } from "@solid-primitives/utils"; | ||
import { createSharedRoot } from "@solid-primitives/rootless"; | ||
import "solid-js"; | ||
import '@solid-primitives/event-listener'; | ||
import { noop } from '@solid-primitives/utils'; | ||
import { createSharedRoot } from '@solid-primitives/rootless'; | ||
import 'solid-js'; | ||
@@ -11,3 +11,3 @@ // src/common.ts | ||
isInside: false, | ||
sourceType: null, | ||
sourceType: null | ||
}; | ||
@@ -21,3 +21,3 @@ var DEFAULT_RELATIVE_ELEMENT_POSITION = { | ||
height: 0, | ||
isInside: false, | ||
isInside: false | ||
}; | ||
@@ -44,3 +44,3 @@ function makeMousePositionListener(target = window, callback, options = {}) { | ||
}; | ||
var getPositionToScreen = () => DEFAULT_MOUSE_POSITION; | ||
var getPositionToScreen = () => DEFAULT_MOUSE_POSITION ; | ||
function createMousePosition(target, options = {}) { | ||
@@ -52,3 +52,3 @@ { | ||
var useMousePosition = /* @__PURE__ */ createSharedRoot( | ||
createMousePosition.bind(void 0, void 0, void 0), | ||
createMousePosition.bind(void 0, void 0, void 0) | ||
); | ||
@@ -58,3 +58,3 @@ function createPositionToElement(element, pos, options = {}) { | ||
...DEFAULT_RELATIVE_ELEMENT_POSITION, | ||
...options.initialValue, | ||
...options.initialValue | ||
}; | ||
@@ -66,11 +66,2 @@ { | ||
export { | ||
createMousePosition, | ||
createPositionToElement, | ||
getPositionInElement, | ||
getPositionToElement, | ||
getPositionToScreen, | ||
makeMouseInsideListener, | ||
makeMousePositionListener, | ||
useMousePosition, | ||
}; | ||
export { createMousePosition, createPositionToElement, getPositionInElement, getPositionToElement, getPositionToScreen, makeMouseInsideListener, makeMousePositionListener, useMousePosition }; |
{ | ||
"name": "@solid-primitives/mouse", | ||
"version": "2.0.8-beta.0", | ||
"version": "2.0.8", | ||
"description": "A collection of Solid Primitives, that capture current mouse cursor position, and help to deal with common related usecases.", | ||
@@ -91,8 +91,8 @@ "author": "Damian Tarnawski <gthetarnav@gmail.com>", | ||
"devDependencies": { | ||
"@solid-primitives/raf": "^2.1.8-beta.0" | ||
"@solid-primitives/raf": "^2.1.8" | ||
}, | ||
"dependencies": { | ||
"@solid-primitives/event-listener": "^2.2.8-beta.0", | ||
"@solid-primitives/rootless": "^1.2.6-beta.0", | ||
"@solid-primitives/utils": "^5.4.0-beta.0" | ||
"@solid-primitives/event-listener": "^2.2.8", | ||
"@solid-primitives/rootless": "^1.2.6", | ||
"@solid-primitives/utils": "^5.4.0" | ||
}, | ||
@@ -99,0 +99,0 @@ "peerDependencies": { |
@@ -0,0 +0,0 @@ <p> |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
0
35017
10
734