shadcn.io is not affiliated with official shadcn/ui
Logo File Input Form Block for React
A single-file field with rules read from the file itself: a square PNG or JPG, 1 MB or less, at least 256 by 256 pixels, shown as one row with its size, type and pixels, or refused with a named reason.
Installation
Overview
Check a logo before it is saved, with this file input form for React with Base UI. A compact header sits over a hairline frame with a plus mark on each corner, in two cells on one hairline: a File Upload drop area with a Browse files button, and a cell holding the chosen file as one row with its name, size (Format Byte), type and pixel size, and a Remove button. The logo must be a PNG or JPG, 1 MB or less, at least 256 by 256 pixels and square. The type and size come from the file's own properties; the pixels come from decoding the image in the tab with createImageBitmap, and the file's bytes are never shown or sent. A file that fails is named in its own slot ("brand.svg is not PNG or JPG", "logo.png is 640 × 480: it must be square") and the current logo stays. Save logo, the only Shine Button, says what is missing or that nothing changed. It's built from File Upload, Format Byte, Guides and Button. For logos, avatars, brand assets and any one file with rules.
FAQ
How are the rules checked, and where do I change them?
Edit LIMIT (bytes), MIN_SIDE (pixels) and TYPES at the top. quickProblem checks the type and size from the file's own properties, so a wrong file is refused before anything is decoded; shapeProblem checks the pixels once they are known. Both are pure functions that return the sentence to show, so the same text serves the drop, the picker and your tests. The file picker is filtered to PNG and JPG as well, but a picker filter can always be bypassed, so the checks run on every file.
How does it know the pixel size, and does it read the file?
In a small helper it calls createImageBitmap(file), which decodes the image in the tab, reads its width and height and releases it. Nothing is drawn, nothing is uploaded and no object URL is created. A file that claims to be a PNG but will not decode ("logo.png could not be read as an image") is refused the same way. Your server must still check the real type and size of whatever it receives.
What happens when a wrong file is chosen after a good one?
The good logo stays and a named reason appears in the slot under it, so a slip of the hand never loses the current logo. Dropping two files at once keeps neither and says to choose one. The slot is always there, two lines high, so the panel does not move.
Is it usable without a mouse?
Yes. Browse files is a real button: Enter or Space opens the picker, and its label changes to Replace file once there is a logo. The drop area is for dragging only; the whole flow works from the button. Each result is spoken once by the foot's polite status, and Remove is a button named for the file ("Remove logo.png").
What stays local, and how do I connect it for real?
Everything: the chosen file is held by the browser, never read as bytes by the page, and Save logo says "The file stayed on this device". In your app, upload the File from the submit handler (a signed URL or a multipart request), show progress in the same row, and validate the upload again on the server: size, type and a real decode.
How do I connect it to react-hook-form or TanStack Form?
Keep the checked file as the form's value and use the rules as the validator. With react-hook-form, store the File with setValue from the accept handler, run quickProblem and the pixel check in a resolver and show the sentence under the field. With TanStack Form, an async onChange validator can call the same pixel helper and return the sentence as the field error.
Related
- Pro
File Attachments Contact
Support and feedbackA support request on a card in card with a file drop zone: up to four PDFs or images, a progress bar per file, an error row for a wrong type, and a form that waits for the files before it sends.
- Pro
Resume Drop Contact
ApplicationsA robotics hiring form that starts with a single PDF dropzone, then a role combobox, a start date and an optional LinkedIn field.
- Pro
URL Input with Paste Cleanup Form
Text fieldsA website field with a fixed https:// in front that cleans what you paste, then reads the address with the browser's URL parser into Host, Path and Secure cells.
- Pro
Text Input with Address Preview Form
Text fieldsA form panel with one required project-name field that shows the web address it will get as you type, flags a name that is taken or past 50 characters, and offers the next free address.
- Pro
Brand Colour Picker Form
Numbers and pickersA brand colour field that makes a scale: a hex input and the colour picker kept in sync, eight named presets, and five derived steps by lightness, each with its hex and a copy button.
- Pro
Case Quantity Number Input Form
Numbers and pickersA quantity field that sells in cases of six: typing a number snaps it up to a whole case on leaving, the keys and buttons move one case or five, and the line total is worked out in whole cents.