-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fiks drag-effekt og dra ut i egne sub-komponenter
- Loading branch information
Showing
6 changed files
with
270 additions
and
98 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,85 @@ | ||
import React, { ChangeEvent, useRef, useState } from "react"; | ||
import { useClientLayoutEffect } from "../../util"; | ||
import { BodyShort } from "../../typography"; | ||
import Wrapper from "./Wrapper"; | ||
import UploadButton from "./UploadButton"; | ||
|
||
interface Props { | ||
label: string; | ||
className: string | undefined; | ||
divRef: React.Ref<HTMLDivElement>; | ||
error: string | undefined; | ||
inputId: string; | ||
multiple: boolean; | ||
accept: string | undefined; | ||
handleUpload: (event: ChangeEvent<HTMLInputElement>) => void; | ||
onDragEnter: () => void; | ||
onDragEnd: () => void; | ||
isDraggingOver: boolean; | ||
} | ||
|
||
const BoxVariant = ({ | ||
label, | ||
className, | ||
onDragEnter, | ||
onDragEnd, | ||
divRef, | ||
error, | ||
isDraggingOver, | ||
inputId, | ||
multiple, | ||
accept, | ||
handleUpload, | ||
}: Props) => { | ||
const labelRef = useRef<HTMLLabelElement | null>(null) | ||
const [widthOverride, setWidthOverride] = useState<number>() | ||
const [heightOverride, setHeightOverride] = useState<number>() | ||
|
||
useClientLayoutEffect(() => { | ||
if (isDraggingOver) { | ||
const requestID = window.requestAnimationFrame(() => { | ||
const boundingClientRect = labelRef?.current?.getBoundingClientRect() | ||
setWidthOverride(boundingClientRect?.width) | ||
setHeightOverride(boundingClientRect?.height) | ||
}); | ||
return () => { | ||
setWidthOverride(undefined); | ||
setHeightOverride(undefined); | ||
cancelAnimationFrame(requestID); | ||
}; | ||
} else { | ||
setWidthOverride(undefined); | ||
setHeightOverride(undefined); | ||
} | ||
}, [isDraggingOver]); | ||
|
||
return (<Wrapper | ||
divRef={divRef} | ||
className={className} | ||
labelClassName="navds-fileupload--box" | ||
onDragEnter={onDragEnter} | ||
onDragEnd={onDragEnd} | ||
labelRef={labelRef} | ||
style={{ | ||
width: widthOverride, | ||
height: heightOverride | ||
}} | ||
error={error} | ||
isDraggingOver={isDraggingOver} | ||
inputId={inputId} | ||
multiple={multiple} | ||
accept={accept} | ||
handleUpload={handleUpload} | ||
> | ||
{widthOverride | ||
? <BodyShort as="span">Slipp</BodyShort> | ||
: (<> | ||
<BodyShort as="span">Dra og slipp</BodyShort> | ||
<BodyShort as="span">eller</BodyShort> | ||
<UploadButton label={label} /> | ||
</>) | ||
} | ||
</Wrapper>) | ||
} | ||
|
||
export default BoxVariant |
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,48 @@ | ||
import React, { ChangeEvent } from "react"; | ||
import Wrapper from "./Wrapper"; | ||
import UploadButton from "./UploadButton"; | ||
|
||
interface Props { | ||
label: string; | ||
className: string | undefined; | ||
divRef: React.Ref<HTMLDivElement>; | ||
error: string | undefined; | ||
inputId: string; | ||
multiple: boolean; | ||
accept: string | undefined; | ||
handleUpload: (event: ChangeEvent<HTMLInputElement>) => void; | ||
onDragEnter: () => void; | ||
onDragEnd: () => void; | ||
isDraggingOver: boolean; | ||
} | ||
|
||
const ButtonVariant = ({ | ||
label, | ||
className, | ||
onDragEnter, | ||
onDragEnd, | ||
divRef, | ||
error, | ||
isDraggingOver, | ||
inputId, | ||
multiple, | ||
accept, | ||
handleUpload | ||
}: Props) => ( | ||
<Wrapper | ||
divRef={divRef} | ||
className={className} | ||
error={error} | ||
inputId={inputId} | ||
multiple={multiple} | ||
accept={accept} | ||
handleUpload={handleUpload} | ||
onDragEnter={onDragEnter} | ||
onDragEnd={onDragEnd} | ||
isDraggingOver={isDraggingOver} | ||
> | ||
<UploadButton label={label} /> | ||
</Wrapper> | ||
) | ||
|
||
export default ButtonVariant |
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,15 @@ | ||
import { UploadIcon } from "@navikt/aksel-icons"; | ||
import React from "react"; | ||
|
||
interface Props { | ||
label: string | ||
} | ||
|
||
const UploadButton = ({ label }: Props) => ( | ||
<span className="navds-button navds-button--secondary navds-fileupload__button"> | ||
<UploadIcon fontSize="1.5rem" focusable={false} aria-hidden={true} className="navds-fileupload__icon" /> | ||
{label} | ||
</span> | ||
) | ||
|
||
export default UploadButton |
Oops, something went wrong.