Skip to content

Commit

Permalink
ModalTitle shrinkOnOverflow + VRT
Browse files Browse the repository at this point in the history
  • Loading branch information
joel-jeremy committed Apr 9, 2024
1 parent ee6137a commit 7c84297
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 5 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 3 additions & 2 deletions packages/desktop-client/src/components/Modals.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import * as monthUtils from 'loot-core/src/shared/months';
import { useActions } from '../hooks/useActions';
import { useSyncServerStatus } from '../hooks/useSyncServerStatus';

import { ModalTitle } from './common/Modal';
import { AccountAutocompleteModal } from './modals/AccountAutocompleteModal';
import { AccountMenuModal } from './modals/AccountMenuModal';
import { BudgetMenuModal } from './modals/BudgetMenuModal';
Expand Down Expand Up @@ -314,7 +315,7 @@ export function Modals() {
return (
<SingleInputModal
modalProps={modalProps}
title="New Category"
title={<ModalTitle title="New Category" shrinkOnOverflow />}
inputPlaceholder="Category name"
buttonText="Add"
onValidate={options.onValidate}
Expand All @@ -326,7 +327,7 @@ export function Modals() {
return (
<SingleInputModal
modalProps={modalProps}
title="New Category Group"
title={<ModalTitle title="New Category Group" shrinkOnOverflow />}
inputPlaceholder="Category group name"
buttonText="Add"
onValidate={options.onValidate}
Expand Down
22 changes: 21 additions & 1 deletion packages/desktop-client/src/components/common/Modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -370,13 +370,15 @@ type ModalTitleProps = {
getStyle?: (isEditing: boolean) => CSSProperties;
onEdit?: (isEditing: boolean) => void;
onTitleUpdate?: (newName: string) => void;
shrinkOnOverflow?: boolean;
};

export function ModalTitle({
title,
isEditable,
getStyle,
onTitleUpdate,
shrinkOnOverflow = false,
}: ModalTitleProps) {
const [isEditing, setIsEditing] = useState(false);
const style = getStyle?.(isEditing);
Expand Down Expand Up @@ -405,6 +407,23 @@ export function ModalTitle({
}
}, [isEditing]);

// Dynamic font size to avoid ellipsis.
const textRef = useRef<HTMLSpanElement>();
const [textFontSize, setTextFontSize] = useState(25);
useEffect(() => {
if (shrinkOnOverflow) {
const containerWidth = textRef.current.offsetWidth;
const textWidth = textRef.current.scrollWidth;

if (textWidth > containerWidth) {
const newFontSize = Math.floor(
(containerWidth / textWidth) * textFontSize,
);
setTextFontSize(newFontSize);
}
}
}, [textFontSize, shrinkOnOverflow]);

return isEditing ? (
<Input
inputRef={inputRef}
Expand All @@ -426,8 +445,9 @@ export function ModalTitle({
/>
) : (
<Text
innerRef={textRef}
style={{
fontSize: 25,
fontSize: textFontSize,
fontWeight: 700,
textAlign: 'center',
whiteSpace: 'nowrap',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// @ts-strict-ignore
import React, { useState } from 'react';
import React, { type ComponentProps, useState } from 'react';

import { styles } from '../../style';
import { Button } from '../common/Button';
Expand All @@ -12,7 +12,7 @@ import { type CommonModalProps } from '../Modals';

type SingleInputModalProps = {
modalProps: Partial<CommonModalProps>;
title: string;
title: ComponentProps<typeof Modal>['title'];
buttonText: string;
onSubmit: (value: string) => void;
onValidate?: (value: string) => string[];
Expand Down

0 comments on commit 7c84297

Please sign in to comment.