Skip to content

Commit

Permalink
Move the cursor utilities to shared lib.
Browse files Browse the repository at this point in the history
  • Loading branch information
thetarnav committed Aug 5, 2022
1 parent 1fa6a83 commit 6f620e1
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 32 deletions.
7 changes: 7 additions & 0 deletions .changeset/smooth-coins-bathe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@solid-devtools/locator": patch
"solid-devtools-shared": patch
"@solid-devtools/ui": patch
---

Move the cursor utilities to shared lib.
30 changes: 0 additions & 30 deletions packages/locator/src/elementCursor.ts

This file was deleted.

4 changes: 2 additions & 2 deletions packages/locator/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import {
import { makeKeyHoldListener } from "@solid-primitives/keyboard"
import { registerDebuggerPlugin } from "@solid-devtools/debugger"
import { WINDOW_PROJECTPATH_PROPERTY } from "@shared/variables"
import { createElementCursor } from "@shared/utils"
import { clearFindComponentCache, findComponent } from "./findComponent"
import { makeHoverElementListener } from "./hoverElement"
import { createElementCursor } from "./elementCursor"
import { openCodeSource, SourceCodeData, TargetIDE, TargetURLFunction } from "./goToSource"
import { ElementOverlay } from "./ElementOverlay"
import { sheet } from "./twind"
Expand Down Expand Up @@ -71,7 +71,7 @@ export function useLocatorPlugin({ targetIDE, key = "altKey" }: LocatorOptions):

if (targetIDE) {
// set pointer cursor to selected component
createElementCursor(() => selected()?.location?.element)
createElementCursor(() => selected()?.location?.element, "pointer")

// go to selected component source code on click
createEffect(() => {
Expand Down
91 changes: 91 additions & 0 deletions packages/shared/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { Accessor, createEffect, createMemo } from "solid-js"
import { access, FalsyValue, MaybeAccessor } from "@solid-primitives/utils"

export function callArrayProp<
K extends PropertyKey,
T extends (...args: Args) => void,
Expand Down Expand Up @@ -50,3 +53,91 @@ export function trimString(str: string, maxLength: number): string {
if (str.length <= maxLength) return str
return str.slice(0, maxLength) + "…"
}

export type CursorProperty =
| "-moz-grab"
| "-webkit-grab"
| "alias"
| "all-scroll"
| "auto"
| "cell"
| "col-resize"
| "context-menu"
| "copy"
| "crosshair"
| "default"
| "e-resize"
| "ew-resize"
| "grab"
| "grabbing"
| "help"
| "move"
| "n-resize"
| "ne-resize"
| "nesw-resize"
| "no-drop"
| "none"
| "not-allowed"
| "ns-resize"
| "nw-resize"
| "nwse-resize"
| "pointer"
| "progress"
| "row-resize"
| "s-resize"
| "se-resize"
| "sw-resize"
| "text"
| "vertical-text"
| "w-resize"
| "wait"
| "zoom-in"
| "zoom-out"
| (string & {})

// TODO: contribute to @solid-primitives/cursor

/**
* Set selected {@link cursor} to {@link target} styles.
*
* @param target
* @param cursor
*/
export function createElementCursor(
target: Accessor<HTMLElement | FalsyValue> | HTMLElement,
cursor: MaybeAccessor<CursorProperty>,
): void {
createEffect<{ el: HTMLElement | FalsyValue; cursor: CursorProperty }>(
prev => {
const el = access(target)
const cursorValue = access(cursor)
if (prev.el === el && prev.cursor === cursorValue) return prev
if (prev.el) prev.el.style.cursor = prev.cursor
if (el) {
const newPrevCursor = el.style.cursor
el.style.cursor = cursorValue
return { el, cursor: newPrevCursor }
}
return { el, cursor: "" }
},
{
el: undefined,
cursor: "",
},
)
}

export function createBodyCursor(cursor: Accessor<CursorProperty | FalsyValue>): void {
let overwritten: string
createEffect((prev: CursorProperty | FalsyValue) => {
const cursorValue = cursor()
if (prev === cursorValue) return prev
if (cursorValue) {
overwritten = document.body.style.cursor
document.body.style.cursor = cursorValue
} else {
document.body.style.cursor = overwritten
}
return cursorValue
})
}

0 comments on commit 6f620e1

Please sign in to comment.