UNPKG

@selfcommunity/react-ui

Version:

React UI Components to integrate a Community created with SelfCommunity Platform.

140 lines (135 loc) 6.74 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const jsx_runtime_1 = require("react/jsx-runtime"); const react_1 = require("react"); const styles_1 = require("@mui/material/styles"); const material_1 = require("@mui/material"); const react_core_1 = require("@selfcommunity/react-core"); const classnames_1 = tslib_1.__importDefault(require("classnames")); const system_1 = require("@mui/system"); const HiddenPlaceholder_1 = tslib_1.__importDefault(require("../../shared/HiddenPlaceholder")); const use_deep_compare_effect_1 = require("use-deep-compare-effect"); const use_resize_observer_1 = tslib_1.__importDefault(require("use-resize-observer")); const constants_1 = require("./constants"); const classes = { root: `${constants_1.PREFIX}-root`, wrap: `${constants_1.PREFIX}-wrap`, image: `${constants_1.PREFIX}-image`, embedCode: `${constants_1.PREFIX}-embed-code`, prefixedHeight: `${constants_1.PREFIX}-prefixed-height` }; const Root = (0, styles_1.styled)(material_1.Box, { name: constants_1.PREFIX, slot: 'Root' })(() => ({})); /** * > API documentation for the Community-JS CustomAdv component. Learn about the available props and the CSS API. * * * This component renders custom adv banners. * Take a look at our <strong>demo</strong> component [here](/docs/sdk/community-js/react-ui/Components/CustomAdv) #### Import ```jsx import {CustomAdv} from '@selfcommunity/react-ui'; ``` #### Component Name The name `SCCustomAdv` can be used when providing style overrides in the theme. #### CSS |Rule Name|Global class|Description| |---|---|---| |root|.SCCustomAdv-root|Styles applied to the root element.| |wrap|.SCCustomAdv-wrap|Styles applied to wrap an element.| |image|.SCCustomAdv-image|Styles applied to the image element.| |embedCode|.SCCustomAdv-embed-code|Styles applied to the embed code section.| |prefixedHeight|.SCCustomAdv-prefixed-height|Styles applied to handle a prefixed height.| * @param inProps */ function CustomAdv(inProps) { // PROPS const props = (0, system_1.useThemeProps)({ props: inProps, name: constants_1.PREFIX }); const { id = 'custom_adv', className, advId = null, position, categoriesId, prefixedHeight, onStateChange, onHeightChange } = props; // REFS const isMountedRef = (0, react_core_1.useIsComponentMountedRef)(); const estimatedHeight = (0, react_1.useRef)(0); // ADV const { scCustomAdv } = (0, react_core_1.useSCFetchCustomAdv)({ id: advId, position, categoriesId }); /** * Virtual Feed update */ (0, use_deep_compare_effect_1.useDeepCompareEffectNoCheck)(() => { if (scCustomAdv) { onStateChange && onStateChange({ advId: scCustomAdv.id, prefixedHeight: estimatedHeight.current }); } onHeightChange && onHeightChange(); }, [scCustomAdv]); /** * Use useResizeObserver to intercept layout change: * onResize callback function, receive the width and height of the * element when it changes and call onHeightChange */ const { ref } = (0, use_resize_observer_1.default)({ round: (n) => { return n; }, onResize: ({ width, height }) => { if (isMountedRef.current) { estimatedHeight.current = height; onStateChange && onStateChange({ advId: scCustomAdv.id, prefixedHeight: height }); } } }); const embedRef = (0, react_1.useRef)(null); (0, react_1.useEffect)(() => { const embedCode = scCustomAdv === null || scCustomAdv === void 0 ? void 0 : scCustomAdv.embed_code; const container = embedRef.current; if (container && embedCode) { // Create a temporary container for parsing HTML const tempDiv = document.createElement('div'); tempDiv.innerHTML = embedCode; // Extract scripts from HTML content const scripts = tempDiv.getElementsByTagName('script'); const scriptContents = []; // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore for (let script of scripts) { if (script.src) { // If the script has a src attribute, create a new script element and set its src const newScript = document.createElement('script'); newScript.src = script.src; document.body.appendChild(newScript); } else { // If the script contains inline code, store its content for later execution scriptContents.push(script.innerHTML); } // Remove the script tag from the tempDiv script.parentNode.removeChild(script); } // Insert the HTML content into the container // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore container.innerHTML = tempDiv.innerHTML; // Execute inline script contents scriptContents.forEach((scriptContent) => { try { const newScript = document.createElement('script'); newScript.appendChild(document.createTextNode(scriptContent)); document.body.appendChild(newScript); } catch (e) { console.error('Error executing script', e); } }); } }, [scCustomAdv]); if (!scCustomAdv) { return (0, jsx_runtime_1.jsx)(HiddenPlaceholder_1.default, {}); } const adv = ((0, jsx_runtime_1.jsxs)(material_1.Box, Object.assign({ className: classes.wrap }, { children: [scCustomAdv.image && ((0, jsx_runtime_1.jsx)("img", { src: scCustomAdv.image, alt: scCustomAdv.title, className: (0, classnames_1.default)(classes.image, { [classes.prefixedHeight]: Boolean(prefixedHeight) }) })), scCustomAdv.embed_code && ((0, jsx_runtime_1.jsx)(material_1.Box, { ref: embedRef, className: (0, classnames_1.default)(classes.embedCode, { [classes.prefixedHeight]: Boolean(prefixedHeight) }), dangerouslySetInnerHTML: { __html: scCustomAdv.embed_code } }))] }))); return ((0, jsx_runtime_1.jsx)(Root, Object.assign({ id: id, className: (0, classnames_1.default)(classes.root, className), ref: ref, style: Object.assign({}, (prefixedHeight ? { paddingBottom: prefixedHeight } : { width: '100%' })) }, { children: scCustomAdv.link ? ((0, jsx_runtime_1.jsx)("a", Object.assign({ href: scCustomAdv.link, title: scCustomAdv.title }, { children: adv }))) : (adv) }))); } exports.default = CustomAdv;