@ionic/core 9.0.11-dev.11794224641.1ae810e7 → 9.0.11-dev.11794308212.1f37a3b7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1615,6 +1615,7 @@ const Toolbar = class {
1615
1615
  constructor(hostRef) {
1616
1616
  index.registerInstance(this, hostRef);
1617
1617
  this.childrenStyles = new Map();
1618
+ this.slotNames = ['start', 'end', 'primary', 'secondary'];
1618
1619
  this.slotClasses = [
1619
1620
  'has-start-content',
1620
1621
  'has-end-content',
@@ -1623,6 +1624,7 @@ const Toolbar = class {
1623
1624
  ];
1624
1625
  this.showClasses = ['show-start', 'show-end', 'show-primary', 'show-secondary'];
1625
1626
  this.slotSizeVars = ['--start-end-size', '--primary-secondary-size'];
1627
+ this.didLoad = false;
1626
1628
  }
1627
1629
  componentWillLoad() {
1628
1630
  const buttons = Array.from(this.el.querySelectorAll('ion-buttons'));
@@ -1641,9 +1643,23 @@ const Toolbar = class {
1641
1643
  }
1642
1644
  this.updateSlotClasses();
1643
1645
  }
1646
+ connectedCallback() {
1647
+ /**
1648
+ * The resize observer is stopped when the toolbar is removed from the DOM.
1649
+ * If the toolbar is added back (for example, when it is moved), start it again.
1650
+ * On the first connection the toolbar has not rendered yet, so this is left
1651
+ * to componentDidLoad.
1652
+ */
1653
+ if (this.didLoad) {
1654
+ this.updateSlotClasses();
1655
+ }
1656
+ }
1644
1657
  componentDidLoad() {
1658
+ this.didLoad = true;
1645
1659
  this.updateSlotClasses();
1646
- this.updateSlotWidths();
1660
+ }
1661
+ disconnectedCallback() {
1662
+ this.stopObservingSlotSizes();
1647
1663
  }
1648
1664
  titlePlacementChanged() {
1649
1665
  this.updateSlotClasses();
@@ -1663,26 +1679,60 @@ const Toolbar = class {
1663
1679
  * Updates the CSS custom properties for slot widths
1664
1680
  * This ensures that slots shown by their met conditions
1665
1681
  * have a minimum width matching their required slot
1682
+ *
1683
+ * The slotted content is watched with a ResizeObserver, which runs
1684
+ * before paint and again whenever the content changes size, so the
1685
+ * title is centered on first paint and stays centered.
1666
1686
  */
1667
- updateSlotWidths(tries = 0) {
1668
- // Set timeout to try to execute after everything is rendered
1669
- setTimeout(() => {
1670
- // Attempt to measure and update
1671
- const success = this.measureAndUpdateSlots();
1672
- // If not all measurements were successful, try again in 100 ms
1673
- // cap recursion at 5 tries for safety
1674
- if (!success && tries < 5) {
1675
- setTimeout(() => {
1676
- this.updateSlotWidths(tries + 1);
1677
- }, 100);
1687
+ updateSlotWidths() {
1688
+ // Stop watching the previous content before watching the current one.
1689
+ this.stopObservingSlotSizes();
1690
+ // Browsers without ResizeObserver can only measure once.
1691
+ if (typeof ResizeObserver === 'undefined') {
1692
+ this.measureAndUpdateSlots();
1693
+ return;
1694
+ }
1695
+ const elementsToObserve = [];
1696
+ this.slotNames.forEach((slot) => {
1697
+ if (this.el.classList.contains(`has-${slot}-content`)) {
1698
+ elementsToObserve.push(...this.getSlottedElements(slot));
1678
1699
  }
1679
1700
  });
1701
+ if (elementsToObserve.length === 0) {
1702
+ return;
1703
+ }
1704
+ /**
1705
+ * The observer also calls the callback once right after observing
1706
+ * an element, which takes care of the first measurement.
1707
+ */
1708
+ const observer = new ResizeObserver(() => this.measureAndUpdateSlots());
1709
+ elementsToObserve.forEach((element) => observer.observe(element));
1710
+ this.slotResizeObserver = observer;
1711
+ }
1712
+ /**
1713
+ * Stops watching the slotted content for size changes.
1714
+ */
1715
+ stopObservingSlotSizes() {
1716
+ if (this.slotResizeObserver) {
1717
+ this.slotResizeObserver.disconnect();
1718
+ this.slotResizeObserver = undefined;
1719
+ }
1720
+ }
1721
+ /**
1722
+ * Returns the elements assigned to the given slot of the toolbar.
1723
+ */
1724
+ getSlottedElements(slotName) {
1725
+ const slotElement = this.getSlotElement(slotName);
1726
+ if (!slotElement) {
1727
+ return [];
1728
+ }
1729
+ return slotElement.assignedElements({ flatten: true });
1680
1730
  }
1681
1731
  /**
1682
1732
  * Measure the widths of the slots and update the CSS custom properties
1683
1733
  * for the minimum width of each pair of slots based on the largest width in each pair.
1684
- * Returns whether we successfully measured all of the slots we expect to have content.
1685
- * If not, the content probably hasn't rendered yet and we need to try again.
1734
+ * Slots that measure 0 (for example, in a hidden toolbar) are skipped. The
1735
+ * ResizeObserver measures them again when their content changes size.
1686
1736
  */
1687
1737
  measureAndUpdateSlots() {
1688
1738
  // Define the relationship between slots based on the conditions array
@@ -1691,14 +1741,19 @@ const Toolbar = class {
1691
1741
  { name: 'start-end', slots: ['start', 'end'] },
1692
1742
  { name: 'primary-secondary', slots: ['primary', 'secondary'] },
1693
1743
  ];
1744
+ /**
1745
+ * Once a size property is set, the slots cannot be narrower than it.
1746
+ * Remove the properties first so each slot is measured at the width of
1747
+ * its content, which lets the slots shrink when their content does.
1748
+ * This runs before paint, so the slots are never painted without a size.
1749
+ */
1750
+ this.slotSizeVars.forEach((cssVar) => this.el.style.removeProperty(cssVar));
1694
1751
  // First, measure all slot widths
1695
1752
  const slotWidths = new Map();
1696
- let allMeasurementsSuccessful = true;
1697
1753
  // Measure all slots with content
1698
- const slots = ['start', 'end', 'primary', 'secondary'];
1699
- slots.forEach((slot) => {
1754
+ this.slotNames.forEach((slot) => {
1700
1755
  if (this.el.classList.contains(`has-${slot}-content`)) {
1701
- const slotElement = this.el.shadowRoot?.querySelector(`slot[name="${slot}"]`);
1756
+ const slotElement = this.getSlotElement(slot);
1702
1757
  if (slotElement) {
1703
1758
  // Check if the slot contains an img or ion-img
1704
1759
  const assignedElements = slotElement.assignedElements({ flatten: true });
@@ -1720,9 +1775,6 @@ const Toolbar = class {
1720
1775
  if (width > 0) {
1721
1776
  slotWidths.set(slot, width);
1722
1777
  }
1723
- else {
1724
- allMeasurementsSuccessful = false;
1725
- }
1726
1778
  }
1727
1779
  }
1728
1780
  });
@@ -1743,12 +1795,13 @@ const Toolbar = class {
1743
1795
  this.el.style.setProperty(`--${name}-size`, `${maxWidth}px`);
1744
1796
  }
1745
1797
  });
1746
- return allMeasurementsSuccessful;
1747
1798
  }
1748
1799
  /**
1749
1800
  * Removes all slot visibility classes and slot width CSS variables.
1750
1801
  */
1751
1802
  removeSlotClasses() {
1803
+ // The slot widths are no longer used, so there is nothing to watch.
1804
+ this.stopObservingSlotSizes();
1752
1805
  this.el.classList.remove(...this.slotClasses, ...this.showClasses);
1753
1806
  this.slotSizeVars.forEach((cssVar) => this.el.style.removeProperty(cssVar));
1754
1807
  }
@@ -1759,10 +1812,9 @@ const Toolbar = class {
1759
1812
  return;
1760
1813
  }
1761
1814
  // Check if slots have content
1762
- const slots = ['start', 'end', 'primary', 'secondary'];
1763
1815
  const classesToAdd = [];
1764
1816
  const classesToRemove = [];
1765
- slots.forEach((slot) => {
1817
+ this.slotNames.forEach((slot) => {
1766
1818
  const slotHasContent = this.hasSlotContent(slot);
1767
1819
  const slotClass = `has-${slot}-content`;
1768
1820
  if (slotHasContent) {
@@ -1801,9 +1853,12 @@ const Toolbar = class {
1801
1853
  this.updateSlotWidths();
1802
1854
  }
1803
1855
  hasSlotContent(slotName) {
1804
- const slotNode = this.el.shadowRoot?.querySelector(`slot[name="${slotName}"]`);
1856
+ const slotNode = this.getSlotElement(slotName);
1805
1857
  return !!slotNode && slotNode.assignedNodes().length > 0;
1806
1858
  }
1859
+ getSlotElement(slotName) {
1860
+ return this.el.shadowRoot?.querySelector(`slot[name="${slotName}"]`) ?? null;
1861
+ }
1807
1862
  childrenStyle(ev) {
1808
1863
  ev.stopPropagation();
1809
1864
  const tagName = ev.target.tagName;
@@ -1833,14 +1888,14 @@ const Toolbar = class {
1833
1888
  Object.assign(childStyles, style);
1834
1889
  });
1835
1890
  const titlePlacement = this.getTitlePlacement();
1836
- return (index.h(index.Host, { key: '3b97d3de5b1910257170355995eb7a50a2b6dcc4', class: {
1891
+ return (index.h(index.Host, { key: '003f41d137af84bdf99fb40415925b3eee6404df', class: {
1837
1892
  ...theme.createColorClasses(this.color, {
1838
1893
  [theme$1]: true,
1839
1894
  'in-toolbar': theme.hostContext('ion-toolbar', this.el),
1840
1895
  [`toolbar-title-placement-${titlePlacement}`]: true,
1841
1896
  }),
1842
1897
  ...childStyles,
1843
- } }, index.h("div", { key: '26f85390aa62f118e585fd8560fba0620b30fd51', class: "toolbar-background", part: "background" }), index.h("div", { key: '57e32773d5642dde7f5d7cca9aaceb41b4d800bd', class: "toolbar-container", part: "container" }, index.h("slot", { key: '5429b3f898703339749a55af6bb28d7e4d969679', name: "start", onSlotchange: () => this.updateSlotClasses }), index.h("slot", { key: 'c5565f7020f52a1f075d9c6908ace042d488642f', name: "secondary", onSlotchange: () => this.updateSlotClasses }), index.h("div", { key: '2d083bcd57512041321a68b43d6b90144eb20166', class: "toolbar-content", part: "content" }, index.h("slot", { key: 'eb8abc0b9b96c699355891fbd0d284094cde7632' })), index.h("slot", { key: 'ca66f2dcce1f5382ed40a120cf32f759597a11ad', name: "primary", onSlotchange: () => this.updateSlotClasses }), index.h("slot", { key: '19e3eb792d45c21404fe48f119d68248b5d7dca7', name: "end", onSlotchange: () => this.updateSlotClasses }))));
1898
+ } }, index.h("div", { key: 'c4699e28eeb587f46e334fe6c28f027328035637', class: "toolbar-background", part: "background" }), index.h("div", { key: '1205f1292f892f689d6254f6bef2e8e29ab24737', class: "toolbar-container", part: "container" }, index.h("slot", { key: 'fd96057417db7d4c6bb5cbee1df2944c4566b99e', name: "start", onSlotchange: () => this.updateSlotClasses() }), index.h("slot", { key: '7ea7e907c0d1935683b662a5898692d3986f2090', name: "secondary", onSlotchange: () => this.updateSlotClasses() }), index.h("div", { key: 'e78ba31dbad2d9a7be9a336d7a81a1f8bedae34b', class: "toolbar-content", part: "content" }, index.h("slot", { key: 'dfe3ad669d9a6dfbdbd04fb6f81792dce8446866' })), index.h("slot", { key: '6de21658d26cd479114b44c16f1f921c0d7b3753', name: "primary", onSlotchange: () => this.updateSlotClasses() }), index.h("slot", { key: '176e7c6d4522f1dbc7ab2f390a4075b7a66307e9', name: "end", onSlotchange: () => this.updateSlotClasses() }))));
1844
1899
  }
1845
1900
  get el() { return index.getElement(this); }
1846
1901
  static get watchers() { return {
@@ -21,6 +21,7 @@ import { getIonTheme } from "../../global/ionic-global";
21
21
  export class Toolbar {
22
22
  constructor() {
23
23
  this.childrenStyles = new Map();
24
+ this.slotNames = ['start', 'end', 'primary', 'secondary'];
24
25
  this.slotClasses = [
25
26
  'has-start-content',
26
27
  'has-end-content',
@@ -29,6 +30,7 @@ export class Toolbar {
29
30
  ];
30
31
  this.showClasses = ['show-start', 'show-end', 'show-primary', 'show-secondary'];
31
32
  this.slotSizeVars = ['--start-end-size', '--primary-secondary-size'];
33
+ this.didLoad = false;
32
34
  }
33
35
  componentWillLoad() {
34
36
  const buttons = Array.from(this.el.querySelectorAll('ion-buttons'));
@@ -47,9 +49,23 @@ export class Toolbar {
47
49
  }
48
50
  this.updateSlotClasses();
49
51
  }
52
+ connectedCallback() {
53
+ /**
54
+ * The resize observer is stopped when the toolbar is removed from the DOM.
55
+ * If the toolbar is added back (for example, when it is moved), start it again.
56
+ * On the first connection the toolbar has not rendered yet, so this is left
57
+ * to componentDidLoad.
58
+ */
59
+ if (this.didLoad) {
60
+ this.updateSlotClasses();
61
+ }
62
+ }
50
63
  componentDidLoad() {
64
+ this.didLoad = true;
51
65
  this.updateSlotClasses();
52
- this.updateSlotWidths();
66
+ }
67
+ disconnectedCallback() {
68
+ this.stopObservingSlotSizes();
53
69
  }
54
70
  titlePlacementChanged() {
55
71
  this.updateSlotClasses();
@@ -69,26 +85,60 @@ export class Toolbar {
69
85
  * Updates the CSS custom properties for slot widths
70
86
  * This ensures that slots shown by their met conditions
71
87
  * have a minimum width matching their required slot
88
+ *
89
+ * The slotted content is watched with a ResizeObserver, which runs
90
+ * before paint and again whenever the content changes size, so the
91
+ * title is centered on first paint and stays centered.
72
92
  */
73
- updateSlotWidths(tries = 0) {
74
- // Set timeout to try to execute after everything is rendered
75
- setTimeout(() => {
76
- // Attempt to measure and update
77
- const success = this.measureAndUpdateSlots();
78
- // If not all measurements were successful, try again in 100 ms
79
- // cap recursion at 5 tries for safety
80
- if (!success && tries < 5) {
81
- setTimeout(() => {
82
- this.updateSlotWidths(tries + 1);
83
- }, 100);
93
+ updateSlotWidths() {
94
+ // Stop watching the previous content before watching the current one.
95
+ this.stopObservingSlotSizes();
96
+ // Browsers without ResizeObserver can only measure once.
97
+ if (typeof ResizeObserver === 'undefined') {
98
+ this.measureAndUpdateSlots();
99
+ return;
100
+ }
101
+ const elementsToObserve = [];
102
+ this.slotNames.forEach((slot) => {
103
+ if (this.el.classList.contains(`has-${slot}-content`)) {
104
+ elementsToObserve.push(...this.getSlottedElements(slot));
84
105
  }
85
106
  });
107
+ if (elementsToObserve.length === 0) {
108
+ return;
109
+ }
110
+ /**
111
+ * The observer also calls the callback once right after observing
112
+ * an element, which takes care of the first measurement.
113
+ */
114
+ const observer = new ResizeObserver(() => this.measureAndUpdateSlots());
115
+ elementsToObserve.forEach((element) => observer.observe(element));
116
+ this.slotResizeObserver = observer;
117
+ }
118
+ /**
119
+ * Stops watching the slotted content for size changes.
120
+ */
121
+ stopObservingSlotSizes() {
122
+ if (this.slotResizeObserver) {
123
+ this.slotResizeObserver.disconnect();
124
+ this.slotResizeObserver = undefined;
125
+ }
126
+ }
127
+ /**
128
+ * Returns the elements assigned to the given slot of the toolbar.
129
+ */
130
+ getSlottedElements(slotName) {
131
+ const slotElement = this.getSlotElement(slotName);
132
+ if (!slotElement) {
133
+ return [];
134
+ }
135
+ return slotElement.assignedElements({ flatten: true });
86
136
  }
87
137
  /**
88
138
  * Measure the widths of the slots and update the CSS custom properties
89
139
  * for the minimum width of each pair of slots based on the largest width in each pair.
90
- * Returns whether we successfully measured all of the slots we expect to have content.
91
- * If not, the content probably hasn't rendered yet and we need to try again.
140
+ * Slots that measure 0 (for example, in a hidden toolbar) are skipped. The
141
+ * ResizeObserver measures them again when their content changes size.
92
142
  */
93
143
  measureAndUpdateSlots() {
94
144
  // Define the relationship between slots based on the conditions array
@@ -97,14 +147,19 @@ export class Toolbar {
97
147
  { name: 'start-end', slots: ['start', 'end'] },
98
148
  { name: 'primary-secondary', slots: ['primary', 'secondary'] },
99
149
  ];
150
+ /**
151
+ * Once a size property is set, the slots cannot be narrower than it.
152
+ * Remove the properties first so each slot is measured at the width of
153
+ * its content, which lets the slots shrink when their content does.
154
+ * This runs before paint, so the slots are never painted without a size.
155
+ */
156
+ this.slotSizeVars.forEach((cssVar) => this.el.style.removeProperty(cssVar));
100
157
  // First, measure all slot widths
101
158
  const slotWidths = new Map();
102
- let allMeasurementsSuccessful = true;
103
159
  // Measure all slots with content
104
- const slots = ['start', 'end', 'primary', 'secondary'];
105
- slots.forEach((slot) => {
160
+ this.slotNames.forEach((slot) => {
106
161
  if (this.el.classList.contains(`has-${slot}-content`)) {
107
- const slotElement = this.el.shadowRoot?.querySelector(`slot[name="${slot}"]`);
162
+ const slotElement = this.getSlotElement(slot);
108
163
  if (slotElement) {
109
164
  // Check if the slot contains an img or ion-img
110
165
  const assignedElements = slotElement.assignedElements({ flatten: true });
@@ -126,9 +181,6 @@ export class Toolbar {
126
181
  if (width > 0) {
127
182
  slotWidths.set(slot, width);
128
183
  }
129
- else {
130
- allMeasurementsSuccessful = false;
131
- }
132
184
  }
133
185
  }
134
186
  });
@@ -149,12 +201,13 @@ export class Toolbar {
149
201
  this.el.style.setProperty(`--${name}-size`, `${maxWidth}px`);
150
202
  }
151
203
  });
152
- return allMeasurementsSuccessful;
153
204
  }
154
205
  /**
155
206
  * Removes all slot visibility classes and slot width CSS variables.
156
207
  */
157
208
  removeSlotClasses() {
209
+ // The slot widths are no longer used, so there is nothing to watch.
210
+ this.stopObservingSlotSizes();
158
211
  this.el.classList.remove(...this.slotClasses, ...this.showClasses);
159
212
  this.slotSizeVars.forEach((cssVar) => this.el.style.removeProperty(cssVar));
160
213
  }
@@ -165,10 +218,9 @@ export class Toolbar {
165
218
  return;
166
219
  }
167
220
  // Check if slots have content
168
- const slots = ['start', 'end', 'primary', 'secondary'];
169
221
  const classesToAdd = [];
170
222
  const classesToRemove = [];
171
- slots.forEach((slot) => {
223
+ this.slotNames.forEach((slot) => {
172
224
  const slotHasContent = this.hasSlotContent(slot);
173
225
  const slotClass = `has-${slot}-content`;
174
226
  if (slotHasContent) {
@@ -207,9 +259,12 @@ export class Toolbar {
207
259
  this.updateSlotWidths();
208
260
  }
209
261
  hasSlotContent(slotName) {
210
- const slotNode = this.el.shadowRoot?.querySelector(`slot[name="${slotName}"]`);
262
+ const slotNode = this.getSlotElement(slotName);
211
263
  return !!slotNode && slotNode.assignedNodes().length > 0;
212
264
  }
265
+ getSlotElement(slotName) {
266
+ return this.el.shadowRoot?.querySelector(`slot[name="${slotName}"]`) ?? null;
267
+ }
213
268
  childrenStyle(ev) {
214
269
  ev.stopPropagation();
215
270
  const tagName = ev.target.tagName;
@@ -239,14 +294,14 @@ export class Toolbar {
239
294
  Object.assign(childStyles, style);
240
295
  });
241
296
  const titlePlacement = this.getTitlePlacement();
242
- return (h(Host, { key: '3b97d3de5b1910257170355995eb7a50a2b6dcc4', class: {
297
+ return (h(Host, { key: '003f41d137af84bdf99fb40415925b3eee6404df', class: {
243
298
  ...createColorClasses(this.color, {
244
299
  [theme]: true,
245
300
  'in-toolbar': hostContext('ion-toolbar', this.el),
246
301
  [`toolbar-title-placement-${titlePlacement}`]: true,
247
302
  }),
248
303
  ...childStyles,
249
- } }, h("div", { key: '26f85390aa62f118e585fd8560fba0620b30fd51', class: "toolbar-background", part: "background" }), h("div", { key: '57e32773d5642dde7f5d7cca9aaceb41b4d800bd', class: "toolbar-container", part: "container" }, h("slot", { key: '5429b3f898703339749a55af6bb28d7e4d969679', name: "start", onSlotchange: () => this.updateSlotClasses }), h("slot", { key: 'c5565f7020f52a1f075d9c6908ace042d488642f', name: "secondary", onSlotchange: () => this.updateSlotClasses }), h("div", { key: '2d083bcd57512041321a68b43d6b90144eb20166', class: "toolbar-content", part: "content" }, h("slot", { key: 'eb8abc0b9b96c699355891fbd0d284094cde7632' })), h("slot", { key: 'ca66f2dcce1f5382ed40a120cf32f759597a11ad', name: "primary", onSlotchange: () => this.updateSlotClasses }), h("slot", { key: '19e3eb792d45c21404fe48f119d68248b5d7dca7', name: "end", onSlotchange: () => this.updateSlotClasses }))));
304
+ } }, h("div", { key: 'c4699e28eeb587f46e334fe6c28f027328035637', class: "toolbar-background", part: "background" }), h("div", { key: '1205f1292f892f689d6254f6bef2e8e29ab24737', class: "toolbar-container", part: "container" }, h("slot", { key: 'fd96057417db7d4c6bb5cbee1df2944c4566b99e', name: "start", onSlotchange: () => this.updateSlotClasses() }), h("slot", { key: '7ea7e907c0d1935683b662a5898692d3986f2090', name: "secondary", onSlotchange: () => this.updateSlotClasses() }), h("div", { key: 'e78ba31dbad2d9a7be9a336d7a81a1f8bedae34b', class: "toolbar-content", part: "content" }, h("slot", { key: 'dfe3ad669d9a6dfbdbd04fb6f81792dce8446866' })), h("slot", { key: '6de21658d26cd479114b44c16f1f921c0d7b3753', name: "primary", onSlotchange: () => this.updateSlotClasses() }), h("slot", { key: '176e7c6d4522f1dbc7ab2f390a4075b7a66307e9', name: "end", onSlotchange: () => this.updateSlotClasses() }))));
250
305
  }
251
306
  static get is() { return "ion-toolbar"; }
252
307
  static get encapsulation() { return "shadow"; }
package/dist/docs.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "timestamp": "2026-11-09T11:46:37",
2
+ "timestamp": "2026-11-10T10:59:33",
3
3
  "compiler": {
4
4
  "name": "@stencil/core",
5
5
  "version": "4.45.2",
@@ -1613,6 +1613,7 @@ const Toolbar = class {
1613
1613
  constructor(hostRef) {
1614
1614
  registerInstance(this, hostRef);
1615
1615
  this.childrenStyles = new Map();
1616
+ this.slotNames = ['start', 'end', 'primary', 'secondary'];
1616
1617
  this.slotClasses = [
1617
1618
  'has-start-content',
1618
1619
  'has-end-content',
@@ -1621,6 +1622,7 @@ const Toolbar = class {
1621
1622
  ];
1622
1623
  this.showClasses = ['show-start', 'show-end', 'show-primary', 'show-secondary'];
1623
1624
  this.slotSizeVars = ['--start-end-size', '--primary-secondary-size'];
1625
+ this.didLoad = false;
1624
1626
  }
1625
1627
  componentWillLoad() {
1626
1628
  const buttons = Array.from(this.el.querySelectorAll('ion-buttons'));
@@ -1639,9 +1641,23 @@ const Toolbar = class {
1639
1641
  }
1640
1642
  this.updateSlotClasses();
1641
1643
  }
1644
+ connectedCallback() {
1645
+ /**
1646
+ * The resize observer is stopped when the toolbar is removed from the DOM.
1647
+ * If the toolbar is added back (for example, when it is moved), start it again.
1648
+ * On the first connection the toolbar has not rendered yet, so this is left
1649
+ * to componentDidLoad.
1650
+ */
1651
+ if (this.didLoad) {
1652
+ this.updateSlotClasses();
1653
+ }
1654
+ }
1642
1655
  componentDidLoad() {
1656
+ this.didLoad = true;
1643
1657
  this.updateSlotClasses();
1644
- this.updateSlotWidths();
1658
+ }
1659
+ disconnectedCallback() {
1660
+ this.stopObservingSlotSizes();
1645
1661
  }
1646
1662
  titlePlacementChanged() {
1647
1663
  this.updateSlotClasses();
@@ -1661,26 +1677,60 @@ const Toolbar = class {
1661
1677
  * Updates the CSS custom properties for slot widths
1662
1678
  * This ensures that slots shown by their met conditions
1663
1679
  * have a minimum width matching their required slot
1680
+ *
1681
+ * The slotted content is watched with a ResizeObserver, which runs
1682
+ * before paint and again whenever the content changes size, so the
1683
+ * title is centered on first paint and stays centered.
1664
1684
  */
1665
- updateSlotWidths(tries = 0) {
1666
- // Set timeout to try to execute after everything is rendered
1667
- setTimeout(() => {
1668
- // Attempt to measure and update
1669
- const success = this.measureAndUpdateSlots();
1670
- // If not all measurements were successful, try again in 100 ms
1671
- // cap recursion at 5 tries for safety
1672
- if (!success && tries < 5) {
1673
- setTimeout(() => {
1674
- this.updateSlotWidths(tries + 1);
1675
- }, 100);
1685
+ updateSlotWidths() {
1686
+ // Stop watching the previous content before watching the current one.
1687
+ this.stopObservingSlotSizes();
1688
+ // Browsers without ResizeObserver can only measure once.
1689
+ if (typeof ResizeObserver === 'undefined') {
1690
+ this.measureAndUpdateSlots();
1691
+ return;
1692
+ }
1693
+ const elementsToObserve = [];
1694
+ this.slotNames.forEach((slot) => {
1695
+ if (this.el.classList.contains(`has-${slot}-content`)) {
1696
+ elementsToObserve.push(...this.getSlottedElements(slot));
1676
1697
  }
1677
1698
  });
1699
+ if (elementsToObserve.length === 0) {
1700
+ return;
1701
+ }
1702
+ /**
1703
+ * The observer also calls the callback once right after observing
1704
+ * an element, which takes care of the first measurement.
1705
+ */
1706
+ const observer = new ResizeObserver(() => this.measureAndUpdateSlots());
1707
+ elementsToObserve.forEach((element) => observer.observe(element));
1708
+ this.slotResizeObserver = observer;
1709
+ }
1710
+ /**
1711
+ * Stops watching the slotted content for size changes.
1712
+ */
1713
+ stopObservingSlotSizes() {
1714
+ if (this.slotResizeObserver) {
1715
+ this.slotResizeObserver.disconnect();
1716
+ this.slotResizeObserver = undefined;
1717
+ }
1718
+ }
1719
+ /**
1720
+ * Returns the elements assigned to the given slot of the toolbar.
1721
+ */
1722
+ getSlottedElements(slotName) {
1723
+ const slotElement = this.getSlotElement(slotName);
1724
+ if (!slotElement) {
1725
+ return [];
1726
+ }
1727
+ return slotElement.assignedElements({ flatten: true });
1678
1728
  }
1679
1729
  /**
1680
1730
  * Measure the widths of the slots and update the CSS custom properties
1681
1731
  * for the minimum width of each pair of slots based on the largest width in each pair.
1682
- * Returns whether we successfully measured all of the slots we expect to have content.
1683
- * If not, the content probably hasn't rendered yet and we need to try again.
1732
+ * Slots that measure 0 (for example, in a hidden toolbar) are skipped. The
1733
+ * ResizeObserver measures them again when their content changes size.
1684
1734
  */
1685
1735
  measureAndUpdateSlots() {
1686
1736
  // Define the relationship between slots based on the conditions array
@@ -1689,14 +1739,19 @@ const Toolbar = class {
1689
1739
  { name: 'start-end', slots: ['start', 'end'] },
1690
1740
  { name: 'primary-secondary', slots: ['primary', 'secondary'] },
1691
1741
  ];
1742
+ /**
1743
+ * Once a size property is set, the slots cannot be narrower than it.
1744
+ * Remove the properties first so each slot is measured at the width of
1745
+ * its content, which lets the slots shrink when their content does.
1746
+ * This runs before paint, so the slots are never painted without a size.
1747
+ */
1748
+ this.slotSizeVars.forEach((cssVar) => this.el.style.removeProperty(cssVar));
1692
1749
  // First, measure all slot widths
1693
1750
  const slotWidths = new Map();
1694
- let allMeasurementsSuccessful = true;
1695
1751
  // Measure all slots with content
1696
- const slots = ['start', 'end', 'primary', 'secondary'];
1697
- slots.forEach((slot) => {
1752
+ this.slotNames.forEach((slot) => {
1698
1753
  if (this.el.classList.contains(`has-${slot}-content`)) {
1699
- const slotElement = this.el.shadowRoot?.querySelector(`slot[name="${slot}"]`);
1754
+ const slotElement = this.getSlotElement(slot);
1700
1755
  if (slotElement) {
1701
1756
  // Check if the slot contains an img or ion-img
1702
1757
  const assignedElements = slotElement.assignedElements({ flatten: true });
@@ -1718,9 +1773,6 @@ const Toolbar = class {
1718
1773
  if (width > 0) {
1719
1774
  slotWidths.set(slot, width);
1720
1775
  }
1721
- else {
1722
- allMeasurementsSuccessful = false;
1723
- }
1724
1776
  }
1725
1777
  }
1726
1778
  });
@@ -1741,12 +1793,13 @@ const Toolbar = class {
1741
1793
  this.el.style.setProperty(`--${name}-size`, `${maxWidth}px`);
1742
1794
  }
1743
1795
  });
1744
- return allMeasurementsSuccessful;
1745
1796
  }
1746
1797
  /**
1747
1798
  * Removes all slot visibility classes and slot width CSS variables.
1748
1799
  */
1749
1800
  removeSlotClasses() {
1801
+ // The slot widths are no longer used, so there is nothing to watch.
1802
+ this.stopObservingSlotSizes();
1750
1803
  this.el.classList.remove(...this.slotClasses, ...this.showClasses);
1751
1804
  this.slotSizeVars.forEach((cssVar) => this.el.style.removeProperty(cssVar));
1752
1805
  }
@@ -1757,10 +1810,9 @@ const Toolbar = class {
1757
1810
  return;
1758
1811
  }
1759
1812
  // Check if slots have content
1760
- const slots = ['start', 'end', 'primary', 'secondary'];
1761
1813
  const classesToAdd = [];
1762
1814
  const classesToRemove = [];
1763
- slots.forEach((slot) => {
1815
+ this.slotNames.forEach((slot) => {
1764
1816
  const slotHasContent = this.hasSlotContent(slot);
1765
1817
  const slotClass = `has-${slot}-content`;
1766
1818
  if (slotHasContent) {
@@ -1799,9 +1851,12 @@ const Toolbar = class {
1799
1851
  this.updateSlotWidths();
1800
1852
  }
1801
1853
  hasSlotContent(slotName) {
1802
- const slotNode = this.el.shadowRoot?.querySelector(`slot[name="${slotName}"]`);
1854
+ const slotNode = this.getSlotElement(slotName);
1803
1855
  return !!slotNode && slotNode.assignedNodes().length > 0;
1804
1856
  }
1857
+ getSlotElement(slotName) {
1858
+ return this.el.shadowRoot?.querySelector(`slot[name="${slotName}"]`) ?? null;
1859
+ }
1805
1860
  childrenStyle(ev) {
1806
1861
  ev.stopPropagation();
1807
1862
  const tagName = ev.target.tagName;
@@ -1831,14 +1886,14 @@ const Toolbar = class {
1831
1886
  Object.assign(childStyles, style);
1832
1887
  });
1833
1888
  const titlePlacement = this.getTitlePlacement();
1834
- return (h(Host, { key: '3b97d3de5b1910257170355995eb7a50a2b6dcc4', class: {
1889
+ return (h(Host, { key: '003f41d137af84bdf99fb40415925b3eee6404df', class: {
1835
1890
  ...createColorClasses(this.color, {
1836
1891
  [theme]: true,
1837
1892
  'in-toolbar': hostContext('ion-toolbar', this.el),
1838
1893
  [`toolbar-title-placement-${titlePlacement}`]: true,
1839
1894
  }),
1840
1895
  ...childStyles,
1841
- } }, h("div", { key: '26f85390aa62f118e585fd8560fba0620b30fd51', class: "toolbar-background", part: "background" }), h("div", { key: '57e32773d5642dde7f5d7cca9aaceb41b4d800bd', class: "toolbar-container", part: "container" }, h("slot", { key: '5429b3f898703339749a55af6bb28d7e4d969679', name: "start", onSlotchange: () => this.updateSlotClasses }), h("slot", { key: 'c5565f7020f52a1f075d9c6908ace042d488642f', name: "secondary", onSlotchange: () => this.updateSlotClasses }), h("div", { key: '2d083bcd57512041321a68b43d6b90144eb20166', class: "toolbar-content", part: "content" }, h("slot", { key: 'eb8abc0b9b96c699355891fbd0d284094cde7632' })), h("slot", { key: 'ca66f2dcce1f5382ed40a120cf32f759597a11ad', name: "primary", onSlotchange: () => this.updateSlotClasses }), h("slot", { key: '19e3eb792d45c21404fe48f119d68248b5d7dca7', name: "end", onSlotchange: () => this.updateSlotClasses }))));
1896
+ } }, h("div", { key: 'c4699e28eeb587f46e334fe6c28f027328035637', class: "toolbar-background", part: "background" }), h("div", { key: '1205f1292f892f689d6254f6bef2e8e29ab24737', class: "toolbar-container", part: "container" }, h("slot", { key: 'fd96057417db7d4c6bb5cbee1df2944c4566b99e', name: "start", onSlotchange: () => this.updateSlotClasses() }), h("slot", { key: '7ea7e907c0d1935683b662a5898692d3986f2090', name: "secondary", onSlotchange: () => this.updateSlotClasses() }), h("div", { key: 'e78ba31dbad2d9a7be9a336d7a81a1f8bedae34b', class: "toolbar-content", part: "content" }, h("slot", { key: 'dfe3ad669d9a6dfbdbd04fb6f81792dce8446866' })), h("slot", { key: '6de21658d26cd479114b44c16f1f921c0d7b3753', name: "primary", onSlotchange: () => this.updateSlotClasses() }), h("slot", { key: '176e7c6d4522f1dbc7ab2f390a4075b7a66307e9', name: "end", onSlotchange: () => this.updateSlotClasses() }))));
1842
1897
  }
1843
1898
  get el() { return getElement(this); }
1844
1899
  static get watchers() { return {