-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(devtools): updated ui selector logic (#5898)
Co-authored-by: Omer Aplak <omer@refine.dev>
- Loading branch information
Showing
10 changed files
with
457 additions
and
451 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
"@refinedev/devtools": minor | ||
--- | ||
|
||
feat: devtools selector with all selectables | ||
|
||
Updated devtools selector to display all available elements instead of relying on the user's pointer to select the element. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import React from "react"; | ||
|
||
type Props = { | ||
children: string; | ||
}; | ||
|
||
export const ApplyStyles = ({ children }: Props) => { | ||
React.useEffect(() => { | ||
const element = document.createElement("style"); | ||
element.innerHTML = children; | ||
document.head.appendChild(element); | ||
|
||
return () => { | ||
document.head.removeChild(element); | ||
}; | ||
}, [children]); | ||
|
||
return null; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,109 +1,67 @@ | ||
import React from "react"; | ||
import { createPortal } from "react-dom"; | ||
import { useSelector } from "src/utilities/use-selector"; | ||
import { SelectorBox } from "./selector-box"; | ||
import { SelectorHint } from "./selector-hint"; | ||
import { ApplyStyles } from "./apply-styles"; | ||
import { SelectableElements } from "./selectable-elements"; | ||
|
||
type Props = { | ||
onSelectorOpen: () => void; | ||
active: boolean; | ||
setActive: React.Dispatch<React.SetStateAction<boolean>>; | ||
onHighlight: (name: string) => void; | ||
icon?: React.ReactNode; | ||
style?: React.CSSProperties; | ||
}; | ||
|
||
export const DevtoolsSelector = ({ | ||
onSelectorOpen, | ||
active, | ||
setActive, | ||
onHighlight, | ||
icon, | ||
style, | ||
}: Props) => { | ||
const [hover, setHover] = React.useState(false); | ||
const [active, setActive] = React.useState(false); | ||
const { rect, name } = useSelector(active); | ||
const { selectableElements } = useSelector(active); | ||
|
||
const [selectorBoxRoot, setSelectorBoxRoot] = | ||
React.useState<HTMLElement | null>(null); | ||
|
||
React.useEffect(() => { | ||
if (!selectorBoxRoot) { | ||
const element = document.createElement("div"); | ||
element.id = "selector-box-root"; | ||
|
||
document.body.appendChild(element); | ||
|
||
setSelectorBoxRoot(element); | ||
} | ||
}, []); | ||
|
||
React.useEffect(() => { | ||
if (active) { | ||
document.body.style.cursor = "crosshair"; | ||
} else { | ||
document.body.style.cursor = "default"; | ||
} | ||
}, [active]); | ||
|
||
React.useEffect(() => { | ||
const onMouseClick = (e: MouseEvent) => { | ||
if (!active) return; | ||
if (!name) return; | ||
|
||
e?.preventDefault(); | ||
e?.stopPropagation(); | ||
e.stopImmediatePropagation(); | ||
onHighlight(name); | ||
setActive(false); | ||
}; | ||
|
||
if (active) { | ||
document.addEventListener("click", onMouseClick, { | ||
capture: true, | ||
}); | ||
|
||
return () => { | ||
document.removeEventListener("click", onMouseClick, { | ||
capture: true, | ||
}); | ||
}; | ||
} | ||
|
||
return () => 0; | ||
}, [name, onHighlight, active]); | ||
|
||
React.useEffect(() => { | ||
if (active) { | ||
onSelectorOpen(); | ||
} | ||
}, [active, onSelectorOpen]); | ||
const onSelect = (name: string) => { | ||
onHighlight(name); | ||
setActive(false); | ||
}; | ||
|
||
return ( | ||
<div style={style}> | ||
{/* biome-ignore lint/a11y/useKeyWithClickEvents: <explanation> */} | ||
<div | ||
role="button" | ||
title="Element Selector" | ||
onMouseOver={() => setHover(true)} | ||
onMouseOut={() => setHover(false)} | ||
className="refine-devtools-selector-button" | ||
onClick={(event) => { | ||
event.preventDefault(); | ||
event.stopPropagation(); | ||
(document?.activeElement as HTMLElement)?.blur(); | ||
setActive((active) => !active); | ||
}} | ||
style={{ | ||
padding: 0, | ||
margin: 0, | ||
height: "100%", | ||
width: "100%", | ||
transform: hover ? "rotate(180deg)" : "rotate(0deg)", | ||
transition: "transform 0.2s ease-in-out", | ||
}} | ||
> | ||
{icon} | ||
</div> | ||
<SelectorHint active={active} /> | ||
{active && | ||
selectorBoxRoot && | ||
createPortal(<SelectorBox {...rect} name={name} />, selectorBoxRoot)} | ||
{active && ( | ||
<SelectableElements elements={selectableElements} onSelect={onSelect} /> | ||
)} | ||
<ApplyStyles> | ||
{ | ||
/* css */ ` | ||
.refine-devtools-selector-button { | ||
padding: 0; | ||
margin: 0; | ||
height: 100%; | ||
width: 100%; | ||
transform: rotate(0deg); | ||
transition: transform 0.2s ease-in-out; | ||
} | ||
.refine-devtools-selector-button:hover { | ||
transform: rotate(180deg); | ||
} | ||
` | ||
} | ||
</ApplyStyles> | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.