Skip to content

Commit 95a373c

Browse files
committed
fix(toolbar): allow slot to size to content to account for images
1 parent 9131408 commit 95a373c

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

core/src/components/toolbar/toolbar.ionic.scss

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,12 @@
7070
// Ionic Toolbar Slot Placement
7171
// --------------------------------------------------
7272
// We're using the slots to force the main toolbar content to be
73-
// cenetered in the toolbar. This is a bit of a hack but it works.
73+
// centered in the toolbar. This is a bit of a hack but it works.
7474
// The main content is placed in the center and then JavaScript evaluates
7575
// the sizes of the different slots and sets what size the pairs should be
7676
// based on the larger one. We then use `flex-basis` to set the expected
7777
// size of the slots and disable `flex-shrink` so that the smaller slot cannot
78-
// shrink and throw off the center, we also diable flex-grow so that slots do
78+
// shrink and throw off the center, we also disable flex-grow so that slots do
7979
// not grow more than they need. The slots are paired up so the mirroring slots
8080
// always have the same size. That is, start and end are paired and primary
8181
// and secondary are paired. All of this works together to force the main

core/src/components/toolbar/toolbar.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,17 @@ export class Toolbar implements ComponentInterface {
112112
if (this.el.classList.contains(`has-${slot}-content`)) {
113113
const slotElement = this.el.shadowRoot?.querySelector(`slot[name="${slot}"]`) as HTMLElement | null;
114114
if (slotElement) {
115+
// Temporarily allow slot to size to content by setting flex-basis
116+
// to 'auto'. This ensures that slotted content (like images) can
117+
// render at their intrinsic width for measurement.
118+
const { name } = slotPairs.find((pair) => pair.slots.includes(slot))!;
119+
this.el.style.setProperty(`--${name}-size`, 'auto');
120+
115121
const width = slotElement.offsetWidth;
122+
123+
// Set the slot size variable to the measured width
116124
if (width > 0) {
125+
this.el.style.setProperty(`--${name}-size`, `${width}px`);
117126
slotWidths.set(slot, width);
118127
} else {
119128
allMeasurementsSuccessful = false;

0 commit comments

Comments
 (0)