@catalystsoftware/ui 1.0.4 → 1.0.5
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/data/tailwind.config.js +261 -3821
- package/dist/components/catalyst-ui/buttons/burger.tsx +207 -0
- package/dist/components/catalyst-ui/core/data-display/timeline.tsx +210 -0
- package/dist/components/catalyst-ui/core/feedback/alert.tsx +491 -0
- package/dist/components/catalyst-ui/core/feedback/spinner-1.tsx +65 -0
- package/dist/components/catalyst-ui/core/feedback/toast.tsx +1857 -0
- package/dist/components/catalyst-ui/core/navigation/menu.tsx +164 -0
- package/dist/components/catalyst-ui/forms/toggle-class.tsx +176 -0
- package/dist/components/catalyst-ui/hooks/use-copy-to-clipboard.tsx +419 -0
- package/dist/components/catalyst-ui/hooks/use-counter.tsx +13 -0
- package/dist/components/catalyst-ui/hooks/use-event-listener.tsx +23 -0
- package/dist/components/catalyst-ui/hooks/use-export-markdown.tsx +47 -0
- package/dist/components/catalyst-ui/hooks/use-focus.tsx +17 -0
- package/dist/components/catalyst-ui/hooks/use-interval.tsx +23 -0
- package/dist/components/catalyst-ui/hooks/use-is-client.tsx +16 -0
- package/dist/components/catalyst-ui/hooks/use-media-query.tsx +19 -0
- package/dist/components/catalyst-ui/hooks/use-mobile.tsx +19 -0
- package/dist/components/catalyst-ui/hooks/use-resize-observer.tsx +81 -0
- package/dist/components/catalyst-ui/hooks/use-timeout.tsx +21 -0
- package/dist/components/catalyst-ui/hooks/use-timer.tsx +209 -0
- package/dist/components/catalyst-ui/hooks/use-toggle.tsx +12 -0
- package/dist/components/catalyst-ui/media/image.tsx +13 -0
- package/dist/components/catalyst-ui/overlays/dual-sidebar.tsx +4142 -0
- package/dist/components/catalyst-ui/overlays/sidebar-original.tsx +726 -0
- package/dist/components/catalyst-ui/primitives/accordion.tsx +250 -0
- package/dist/components/catalyst-ui/primitives/alert-dialog.tsx +126 -0
- package/dist/components/catalyst-ui/primitives/aspect-ratio.tsx +9 -0
- package/dist/components/catalyst-ui/primitives/avatar.tsx +296 -0
- package/dist/components/catalyst-ui/primitives/badge.tsx +57 -0
- package/dist/components/catalyst-ui/primitives/breadcrumb.tsx +101 -0
- package/dist/components/catalyst-ui/primitives/button.tsx +265 -0
- package/dist/components/catalyst-ui/primitives/calendar-v4.tsx +208 -0
- package/dist/components/catalyst-ui/primitives/calendar.tsx +295 -0
- package/dist/components/catalyst-ui/primitives/card.tsx +618 -0
- package/dist/components/catalyst-ui/primitives/carousel.tsx +238 -0
- package/dist/components/catalyst-ui/primitives/chart.tsx +347 -0
- package/dist/components/catalyst-ui/primitives/checkbox.tsx +225 -0
- package/dist/components/catalyst-ui/primitives/collapsible.tsx +212 -0
- package/dist/components/catalyst-ui/primitives/command.tsx +393 -0
- package/dist/components/catalyst-ui/primitives/context-menu.tsx +236 -0
- package/dist/components/catalyst-ui/primitives/dialog.tsx +471 -0
- package/dist/components/catalyst-ui/primitives/drawer.tsx +761 -0
- package/dist/components/catalyst-ui/primitives/dropdown-menu.tsx +290 -0
- package/dist/components/catalyst-ui/primitives/empty.tsx +104 -0
- package/dist/components/catalyst-ui/primitives/field.tsx +244 -0
- package/dist/components/catalyst-ui/primitives/hover-card.tsx +124 -0
- package/dist/components/catalyst-ui/primitives/input-otp.tsx +76 -0
- package/dist/components/catalyst-ui/primitives/input.tsx +64 -0
- package/dist/components/catalyst-ui/primitives/item.tsx +196 -0
- package/dist/components/catalyst-ui/primitives/kbd.tsx +75 -0
- package/dist/components/catalyst-ui/primitives/label.tsx +24 -0
- package/dist/components/catalyst-ui/primitives/navigation-menu.tsx +150 -0
- package/dist/components/catalyst-ui/primitives/pagination.tsx +198 -0
- package/dist/components/catalyst-ui/primitives/popover.tsx +232 -0
- package/dist/components/catalyst-ui/primitives/progress.tsx +34 -0
- package/dist/components/catalyst-ui/primitives/radio-group.tsx +43 -0
- package/dist/components/catalyst-ui/primitives/resizable.tsx +56 -0
- package/dist/components/catalyst-ui/primitives/select.tsx +155 -0
- package/dist/components/catalyst-ui/primitives/separator.tsx +74 -0
- package/dist/components/catalyst-ui/primitives/sheet.tsx +126 -0
- package/dist/components/catalyst-ui/primitives/skeleton.tsx +15 -0
- package/dist/components/catalyst-ui/primitives/slider.tsx +27 -0
- package/dist/components/catalyst-ui/primitives/switch.tsx +187 -0
- package/dist/components/catalyst-ui/primitives/tabs.tsx +335 -0
- package/dist/components/catalyst-ui/primitives/textarea.tsx +24 -0
- package/dist/components/catalyst-ui/primitives/toggle-group.tsx +55 -0
- package/dist/components/catalyst-ui/primitives/toggle.tsx +42 -0
- package/dist/components/catalyst-ui/primitives/tooltip.tsx +116 -0
- package/dist/components/catalyst-ui/utils/basic-auth.tsx +40 -0
- package/dist/components/catalyst-ui/utils/context-storage.tsx +19 -0
- package/dist/components/catalyst-ui/utils/cors-middleware.tsx +71 -0
- package/dist/components/catalyst-ui/utils/deferred-content.tsx +595 -0
- package/dist/components/catalyst-ui/utils/honeypot-middleware.tsx +38 -0
- package/dist/components/catalyst-ui/utils/incId.tsx +75 -0
- package/dist/components/catalyst-ui/utils/jwk-auth.tsx +36 -0
- package/dist/components/catalyst-ui/utils/request-id.tsx +14 -0
- package/dist/components/catalyst-ui/utils/secure-headers.tsx +37 -0
- package/dist/components/catalyst-ui/utils/server-timing.tsx +23 -0
- package/dist/components/catalyst-ui/utils/utils.ts +43 -0
- package/dist/components/catalyst-ui/utils/with-cookie.tsx +43 -0
- package/dist/components/catalyst-ui/x/accordian-x.tsx +428 -0
- package/dist/components/catalyst-ui/x/alert-x.tsx +413 -0
- package/dist/components/catalyst-ui/x/animated-text-x.tsx +2242 -0
- package/dist/components/catalyst-ui/x/avatar-x.tsx +515 -0
- package/dist/components/catalyst-ui/x/badge-x.tsx +670 -0
- package/dist/components/catalyst-ui/x/button-X.tsx +2857 -0
- package/dist/components/catalyst-ui/x/button-group-x.tsx +847 -0
- package/dist/components/catalyst-ui/x/calendar-x.tsx +1910 -0
- package/dist/components/catalyst-ui/x/card-x.tsx +2597 -0
- package/dist/components/catalyst-ui/x/checkbox-x.tsx +656 -0
- package/dist/components/catalyst-ui/x/collapsible-x.tsx +1360 -0
- package/dist/components/catalyst-ui/x/combobox-x.tsx +911 -0
- package/dist/components/catalyst-ui/x/data-table-x.tsx +1753 -0
- package/dist/components/catalyst-ui/x/date-picker-x.tsx +648 -0
- package/dist/components/catalyst-ui/x/dialog-x.tsx +659 -0
- package/dist/components/catalyst-ui/x/dropdown-menu-x.tsx +612 -0
- package/dist/components/catalyst-ui/x/hover-card-x.tsx +375 -0
- package/dist/components/catalyst-ui/x/icon-x.tsx +840 -0
- package/dist/components/catalyst-ui/x/input-mask-x.tsx +981 -0
- package/dist/components/catalyst-ui/x/input-otp-x.tsx +659 -0
- package/dist/components/catalyst-ui/x/loader-x.tsx +1757 -0
- package/dist/components/catalyst-ui/x/pagination-x.tsx +622 -0
- package/dist/components/catalyst-ui/x/popover-x.tsx +744 -0
- package/dist/components/catalyst-ui/x/radio-group-x.tsx +499 -0
- package/dist/components/catalyst-ui/x/select-x.tsx +1127 -0
- package/dist/components/catalyst-ui/x/sheet-x.tsx +668 -0
- package/dist/components/catalyst-ui/x/switch-x.tsx +681 -0
- package/dist/components/catalyst-ui/x/table-x.tsx +574 -0
- package/dist/components/catalyst-ui/x/tabs-x.tsx +839 -0
- package/dist/components/catalyst-ui/x/textarea-x.tsx +1263 -0
- package/dist/components/catalyst-ui/x/tooltip-x.tsx +396 -0
- package/dist/components/catalyst-ui/x/tracker-x.tsx +560 -0
- package/dist/data/bg-data.tsx +901 -0
- package/dist/data/buttons-data.tsx +2327 -0
- package/dist/data/charts-data.tsx +102 -0
- package/dist/data/chat-data.tsx +83 -0
- package/dist/data/code-data.tsx +1040 -0
- package/dist/data/comboboxes-data.tsx +1843 -0
- package/dist/data/command-data.tsx +1381 -0
- package/dist/data/core-data.tsx +15953 -0
- package/dist/data/crm-data.tsx +47 -0
- package/dist/data/data.tsx +159 -0
- package/dist/data/date-and-time-data.tsx +554 -0
- package/dist/data/dependencies.tsx +7 -0
- package/dist/data/ecommerce-data.tsx +1387 -0
- package/dist/data/forms-data.tsx +7890 -0
- package/dist/data/hooks-data.tsx +5487 -0
- package/dist/data/index.ts +34 -0
- package/dist/data/inputs-data.tsx +557 -0
- package/dist/data/interactive-data.tsx +5394 -0
- package/dist/data/lofi-data.tsx +18295 -0
- package/dist/data/marketing-data.tsx +2546 -0
- package/dist/data/media-data.tsx +1510 -0
- package/dist/data/motion-data.tsx +5801 -0
- package/dist/data/overlay-data.tsx +4136 -0
- package/dist/data/pdf-data.tsx +124 -0
- package/dist/data/pos-data.tsx +213 -0
- package/dist/data/postcss.config.js +6 -0
- package/dist/data/primitive-data.tsx +5170 -0
- package/dist/data/prompt-data.tsx +1226 -0
- package/dist/data/requiredLibs.ts +4 -0
- package/dist/data/sandbox-data.tsx +1 -0
- package/dist/data/sidebars-data.tsx +5421 -0
- package/dist/data/stacks-data.tsx +32 -0
- package/dist/data/table-data.tsx +706 -0
- package/dist/data/tailwind.config.js +270 -0
- package/dist/data/tailwind.config.ngin.js +3830 -0
- package/dist/data/tailwind.css +431 -0
- package/dist/data/tools-data.tsx +6910 -0
- package/dist/data/typography-data.tsx +2050 -0
- package/dist/data/utils-data.tsx +6500 -0
- package/dist/data/x-data.tsx +1171 -0
- package/dist/data.tsx +159 -0
- package/package.json +1 -1
- package/dist/index.d.ts +0 -3
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -362
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { useState, useEffect, useCallback, useRef } from "react";
|
|
2
|
+
|
|
3
|
+
export function CORSMiddleware() {
|
|
4
|
+
const handleCORS = useCallback((
|
|
5
|
+
request: Request,
|
|
6
|
+
options: CORSOptions
|
|
7
|
+
): Response | null => {
|
|
8
|
+
const origin = request.headers.get('Origin');
|
|
9
|
+
const method = request.method;
|
|
10
|
+
|
|
11
|
+
if (method === 'OPTIONS') {
|
|
12
|
+
const headers = new Headers();
|
|
13
|
+
|
|
14
|
+
if (typeof options.origin === 'boolean') {
|
|
15
|
+
headers.set('Access-Control-Allow-Origin', options.origin ? '*' : 'null');
|
|
16
|
+
} else if (typeof options.origin === 'string') {
|
|
17
|
+
headers.set('Access-Control-Allow-Origin', options.origin);
|
|
18
|
+
} else if (Array.isArray(options.origin) && origin) {
|
|
19
|
+
if (options.origin.includes(origin)) {
|
|
20
|
+
headers.set('Access-Control-Allow-Origin', origin);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
if (options.credentials) {
|
|
25
|
+
headers.set('Access-Control-Allow-Credentials', 'true');
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
if (options.methods) {
|
|
29
|
+
headers.set('Access-Control-Allow-Methods', options.methods.join(', '));
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
if (options.headers) {
|
|
33
|
+
headers.set('Access-Control-Allow-Headers', options.headers.join(', '));
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return new Response(null, { status: 204, headers });
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return null;
|
|
40
|
+
}, []);
|
|
41
|
+
|
|
42
|
+
const addCORSHeaders = useCallback((
|
|
43
|
+
response: Response,
|
|
44
|
+
options: CORSOptions,
|
|
45
|
+
requestOrigin?: string
|
|
46
|
+
): Response => {
|
|
47
|
+
const headers = new Headers(response.headers);
|
|
48
|
+
|
|
49
|
+
if (typeof options.origin === 'boolean') {
|
|
50
|
+
headers.set('Access-Control-Allow-Origin', options.origin ? '*' : 'null');
|
|
51
|
+
} else if (typeof options.origin === 'string') {
|
|
52
|
+
headers.set('Access-Control-Allow-Origin', options.origin);
|
|
53
|
+
} else if (Array.isArray(options.origin) && requestOrigin) {
|
|
54
|
+
if (options.origin.includes(requestOrigin)) {
|
|
55
|
+
headers.set('Access-Control-Allow-Origin', requestOrigin);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (options.credentials) {
|
|
60
|
+
headers.set('Access-Control-Allow-Credentials', 'true');
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return new Response(response.body, {
|
|
64
|
+
status: response.status,
|
|
65
|
+
statusText: response.statusText,
|
|
66
|
+
headers,
|
|
67
|
+
});
|
|
68
|
+
}, []);
|
|
69
|
+
|
|
70
|
+
return { handleCORS, addCORSHeaders };
|
|
71
|
+
}
|
|
@@ -0,0 +1,595 @@
|
|
|
1
|
+
import React, { useState, useRef, useEffect, ReactNode } from 'react';
|
|
2
|
+
import { cn } from '~/components/catalyst-ui';
|
|
3
|
+
import { Loader2 } from 'lucide-react';
|
|
4
|
+
|
|
5
|
+
export interface DeferredContentProps {
|
|
6
|
+
children: ReactNode;
|
|
7
|
+
className?: string;
|
|
8
|
+
onLoad?: () => void;
|
|
9
|
+
loading?: boolean;
|
|
10
|
+
loadingTemplate?: ReactNode;
|
|
11
|
+
threshold?: number;
|
|
12
|
+
rootMargin?: string;
|
|
13
|
+
root?: Element | null;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function DeferredContent({
|
|
17
|
+
children,
|
|
18
|
+
className,
|
|
19
|
+
onLoad,
|
|
20
|
+
loading = false,
|
|
21
|
+
loadingTemplate,
|
|
22
|
+
threshold = 0,
|
|
23
|
+
rootMargin = '0px',
|
|
24
|
+
root = null,
|
|
25
|
+
}: DeferredContentProps) {
|
|
26
|
+
const [isVisible, setIsVisible] = useState(false);
|
|
27
|
+
const [hasLoaded, setHasLoaded] = useState(false);
|
|
28
|
+
const elementRef = useRef<HTMLDivElement>(null);
|
|
29
|
+
|
|
30
|
+
useEffect(() => {
|
|
31
|
+
const element = elementRef.current;
|
|
32
|
+
if (!element) return;
|
|
33
|
+
|
|
34
|
+
const observer = new IntersectionObserver(
|
|
35
|
+
(entries) => {
|
|
36
|
+
const [entry] = entries;
|
|
37
|
+
if (entry.isIntersecting && !hasLoaded) {
|
|
38
|
+
setIsVisible(true);
|
|
39
|
+
setHasLoaded(true);
|
|
40
|
+
onLoad?.();
|
|
41
|
+
observer.unobserve(element);
|
|
42
|
+
}
|
|
43
|
+
},
|
|
44
|
+
{
|
|
45
|
+
threshold,
|
|
46
|
+
rootMargin,
|
|
47
|
+
root,
|
|
48
|
+
}
|
|
49
|
+
);
|
|
50
|
+
|
|
51
|
+
observer.observe(element);
|
|
52
|
+
|
|
53
|
+
return () => {
|
|
54
|
+
if (element) {
|
|
55
|
+
observer.unobserve(element);
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
}, [threshold, rootMargin, root, hasLoaded, onLoad]);
|
|
59
|
+
|
|
60
|
+
const defaultLoadingTemplate = (
|
|
61
|
+
<div className="flex items-center justify-center p-8">
|
|
62
|
+
<Loader2 className="h-6 w-6 animate-spin text-muted-foreground" />
|
|
63
|
+
</div>
|
|
64
|
+
);
|
|
65
|
+
|
|
66
|
+
return (
|
|
67
|
+
<div
|
|
68
|
+
ref={elementRef}
|
|
69
|
+
className={cn('min-h-[1px]', className)}
|
|
70
|
+
>
|
|
71
|
+
{hasLoaded && !loading ? (
|
|
72
|
+
children
|
|
73
|
+
) : (
|
|
74
|
+
loadingTemplate || defaultLoadingTemplate
|
|
75
|
+
)}
|
|
76
|
+
</div>
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export interface DeferredImageProps {
|
|
81
|
+
src: string;
|
|
82
|
+
alt?: string;
|
|
83
|
+
className?: string;
|
|
84
|
+
loadingClassName?: string;
|
|
85
|
+
threshold?: number;
|
|
86
|
+
rootMargin?: string;
|
|
87
|
+
onLoad?: () => void;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export function DeferredImage({
|
|
91
|
+
src,
|
|
92
|
+
alt = '',
|
|
93
|
+
className,
|
|
94
|
+
loadingClassName,
|
|
95
|
+
threshold = 0,
|
|
96
|
+
rootMargin = '0px',
|
|
97
|
+
onLoad,
|
|
98
|
+
}: DeferredImageProps) {
|
|
99
|
+
const [imageLoaded, setImageLoaded] = useState(false);
|
|
100
|
+
const [imageError, setImageError] = useState(false);
|
|
101
|
+
|
|
102
|
+
const handleImageLoad = () => {
|
|
103
|
+
setImageLoaded(true);
|
|
104
|
+
onLoad?.();
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
const handleImageError = () => {
|
|
108
|
+
setImageError(true);
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
const loadingTemplate = (
|
|
112
|
+
<div className={cn(
|
|
113
|
+
'bg-muted animate-pulse rounded-md flex items-center justify-center',
|
|
114
|
+
'min-h-[200px] w-full',
|
|
115
|
+
loadingClassName
|
|
116
|
+
)}>
|
|
117
|
+
<div className="text-muted-foreground text-sm">Loading...</div>
|
|
118
|
+
</div>
|
|
119
|
+
);
|
|
120
|
+
|
|
121
|
+
return (
|
|
122
|
+
<DeferredContent
|
|
123
|
+
threshold={threshold}
|
|
124
|
+
rootMargin={rootMargin}
|
|
125
|
+
loadingTemplate={loadingTemplate}
|
|
126
|
+
>
|
|
127
|
+
{imageError ? (
|
|
128
|
+
<div className={cn(
|
|
129
|
+
'bg-muted rounded-md flex items-center justify-center',
|
|
130
|
+
'min-h-[200px] w-full text-muted-foreground',
|
|
131
|
+
className
|
|
132
|
+
)}>
|
|
133
|
+
Failed to load image
|
|
134
|
+
</div>
|
|
135
|
+
) : (
|
|
136
|
+
<img
|
|
137
|
+
src={src}
|
|
138
|
+
alt={alt}
|
|
139
|
+
className={cn(
|
|
140
|
+
'transition-opacity duration-300',
|
|
141
|
+
imageLoaded ? 'opacity-100' : 'opacity-0',
|
|
142
|
+
className
|
|
143
|
+
)}
|
|
144
|
+
onLoad={handleImageLoad}
|
|
145
|
+
onError={handleImageError}
|
|
146
|
+
/>
|
|
147
|
+
)}
|
|
148
|
+
</DeferredContent>
|
|
149
|
+
);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
export interface DeferredTableProps {
|
|
153
|
+
data: any[];
|
|
154
|
+
columns: Array<{
|
|
155
|
+
field: string;
|
|
156
|
+
header: string;
|
|
157
|
+
className?: string;
|
|
158
|
+
}>;
|
|
159
|
+
className?: string;
|
|
160
|
+
threshold?: number;
|
|
161
|
+
rootMargin?: string;
|
|
162
|
+
onLoad?: () => void;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
export function DeferredTable({
|
|
166
|
+
data,
|
|
167
|
+
columns,
|
|
168
|
+
className,
|
|
169
|
+
threshold = 0,
|
|
170
|
+
rootMargin = '0px',
|
|
171
|
+
onLoad,
|
|
172
|
+
}: DeferredTableProps) {
|
|
173
|
+
const loadingTemplate = (
|
|
174
|
+
<div className="space-y-4">
|
|
175
|
+
<div className="h-8 bg-muted animate-pulse rounded" />
|
|
176
|
+
{Array.from({ length: 5 }).map((_, i) => (
|
|
177
|
+
<div key={i} className="h-12 bg-muted animate-pulse rounded" />
|
|
178
|
+
))}
|
|
179
|
+
</div>
|
|
180
|
+
);
|
|
181
|
+
|
|
182
|
+
return (
|
|
183
|
+
<DeferredContent
|
|
184
|
+
threshold={threshold}
|
|
185
|
+
rootMargin={rootMargin}
|
|
186
|
+
loadingTemplate={loadingTemplate}
|
|
187
|
+
onLoad={onLoad}
|
|
188
|
+
>
|
|
189
|
+
<div className={cn('overflow-x-auto', className)}>
|
|
190
|
+
<table className="w-full border-collapse">
|
|
191
|
+
<thead>
|
|
192
|
+
<tr className="border-b border-border">
|
|
193
|
+
{columns.map((column) => (
|
|
194
|
+
<th
|
|
195
|
+
key={column.field}
|
|
196
|
+
className={cn(
|
|
197
|
+
'text-left p-4 font-medium text-foreground',
|
|
198
|
+
column.className
|
|
199
|
+
)}
|
|
200
|
+
>
|
|
201
|
+
{column.header}
|
|
202
|
+
</th>
|
|
203
|
+
))}
|
|
204
|
+
</tr>
|
|
205
|
+
</thead>
|
|
206
|
+
<tbody>
|
|
207
|
+
{data.map((row, index) => (
|
|
208
|
+
<tr
|
|
209
|
+
key={index}
|
|
210
|
+
className="border-b border-border hover:bg-muted/50"
|
|
211
|
+
>
|
|
212
|
+
{columns.map((column) => (
|
|
213
|
+
<td
|
|
214
|
+
key={column.field}
|
|
215
|
+
className={cn('p-4 text-foreground', column.className)}
|
|
216
|
+
>
|
|
217
|
+
{row[column.field]}
|
|
218
|
+
</td>
|
|
219
|
+
))}
|
|
220
|
+
</tr>
|
|
221
|
+
))}
|
|
222
|
+
</tbody>
|
|
223
|
+
</table>
|
|
224
|
+
</div>
|
|
225
|
+
</DeferredContent>
|
|
226
|
+
);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
export interface DeferredCardProps {
|
|
230
|
+
title: string;
|
|
231
|
+
description?: string;
|
|
232
|
+
imageUrl?: string;
|
|
233
|
+
buttonText?: string;
|
|
234
|
+
secondaryButtonText?: string;
|
|
235
|
+
onButtonClick?: () => void;
|
|
236
|
+
onSecondaryButtonClick?: () => void;
|
|
237
|
+
className?: string;
|
|
238
|
+
threshold?: number;
|
|
239
|
+
rootMargin?: string;
|
|
240
|
+
onLoad?: () => void;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
export function DeferredCard({
|
|
244
|
+
title,
|
|
245
|
+
description,
|
|
246
|
+
imageUrl,
|
|
247
|
+
buttonText,
|
|
248
|
+
secondaryButtonText,
|
|
249
|
+
onButtonClick,
|
|
250
|
+
onSecondaryButtonClick,
|
|
251
|
+
className,
|
|
252
|
+
threshold = 0,
|
|
253
|
+
rootMargin = '0px',
|
|
254
|
+
onLoad,
|
|
255
|
+
}: DeferredCardProps) {
|
|
256
|
+
const loadingTemplate = (
|
|
257
|
+
<div className="bg-background border border-border rounded-lg p-6 space-y-4">
|
|
258
|
+
<div className="h-6 bg-muted animate-pulse rounded" />
|
|
259
|
+
<div className="h-4 bg-muted animate-pulse rounded" />
|
|
260
|
+
<div className="h-4 bg-muted animate-pulse rounded w-2/3" />
|
|
261
|
+
{imageUrl && <div className="h-48 bg-muted animate-pulse rounded" />}
|
|
262
|
+
<div className="flex gap-2">
|
|
263
|
+
<div className="h-10 bg-muted animate-pulse rounded w-24" />
|
|
264
|
+
{secondaryButtonText && (
|
|
265
|
+
<div className="h-10 bg-muted animate-pulse rounded w-24" />
|
|
266
|
+
)}
|
|
267
|
+
</div>
|
|
268
|
+
</div>
|
|
269
|
+
);
|
|
270
|
+
|
|
271
|
+
return (
|
|
272
|
+
<DeferredContent
|
|
273
|
+
threshold={threshold}
|
|
274
|
+
rootMargin={rootMargin}
|
|
275
|
+
loadingTemplate={loadingTemplate}
|
|
276
|
+
onLoad={onLoad}
|
|
277
|
+
>
|
|
278
|
+
<div className={cn(
|
|
279
|
+
'bg-background border border-border rounded-lg p-6 space-y-4',
|
|
280
|
+
className
|
|
281
|
+
)}>
|
|
282
|
+
<h3 className="text-lg font-semibold text-foreground">{title}</h3>
|
|
283
|
+
{description && (
|
|
284
|
+
<p className="text-muted-foreground">{description}</p>
|
|
285
|
+
)}
|
|
286
|
+
{imageUrl && (
|
|
287
|
+
<DeferredImage
|
|
288
|
+
src={imageUrl}
|
|
289
|
+
alt={title}
|
|
290
|
+
className="w-full h-48 object-cover rounded-md"
|
|
291
|
+
/>
|
|
292
|
+
)}
|
|
293
|
+
{(buttonText || secondaryButtonText) && (
|
|
294
|
+
<div className="flex gap-2">
|
|
295
|
+
{buttonText && (
|
|
296
|
+
<button
|
|
297
|
+
onClick={onButtonClick}
|
|
298
|
+
className="px-4 py-2 bg-primary text-primary-foreground rounded-md hover:bg-primary/90 transition-colors"
|
|
299
|
+
>
|
|
300
|
+
{buttonText}
|
|
301
|
+
</button>
|
|
302
|
+
)}
|
|
303
|
+
{secondaryButtonText && (
|
|
304
|
+
<button
|
|
305
|
+
onClick={onSecondaryButtonClick}
|
|
306
|
+
className="px-4 py-2 border border-border text-foreground rounded-md hover:bg-muted transition-colors"
|
|
307
|
+
>
|
|
308
|
+
{secondaryButtonText}
|
|
309
|
+
</button>
|
|
310
|
+
)}
|
|
311
|
+
</div>
|
|
312
|
+
)}
|
|
313
|
+
</div>
|
|
314
|
+
</DeferredContent>
|
|
315
|
+
);
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
|
|
319
|
+
export default function DeferredContentDemo() {
|
|
320
|
+
const tableData = [
|
|
321
|
+
{ id: 1, name: 'John Doe', email: 'john@example.com', role: 'Developer' },
|
|
322
|
+
{ id: 2, name: 'Jane Smith', email: 'jane@example.com', role: 'Designer' },
|
|
323
|
+
{ id: 3, name: 'Bob Johnson', email: 'bob@example.com', role: 'Manager' },
|
|
324
|
+
{ id: 4, name: 'Alice Brown', email: 'alice@example.com', role: 'Developer' },
|
|
325
|
+
{ id: 5, name: 'Charlie Wilson', email: 'charlie@example.com', role: 'Designer' },
|
|
326
|
+
];
|
|
327
|
+
|
|
328
|
+
const tableColumns = [
|
|
329
|
+
{ field: 'id', header: 'ID', className: 'w-16' },
|
|
330
|
+
{ field: 'name', header: 'Name' },
|
|
331
|
+
{ field: 'email', header: 'Email' },
|
|
332
|
+
{ field: 'role', header: 'Role' },
|
|
333
|
+
];
|
|
334
|
+
|
|
335
|
+
const handleLoad = (section: string) => {
|
|
336
|
+
console.log(`${section} loaded!`);
|
|
337
|
+
};
|
|
338
|
+
|
|
339
|
+
return (
|
|
340
|
+
<div className="min-h-screen bg-background">
|
|
341
|
+
<div className="container mx-auto px-4 py-8 space-y-12">
|
|
342
|
+
<div className="text-center space-y-4">
|
|
343
|
+
<h1 className="text-4xl font-bold text-foreground">
|
|
344
|
+
DeferredContent Demo
|
|
345
|
+
</h1>
|
|
346
|
+
<p className="text-lg text-muted-foreground max-w-2xl mx-auto">
|
|
347
|
+
Scroll down to see different deferred content components load as they come into view.
|
|
348
|
+
This technique improves page performance by only loading content when needed.
|
|
349
|
+
</p>
|
|
350
|
+
</div>
|
|
351
|
+
|
|
352
|
+
<div className="h-screen flex items-center justify-center bg-muted/20 rounded-lg">
|
|
353
|
+
<div className="text-center space-y-4">
|
|
354
|
+
<h2 className="text-2xl font-semibold text-foreground">
|
|
355
|
+
Scroll Down to See Deferred Content
|
|
356
|
+
</h2>
|
|
357
|
+
<p className="text-muted-foreground">
|
|
358
|
+
The components below will load when they become visible
|
|
359
|
+
</p>
|
|
360
|
+
</div>
|
|
361
|
+
</div>
|
|
362
|
+
|
|
363
|
+
<section className="space-y-6">
|
|
364
|
+
<h2 className="text-2xl font-bold text-foreground">Basic Deferred Content</h2>
|
|
365
|
+
<DeferredContent
|
|
366
|
+
onLoad={() => handleLoad('Basic Content')}
|
|
367
|
+
className="bg-card border border-border rounded-lg p-8"
|
|
368
|
+
>
|
|
369
|
+
<div className="text-center space-y-4">
|
|
370
|
+
<h3 className="text-xl font-semibold text-foreground">
|
|
371
|
+
This content was loaded lazily!
|
|
372
|
+
</h3>
|
|
373
|
+
<p className="text-muted-foreground">
|
|
374
|
+
This content only loaded when it became visible in the viewport.
|
|
375
|
+
This helps improve initial page load performance.
|
|
376
|
+
</p>
|
|
377
|
+
</div>
|
|
378
|
+
</DeferredContent>
|
|
379
|
+
</section>
|
|
380
|
+
|
|
381
|
+
<section className="space-y-6">
|
|
382
|
+
<h2 className="text-2xl font-bold text-foreground">Deferred Images</h2>
|
|
383
|
+
<div className="grid md:grid-cols-2 gap-6">
|
|
384
|
+
<DeferredImage
|
|
385
|
+
src="https://images.unsplash.com/photo-1506905925346-21bda4d32df4?w=500&h=300&fit=crop"
|
|
386
|
+
alt="Mountain landscape"
|
|
387
|
+
className="w-full h-64 object-cover rounded-lg"
|
|
388
|
+
onLoad={() => handleLoad('Mountain Image')}
|
|
389
|
+
/>
|
|
390
|
+
<DeferredImage
|
|
391
|
+
src="https://images.unsplash.com/photo-1441974231531-c6227db76b6e?w=500&h=300&fit=crop"
|
|
392
|
+
alt="Forest landscape"
|
|
393
|
+
className="w-full h-64 object-cover rounded-lg"
|
|
394
|
+
onLoad={() => handleLoad('Forest Image')}
|
|
395
|
+
/>
|
|
396
|
+
</div>
|
|
397
|
+
</section>
|
|
398
|
+
|
|
399
|
+
<section className="space-y-6">
|
|
400
|
+
<h2 className="text-2xl font-bold text-foreground">Deferred Table</h2>
|
|
401
|
+
<DeferredTable
|
|
402
|
+
data={tableData}
|
|
403
|
+
columns={tableColumns}
|
|
404
|
+
className="bg-card border border-border rounded-lg overflow-hidden"
|
|
405
|
+
onLoad={() => handleLoad('User Table')}
|
|
406
|
+
/>
|
|
407
|
+
</section>
|
|
408
|
+
|
|
409
|
+
<section className="space-y-6">
|
|
410
|
+
<h2 className="text-2xl font-bold text-foreground">Deferred Cards</h2>
|
|
411
|
+
<div className="grid md:grid-cols-2 lg:grid-cols-3 gap-6">
|
|
412
|
+
<DeferredCard
|
|
413
|
+
title="Product Demo"
|
|
414
|
+
description="Experience our latest product features with this interactive demonstration."
|
|
415
|
+
imageUrl="https://images.unsplash.com/photo-1460925895917-afdab827c52f?w=400&h=200&fit=crop"
|
|
416
|
+
buttonText="Try Demo"
|
|
417
|
+
secondaryButtonText="Learn More"
|
|
418
|
+
onButtonClick={() => console.log('Demo clicked')}
|
|
419
|
+
onSecondaryButtonClick={() => console.log('Learn more clicked')}
|
|
420
|
+
onLoad={() => handleLoad('Product Card')}
|
|
421
|
+
/>
|
|
422
|
+
|
|
423
|
+
<DeferredCard
|
|
424
|
+
title="Get Started"
|
|
425
|
+
description="Ready to begin your journey? Follow our comprehensive getting started guide."
|
|
426
|
+
buttonText="Start Now"
|
|
427
|
+
onButtonClick={() => console.log('Start clicked')}
|
|
428
|
+
onLoad={() => handleLoad('Getting Started Card')}
|
|
429
|
+
/>
|
|
430
|
+
|
|
431
|
+
<DeferredCard
|
|
432
|
+
title="Documentation"
|
|
433
|
+
description="Explore our complete documentation with examples, API references, and best practices."
|
|
434
|
+
imageUrl="https://images.unsplash.com/photo-1481627834876-b7833e8f5570?w=400&h=200&fit=crop"
|
|
435
|
+
buttonText="Read Docs"
|
|
436
|
+
secondaryButtonText="API Reference"
|
|
437
|
+
onButtonClick={() => console.log('Docs clicked')}
|
|
438
|
+
onSecondaryButtonClick={() => console.log('API clicked')}
|
|
439
|
+
onLoad={() => handleLoad('Documentation Card')}
|
|
440
|
+
/>
|
|
441
|
+
</div>
|
|
442
|
+
</section>
|
|
443
|
+
|
|
444
|
+
<section className="space-y-6">
|
|
445
|
+
<h2 className="text-2xl font-bold text-foreground">Custom Loading Template</h2>
|
|
446
|
+
<DeferredContent
|
|
447
|
+
onLoad={() => handleLoad('Custom Loading')}
|
|
448
|
+
loadingTemplate={
|
|
449
|
+
<div className="bg-card border border-border rounded-lg p-8">
|
|
450
|
+
<div className="flex items-center justify-center space-x-2">
|
|
451
|
+
<div className="w-3 h-3 bg-primary rounded-full animate-bounce"></div>
|
|
452
|
+
<div className="w-3 h-3 bg-primary rounded-full animate-bounce" style={{ animationDelay: '0.1s' }}></div>
|
|
453
|
+
<div className="w-3 h-3 bg-primary rounded-full animate-bounce" style={{ animationDelay: '0.2s' }}></div>
|
|
454
|
+
</div>
|
|
455
|
+
<p className="text-center text-muted-foreground mt-4">
|
|
456
|
+
Custom loading animation...
|
|
457
|
+
</p>
|
|
458
|
+
</div>
|
|
459
|
+
}
|
|
460
|
+
>
|
|
461
|
+
<div className="bg-card border border-border rounded-lg p-8 text-center space-y-4">
|
|
462
|
+
<h3 className="text-xl font-semibold text-foreground">
|
|
463
|
+
Content with Custom Loading
|
|
464
|
+
</h3>
|
|
465
|
+
<p className="text-muted-foreground">
|
|
466
|
+
This section used a custom loading template with bouncing dots animation.
|
|
467
|
+
</p>
|
|
468
|
+
</div>
|
|
469
|
+
</DeferredContent>
|
|
470
|
+
</section>
|
|
471
|
+
|
|
472
|
+
<section className="space-y-6">
|
|
473
|
+
<h2 className="text-2xl font-bold text-foreground">Advanced Configuration</h2>
|
|
474
|
+
<div className="grid gap-6">
|
|
475
|
+
<DeferredContent
|
|
476
|
+
threshold={0.5}
|
|
477
|
+
rootMargin="50px"
|
|
478
|
+
onLoad={() => handleLoad('Advanced Config 1')}
|
|
479
|
+
className="bg-card border border-border rounded-lg p-6"
|
|
480
|
+
>
|
|
481
|
+
<div className="space-y-2">
|
|
482
|
+
<h3 className="text-lg font-semibold text-foreground">
|
|
483
|
+
50% Visible + 50px Margin
|
|
484
|
+
</h3>
|
|
485
|
+
<p className="text-sm text-muted-foreground">
|
|
486
|
+
threshold: 0.5, rootMargin: "50px"
|
|
487
|
+
</p>
|
|
488
|
+
<p className="text-muted-foreground">
|
|
489
|
+
This content loads when 50% is visible, with 50px extra margin for early triggering.
|
|
490
|
+
</p>
|
|
491
|
+
</div>
|
|
492
|
+
</DeferredContent>
|
|
493
|
+
|
|
494
|
+
<DeferredContent
|
|
495
|
+
threshold={1.0}
|
|
496
|
+
onLoad={() => handleLoad('Advanced Config 2')}
|
|
497
|
+
className="bg-card border border-border rounded-lg p-6"
|
|
498
|
+
>
|
|
499
|
+
<div className="space-y-2">
|
|
500
|
+
<h3 className="text-lg font-semibold text-foreground">
|
|
501
|
+
Fully Visible Required
|
|
502
|
+
</h3>
|
|
503
|
+
<p className="text-sm text-muted-foreground">
|
|
504
|
+
threshold: 1.0
|
|
505
|
+
</p>
|
|
506
|
+
<p className="text-muted-foreground">
|
|
507
|
+
This content only loads when the entire element is visible in the viewport.
|
|
508
|
+
</p>
|
|
509
|
+
</div>
|
|
510
|
+
</DeferredContent>
|
|
511
|
+
</div>
|
|
512
|
+
</section>
|
|
513
|
+
|
|
514
|
+
<div className="h-32 flex items-center justify-center bg-muted/20 rounded-lg">
|
|
515
|
+
<p className="text-muted-foreground text-center">
|
|
516
|
+
End of demo - Check your console for load events!
|
|
517
|
+
</p>
|
|
518
|
+
</div>
|
|
519
|
+
</div>
|
|
520
|
+
</div>
|
|
521
|
+
);
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
function DeferredContentDemo1() {
|
|
525
|
+
return (
|
|
526
|
+
<div className="space-y-8 p-6">
|
|
527
|
+
<h1>Deferred Content Demo</h1>
|
|
528
|
+
|
|
529
|
+
{/* Basic Deferred Content */}
|
|
530
|
+
<DeferredContent
|
|
531
|
+
threshold={0.1}
|
|
532
|
+
rootMargin="100px"
|
|
533
|
+
onLoad={() => console.log('Content loaded!')}
|
|
534
|
+
loadingTemplate={<div>Custom loading...</div>}
|
|
535
|
+
>
|
|
536
|
+
<div className="p-6 bg-blue-100 rounded-lg">
|
|
537
|
+
<h2>This content loads when visible</h2>
|
|
538
|
+
<p>It uses Intersection Observer to defer loading until needed.</p>
|
|
539
|
+
</div>
|
|
540
|
+
</DeferredContent>
|
|
541
|
+
|
|
542
|
+
{/* Deferred Image */}
|
|
543
|
+
<DeferredImage
|
|
544
|
+
src="https://example.com/large-image.jpg"
|
|
545
|
+
alt="Large image"
|
|
546
|
+
className="rounded-lg shadow-lg"
|
|
547
|
+
loadingClassName="rounded-lg"
|
|
548
|
+
threshold={0.2}
|
|
549
|
+
onLoad={() => console.log('Image loaded!')}
|
|
550
|
+
/>
|
|
551
|
+
|
|
552
|
+
{/* Deferred Table */}
|
|
553
|
+
<DeferredTable
|
|
554
|
+
data={[
|
|
555
|
+
{ id: 1, name: 'John', age: 30 },
|
|
556
|
+
{ id: 2, name: 'Jane', age: 25 }
|
|
557
|
+
]}
|
|
558
|
+
columns={[
|
|
559
|
+
{ field: 'id', header: 'ID', className: 'w-20' },
|
|
560
|
+
{ field: 'name', header: 'Name' },
|
|
561
|
+
{ field: 'age', header: 'Age', className: 'w-20' }
|
|
562
|
+
]}
|
|
563
|
+
threshold={0.3}
|
|
564
|
+
onLoad={() => console.log('Table loaded!')}
|
|
565
|
+
/>
|
|
566
|
+
|
|
567
|
+
{/* Deferred Card */}
|
|
568
|
+
<DeferredCard
|
|
569
|
+
title="Product Card"
|
|
570
|
+
description="This card loads when it enters the viewport"
|
|
571
|
+
imageUrl="https://example.com/product.jpg"
|
|
572
|
+
buttonText="Buy Now"
|
|
573
|
+
secondaryButtonText="Learn More"
|
|
574
|
+
onButtonClick={() => console.log('Buy clicked')}
|
|
575
|
+
onSecondaryButtonClick={() => console.log('Learn more clicked')}
|
|
576
|
+
threshold={0.5}
|
|
577
|
+
rootMargin="200px"
|
|
578
|
+
onLoad={() => console.log('Card loaded!')}
|
|
579
|
+
/>
|
|
580
|
+
|
|
581
|
+
{/* Multiple deferred items in a grid */}
|
|
582
|
+
<div className="grid grid-cols-1 md:grid-cols-2 gap-6">
|
|
583
|
+
{[1, 2, 3, 4].map((item) => (
|
|
584
|
+
<DeferredCard
|
|
585
|
+
key={item}
|
|
586
|
+
title={`Card ${item}`}
|
|
587
|
+
description={`This is card number ${item}`}
|
|
588
|
+
threshold={0.1}
|
|
589
|
+
rootMargin="50px"
|
|
590
|
+
/>
|
|
591
|
+
))}
|
|
592
|
+
</div>
|
|
593
|
+
</div>
|
|
594
|
+
);
|
|
595
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
|
|
2
|
+
import { useState, useEffect, useCallback, useRef } from "react";
|
|
3
|
+
|
|
4
|
+
export function HoneypotMiddleware() {
|
|
5
|
+
const createHoneypot = useCallback((config: HoneypotConfig) => {
|
|
6
|
+
const timestamp = Date.now().toString();
|
|
7
|
+
const hash = btoa(`${timestamp}-${config.encryptionSeed}`);
|
|
8
|
+
|
|
9
|
+
return {
|
|
10
|
+
nameField: config.nameFieldName,
|
|
11
|
+
validFromField: config.validFromFieldName,
|
|
12
|
+
encryptedValidFrom: hash,
|
|
13
|
+
};
|
|
14
|
+
}, []);
|
|
15
|
+
|
|
16
|
+
const validateHoneypot = useCallback((
|
|
17
|
+
formData: FormData,
|
|
18
|
+
config: HoneypotConfig
|
|
19
|
+
): boolean => {
|
|
20
|
+
const nameFieldValue = formData.get(config.nameFieldName);
|
|
21
|
+
const validFromValue = formData.get(config.validFromFieldName);
|
|
22
|
+
|
|
23
|
+
if (nameFieldValue) return false;
|
|
24
|
+
|
|
25
|
+
try {
|
|
26
|
+
const decrypted = atob(validFromValue as string);
|
|
27
|
+
const [timestamp] = decrypted.split('-');
|
|
28
|
+
const submissionTime = Date.now();
|
|
29
|
+
const honeypotTime = parseInt(timestamp);
|
|
30
|
+
|
|
31
|
+
return submissionTime - honeypotTime > 1000;
|
|
32
|
+
} catch {
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
}, []);
|
|
36
|
+
|
|
37
|
+
return { createHoneypot, validateHoneypot };
|
|
38
|
+
}
|