@amaui/ui-react
Version:
UI for React
260 lines (259 loc) • 11.6 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 Type_1 = __importDefault(require("../Type"));
const Surface_1 = __importDefault(require("../Surface"));
const Line_1 = __importDefault(require("../Line"));
const useVisible_1 = __importDefault(require("../useVisible/useVisible"));
const utils_2 = require("../utils");
const useStyle = (0, style_react_1.style)(theme => ({
root: {
position: 'relative',
display: 'inline-block',
lineHeight: '0'
},
root_figure: {
'& img': {
height: 'auto',
width: '100%'
}
},
root_picture: {
'& img': {
height: 'auto',
width: '100%'
}
},
picture: {
position: 'relative',
display: 'inline-block'
},
figcaption: {
'&.amaui-Type-root': {
padding: `${theme.methods.space.value(1.5, 'px')} ${theme.methods.space.value(2, 'px')}`
}
},
align_description_start: {
textAlign: 'start'
},
align_description_left: {
textAlign: 'left'
},
align_description_center: {
textAlign: 'center'
},
align_description_right: {
textAlign: 'right'
},
align_description_end: {
textAlign: 'end'
},
align_start: {
marginInlineEnd: 'auto'
},
align_left: {
marginLeft: 'auto'
},
align_center: {
marginInline: 'auto'
},
align_right: {
marginRight: 'auto'
},
align_end: {
marginInlineStart: 'auto'
},
align_unset: {
marginInline: 'unset'
},
responsive: {
height: 'auto',
maxWidth: '100%',
},
// 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' },
fullWidth: {
display: 'block',
width: '100%'
},
noImage: {
width: '100%',
maxWidth: '540px',
aspectRatio: '4 / 3',
backgroundColor: theme.palette.color.neutral[100],
backgroundImage: `linear-gradient(${theme.palette.light ? '130deg' : '330deg'}, ${theme.methods.palette.color.colorToRgb(theme.palette.color.primary.main, 0.14)} 0%, ${theme.methods.palette.color.colorToRgb(theme.palette.color.secondary.main, 0.14)} 40%, ${theme.methods.palette.color.colorToRgb(theme.palette.color.quaternary.main, 0.14)} 100%)`,
cursor: 'default',
userSelect: 'none'
}
}), { name: 'amaui-Image' });
const Image = 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.amauiImage) === 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 Type = react_1.default.useMemo(() => { var _a; return ((_a = theme === null || theme === void 0 ? void 0 : theme.elements) === null || _a === void 0 ? void 0 : _a.Type) || Type_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 = 'primary', src, url, urlSmall, sources, alt, description, alignDescription = 'start', width, height, align, responsive = true, lazyLoad = true, fullWidth, maxWidth, loading, RootProps, NoImageProps, DescriptionProps, UseVisibleProps, className, style, children: children_ } = props, other = __rest(props, ["tonal", "color", "src", "url", "urlSmall", "sources", "alt", "description", "alignDescription", "width", "height", "align", "responsive", "lazyLoad", "fullWidth", "maxWidth", "loading", "RootProps", "NoImageProps", "DescriptionProps", "UseVisibleProps", "className", "style", "children"]);
const { classes } = useStyle();
let Component = 'img';
const [root, setRoot] = react_1.default.useState();
const [loaded, setLoaded] = react_1.default.useState(false);
const [source, setSource] = react_1.default.useState('');
const visiblity = lazyLoad ? (0, useVisible_1.default)(Object.assign({ element: root }, UseVisibleProps)) : { visible: true };
const refs = {
root: react_1.default.useRef(undefined),
loaded: react_1.default.useRef(loaded)
};
refs.loaded.current = loaded;
const imageLoad = react_1.default.useCallback((value) => new Promise((resolve, reject) => {
const image = window.document.createElement('img');
let imageLoaded = false;
const check = () => {
if (image.complete && image.naturalWidth !== 0)
imageLoaded = true;
if (imageLoaded)
return resolve(true);
setTimeout(check, 40);
};
const onLoad = (event) => {
check();
};
const onError = () => {
resolve(false);
};
image.onload = onLoad;
image.onerror = onError;
image.src = value;
if (image.complete)
return resolve(true);
}), []);
const init = react_1.default.useCallback(async () => {
const URL = src || url || urlSmall;
const isLoaded = await imageLoad(URL);
setSource(isLoaded ? URL : '');
setLoaded(true);
}, [src, url, urlSmall, loaded]);
react_1.default.useEffect(() => {
if ((visiblity === null || visiblity === void 0 ? void 0 : visiblity.visible) && !refs.loaded.current) {
init();
}
}, [visiblity === null || visiblity === void 0 ? void 0 : visiblity.visible]);
react_1.default.useEffect(() => {
if (loaded)
init();
}, [src]);
const noImage = loaded && !source;
const children = react_1.default.Children.toArray(children_);
// picture
// with sources
let isPicture = false;
const picture = !!children.length || !!(sources === null || sources === void 0 ? void 0 : sources.length);
if (picture && !description) {
Component = 'picture';
isPicture = true;
}
// description
if (description)
Component = 'figure';
const rootIsImage = !picture && !description;
// surface
if (picture || description) {
other.tonal = tonal;
other.color = color;
other.Component = Component;
Component = Surface;
}
else if (rootIsImage && !noImage) {
other.src = source;
other.atl = alt;
other.width = width;
other.height = height;
other.loading = loading;
other.style = style;
}
const Wrapper = (picture && description) ? 'picture' : react_1.default.Fragment;
const WrapperProps = (picture && description) ? {
className: (0, style_react_1.classNames)([
(0, utils_2.staticClassName)('Image', theme) && [
'amaui-Image-picture'
],
classes.picture
])
} : {};
if (!rootIsImage && !noImage) {
const imgElement = ((0, jsx_runtime_1.jsx)("img", { src: src, alt: alt || '', width: width, height: height, loading: loading, className: (0, style_react_1.classNames)([
(0, utils_2.staticClassName)('Image', theme) && [
'amaui-Image-img'
],
className,
classes.img
]), style: style }));
other.children = (0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsxs)(Wrapper, Object.assign({}, WrapperProps, { children: [(sources === null || sources === void 0 ? void 0 : sources.length) && sources.map((item, index) => ((0, jsx_runtime_1.jsx)("source", Object.assign({}, item), index))), children, imgElement] })), description && ((0, jsx_runtime_1.jsx)(Type, Object.assign({ Component: 'figcaption', version: 'b3' }, DescriptionProps, { className: (0, style_react_1.classNames)([
(0, utils_2.staticClassName)('Image', theme) && [
'amaui-Image-figcaption'
],
DescriptionProps === null || DescriptionProps === void 0 ? void 0 : DescriptionProps.className,
className,
classes.figcaption,
classes[`align_description_${alignDescription}`]
]) }, { children: description })))] });
}
if (noImage) {
Component = Line;
other.fullWidth = true;
other.children = children;
other.style = style;
delete other.Component;
}
return ((0, jsx_runtime_1.jsx)(Component, Object.assign({ ref: (item) => {
if (ref) {
if ((0, utils_1.is)('function', ref))
ref(item);
else
ref.current = item;
}
setRoot(item);
refs.root.current = item;
} }, RootProps, { className: (0, style_react_1.classNames)([
(0, utils_2.staticClassName)('Image', theme) && [
'amaui-Image-root',
noImage && 'amaui-Image-no-image',
(picture && !description) && `amaui-Image-picture`
],
RootProps === null || RootProps === void 0 ? void 0 : RootProps.className,
className,
classes.root,
classes[`align_${align}`],
noImage && classes.noImage,
responsive && classes.responsive,
maxWidth && classes[`maxWidth_${maxWidth}`],
fullWidth && classes.fullWidth,
rootIsImage && classes.root_img,
(picture && !description) && classes.root_picture,
description && classes.root_figure
]) }, other)));
});
Image.displayName = 'amaui-Image';
exports.default = Image;
;