Skip to content

Commit f9a570f

Browse files
committed
fixed resizing for chrome and firefox
Signed-off-by: Maximilian Inckmann <[email protected]>
1 parent 6604eca commit f9a570f

File tree

4 files changed

+90
-19
lines changed

4 files changed

+90
-19
lines changed

packages/stencil-library/src/components/pid-collapsible/collapsible.css

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,12 @@ pid-collapsible.resize-both {
3030
resize: both !important;
3131
overflow: auto !important;
3232
max-width: 100% !important; /* Prevent exceeding parent container */
33+
max-height: 90vh !important; /* Prevent excessive vertical sizing */
3334
will-change: width, height; /* Hint to the browser to optimize for resizing */
3435
transition: none !important; /* Disable transitions during resize for better performance */
35-
min-height: fit-content; /* Ensure the component expands to fit its content */
36-
min-width: fit-content; /* Ensure the component expands to fit its content */
36+
min-height: min-content !important; /* Ensure the component doesn't shrink below content */
37+
min-width: min-content !important; /* Ensure the component doesn't shrink below content */
38+
height: fit-content !important; /* Set initial height to fit content exactly */
3739
}
3840

3941
/*

packages/stencil-library/src/components/pid-collapsible/pid-collapsible.tsx

Lines changed: 62 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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 (/^((?!chrome|android).)*safari/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
});

packages/stencil-library/src/components/pid-component/pid-component.css

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,15 @@ pid-component {
3232
-webkit-resize: both !important;
3333
resize: both !important;
3434
overflow: auto !important;
35+
max-height: fit-content !important; /* Fix Safari excessive height issue */
36+
height: fit-content !important; /* Ensure height matches content in Safari */
37+
}
38+
39+
/* Fix for high/narrow preview when openByDefault is set */
40+
pid-component[openbydefault] pid-collapsible {
41+
height: fit-content !important;
42+
min-height: fit-content !important;
43+
width: auto !important;
44+
min-width: 75% !important;
3545
}
3646
}

packages/stencil-library/src/components/pid-component/pid-component.tsx

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -194,21 +194,30 @@ export class PidComponent {
194194
this.ensureComponentId();
195195

196196
// Ensure collapsible gets proper initial width and open state after load
197+
// Use a longer delay to ensure DOM is fully rendered before recalculating
197198
setTimeout(() => {
198199
const collapsible = this.el.querySelector('pid-collapsible');
199200
if (collapsible) {
200201
// Set open state explicitly based on openByDefault property
201202
if (this.openByDefault) {
202203
(collapsible as any).open = true;
203204
(collapsible as any).expanded = true;
204-
}
205205

206-
// Recalculate dimensions after setting open state
207-
if (typeof (collapsible as any).recalculateContentDimensions === 'function') {
208-
(collapsible as any).recalculateContentDimensions();
206+
// When openByDefault is true, make sure the display correctly fills the space
207+
// by giving the DOM time to render before recalculating dimensions
208+
setTimeout(() => {
209+
if (typeof (collapsible as any).recalculateContentDimensions === 'function') {
210+
(collapsible as any).recalculateContentDimensions();
211+
}
212+
}, 100);
213+
} else {
214+
// For collapsed state, still recalculate dimensions
215+
if (typeof (collapsible as any).recalculateContentDimensions === 'function') {
216+
(collapsible as any).recalculateContentDimensions();
217+
}
209218
}
210219
}
211-
}, 0);
220+
}, 50); // Increase initial timeout to ensure proper rendering
212221
}
213222

214223
/**

0 commit comments

Comments
 (0)