Image Field

Displays an input that lets users upload an image and see its preview.

Upload image

Import the component and piece the parts together.

The Image Field component is made up of Root and Preview. View source

Displays a stylized image input field.

PropTypeDefault
defaultFileFile | string | null
fileFile | string | null
onFileChange(file: File | null) => void
onError(errors: ImageFieldError[]) => void
acceptImageFieldMimeType[]['image/jpeg', 'image/png', 'image/gif', 'image/svg+xml', 'image/webp', 'image/avif', 'image/x-icon', 'image/vnd.microsoft.icon']
maxSizeKBnumber
maxDimensions{ width: number; height: number }
minDimensions{ width: number; height: number }
rationumber
state"normal" | "disabled" | "read-only""normal"
placeholderstring"Upload image"

The following errors may be returned by the onError function:

ErrorDescription
"aspect-ratio-incorrect"The aspect ratio of the image doesn’t match the ratio value
"dimensions-too-large"The width or height of the image exceed maxDimensions
"dimensions-too-small"The width or height of the image fall short of minDimensions
"file-size-too-large"The file size exceeds the maxSizeKB value
"file-unreadable"The file could not be processed by the browser
"format-unacceptable"The file format doesn’t match any of the accept values

This component is based on the div element and supports common margin props m, mx, my, ml, mr, mt, mb.

Displays a preview of the uploaded image.

PropTypeDefault
backgroundColorstring
onSrcChange(src?: string) => void
children(src: string) => React.ReactNode

Pressing the Backspace key clears the Image Field contents and related errors.

A placeholder is rendered when no preview is displayed. Use the placeholder prop to change the default text.

Dark mode logo

Make the Root part controlled to access the uploaded file.

Upload image

Use the onSrcChange prop to access the temporary URL of the preview image.

Upload image

Use a URL for the defaultFile prop to create an uncontrolled field with an initial value.

You can render a custom preview layout if you pass a function as a child of the Preview part.

Use the backgroundColor prop on the Preview part to set a background color on the preview image.

Use the state prop to create a disabled or read-only Image Field.

Disabled
Read-only

Use the available validation props to enforce constraints upon the uploaded images. Use the onError prop to access the errors.

Upload image