Image Fields
Preview: point at the image for the change and clear buttons
data-fit="contain" shows the whole image, and a width utility makes the box wide, for a logo
<div class="field">
<label for="demo-image-field-logo">Store Logo</label>
<div class="image-field w-72" data-fit="contain">
<img src="/demo-assets/logos/acme-store-logo.svg" alt="Current store logo" />
<i class="icon icon-image" aria-hidden="true"></i>
<button type="button" class="btn" data-variant="outline" data-size="icon-xs" id="demo-image-field-logo" data-change aria-label="Change store logo" data-tooltip="Change"><i class="icon icon-pencil" aria-hidden="true"></i></button>
<button type="button" class="btn" data-variant="outline" data-size="icon-xs" data-clear aria-label="Clear store logo" data-tooltip="Clear"><i class="icon icon-x" aria-hidden="true"></i></button>
<input type="file" name="logo" accept="image/*" />
</div>
</div>
data-empty: no image yet, so the box shows its icon and only the change button
A checkbox inside the field is checked while the image is cleared, and the form hears change
<form class="flex flex-col items-start gap-3" id="demo-image-field-form">
<div class="field">
<label for="demo-image-field-form-icon">Store Icon</label>
<div class="image-field">
<img src="/demo-assets/logos/acme-store.svg" alt="Current store icon" />
<i class="icon icon-image" aria-hidden="true"></i>
<button type="button" class="btn" data-variant="outline" data-size="icon-xs" id="demo-image-field-form-icon" data-change aria-label="Change store icon" data-tooltip="Change"><i class="icon icon-pencil" aria-hidden="true"></i></button>
<button type="button" class="btn" data-variant="outline" data-size="icon-xs" data-clear aria-label="Clear store icon" data-tooltip="Clear"><i class="icon icon-x" aria-hidden="true"></i></button>
<input type="file" name="icon" accept="image/*" />
<input type="checkbox" name="icon-clear" />
</div>
</div>
<pre class="bg-muted overflow-x-auto rounded-lg p-4 font-mono text-xs" id="demo-image-field-result">icon-clear: not sent</pre>
</form>
<script>
document.getElementById("demo-image-field-form").addEventListener("change", (event) => {
const sent = new FormData(event.currentTarget).get("icon-clear");
document.getElementById("demo-image-field-result").textContent = "icon-clear: " + (sent ? sent : "not sent");
});
</script>
API Reference
Attributes
| Attribute | On | Values | Default | Description |
|---|---|---|---|---|
| src | the img | an address, or absent | absent | The image the field starts with. Without it the field starts empty. |
| data-fit | .image-field | "contain" | none | Shows the whole image with room around it. Without it the image fills the box and is cropped. |
| data-change | a button | present | none | The button that opens the file picker. The field needs one. |
| data-clear | a button | present | none | The button that clears the image. Leave it out for an image that cannot be removed. |
| name | the input[type=file] | text | none | Sends the chosen file with the form. |
| accept | the input[type=file] | file types | any | The file types the browser's picker offers, such as "image/*". |
| checked | the input[type=checkbox] | present or absent | set by the script | The checkbox is optional. It is checked while the image is cleared, so the form tells the server to remove the stored file. |
| data-empty | .image-field | present or absent | set by the script | Present while the field shows no image. Write it by hand so a field with no image is right before the script runs. |
Events
| Event | On | Detail | Description |
|---|---|---|---|
| input | the input[type=file], and it bubbles | none | Sent when a file is chosen, by the browser, and when the image is cleared. |
| change | the input[type=file], and it bubbles | none | Sent when a file is chosen, by the browser, and when the image is cleared. event.target.files is empty after a clear. |
Keyboard
| Key | Description |
|---|---|
| Tab | Moves to the change button, then the clear button. Both are drawn while either has the focus. |
| Enter, Space | On the change button, opens the file picker. On the clear button, clears the image and moves the focus to the change button. |
The field never uploads. The form it is in sends the file.
The two buttons are drawn under the pointer, while one has the keyboard focus, on an empty field and always on a touch screen.
The field has no methods of its own: read the file through the input.
Once the script has started it, the element carries data-image-field-initialized="true" and sends tsugi:initialized. The methods and properties above are on the element itself.