Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(core): file/image input layout in narrow widths
- Loading branch information
1 parent
2846eec
commit 125dc41
Showing
1 changed file
with
47 additions
and
34 deletions.
There are no files selected for viewing
81 changes: 47 additions & 34 deletions
81
packages/sanity/src/core/form/inputs/files/common/UploadPlaceholder.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,76 @@ | ||
import React from 'react' | ||
import React, {useState} from 'react' | ||
import {UploadIcon} from '@sanity/icons' | ||
import {Flex, Inline} from '@sanity/ui' | ||
import {Flex, useElementSize} from '@sanity/ui' | ||
import {FileLike} from '../../../studio/uploads/types' | ||
import {FileInputButton} from './FileInputButton/FileInputButton' | ||
import {PlaceholderText} from './PlaceholderText' | ||
|
||
type UploadPlaceholderProps = { | ||
onUpload?: (files: File[]) => void | ||
interface UploadPlaceholderProps { | ||
accept: string | ||
acceptedFiles: FileLike[] | ||
browse?: React.ReactNode | ||
readOnly?: boolean | ||
type: string | ||
directUploads?: boolean | ||
hoveringFiles: FileLike[] | ||
acceptedFiles: FileLike[] | ||
onUpload?: (files: File[]) => void | ||
readOnly?: boolean | ||
rejectedFilesCount: number | ||
accept: string | ||
directUploads?: boolean | ||
type: string | ||
} | ||
|
||
export const UploadPlaceholder = React.memo(function UploadPlaceholder({ | ||
browse, | ||
onUpload, | ||
readOnly, | ||
type, | ||
hoveringFiles, | ||
acceptedFiles, | ||
rejectedFilesCount, | ||
accept, | ||
directUploads, | ||
}: UploadPlaceholderProps) { | ||
function UploadPlaceholderComponent(props: UploadPlaceholderProps) { | ||
const { | ||
accept, | ||
acceptedFiles, | ||
browse, | ||
directUploads, | ||
hoveringFiles, | ||
onUpload, | ||
readOnly, | ||
rejectedFilesCount, | ||
type, | ||
} = props | ||
|
||
const [rootElement, setRootElement] = useState<HTMLDivElement | null>(null) | ||
const rect = useElementSize(rootElement) | ||
|
||
// Adjust the layout in narrow containers | ||
const collapsed = rect?.border && rect.border.width < 440 | ||
|
||
return ( | ||
<Flex | ||
align="center" | ||
justify="space-between" | ||
align={collapsed ? undefined : 'center'} | ||
direction={collapsed ? 'column' : 'row'} | ||
gap={4} | ||
direction={['column', 'column', 'row']} | ||
paddingY={[2, 2, 0]} | ||
justify="space-between" | ||
paddingY={collapsed ? 1 : undefined} | ||
ref={setRootElement} | ||
> | ||
<Flex align="center" justify="center" gap={2} flex={1}> | ||
<Flex flex={1} justify="center"> | ||
<PlaceholderText | ||
readOnly={readOnly} | ||
hoveringFiles={hoveringFiles} | ||
acceptedFiles={acceptedFiles} | ||
directUploads={directUploads} | ||
hoveringFiles={hoveringFiles} | ||
readOnly={readOnly} | ||
rejectedFilesCount={rejectedFilesCount} | ||
type={type} | ||
directUploads={directUploads} | ||
/> | ||
</Flex> | ||
<Inline space={2}> | ||
|
||
<Flex align="center" gap={2} justify="center" wrap="wrap"> | ||
<FileInputButton | ||
accept={accept} | ||
data-testid="file-input-upload-button" | ||
disabled={readOnly || !directUploads} | ||
icon={UploadIcon} | ||
mode="ghost" | ||
onSelect={onUpload} | ||
accept={accept} | ||
text="Upload" | ||
data-testid="file-input-upload-button" | ||
disabled={readOnly || !directUploads} | ||
/> | ||
|
||
{browse} | ||
</Inline> | ||
</Flex> | ||
</Flex> | ||
) | ||
}) | ||
} | ||
|
||
export const UploadPlaceholder = React.memo(UploadPlaceholderComponent) |