@betterinternship/components 3.1.0 → 3.1.2
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/dist/components/file-upload.d.ts +13 -0
- package/dist/components/file-upload.js +70 -12
- package/dist/components/file-upload.js.map +1 -1
- package/dist/components/page-header.d.ts +7 -1
- package/dist/components/page-header.js +2 -2
- package/dist/components/page-header.js.map +1 -1
- package/package.json +1 -1
- package/theme.css +5 -0
|
@@ -1,16 +1,29 @@
|
|
|
1
1
|
import * as React from "react";
|
|
2
2
|
export type FileUploadVariant = "compact" | "dropzone";
|
|
3
|
+
export type FileRejectReason = "type" | "size";
|
|
3
4
|
export type FileUploadProps = {
|
|
4
5
|
label: string;
|
|
5
6
|
name: string;
|
|
6
7
|
variant?: FileUploadVariant;
|
|
8
|
+
/** Same format as the input's `accept`: extensions (".pdf") and/or MIME types ("image/*"). Also checked on drop. */
|
|
7
9
|
accept?: string;
|
|
10
|
+
/** Files larger than this are rejected through `onReject` instead of being selected. */
|
|
11
|
+
maxSizeMB?: number;
|
|
8
12
|
multiple?: boolean;
|
|
9
13
|
required?: boolean;
|
|
10
14
|
placeholder?: string;
|
|
11
15
|
disabled?: boolean;
|
|
12
16
|
className?: string;
|
|
17
|
+
/**
|
|
18
|
+
* The selected file, when the parent owns it. Pass `null` to clear the field.
|
|
19
|
+
* Leave undefined to let the field remember the last pick itself.
|
|
20
|
+
*/
|
|
21
|
+
value?: File | null;
|
|
22
|
+
/** Shown under the field and marks it invalid. */
|
|
23
|
+
error?: string | null;
|
|
13
24
|
onFileSelect?: (file: File | null) => void;
|
|
14
25
|
onFilesSelect?: (files: File[]) => void;
|
|
26
|
+
/** Called with each file that fails `accept` or `maxSizeMB`. */
|
|
27
|
+
onReject?: (file: File, reason: FileRejectReason) => void;
|
|
15
28
|
};
|
|
16
29
|
export declare const FileUpload: React.ForwardRefExoticComponent<FileUploadProps & React.RefAttributes<HTMLInputElement>>;
|
|
@@ -5,23 +5,81 @@ import { Upload } from 'lucide-react';
|
|
|
5
5
|
import { cn } from '../utils.js';
|
|
6
6
|
import { FieldLabel } from './label.js';
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
/** True when the file matches one of the `accept` entries (or there are none). */
|
|
9
|
+
function matchesAccept(file, accept) {
|
|
10
|
+
if (!accept)
|
|
11
|
+
return true;
|
|
12
|
+
const name = file.name.toLowerCase();
|
|
13
|
+
const type = file.type.toLowerCase();
|
|
14
|
+
return accept
|
|
15
|
+
.split(",")
|
|
16
|
+
.map((entry) => entry.trim().toLowerCase())
|
|
17
|
+
.filter(Boolean)
|
|
18
|
+
.some((entry) => entry.startsWith(".")
|
|
19
|
+
? name.endsWith(entry)
|
|
20
|
+
: entry.endsWith("/*")
|
|
21
|
+
? type.startsWith(entry.slice(0, -1))
|
|
22
|
+
: type === entry);
|
|
23
|
+
}
|
|
24
|
+
const FileUpload = React.forwardRef(({ label, name, variant = "compact", accept, maxSizeMB, multiple, required, placeholder, disabled, className, value, error, onFileSelect, onFilesSelect, onReject, }, ref) => {
|
|
9
25
|
const inputRef = React.useRef(null);
|
|
10
|
-
const [
|
|
26
|
+
const [pickedName, setPickedName] = React.useState(null);
|
|
11
27
|
const [dragging, setDragging] = React.useState(false);
|
|
28
|
+
const errorId = `${name}-error`;
|
|
29
|
+
const controlled = value !== undefined;
|
|
30
|
+
const fileName = controlled ? (value?.name ?? null) : pickedName;
|
|
12
31
|
const selectFiles = (files) => {
|
|
13
|
-
const
|
|
14
|
-
|
|
32
|
+
const accepted = files.filter((file) => {
|
|
33
|
+
if (!matchesAccept(file, accept)) {
|
|
34
|
+
onReject?.(file, "type");
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
if (maxSizeMB !== undefined && file.size > maxSizeMB * 1024 * 1024) {
|
|
38
|
+
onReject?.(file, "size");
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
return true;
|
|
42
|
+
});
|
|
43
|
+
// Every file was rejected: keep the current selection as it was.
|
|
44
|
+
if (files.length > 0 && accepted.length === 0)
|
|
45
|
+
return;
|
|
46
|
+
const file = accepted[0] ?? null;
|
|
47
|
+
setPickedName(accepted.length > 1
|
|
48
|
+
? `${accepted.length} files selected`
|
|
49
|
+
: (file?.name ?? null));
|
|
15
50
|
onFileSelect?.(file);
|
|
16
|
-
onFilesSelect?.(
|
|
51
|
+
onFilesSelect?.(accepted);
|
|
17
52
|
};
|
|
18
|
-
const openPicker = () => {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
53
|
+
const openPicker = () => {
|
|
54
|
+
if (!disabled)
|
|
55
|
+
inputRef.current?.click();
|
|
56
|
+
};
|
|
57
|
+
return (jsxs("div", { className: cn("grid gap-field-label-gap", className), children: [jsxs(FieldLabel, { htmlFor: name, children: [label, required && jsx("span", { className: "text-destructive", children: " *" })] }), jsxs("button", { type: "button", disabled: disabled, onClick: openPicker, "aria-invalid": error ? true : undefined, "aria-describedby": error ? errorId : undefined, onDragEnter: (event) => {
|
|
58
|
+
event.preventDefault();
|
|
59
|
+
setDragging(true);
|
|
60
|
+
}, onDragOver: (event) => {
|
|
61
|
+
event.preventDefault();
|
|
62
|
+
if (!disabled)
|
|
63
|
+
setDragging(true);
|
|
64
|
+
}, onDragLeave: () => setDragging(false), onDrop: (event) => {
|
|
65
|
+
event.preventDefault();
|
|
66
|
+
setDragging(false);
|
|
67
|
+
if (disabled)
|
|
68
|
+
return;
|
|
69
|
+
selectFiles(Array.from(event.dataTransfer.files).slice(0, multiple ? undefined : 1));
|
|
70
|
+
}, className: cn("flex w-full cursor-pointer items-center gap-2 border border-input text-left text-sm text-muted-foreground transition-colors hover:border-input-hover hover:bg-accent focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-ring motion-reduce:transition-none disabled:cursor-not-allowed disabled:opacity-60", variant === "dropzone"
|
|
71
|
+
? "min-h-36 flex-col justify-center rounded-card border-solid border-border px-6 py-6 text-center"
|
|
72
|
+
: "min-h-11 rounded-control-lg px-4 py-2", dragging && "border-primary bg-primary-tint", error && "border-destructive"), children: [jsx(Upload, { className: "size-5 shrink-0 text-primary" }), jsx("span", { className: cn("min-w-0 truncate", fileName && "text-foreground"), children: fileName ?? placeholder ?? "Drop a file here or click to upload" })] }), jsx("input", { ref: (node) => {
|
|
73
|
+
inputRef.current = node;
|
|
74
|
+
if (typeof ref === "function")
|
|
75
|
+
ref(node);
|
|
76
|
+
else if (ref)
|
|
77
|
+
ref.current = node;
|
|
78
|
+
}, id: name, type: "file", name: name, accept: accept, multiple: multiple, required: required, disabled: disabled, onChange: (event) => {
|
|
79
|
+
selectFiles(Array.from(event.target.files ?? []));
|
|
80
|
+
// Let the same file be picked again, e.g. after a rejection.
|
|
81
|
+
event.target.value = "";
|
|
82
|
+
}, className: "sr-only" }), error && (jsx("p", { id: errorId, role: "alert", className: "m-0 text-xs text-destructive", children: error }))] }));
|
|
25
83
|
});
|
|
26
84
|
FileUpload.displayName = "FileUpload";
|
|
27
85
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"file-upload.js","sources":["../../src/components/file-upload.tsx"],"sourcesContent":["\"use client\";\n\nimport * as React from \"react\";\nimport { Upload } from \"lucide-react\";\nimport { cn } from \"@/utils\";\nimport { FieldLabel } from \"./label\";\n\nexport type FileUploadVariant = \"compact\" | \"dropzone\";\nexport type FileUploadProps = {\n label: string;\n name: string;\n variant?: FileUploadVariant;\n accept?: string;\n multiple?: boolean;\n required?: boolean;\n placeholder?: string;\n disabled?: boolean;\n className?: string;\n onFileSelect?: (file: File | null) => void;\n onFilesSelect?: (files: File[]) => void;\n};\n\nexport const FileUpload = React.forwardRef<HTMLInputElement, FileUploadProps>(\n ({
|
|
1
|
+
{"version":3,"file":"file-upload.js","sources":["../../src/components/file-upload.tsx"],"sourcesContent":["\"use client\";\n\nimport * as React from \"react\";\nimport { Upload } from \"lucide-react\";\nimport { cn } from \"@/utils\";\nimport { FieldLabel } from \"./label\";\n\nexport type FileUploadVariant = \"compact\" | \"dropzone\";\nexport type FileRejectReason = \"type\" | \"size\";\nexport type FileUploadProps = {\n label: string;\n name: string;\n variant?: FileUploadVariant;\n /** Same format as the input's `accept`: extensions (\".pdf\") and/or MIME types (\"image/*\"). Also checked on drop. */\n accept?: string;\n /** Files larger than this are rejected through `onReject` instead of being selected. */\n maxSizeMB?: number;\n multiple?: boolean;\n required?: boolean;\n placeholder?: string;\n disabled?: boolean;\n className?: string;\n /**\n * The selected file, when the parent owns it. Pass `null` to clear the field.\n * Leave undefined to let the field remember the last pick itself.\n */\n value?: File | null;\n /** Shown under the field and marks it invalid. */\n error?: string | null;\n onFileSelect?: (file: File | null) => void;\n onFilesSelect?: (files: File[]) => void;\n /** Called with each file that fails `accept` or `maxSizeMB`. */\n onReject?: (file: File, reason: FileRejectReason) => void;\n};\n\n/** True when the file matches one of the `accept` entries (or there are none). */\nfunction matchesAccept(file: File, accept?: string) {\n if (!accept) return true;\n const name = file.name.toLowerCase();\n const type = file.type.toLowerCase();\n return accept\n .split(\",\")\n .map((entry) => entry.trim().toLowerCase())\n .filter(Boolean)\n .some((entry) =>\n entry.startsWith(\".\")\n ? name.endsWith(entry)\n : entry.endsWith(\"/*\")\n ? type.startsWith(entry.slice(0, -1))\n : type === entry,\n );\n}\n\nexport const FileUpload = React.forwardRef<HTMLInputElement, FileUploadProps>(\n (\n {\n label,\n name,\n variant = \"compact\",\n accept,\n maxSizeMB,\n multiple,\n required,\n placeholder,\n disabled,\n className,\n value,\n error,\n onFileSelect,\n onFilesSelect,\n onReject,\n },\n ref,\n ) => {\n const inputRef = React.useRef<HTMLInputElement>(null);\n const [pickedName, setPickedName] = React.useState<string | null>(null);\n const [dragging, setDragging] = React.useState(false);\n const errorId = `${name}-error`;\n const controlled = value !== undefined;\n const fileName = controlled ? (value?.name ?? null) : pickedName;\n\n const selectFiles = (files: File[]) => {\n const accepted = files.filter((file) => {\n if (!matchesAccept(file, accept)) {\n onReject?.(file, \"type\");\n return false;\n }\n if (maxSizeMB !== undefined && file.size > maxSizeMB * 1024 * 1024) {\n onReject?.(file, \"size\");\n return false;\n }\n return true;\n });\n // Every file was rejected: keep the current selection as it was.\n if (files.length > 0 && accepted.length === 0) return;\n\n const file = accepted[0] ?? null;\n setPickedName(\n accepted.length > 1\n ? `${accepted.length} files selected`\n : (file?.name ?? null),\n );\n onFileSelect?.(file);\n onFilesSelect?.(accepted);\n };\n const openPicker = () => {\n if (!disabled) inputRef.current?.click();\n };\n\n return (\n <div className={cn(\"grid gap-field-label-gap\", className)}>\n <FieldLabel htmlFor={name}>\n {label}\n {required && <span className=\"text-destructive\"> *</span>}\n </FieldLabel>\n <button\n type=\"button\"\n disabled={disabled}\n onClick={openPicker}\n aria-invalid={error ? true : undefined}\n aria-describedby={error ? errorId : undefined}\n onDragEnter={(event) => {\n event.preventDefault();\n setDragging(true);\n }}\n onDragOver={(event) => {\n event.preventDefault();\n if (!disabled) setDragging(true);\n }}\n onDragLeave={() => setDragging(false)}\n onDrop={(event) => {\n event.preventDefault();\n setDragging(false);\n if (disabled) return;\n selectFiles(\n Array.from(event.dataTransfer.files).slice(\n 0,\n multiple ? undefined : 1,\n ),\n );\n }}\n className={cn(\n \"flex w-full cursor-pointer items-center gap-2 border border-input text-left text-sm text-muted-foreground transition-colors hover:border-input-hover hover:bg-accent focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-ring motion-reduce:transition-none disabled:cursor-not-allowed disabled:opacity-60\",\n variant === \"dropzone\"\n ? \"min-h-36 flex-col justify-center rounded-card border-solid border-border px-6 py-6 text-center\"\n : \"min-h-11 rounded-control-lg px-4 py-2\",\n dragging && \"border-primary bg-primary-tint\",\n error && \"border-destructive\",\n )}\n >\n <Upload className=\"size-5 shrink-0 text-primary\" />\n <span className={cn(\"min-w-0 truncate\", fileName && \"text-foreground\")}>\n {fileName ?? placeholder ?? \"Drop a file here or click to upload\"}\n </span>\n </button>\n <input\n ref={(node) => {\n inputRef.current = node;\n if (typeof ref === \"function\") ref(node);\n else if (ref) ref.current = node;\n }}\n id={name}\n type=\"file\"\n name={name}\n accept={accept}\n multiple={multiple}\n required={required}\n disabled={disabled}\n onChange={(event) => {\n selectFiles(Array.from(event.target.files ?? []));\n // Let the same file be picked again, e.g. after a rejection.\n event.target.value = \"\";\n }}\n className=\"sr-only\"\n />\n {error && (\n <p id={errorId} role=\"alert\" className=\"m-0 text-xs text-destructive\">\n {error}\n </p>\n )}\n </div>\n );\n },\n);\nFileUpload.displayName = \"FileUpload\";\n"],"names":[],"mappings":";;;;;;;AAmCA;AACA;AACE;AAAa;;;AAGb;;AAEG;;;AAIG;AACA;AACE;AACA;AAEV;;;AAwBI;AACA;AACA;AACA;AACA;AAEA;;;AAGM;AACA;;AAEF;AACE;AACA;;AAEF;AACF;;;;;AAKA;AAEI;;AAGJ;AACA;AACF;;AAEE;AAAe;AACjB;AAEA;;;AAeM;;AAGE;;AACF;;;AAKE;;;;AAWE;;AAaF;;;AAEK;AAAS;AAChB;AASE;;AAEA;;AAWV;AAEF;;"}
|
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
import * as React from 'react';
|
|
2
|
-
export declare function PageHeader({ title, description, className, }: {
|
|
2
|
+
export declare function PageHeader({ title, description, animated, className, }: {
|
|
3
3
|
title: string;
|
|
4
4
|
description?: React.ReactNode;
|
|
5
|
+
/**
|
|
6
|
+
* Fades the header up into place when it mounts (skipped under reduced
|
|
7
|
+
* motion). Give the header a new `key` to play it again, e.g. on tab change.
|
|
8
|
+
* Leave it off in loading skeletons so the effect plays once, on the page.
|
|
9
|
+
*/
|
|
10
|
+
animated?: boolean;
|
|
5
11
|
className?: string;
|
|
6
12
|
}): import("react/jsx-runtime").JSX.Element;
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { jsxs, jsx } from 'react/jsx-runtime';
|
|
2
2
|
import { cn } from '../utils.js';
|
|
3
3
|
|
|
4
|
-
function PageHeader({ title, description, className, }) {
|
|
5
|
-
return (jsxs("header", { className: cn('text-foreground', className), children: [jsx("h1", { className: "text-page-heading font-page-heading", children: title }), description && (jsx("p", { className: "mt-page-description-gap max-w-page-description text-muted-foreground text-base leading-page-description", children: description }))] }));
|
|
4
|
+
function PageHeader({ title, description, animated = false, className, }) {
|
|
5
|
+
return (jsxs("header", { className: cn('text-foreground', animated && 'motion-safe:animate-page-enter', className), children: [jsx("h1", { className: "text-page-heading font-page-heading", children: title }), description && (jsx("p", { className: "mt-page-description-gap max-w-page-description text-muted-foreground text-base leading-page-description", children: description }))] }));
|
|
6
6
|
}
|
|
7
7
|
|
|
8
8
|
export { PageHeader };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"page-header.js","sources":["../../src/components/page-header.tsx"],"sourcesContent":["import * as React from 'react';\nimport { cn } from '@/utils';\n\n\nexport function PageHeader({\n title,\n description,\n className,\n}: {\n title: string;\n description?: React.ReactNode;\n className?: string;\n}) {\n return (\n <header
|
|
1
|
+
{"version":3,"file":"page-header.js","sources":["../../src/components/page-header.tsx"],"sourcesContent":["import * as React from 'react';\nimport { cn } from '@/utils';\n\n\nexport function PageHeader({\n title,\n description,\n animated = false,\n className,\n}: {\n title: string;\n description?: React.ReactNode;\n /**\n * Fades the header up into place when it mounts (skipped under reduced\n * motion). Give the header a new `key` to play it again, e.g. on tab change.\n * Leave it off in loading skeletons so the effect plays once, on the page.\n */\n animated?: boolean;\n className?: string;\n}) {\n return (\n <header\n className={cn(\n 'text-foreground',\n animated && 'motion-safe:animate-page-enter',\n className,\n )}\n >\n <h1 className=\"text-page-heading font-page-heading\">\n {title}\n </h1>\n {description && (\n <p className=\"mt-page-description-gap max-w-page-description text-muted-foreground text-base leading-page-description\">{description}</p>\n )}\n </header>\n );\n}\n"],"names":["_jsxs","_jsx"],"mappings":";;;AAIM,SAAU,UAAU,CAAC,EACzB,KAAK,EACL,WAAW,EACX,QAAQ,GAAG,KAAK,EAChB,SAAS,GAWV,EAAA;AACC,IAAA,QACEA,IAAA,CAAA,QAAA,EAAA,EACE,SAAS,EAAE,EAAE,CACX,iBAAiB,EACjB,QAAQ,IAAI,gCAAgC,EAC5C,SAAS,CACV,EAAA,QAAA,EAAA,CAECC,YAAI,SAAS,EAAC,qCAAqC,EAAA,QAAA,EAChD,KAAK,EAAA,CACH,EACJ,WAAW,KACVA,GAAA,CAAA,GAAA,EAAA,EAAG,SAAS,EAAC,yGAAyG,YAAE,WAAW,EAAA,CAAK,CACzI,CAAA,EAAA,CACI;AAEb;;;;"}
|
package/package.json
CHANGED
package/theme.css
CHANGED
|
@@ -159,6 +159,7 @@
|
|
|
159
159
|
--animate-accordion-down: accordion-down 0.2s ease-out;
|
|
160
160
|
--animate-accordion-up: accordion-up 0.2s ease-out;
|
|
161
161
|
--animate-error-bob: error-bob 2.8s ease-in-out infinite;
|
|
162
|
+
--animate-page-enter: page-enter 0.2s ease-out both;
|
|
162
163
|
|
|
163
164
|
--font-sans:
|
|
164
165
|
Inter, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto,
|
|
@@ -240,6 +241,10 @@
|
|
|
240
241
|
0%, 100% { transform: translateY(0); }
|
|
241
242
|
50% { transform: translateY(-6px); }
|
|
242
243
|
}
|
|
244
|
+
/* Page title entrance: a short fade up when a page opens. */
|
|
245
|
+
@keyframes page-enter {
|
|
246
|
+
from { opacity: 0; transform: translateY(6px); }
|
|
247
|
+
}
|
|
243
248
|
}
|
|
244
249
|
|
|
245
250
|
.dark {
|