@amaui/ui-react
Version:
UI for React
138 lines (137 loc) • 6.61 kB
JavaScript
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: {
width: '100%'
},
// alignment
alignment_start: {
marginInlineEnd: 'auto'
},
alignment_center: {
marginInline: 'auto'
},
alignment_end: {
marginInlineStart: 'auto'
},
// padding
// vertical
padding_vertical_both: {
paddingBlock: theme.methods.space.value(2, 'px')
},
padding_vertical_start: {
paddingBlockStart: theme.methods.space.value(2, 'px')
},
padding_vertical_end: {
paddingBlockEnd: theme.methods.space.value(2, 'px')
},
padding_vertical_none: {
paddingBlock: '0px'
},
// horizontal
padding_horizontal_both: {
paddingInline: theme.methods.space.value(2, 'px')
},
padding_horizontal_start: {
paddingInlineStart: theme.methods.space.value(2, 'px')
},
padding_horizontal_end: {
paddingInlineEnd: theme.methods.space.value(2, 'px')
},
padding_horizontal_none: {
paddingInline: '0px'
},
// maxWidth
maxWidth_xxs: { maxWidth: '320px' },
maxWidth_xs: { maxWidth: '400px' },
maxWidth_sm: { maxWidth: '600px' },
maxWidth_rg: { maxWidth: '960px' },
maxWidth_lg: { maxWidth: '1240px' },
maxWidth_xl: { maxWidth: '1920px' },
maxWidth_unset: { maxWidth: 'unset' },
// full width
fullWidth: {
width: '100%',
maxWidth: 'unset'
},
}), { name: 'amaui-Container' });
const Container = 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.amauiContainer) === 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 { alignment: alignment_, paddingVertical: paddingVertical_, paddingHorizontal: paddingHorizontal_, fullWidth: fullWidth_, maxWidth: maxWidth_, Component = 'div', style, className, children } = props, other = __rest(props, ["alignment", "paddingVertical", "paddingHorizontal", "fullWidth", "maxWidth", "Component", "style", "className", "children"]);
const { classes } = useStyle();
const refs = {
root: react_1.default.useRef(undefined)
};
const keys = react_1.default.useMemo(() => {
const result = [];
const items = [alignment_, paddingVertical_, paddingHorizontal_, fullWidth_, maxWidth_];
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);
}, [alignment_, paddingVertical_, paddingHorizontal_, fullWidth_, maxWidth_]);
const breakpoints = {};
keys.forEach(key => {
breakpoints[key] = (0, useMediaQuery_1.default)(theme.breakpoints.media[key], { element: refs.root.current });
});
const alignment = (0, utils_2.valueBreakpoints)(alignment_, 'center', breakpoints, theme);
const paddingVertical = (0, utils_2.valueBreakpoints)(paddingVertical_, 'none', breakpoints, theme);
const paddingHorizontal = (0, utils_2.valueBreakpoints)(paddingHorizontal_, 'both', breakpoints, theme);
const fullWidth = (0, utils_2.valueBreakpoints)(fullWidth_, undefined, breakpoints, theme);
const maxWidth = (0, utils_2.valueBreakpoints)(maxWidth_, undefined, breakpoints, theme);
const styles = {
root: {}
};
if (!classes[`maxWidth_${maxWidth}`])
styles.root.maxWidth = maxWidth;
if (!['both', 'start', 'end', 'none'].includes(paddingHorizontal))
styles.root.paddingInline = paddingHorizontal;
if (!['both', 'start', 'end', 'none'].includes(paddingVertical))
styles.root.paddingBlock = paddingVertical;
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: 0, direction: 'row', className: (0, style_react_1.classNames)([
(0, utils_2.staticClassName)('Container', theme) && [
'amaui-Container-root'
],
className,
classes.root,
classes[`alignment_${alignment}`],
classes[`maxWidth_${maxWidth}`],
classes[`padding_vertical_${paddingVertical}`],
classes[`padding_horizontal_${paddingHorizontal}`],
fullWidth && classes.fullWidth
]), style: Object.assign(Object.assign({}, styles.root), style) }, other, { children: children })));
});
Container.displayName = 'amaui-Container';
exports.default = Container;
;