@@ -8023,7 +8023,7 @@ Description: A text highlighter that mimics the effect of a human-drawn marker s
80238023--- file: magicui/highlighter.tsx ---
80248024"use client"
80258025
8026- import { useEffect , useRef } from "react"
8026+ import { useLayoutEffect , useRef } from "react"
80278027import type React from "react"
80288028import { useInView } from "motion/react"
80298029import { annotate } from "rough-notation"
@@ -8062,7 +8062,6 @@ export function Highlighter({
80628062 isView = false,
80638063}: HighlighterProps) {
80648064 const elementRef = useRef<HTMLSpanElement>(null)
8065- const annotationRef = useRef<RoughAnnotation | null>(null)
80668065
80678066 const isInView = useInView(elementRef, {
80688067 once: true,
@@ -8072,8 +8071,9 @@ export function Highlighter({
80728071 // If isView is false, always show. If isView is true, wait for inView
80738072 const shouldShow = !isView || isInView
80748073
8075- useEffect (() => {
8074+ useLayoutEffect (() => {
80768075 const element = elementRef.current
8076+ let annotation: RoughAnnotation | null = null
80778077 let resizeObserver: ResizeObserver | null = null
80788078
80798079 if (shouldShow && element) {
@@ -8087,25 +8087,21 @@ export function Highlighter({
80878087 multiline,
80888088 }
80898089
8090- const annotation = annotate(element, annotationConfig)
8091-
8092- annotationRef.current = annotation
8093- annotation.show()
8090+ const currentAnnotation = annotate(element, annotationConfig)
8091+ annotation = currentAnnotation
8092+ currentAnnotation.show()
80948093
80958094 resizeObserver = new ResizeObserver(() => {
8096- annotation .hide()
8097- annotation .show()
8095+ currentAnnotation .hide()
8096+ currentAnnotation .show()
80988097 })
80998098
81008099 resizeObserver.observe(element)
81018100 resizeObserver.observe(document.body)
81028101 }
81038102
81048103 return () => {
8105- if (annotationRef.current) {
8106- annotationRef.current.remove()
8107- annotationRef.current = null
8108- }
8104+ annotation?.remove()
81098105 if (resizeObserver) {
81108106 resizeObserver.disconnect()
81118107 }
@@ -8156,7 +8152,7 @@ export default function HighlighterDemo() {
81568152--- file: magicui/highlighter.tsx ---
81578153"use client"
81588154
8159- import { useEffect , useRef } from "react"
8155+ import { useLayoutEffect , useRef } from "react"
81608156import type React from "react"
81618157import { useInView } from "motion/react"
81628158import { annotate } from "rough-notation"
@@ -8195,7 +8191,6 @@ export function Highlighter({
81958191 isView = false,
81968192}: HighlighterProps) {
81978193 const elementRef = useRef<HTMLSpanElement>(null)
8198- const annotationRef = useRef<RoughAnnotation | null>(null)
81998194
82008195 const isInView = useInView(elementRef, {
82018196 once: true,
@@ -8205,8 +8200,9 @@ export function Highlighter({
82058200 // If isView is false, always show. If isView is true, wait for inView
82068201 const shouldShow = !isView || isInView
82078202
8208- useEffect (() => {
8203+ useLayoutEffect (() => {
82098204 const element = elementRef.current
8205+ let annotation: RoughAnnotation | null = null
82108206 let resizeObserver: ResizeObserver | null = null
82118207
82128208 if (shouldShow && element) {
@@ -8220,25 +8216,21 @@ export function Highlighter({
82208216 multiline,
82218217 }
82228218
8223- const annotation = annotate(element, annotationConfig)
8224-
8225- annotationRef.current = annotation
8226- annotation.show()
8219+ const currentAnnotation = annotate(element, annotationConfig)
8220+ annotation = currentAnnotation
8221+ currentAnnotation.show()
82278222
82288223 resizeObserver = new ResizeObserver(() => {
8229- annotation .hide()
8230- annotation .show()
8224+ currentAnnotation .hide()
8225+ currentAnnotation .show()
82318226 })
82328227
82338228 resizeObserver.observe(element)
82348229 resizeObserver.observe(document.body)
82358230 }
82368231
82378232 return () => {
8238- if (annotationRef.current) {
8239- annotationRef.current.remove()
8240- annotationRef.current = null
8241- }
8233+ annotation?.remove()
82428234 if (resizeObserver) {
82438235 resizeObserver.disconnect()
82448236 }
0 commit comments