Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: lightbox focus #765

Merged
merged 1 commit into from
Oct 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 47 additions & 49 deletions src/components/Shared/Attachments.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ interface Props {
}

const Attachments: FC<Props> = ({ attachments, setAttachments, isNew = false, hideDelete = false }) => {
const [isImageExpanded, setIsImageExpanded] = useState(false);
const [expanedImage, setExpandedImage] = useState<string | null>(null);

const removeAttachment = (attachment: any) => {
const arr = attachments;
Expand All @@ -58,61 +58,59 @@ const Attachments: FC<Props> = ({ attachments, setAttachments, isNew = false, hi
: attachments?.slice(0, 4);

return slicedAttachments?.length !== 0 ? (
<div className={clsx(getClass(slicedAttachments?.length)?.row, 'grid grid-flow-col gap-2 pt-3')}>
{slicedAttachments?.map((attachment: LensterAttachment & MediaSet) => {
const type = isNew ? attachment.type : attachment.original.mimeType;
const url = isNew ? getIPFSLink(attachment.item) : getIPFSLink(attachment.original.url);
<>
<div className={clsx(getClass(slicedAttachments?.length)?.row, 'grid grid-flow-col gap-2 pt-3')}>
{slicedAttachments?.map((attachment: LensterAttachment & MediaSet) => {
const type = isNew ? attachment.type : attachment.original.mimeType;
const url = isNew ? getIPFSLink(attachment.item) : getIPFSLink(attachment.original.url);

return (
<div
className={clsx(type === 'video/mp4' ? '' : getClass(slicedAttachments?.length)?.aspect)}
key={url}
onClick={(event) => {
event.stopPropagation();
}}
>
{type === 'image/svg+xml' ? (
<Button
className="text-sm"
variant="primary"
icon={<ExternalLinkIcon className="h-4 w-4" />}
onClick={() => {
window.open(url, '_blank');
}}
>
<span>Open Image in new tab</span>
</Button>
) : type === 'video/mp4' ? (
<Video src={url} />
) : (
<>
{isImageExpanded && (
<LightBox url={url} show={isImageExpanded} onClose={() => setIsImageExpanded(false)} />
)}
return (
<div
className={clsx(type === 'video/mp4' ? '' : getClass(slicedAttachments?.length)?.aspect)}
key={url}
onClick={(event) => {
event.stopPropagation();
}}
>
{type === 'image/svg+xml' ? (
<Button
className="text-sm"
variant="primary"
icon={<ExternalLinkIcon className="h-4 w-4" />}
onClick={() => {
window.open(url, '_blank');
}}
>
<span>Open Image in new tab</span>
</Button>
) : type === 'video/mp4' ? (
<Video src={url} />
) : (
<img
className="object-cover bg-gray-100 rounded-lg border cursor-pointer dark:bg-gray-800 dark:border-gray-700/80"
loading="lazy"
onClick={() => setIsImageExpanded(true)}
onClick={() => setExpandedImage(url)}
src={imagekitURL(url, 'attachment')}
alt={imagekitURL(url, 'attachment')}
/>
</>
)}
{isNew && !hideDelete && (
<div className="m-3">
<button
type="button"
className="p-1.5 bg-gray-900 rounded-full opacity-75"
onClick={() => removeAttachment(attachment)}
>
<XIcon className="w-4 h-4 text-white" />
</button>
</div>
)}
</div>
);
})}
</div>
)}
{isNew && !hideDelete && (
<div className="m-3">
<button
type="button"
className="p-1.5 bg-gray-900 rounded-full opacity-75"
onClick={() => removeAttachment(attachment)}
>
<XIcon className="w-4 h-4 text-white" />
</button>
</div>
)}
</div>
);
})}
</div>
<LightBox show={!!expanedImage} url={expanedImage} onClose={() => setExpandedImage(null)} />
</>
) : null;
};

Expand Down
4 changes: 2 additions & 2 deletions src/components/UI/LightBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { FC, Fragment } from 'react';

interface Props {
show: boolean;
url: string;
url: string | null;
onClose: () => void;
}

Expand Down Expand Up @@ -34,7 +34,7 @@ export const LightBox: FC<Props> = ({ show, url, onClose }) => {
leaveFrom="opacity-100 translate-y-0 sm:scale-100"
leaveTo="opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95"
>
<img className="max-h-screen p-8" src={url} alt={url} onClick={onClose} />
<img className="max-h-screen p-8" src={url ?? ''} alt={url ?? ''} onClick={onClose} />
</Transition.Child>
</div>
</Dialog>
Expand Down