Skip to content
Closed
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
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"**/node_modules": true,
".{dev,shadowenv.d}": true,
"build-intermediate/": true,
"build/": true,
"build/": false,
"docs/": true,
"esnext/": true,
"index.*": true,
Expand Down
1 change: 1 addition & 0 deletions UNRELEASED.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Use [the changelog guidelines](https://git.io/polaris-changelog-guidelines) to f

- Fixed accessibility issue with ChoiceList errors not being correctly connected to the inputs ([#1824](https://github.com/Shopify/polaris-react/pull/1824));
- Fixed `Tab` `aria-controls` pointing to a non-existent `Panel` `id` ([#1869](https://github.com/Shopify/polaris-react/pull/1869))
- Constrained `DropZone` height based on inherited wrapper height ([#1138](https://github.com/Shopify/polaris-react/pull/1138))

### Documentation

Expand Down
11 changes: 11 additions & 0 deletions src/components/DropZone/DropZone.scss
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,17 @@ $dropzone-overlay-border-color-error: color('red');
$dropzone-overlay-background: color('indigo', 'lighter');
$dropzone-overlay-background-error: color('red', 'lighter');

.DropZoneWrapper {
height: inherit;
}

.DropZone {
position: relative;
display: flex;
justify-content: center;
background-color: $dropzone-background;
border-radius: $dropzone-border-radius;
height: inherit;
}

.hasOutline {
Expand All @@ -38,6 +43,10 @@ $dropzone-overlay-background-error: color('red', 'lighter');
cursor: not-allowed;
}

.isMeasuring {
visibility: hidden;
}

.sizeExtraLarge {
min-height: $dropzone-min-height-extra-large;
}
Expand All @@ -58,7 +67,9 @@ $dropzone-overlay-background-error: color('red', 'lighter');
}

.Container {
display: flex;
flex: 1;
justify-content: center;
}

.Overlay {
Expand Down
109 changes: 72 additions & 37 deletions src/components/DropZone/DropZone.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,23 @@ import {withAppProvider, WithAppProviderProps} from '../AppProvider';
import {FileUpload, Provider} from './components';

import {fileAccepted, getDataTransferFiles} from './utils';
import {DropZoneContext} from './types';
import {DropZoneContext, Size} from './types';

import styles from './DropZone.scss';

export type Type = 'file' | 'image';

export interface State {
id: string;
size: string;
height: Size;
width: Size;
type?: string;
error?: boolean;
dragging: boolean;
overlayText?: string;
errorOverlayText?: string;
numFiles: number;
measuring: boolean;
}

export interface Props {
Expand Down Expand Up @@ -163,18 +165,10 @@ class DropZone extends React.Component<CombinedProps, State> {
return;
}

let size = 'extraLarge';
const width = this.node.current.getBoundingClientRect().width;
const height = this.setWrapperSize('height', this.node);
const width = this.setWrapperSize('width', this.node);

if (width < 100) {
size = 'small';
} else if (width < 160) {
size = 'medium';
} else if (width < 300) {
size = 'large';
}

this.setState({size});
this.setState({height, width, measuring: false});
},
50,
{trailing: true},
Expand All @@ -195,19 +189,23 @@ class DropZone extends React.Component<CombinedProps, State> {
this.state = {
type,
id: props.id || getUniqueID(),
size: 'extraLarge',
height: Size.ExtraLarge,
dragging: false,
error: false,
overlayText: translate(`Polaris.DropZone.overlayText${suffix}`),
errorOverlayText: translate(`Polaris.DropZone.errorOverlayText${suffix}`),
numFiles: 0,
width: Size.ExtraLarge,
measuring: true,
};
}

get getContext(): DropZoneContext {
const {width, height, type = 'file'} = this.state;
return {
size: this.state.size,
type: this.state.type || 'file',
width,
height,
type,
};
}

Expand All @@ -220,9 +218,11 @@ class DropZone extends React.Component<CombinedProps, State> {
id,
dragging,
error,
size,
height,
width,
overlayText,
errorOverlayText,
measuring,
} = this.state;
const {
label,
Expand Down Expand Up @@ -253,43 +253,58 @@ class DropZone extends React.Component<CombinedProps, State> {
styles.DropZone,
outline && styles.hasOutline,
(active || dragging) && styles.isDragging,
measuring && styles.isMeasuring,
error && styles.hasError,
size && size === 'extraLarge' && styles.sizeExtraLarge,
size && size === 'large' && styles.sizeLarge,
size && size === 'medium' && styles.sizeMedium,
size && size === 'small' && styles.sizeSmall,
height === Size.ExtraLarge && styles.sizeExtraLarge,
height === Size.Large && styles.sizeLarge,
height === Size.Medium && styles.sizeMedium,
height === Size.Small && styles.sizeSmall,
);

const extraLargeDropZoneSize =
width === Size.ExtraLarge && height !== Size.Small;

const mediumLargeDropZoneSize =
(width === Size.Medium || width === Size.Large) && height !== Size.Small;

const dragOverlayDisplayText = extraLargeDropZoneSize && (
<DisplayText size="small" element="p">
{overlayText}
</DisplayText>
);

const dragOverlayCaption = mediumLargeDropZoneSize && (
<Caption>{overlayText}</Caption>
);

const dragOverlay =
(active || dragging) && !error && overlay ? (
<div className={styles.Overlay}>
<Stack vertical spacing="tight">
<Icon source={DragDropMajorMonotone} color="indigo" />
{size === 'extraLarge' && (
<DisplayText size="small" element="p">
{overlayText}
</DisplayText>
)}
{(size === 'medium' || size === 'large') && (
<Caption>{overlayText}</Caption>
)}
{dragOverlayDisplayText}
{dragOverlayCaption}
</Stack>
</div>
) : null;

const dragErrorOverlayDisplayText = extraLargeDropZoneSize && (
<DisplayText size="small" element="p">
{errorOverlayText}
</DisplayText>
);

const dragErrorOverlayCaption = mediumLargeDropZoneSize && (
<Caption>{errorOverlayText}</Caption>
);

const dragErrorOverlay =
dragging && error ? (
<div className={styles.Overlay}>
<Stack vertical spacing="tight">
<Icon source={CircleAlertMajorMonotone} color="red" />
{size === 'extraLarge' && (
<DisplayText size="small" element="p">
{errorOverlayText}
</DisplayText>
)}
{(size === 'medium' || size === 'large') && (
<Caption>{errorOverlayText}</Caption>
)}
{dragErrorOverlayDisplayText}
{dragErrorOverlayCaption}
</Stack>
</div>
) : null;
Expand Down Expand Up @@ -382,6 +397,26 @@ class DropZone extends React.Component<CombinedProps, State> {
this.fileInputNode.current.click();
};

private setWrapperSize = (
size: string,
node: React.RefObject<HTMLDivElement>,
) => {
let wrapperSize = Size.ExtraLarge;

if (node.current) {
const getSize = size === 'height' ? 'height' : 'width';
const wrapper = node.current.getBoundingClientRect()[getSize];
if (wrapper < Size.Small) {
wrapperSize = Size.Small;
} else if (wrapper < Size.Medium) {
wrapperSize = Size.Medium;
} else if (wrapper < Size.Large) {
wrapperSize = Size.Large;
}
}
return wrapperSize;
};

private getValidatedFiles = (files: File[] | DataTransferItem[]) => {
const {accept, allowMultiple, customValidator} = this.props;

Expand Down
5 changes: 3 additions & 2 deletions src/components/DropZone/components/Context/Context.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import * as React from 'react';
import {DropZoneContext} from '../../types';
import {DropZoneContext, Size} from '../../types';

const {Provider, Consumer} = React.createContext<DropZoneContext>({
size: 'extraLarge',
width: Size.ExtraLarge,
height: Size.ExtraLarge,
type: 'file',
});

Expand Down
Loading