From 40905017318e1dd9321b5a919d42d3a82c5b5ab0 Mon Sep 17 00:00:00 2001 From: Harry Shoff Date: Wed, 8 Nov 2017 14:49:27 -0800 Subject: [PATCH 1/4] [responsive] add + resize-observer-polyfill --- packages/vx-responsive/package.json | 20 ++----- .../src/components/ParentSIze.js | 57 +++++++++++++++++++ packages/vx-responsive/src/index.js | 9 ++- .../vx-responsive/test/ParentSize.test.js | 7 +++ 4 files changed, 76 insertions(+), 17 deletions(-) create mode 100644 packages/vx-responsive/src/components/ParentSIze.js create mode 100644 packages/vx-responsive/test/ParentSize.test.js diff --git a/packages/vx-responsive/package.json b/packages/vx-responsive/package.json index d1af44be5..990edf298 100644 --- a/packages/vx-responsive/package.json +++ b/packages/vx-responsive/package.json @@ -3,9 +3,7 @@ "version": "0.0.143", "description": "vx responsive svg", "main": "build/index.js", - "files": [ - "build" - ], + "files": ["build"], "scripts": { "build": "make build SRC=./src", "prepublish": "make build SRC=./src", @@ -15,13 +13,7 @@ "type": "git", "url": "git+https://github.com/hshoff/vx.git" }, - "keywords": [ - "vx", - "react", - "d3", - "visualizations", - "charts" - ], + "keywords": ["vx", "react", "d3", "visualizations", "charts"], "author": "@hshoff", "license": "MIT", "bugs": { @@ -42,7 +34,8 @@ "regenerator-runtime": "^0.10.5" }, "dependencies": { - "lodash": "^4.0.8" + "lodash": "4.0.8", + "resize-observer-polyfill": "1.4.2" }, "peerDependencies": { "react": "^15.0.0-0 || ^16.0.0-0" @@ -51,9 +44,6 @@ "access": "public" }, "jest": { - "setupFiles": [ - "raf/polyfill", - "/test/enzyme-setup.js" - ] + "setupFiles": ["raf/polyfill", "/test/enzyme-setup.js"] } } diff --git a/packages/vx-responsive/src/components/ParentSIze.js b/packages/vx-responsive/src/components/ParentSIze.js new file mode 100644 index 000000000..b7a44e7af --- /dev/null +++ b/packages/vx-responsive/src/components/ParentSIze.js @@ -0,0 +1,57 @@ +import React from 'react'; +import PropTypes from 'prop-types'; +import ResizeObserver from 'resize-observer-polyfill'; + +export default class ParentSize extends React.Component { + constructor(props) { + super(props); + this.state = {}; + this.resize = this.resize.bind(this); + this.setTarget = this.setTarget.bind(this); + } + componentDidMount() { + this.ro = new ResizeObserver((entries, observer) => { + for (const entry of entries) { + const { left, top, width, height } = entry.contentRect; + this.resize({ + width, + height, + top, + left, + }); + } + }); + this.ro.observe(this.target); + } + componentWillUnmount() { + this.ro.disconnect(); + } + resize({ width, height, top, left }) { + this.setState(() => ({ + width, + height, + top, + left, + })); + } + setTarget(ref) { + this.target = ref; + } + render() { + const { className, children } = this.props; + return ( +
+ {children({ + ...this.state, + ref: this.target, + resize: this.resize, + })} +
+ ); + } +} + +ParentSize.propTypes = { + className: PropTypes.string, + children: PropTypes.func.isRequired, +}; diff --git a/packages/vx-responsive/src/index.js b/packages/vx-responsive/src/index.js index d28c0b69e..226be3340 100644 --- a/packages/vx-responsive/src/index.js +++ b/packages/vx-responsive/src/index.js @@ -1,3 +1,8 @@ export { default as ScaleSVG } from './components/ScaleSVG'; -export { default as withParentSize } from './enhancers/withParentSize'; -export { default as withScreenSize } from './enhancers/withScreenSize'; +export { default as ParentSize } from './components/ParentSize'; +export { + default as withParentSize, +} from './enhancers/withParentSize'; +export { + default as withScreenSize, +} from './enhancers/withScreenSize'; diff --git a/packages/vx-responsive/test/ParentSize.test.js b/packages/vx-responsive/test/ParentSize.test.js new file mode 100644 index 000000000..8f06130b5 --- /dev/null +++ b/packages/vx-responsive/test/ParentSize.test.js @@ -0,0 +1,7 @@ +import { ParentSize } from '../src'; + +describe('', () => { + test('it should be defined', () => { + expect(ParentSize).toBeDefined(); + }); +}); From 1c5a13bd7e875279a99b4840b4c230dc7c27d40a Mon Sep 17 00:00:00 2001 From: Harry Shoff Date: Wed, 8 Nov 2017 16:06:59 -0800 Subject: [PATCH 2/4] [demo] use for responsive tiles --- packages/vx-demo/components/gallery.js | 381 +++++++++++------- packages/vx-demo/components/show.js | 4 +- packages/vx-demo/components/tiles/lines.js | 44 +- packages/vx-responsive/package.json | 2 +- .../{ParentSIze.js => ParentSize.js} | 8 +- 5 files changed, 264 insertions(+), 175 deletions(-) rename packages/vx-responsive/src/components/{ParentSIze.js => ParentSize.js} (86%) diff --git a/packages/vx-demo/components/gallery.js b/packages/vx-demo/components/gallery.js index a72c49662..291176a4a 100644 --- a/packages/vx-demo/components/gallery.js +++ b/packages/vx-demo/components/gallery.js @@ -1,6 +1,7 @@ import React from 'react'; import Tilt from 'react-tilt'; import Link from 'next/link'; +import { ParentSize } from '@vx/responsive'; import { extent, max } from 'd3-array'; import Page from '../components/page'; @@ -49,59 +50,12 @@ const items = [ ]; export default class Gallery extends React.Component { - constructor() { - super(); - this.nodes = new Set(); - this.state = { dimensions: [] }; - this.resize = this.resize.bind(this); - } - - componentDidMount() { - window.addEventListener('resize', this.resize, false); - setTimeout(() => { - this.resize(); - }, 1); - } - - componentWillUnmount() { - window.removeEventListener('resize', this.resize); - } - - resize() { - const newState = []; - this.nodes.forEach(node => { - if (!node) return; - newState.push([node.offsetWidth, node.clientHeight]); - }); - this.setState({ dimensions: newState }); + constructor(props) { + super(props); } render() { - const t1 = this.state.dimensions[0] || [8, 300]; - const t2 = this.state.dimensions[1] || [8, 300]; - const t3 = this.state.dimensions[2] || [8, 300]; - const t4 = this.state.dimensions[3] || [8, 300]; - const t5 = this.state.dimensions[4] || [8, 300]; - const t6 = this.state.dimensions[5] || [8, 300]; - const t7 = this.state.dimensions[6] || [8, 300]; - const t8 = this.state.dimensions[7] || [8, 300]; - const t9 = this.state.dimensions[8] || [8, 300]; - const t10 = this.state.dimensions[9] || [8, 300]; - const t11 = this.state.dimensions[10] || [8, 300]; - const t12 = this.state.dimensions[11] || [8, 300]; - const t13 = this.state.dimensions[12] || [8, 300]; - const t14 = this.state.dimensions[13] || [8, 300]; - const t15 = this.state.dimensions[14] || [8, 300]; - const t16 = this.state.dimensions[15] || [8, 300]; - const t17 = this.state.dimensions[16] || [8, 300]; - const t18 = this.state.dimensions[17] || [8, 300]; - const t19 = this.state.dimensions[18] || [8, 300]; - const t20 = this.state.dimensions[19] || [8, 300]; - const t21 = this.state.dimensions[20] || [8, 300]; - const t22 = this.state.dimensions[21] || [8, 300]; - const t23 = this.state.dimensions[22] || [8, 300]; - const t24 = this.state.dimensions[23] || [8, 300]; - + const detailsHeight = 76; return (
@@ -110,10 +64,16 @@ export default class Gallery extends React.Component {
this.nodes.add(d)} >
- + + {({ width, height }) => ( + + )} +
Lines
@@ -129,10 +89,16 @@ export default class Gallery extends React.Component {
this.nodes.add(d)} >
- + + {({ width, height }) => ( + + )} +
Bars
@@ -148,10 +114,16 @@ export default class Gallery extends React.Component {
this.nodes.add(d)} >
- + + {({ width, height }) => ( + + )} +
this.nodes.add(d)} >
- + + {({ width, height }) => ( + + )} +
Patterns
@@ -189,19 +167,22 @@ export default class Gallery extends React.Component {
this.nodes.add(d)} >
- + + {({ width, height }) => ( + + )} +
Areas
@@ -217,19 +198,22 @@ export default class Gallery extends React.Component {
this.nodes.add(d)} >
- + + {({ width, height }) => ( + + )} +
this.nodes.add(d)} >
- + + {({ width, height }) => ( + + )} +
Gradients
@@ -270,19 +266,22 @@ export default class Gallery extends React.Component {
this.nodes.add(d)} >
- + + {({ width, height }) => ( + + )} +
this.nodes.add(d)} >
- + + {({ width, height }) => ( + + )} +
Axis
@@ -330,10 +332,16 @@ export default class Gallery extends React.Component {
this.nodes.add(d)} >
- + + {({ width, height }) => ( + + )} +
Bar Group
@@ -349,10 +357,16 @@ export default class Gallery extends React.Component {
this.nodes.add(d)} >
- + + {({ width, height }) => ( + + )} +
this.nodes.add(d)} >
- + + {({ width, height }) => ( + + )} +
this.nodes.add(d)} >
- + + {({ width, height }) => ( + + )} +
Radial Lines
@@ -412,10 +435,16 @@ export default class Gallery extends React.Component {
this.nodes.add(d)} >
- + + {({ width, height }) => ( + + )} +
Pies
@@ -431,10 +460,16 @@ export default class Gallery extends React.Component {
this.nodes.add(d)} >
- + + {({ width, height }) => ( + + )} +
Trees
@@ -451,10 +486,16 @@ export default class Gallery extends React.Component {
this.nodes.add(d)} >
- + + {({ width, height }) => ( + + )} +
Dendrograms
@@ -471,7 +512,6 @@ export default class Gallery extends React.Component {
this.nodes.add(d)} >
@@ -489,7 +529,6 @@ export default class Gallery extends React.Component {
this.nodes.add(d)} style={{ boxShadow: 'rgba(0, 0, 0, 0.1) 0px 1px 6px', }} @@ -501,7 +540,14 @@ export default class Gallery extends React.Component { borderRadius: 14, }} > - + + {({ width, height }) => ( + + )} +
Voronoi
@@ -517,11 +563,17 @@ export default class Gallery extends React.Component {
this.nodes.add(d)} - style={{ background: '#fd7e14' }} + style={{ background: '#88d1d9' }} >
- + + {({ width, height }) => ( + + )} +
-
this.nodes.add(d)} - > +
- + + {({ width, height }) => ( + + )} +
Geo
@@ -557,12 +613,16 @@ export default class Gallery extends React.Component { -
this.nodes.add(d)} - > +
- + + {({ width, height }) => ( + + )} +
this.nodes.add(d)} style={{ background: '#ffd7d9' }} >
- + + {({ width, height }) => ( + + )} +
this.nodes.add(d)} style={{ background: '#ffffff', boxShadow: 'rgba(0, 0, 0, 0.1) 0px 1px 6px', }} >
- + + {({ width, height }) => ( + + )} +
this.nodes.add(d)} style={{ background: '#3436b8', }} >
- + + {({ width, height }) => ( + + )} +
this.nodes.add(d)} style={{ background: '#FAF7E9', }} >
- + + {({ width, height }) => ( + + )} +
this.nodes.add(d)} style={{ background: '#FAF7E9', }} >
- + + {({ width, height }) => ( + + )} +
{ const padding = 40; - const height = screenHeight * 0.6; let width = screenWidth - padding; if (width > 800) width = 800; + const height = width * 0.6; return ( @@ -64,7 +64,7 @@ export default withScreenSize( .container h1 { margin-top: 15px; line-height: 0.9em; - letter-spacing: -.03em; + letter-spacing: -0.03em; } .container h2 { margin-top: 15px; diff --git a/packages/vx-demo/components/tiles/lines.js b/packages/vx-demo/components/tiles/lines.js index db392bb3f..73664d051 100644 --- a/packages/vx-demo/components/tiles/lines.js +++ b/packages/vx-demo/components/tiles/lines.js @@ -9,22 +9,19 @@ import { extent, max } from 'd3-array'; function genLines(num) { return new Array(num).fill(1).map(() => { return genDateValue(25); - }) + }); } const series = genLines(12); const data = series.reduce((rec, d) => { - return rec.concat(d) + return rec.concat(d); }, []); // accessors const x = d => d.date; const y = d => d.value; -export default ({ - width, - height, -}) => { +export default ({ width, height }) => { // bounds const xMax = width; const yMax = height / 8; @@ -49,22 +46,23 @@ export default ({ fill="#242424" rx={14} /> - {xMax > 8 && series.map((d, i) => { - return ( - - - - ); - })} + {xMax > 8 && + series.map((d, i) => { + return ( + + + + ); + })} ); -} +}; diff --git a/packages/vx-responsive/package.json b/packages/vx-responsive/package.json index 990edf298..df2d998c5 100644 --- a/packages/vx-responsive/package.json +++ b/packages/vx-responsive/package.json @@ -34,7 +34,7 @@ "regenerator-runtime": "^0.10.5" }, "dependencies": { - "lodash": "4.0.8", + "lodash": "^4.0.8", "resize-observer-polyfill": "1.4.2" }, "peerDependencies": { diff --git a/packages/vx-responsive/src/components/ParentSIze.js b/packages/vx-responsive/src/components/ParentSize.js similarity index 86% rename from packages/vx-responsive/src/components/ParentSIze.js rename to packages/vx-responsive/src/components/ParentSize.js index b7a44e7af..a04b71c9f 100644 --- a/packages/vx-responsive/src/components/ParentSIze.js +++ b/packages/vx-responsive/src/components/ParentSize.js @@ -5,7 +5,7 @@ import ResizeObserver from 'resize-observer-polyfill'; export default class ParentSize extends React.Component { constructor(props) { super(props); - this.state = {}; + this.state = { width: 0, height: 0, top: 0, left: 0 }; this.resize = this.resize.bind(this); this.setTarget = this.setTarget.bind(this); } @@ -40,7 +40,11 @@ export default class ParentSize extends React.Component { render() { const { className, children } = this.props; return ( -
+
{children({ ...this.state, ref: this.target, From 67e2eb35792d8da8765f1943f8e764d6944a9825 Mon Sep 17 00:00:00 2001 From: Harry Shoff Date: Mon, 13 Nov 2017 12:22:41 -0800 Subject: [PATCH 3/4] [responsive] add ParentSize component --- packages/vx-demo/components/gallery.js | 42 ++++++- packages/vx-demo/components/tiles/axis.js | 1 - .../vx-demo/components/tiles/gradients.js | 13 ++- .../vx-demo/components/tiles/responsive.js | 100 ++++++++++++++++ packages/vx-demo/pages/axis.js | 1 - packages/vx-demo/pages/responsive.js | 108 ++++++++++++++++++ 6 files changed, 258 insertions(+), 7 deletions(-) create mode 100644 packages/vx-demo/components/tiles/responsive.js create mode 100644 packages/vx-demo/pages/responsive.js diff --git a/packages/vx-demo/components/gallery.js b/packages/vx-demo/components/gallery.js index 291176a4a..305eb5445 100644 --- a/packages/vx-demo/components/gallery.js +++ b/packages/vx-demo/components/gallery.js @@ -33,6 +33,7 @@ import Streamgraph from '../components/tiles/streamgraph'; import Pack from '../components/tiles/pack'; import Treemap from '../components/tiles/treemap'; import Radar from '../components/tiles/radar'; +import Responsive from '../components/tiles/responsive'; const items = [ '#242424', @@ -53,7 +54,6 @@ export default class Gallery extends React.Component { constructor(props) { super(props); } - render() { const detailsHeight = 76; return ( @@ -797,7 +797,45 @@ export default class Gallery extends React.Component {
-
+ + +
+
+ + {({ width, height }) => ( + + )} + +
+
+
Responsive
+
+
{``}
+
+
+
+ +
+ {false &&
}
diff --git a/packages/vx-demo/components/tiles/axis.js b/packages/vx-demo/components/tiles/axis.js index ab0ca96bb..7854b4168 100644 --- a/packages/vx-demo/components/tiles/axis.js +++ b/packages/vx-demo/components/tiles/axis.js @@ -132,7 +132,6 @@ export default ({ width, height, margin }) => { label="time" > {props => { - console.log('Custom AxisBottom props', props); const tickLabelSize = 10; const tickRotate = 45; const tickColor = '#8e205f'; diff --git a/packages/vx-demo/components/tiles/gradients.js b/packages/vx-demo/components/tiles/gradients.js index 8196dae7e..69c32c79e 100644 --- a/packages/vx-demo/components/tiles/gradients.js +++ b/packages/vx-demo/components/tiles/gradients.js @@ -24,8 +24,10 @@ export default ({ bottom: 80, }, }) => { - const w = width / 4; - const h = (height - margin.bottom) / 2; + let w = width / 4; + let h = (height - margin.bottom) / 2; + w = w < 0 ? 0 : w; + h = h < 0 ? 0 : h; return ( @@ -35,7 +37,12 @@ export default ({ - + +
  • 🤖
  • +
  • Home
  • +
  • Profile
  • +
  • Favorites
  • +
  • Settings
  • + + ); +} + +export default class App extends React.Component { + constructor(props) { + super(props); + this.state = { showNav: true }; + this.toggleNav = this.toggleNav.bind(this); + } + toggleNav(event) { + event.preventDefault(); + event.stopPropagation(); + this.setState(prevState => { + return { + showNav: !prevState.showNav, + }; + }); + } + render() { + const { width, height } = this.props; + if (width < 20) return null; + if (height < 20) return null; + return ( +
    +
    +
    +
    +
    + +
    +
    + + {({ width: w, height: h }) => { + return ( + + ); + }} + +
    +
    + + +
    + ); + } +} diff --git a/packages/vx-demo/pages/axis.js b/packages/vx-demo/pages/axis.js index e480d5244..bba888349 100644 --- a/packages/vx-demo/pages/axis.js +++ b/packages/vx-demo/pages/axis.js @@ -148,7 +148,6 @@ export default ({ width, height, margin }) => { label="time" > {props => { - console.log('Custom AxisBottom props', props); const tickLabelSize = 10; const tickRotate = 45; const tickColor = '#8e205f'; diff --git a/packages/vx-demo/pages/responsive.js b/packages/vx-demo/pages/responsive.js new file mode 100644 index 000000000..dff34bf0d --- /dev/null +++ b/packages/vx-demo/pages/responsive.js @@ -0,0 +1,108 @@ +import React from 'react'; +import Show from '../components/show'; +import Responsive from '../components/tiles/responsive'; + +export default () => { + return ( + + {`import React from 'react'; +import { ParentSize } from '@vx/responsive'; +import Treemap from './treemap'; + +function Nav() { + return ( +
      +
    • 🤖
    • +
    • Home
    • +
    • Profile
    • +
    • Favorites
    • +
    • Settings
    • +
    + ); +} + +export default class App extends React.Component { + constructor(props) { + super(props); + this.state = { showNav: true }; + this.toggleNav = this.toggleNav.bind(this); + } + toggleNav() { + this.setState(prevState => { + return { + showNav: !prevState.showNav, + }; + }); + } + render() { + const { width, height } = this.props; + return ( +
    +
    +
    +
    +
    + +
    +
    + + {({ width: w, height: h }) => { + return ( + + ); + }} + +
    +
    + + +
    + ); + } +}`} +
    + ); +}; From c0b9df4ed6362f25032dd0d5258cf664eb26c6c8 Mon Sep 17 00:00:00 2001 From: Harry Shoff Date: Mon, 13 Nov 2017 12:29:00 -0800 Subject: [PATCH 4/4] [demo] responsive treemap => lines --- packages/vx-demo/components/tiles/responsive.js | 4 ++-- packages/vx-demo/pages/responsive.js | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/vx-demo/components/tiles/responsive.js b/packages/vx-demo/components/tiles/responsive.js index 2e3df3df1..ba5541fed 100644 --- a/packages/vx-demo/components/tiles/responsive.js +++ b/packages/vx-demo/components/tiles/responsive.js @@ -1,6 +1,6 @@ import React from 'react'; import { ParentSize } from '@vx/responsive'; -import Treemap from './lines'; +import Lines from './lines'; function Nav() { return ( @@ -51,7 +51,7 @@ export default class App extends React.Component { {({ width: w, height: h }) => { return ( - { {`import React from 'react'; import { ParentSize } from '@vx/responsive'; -import Treemap from './treemap'; +import Lines from './lines'; function Nav() { return ( @@ -54,7 +54,7 @@ export default class App extends React.Component { {({ width: w, height: h }) => { return ( -