-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Output mux new_asset_settings from upload config
- Loading branch information
1 parent
182b028
commit 35da205
Showing
10 changed files
with
816 additions
and
421 deletions.
There are no files selected for viewing
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
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 |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import {PropsWithChildren, useRef, useState} from 'react' | ||
import {Box, Button, Card, CardTone, Flex, Inline, Text} from '@sanity/ui' | ||
import {FileInputButton} from './FileInputButton' | ||
import {UploadIcon} from '@sanity/icons' | ||
import {extractDroppedFiles} from '../util/extractFiles' | ||
|
||
interface FileInputAreaProps extends PropsWithChildren { | ||
accept?: string | ||
acceptMIMETypes?: string[] | ||
label: React.ReactNode | ||
onSelect: (files: FileList | File[]) => void | ||
} | ||
|
||
export default function FileInputArea({ | ||
label, | ||
accept, | ||
acceptMIMETypes, | ||
onSelect, | ||
}: FileInputAreaProps) { | ||
const dragEnteredEls = useRef<EventTarget[]>([]) | ||
const [dragState, setDragState] = useState<'valid' | 'invalid' | null>(null) | ||
|
||
// Stages and validates an upload from dragging+dropping files or folders | ||
const handleDrop: React.DragEventHandler<HTMLDivElement> = (event) => { | ||
setDragState(null) | ||
event.preventDefault() | ||
event.stopPropagation() | ||
extractDroppedFiles(event.nativeEvent.dataTransfer!).then(onSelect) | ||
} | ||
|
||
/* ------------------------------- Drag State ------------------------------- */ | ||
|
||
const handleDragOver: React.DragEventHandler<HTMLDivElement> = (event) => { | ||
event.preventDefault() | ||
event.stopPropagation() | ||
} | ||
|
||
const handleDragEnter: React.DragEventHandler<HTMLDivElement> = (event) => { | ||
event.stopPropagation() | ||
dragEnteredEls.current.push(event.target) | ||
const type = event.dataTransfer.items?.[0]?.type | ||
setDragState( | ||
!acceptMIMETypes || acceptMIMETypes.some((mimeType) => type?.match(mimeType)) | ||
? 'valid' | ||
: 'invalid' | ||
) | ||
} | ||
|
||
const handleDragLeave: React.DragEventHandler<HTMLDivElement> = (event) => { | ||
event.stopPropagation() | ||
const idx = dragEnteredEls.current.indexOf(event.target) | ||
if (idx > -1) { | ||
dragEnteredEls.current.splice(idx, 1) | ||
} | ||
if (dragEnteredEls.current.length === 0) { | ||
setDragState(null) | ||
} | ||
} | ||
|
||
let tone: CardTone = 'inherit' | ||
if (dragState) tone = dragState === 'valid' ? 'positive' : 'critical' | ||
return ( | ||
<Card border sizing="border" tone={tone} style={{borderStyle: 'dashed'}} padding={3}> | ||
<Flex | ||
align="center" | ||
justify="space-between" | ||
gap={4} | ||
direction={['column', 'column', 'row']} | ||
paddingY={[2, 2, 0]} | ||
sizing="border" | ||
onDrop={handleDrop} | ||
onDragOver={handleDragOver} | ||
onDragLeave={handleDragLeave} | ||
onDragEnter={handleDragEnter} | ||
> | ||
<Flex align="center" justify="center" gap={2} flex={1}> | ||
{label} | ||
</Flex> | ||
<Inline space={2}> | ||
<FileInputButton | ||
mode="ghost" | ||
tone="default" | ||
icon={UploadIcon} | ||
text="Upload" | ||
onSelect={onSelect} | ||
accept={accept} | ||
/> | ||
</Inline> | ||
</Flex> | ||
</Card> | ||
) | ||
} |
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
Oops, something went wrong.