@solid-primitives/mouse
Advanced tools
Comparing version 2.0.8 to 2.0.9
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 { createHydratableSingletonRoot } from '@solid-primitives/rootless'; | ||
import { createEffect, sharedConfig, onMount, createComputed } from 'solid-js'; | ||
@@ -21,3 +21,3 @@ // src/common.ts | ||
height: 0, | ||
isInside: false | ||
isInside: true | ||
}; | ||
@@ -84,6 +84,7 @@ function makeMousePositionListener(target = window, callback, options = {}) { | ||
function createMousePosition(target, options = {}) { | ||
const [state, setState] = createStaticStore({ | ||
const fallback = { | ||
...DEFAULT_MOUSE_POSITION, | ||
...options.initialValue | ||
}); | ||
}; | ||
const [state, setState] = createStaticStore(fallback); | ||
const attachListeners = (el) => { | ||
@@ -99,3 +100,3 @@ makeMousePositionListener(el, setState, options); | ||
} | ||
var useMousePosition = /* @__PURE__ */ createSharedRoot( | ||
var useMousePosition = /* @__PURE__ */ createHydratableSingletonRoot( | ||
createMousePosition.bind(void 0, void 0, void 0) | ||
@@ -108,2 +109,3 @@ ); | ||
}; | ||
const isFn = typeof element === "function"; | ||
const calcState = (el) => { | ||
@@ -113,10 +115,16 @@ const { x, y } = pos(); | ||
}; | ||
const getState = typeof element === "function" ? () => { | ||
const [state, setState] = createStaticStore( | ||
isFn || sharedConfig.context ? fallback : calcState(element) | ||
); | ||
const getState = isFn ? () => { | ||
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())); | ||
} : calcState.bind(void 0, element), updateState = () => setState(getState()); | ||
if (sharedConfig.context) { | ||
onMount(() => createComputed(updateState)); | ||
} else { | ||
createComputed(updateState); | ||
if (isFn) | ||
onMount(updateState); | ||
} | ||
return state; | ||
@@ -123,0 +131,0 @@ } |
@@ -102,3 +102,3 @@ import { Position, MaybeAccessor } from '@solid-primitives/utils'; | ||
* | ||
* This is a [shared root primitive](https://github.com/solidjs-community/solid-primitives/tree/main/packages/rootless#createSharedRoot). | ||
* This is a [singleton root primitive](https://github.com/solidjs-community/solid-primitives/tree/main/packages/rootless#createSingletonRoot). | ||
* @returns reactive object of current mouse position on the page | ||
@@ -105,0 +105,0 @@ * ```ts |
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 { createHydratableSingletonRoot } from '@solid-primitives/rootless'; | ||
import { createEffect, sharedConfig, onMount, createComputed } from 'solid-js'; | ||
@@ -21,3 +21,3 @@ // src/common.ts | ||
height: 0, | ||
isInside: false | ||
isInside: true | ||
}; | ||
@@ -84,6 +84,7 @@ function makeMousePositionListener(target = window, callback, options = {}) { | ||
function createMousePosition(target, options = {}) { | ||
const [state, setState] = createStaticStore({ | ||
const fallback = { | ||
...DEFAULT_MOUSE_POSITION, | ||
...options.initialValue | ||
}); | ||
}; | ||
const [state, setState] = createStaticStore(fallback); | ||
const attachListeners = (el) => { | ||
@@ -99,3 +100,3 @@ makeMousePositionListener(el, setState, options); | ||
} | ||
var useMousePosition = /* @__PURE__ */ createSharedRoot( | ||
var useMousePosition = /* @__PURE__ */ createHydratableSingletonRoot( | ||
createMousePosition.bind(void 0, void 0, void 0) | ||
@@ -108,2 +109,3 @@ ); | ||
}; | ||
const isFn = typeof element === "function"; | ||
const calcState = (el) => { | ||
@@ -113,10 +115,16 @@ const { x, y } = pos(); | ||
}; | ||
const getState = typeof element === "function" ? () => { | ||
const [state, setState] = createStaticStore( | ||
isFn || sharedConfig.context ? fallback : calcState(element) | ||
); | ||
const getState = isFn ? () => { | ||
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())); | ||
} : calcState.bind(void 0, element), updateState = () => setState(getState()); | ||
if (sharedConfig.context) { | ||
onMount(() => createComputed(updateState)); | ||
} else { | ||
createComputed(updateState); | ||
if (isFn) | ||
onMount(updateState); | ||
} | ||
return state; | ||
@@ -123,0 +131,0 @@ } |
import '@solid-primitives/event-listener'; | ||
import { noop } from '@solid-primitives/utils'; | ||
import { createSharedRoot } from '@solid-primitives/rootless'; | ||
import { createHydratableSingletonRoot } from '@solid-primitives/rootless'; | ||
import 'solid-js'; | ||
@@ -20,3 +20,3 @@ | ||
height: 0, | ||
isInside: false | ||
isInside: true | ||
}; | ||
@@ -45,7 +45,11 @@ function makeMousePositionListener(target = window, callback, options = {}) { | ||
function createMousePosition(target, options = {}) { | ||
const fallback = { | ||
...DEFAULT_MOUSE_POSITION, | ||
...options.initialValue | ||
}; | ||
{ | ||
return DEFAULT_MOUSE_POSITION; | ||
return fallback; | ||
} | ||
} | ||
var useMousePosition = /* @__PURE__ */ createSharedRoot( | ||
var useMousePosition = /* @__PURE__ */ createHydratableSingletonRoot( | ||
createMousePosition.bind(void 0, void 0, void 0) | ||
@@ -52,0 +56,0 @@ ); |
{ | ||
"name": "@solid-primitives/mouse", | ||
"version": "2.0.8", | ||
"version": "2.0.9", | ||
"description": "A collection of Solid Primitives, that capture current mouse cursor position, and help to deal with common related usecases.", | ||
@@ -95,4 +95,4 @@ "author": "Damian Tarnawski <gthetarnav@gmail.com>", | ||
"@solid-primitives/event-listener": "^2.2.8", | ||
"@solid-primitives/rootless": "^1.2.6", | ||
"@solid-primitives/utils": "^5.4.0" | ||
"@solid-primitives/rootless": "^1.3.0", | ||
"@solid-primitives/utils": "^5.5.0" | ||
}, | ||
@@ -99,0 +99,0 @@ "peerDependencies": { |
@@ -74,3 +74,3 @@ <p> | ||
This primitive provides a [shared root](https://github.com/solidjs-community/solid-primitives/tree/main/packages/rootless#createSharedRoot) variant that will listen to window mouse position, and reuse event listeners and signals across dependents. | ||
This primitive provides a [singleton root](https://github.com/solidjs-community/solid-primitives/tree/main/packages/rootless#createSingletonRoot) variant that will listen to window mouse position, and reuse event listeners and signals across dependents. | ||
@@ -77,0 +77,0 @@ ```ts |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
36158
774