-
Notifications
You must be signed in to change notification settings - Fork 224
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[FR] Styles for input[type='file']
#25
Comments
I second that. Converted my project to tailwind 2.0. The file input elements are all vanilla. |
Perhaps this tailwindlabs/tailwindcss#3515 is relevant? |
@carrete I don't think I'm running into tailwindlabs/tailwindcss#3515. But in looking at that issue, I realized that this project already does (and always has done) a little bit of styling on file inputs. It appears to reset the styles generally, I suppose to make it easier to style file inputs with Tailwind utilities. It does not do anything to remove or re-style the browser-generated button or the name of the selected file, which is what my original question was about. That button looks pretty out of place on a page otherwise styled by Tailwind. But... since writing this Issue, I've done some more research and found something that works for me. My requirements were: 1) the UI should look and behave like a regular button styled with Tailwind; 2) the style should be tweakable with Tailwind utilities; 3) the selected file name should be visible, without resorting to JS. Most solutions floating around the internet hide the whole input element, which hides the button but also hides the name of the selected file. There are various approaches that use JS to restore the name of the selected file, but that isn't really in the Tailwind spirit. Instead, I hide only the button by targeting it with the pseudo classes: /* file: styles.css */
/* for some reason, the browsers are ignoring these if they are combined into one rule, separated by commas */
::file-selector-button { @apply sr-only; }
::-webkit-file-upload-button { @apply sr-only; }
::-ms-browse { @apply sr-only; } <label class="text-sm font-semibold px-3 py-2 rounded-md hover:bg-gray-100 ring-1 ring-gray-500 focus-within:ring-2 focus-within:ring-indigo-500">
Choose file:
<input type="file" class="focus:outline-none">
</label> I defer to the Tailwind team as to whether tailwindcss-forms should do something similar by default. It'd be hard to un-hide the default button, so my guess is "no", but perhaps I'm not considering all the factors.
|
@mainej and others, thanks for the pointer. I used it to create this OK styled file component that others might find useful. I too found the same problem with having to duplicate the rules to types the file selector. I'm not familiar with which browser uses If anybody improves on the look of this. E.g., figures out how to shift the "no file selected" prompt to the right please share a link. Edit: Updated link to revised playground with better support for errors and a few other tweaks inspired from Bootstrap 5. |
The reason you need to duplicate the rules is because when browsers see an invalid selector the whole block gets ignored. So for browsers that don't support the standard version they can't fallback to the webkit version. https://play.tailwindcss.com/dAUlXDZt6r - Anyone who wants a file-selector-button variant this should demonstrate one. Though be aware it doesn't work with other variants like hover. (I'm not entirely sure why) |
Here's a version of @lukewarlow's Tailwind Play example that works with other variants like hover: https://play.tailwindcss.com/Bgd6hn9eI9. Note that this works (for me) in Chrome but for some reason not Firefox. |
And here's @gshaw's example re-worked to use the plugin: https://play.tailwindcss.com/LlHPfGpozT?layout=horizontal |
Fixed by tailwindlabs/tailwindcss#4936. 🎉 |
For those that end up here looking for the solution (like myself), here's a link to the section of the tailwind docs that mentions styling file input buttons: https://tailwindcss.com/docs/hover-focus-and-other-states#file-input-buttons |
I'm wondering if you have thoughts on styling file inputs. The vanilla file input is weird, with both a button and some dynamic text. Does that mean individual projects should choose styling on their own? Or is there some useful global reset?
The text was updated successfully, but these errors were encountered: