UNPKG

@amaui/ui-react

Version:
190 lines (189 loc) 9.92 kB
"use strict"; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const jsx_runtime_1 = require("react/jsx-runtime"); const react_1 = __importDefault(require("react")); const utils_1 = require("@amaui/utils"); const style_react_1 = require("@amaui/style-react"); const Line_1 = __importDefault(require("../Line")); const useMediaQuery_1 = __importDefault(require("../useMediaQuery")); const utils_2 = require("../utils"); const useStyle = (0, style_react_1.style)(theme => ({ root: { position: 'relative' } }), { name: 'amaui-Masonry' }); const Masonry = react_1.default.forwardRef((props_, ref) => { const theme = (0, style_react_1.useAmauiTheme)(); const props = react_1.default.useMemo(() => { var _a, _b, _c, _d, _e, _f, _g, _h; return (Object.assign(Object.assign(Object.assign({}, (_d = (_c = (_b = (_a = theme === null || theme === void 0 ? void 0 : theme.ui) === null || _a === void 0 ? void 0 : _a.elements) === null || _b === void 0 ? void 0 : _b.all) === null || _c === void 0 ? void 0 : _c.props) === null || _d === void 0 ? void 0 : _d.default), (_h = (_g = (_f = (_e = theme === null || theme === void 0 ? void 0 : theme.ui) === null || _e === void 0 ? void 0 : _e.elements) === null || _f === void 0 ? void 0 : _f.amauiMasonry) === null || _g === void 0 ? void 0 : _g.props) === null || _h === void 0 ? void 0 : _h.default), props_)); }, [props_]); const Line = react_1.default.useMemo(() => { var _a; return ((_a = theme === null || theme === void 0 ? void 0 : theme.elements) === null || _a === void 0 ? void 0 : _a.Line) || Line_1.default; }, [theme]); const { gap: gap_, columns: columns_, className, style, noMasonry, NoMasonryProps, children: children_ } = props, other = __rest(props, ["gap", "columns", "className", "style", "noMasonry", "NoMasonryProps", "children"]); const { classes } = useStyle(); const [init, setInit] = react_1.default.useState(false); const refs = { root: react_1.default.useRef(undefined), gap: react_1.default.useRef(undefined), columns: react_1.default.useRef(undefined), init: react_1.default.useRef(undefined), useMasonry: react_1.default.useRef(undefined) }; const keys = react_1.default.useMemo(() => { const result = []; const items = [gap_, columns_]; items.forEach(item => { if ((0, utils_1.is)('object', item)) Object.keys(item).filter(key => theme.breakpoints.media[key]).forEach(key => result.push(key)); }); return (0, utils_1.unique)(result); }, [gap_, columns_]); const breakpoints = {}; keys.forEach(key => { breakpoints[key] = (0, useMediaQuery_1.default)(theme.breakpoints.media[key], { element: refs.root.current }); }); refs.init.current = init; const styles = { root: {}, item: {} }; const gap = (0, utils_2.valueBreakpoints)(gap_, 2, breakpoints, theme) || 0; const columns = (0, utils_2.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_1.default.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 => { if (columns__[column] > highestValue) highestValue = columns__[column]; }); // Height const height = highestValue + (gap * theme.space.unit); refs.root.current.style.height = `${height}px`; if (refs.init.current !== 'animated') setInit(true); } }; const method = react_1.default.useCallback(update, []); react_1.default.useEffect(() => { setTimeout(update); const observer = new ResizeObserver(method); [refs.root.current, ...Array.from(refs.root.current.children)].forEach((item) => observer.observe(item)); return () => { // Clean up observer.disconnect(); }; }, []); react_1.default.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 ((0, jsx_runtime_1.jsx)(Line, Object.assign({ ref: item => { if (ref) { if ((0, utils_1.is)('function', ref)) ref(item); else ref.current = item; } refs.root.current = item; }, gap: gap, direction: 'row', wrap: 'wrap', fullWidth: true }, NoMasonryProps, { className: (0, style_react_1.classNames)([ (0, utils_2.staticClassName)('Masonry', theme) && [ 'amaui-Masonry-root', 'amaui-Masonry-noMasonry' ], NoMasonryProps === null || NoMasonryProps === void 0 ? void 0 : NoMasonryProps.className, className, classes.root ]), style: Object.assign(Object.assign(Object.assign({}, style), { height: 'auto' }), NoMasonryProps === null || NoMasonryProps === void 0 ? void 0 : NoMasonryProps.style) }, other, { children: children.map((item, index) => (react_1.default.cloneElement(item, { key: index, style: Object.assign(Object.assign({}, styles.item), item.props.style) }))) }))); } return ((0, jsx_runtime_1.jsxs)(Line, Object.assign({ ref: item => { if (ref) { if ((0, utils_1.is)('function', ref)) ref(item); else ref.current = item; } refs.root.current = item; }, gap: gap, wrap: 'wrap', align: 'unset', justify: 'unset', direction: 'column', fullWidth: true, className: (0, style_react_1.classNames)([ (0, utils_2.staticClassName)('Masonry', theme) && [ 'amaui-Masonry-root' ], className, classes.root ]), style: Object.assign(Object.assign({}, styles.root), style) }, other, { children: [react_1.default.Children.toArray(children).map((item, index) => (react_1.default.cloneElement(item, { key: index, style: Object.assign(Object.assign({}, styles.item), item.props.style) }))), columns > 1 && new Array(columns - 1).fill(true).map((item, index) => ((0, jsx_runtime_1.jsx)("div", { className: (0, style_react_1.classNames)([ (0, utils_2.staticClassName)('Masonry', theme) && [ 'amaui-Masonry-line-break' ] ]), style: { flex: '1 1 100%', order: index + 1 } }, index)))] }))); }); Masonry.displayName = 'amaui-Masonry'; exports.default = Masonry;