UNPKG

@amaui/ui-react

Version:
119 lines (118 loc) 6.63 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 Surface_1 = __importDefault(require("../Surface")); 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: { // Reset margin: '0', listStyle: 'none', width: '100%', position: 'relative', borderRadius: `${theme.shape.radius.unit / 2}px`, }, padding_vertical_both: { paddingBlock: theme.methods.space.value('sm', 'px') }, padding_vertical_start: { paddingBlockStart: theme.methods.space.value('sm', 'px') }, padding_vertical_end: { paddingBlockEnd: theme.methods.space.value('sm', 'px') }, padding_vertical_none: { paddingBlock: '0' }, padding_horizontal_both: { paddingInline: theme.methods.space.value('rg', 'px') }, padding_horizontal_start: { paddingInlineStart: theme.methods.space.value('rg', 'px') }, padding_horizontal_end: { paddingInlineEnd: theme.methods.space.value('rg', 'px') }, padding_horizontal_none: { paddingInline: '0' }, menu: {}, noMaxWidth: { maxWidth: 'unset' }, noBackground: { '&.amaui-Surface-root': { background: 'none' } } }), { name: 'amaui-List' }); const List = 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.amauiList) === 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 Surface = react_1.default.useMemo(() => { var _a; return ((_a = theme === null || theme === void 0 ? void 0 : theme.elements) === null || _a === void 0 ? void 0 : _a.Surface) || Surface_1.default; }, [theme]); const { tonal = true, color = 'themed', size: size_, elevation = (props.menu && theme.palette.light) ? 2 : 0, menu, menuOpen, noMaxWidth, indent, paddingHorizontal = 'none', paddingVertical = 'both', noBackground, noChildrenTransform, onMenuDesktopClose, SurfaceProps, Component = 'ul', className, style, children } = props, other = __rest(props, ["tonal", "color", "size", "elevation", "menu", "menuOpen", "noMaxWidth", "indent", "paddingHorizontal", "paddingVertical", "noBackground", "noChildrenTransform", "onMenuDesktopClose", "SurfaceProps", "Component", "className", "style", "children"]); const { classes } = useStyle(); const refs = { root: react_1.default.useRef(undefined) }; const mobile = (0, useMediaQuery_1.default)('(max-width: 767px)', { element: refs.root.current }); const size = size_ !== undefined ? size_ : mobile ? 'small' : 'regular'; const styles = { root: {} }; if (indent !== undefined) styles.root.paddingInlineStart = `${indent * theme.space.unit}px`; return ((0, jsx_runtime_1.jsx)(Surface, Object.assign({ ref: item => { if (ref) { if ((0, utils_1.is)('function', ref)) ref(item); else ref.current = item; } refs.root.current = item; }, tonal: tonal, color: color, elevation: elevation, gap: 0, direction: 'column', align: 'flex-start', role: 'listbox', Component: Line, AdditionalProps: { Component } }, SurfaceProps, { className: (0, style_react_1.classNames)([ (0, utils_2.staticClassName)('List', theme) && [ 'amaui-List-root', `amaui-List-size-${size}` ], SurfaceProps === null || SurfaceProps === void 0 ? void 0 : SurfaceProps.className, className, classes.root, classes[`padding_vertical_${paddingVertical}`], classes[`padding_horizontal_${paddingHorizontal}`], menu && classes.menu, noMaxWidth && classes.noMaxWidth, noBackground && classes.noBackground ]), style: Object.assign(Object.assign({}, style), styles.root) }, { children: noChildrenTransform ? children : react_1.default.Children.toArray(children).map((item, index) => { return react_1.default.cloneElement(item, Object.assign(Object.assign(Object.assign({ key: index, menuItem: menu, menuOpen, tonal: item.props.tonal !== undefined ? item.props.tonal : tonal, color: item.props.color !== undefined ? item.props.color : color, size: item.props.size !== undefined ? item.props.size : size }, other), item.props), { onClick: (event) => { if ((0, utils_1.is)('function', onMenuDesktopClose) && item.props.menuCloseOnClick) onMenuDesktopClose(); if ((0, utils_1.is)('function', item.props.onClick)) item.props.onClick(event); } })); }) }))); }); List.displayName = 'amaui-List'; exports.default = List;