@onesy/ui-react
Version:
UI for React
206 lines (201 loc) • 8.08 kB
JavaScript
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
const _excluded = ["ref", "gap", "columns", "className", "style", "noMasonry", "NoMasonryProps", "children"];
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
import React from 'react';
import { is, unique } from '@onesy/utils';
import { classNames, style as styleMethod, useOnesyTheme } from '@onesy/style-react';
import LineElement from '../Line';
import useMediaQuery from '../useMediaQuery';
import { valueBreakpoints, staticClassName } from '../utils';
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
const useStyle = styleMethod(theme => ({
root: {
position: 'relative'
}
}), {
name: 'onesy-Masonry'
});
const Masonry = props_ => {
const theme = useOnesyTheme();
const props = _objectSpread(_objectSpread(_objectSpread({}, theme?.ui?.elements?.all?.props?.default), theme?.ui?.elements?.onesyMasonry?.props?.default), props_);
const Line = theme?.elements?.Line || LineElement;
const {
ref,
gap: gap_,
columns: columns_,
className,
style,
noMasonry,
NoMasonryProps,
children: children_
} = props,
other = _objectWithoutProperties(props, _excluded);
const {
classes
} = useStyle();
const [init, setInit] = React.useState(false);
const refs = {
root: React.useRef(undefined),
gap: React.useRef(undefined),
columns: React.useRef(undefined),
init: React.useRef(undefined),
useMasonry: React.useRef(undefined)
};
const keys = React.useMemo(() => {
const result = [];
const items = [gap_, columns_];
items.forEach(item => {
if (is('object', item)) Object.keys(item).filter(key => theme.breakpoints.media[key]).forEach(key_0 => result.push(key_0));
});
return unique(result);
}, [gap_, columns_]);
const breakpoints = {};
keys.forEach(key_1 => {
breakpoints[key_1] = useMediaQuery(theme.breakpoints.media[key_1], {
element: refs.root.current
});
});
refs.init.current = init;
const styles = {
root: {},
item: {}
};
const gap = valueBreakpoints(gap_, 2, breakpoints, theme) || 0;
const columns = valueBreakpoints(columns_, {
400: 1,
500: 2,
700: 3,
1100: 4,
1400: 5,
1700: 6,
2000: 7,
default: 4
}, breakpoints, theme);
refs.gap.current = gap;
refs.columns.current = columns;
const children = React.Children.toArray(children_);
let useMasonry = true;
if (columns === 1 || children.length < columns) useMasonry = false;
if (noMasonry) useMasonry = false;
refs.useMasonry.current = useMasonry;
const update = () => {
if (!refs.useMasonry.current) {
if (refs.root.current) {
refs.root.current.style.removeProperty('height');
}
return;
}
if (refs.root.current) {
// Get all children
const elements = Array.from(refs.root.current.children).slice(0, refs.columns.current > 1 ? -(refs.columns.current - 1) : undefined);
const width = refs.columns.current === 1 ? '100%' : `calc(${100 / refs.columns.current}% - ${gap * theme.space.unit * (refs.columns.current - 1) / refs.columns.current}px)`;
const columns__ = {};
const order = {};
for (let i = 1; i < refs.columns.current + 1; i++) columns__[i] = 0;
// order them by adding to lowest available order value
let lowestColumn = 1;
let lowestValue = Number.MAX_SAFE_INTEGER;
let highestValue = Number.MIN_SAFE_INTEGER;
elements.forEach((element, index) => {
// Width
element.style.width = width;
// Update lowest column
lowestValue = Number.MAX_SAFE_INTEGER;
Object.keys(columns__).forEach(column => {
if (columns__[column] < lowestValue) {
lowestColumn = +column;
lowestValue = columns__[column];
}
});
const addition = columns__[lowestColumn] === 0 ? 0 : refs.gap.current * theme.space.unit;
columns__[lowestColumn] += element.clientHeight + addition;
order[index] = lowestColumn;
// Order
element.style.order = String(lowestColumn);
});
// update height (biggest column height)
Object.keys(columns__).forEach(column_0 => {
if (columns__[column_0] > highestValue) highestValue = columns__[column_0];
});
// Height
const height = highestValue + gap * theme.space.unit;
refs.root.current.style.height = `${height}px`;
if (refs.init.current !== 'animated') setInit(true);
}
};
const method = update;
React.useEffect(() => {
setTimeout(update);
const observer = new ResizeObserver(method);
[refs.root.current, ...Array.from(refs.root.current.children)].forEach(item_0 => observer.observe(item_0));
return () => {
// Clean up
observer.disconnect();
};
}, []);
React.useEffect(method, [children.length, gap, columns]);
styles.item.margin = 0;
styles.root.gap = 'unset';
styles.root.rowGap = `${gap * theme.space.unit}px`;
styles.root.columnGap = `${gap / 2 * theme.space.unit}px`;
if (!init) {
styles.root.position = 'fixed';
styles.root.visibility = 'hidden';
}
if (!useMasonry) {
styles.item.width = columns === 1 ? '100%' : `calc(${100 / columns}% - ${gap * theme.space.unit * (columns - 1) / columns}px)`;
return /*#__PURE__*/_jsx(Line, _objectSpread(_objectSpread(_objectSpread({
ref: item_1 => {
if (ref) {
if (is('function', ref)) ref(item_1);else ref.current = item_1;
}
refs.root.current = item_1;
},
gap: gap,
direction: "row",
wrap: "wrap",
fullWidth: true
}, NoMasonryProps), {}, {
className: classNames([staticClassName('Masonry', theme) && ['onesy-Masonry-root', 'onesy-Masonry-noMasonry'], NoMasonryProps?.className, className, classes.root]),
style: _objectSpread(_objectSpread({}, style), {}, {
height: 'auto'
}, NoMasonryProps?.style)
}, other), {}, {
children: children.map((item_2, index_0) => (/*#__PURE__*/React.cloneElement(item_2, {
key: index_0,
style: _objectSpread(_objectSpread({}, styles.item), item_2.props.style)
})))
}));
}
return /*#__PURE__*/_jsxs(Line, _objectSpread(_objectSpread({
ref: item_3 => {
if (ref) {
if (is('function', ref)) ref(item_3);else ref.current = item_3;
}
refs.root.current = item_3;
},
gap: gap,
wrap: "wrap",
align: "unset",
justify: "unset",
direction: "column",
fullWidth: true,
className: classNames([staticClassName('Masonry', theme) && ['onesy-Masonry-root'], className, classes.root]),
style: _objectSpread(_objectSpread({}, styles.root), style)
}, other), {}, {
children: [React.Children.toArray(children).map((item_4, index_1) => (/*#__PURE__*/React.cloneElement(item_4, {
key: index_1,
style: _objectSpread(_objectSpread({}, styles.item), item_4.props.style)
}))), columns > 1 && new Array(columns - 1).fill(true).map((item_5, index_2) => /*#__PURE__*/_jsx("div", {
className: classNames([staticClassName('Masonry', theme) && ['onesy-Masonry-line-break']]),
style: {
flex: '1 1 100%',
order: index_2 + 1
}
}, index_2))]
}));
};
Masonry.displayName = 'onesy-Masonry';
export default Masonry;