Skip to content

Commit

Permalink
feat: adjust alignment of iframe
Browse files Browse the repository at this point in the history
  • Loading branch information
niktverd committed Jan 12, 2024
1 parent a41480e commit 1cb314e
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 7 deletions.
18 changes: 17 additions & 1 deletion src/components/Media/Iframe/Iframe.scss
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,28 @@ $block: '.#{$ns}media-component-iframe';
}
}

&_justify-content {
&_left {
text-align: left;
}

&_center {
text-align: center;
}

&_right {
text-align: right;
}
}

#{$block}__iframe {
border-radius: 0;
}

&__item {
width: 100%;
&_fixed-width {
width: 100%;
}

&_fixed-height {
height: 100%;
Expand Down
20 changes: 15 additions & 5 deletions src/components/Media/Iframe/Iframe.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ const b = block('media-component-iframe');

const Iframe = (props: MediaComponentIframeProps) => {
const {iframe, margins = true} = props;
const {height = 400, src, width, name, title} = iframe;
const {height = 400, src, width, name, title, justifyContent = 'center'} = iframe;

const formContainerRef = useRef<HTMLDivElement>(null);
const iframeRef = useRef<HTMLIFrameElement>();
const [iframeId] = useState(uuidv4());
const [fixedHeight] = useState(typeof height === 'number');
const [fullHeight] = useState(typeof height === 'number');
const [fullWidth] = useState(typeof width === 'number');

const updateFormIframe = useCallback(
(container: HTMLDivElement) => {
Expand All @@ -35,11 +36,14 @@ const Iframe = (props: MediaComponentIframeProps) => {
iframeRef.current.frameBorder = '0';
iframeRef.current.scrolling = 'no';
iframeRef.current.width = iframeWidth;
iframeRef.current.className = b('item', {'fixed-height': fixedHeight});
iframeRef.current.className = b('item', {
'fixed-height': fullHeight,
'fixed-width': !fullWidth,
});
container.appendChild(iframeRef.current);
}
},
[src, width, iframeId, name, title, fixedHeight],
[src, width, iframeId, name, title, fullHeight, fullWidth],
);

const handleMessage = useCallback(
Expand Down Expand Up @@ -79,7 +83,13 @@ const Iframe = (props: MediaComponentIframeProps) => {
return () => window.removeEventListener('message', handleMessage);
}, [addIframe, handleMessage]);

return iframe ? <div className={b({margins})} ref={formContainerRef} style={{height}} /> : null;
return iframe ? (
<div
className={b({margins, 'justify-content': justifyContent})}
ref={formContainerRef}
style={{height}}
/>
) : null;
};

export default Iframe;
3 changes: 3 additions & 0 deletions src/models/constructor-items/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -344,12 +344,15 @@ export interface DataLensObjectProps {
theme: 'dark' | 'light';
}

export type JustifyValues = 'left' | 'right' | 'center';

export interface IframeProps {
src: string;
width?: number;
height?: number | string;
title?: string;
name?: string;
justifyContent?: JustifyValues;
}

export type DataLensProps = string | DataLensObjectProps;
Expand Down
6 changes: 5 additions & 1 deletion src/schema/validators/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -602,11 +602,15 @@ const IframeProps = {
type: 'string',
},
height: {
type: 'number',
oneOf: [{type: 'number'}, {type: 'string', enum: ['auto']}],
},
width: {
type: 'number',
},
justifyContent: {
type: 'string',
enum: ['left', 'right', 'center'],
},
},
};

Expand Down

0 comments on commit 1cb314e

Please sign in to comment.