Skip to content

Commit fd23e9c

Browse files
author
Mint de Wit
committed
chore: reimplement script hover preview
1 parent bfc7122 commit fd23e9c

File tree

7 files changed

+325
-230
lines changed

7 files changed

+325
-230
lines changed

packages/webui/src/client/ui/PreviewPopUp/PreviewPopUp.scss

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@
1717

1818
box-shadow: 0 0 20px 0 rgba(0, 0, 0, 0.6);
1919

20+
z-index: 9999;
21+
2022
&--large {
2123
width: 480px;
2224
}
2325

24-
&-small {
26+
&--small {
2527
width: 320px;
2628
}
2729

@@ -32,6 +34,11 @@
3234

3335
.preview-popUp__preview {
3436
grid-area: 'preview';
37+
38+
.script {
39+
padding: 0.3em;
40+
font-style: italic;
41+
}
3542
}
3643

3744
.preview-popUp__controls {

packages/webui/src/client/ui/PreviewPopUp/PreviewPopUp.tsx

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,28 @@ export const PreviewPopUp = React.forwardRef<
6060
}),
6161
[padding]
6262
)
63-
const { styles, attributes, update } = usePopper(anchor, popperEl, popperOptions)
63+
const virtualElement = useRef<VirtualElement>({
64+
getBoundingClientRect: generateGetBoundingClientRect(),
65+
})
66+
const { styles, attributes, update } = usePopper(virtualElement.current, popperEl, popperOptions)
6467

6568
const updateRef = useRef(update)
6669

6770
useEffect(() => {
6871
updateRef.current = update
72+
73+
const listener = ({ clientX: x }: MouseEvent) => {
74+
virtualElement.current.getBoundingClientRect = generateGetBoundingClientRect(
75+
x,
76+
anchor?.getBoundingClientRect().y ?? 0
77+
)
78+
if (update) update()
79+
}
80+
document.addEventListener('mousemove', listener)
81+
82+
return () => {
83+
document.removeEventListener('mousemove', listener)
84+
}
6985
}, [update])
7086

7187
useImperativeHandle(
@@ -111,3 +127,17 @@ export const PreviewPopUp = React.forwardRef<
111127
export type PreviewPopUpHandle = {
112128
readonly update: () => void
113129
}
130+
131+
function generateGetBoundingClientRect(x = 0, y = 0) {
132+
return () => ({
133+
width: 0,
134+
height: 0,
135+
x: x,
136+
y: y,
137+
top: y,
138+
right: x,
139+
bottom: y,
140+
left: x,
141+
toJSON: () => '',
142+
})
143+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import React from 'react'
2+
import { PreviewContent } from './PreviewPopUpContext'
3+
4+
interface PreviewPopUpContentProps {
5+
content: PreviewContent
6+
}
7+
8+
export function PreviewPopUpContent({ content }: PreviewPopUpContentProps): React.ReactElement {
9+
switch (content.type) {
10+
case 'text':
11+
return <ScriptPreviewElement content={content} />
12+
default:
13+
return <></>
14+
}
15+
}
16+
17+
interface ScriptPreviewProps {
18+
content: {
19+
type: 'text'
20+
content: string
21+
}
22+
}
23+
export function ScriptPreviewElement({ content }: ScriptPreviewProps): React.ReactElement {
24+
return <div className="script">{content.content}</div>
25+
}

packages/webui/src/client/ui/PreviewPopUp/PreviewPopUpContext.tsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React, { useRef, useState } from 'react'
22
import { PreviewPopUp, PreviewPopUpHandle } from './PreviewPopUp'
33
import { Padding, Placement } from '@popperjs/core'
4+
import { PreviewPopUpContent } from './PreviewPopUpContent'
45

56
type VirtualElement = {
67
getBoundingClientRect: () => DOMRect
@@ -32,7 +33,7 @@ export type PreviewContent =
3233
content: unknown
3334
}
3435

35-
interface IPreviewPopUpSession {
36+
export interface IPreviewPopUpSession {
3637
/**
3738
* Update the open preview with new content or modify the content already being previewed, such as change current showing
3839
* time in the video, etc.
@@ -77,7 +78,7 @@ interface IPreviewPopUpContext {
7778
): IPreviewPopUpSession
7879
}
7980

80-
const PreviewPopUpContext = React.createContext<IPreviewPopUpContext>({
81+
export const PreviewPopUpContext = React.createContext<IPreviewPopUpContext>({
8182
requestPreview: () => {
8283
throw new Error('Preview PopUp needs to set up with `PreviewPopUpContextProvider`.')
8384
},
@@ -101,7 +102,7 @@ export function PreviewPopUpContextProvider({ children }: React.PropsWithChildre
101102
const previewRef = useRef<PreviewPopUpHandle>(null)
102103

103104
const [previewSession, setPreviewSession] = useState<PreviewSession | null>(null)
104-
const [_previewContent, setPreviewContent] = useState<PreviewContent | null>(null)
105+
const [previewContent, setPreviewContent] = useState<PreviewContent | null>(null)
105106

106107
const context: IPreviewPopUpContext = {
107108
requestPreview: (anchor, content, opts) => {
@@ -143,7 +144,9 @@ export function PreviewPopUpContextProvider({ children }: React.PropsWithChildre
143144
contentInfo={previewSession.contentInfo}
144145
controls={previewSession.controls}
145146
warnings={previewSession.warnings}
146-
/>
147+
>
148+
{previewContent && <PreviewPopUpContent content={previewContent} />}
149+
</PreviewPopUp>
147150
)}
148151
</PreviewPopUpContext.Provider>
149152
)

0 commit comments

Comments
 (0)