updating font size of import card based on title
This commit is contained in:
parent
e3656f7998
commit
3a7644dd2c
1 changed files with 50 additions and 5 deletions
|
|
@ -1,4 +1,4 @@
|
||||||
import React, { ReactNode } from "react";
|
import React, { ReactNode, useEffect } from "react";
|
||||||
import { DocumentDuplicateIcon } from "@heroicons/react/solid";
|
import { DocumentDuplicateIcon } from "@heroicons/react/solid";
|
||||||
import { classNames } from "../../../utils";
|
import { classNames } from "../../../utils";
|
||||||
import Tooltip from "../../../components/TooltipComponent";
|
import Tooltip from "../../../components/TooltipComponent";
|
||||||
|
|
@ -30,6 +30,8 @@ export default function ButtonBox({
|
||||||
let marginTop: string;
|
let marginTop: string;
|
||||||
let height: string;
|
let height: string;
|
||||||
let width: string;
|
let width: string;
|
||||||
|
let textHeight: number;
|
||||||
|
let textWidth: number;
|
||||||
switch (size) {
|
switch (size) {
|
||||||
case "small":
|
case "small":
|
||||||
bigCircle = "h-12 w-12";
|
bigCircle = "h-12 w-12";
|
||||||
|
|
@ -39,6 +41,8 @@ export default function ButtonBox({
|
||||||
padding = "p-2 py-3";
|
padding = "p-2 py-3";
|
||||||
marginTop = "mt-2";
|
marginTop = "mt-2";
|
||||||
height = "h-36";
|
height = "h-36";
|
||||||
|
textHeight=70;
|
||||||
|
textWidth=80;
|
||||||
width = "w-32";
|
width = "w-32";
|
||||||
break;
|
break;
|
||||||
case "medium":
|
case "medium":
|
||||||
|
|
@ -48,6 +52,8 @@ export default function ButtonBox({
|
||||||
descriptionFontSize = "text-sm";
|
descriptionFontSize = "text-sm";
|
||||||
padding = "p-4 py-5";
|
padding = "p-4 py-5";
|
||||||
marginTop = "mt-3";
|
marginTop = "mt-3";
|
||||||
|
textHeight=112;
|
||||||
|
textWidth=162;
|
||||||
height = "h-44";
|
height = "h-44";
|
||||||
width = "w-36";
|
width = "w-36";
|
||||||
break;
|
break;
|
||||||
|
|
@ -72,6 +78,46 @@ export default function ButtonBox({
|
||||||
width = "w-44";
|
width = "w-44";
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const titleRef = React.useRef<HTMLHeadingElement>(null);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const resizeFont = () => {
|
||||||
|
const titleElement = titleRef.current;
|
||||||
|
if (titleElement) {
|
||||||
|
const containerWidth = titleElement.offsetWidth;
|
||||||
|
const containerHeight = titleElement.offsetHeight;
|
||||||
|
|
||||||
|
const titleComputedStyle = window.getComputedStyle(titleElement);
|
||||||
|
const titleWidth = titleElement.getBoundingClientRect().width;
|
||||||
|
|
||||||
|
const currentFontSize = parseFloat(titleComputedStyle.fontSize);
|
||||||
|
|
||||||
|
const desiredWidth = textWidth - 10; // Subtracting the desired padding
|
||||||
|
|
||||||
|
// Calculate the desired font size based on the adjusted width
|
||||||
|
let desiredFontSize = currentFontSize * (desiredWidth / titleWidth);
|
||||||
|
|
||||||
|
// Adjust the desired font size to fit within the container height, if needed
|
||||||
|
const maxHeight = containerHeight - 10; // Subtracting the desired top padding
|
||||||
|
const maxHeightFontSize = maxHeight * 0.8; // Adjust the scaling factor as needed
|
||||||
|
desiredFontSize = Math.min(desiredFontSize, maxHeightFontSize);
|
||||||
|
|
||||||
|
// Apply the desired font size and padding to the title element
|
||||||
|
titleElement.style.fontSize = `${desiredFontSize}px`;
|
||||||
|
titleElement.style.paddingLeft = '5px';
|
||||||
|
titleElement.style.paddingRight = '5px';
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
resizeFont();
|
||||||
|
window.addEventListener("resize", resizeFont);
|
||||||
|
return () => {
|
||||||
|
window.removeEventListener("resize", resizeFont);
|
||||||
|
};
|
||||||
|
}, []);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<button disabled={deactivate} onClick={onClick}>
|
<button disabled={deactivate} onClick={onClick}>
|
||||||
<div
|
<div
|
||||||
|
|
@ -92,17 +138,16 @@ export default function ButtonBox({
|
||||||
<div className={textColor}>{icon}</div>
|
<div className={textColor}>{icon}</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div className="mt-auto mb-auto">
|
|
||||||
<h3
|
<h3
|
||||||
|
ref={titleRef}
|
||||||
className={classNames(
|
className={classNames(
|
||||||
"font-semibold text-white dark:text-white/80",
|
" font-semibold text-white dark:text-white/80",
|
||||||
titleFontSize,
|
|
||||||
marginTop
|
marginTop
|
||||||
)}
|
)}
|
||||||
>
|
>
|
||||||
{title}
|
{title}
|
||||||
</h3>
|
</h3>
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
</button>
|
</button>
|
||||||
);
|
);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue