@jamsrui/file-upload 0.0.2 → 0.0.3

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.
@@ -1 +1 @@
1
- "use client";import{a,b}from"./chunk-XVMAP2VZ.mjs";export{a as FileUploadConfig,b as useFileUploadConfig};
1
+ "use client";import{createConfigContext as o}from"@jamsrui/utils";const[l,r]=o({displayName:"FileUploadConfig"});export{l as FileUploadConfig,r as useFileUploadConfig};
@@ -1 +1 @@
1
- "use client";import{a}from"./chunk-6IBQMG7P.mjs";import"./chunk-VU6NRBJY.mjs";export{a as FileUpload};
1
+ "use client";import{jsx as o,jsxs as l}from"react/jsx-runtime";import{cn as m}from"@jamsrui/utils";import{useState as c}from"react";import{useFileUpload as g}from"./use-file-upload.mjs";const v=u=>{const[i,d]=c([]),{getInputProps:a,isDragging:t,files:p,removeFile:n}=g({onFilesAdded(e){d(r=>[...r,...e.map(s=>({fileId:s.id,progress:0,completed:!1}))])}});return l("div",{children:[o("input",{className:m("w-full h-24 border",t&&"border-dashed border-primary"),multiple:!0,...a({})}),p.length>0&&o("ul",{className:"flex gap-4 py-4",children:p.map(e=>{const r=i.find(s=>s.fileId===e.id);return l("li",{className:"flex flex-col gap-4",children:[o("img",{className:"size-20",src:e.preview,alt:e.file.name}),o("p",{children:e.file.name}),o("button",{onClick:()=>n(e.id),children:"Remove"}),r&&o("div",{})]},e.id)})})]})};export{v as FileUpload};
package/dist/index.mjs CHANGED
@@ -1 +1 @@
1
- import{a as o,b as e}from"./chunk-XVMAP2VZ.mjs";import{a as l}from"./chunk-6IBQMG7P.mjs";import{a as r}from"./chunk-VU6NRBJY.mjs";import{a as p}from"./chunk-XDA6AU4J.mjs";export{l as FileUpload,o as FileUploadConfig,p as formatBytes,r as useFileUpload,e as useFileUploadConfig};
1
+ import{FileUpload as r}from"./file-upload.mjs";import{FileUploadConfig as p,useFileUploadConfig as f}from"./file-upload-config.mjs";import{useFileUpload as t}from"./use-file-upload.mjs";import{formatBytes as m}from"./utils.mjs";export{r as FileUpload,p as FileUploadConfig,m as formatBytes,t as useFileUpload,f as useFileUploadConfig};
@@ -1 +1 @@
1
- "use client";import{a}from"./chunk-VU6NRBJY.mjs";export{a as useFileUpload};
1
+ "use client";import{useCallback as n,useMemo as O,useRef as z,useState as y}from"react";import{cn as C,mergeProps as N}from"@jamsrui/utils";const j=x=>{const{accept:H,maxFiles:d=Number.POSITIVE_INFINITY,maxSize:m=Number.POSITIVE_INFINITY,multiple:a,onFilesAdded:F,onFilesChange:s}=x,[v,u]=y(!1),[r,o]=y([]),l=z(null),h=n(e=>e instanceof File?e.type.startsWith("image/")?URL.createObjectURL(e):void 0:e.url,[]),P=n(e=>e instanceof File?`${e.name}-${Date.now()}-${Math.random().toString(36).substring(2,9)}`:e.id,[]),c=n(()=>{o(e=>{for(const t of e)t.preview&&t.file instanceof File&&URL.revokeObjectURL(t.preview);return[]}),s?.([])},[s]),p=n(e=>{if(e.length===0||(a||c(),a&&r.length+e.length>d))return;const t=[],f=Array.from(e);for(const i of f)a&&r.some(b=>b.file.name===i.name&&b.file.size===i.size)||i.size>m||t.push({file:i,id:P(i),preview:h(i)});t.length<1||(F?.(t),o(i=>{const g=a?[...i,...t]:t.slice(0,1);return s?.(g),g}),l.current&&(l.current.value=""))},[c,h,r,P,d,m,a,F,s]),D=n(e=>{if((e.target.files?.length??0)>0){const t=e.target.files;t&&p(t)}},[p]),I=n(e=>{const t=r.find(i=>i.id===e);t&&t.file instanceof File&&t.preview&&URL.revokeObjectURL(t.preview);const f=r.filter(i=>i.id!==e);s?.(f),o(f)},[r,s]),E=n(e=>{e.preventDefault(),e.stopPropagation(),u(!0)},[]),L=n(e=>{e.preventDefault(),e.stopPropagation(),!e.currentTarget.contains(e.relatedTarget)&&u(!1)},[]),R=n(e=>{e.preventDefault(),e.stopPropagation()},[]),w=n(e=>{e.preventDefault(),e.stopPropagation();const t=e.dataTransfer.files;t&&p(t),u(!1)},[p]),T=n(e=>({type:"file",multiple:a,ref:l,className:C(e.className,"sr-only"),...N(e,{onChange:D})}),[D,a]),U=n(e=>({...N(e,{onDragEnter:E,onDragLeave:L,onDrop:w,onDragOver:R})}),[E,L,w,R]),M=n(()=>{l.current&&l.current.click()},[]);return O(()=>({isDragging:v,getInputProps:T,files:r,setFiles:o,removeFile:I,getRootProps:U,openFileDialog:M,clearFiles:c}),[v,T,r,o,I,U,M,c])};export{j as useFileUpload};
package/dist/use-file.mjs CHANGED
@@ -1 +1 @@
1
- "use client";import{useCallback as r,useRef as A,useState as B}from"react";var N=(m={})=>{let{maxFiles:f=Number.POSITIVE_INFINITY,maxSize:a=Number.POSITIVE_INFINITY,accept:p="*",multiple:l=!1,initialFiles:F=[],onFilesChange:d,onFilesAdded:D}=m,[v,o]=B({errors:[],files:F.map(e=>({file:e,id:e.id,preview:e.url})),isDragging:!1}),u=A(null),L=r(e=>{if(e instanceof File){if(e.size>a)return`File "${e.name}" exceeds the maximum size of ${h(a)}.`}else if(e.size>a)return`File "${e.name}" exceeds the maximum size of ${h(a)}.`;if(p!=="*"){let i=p.split(",").map(s=>s.trim()),n=e instanceof File?e.type||"":e.type,c=`.${e instanceof File,e.name.split(".").pop()}`;if(!i.some(s=>{if(s.startsWith("."))return c.toLowerCase()===s.toLowerCase();if(s.endsWith("/*")){let x=s.split("/")[0];return n.startsWith(`${x}/`)}return n===s}))return`File "${e instanceof File,e.name}" is not an accepted file type.`}return null},[p,a]),M=r(e=>e instanceof File?URL.createObjectURL(e):e.url,[]),I=r(e=>e instanceof File?`${e.name}-${Date.now()}-${Math.random().toString(36).substring(2,9)}`:e.id,[]),E=r(()=>{o(e=>{for(let n of e.files??[])n.preview&&n.file instanceof File&&n.file.type.startsWith("image/")&&URL.revokeObjectURL(n.preview);u.current&&(u.current.value="");let i={...e,errors:[],files:[]};return d?.(i.files),i})},[d]),g=r(e=>{if(!e||e.length===0)return;let i=Array.from(e),n=[];if(o(t=>({...t,errors:[]})),l||E(),l&&f!==Number.POSITIVE_INFINITY&&v.files.length+i.length>f){n.push(`You can only upload a maximum of ${f} files.`),o(t=>({...t,errors:n}));return}let c=[];for(let t of i){if(l&&v.files.some(b=>b.file.name===t.name&&b.file.size===t.size))continue;if(t.size>a){n.push(l?`Some files exceed the maximum size of ${h(a)}.`:`File exceeds the maximum size of ${h(a)}.`);continue}let s=L(t);if(s){n.push(s);continue}c.push({file:t,id:I(t),preview:M(t)})}c.length>0?(D?.(c),o(t=>{let s=l?[...t.files,...c]:c;return d?.(s),{...t,errors:n,files:s}})):n.length>0&&o(t=>({...t,errors:n})),u.current&&(u.current.value="")},[v.files,f,l,a,L,M,I,E,d,D]),w=r(e=>{o(i=>{let n=i.files.find(t=>t.id===e);n?.preview&&n.file instanceof File&&n.file.type.startsWith("image/")&&URL.revokeObjectURL(n.preview);let c=i.files.filter(t=>t.id!==e);return d?.(c),{...i,errors:[],files:c}})},[d]),y=r(()=>{o(e=>({...e,errors:[]}))},[]),H=r(e=>{e.preventDefault(),e.stopPropagation(),o(i=>({...i,isDragging:!0}))},[]),P=r(e=>{e.preventDefault(),e.stopPropagation(),!e.currentTarget.contains(e.relatedTarget)&&o(i=>({...i,isDragging:!1}))},[]),U=r(e=>{e.preventDefault(),e.stopPropagation()},[]),z=r(e=>{if(e.preventDefault(),e.stopPropagation(),o(i=>({...i,isDragging:!1})),!u.current?.disabled&&e.dataTransfer.files&&e.dataTransfer.files.length>0)if(l)g(e.dataTransfer.files);else{let i=e.dataTransfer.files[0];i&&g([i])}},[g,l]),T=r(e=>{e.target.files&&e.target.files.length>0&&g(e.target.files)},[g]),S=r(()=>{u.current&&u.current.click()},[]),$=r((e={})=>({...e,accept:e.accept||p,multiple:e.multiple!==void 0?e.multiple:l,onChange:T,ref:u,type:"file"}),[p,l,T]);return[v,{addFiles:g,clearErrors:y,clearFiles:E,getInputProps:$,handleDragEnter:H,handleDragLeave:P,handleDragOver:U,handleDrop:z,handleFileChange:T,openFileDialog:S,removeFile:w}]},h=(m,f=2)=>{if(m===0)return"0 Bytes";let a=1024,p=f<0?0:f,l=["Bytes","KB","MB","GB","TB","PB","EB","ZB","YB"],F=Math.floor(Math.log(m)/Math.log(a));return Number.parseFloat((m/a**F).toFixed(p))+l[F]};export{h as formatBytes,N as useFileUpload};
1
+ "use client";import{useCallback as r,useRef as A,useState as B}from"react";const N=(m={})=>{const{maxFiles:f=Number.POSITIVE_INFINITY,maxSize:a=Number.POSITIVE_INFINITY,accept:p="*",multiple:l=!1,initialFiles:F=[],onFilesChange:d,onFilesAdded:D}=m,[v,o]=B({errors:[],files:F.map(e=>({file:e,id:e.id,preview:e.url})),isDragging:!1}),u=A(null),L=r(e=>{if(e instanceof File){if(e.size>a)return`File "${e.name}" exceeds the maximum size of ${h(a)}.`}else if(e.size>a)return`File "${e.name}" exceeds the maximum size of ${h(a)}.`;if(p!=="*"){const i=p.split(",").map(s=>s.trim()),n=e instanceof File?e.type||"":e.type,c=`.${e instanceof File,e.name.split(".").pop()}`;if(!i.some(s=>{if(s.startsWith("."))return c.toLowerCase()===s.toLowerCase();if(s.endsWith("/*")){const x=s.split("/")[0];return n.startsWith(`${x}/`)}return n===s}))return`File "${e instanceof File,e.name}" is not an accepted file type.`}return null},[p,a]),M=r(e=>e instanceof File?URL.createObjectURL(e):e.url,[]),I=r(e=>e instanceof File?`${e.name}-${Date.now()}-${Math.random().toString(36).substring(2,9)}`:e.id,[]),E=r(()=>{o(e=>{for(const n of e.files??[])n.preview&&n.file instanceof File&&n.file.type.startsWith("image/")&&URL.revokeObjectURL(n.preview);u.current&&(u.current.value="");const i={...e,errors:[],files:[]};return d?.(i.files),i})},[d]),g=r(e=>{if(!e||e.length===0)return;const i=Array.from(e),n=[];if(o(t=>({...t,errors:[]})),l||E(),l&&f!==Number.POSITIVE_INFINITY&&v.files.length+i.length>f){n.push(`You can only upload a maximum of ${f} files.`),o(t=>({...t,errors:n}));return}const c=[];for(const t of i){if(l&&v.files.some(b=>b.file.name===t.name&&b.file.size===t.size))continue;if(t.size>a){n.push(l?`Some files exceed the maximum size of ${h(a)}.`:`File exceeds the maximum size of ${h(a)}.`);continue}const s=L(t);if(s){n.push(s);continue}c.push({file:t,id:I(t),preview:M(t)})}c.length>0?(D?.(c),o(t=>{const s=l?[...t.files,...c]:c;return d?.(s),{...t,errors:n,files:s}})):n.length>0&&o(t=>({...t,errors:n})),u.current&&(u.current.value="")},[v.files,f,l,a,L,M,I,E,d,D]),w=r(e=>{o(i=>{const n=i.files.find(t=>t.id===e);n?.preview&&n.file instanceof File&&n.file.type.startsWith("image/")&&URL.revokeObjectURL(n.preview);const c=i.files.filter(t=>t.id!==e);return d?.(c),{...i,errors:[],files:c}})},[d]),y=r(()=>{o(e=>({...e,errors:[]}))},[]),H=r(e=>{e.preventDefault(),e.stopPropagation(),o(i=>({...i,isDragging:!0}))},[]),P=r(e=>{e.preventDefault(),e.stopPropagation(),!e.currentTarget.contains(e.relatedTarget)&&o(i=>({...i,isDragging:!1}))},[]),U=r(e=>{e.preventDefault(),e.stopPropagation()},[]),z=r(e=>{if(e.preventDefault(),e.stopPropagation(),o(i=>({...i,isDragging:!1})),!u.current?.disabled&&e.dataTransfer.files&&e.dataTransfer.files.length>0)if(l)g(e.dataTransfer.files);else{const i=e.dataTransfer.files[0];i&&g([i])}},[g,l]),T=r(e=>{e.target.files&&e.target.files.length>0&&g(e.target.files)},[g]),S=r(()=>{u.current&&u.current.click()},[]),$=r((e={})=>({...e,accept:e.accept||p,multiple:e.multiple!==void 0?e.multiple:l,onChange:T,ref:u,type:"file"}),[p,l,T]);return[v,{addFiles:g,clearErrors:y,clearFiles:E,getInputProps:$,handleDragEnter:H,handleDragLeave:P,handleDragOver:U,handleDrop:z,handleFileChange:T,openFileDialog:S,removeFile:w}]},h=(m,f=2)=>{if(m===0)return"0 Bytes";const a=1024,p=f<0?0:f,l=["Bytes","KB","MB","GB","TB","PB","EB","ZB","YB"],F=Math.floor(Math.log(m)/Math.log(a));return Number.parseFloat((m/a**F).toFixed(p))+l[F]};export{h as formatBytes,N as useFileUpload};
package/dist/utils.mjs CHANGED
@@ -1 +1 @@
1
- import{a}from"./chunk-XDA6AU4J.mjs";export{a as formatBytes};
1
+ const e=(t,o=2)=>{if(t===0)return"0 Bytes";const r=1024,B=o<0?0:o,n=["Bytes","KB","MB","GB","TB","PB","EB","ZB","YB"],s=Math.floor(Math.log(t)/Math.log(r));return Number.parseFloat((t/r**s).toFixed(B))+" "+n[s]};export{e as formatBytes};
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@jamsrui/file-upload",
3
- "version": "0.0.2",
3
+ "version": "0.0.3",
4
4
  "peerDependencies": {
5
5
  "react": ">=19"
6
6
  },
