@c-rex/contexts 0.3.0-build.34 → 0.3.0-build.36

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@c-rex/contexts",
3
- "version": "0.3.0-build.34",
3
+ "version": "0.3.0-build.36",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "src"
@@ -15,6 +15,7 @@ type HighlightContextType = {
15
15
  next: () => void;
16
16
  prev: () => void;
17
17
  registerContainer: (el: HTMLElement | null) => void;
18
+ refreshMarks: () => void;
18
19
  };
19
20
 
20
21
  const HighlightContext = createContext<HighlightContextType | null>(null);
@@ -25,18 +26,24 @@ export const HighlightProvider = ({ children }: { children: React.ReactNode }) =
25
26
  const [marks, setMarks] = useState<NodeListOf<HTMLElement> | null>(null);
26
27
  const [currentIndex, setCurrentIndex] = useState(-1);
27
28
 
28
- const registerContainer = useCallback((el: HTMLElement | null) => {
29
- containerRef.current = el;
30
- if (el) {
31
- const found = el.querySelectorAll("mark");
32
- setMarks(found);
33
- setCurrentIndex(found.length > 0 ? 0 : -1);
34
- } else {
29
+ const syncMarks = useCallback(() => {
30
+ const container = containerRef.current;
31
+ if (!container) {
35
32
  setMarks(null);
36
33
  setCurrentIndex(-1);
34
+ return;
37
35
  }
36
+
37
+ const found = container.querySelectorAll("mark");
38
+ setMarks(found);
39
+ setCurrentIndex(found.length > 0 ? 0 : -1);
38
40
  }, []);
39
41
 
42
+ const registerContainer = useCallback((el: HTMLElement | null) => {
43
+ containerRef.current = el;
44
+ syncMarks();
45
+ }, [syncMarks]);
46
+
40
47
  useEffect(() => {
41
48
  if (marks && currentIndex >= 0 && marks[currentIndex]) {
42
49
  marks.forEach((m) => m.classList.remove("bg-yellow-200", "bg-orange-300"));
@@ -63,6 +70,7 @@ export const HighlightProvider = ({ children }: { children: React.ReactNode }) =
63
70
  next,
64
71
  prev,
65
72
  registerContainer,
73
+ refreshMarks: syncMarks,
66
74
  }}
67
75
  >
68
76
  {children}