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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Map loader + Chart loader + sourceLink on chart container #44

Merged
merged 7 commits into from
Aug 22, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -55,6 +55,7 @@
"leaflet": "^1.5.1",
"lodash": "^4.17.14",
"prop-types": "^15.7.2",
"react-content-loader": "^4.2.2",
"victory": "^32.3.1"
},
"peerDependencies": {
Expand Down
42 changes: 42 additions & 0 deletions src/A.js
@@ -0,0 +1,42 @@
import React from 'react';
import PropTypes from 'prop-types';

import { Link } from '@material-ui/core';
import { withStyles } from '@material-ui/core/styles';

const styles = () => ({});

function A({ ref, children, className, href, variant, ...props }) {
return (
<Link
ref={ref}
href={href}
target="_blank"
rel="noopener noreferrer"
className={className}
variant={variant}
underline="always"
{...props}
>
{children}
</Link>
);
}
A.propTypes = {
ref: PropTypes.func,
children: PropTypes.oneOfType([
PropTypes.arrayOf(PropTypes.node),
PropTypes.node
]).isRequired,
href: PropTypes.string.isRequired,
className: PropTypes.string,
variant: PropTypes.string
};

A.defaultProps = {
ref: undefined,
className: null,
variant: 'inherit'
};

export default withStyles(styles)(A);
135 changes: 103 additions & 32 deletions src/ChartContainer/index.js
@@ -1,12 +1,15 @@
import React from 'react';
import React, { Fragment } from 'react';
import PropTypes from 'prop-types';

import { makeStyles } from '@material-ui/styles';
import { ButtonBase, Typography } from '@material-ui/core';
import { ButtonBase } from '@material-ui/core';
import Grid from '@material-ui/core/Grid';
import ContentLoader from '../ContentLoader';
import TypographyLoader from '../TypographyLoader';

import infoIcon from '../assets/info.png';
import shareIcon from '../assets/share.png';
import A from '../A';

const useStyles = makeStyles({
root: {
Expand All @@ -16,9 +19,8 @@ const useStyles = makeStyles({
padding: '1.5625rem 1.25rem'
},
content: {
padding: '1.25rem',
width: '100%',
height: '100%'
padding: '1.25rem 0',
overflow: 'hidden'
},
button: {
border: '0.0625rem solid #d8d8d8',
Expand All @@ -29,10 +31,14 @@ const useStyles = makeStyles({
}
},
title: {},
subtitle: {}
subtitle: {},
sourceLink: {}
});

function ChartContainer({
loading,
content,
sourceUrl,
title,
subtitle,
children,
Expand Down Expand Up @@ -64,13 +70,30 @@ function ChartContainer({
justify="space-between"
kilemensi marked this conversation as resolved.
Show resolved Hide resolved
>
<Grid item xs={8}>
<Typography className={classes.title} variant="h5">
<TypographyLoader
loading={loading}
loader={{
primaryOpacity: 0.5,
secondaryOpacity: 1
}}
className={classes.title}
variant="h5"
>
{title}
</Typography>
<Typography className={classes.subtitle} variant="h6">
</TypographyLoader>
<TypographyLoader
loading={loading}
loader={{
primaryOpacity: 0.5,
secondaryOpacity: 1
}}
className={classes.subtitle}
variant="h6"
>
{subtitle}
</Typography>
</TypographyLoader>
</Grid>

<Grid
item
xs={4}
Expand All @@ -79,29 +102,65 @@ function ChartContainer({
direction="row"
justify="flex-end"
>
<ButtonBase
className={classes.button}
onClick={() =>
onClickInfo && onClickInfo(getReferenceObject(infoRef))
}
ref={infoRef}
>
<img alt="Info" src={infoIcon} />
</ButtonBase>
<ButtonBase
className={classes.button}
onClick={() =>
onClickShare && onClickShare(getReferenceObject(shareRef))
}
ref={shareRef}
>
<img alt="Share" src={shareIcon} />
</ButtonBase>
{loading ? (
<ContentLoader
primaryOpacity={0.5}
secondaryOpacity={1}
width="5rem"
height="2.5rem"
>
<rect x="0" y="0" width="100%" height="100%" />
</ContentLoader>
) : (
<Fragment>
<ButtonBase
className={classes.button}
onClick={() =>
onClickInfo && onClickInfo(getReferenceObject(infoRef))
}
ref={infoRef}
>
<img alt="Info" src={infoIcon} />
</ButtonBase>
<ButtonBase
className={classes.button}
onClick={() =>
onClickShare && onClickShare(getReferenceObject(shareRef))
}
ref={shareRef}
>
<img alt="Share" src={shareIcon} />
</ButtonBase>
</Fragment>
)}
</Grid>
</Grid>
<Grid container justify="center">
<div className={classes.content}>{children}</div>
<Grid
container
justify="center"
className={classes.content}
style={{ width: content.width, height: content.height }}
>
{loading ? (
<ContentLoader primaryOpacity={0.5} secondaryOpacity={1}>
<rect x="0" y="0" width="100%" height="100%" />
</ContentLoader>
) : (
children
)}
</Grid>
<TypographyLoader
loading={loading}
loader={{
primaryOpacity: 0.5,
secondaryOpacity: 1
}}
component="span"
>
<A className={classes.sourceLink} href={sourceUrl}>
{sourceUrl}
</A>
</TypographyLoader>
</Grid>
);
}
Expand All @@ -114,12 +173,24 @@ ChartContainer.propTypes = {
onClickInfo: PropTypes.func,
onClickShare: PropTypes.func,
subtitle: PropTypes.string.isRequired,
title: PropTypes.string.isRequired
title: PropTypes.string.isRequired,
sourceUrl: PropTypes.string,
loading: PropTypes.bool,
content: PropTypes.shape({
width: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
height: PropTypes.oneOfType([PropTypes.number, PropTypes.string])
})
};

ChartContainer.defaultProps = {
sourceUrl: undefined,
onClickInfo: undefined,
onClickShare: undefined
onClickShare: undefined,
loading: false,
content: {
width: '100%',
height: '100%'
}
};

export default ChartContainer;
35 changes: 35 additions & 0 deletions src/ContentLoader.js
@@ -0,0 +1,35 @@
import React from 'react';
import PropTypes from 'prop-types';
import ContentLoader from 'react-content-loader';

export default function CustomContentLoader({
children,
height,
width,
...props
}) {
return (
<ContentLoader
primaryOpacity={0.01}
secondaryOpacity={0.1}
width="100%"
height="100%"
viewBox={`0 0 ${width} ${height}`}
style={{ width, height }}
{...props}
>
{children}
</ContentLoader>
);
}

CustomContentLoader.propTypes = {
children: PropTypes.shape().isRequired,
width: PropTypes.oneOf([PropTypes.string, PropTypes.number]),
height: PropTypes.oneOf([PropTypes.string, PropTypes.number])
};

CustomContentLoader.defaultProps = {
width: '100%',
height: '100%'
};
31 changes: 29 additions & 2 deletions src/MapIt/index.js
@@ -1,10 +1,17 @@
import React, { useEffect, useRef, useCallback, useState } from 'react';
import React, {
useEffect,
useRef,
useCallback,
useState,
Fragment
} from 'react';
import PropTypes from 'prop-types';

import leaflet from 'leaflet';
import { makeStyles } from '@material-ui/core';

import 'leaflet/dist/leaflet.css';
import ContentLoader from '../ContentLoader';
import useDeepRef from './useDeepRef';

const useStyles = makeStyles({
Expand Down Expand Up @@ -291,7 +298,27 @@ function MapIt({
]);
const classes = useStyles();

return <div id={mapId} className={classes.root} />;
return (
<Fragment>
{!featuresToDraw && (
<div className={classes.root}>
<ContentLoader
width="100%"
height="100%"
primaryOpacity={0.5}
secondaryOpacity={1}
>
<rect x="0" y="0" width="100%" height="100%" />
</ContentLoader>
</div>
)}
<div
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This couldn't be a <BlockLoader ...><div ... /></BlockLoader>

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't do this because of the root div.
Also because initial loading will look for div with mapId.
I will track this am make enhancement to the code later.

id={mapId}
className={classes.root}
style={{ display: !featuresToDraw ? 'hidden' : 'block' }}
/>
</Fragment>
);
}

MapIt.propTypes = {
Expand Down
3 changes: 0 additions & 3 deletions src/Theme.js

This file was deleted.

49 changes: 49 additions & 0 deletions src/TypographyLoader.js
@@ -0,0 +1,49 @@
import React, { useRef, useEffect, useState } from 'react';
import { Typography } from '@material-ui/core';
import PropTypes from 'prop-types';
import ContentLoader from './ContentLoader';

export default function TypographyLoader({
loading,
loader,
children,
...props
}) {
const ref = useRef();
const [height, setHeight] = useState();
useEffect(() => {
if (ref.current) {
const typography = ref.current;
const style = window.getComputedStyle(typography);
setHeight(style.lineHeight);
}
}, []);
return (
<Typography ref={ref} {...props}>
{loading ? (
<ContentLoader style={{ height, width: '100%' }} {...loader}>
<rect x="0" y="0" width="100%" height="100%" />
</ContentLoader>
) : (
children
)}
</Typography>
);
}

TypographyLoader.propTypes = {
children: PropTypes.shape().isRequired,
loading: PropTypes.bool,
loader: PropTypes.shape({
width: PropTypes.oneOf([PropTypes.number, PropTypes.string]).isRequired,
height: PropTypes.oneOf([PropTypes.number, PropTypes.string]).isRequired
})
};

TypographyLoader.defaultProps = {
loading: false,
loader: {
width: '100%',
height: '20px'
}
};
3 changes: 3 additions & 0 deletions src/index.js
Expand Up @@ -12,3 +12,6 @@ export { default as ComparisonBarChart } from './ComparisonBarChart';
export { default as ChartContainer } from './ChartContainer';
export { default as EmbedPopup } from './ChartContainer/EmbedPopup';
export { default as InfoPopup } from './ChartContainer/InfoPopup';
export { default as ContentLoader } from './ContentLoader';
export { default as TypographyLoader } from './TypographyLoader';
export { default as A } from './A';
3 changes: 3 additions & 0 deletions stories/Theme.js
@@ -0,0 +1,3 @@
import createTheme from '../src/styles/createTheme';

export default createTheme();