Skip to content

Commit

Permalink
adding width as string instead
Browse files Browse the repository at this point in the history
  • Loading branch information
houmland committed Dec 19, 2023
1 parent ab1884e commit 10ba136
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 28 deletions.
2 changes: 1 addition & 1 deletion example/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ function App() {
<h1>Demo website</h1>
<Stack
project="https://www.stack-ai.com/embed/46bf5b6a-9b4d-48f6-8a13-cdfc4fe58520/11da0c81-afe2-4ccd-b498-807bbde8e7f1/653fefcfcc37c0093d55e6a9"
width = {35}
width = {'35rem'}
fixed = {true}
/>
</>
Expand Down
73 changes: 46 additions & 27 deletions src/Stack.tsx
Original file line number Diff line number Diff line change
@@ -1,55 +1,75 @@
import { forwardRef, useEffect, LegacyRef} from 'react';
import React, { forwardRef, LegacyRef, useEffect } from 'react';

type StackProps = {
project: string;
innerRef?: LegacyRef<HTMLIFrameElement> | undefined;
width?: number;
width?: string;
fixed?: boolean;
};

const Stack = forwardRef(function Stack({
project,
innerRef,
width = 35,
width = '35rem',
fixed = true
}: StackProps) {

const height = '38.5rem';

const adjustWidth = (w: number) => {
const minWidth = 15;

if (w < minWidth) {
console.warn(`Width is too small (${w}rem). Adjusting to minimum width (${minWidth}rem).`);
const adjustWidth = (w: string) => {
const minWidth = 15;
let adjustedWidth = '';

if (w.match(/(rem|px|em|%)$/)) {
adjustedWidth = w;
} else {
// If no unit is found, treat the string as a number and append 'rem'
const numericWidth = parseFloat(w);
if (isNaN(numericWidth)) {
throw new Error(`Invalid width: "${w}". Width must be a numeric value followed by a unit (e.g., '35rem', '100px').`);
}

if (numericWidth < minWidth) {
console.warn(`Width is too small (${numericWidth}rem). Adjusting to minimum width (${minWidth}rem).`);
adjustedWidth = `${minWidth}rem`;
} else {
adjustedWidth = `${numericWidth}rem`;
}
}

return {
adjustedWidth: w < minWidth ? `${minWidth}rem` : `${w}rem`,
};
return { adjustedWidth };
};

useEffect(() => {
const iframe = document.getElementById('responsiveIframe');
if (iframe) {
// Adjust width only as height is now constant
const { adjustedWidth } = adjustWidth(width);
iframe.style.width = adjustedWidth;
iframe.style.height = height;
try {
// Adjust width only as height is now constant
const { adjustedWidth } = adjustWidth(width);
iframe.style.width = adjustedWidth;
iframe.style.height = height;
} catch (error) {
console.error(error);
}
}

const handleMessage = (event: MessageEvent) => {
const iframe = document.getElementById('responsiveIframe');
if (iframe && event.data.type === 'chatbotStateChange') {
const isMobile = window.innerWidth < 1000;
if (isMobile) {
// Mobile
iframe.style.width = '100vw';
iframe.style.height = height;
} else {
// Desktop
const { adjustedWidth } = adjustWidth(width);
iframe.style.width = adjustedWidth;
iframe.style.height = height;
try {
const isMobile = window.innerWidth < 1000;
if (isMobile) {
// Mobile
iframe.style.width = '100vw';
iframe.style.height = height;
} else {
// Desktop
const { adjustedWidth } = adjustWidth(width);
iframe.style.width = adjustedWidth;
iframe.style.height = height;
}
} catch (error) {
console.error(error);
}
}
};
Expand All @@ -59,7 +79,7 @@ const Stack = forwardRef(function Stack({
return () => {
window.removeEventListener('message', handleMessage);
};
}, [width]); // Removed height from dependency array
}, [width]);

return (
<iframe
Expand All @@ -82,4 +102,3 @@ const Stack = forwardRef(function Stack({
});

export default Stack;

0 comments on commit 10ba136

Please sign in to comment.