Skip to content

Commit

Permalink
Feat(web-react): Attachment image preview object fit #DS-1005
Browse files Browse the repository at this point in the history
- Now accepts cover/contain value for objectFit prop to
`FileUploaderAttachment` component
  • Loading branch information
pavelklibani committed Oct 24, 2023
1 parent f339e24 commit 9e7539a
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,29 @@ type AttachmentImagePreviewProps = {
imagePreview: string;
label: string;
meta?: FileMetadata;
imageObjectFit?: 'contain' | 'cover';
};

const AttachmentImagePreview = ({ label, imagePreview, meta }: AttachmentImagePreviewProps) => {
const { classProps } = useFileUploaderStyleProps({ meta });
const { imageCropStyles } = classProps;
const AttachmentImagePreview = ({ label, imagePreview, meta, imageObjectFit }: AttachmentImagePreviewProps) => {
const { classProps } = useFileUploaderStyleProps({ meta, imageObjectFit });
const { imageCropStyles, attachmentStyles } = classProps;

return (
<span className={classProps.attachment.image}>
<img src={imagePreview} width={IMAGE_DIMENSION} height={IMAGE_DIMENSION} alt={label} style={imageCropStyles} />
<img
src={imagePreview}
width={IMAGE_DIMENSION}
height={IMAGE_DIMENSION}
alt={label}
style={{ ...imageCropStyles, ...attachmentStyles }}
/>
</span>
);
};

AttachmentImagePreview.defaultProps = {
meta: undefined,
imageObjectFit: 'cover',
};

export default AttachmentImagePreview;
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@ const FileUploaderAttachment = (props: SpiritFileUploaderAttachmentProps) => {
hasImagePreview,
iconName = DEFAULT_ICON_NAME,
id,
imageObjectFit,
label,
meta,
name,
onDismiss,
onEdit,
onError,
removeText,
meta,
...restProps
} = props;
const [imagePreview, setImagePreview] = useState<string>('');
Expand Down Expand Up @@ -83,7 +84,7 @@ const FileUploaderAttachment = (props: SpiritFileUploaderAttachmentProps) => {
className={classNames(classProps.attachment.root, styleProps.className)}
>
{hasImagePreview && imagePreview ? (
<AttachmentImagePreview label={label} imagePreview={imagePreview} meta={meta} />
<AttachmentImagePreview label={label} imagePreview={imagePreview} meta={meta} imageObjectFit={imageObjectFit} />
) : (
<Icon name={iconName} aria-hidden="true" />
)}
Expand Down
1 change: 1 addition & 0 deletions packages/web-react/src/components/FileUploader/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,7 @@ The rest of the properties are created from the default `<ul>` element. [More ab
| `hasImagePreview` | `bool` | false || Show image preview |
| `iconName` | `string` | `file` || Icon shown along the file |
| `id` | `string` ||| FileUploaderAttachment id |
| `imageObjectFit` | [`cover` \| `contain`] | `cover` || Defines FileUploaderAttachment image fit in container |
| `label` | `string` ||| File name |
| `name` | `string` ||| Input field name |
| `onDismiss` | `(key: string) => FileQueueMapType` ||| Callback to delete an item from the queue |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@ import { useClassNamePrefix } from '../../hooks';
import { FileMetadata, FileUploaderQueueLimitBehaviorType, Validation } from '../../types';

export interface FileUploaderStyleProps extends Validation {
isDragAndDropSupported?: boolean;
isLabelHidden?: boolean;
imageObjectFit?: 'contain' | 'cover';
isDisabled?: boolean;
isDisabledByQueueLimitBehavior?: boolean;
isDragAndDropSupported?: boolean;
isDragging?: boolean;
isDropZoneHidden?: boolean;
isFluid?: boolean;
queueLimitBehavior?: FileUploaderQueueLimitBehaviorType;
isLabelHidden?: boolean;
meta?: FileMetadata;
queueLimitBehavior?: FileUploaderQueueLimitBehaviorType;
}

type ImageCropCSS = {
Expand All @@ -23,6 +24,10 @@ type ImageCropCSS = {
[FileUploaderCropCSS.HEIGHT]?: string;
} & CSSProperties;

type ImageObjectFit = {
'--file-uploader-attachment-image-object-fit': string;
};

export interface FileUploaderStyleReturn {
/** className props */
classProps: {
Expand All @@ -49,6 +54,7 @@ export interface FileUploaderStyleReturn {
slot: string;
};
imageCropStyles?: ImageCropCSS;
attachmentStyles?: ImageObjectFit;
};
}

Expand Down Expand Up @@ -79,8 +85,9 @@ export const useFileUploaderStyleProps = (props?: FileUploaderStyleProps): FileU
const fileUploaderAttachmentImageClass = `${fileUploaderAttachmentClass}__image`;
const fileUploaderAttachmentSlotClass = `${fileUploaderAttachmentClass}__slot`;

const { meta } = props || {};
const { meta, imageObjectFit } = props || {};
let imageCropCSS: ImageCropCSS | undefined;
let imageObjectFitCSS: ImageObjectFit | undefined;
const hasCoords = meta && meta.x != null && meta.y != null && meta.width != null && meta.height != null;

if (hasCoords) {
Expand All @@ -94,6 +101,12 @@ export const useFileUploaderStyleProps = (props?: FileUploaderStyleProps): FileU
};
}

if (imageObjectFit) {
imageObjectFitCSS = {
'--file-uploader-attachment-image-object-fit': imageObjectFit,
};
}

return {
classProps: {
root: classNames(fileUploaderClass, { [fileUploaderFluidClass]: props?.isFluid }),
Expand Down Expand Up @@ -129,6 +142,7 @@ export const useFileUploaderStyleProps = (props?: FileUploaderStyleProps): FileU
slot: fileUploaderAttachmentSlotClass,
},
...(hasCoords && { imageCropStyles: imageCropCSS }),
...(imageObjectFit && { attachmentStyles: imageObjectFitCSS }),
},
};
};
1 change: 1 addition & 0 deletions packages/web-react/src/types/fileUploader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ export interface FileUploaderAttachmentBaseProps extends Omit<SpiritLItemElement
hasImagePreview?: boolean;
iconName?: string;
id: string;
imageObjectFit?: 'contain' | 'cover';
label: string;
meta?: FileMetadata;
name: string;
Expand Down

0 comments on commit 9e7539a

Please sign in to comment.