Skip to content

Commit

Permalink
Flex direction-specific property access now in adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
outoftime committed May 27, 2018
1 parent 7e6a73d commit 6211922
Showing 1 changed file with 33 additions and 11 deletions.
44 changes: 33 additions & 11 deletions src/util/resizableFlex.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,23 @@ import times from 'lodash-es/times';
import {makeGetResizableFlex} from '../selectors';
import {updateFlexGrow} from '../actions';

const directionAdapters = {
column: {
getCurrentSize(element) {
return element.offsetHeight;
},

getDesiredSize(element, {y}) {
return y - element.offsetTop;
},
},
};

function directionAdapterFor({parentNode}) {
const flexDirection = getComputedStyle(parentNode)['flex-direction'];
return directionAdapters[flexDirection];
}

function calculateFlexGrowAfterDrag(
{
currentFlexGrow: currentBeforeFlexGrow,
Expand Down Expand Up @@ -58,14 +75,17 @@ export default function resizableFlex(size) {
const [{current: before}, {current: after}] =
at(regions, [beforeIndex, afterIndex]);

const {getCurrentSize, getDesiredSize} =
directionAdapterFor(before);

const [desiredBeforeFlex, desiredAfterFlex] =
calculateFlexGrowAfterDrag(
{
currentFlexGrow: Number(
getComputedStyle(before)['flex-grow'],
),
currentSize: before.offsetHeight,
desiredSize: payload.y - before.offsetTop,
currentSize: getCurrentSize(before),
desiredSize: getDesiredSize(before, payload),
initialMainSize: initialMainSizes[beforeIndex],
},
{
Expand All @@ -88,18 +108,20 @@ export default function resizableFlex(size) {

flexRefs: map(
regions,
(region, index) => (ref) => {
region.current = ref;
if (isNull(ref)) {
(region, index) => (element) => {
region.current = element;
if (isNull(element)) {
initialMainSizes[index] = null;
return;
}
const flexGrowWas = ref.style.flexGrow;
const flexShrinkWas = ref.style.flexShrink;
ref.style.flexGrow = ref.style.flexShrink = '0';
initialMainSizes[index] = ref.offsetHeight;
ref.style.flexGrow = flexGrowWas;
ref.style.flexShrink = flexShrinkWas;

const flexGrowWas = element.style.flexGrow;
const flexShrinkWas = element.style.flexShrink;
element.style.flexGrow = element.style.flexShrink = '0';
initialMainSizes[index] = directionAdapterFor(element).
getCurrentSize(element);
element.style.flexGrow = flexGrowWas;
element.style.flexShrink = flexShrinkWas;
},
),

Expand Down

0 comments on commit 6211922

Please sign in to comment.