-
Notifications
You must be signed in to change notification settings - Fork 133
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
1 parent
b7173e1
commit b82bdbf
Showing
5 changed files
with
171 additions
and
26 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
82 changes: 82 additions & 0 deletions
82
examples/ui-demo/src/components/configuration/PhotoUpload.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 |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { useConfig } from "@/src/app/state"; | ||
import { PhotoIcon } from "../icons/photo"; | ||
import FileUploadInput from "../shared/FileUploadInput"; | ||
import { ChangeEvent } from "react"; | ||
|
||
const LENGTH = 10; | ||
function truncatedFileName(name: string) { | ||
if (name.length < LENGTH) return name; | ||
|
||
return `${name.slice(0, LENGTH - 2)}...`; | ||
} | ||
|
||
export function PhotoUploads({ mode }: { mode: "dark" | "light" }) { | ||
const { config, setConfig } = useConfig(); | ||
|
||
const logo = mode === "dark" ? config.ui.logoDark : config.ui.logoLight; | ||
|
||
const onUpload = (e: ChangeEvent<HTMLInputElement>) => { | ||
if (!e.target.files || e.target.files.length < 1) return; | ||
|
||
const file = e.target.files[0]; | ||
setConfig((prev) => ({ | ||
...prev, | ||
ui: { | ||
...prev.ui, | ||
[mode === "dark" ? "logoDark" : "logoLight"]: { | ||
fileName: file.name, | ||
fileSrc: URL.createObjectURL(file), | ||
}, | ||
}, | ||
})); | ||
}; | ||
|
||
const onRemove = () => { | ||
if (!logo?.fileSrc) return | ||
|
||
setConfig((prev) => ({ | ||
...prev, | ||
ui: { | ||
...prev.ui, | ||
[mode === "dark" ? "logoDark" : "logoLight"]: undefined, | ||
}, | ||
})); | ||
URL.revokeObjectURL(logo.fileSrc) | ||
} | ||
|
||
return ( | ||
<div className="flex gap-3 flex-1 basis-0"> | ||
<div | ||
className={`flex items-center justify-center h-[56px] w-[56px] rounded-xl ${ | ||
mode === "light" ? "bg-gray-100" : "bg-gray-500" | ||
}`} | ||
style={{ | ||
backgroundImage: logo?.fileSrc ? `url(${logo.fileSrc})` : undefined, | ||
backgroundSize: "cover", | ||
}} | ||
> | ||
{logo?.fileSrc ? null : ( | ||
<PhotoIcon color={mode === "dark" ? "white" : undefined} /> | ||
)} | ||
</div> | ||
<div className="flex flex-col gap-[2px]"> | ||
<div className="text-fg-secondary text-xs font-semibold"> | ||
{mode === "light" ? "Light" : "Dark"} mode | ||
</div> | ||
<div className="text-xs text-gray-500 font-medium"> | ||
{logo?.fileName ? truncatedFileName(logo.fileName) : "File name"} | ||
</div> | ||
{logo ? ( | ||
<button onClick={onRemove} className="text-left text-blue-600 text-xs font-semibold">Remove</button> | ||
) : ( | ||
<FileUploadInput | ||
className="text-left text-blue-600 text-xs font-semibold" | ||
onChange={onUpload} | ||
> | ||
Upload | ||
</FileUploadInput> | ||
)} | ||
</div> | ||
</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
52 changes: 52 additions & 0 deletions
52
examples/ui-demo/src/components/shared/FileUploadInput.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 |
---|---|---|
@@ -0,0 +1,52 @@ | ||
"use client" | ||
|
||
import React, { ChangeEvent, useRef } from "react" | ||
|
||
interface FileUploadInputProps { | ||
children: React.ReactNode | ||
onChange: (event: ChangeEvent<HTMLInputElement>) => void | ||
accept?: string | ||
[key: string]: any | ||
className?: string | ||
} | ||
|
||
const FileUploadInput: React.FC<FileUploadInputProps> = ({ | ||
children, | ||
onChange, | ||
accept = "image/*", | ||
className, | ||
...props | ||
}) => { | ||
const ref = useRef<HTMLInputElement>(null) | ||
|
||
const selectImage = () => { | ||
if (ref.current) { | ||
// reset the current value so that the input's onChange handler fires even if the | ||
// user keeps selecting the same file | ||
ref.current.value = "" | ||
ref.current.click() | ||
} | ||
} | ||
|
||
return ( | ||
<button | ||
className={className} | ||
onClick={selectImage} | ||
onKeyDown={selectImage} | ||
type="button" | ||
> | ||
{children} | ||
<input | ||
{...props} | ||
ref={ref} | ||
onChange={onChange} | ||
style={{ display: "none" }} | ||
type="file" | ||
size={10000} | ||
accept={accept} | ||
/> | ||
</button> | ||
) | ||
} | ||
|
||
export default FileUploadInput |