|
1 | | -import type React from 'react'; |
2 | | -import { useEffect, useState } from 'react'; |
3 | | - |
4 | | -export type Position = |
5 | | - | 'bottom-center' |
6 | | - | 'top-center' |
7 | | - | 'top-left' |
8 | | - | 'top-right' |
9 | | - | 'bottom-left' |
10 | | - | 'bottom-right'; |
11 | | - |
12 | | -export interface BreakPointerProps { |
13 | | - initiallyVisible?: boolean; |
14 | | - toggleKey?: string; |
15 | | - position?: Position; |
16 | | - zIndex?: number; |
17 | | - hideInProduction?: boolean; |
18 | | - showDimensions?: boolean; |
19 | | - className?: string; |
20 | | - style?: React.CSSProperties; |
21 | | - fontFamily?: string; |
22 | | -} |
23 | | - |
24 | | -const DEFAULT_FONT_FAMILY = |
25 | | - 'JetBrains Mono, ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "DejaVu Sans Mono", "Courier New", monospace'; |
26 | | - |
27 | | -const positionClasses: Record<Position, string> = { |
28 | | - 'bottom-center': 'fixed bottom-2 left-1/2 -translate-x-1/2', |
29 | | - 'top-center': 'fixed top-2 left-1/2 -translate-x-1/2', |
30 | | - 'top-left': 'fixed top-2 left-2', |
31 | | - 'top-right': 'fixed top-2 right-2', |
32 | | - 'bottom-left': 'fixed bottom-2 left-2', |
33 | | - 'bottom-right': 'fixed bottom-2 right-2', |
34 | | -}; |
35 | | - |
36 | | -export const BreakPointer: React.FC<BreakPointerProps> = ({ |
37 | | - initiallyVisible = true, |
38 | | - toggleKey = 't', |
39 | | - position = 'bottom-center', |
40 | | - zIndex = 9999, |
41 | | - hideInProduction = true, |
42 | | - showDimensions = true, |
43 | | - className = '', |
44 | | - style = {}, |
45 | | - fontFamily = DEFAULT_FONT_FAMILY, |
46 | | -}) => { |
47 | | - const [isVisible, setIsVisible] = useState(initiallyVisible); |
48 | | - const [viewport, setViewport] = useState({ |
49 | | - width: 0, |
50 | | - height: 0, |
51 | | - }); |
52 | | - const [isMounted, setIsMounted] = useState(false); |
53 | | - |
54 | | - useEffect(() => { |
55 | | - setIsMounted(true); |
56 | | - |
57 | | - const updateViewport = () => { |
58 | | - if (typeof window !== 'undefined') { |
59 | | - setViewport({ |
60 | | - width: window.innerWidth, |
61 | | - height: window.innerHeight, |
62 | | - }); |
63 | | - } |
64 | | - }; |
65 | | - |
66 | | - const handleKeyPress = (event: KeyboardEvent) => { |
67 | | - if (event.key === toggleKey || event.key === toggleKey.toUpperCase()) { |
68 | | - setIsVisible((prev) => !prev); |
69 | | - } |
70 | | - }; |
71 | | - |
72 | | - // Throttle resize events for better performance |
73 | | - let resizeTimeout: NodeJS.Timeout; |
74 | | - const throttledResize = () => { |
75 | | - clearTimeout(resizeTimeout); |
76 | | - resizeTimeout = setTimeout(updateViewport, 16); // ~60fps |
77 | | - }; |
78 | | - |
79 | | - updateViewport(); |
80 | | - |
81 | | - if (typeof window !== 'undefined' && typeof document !== 'undefined') { |
82 | | - document.addEventListener('keydown', handleKeyPress); |
83 | | - window.addEventListener('resize', throttledResize); |
84 | | - } |
85 | | - |
86 | | - return () => { |
87 | | - clearTimeout(resizeTimeout); |
88 | | - if (typeof window !== 'undefined' && typeof document !== 'undefined') { |
89 | | - document.removeEventListener('keydown', handleKeyPress); |
90 | | - window.removeEventListener('resize', throttledResize); |
91 | | - } |
92 | | - }; |
93 | | - }, [toggleKey]); |
94 | | - |
95 | | - // Early return for production builds |
96 | | - // In Next.js App Router, this check happens server-side, preventing the component from being sent to production |
97 | | - if ( |
98 | | - hideInProduction && |
99 | | - typeof process !== 'undefined' && |
100 | | - process.env?.NODE_ENV === 'production' |
101 | | - ) { |
102 | | - return null; |
103 | | - } |
104 | | - |
105 | | - if (!isMounted) { |
106 | | - return null; |
107 | | - } |
108 | | - |
109 | | - if (!isVisible) { |
110 | | - return null; |
111 | | - } |
112 | | - |
113 | | - const positionClass = positionClasses[position] || positionClasses['bottom-center']; |
114 | | - |
115 | | - const containerStyles: React.CSSProperties = { |
116 | | - zIndex, |
117 | | - fontFamily, |
118 | | - ...style, |
119 | | - }; |
120 | | - |
121 | | - return ( |
122 | | - <div |
123 | | - className={`${positionClass} rounded border-2 border-black text-xs ${className}`} |
124 | | - style={containerStyles} |
125 | | - aria-hidden="true" |
126 | | - > |
127 | | - <span className="block items-center bg-[#ec2427] px-2 py-1 font-mono font-semibold tracking-tighter text-white sm:hidden md:hidden lg:hidden xl:hidden 2xl:hidden"> |
128 | | - 1/6 <span className="text-black">•</span> xs <span className="text-black">•</span>{' '} |
129 | | - {showDimensions && ( |
130 | | - <span className="text-gray-100"> |
131 | | - {viewport.width}px <span className="text-black"><</span> 640px |
132 | | - </span> |
133 | | - )} |
134 | | - </span> |
135 | | - |
136 | | - <span className="hidden items-center bg-[#f36525] px-2 py-1 font-mono font-semibold tracking-tighter text-white sm:block md:hidden lg:hidden xl:hidden 2xl:hidden"> |
137 | | - 2/6 <span className="text-black">•</span> sm <span className="text-black">•</span>{' '} |
138 | | - {showDimensions && ( |
139 | | - <span className="text-gray-100"> |
140 | | - {viewport.width}px <span className="text-black"><</span> 768px |
141 | | - </span> |
142 | | - )} |
143 | | - </span> |
144 | | - |
145 | | - <span className="hidden items-center bg-[#edb41f] px-2 py-1 font-mono font-semibold tracking-tighter text-white md:block lg:hidden xl:hidden 2xl:hidden"> |
146 | | - 3/6 <span className="text-black">•</span> md <span className="text-black">•</span>{' '} |
147 | | - {showDimensions && ( |
148 | | - <span className="text-gray-100"> |
149 | | - {viewport.width}px <span className="text-black"><</span> 1024px |
150 | | - </span> |
151 | | - )} |
152 | | - </span> |
153 | | - |
154 | | - <span className="hidden items-center bg-[#f7ee49] px-2 py-1 font-mono font-semibold tracking-tighter text-black lg:block xl:hidden 2xl:hidden"> |
155 | | - 4/6 <span className="text-black">•</span> lg <span className="text-black">•</span>{' '} |
156 | | - {showDimensions && ( |
157 | | - <span className="text-black"> |
158 | | - {viewport.width}px <span className="text-black"><</span> 1280px |
159 | | - </span> |
160 | | - )} |
161 | | - </span> |
162 | | - |
163 | | - <span className="hidden items-center bg-[#4686c5] px-2 py-1 font-mono font-semibold tracking-tighter text-white xl:block 2xl:hidden"> |
164 | | - 5/6 <span className="text-black">•</span> xl <span className="text-black">•</span>{' '} |
165 | | - {showDimensions && ( |
166 | | - <span className="text-gray-100"> |
167 | | - {viewport.width}px <span className="text-black"><</span> 1536px |
168 | | - </span> |
169 | | - )} |
170 | | - </span> |
171 | | - |
172 | | - <span className="hidden items-center bg-[#45b64a] px-2 py-1 font-mono font-semibold tracking-tighter text-white 2xl:block"> |
173 | | - 6/6 <span className="text-black">•</span> 2xl <span className="text-black">•</span>{' '} |
174 | | - {showDimensions && ( |
175 | | - <span className="text-gray-100"> |
176 | | - {viewport.width}px <span className="text-black">≥</span> 1536px |
177 | | - </span> |
178 | | - )} |
179 | | - </span> |
180 | | - </div> |
181 | | - ); |
182 | | -}; |
183 | | - |
184 | | -// Remove default export to avoid mixed exports warning |
| 1 | +export { BreakPointer, type BreakPointerProps, type Position } from './client'; |
0 commit comments