@@ -167,9 +167,21 @@ export class PidCollapsible {
167167 componentWillLoad ( ) {
168168 // Initialize state from props
169169 this . expanded = this . open ;
170- // Default to 75% width if no initial width is provided
171- this . currentWidth = this . initialWidth || '75%' ; // Changed from DEFAULT_WIDTH to use 75% (w-3/4)
172- this . currentHeight = this . initialHeight || CONSTANTS . DEFAULT_HEIGHT ;
170+
171+ // Initialize dimensions but delay actual calculation until content is rendered
172+ if ( this . initialWidth ) {
173+ this . currentWidth = this . initialWidth ;
174+ } else {
175+ // Default to 75% width if no initial width is provided
176+ this . currentWidth = '75%' ;
177+ }
178+
179+ if ( this . initialHeight ) {
180+ this . currentHeight = this . initialHeight ;
181+ } else {
182+ // Use default height initially, will be recalculated after content renders
183+ this . currentHeight = CONSTANTS . DEFAULT_HEIGHT ;
184+ }
173185
174186 // Initialize dark mode
175187 this . initializeDarkMode ( ) ;
@@ -181,6 +193,15 @@ export class PidCollapsible {
181193 this . addBrowserCompatibilityListeners ( ) ;
182194 this . addComponentEventListeners ( ) ;
183195
196+ // When component is initially open, ensure dimensions are properly calculated after rendering
197+ if ( this . open ) {
198+ // Use a small delay to ensure the DOM is fully rendered
199+ setTimeout ( ( ) => {
200+ // Force recalculation of dimensions for open components
201+ this . recalculateContentDimensions ( ) ;
202+ } , 50 ) ;
203+ }
204+
184205 // Add clearfix for Safari - prevent text flow issues
185206 if ( / ^ ( (? ! c h r o m e | a n d r o i d ) .) * s a f a r i / i. test ( navigator . userAgent ) ) {
186207 this . el . style . display = 'inline-block' ;
@@ -593,12 +614,14 @@ export class PidCollapsible {
593614 */
594615 private calculateContentDimensions ( ) {
595616 const contentElement = this . el . querySelector ( '.flex-grow' ) ;
596- const contentWidth = contentElement ?. scrollWidth || CONSTANTS . MIN_WIDTH ;
597- const contentHeight = contentElement ?. scrollHeight || CONSTANTS . MIN_HEIGHT ;
617+ // Get the actual visible content width/height (not just scroll dimensions)
618+ const contentWidth = contentElement ? Math . max ( ( contentElement as HTMLElement ) . offsetWidth , contentElement ?. scrollWidth || 0 ) : CONSTANTS . MIN_WIDTH ;
619+ const contentHeight = contentElement ? Math . max ( ( contentElement as HTMLElement ) . offsetHeight , contentElement ?. scrollHeight || 0 ) : CONSTANTS . MIN_HEIGHT ;
598620
599621 // Add padding for better appearance, plus footer height if footer is shown
600622 const footerHeight = this . showFooter ? CONSTANTS . FOOTER_HEIGHT : 0 ;
601623 const maxWidth = contentWidth + CONSTANTS . PADDING_WIDTH ;
624+ // Use actual content height rather than adding arbitrary padding
602625 const maxHeight = contentHeight + CONSTANTS . PADDING_HEIGHT + footerHeight ;
603626
604627 return { contentWidth, contentHeight, maxWidth, maxHeight } ;
@@ -614,15 +637,34 @@ export class PidCollapsible {
614637
615638 const { contentWidth, contentHeight, maxWidth, maxHeight } = dimensions ;
616639
617- // Always set exact content dimensions to prevent resizing beyond content
618- // Ensure width is within minimum and maximum bounds
619- const optimalWidth = Math . min ( Math . max ( contentWidth + CONSTANTS . PADDING_WIDTH , CONSTANTS . MIN_WIDTH ) , maxWidth ) ;
620- this . currentWidth = `${ optimalWidth } px` ;
640+ // Calculate initial width based on content but prefer initialWidth if provided
641+ let optimalWidth ;
642+ if ( this . initialWidth ) {
643+ // Use initialWidth directly if provided
644+ this . currentWidth = this . initialWidth ;
645+ } else {
646+ // Calculate a content-based width with minimal padding
647+ optimalWidth = Math . min ( Math . max ( contentWidth + CONSTANTS . PADDING_WIDTH , CONSTANTS . MIN_WIDTH ) , maxWidth ) ;
648+ // Default to 75% if we don't have specific content dimensions
649+ this . currentWidth = contentWidth > 0 ? `${ optimalWidth } px` : '75%' ;
650+ }
621651
622- // Calculate optimal height including padding and footer if needed
652+ // Calculate optimal height based on actual content
623653 const footerHeight = this . showFooter ? CONSTANTS . FOOTER_HEIGHT : 0 ;
624- const optimalHeight = Math . min ( Math . max ( contentHeight + CONSTANTS . PADDING_HEIGHT + footerHeight , CONSTANTS . MIN_HEIGHT ) , maxHeight ) ;
625- this . currentHeight = `${ optimalHeight } px` ;
654+
655+ // Use more precise content height calculation to prevent extra space
656+ // Add minimal padding to prevent content being cut off
657+ const minPadding = 20 ; // Minimal padding to prevent content being cut off
658+ const calculatedHeight = contentHeight + minPadding + footerHeight ;
659+
660+ // Use initialHeight if provided, otherwise use calculated height
661+ if ( this . initialHeight ) {
662+ this . currentHeight = this . initialHeight ;
663+ } else {
664+ // Ensure height is between min height and max height constraints
665+ const optimalHeight = Math . min ( Math . max ( calculatedHeight , CONSTANTS . MIN_HEIGHT ) , maxHeight ) ;
666+ this . currentHeight = `${ optimalHeight } px` ;
667+ }
626668
627669 // Store these dimensions for future reference
628670 this . lastExpandedWidth = this . currentWidth ;
@@ -634,6 +676,14 @@ export class PidCollapsible {
634676 this . el . style . width = this . currentWidth ;
635677 this . el . style . height = this . currentHeight ;
636678
679+ // Set min-height/min-width to prevent resizing smaller than content
680+ this . el . style . minHeight = `${ Math . max ( calculatedHeight , CONSTANTS . MIN_HEIGHT ) } px` ;
681+ this . el . style . minWidth = `${ CONSTANTS . MIN_WIDTH } px` ;
682+
683+ // Also set max-height/max-width to prevent excessive resizing
684+ this . el . style . maxHeight = `${ maxHeight } px` ;
685+ this . el . style . maxWidth = `${ maxWidth } px` ;
686+
637687 // Remove the optimization class after updates are applied
638688 this . el . classList . remove ( 'resizing' ) ;
639689 } ) ;
0 commit comments