mirror of
https://gh.wpcy.net/https://github.com/elementor/one-click-accessibility.git
synced 2026-04-25 05:47:00 +08:00
38 lines
960 B
JavaScript
38 lines
960 B
JavaScript
import PropTypes from 'prop-types';
|
|
import { StyledPaper } from '@ea11y-apps/scanner/styles/alt-text-form.styles';
|
|
import { useEffect, useRef } from '@wordpress/element';
|
|
|
|
export const ImagePreview = ({ element }) => {
|
|
const previewRef = useRef(null);
|
|
|
|
useEffect(() => {
|
|
if (!element || !previewRef.current) {
|
|
return;
|
|
}
|
|
|
|
// Only allow previewing safe elements
|
|
const tag = element.tagName.toLowerCase();
|
|
const allowedTags = ['img', 'svg'];
|
|
|
|
if (!allowedTags.includes(tag)) {
|
|
return;
|
|
}
|
|
|
|
const clone = element.cloneNode(true); // Deep clone
|
|
|
|
// Remove inline styles from the cloned element
|
|
clone.removeAttribute('style');
|
|
clone.style.cssText = '';
|
|
|
|
previewRef.current.innerHTML = ''; // Clear previous
|
|
previewRef.current.appendChild(clone);
|
|
}, [element]);
|
|
|
|
return (
|
|
<StyledPaper color="secondary" elevation={0} square ref={previewRef} />
|
|
);
|
|
};
|
|
|
|
ImagePreview.propTypes = {
|
|
element: PropTypes.instanceOf(Node).isRequired,
|
|
};
|