Skip to content

Commit 3124c69

Browse files
committed
fix: improve annotations positioning
1 parent 746d467 commit 3124c69

File tree

4 files changed

+25
-17
lines changed

4 files changed

+25
-17
lines changed

packages/lector/size.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
{
33
"name": "Client",
44
"passed": true,
5-
"size": 70348,
5+
"size": 70508,
66
"sizeLimit": 150000
77
}
88
]

packages/lector/src/components/annotation-tooltip.tsx

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
1-
import { useCallback, useRef } from "react";
1+
import { useCallback, useRef, useState } from "react";
22
import { createPortal } from "react-dom";
33

44
import type { Annotation } from "../hooks/useAnnotations";
55
import { useAnnotationTooltip } from "../hooks/useAnnotationTooltip";
66
import { usePdf } from "../internal";
77

8+
9+
export type AnnotationTooltipContentProps = {
10+
annotation: Annotation;
11+
onClose: () => void;
12+
setPosition?: (position: 'top' | 'bottom' | undefined) => void;
13+
};
14+
815
interface AnnotationTooltipProps {
916
annotation: Annotation;
1017
children: React.ReactNode;
11-
tooltipContent: React.ReactNode;
18+
renderTooltipContent: (props: AnnotationTooltipContentProps) => React.ReactNode;
1219
hoverTooltipContent?: React.ReactNode;
1320
onOpenChange?: (open: boolean) => void;
1421
isOpen?: boolean;
@@ -25,7 +32,7 @@ interface AnnotationTooltipProps {
2532
export const AnnotationTooltip = ({
2633
annotation,
2734
children,
28-
tooltipContent,
35+
renderTooltipContent,
2936
hoverTooltipContent,
3037
onOpenChange,
3138
className,
@@ -37,6 +44,7 @@ export const AnnotationTooltip = ({
3744
const viewportRef = usePdf((state) => state.viewportRef);
3845
const closeTimeoutRef = useRef<NodeJS.Timeout | null>(null);
3946
const isMouseInTooltipRef = useRef(false);
47+
const [triggeredPosition, setTriggeredPosition] = useState<'top' | 'bottom' | undefined>();
4048

4149
const {
4250
isOpen: uncontrolledIsOpen,
@@ -48,6 +56,7 @@ export const AnnotationTooltip = ({
4856
} = useAnnotationTooltip({
4957
annotation,
5058
onOpenChange,
59+
position: triggeredPosition,
5160
isOpen: controlledIsOpen,
5261
});
5362

@@ -152,7 +161,11 @@ export const AnnotationTooltip = ({
152161
}}
153162
{...getFloatingProps()}
154163
>
155-
{tooltipContent}
164+
{renderTooltipContent({
165+
annotation,
166+
onClose: () => setIsOpen(false),
167+
setPosition: (position: 'top' | 'bottom' | undefined) => setTriggeredPosition(position)
168+
})}
156169
</div>,
157170
viewportRef.current
158171
)}

packages/lector/src/components/layers/annotation-highlight-layer.tsx

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
import type { Annotation } from "../../hooks/useAnnotations";
22
import { useAnnotations } from "../../hooks/useAnnotations";
33
import { usePDFPageNumber } from "../../hooks/usePdfPageNumber";
4-
import { AnnotationTooltip } from "../annotation-tooltip";
4+
import { AnnotationTooltip, type AnnotationTooltipContentProps } from "../annotation-tooltip";
5+
56

67
interface AnnotationHighlightLayerProps {
78
className?: string;
89
style?: React.CSSProperties;
9-
renderTooltipContent: (props: {
10-
annotation: Annotation;
11-
onClose: () => void;
12-
}) => React.ReactNode;
10+
renderTooltipContent: (props: AnnotationTooltipContentProps) => React.ReactNode;
1311
renderHoverTooltipContent: (props: {
1412
annotation: Annotation;
1513
onClose: () => void;
@@ -59,12 +57,8 @@ export const AnnotationHighlightLayer = ({
5957
onAnnotationClick(annotation);
6058
}
6159
}}
62-
tooltipContent={(
63-
renderTooltipContent({
64-
annotation,
65-
onClose: () => {},
66-
})
67-
)
60+
renderTooltipContent={
61+
renderTooltipContent
6862
}
6963
hoverTooltipContent={(
7064
renderHoverTooltipContent({

packages/lector/src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
export type { AnnotationTooltipContentProps } from "./components/annotation-tooltip";
12
export { AnnotationTooltip } from "./components/annotation-tooltip";
23
export { AnnotationHighlightLayer } from "./components/layers/annotation-highlight-layer";
34
export { AnnotationLayer } from "./components/layers/annotation-layer";
@@ -32,4 +33,4 @@ export { useAnnotations } from "./hooks/useAnnotations";
3233
export { LinkService } from "./hooks/usePDFLinkService";
3334
export { usePDFPageNumber } from "./hooks/usePdfPageNumber";
3435
export { useSelectionDimensions } from "./hooks/useSelectionDimensions";
35-
export { type ColoredHighlight, type HighlightRect, usePdf } from "./internal";
36+
export { type ColoredHighlight, type HighlightRect, usePdf } from "./internal";

0 commit comments

Comments
 (0)