UNPKG

@amaui/ui-react

Version:
135 lines (134 loc) 6.28 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 useMediaQuery_1 = __importDefault(require("../useMediaQuery")); const utils_2 = require("../utils"); const useStyle = (0, style_react_1.style)(theme => ({ root: { listStyleType: 'none' }, version_standard: { display: 'grid' }, version_quilted: { display: 'grid' }, version_vowen: { display: 'grid', '& > *:nth-of-type(2n)': { height: '70%', overflow: 'hidden', alignSelf: 'center' } }, version_masonry: { display: 'block' } }), { name: 'amaui-ImageList' }); const ImageList = 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.amauiImageList) === null || _g === void 0 ? void 0 : _g.props) === null || _h === void 0 ? void 0 : _h.default), props_)); }, [props_]); const { classes } = useStyle(); const { version = 'standard', gap: gap_, rowGap: rowGap_, columnGap: columnGap_, columns: columns_, Component = 'ul', className, style, children } = props, other = __rest(props, ["version", "gap", "rowGap", "columnGap", "columns", "Component", "className", "style", "children"]); const refs = { root: react_1.default.useRef(undefined) }; const keys = react_1.default.useMemo(() => { const result = []; const items = [gap_, rowGap_, columnGap_, 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_, rowGap_, columnGap_, columns_]); const breakpoints = {}; keys.forEach(key => { breakpoints[key] = (0, useMediaQuery_1.default)(theme.breakpoints.media[key], { element: refs.root.current }); }); const gap = (0, utils_2.valueBreakpoints)(gap_, 0.5, breakpoints, theme); const rowGap = (0, utils_2.valueBreakpoints)(rowGap_, undefined, breakpoints, theme); const columnGap = (0, utils_2.valueBreakpoints)(columnGap_, undefined, breakpoints, theme); 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); const styles = { root: {}, item: {} }; if (['standard', 'quilted', 'vowen'].includes(version)) { styles.root.gridTemplateColumns = `repeat(${columns}, 1fr)`; } if (version === 'masonry') styles.root.columnCount = columns; if (rowGap !== undefined || columnGap !== undefined) { if (rowGap !== undefined) { styles.root.rowGap = `${rowGap}px`; if (version === 'masonry') styles.item.marginBottom = `${rowGap * theme.space.unit}px`; } if (columnGap !== undefined) styles.root.columnGap = `${columnGap * theme.space.unit}px`; } else { styles.root.gap = `${gap * theme.space.unit}px`; if (version === 'masonry') styles.item.marginBottom = `${gap * theme.space.unit}px`; } const Wrapper = version !== 'masonry' ? react_1.default.Fragment : 'div'; const WrapperProps = {}; if (version === 'masonry') { WrapperProps.ref = item => { if (ref) { if ((0, utils_1.is)('function', ref)) ref(item); else ref.current = item; } refs.root.current = item; }; WrapperProps.style = Object.assign({}, style); other.style = Object.assign({}, styles.root); } else { other.ref = item => { if (ref) { if ((0, utils_1.is)('function', ref)) ref(item); else ref.current = item; } refs.root.current = item; }; other.style = Object.assign(Object.assign({}, styles.root), style); } return ((0, jsx_runtime_1.jsx)(Wrapper, Object.assign({}, WrapperProps, { children: (0, jsx_runtime_1.jsx)(Component, Object.assign({ className: (0, style_react_1.classNames)([ (0, utils_2.staticClassName)('ImageList', theme) && [ `amaui-ImageList-root` ], className, classes.root, classes[`version_${version}`] ]) }, other, { children: react_1.default.Children.toArray(children).map((item, index) => (react_1.default.cloneElement(item, { key: index, version, style: Object.assign(Object.assign({}, styles.item), item.props.style) }))) })) }))); }); ImageList.displayName = 'amaui-ImageList'; exports.default = ImageList;