Skip to content

Commit

Permalink
feat: update a way params are thrown to iframe styles
Browse files Browse the repository at this point in the history
  • Loading branch information
niktverd committed Dec 16, 2023
1 parent 818f843 commit fe87823
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 49 deletions.
24 changes: 0 additions & 24 deletions src/components/Media/Iframe/Iframe.scss
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,7 @@ $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 {
&_fixed-width {
width: 100%;
}

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

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

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

const updateFormIframe = useCallback(
(container: HTMLDivElement) => {
if (iframeRef.current) {
iframeRef.current.src = src;
} else {
const iframeWidth = typeof width === 'number' ? `${width}px` : '100%';
const iframeWidth = typeof width === 'number' ? `${width}px` : width;
let iframeHeight: string | number | undefined =
typeof height === 'number' ? `${height}px` : height;

if (height === 'auto') {
iframeHeight = undefined;
}

iframeRef.current = document.createElement('iframe');
iframeRef.current.src = src;
iframeRef.current.id = iframeId;
Expand All @@ -36,14 +41,15 @@ const Iframe = (props: MediaComponentIframeProps) => {
iframeRef.current.frameBorder = '0';
iframeRef.current.scrolling = 'no';
iframeRef.current.width = iframeWidth;
iframeRef.current.className = b('item', {
'fixed-height': fullHeight,
'fixed-width': !fullWidth,
});
iframeRef.current.style.width = iframeWidth;
if (iframeHeight) {
iframeRef.current.style.height = iframeHeight;
}

container.appendChild(iframeRef.current);
}
},
[src, width, iframeId, name, title, fullHeight, fullWidth],
[src, width, iframeId, name, title, height],
);

const handleMessage = useCallback(
Expand All @@ -55,17 +61,20 @@ const Iframe = (props: MediaComponentIframeProps) => {

try {
const parsed = JSON.parse(data);
const frameHeight = parsed['iframe-height'];
const {message} = parsed;
const iframeHeight = parsed['iframe-height'];
const {message, name: iframeName} = parsed;
if (iframeName !== name && iframeName !== iframeId) {
return;
}

if (iframeRef.current && frameHeight && !message) {
iframeRef.current.height = `${frameHeight}px`;
if (iframeRef.current && iframeHeight && !message) {
iframeRef.current.height = `${iframeHeight}px`;
}
} catch (error) {
return;
}
},
[height],
[height, iframeId, name],
);

const addIframe = useCallback(() => {
Expand All @@ -85,9 +94,9 @@ const Iframe = (props: MediaComponentIframeProps) => {

return iframe ? (
<div
className={b({margins, 'justify-content': justifyContent})}
className={b({margins})}
ref={formContainerRef}
style={{height}}
style={{height, textAlign: justifyContent}}
/>
) : null;
};
Expand Down
20 changes: 12 additions & 8 deletions src/components/Media/__stories__/Media.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@ const DefaultTemplate: StoryFn<MediaAllProps> = (args) => (
</div>
);

const IframeTemplate: StoryFn<MediaAllProps> = (args) => (
<div style={{maxWidth: '500px'}}>
<h1>Iframe with margins (default)</h1>
<Media {...args} />
<h1>Iframe without margins</h1>
<Media {...args} margins={false} />
</div>
);
const IframeTemplate: StoryFn<MediaAllProps> = (args) => {
return (
<div style={{maxWidth: '700px'}}>
<h1>Iframe with margins (default)</h1>
<Media {...args} />
<h1>Iframe without margins</h1>
<Media {...args} margins={false} />
</div>
);
};

export const Image = DefaultTemplate.bind({});
export const ImageSlider = DefaultTemplate.bind({});
Expand All @@ -34,6 +36,7 @@ export const Youtube = DefaultTemplate.bind({});
export const DataLens = DefaultTemplate.bind({});
export const DataLensDarkTheme = DefaultTemplate.bind({});
export const Iframe = IframeTemplate.bind({});
export const IframeForm = IframeTemplate.bind({});

Image.args = data.image.content;
ImageSlider.args = data.imageSlider.content;
Expand All @@ -42,3 +45,4 @@ Youtube.args = data.youtube.content;
DataLens.args = data.dataLens.content;
DataLensDarkTheme.args = data.dataLensDarkTheme.content as MediaProps;
Iframe.args = data.iframe.content as MediaProps;
IframeForm.args = data.iframeForm.content as MediaProps;
10 changes: 10 additions & 0 deletions src/components/Media/__stories__/data.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@
}
}
},
"iframeForm": {
"content": {
"iframe": {
"src": "https://forms.yandex.ru/cloud/61a4e639d4d24e0dbba36f5c/?viewMode=docs&id=components-yandexform--docs&args=&url=http%3A%2F%2Flocalhost%3A7009%2Fiframe.html&iframe=1&lang=en&theme=cloud-www",
"name": "iframe-form-1",
"height": "auto",
"justifyContent": "center"
}
}
},
"youtube": {
"content": {
"youtube": "https://youtu.be/0Qd3T6skprA",
Expand Down
2 changes: 1 addition & 1 deletion src/models/constructor-items/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ export type JustifyValues = 'left' | 'right' | 'center';

export interface IframeProps {
src: string;
width?: number;
width?: number | string;
height?: number | string;
title?: string;
name?: string;
Expand Down

0 comments on commit fe87823

Please sign in to comment.