From 604b523f7240332b5e713ddbf8545ef46cc51525 Mon Sep 17 00:00:00 2001 From: plouc Date: Mon, 16 Nov 2020 13:45:30 +0900 Subject: [PATCH] feat(marimekko): fix vertical layout and diverging offset --- packages/marimekko/src/hooks.ts | 20 ++++++++----------- .../marimekko/stories/marimekko.stories.tsx | 14 ++++++++++++- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/packages/marimekko/src/hooks.ts b/packages/marimekko/src/hooks.ts index 9d5f62fdb6..80786cb14b 100644 --- a/packages/marimekko/src/hooks.ts +++ b/packages/marimekko/src/hooks.ts @@ -84,14 +84,11 @@ export const useDimensionsScale = ( layout: Layout ) => useMemo(() => { - const dimensionsScale = scaleLinear().domain([min, max]) if (layout === 'vertical') { - dimensionsScale.range([0, height]) - } else { - dimensionsScale.range([0, width]) + return scaleLinear().domain([max, min]).range([0, height]) } - return dimensionsScale + return scaleLinear().domain([min, max]).range([0, width]) }, [min, max, width, height, layout]) export const useNormalizedData = ( @@ -192,19 +189,18 @@ export const useComputedData = ({ height: 0, } - const position0 = dimensionsScale(Math.min(dimensionPoint[0], dimensionPoint[1])) - const position1 = dimensionsScale(Math.max(dimensionPoint[0], dimensionPoint[1])) - const size = position1 - position0 + const position0 = dimensionsScale(dimensionPoint[0]) + const position1 = dimensionsScale(dimensionPoint[1]) if (layout === 'vertical') { dimensionDatum.x = computedDatum.x - dimensionDatum.y = position0 + dimensionDatum.y = Math.min(position0, position1) dimensionDatum.width = computedDatum.thickness - dimensionDatum.height = size + dimensionDatum.height = Math.max(position0, position1) - dimensionDatum.y } else { - dimensionDatum.x = position0 + dimensionDatum.x = Math.min(position0, position1) dimensionDatum.y = computedDatum.y - dimensionDatum.width = size + dimensionDatum.width = Math.max(position0, position1) - dimensionDatum.x dimensionDatum.height = computedDatum.thickness } diff --git a/packages/marimekko/stories/marimekko.stories.tsx b/packages/marimekko/stories/marimekko.stories.tsx index af4aed92bc..03670c99af 100644 --- a/packages/marimekko/stories/marimekko.stories.tsx +++ b/packages/marimekko/stories/marimekko.stories.tsx @@ -15,6 +15,8 @@ const commonProps = { id: 'id', value: 'value', layout: 'vertical' as Layout, + axisLeft: {}, + axisBottom: {}, dimensions: [ { id: 'cool stuff', @@ -118,5 +120,15 @@ stories.add('diverging', () => { }, ] - return + return ( + Math.abs(v), + }} + /> + ) })