7
7
  "dependencies": {
8
- "@jamsrui/hooks": "^0.0.13",
9
- "@jamsrui/utils": "^0.0.13",
10
- "@jamsrui/core": "^0.0.11"
8
+ "@jamsrui/core": "^0.0.12",
9
+ "@jamsrui/hooks": "^0.0.14",
10
+ "@jamsrui/utils": "^0.0.14"
11
11
  },
12
12
  "exports": {
13
13
  ".": {
@@ -1 +0,0 @@
1
- import{a as l}from"./chunk-VU6NRBJY.mjs";import{cn as c}from"@jamsrui/utils";import{useState as g}from"react";import{jsx as o,jsxs as i}from"react/jsx-runtime";var v=u=>{let[d,a]=g([]),{getInputProps:t,isDragging:n,files:p,removeFile:m}=l({onFilesAdded(e){a(r=>[...r,...e.map(s=>({fileId:s.id,progress:0,completed:!1}))])}});return i("div",{children:[o("input",{className:c("w-full h-24 border",n&&"border-dashed border-primary"),multiple:!0,...t({})}),p.length>0&&o("ul",{className:"flex gap-4 py-4",children:p.map(e=>{let r=d.find(s=>s.fileId===e.id);return i("li",{className:"flex flex-col gap-4",children:[o("img",{className:"size-20",src:e.preview,alt:e.file.name}),o("p",{children:e.file.name}),o("button",{onClick:()=>m(e.id),children:"Remove"}),r&&o("div",{})]},e.id)})})]})};export{v as a};
@@ -1 +0,0 @@
1
- import{useCallback as n,useMemo as O,useRef as z,useState as y}from"react";import{cn as C,mergeProps as N}from"@jamsrui/utils";var j=x=>{let{accept:H,maxFiles:d=Number.POSITIVE_INFINITY,maxSize:m=Number.POSITIVE_INFINITY,multiple:a,onFilesAdded:F,onFilesChange:s}=x,[v,u]=y(!1),[r,o]=y([]),l=z(null),h=n(e=>e instanceof File?e.type.startsWith("image/")?URL.createObjectURL(e):void 0:e.url,[]),P=n(e=>e instanceof File?`${e.name}-${Date.now()}-${Math.random().toString(36).substring(2,9)}`:e.id,[]),c=n(()=>{o(e=>{for(let t of e)t.preview&&t.file instanceof File&&URL.revokeObjectURL(t.preview);return[]}),s?.([])},[s]),p=n(e=>{if(e.length===0||(a||c(),a&&r.length+e.length>d))return;let t=[],f=Array.from(e);for(let i of f)a&&r.some(b=>b.file.name===i.name&&b.file.size===i.size)||i.size>m||t.push({file:i,id:P(i),preview:h(i)});t.length<1||(F?.(t),o(i=>{let g=a?[...i,...t]:t.slice(0,1);return s?.(g),g}),l.current&&(l.current.value=""))},[c,h,r,P,d,m,a,F,s]),D=n(e=>{if((e.target.files?.length??0)>0){let t=e.target.files;t&&p(t)}},[p]),I=n(e=>{let t=r.find(i=>i.id===e);t&&t.file instanceof File&&t.preview&&URL.revokeObjectURL(t.preview);let f=r.filter(i=>i.id!==e);s?.(f),o(f)},[r,s]),E=n(e=>{e.preventDefault(),e.stopPropagation(),u(!0)},[]),L=n(e=>{e.preventDefault(),e.stopPropagation(),!e.currentTarget.contains(e.relatedTarget)&&u(!1)},[]),R=n(e=>{e.preventDefault(),e.stopPropagation()},[]),w=n(e=>{e.preventDefault(),e.stopPropagation();let t=e.dataTransfer.files;t&&p(t),u(!1)},[p]),T=n(e=>({type:"file",multiple:a,ref:l,className:C(e.className,"sr-only"),...N(e,{onChange:D})}),[D,a]),U=n(e=>({...N(e,{onDragEnter:E,onDragLeave:L,onDrop:w,onDragOver:R})}),[E,L,w,R]),M=n(()=>{l.current&&l.current.click()},[]);return O(()=>({isDragging:v,getInputProps:T,files:r,setFiles:o,removeFile:I,getRootProps:U,openFileDialog:M,clearFiles:c}),[v,T,r,o,I,U,M,c])};export{j as a};
@@ -1 +0,0 @@
1
- var e=(t,o=2)=>{if(t===0)return"0 Bytes";let r=1024,B=o<0?0:o,n=["Bytes","KB","MB","GB","TB","PB","EB","ZB","YB"],s=Math.floor(Math.log(t)/Math.log(r));return Number.parseFloat((t/r**s).toFixed(B))+" "+n[s]};export{e as a};
@@ -1 +0,0 @@
1
- import{createConfigContext as o}from"@jamsrui/utils";var[i,p]=o({displayName:"FileUploadConfig"});export{i as a,p as b};