-
Notifications
You must be signed in to change notification settings - Fork 584
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
135 additions
and
69 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { classList } from "react-common/components/util"; | ||
import { Strings } from "../constants"; | ||
import { NoticeLabel } from "./NoticeLabel"; | ||
import { useState } from "react"; | ||
import css from "./styling/DragAndDropFileSurface.module.scss"; | ||
|
||
export interface DragAndDropFileSurfaceProps { | ||
onFileDroppedAsync: (file: File) => void; | ||
errorMessage?: string; | ||
} | ||
export const DragAndDropFileSurface: React.FC<DragAndDropFileSurfaceProps> = ({ onFileDroppedAsync, errorMessage }) => { | ||
const [fileIsOverSurface, setFileIsOverSurface] = useState(false); | ||
|
||
function handleDragOver(event: React.DragEvent<HTMLDivElement>) { | ||
// Stop the browser from intercepting the file. | ||
event.preventDefault(); | ||
} | ||
|
||
function handleDragEnter(event: React.DragEvent<HTMLDivElement>) { | ||
event.preventDefault(); | ||
setFileIsOverSurface(true); | ||
} | ||
|
||
function handleDragLeave(event: React.DragEvent<HTMLDivElement>) { | ||
event.preventDefault(); | ||
setFileIsOverSurface(false); | ||
} | ||
|
||
function handleDrop(event: React.DragEvent<HTMLDivElement>) { | ||
event.preventDefault(); | ||
|
||
setFileIsOverSurface(false); | ||
|
||
const file = event.dataTransfer.files[0]; | ||
if (file) { | ||
onFileDroppedAsync(file); | ||
} | ||
} | ||
|
||
return ( | ||
<div className={css["drag-and-drop-file-surface"]}> | ||
<div className={css["instruction-container"]}> | ||
<i className={classList("fas fa-file-upload", css["upload-icon"])}></i> | ||
<div className="no-select">{fileIsOverSurface ? Strings.ReleaseToUpload : Strings.DragAndDrop}</div> | ||
</div> | ||
|
||
{errorMessage && ( | ||
<div className={css["error-label-container"]}> | ||
<NoticeLabel severity="error">{errorMessage}</NoticeLabel> | ||
</div> | ||
)} | ||
|
||
{/* | ||
Use a transparent div over everything to detect drop events. | ||
We can't use drag-and-drop-file-surface directly because child elements interfere with the drag events. | ||
*/} | ||
<div | ||
className={css["droppable-surface"]} | ||
onDrop={handleDrop} | ||
onDragEnter={handleDragEnter} | ||
onDragLeave={handleDragLeave} | ||
onDragOver={handleDragOver} | ||
/> | ||
</div> | ||
); | ||
}; |
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
40 changes: 40 additions & 0 deletions
40
teachertool/src/components/styling/DragAndDropFileSurface.module.scss
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,40 @@ | ||
.drag-and-drop-file-surface { | ||
display: flex; | ||
flex-direction: column; | ||
align-items: center; | ||
justify-content: center; | ||
position: relative; | ||
|
||
background-color: var(--pxt-content-background); | ||
border-radius: 0.5rem; | ||
border: 1px dashed lighten(black, 50%); // todo thsparks : yet another theme variable? | ||
|
||
padding: 2rem; | ||
min-height: 30vh; | ||
|
||
.instruction-container { | ||
display: flex; | ||
flex-direction: column; | ||
align-items: center; | ||
|
||
color: lighten(black, 50%); // todo thsparks : yet another theme variable? | ||
font-size: 2rem; | ||
|
||
.upload-icon { | ||
font-size: 3rem; | ||
} | ||
} | ||
|
||
.error-label-container { | ||
position: absolute; | ||
bottom: 2rem; | ||
} | ||
|
||
.droppable-surface { | ||
position: absolute; | ||
top: 0; | ||
left: 0; | ||
right: 0; | ||
bottom: 0; | ||
} | ||
} |
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