Skip to content

Commit

Permalink
fix: avoid DropZone leaking style to children
Browse files Browse the repository at this point in the history
  • Loading branch information
targos committed Nov 15, 2022
1 parent 48b1d7f commit a267efa
Show file tree
Hide file tree
Showing 6 changed files with 378 additions and 433 deletions.
26 changes: 14 additions & 12 deletions src/components/drop-zone/DropZone.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@ export interface DropZoneProps {
) => void;
fileValidator?: <T extends File>(file: T) => FileError | FileError[] | null;
}
const centerStyle = css`

const messageStyle = css`
font-weight: 600;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
`;

export function DropZoneContainer(
props: DropZoneProps & {
children: JSX.Element;
Expand Down Expand Up @@ -55,7 +58,7 @@ function DropZoneContent(
color = 'black',
children,
onDrop,
emptyText = 'Drag and drop your files here, or click to select files',
emptyText = 'Click or drag and drop to add data.',
isContainer = false,
onClick,
fileValidator,
Expand Down Expand Up @@ -85,17 +88,14 @@ function DropZoneContent(
${isContainer ? null : 'min-height: 150px; cursor: pointer;'}
height: 100%;
width: 100%;
${isDragActive || !isContainer ? `border: 2px dashed ${color};` : null}
color: ${color};
display: flex;
align-items: center;
justify-content: center;
`}
style={{
border:
isDragActive || !isContainer ? `2px dashed ${color}` : undefined,
}}
>
<div
css={css`
text-align: center;
font-weight: 600;
width: 100%;
height: 100%;
display: flex;
Expand All @@ -112,18 +112,20 @@ function DropZoneContent(

<div style={{ fontSize: '1.5em' }}>
{isDragActive ? (
<div css={centerStyle}>
<div css={messageStyle} style={{ color }}>
<FaCloudUploadAlt
size={70}
css={css`
margin: auto;
`}
/>

<p>Drop the files here</p>
<p>Drop the files here.</p>
</div>
) : isContainer ? null : (
<p css={centerStyle}>{emptyText}</p>
<p css={messageStyle} style={{ color }}>
{emptyText}
</p>
)}
</div>
</div>
Expand Down
202 changes: 6 additions & 196 deletions src/pages/big-map/App.tsx
Original file line number Diff line number Diff line change
@@ -1,210 +1,20 @@
import { ErrorBoundary } from 'react-error-boundary';
import { FaMeteor, FaTabletAlt, FaGlasses, FaSave } from 'react-icons/fa';
import { KbsProvider, useKbsGlobal } from 'react-kbs';
import { KbsProvider } from 'react-kbs';

import {
AppStateProvider,
useAppDispatch,
useAppState,
getCurrentMeasurement,
download,
getSelectedMeasurement,
} from '../../app-data/index';
import {
MeasurementExplorer,
MeasurementInfoPanel,
MeasurementsPanel,
} from '../../app/index';
import {
FullscreenToolbarButton,
Accordion,
DropZoneContainer,
Header,
RootLayout,
SplitPane,
Toolbar,
FullScreenProvider,
} from '../../components/index';
import { AppStateProvider } from '../../app-data/index';
import { RootLayout, FullScreenProvider } from '../../components/index';

import MainLayout from './MainLayout';

export default function App() {
return (
<RootLayout>
<KbsProvider>
<AppStateProvider>
<FullScreenProvider>
<DropZoneArea />
<MainLayout />
</FullScreenProvider>
</AppStateProvider>
</KbsProvider>
</RootLayout>
);
}

function ErrorFallback({ error, resetErrorBoundary }) {
return (
<div role="alert">
<p>Something went wrong:</p>
<pre>{error.message}</pre>
<button type="button" onClick={resetErrorBoundary}>
Try again
</button>
</div>
);
}

function DropZoneArea() {
const dispatch = useAppDispatch();
const appState = useAppState();
const measurement = getCurrentMeasurement(appState);

function saveHandler(filename = 'file', spaceIndent = 0) {
const data = JSON.stringify(
{ data: appState.data, view: appState.view },
(_key, value) =>
ArrayBuffer.isView(value) ? Array.from(value as any) : value,
spaceIndent,
);
const blob = new Blob([data], { type: 'text/plain' });
download(blob, `${filename}.ium`);
}
useKbsGlobal([
{
shortcut: { key: 's', ctrl: true },
handler() {
saveHandler();
},
},
]);

return (
<DropZoneContainer
onDrop={() => {
// TODO
}}
>
<div
style={{
height: '100%',
display: 'flex',
flexDirection: 'column',
}}
>
<Header>
<Toolbar orientation="horizontal">
<Toolbar.Item titleOrientation="horizontal" title="Logo">
<FaMeteor />
</Toolbar.Item>
<Toolbar.Item
titleOrientation="horizontal"
title="Save as ium"
onClick={() => saveHandler()}
>
<FaSave />
</Toolbar.Item>
</Toolbar>
<Toolbar orientation="horizontal">
<FullscreenToolbarButton />
</Toolbar>
</Header>
<div
style={{
display: 'flex',
flexDirection: 'row',
flex: 1,
}}
>
<div>
<Toolbar orientation="vertical">
<Toolbar.Item title="Glasses" active>
<FaGlasses />
</Toolbar.Item>
<Toolbar.Item title="Open in large mode">
<FaTabletAlt />
</Toolbar.Item>
</Toolbar>
</div>
<div
style={{
width: '100%',
maxHeight: '100%',
}}
>
<SplitPane
initialSize="400px"
initialClosed={500}
controlledSide="end"
>
<ErrorBoundary
FallbackComponent={ErrorFallback}
onReset={() => {
// reset the state of your app so the error doesn't happen again
}}
>
<div
style={{
padding: 5,
width: '100%',
height: '100%',
}}
>
{measurement ? (
<MeasurementExplorer
measurement={measurement}
width="100%"
height="100%"
kind={
appState.view.selectedKind === 'mass' ? 'mass' : '1d'
}
/>
) : (
<span>No data, add them with drag and drop</span>
)}
</div>
</ErrorBoundary>
<div
style={{
width: '100%',
height: '100%',
flex: '1 1 0%',
}}
>
<Accordion>
<Accordion.Item title="Measurement" defaultOpened>
<div
style={{
flex: '1 1 0%',
width: '100%',
}}
>
<MeasurementsPanel
measurements={appState.data.measurements}
onTabSelect={(kind) => {
dispatch({
type: 'SELECT_MEASUREMENT_KIND',
payload: kind,
});
}}
selectedMeasurement={getSelectedMeasurement(appState)}
onMeasurementSelect={({ measurement, kind }) => {
dispatch({
type: 'SELECT_MEASUREMENT',
payload: { id: measurement.id, kind },
});
}}
/>
</div>
</Accordion.Item>
<Accordion.Item title="Info Panel">
{measurement && (
<MeasurementInfoPanel measurement={measurement} />
)}
</Accordion.Item>
</Accordion>
</div>
</SplitPane>
</div>
</div>
</div>
</DropZoneContainer>
);
}
Loading

0 comments on commit a267efa

Please sign in to comment.