Skip to content

Commit d131ff0

Browse files
committed
fix: hover states optimizations
1 parent bc23161 commit d131ff0

File tree

4 files changed

+19
-8
lines changed

4 files changed

+19
-8
lines changed

packages/docs/app/(home)/_components/anara.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ const PDFContent = ({
128128
<CanvasLayer />
129129
<TextLayer />
130130
<AnnotationHighlightLayer
131+
highlightClassName="dark:opacity-40 mix-blend-multiply transition-all duration-200 cursor-pointer"
131132
focusedAnnotationId={focusedAnnotationId}
132133
tooltipClassName="bg-white shadow-lg rounded-lg p-3 min-w-[200px]"
133134
onAnnotationClick={onAnnotationClick}

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ interface AnnotationTooltipProps {
1414
isOpen?: boolean;
1515
className?: string;
1616
hoverClassName?: string;
17+
hoverIsOpen?: boolean;
1718
renderHoverTooltipContent?: (props: {
1819
annotation: Annotation;
1920
onClose: () => void;
@@ -28,8 +29,9 @@ export const AnnotationTooltip = ({
2829
onOpenChange,
2930
className,
3031
hoverClassName,
31-
isOpen: controlledIsOpen,
32-
}: AnnotationTooltipProps) => {
32+
isOpen: controlledIsOpen,
33+
hoverIsOpen: controlledHoverIsOpen,
34+
}: AnnotationTooltipProps) => {
3335
const viewportRef = usePdf((state) => state.viewportRef);
3436
const closeTimeoutRef = useRef<NodeJS.Timeout | null>(null);
3537
const isMouseInTooltipRef = useRef(false);
@@ -47,17 +49,20 @@ export const AnnotationTooltip = ({
4749
});
4850

4951
const {
50-
isOpen: hoverIsOpen,
52+
isOpen: uncontrolledHoverIsOpen,
5153
setIsOpen: setHoverIsOpen,
5254
refs: hoverRefs,
55+
5356
floatingStyles: hoverFloatingStyles,
5457
getFloatingProps: getHoverFloatingProps,
5558
getReferenceProps: getHoverReferenceProps,
5659
} = useAnnotationTooltip({
60+
position: "bottom",
5761
annotation,
5862
});
5963

6064
const isOpen = controlledIsOpen ?? uncontrolledIsOpen;
65+
const hoverIsOpen = controlledHoverIsOpen || uncontrolledHoverIsOpen;
6166

6267
const handleClick = useCallback(() => {
6368
if (controlledIsOpen === undefined) {

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ interface AnnotationHighlightLayerProps {
1515
onClose: () => void;
1616
}) => React.ReactNode;
1717
focusedAnnotationId?: string;
18+
focusedHoverAnnotationId?: string;
1819
onAnnotationClick?: (annotation: Annotation) => void;
1920
tooltipClassName?: string;
2021
hoverTooltipClassName?: string;
22+
highlightClassName?: string;
2123
}
2224

2325
export const AnnotationHighlightLayer = ({
@@ -26,7 +28,9 @@ export const AnnotationHighlightLayer = ({
2628
renderTooltipContent,
2729
renderHoverTooltipContent,
2830
tooltipClassName,
31+
highlightClassName,
2932
focusedAnnotationId,
33+
focusedHoverAnnotationId,
3034
onAnnotationClick,
3135
hoverTooltipClassName
3236
}: AnnotationHighlightLayerProps) => {
@@ -46,6 +50,7 @@ export const AnnotationHighlightLayer = ({
4650
className={tooltipClassName}
4751
hoverClassName={hoverTooltipClassName}
4852
isOpen={focusedAnnotationId === annotation.id}
53+
hoverIsOpen={focusedHoverAnnotationId === annotation.id}
4954
onOpenChange={(open) => {
5055
if (open && onAnnotationClick) {
5156
onAnnotationClick(annotation);
@@ -73,16 +78,14 @@ export const AnnotationHighlightLayer = ({
7378
{annotation.highlights.map((highlight, index) => (
7479
<div
7580
key={index}
81+
className={highlightClassName}
7682
style={{
7783
position: "absolute",
7884
top: highlight.top,
7985
left: highlight.left,
8086
width: highlight.width,
8187
height: highlight.height,
82-
backgroundColor: annotation.color || "rgba(255, 255, 0, 0.3)",
83-
mixBlendMode: "multiply",
84-
transition: "all 0.2s ease",
85-
cursor: "pointer",
88+
backgroundColor: annotation.color,
8689
}}
8790
data-highlight-id={annotation.id}
8891
/>

packages/lector/src/hooks/useAnnotationTooltip.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import type { Annotation } from "./useAnnotations";
1616
interface UseAnnotationTooltipProps {
1717
annotation: Annotation;
1818
onOpenChange?: (open: boolean) => void;
19+
position?: "top" | "bottom" | "left" | "right";
1920
}
2021

2122
interface UseAnnotationTooltipReturn {
@@ -41,6 +42,7 @@ const defaultRect = {
4142
export const useAnnotationTooltip = ({
4243
annotation,
4344
onOpenChange,
45+
position = "top",
4446
}: UseAnnotationTooltipProps): UseAnnotationTooltipReturn => {
4547
// Show tooltip immediately if it's a new annotation
4648
const isNewAnnotation = Date.now() - new Date(annotation.createdAt).getTime() < 1000;
@@ -53,7 +55,7 @@ export const useAnnotationTooltip = ({
5355
floatingStyles,
5456
context,
5557
} = useFloating({
56-
placement: "top",
58+
placement: position,
5759
open: isOpen,
5860
onOpenChange: (open) => {
5961
setIsOpen(open);

0 commit comments

Comments
 (0)