@schukai/monster 4.99.0 → 4.100.1

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.
package/CHANGELOG.md CHANGED
@@ -2,6 +2,25 @@
2
2
 
3
3
 
4
4
 
5
+ ## [4.100.1] - 2026-01-16
6
+
7
+ ### Bug Fixes
8
+
9
+ - config values [#375](https://gitlab.schukai.com/oss/libraries/javascript/monster/issues/375)
10
+ ### Changes
11
+
12
+ - doc
13
+
14
+
15
+
16
+ ## [4.100.0] - 2026-01-16
17
+
18
+ ### Add Features
19
+
20
+ - Add confirmation button and API feedback for issue [#375](https://gitlab.schukai.com/oss/libraries/javascript/monster/issues/375)
21
+
22
+
23
+
5
24
  ## [4.99.0] - 2026-01-15
6
25
 
7
26
  ### Add Features
package/package.json CHANGED
@@ -1 +1 @@
1
- {"author":"Volker Schukai","dependencies":{"@floating-ui/dom":"^1.7.4","@popperjs/core":"^2.11.8"},"description":"Monster is a simple library for creating fast, robust and lightweight websites.","homepage":"https://monsterjs.org/","keywords":["framework","web","dom","css","sass","mobile-first","app","front-end","templates","schukai","core","shopcloud","alvine","monster","buildmap","stack","observer","observable","uuid","node","nodelist","css-in-js","logger","log","theme"],"license":"AGPL 3.0","main":"source/monster.mjs","module":"source/monster.mjs","name":"@schukai/monster","repository":{"type":"git","url":"https://gitlab.schukai.com/oss/libraries/javascript/monster.git"},"type":"module","version":"4.99.0"}
1
+ {"author":"Volker Schukai","dependencies":{"@floating-ui/dom":"^1.7.4","@popperjs/core":"^2.11.8"},"description":"Monster is a simple library for creating fast, robust and lightweight websites.","homepage":"https://monsterjs.org/","keywords":["framework","web","dom","css","sass","mobile-first","app","front-end","templates","schukai","core","shopcloud","alvine","monster","buildmap","stack","observer","observable","uuid","node","nodelist","css-in-js","logger","log","theme"],"license":"AGPL 3.0","main":"source/monster.mjs","module":"source/monster.mjs","name":"@schukai/monster","repository":{"type":"git","url":"https://gitlab.schukai.com/oss/libraries/javascript/monster.git"},"type":"module","version":"4.100.1"}
@@ -15,9 +15,9 @@
15
15
  import { instanceSymbol } from "../../constants.mjs";
16
16
  import { ATTRIBUTE_DISABLED, ATTRIBUTE_ROLE } from "../../dom/constants.mjs";
17
17
  import {
18
- assembleMethodSymbol,
19
- attributeObserverSymbol,
20
- registerCustomElement,
18
+ assembleMethodSymbol,
19
+ attributeObserverSymbol,
20
+ registerCustomElement,
21
21
  } from "../../dom/customelement.mjs";
22
22
  import { isArray, isString } from "../../types/is.mjs";
23
23
  import { validateString } from "../../types/validate.mjs";
@@ -36,6 +36,9 @@ export { MessageStateButton };
36
36
  */
37
37
  const buttonElementSymbol = Symbol("buttonElement");
38
38
  const innerDisabledObserverSymbol = Symbol("innerDisabledObserver");
39
+ const popperElementSymbol = Symbol("popperElement");
40
+ const messageElementSymbol = Symbol("messageElement");
41
+ const measurementPopperSymbol = Symbol("measurementPopper");
39
42
 
40
43
  /**
41
44
  * A specialized button component that combines state management with message display capabilities.
@@ -54,312 +57,326 @@ const innerDisabledObserverSymbol = Symbol("innerDisabledObserver");
54
57
  * @fires monster-click - Fired when button is clicked
55
58
  */
56
59
  class MessageStateButton extends Popper {
57
- /**
58
- * This method is called by the `instanceof` operator.
59
- * @return {symbol}
60
- */
61
- static get [instanceSymbol]() {
62
- return Symbol.for(
63
- "@schukai/monster/components/form/message-state-button@@instance",
64
- );
65
- }
66
-
67
- /**
68
- * Sets the state of the button which affects its visual appearance
69
- *
70
- * @param {string} state - The state to set (e.g. 'success', 'error', 'loading')
71
- * @param {number} timeout - Optional timeout in milliseconds after which state is removed
72
- * @return {MessageStateButton} Returns the button instance for chaining
73
- * @throws {TypeError} When state is not a string or timeout is not a number
74
- */
75
- setState(state, timeout) {
76
- return this[buttonElementSymbol].setState(state, timeout);
77
- }
78
-
79
- /**
80
- *
81
- * @return {MessageStateButton}
82
- */
83
- removeState() {
84
- return this[buttonElementSymbol].removeState();
85
- }
86
-
87
- /**
88
- * @return {MessageStateButton|undefined}
89
- */
90
- getState() {
91
- return this[buttonElementSymbol].getState();
92
- }
93
-
94
- /**
95
- * To set the options via the HTML tag, the attribute `data-monster-options` must be used.
96
- * @see {@link https://monsterjs.org/en/doc/#configurate-a-monster-control}
97
- *
98
- * The individual configuration values can be found in the table.
99
- *
100
- * @property {Object} templates Template definitions
101
- * @property {string} templates.main Main template
102
- * @property {Object} message Message definition
103
- * @property {string|HTMLElement} message.content The message content
104
- * @property {string} message.title The message title
105
- * @property {string} message.icon The message icon
106
- * @property {string} mode The mode of the button, can be `manual` or `submit`
107
- * @property {string} labels.button Button label
108
- * @property {boolean} features.disableButton Disable the button
109
- * @property {Object} aria Aria attributes
110
- * @property {string} aria.role Aria role, only if the button is not a button
111
- */
112
- get defaults() {
113
- return Object.assign({}, super.defaults, {
114
- message: {
115
- title: undefined,
116
- content: undefined,
117
- icon: undefined,
118
- },
119
- templates: {
120
- main: getTemplate(),
121
- },
122
- mode: "manual",
123
- labels: {
124
- button: "<slot></slot>",
125
- },
126
- classes: {
127
- button: "monster-button-outline-primary",
128
- },
129
- actions: {
130
- click: (e) => {},
131
- },
132
-
133
- aria: {
134
- role: null,
135
- label: null,
136
- },
137
- });
138
- }
139
-
140
- /**
141
- * @return {void}
142
- */
143
- [assembleMethodSymbol]() {
144
- super[assembleMethodSymbol]();
145
- initControlReferences.call(this);
146
- initDisabledSync.call(this);
147
-
148
- let modes = null;
149
- const modeOption = this.getOption("mode");
150
- if (typeof modeOption === "string") {
151
- modes = modeOption.split(" ");
152
- }
153
-
154
- if (
155
- modes === null ||
156
- modes === undefined ||
157
- isArray(modes) === false ||
158
- modes.length === 0
159
- ) {
160
- modes = ["manual"];
161
- }
162
-
163
- for (const [, mode] of Object.entries(modes)) {
164
- initEventHandlerByMode.call(this, mode);
165
- }
166
- }
167
-
168
- /**
169
- * Sets the message content to be displayed in the popup overlay
170
- *
171
- * @param {string|HTMLElement} message - The message content as string or HTML element
172
- * @param {string} title - Optional title to show above the message
173
- * @param {string} icon - Optional icon HTML to display next to the title
174
- * @return {MessageStateButton} Returns the button instance for chaining
175
- * @throws {TypeError} When message is empty or invalid type
176
- */
177
- setMessage(message, title, icon) {
178
- if (isString(message)) {
179
- if (message === "") {
180
- throw new TypeError("message must not be empty");
181
- }
182
-
183
- const containerDiv = document.createElement("div");
184
- const messageDiv = document.createElement("div");
185
- const titleDiv = document.createElement("div");
186
- titleDiv.setAttribute(ATTRIBUTE_ROLE, "message-title-box");
187
-
188
- let titleElement, iconElement;
189
- if (title !== undefined) {
190
- title = validateString(title);
191
- titleElement = document.createElement("div");
192
- titleElement.setAttribute("class", "");
193
- titleElement.innerHTML = title;
194
- titleElement.setAttribute(ATTRIBUTE_ROLE, "message-title");
195
- titleDiv.appendChild(titleElement);
196
- }
197
-
198
- if (icon !== undefined) {
199
- icon = validateString(icon);
200
- iconElement = document.createElement("div");
201
- iconElement.setAttribute("class", "");
202
- iconElement.innerHTML = icon;
203
- iconElement.setAttribute(ATTRIBUTE_ROLE, "message-icon");
204
- titleDiv.appendChild(iconElement);
205
- }
206
-
207
- messageDiv.innerHTML = message;
208
- containerDiv.appendChild(titleDiv);
209
- containerDiv.appendChild(messageDiv);
210
-
211
- this.setOption("message.content", containerDiv);
212
- } else if (message instanceof HTMLElement) {
213
- this.setOption("message.content", message);
214
- } else {
215
- throw new TypeError(
216
- "message must be a string or an instance of HTMLElement",
217
- );
218
- }
219
-
220
- return this;
221
- }
222
-
223
- /**
224
- * clears the Message
225
- *
226
- * @return {MessageStateButton}
227
- */
228
- clearMessage() {
229
- this.setOption("message.title", undefined);
230
- this.setOption("message.content", undefined);
231
- this.setOption("message.icon", undefined);
232
- return this;
233
- }
234
-
235
- /**
236
- * Shows the message popup overlay with optional auto-hide timeout
237
- *
238
- * @param {number} timeout - Optional time in milliseconds after which the message will auto-hide
239
- * @return {MessageStateButton} Returns the button instance for chaining
240
- */
241
- showMessage(timeout) {
242
- this.showDialog.call(this);
243
-
244
- if (timeout !== undefined) {
245
- setTimeout(() => {
246
- super.hideDialog();
247
- }, timeout);
248
- }
249
-
250
- return this;
251
- }
252
-
253
- /**
254
- * With this method, you can show the popper.
255
- *
256
- * @return {MessageStateButton}
257
- */
258
- showDialog() {
259
- if (this.getOption("message.content") === undefined) {
260
- return;
261
- }
262
- super.showDialog();
263
- return this;
264
- }
265
-
266
- /**
267
- *
268
- * @return {MessageStateButton}
269
- */
270
- hideMessage() {
271
- super.hideDialog();
272
- return this;
273
- }
274
-
275
- /**
276
- *
277
- * @return {MessageStateButton}
278
- */
279
- toggleMessage() {
280
- super.toggleDialog();
281
- return this;
282
- }
283
-
284
- /**
285
- *
286
- * @return {Object}
287
- */
288
- getMessage() {
289
- return this.getOption("message");
290
- }
291
-
292
- /**
293
- *
294
- * @return {string}
295
- */
296
- static getTag() {
297
- return "monster-message-state-button";
298
- }
299
-
300
- /**
301
- *
302
- * @return {CSSStyleSheet[]}
303
- */
304
- static getCSSStyleSheet() {
305
- const styles = Popper.getCSSStyleSheet();
306
- styles.push(StateButtonStyleSheet);
307
- styles.push(MessageStateButtonStyleSheet);
308
- return styles;
309
- }
310
-
311
- /**
312
- * Programmatically triggers a click event on the button
313
- * Will not trigger if the button is disabled
314
- *
315
- * @since 3.27.0
316
- * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/click}
317
- * @fires monster-click
318
- */
319
- click() {
320
- if (this.getOption("disabled") === true) {
321
- return;
322
- }
323
-
324
- if (
325
- this[buttonElementSymbol] &&
326
- isFunction(this[buttonElementSymbol].click)
327
- ) {
328
- this[buttonElementSymbol].click();
329
- }
330
- }
331
-
332
- /**
333
- * The Button.focus() method sets focus on the internal button element.
334
- *
335
- * @since 3.27.0
336
- * @param {Object} options
337
- * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/focus}
338
- */
339
- focus(options) {
340
- if (this.getOption("disabled") === true) {
341
- return;
342
- }
343
-
344
- if (
345
- this[buttonElementSymbol] &&
346
- isFunction(this[buttonElementSymbol].focus)
347
- ) {
348
- this[buttonElementSymbol].focus(options);
349
- }
350
- }
351
-
352
- /**
353
- * The Button.blur() method removes focus from the internal button element.
354
- */
355
- blur() {
356
- if (
357
- this[buttonElementSymbol] &&
358
- isFunction(this[buttonElementSymbol].blur)
359
- ) {
360
- this[buttonElementSymbol].blur();
361
- }
362
- }
60
+ /**
61
+ * This method is called by the `instanceof` operator.
62
+ * @return {symbol}
63
+ */
64
+ static get [instanceSymbol]() {
65
+ return Symbol.for(
66
+ "@schukai/monster/components/form/message-state-button@@instance",
67
+ );
68
+ }
69
+
70
+ /**
71
+ * Sets the state of the button which affects its visual appearance
72
+ *
73
+ * @param {string} state - The state to set (e.g. 'success', 'error', 'loading')
74
+ * @param {number} timeout - Optional timeout in milliseconds after which state is removed
75
+ * @return {MessageStateButton} Returns the button instance for chaining
76
+ * @throws {TypeError} When state is not a string or timeout is not a number
77
+ */
78
+ setState(state, timeout) {
79
+ return this[buttonElementSymbol].setState(state, timeout);
80
+ }
81
+
82
+ /**
83
+ *
84
+ * @return {MessageStateButton}
85
+ */
86
+ removeState() {
87
+ return this[buttonElementSymbol].removeState();
88
+ }
89
+
90
+ /**
91
+ * @return {MessageStateButton|undefined}
92
+ */
93
+ getState() {
94
+ return this[buttonElementSymbol].getState();
95
+ }
96
+
97
+ /**
98
+ * To set the options via the HTML tag, the attribute `data-monster-options` must be used.
99
+ * @see {@link https://monsterjs.org/en/doc/#configurate-a-monster-control}
100
+ *
101
+ * The individual configuration values can be found in the table.
102
+ *
103
+ * @property {Object} templates Template definitions
104
+ * @property {string} templates.main Main template
105
+ * @property {Object} message Message definition
106
+ * @property {string|HTMLElement} message.content The message content
107
+ * @property {string} message.title The message title
108
+ * @property {string} message.icon The message icon
109
+ * @property {Object} message.width Width options for the message popper
110
+ * @property {string|number} message.width.min Minimum width (px, rem, em, vw)
111
+ * @property {string|number} message.width.max Maximum width (px, rem, em, vw)
112
+ * @property {number} message.width.viewportRatio Max width as ratio of viewport width (0-1)
113
+ * @property {string} mode The mode of the button, can be `manual` or `submit`
114
+ * @property {string} labels.button Button label
115
+ * @property {Object} classes Classes for internal elements
116
+ * @property {string} classes.button Button class
117
+ * @property {Object} actions Action callbacks
118
+ * @property {function} actions.click Action triggered on click
119
+ * @property {Object} aria Aria attributes
120
+ * @property {string} aria.role Aria role, only if the button is not a button
121
+ * @property {string} aria.label Aria label for the button
122
+ */
123
+ get defaults() {
124
+ return Object.assign({}, super.defaults, {
125
+ message: {
126
+ title: undefined,
127
+ content: undefined,
128
+ icon: undefined,
129
+ width: {
130
+ min: "12rem",
131
+ max: "32rem",
132
+ viewportRatio: 0.7,
133
+ },
134
+ },
135
+ templates: {
136
+ main: getTemplate(),
137
+ },
138
+ mode: "manual",
139
+ labels: {
140
+ button: "<slot></slot>",
141
+ },
142
+ classes: {
143
+ button: "monster-button-outline-primary",
144
+ },
145
+ actions: {
146
+ click: (e) => {},
147
+ },
148
+
149
+ aria: {
150
+ role: null,
151
+ label: null,
152
+ },
153
+ });
154
+ }
155
+
156
+ /**
157
+ * @return {void}
158
+ */
159
+ [assembleMethodSymbol]() {
160
+ super[assembleMethodSymbol]();
161
+ initControlReferences.call(this);
162
+ initDisabledSync.call(this);
163
+
164
+ let modes = null;
165
+ const modeOption = this.getOption("mode");
166
+ if (typeof modeOption === "string") {
167
+ modes = modeOption.split(" ");
168
+ }
169
+
170
+ if (
171
+ modes === null ||
172
+ modes === undefined ||
173
+ isArray(modes) === false ||
174
+ modes.length === 0
175
+ ) {
176
+ modes = ["manual"];
177
+ }
178
+
179
+ for (const [, mode] of Object.entries(modes)) {
180
+ initEventHandlerByMode.call(this, mode);
181
+ }
182
+ }
183
+
184
+ /**
185
+ * Sets the message content to be displayed in the popup overlay
186
+ *
187
+ * @param {string|HTMLElement} message - The message content as string or HTML element
188
+ * @param {string} title - Optional title to show above the message
189
+ * @param {string} icon - Optional icon HTML to display next to the title
190
+ * @return {MessageStateButton} Returns the button instance for chaining
191
+ * @throws {TypeError} When message is empty or invalid type
192
+ */
193
+ setMessage(message, title, icon) {
194
+ if (isString(message)) {
195
+ if (message === "") {
196
+ throw new TypeError("message must not be empty");
197
+ }
198
+
199
+ const containerDiv = document.createElement("div");
200
+ const messageDiv = document.createElement("div");
201
+ const titleDiv = document.createElement("div");
202
+ titleDiv.setAttribute(ATTRIBUTE_ROLE, "message-title-box");
203
+
204
+ let titleElement, iconElement;
205
+ if (title !== undefined) {
206
+ title = validateString(title);
207
+ titleElement = document.createElement("div");
208
+ titleElement.setAttribute("class", "");
209
+ titleElement.innerHTML = title;
210
+ titleElement.setAttribute(ATTRIBUTE_ROLE, "message-title");
211
+ titleDiv.appendChild(titleElement);
212
+ }
213
+
214
+ if (icon !== undefined) {
215
+ icon = validateString(icon);
216
+ iconElement = document.createElement("div");
217
+ iconElement.setAttribute("class", "");
218
+ iconElement.innerHTML = icon;
219
+ iconElement.setAttribute(ATTRIBUTE_ROLE, "message-icon");
220
+ titleDiv.appendChild(iconElement);
221
+ }
222
+
223
+ messageDiv.innerHTML = message;
224
+ containerDiv.appendChild(titleDiv);
225
+ containerDiv.appendChild(messageDiv);
226
+
227
+ this.setOption("message.content", containerDiv);
228
+ } else if (message instanceof HTMLElement) {
229
+ this.setOption("message.content", message);
230
+ } else {
231
+ throw new TypeError(
232
+ "message must be a string or an instance of HTMLElement",
233
+ );
234
+ }
235
+
236
+ return this;
237
+ }
238
+
239
+ /**
240
+ * clears the Message
241
+ *
242
+ * @return {MessageStateButton}
243
+ */
244
+ clearMessage() {
245
+ this.setOption("message.title", undefined);
246
+ this.setOption("message.content", undefined);
247
+ this.setOption("message.icon", undefined);
248
+ return this;
249
+ }
250
+
251
+ /**
252
+ * Shows the message popup overlay with optional auto-hide timeout
253
+ *
254
+ * @param {number} timeout - Optional time in milliseconds after which the message will auto-hide
255
+ * @return {MessageStateButton} Returns the button instance for chaining
256
+ */
257
+ showMessage(timeout) {
258
+ applyMeasuredMessageWidth.call(this);
259
+ this.showDialog.call(this);
260
+
261
+ if (timeout !== undefined) {
262
+ setTimeout(() => {
263
+ super.hideDialog();
264
+ }, timeout);
265
+ }
266
+
267
+ return this;
268
+ }
269
+
270
+ /**
271
+ * With this method, you can show the popper.
272
+ *
273
+ * @return {MessageStateButton}
274
+ */
275
+ showDialog() {
276
+ if (this.getOption("message.content") === undefined) {
277
+ return;
278
+ }
279
+ super.showDialog();
280
+ return this;
281
+ }
282
+
283
+ /**
284
+ *
285
+ * @return {MessageStateButton}
286
+ */
287
+ hideMessage() {
288
+ super.hideDialog();
289
+ return this;
290
+ }
291
+
292
+ /**
293
+ *
294
+ * @return {MessageStateButton}
295
+ */
296
+ toggleMessage() {
297
+ super.toggleDialog();
298
+ return this;
299
+ }
300
+
301
+ /**
302
+ *
303
+ * @return {Object}
304
+ */
305
+ getMessage() {
306
+ return this.getOption("message");
307
+ }
308
+
309
+ /**
310
+ *
311
+ * @return {string}
312
+ */
313
+ static getTag() {
314
+ return "monster-message-state-button";
315
+ }
316
+
317
+ /**
318
+ *
319
+ * @return {CSSStyleSheet[]}
320
+ */
321
+ static getCSSStyleSheet() {
322
+ const styles = Popper.getCSSStyleSheet();
323
+ styles.push(StateButtonStyleSheet);
324
+ styles.push(MessageStateButtonStyleSheet);
325
+ return styles;
326
+ }
327
+
328
+ /**
329
+ * Programmatically triggers a click event on the button
330
+ * Will not trigger if the button is disabled
331
+ *
332
+ * @since 3.27.0
333
+ * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/click}
334
+ * @fires monster-click
335
+ */
336
+ click() {
337
+ if (this.getOption("disabled") === true) {
338
+ return;
339
+ }
340
+
341
+ if (
342
+ this[buttonElementSymbol] &&
343
+ isFunction(this[buttonElementSymbol].click)
344
+ ) {
345
+ this[buttonElementSymbol].click();
346
+ }
347
+ }
348
+
349
+ /**
350
+ * The Button.focus() method sets focus on the internal button element.
351
+ *
352
+ * @since 3.27.0
353
+ * @param {Object} options
354
+ * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/focus}
355
+ */
356
+ focus(options) {
357
+ if (this.getOption("disabled") === true) {
358
+ return;
359
+ }
360
+
361
+ if (
362
+ this[buttonElementSymbol] &&
363
+ isFunction(this[buttonElementSymbol].focus)
364
+ ) {
365
+ this[buttonElementSymbol].focus(options);
366
+ }
367
+ }
368
+
369
+ /**
370
+ * The Button.blur() method removes focus from the internal button element.
371
+ */
372
+ blur() {
373
+ if (
374
+ this[buttonElementSymbol] &&
375
+ isFunction(this[buttonElementSymbol].blur)
376
+ ) {
377
+ this[buttonElementSymbol].blur();
378
+ }
379
+ }
363
380
  }
364
381
 
365
382
  /**
@@ -367,27 +384,27 @@ class MessageStateButton extends Popper {
367
384
  * @param mode
368
385
  */
369
386
  function initEventHandlerByMode(mode) {
370
- switch (mode) {
371
- case "manual":
372
- this[buttonElementSymbol].setOption("actions.click", (e) => {
373
- const callback = this.getOption("actions.click");
374
- if (isFunction(callback)) {
375
- callback(e);
376
- }
377
- });
378
-
379
- break;
380
- case "submit":
381
- this[buttonElementSymbol].setOption("actions.click", (e) => {
382
- const form = this.form;
383
-
384
- if (form instanceof HTMLFormElement) {
385
- form.requestSubmit();
386
- }
387
- });
388
-
389
- break;
390
- }
387
+ switch (mode) {
388
+ case "manual":
389
+ this[buttonElementSymbol].setOption("actions.click", (e) => {
390
+ const callback = this.getOption("actions.click");
391
+ if (isFunction(callback)) {
392
+ callback(e);
393
+ }
394
+ });
395
+
396
+ break;
397
+ case "submit":
398
+ this[buttonElementSymbol].setOption("actions.click", (e) => {
399
+ const form = this.form;
400
+
401
+ if (form instanceof HTMLFormElement) {
402
+ form.requestSubmit();
403
+ }
404
+ });
405
+
406
+ break;
407
+ }
391
408
  }
392
409
 
393
410
  /**
@@ -395,77 +412,83 @@ function initEventHandlerByMode(mode) {
395
412
  * @return {Select}
396
413
  */
397
414
  function initControlReferences() {
398
- this[buttonElementSymbol] = this.shadowRoot.querySelector(
399
- `[${ATTRIBUTE_ROLE}=button]`,
400
- );
415
+ this[buttonElementSymbol] = this.shadowRoot.querySelector(
416
+ `[${ATTRIBUTE_ROLE}=button]`,
417
+ );
418
+ this[popperElementSymbol] = this.shadowRoot.querySelector(
419
+ `[${ATTRIBUTE_ROLE}=popper]`,
420
+ );
421
+ this[messageElementSymbol] = this.shadowRoot.querySelector(
422
+ `[${ATTRIBUTE_ROLE}=message]`,
423
+ );
401
424
  }
402
425
 
403
426
  /**
404
427
  * @private
405
428
  */
406
429
  function initDisabledSync() {
407
- const self = this;
408
- const attachInnerObserver = (button) => {
409
- if (!button || !isFunction(button.attachObserver)) {
410
- return;
411
- }
412
-
413
- const existing = self[innerDisabledObserverSymbol];
414
- if (existing?.button === button) {
415
- return;
416
- }
417
-
418
- if (
419
- existing?.button &&
420
- isFunction(existing.button.detachObserver) &&
421
- existing.observer
422
- ) {
423
- existing.button.detachObserver(existing.observer);
424
- }
425
-
426
- const observer = new Observer(syncDisabled);
427
- button.attachObserver(observer);
428
- self[innerDisabledObserverSymbol] = { button, observer };
429
- };
430
-
431
- const syncDisabled = () => {
432
- const disabled = self.getOption("disabled", false);
433
- const button =
434
- self.shadowRoot?.querySelector(`[${ATTRIBUTE_ROLE}=button]`) ??
435
- self[buttonElementSymbol];
436
- if (!button) {
437
- return;
438
- }
439
-
440
- self[buttonElementSymbol] = button;
441
-
442
- if (disabled) {
443
- button.setAttribute(ATTRIBUTE_DISABLED, "");
444
- } else {
445
- button.removeAttribute(ATTRIBUTE_DISABLED);
446
- }
447
-
448
- if (isFunction(button.setOption)) {
449
- button.setOption("disabled", disabled);
450
- }
451
-
452
- attachInnerObserver(button);
453
- };
454
-
455
- syncDisabled();
456
- const existingObserver = self[attributeObserverSymbol]?.[ATTRIBUTE_DISABLED];
457
- if (existingObserver) {
458
- self[attributeObserverSymbol][ATTRIBUTE_DISABLED] = () => {
459
- existingObserver.call(self);
460
- syncDisabled();
461
- };
462
- }
463
- self.attachObserver(new Observer(syncDisabled));
464
- if (typeof customElements?.whenDefined === "function") {
465
- customElements.whenDefined("monster-state-button").then(() => {
466
- syncDisabled();
467
- });
468
- }
430
+ const self = this;
431
+ const attachInnerObserver = (button) => {
432
+ if (!button || !isFunction(button.attachObserver)) {
433
+ return;
434
+ }
435
+
436
+ const existing = self[innerDisabledObserverSymbol];
437
+ if (existing?.button === button) {
438
+ return;
439
+ }
440
+
441
+ if (
442
+ existing?.button &&
443
+ isFunction(existing.button.detachObserver) &&
444
+ existing.observer
445
+ ) {
446
+ existing.button.detachObserver(existing.observer);
447
+ }
448
+
449
+ const observer = new Observer(syncDisabled);
450
+ button.attachObserver(observer);
451
+ self[innerDisabledObserverSymbol] = { button, observer };
452
+ };
453
+
454
+ const syncDisabled = () => {
455
+ const disabled = self.getOption("disabled", false);
456
+ const button =
457
+ self.shadowRoot?.querySelector(`[${ATTRIBUTE_ROLE}=button]`) ??
458
+ self[buttonElementSymbol];
459
+ if (!button) {
460
+ return;
461
+ }
462
+
463
+ self[buttonElementSymbol] = button;
464
+
465
+ if (disabled) {
466
+ button.setAttribute(ATTRIBUTE_DISABLED, "");
467
+ } else {
468
+ button.removeAttribute(ATTRIBUTE_DISABLED);
469
+ }
470
+
471
+ if (isFunction(button.setOption)) {
472
+ button.setOption("disabled", disabled);
473
+ }
474
+
475
+ attachInnerObserver(button);
476
+ };
477
+
478
+ syncDisabled();
479
+ const existingObserver = self[attributeObserverSymbol]?.[ATTRIBUTE_DISABLED];
480
+ if (existingObserver) {
481
+ self[attributeObserverSymbol][ATTRIBUTE_DISABLED] = () => {
482
+ existingObserver.call(self);
483
+ syncDisabled();
484
+ };
485
+ }
486
+ self.attachObserver(new Observer(syncDisabled));
487
+ if (typeof customElements?.whenDefined === "function") {
488
+ customElements.whenDefined("monster-state-button").then(() => {
489
+ syncDisabled();
490
+ });
491
+ }
469
492
  }
470
493
 
471
494
  /**
@@ -473,8 +496,8 @@ function initDisabledSync() {
473
496
  * @return {string}
474
497
  */
475
498
  function getTemplate() {
476
- // language=HTML
477
- return `
499
+ // language=HTML
500
+ return `
478
501
  <div data-monster-role="control" part="control">
479
502
 
480
503
  <monster-state-button exportparts="button:button-button,control:button-control"
@@ -499,4 +522,181 @@ function getTemplate() {
499
522
  `;
500
523
  }
501
524
 
525
+ /**
526
+ * @private
527
+ * @param {string|HTMLElement} content
528
+ * @return {HTMLElement|string|null}
529
+ */
530
+ function getMeasurementContent(content) {
531
+ if (isString(content)) {
532
+ return content;
533
+ }
534
+
535
+ if (content instanceof HTMLElement) {
536
+ return content.cloneNode(true);
537
+ }
538
+
539
+ return null;
540
+ }
541
+
542
+ /**
543
+ * @private
544
+ * @return {{popper: HTMLElement, message: HTMLElement}|null}
545
+ */
546
+ function ensureMeasurementPopper() {
547
+ if (this[measurementPopperSymbol]) {
548
+ return this[measurementPopperSymbol];
549
+ }
550
+
551
+ if (!this.shadowRoot) {
552
+ return null;
553
+ }
554
+
555
+ const popper = document.createElement("div");
556
+ popper.setAttribute(ATTRIBUTE_ROLE, "popper");
557
+ popper.setAttribute("data-measurement", "true");
558
+ popper.setAttribute("aria-hidden", "true");
559
+ popper.style.position = "absolute";
560
+ popper.style.left = "-10000px";
561
+ popper.style.top = "-10000px";
562
+ popper.style.visibility = "hidden";
563
+ popper.style.display = "block";
564
+ popper.style.pointerEvents = "none";
565
+ popper.style.maxWidth = "none";
566
+ popper.style.width = "max-content";
567
+ if (this[popperElementSymbol]?.className) {
568
+ popper.className = this[popperElementSymbol].className;
569
+ }
570
+
571
+ const message = document.createElement("div");
572
+ message.setAttribute(ATTRIBUTE_ROLE, "message");
573
+ message.className = "flex";
574
+
575
+ popper.appendChild(message);
576
+ this.shadowRoot.appendChild(popper);
577
+
578
+ this[measurementPopperSymbol] = { popper, message };
579
+ return this[measurementPopperSymbol];
580
+ }
581
+
582
+ /**
583
+ * @private
584
+ */
585
+ function applyMeasuredMessageWidth() {
586
+ const popper = this[popperElementSymbol];
587
+ if (!popper) {
588
+ return;
589
+ }
590
+
591
+ const content = this.getOption("message.content");
592
+ const measureContent = getMeasurementContent(content);
593
+ if (!measureContent) {
594
+ return;
595
+ }
596
+
597
+ const measurement = ensureMeasurementPopper.call(this);
598
+ if (!measurement?.message) {
599
+ return;
600
+ }
601
+
602
+ if (popper.className && measurement.popper.className !== popper.className) {
603
+ measurement.popper.className = popper.className;
604
+ }
605
+
606
+ measurement.message.innerHTML = "";
607
+ if (isString(measureContent)) {
608
+ measurement.message.innerHTML = measureContent;
609
+ } else {
610
+ measurement.message.appendChild(measureContent);
611
+ }
612
+
613
+ const measuredWidth = Math.ceil(
614
+ measurement.popper.getBoundingClientRect().width,
615
+ );
616
+ const fontSize = parseFloat(getComputedStyle(popper).fontSize) || 16;
617
+ const rootFontSize =
618
+ parseFloat(getComputedStyle(document.documentElement).fontSize) || 16;
619
+ const widthOptions = this.getOption("message.width", {});
620
+ const minWidthOption = resolveLength(
621
+ widthOptions?.min,
622
+ fontSize,
623
+ rootFontSize,
624
+ window.innerWidth,
625
+ );
626
+ const maxWidthOption = resolveLength(
627
+ widthOptions?.max,
628
+ fontSize,
629
+ rootFontSize,
630
+ window.innerWidth,
631
+ );
632
+ const viewportRatio =
633
+ typeof widthOptions?.viewportRatio === "number" &&
634
+ widthOptions.viewportRatio > 0 &&
635
+ widthOptions.viewportRatio <= 1
636
+ ? widthOptions.viewportRatio
637
+ : 0.7;
638
+
639
+ const minWidth = Math.max(0, minWidthOption ?? Math.round(fontSize * 12));
640
+ const maxViewportWidth = Math.max(
641
+ minWidth,
642
+ window.innerWidth * viewportRatio,
643
+ );
644
+ const maxWidth = Math.max(
645
+ minWidth,
646
+ Math.min(maxWidthOption ?? fontSize * 32, maxViewportWidth),
647
+ );
648
+ const targetWidth = Math.max(
649
+ minWidth,
650
+ Math.min(measuredWidth || minWidth, maxWidth),
651
+ );
652
+
653
+ popper.style.width = `${targetWidth}px`;
654
+ popper.style.minWidth = `${minWidth}px`;
655
+ popper.style.maxWidth = `${maxWidth}px`;
656
+ popper.style.whiteSpace = "normal";
657
+ popper.style.overflowWrap = "anywhere";
658
+ }
659
+
660
+ /**
661
+ * @private
662
+ * @param {unknown} value
663
+ * @param {number} fontSize
664
+ * @param {number} rootFontSize
665
+ * @param {number} viewportWidth
666
+ * @return {number|null}
667
+ */
668
+ function resolveLength(value, fontSize, rootFontSize, viewportWidth) {
669
+ if (typeof value === "number" && Number.isFinite(value)) {
670
+ return value;
671
+ }
672
+
673
+ if (!isString(value)) {
674
+ return null;
675
+ }
676
+
677
+ const trimmed = value.trim();
678
+ const number = parseFloat(trimmed);
679
+ if (!Number.isFinite(number)) {
680
+ return null;
681
+ }
682
+
683
+ if (trimmed.endsWith("rem")) {
684
+ return number * rootFontSize;
685
+ }
686
+
687
+ if (trimmed.endsWith("em")) {
688
+ return number * fontSize;
689
+ }
690
+
691
+ if (trimmed.endsWith("vw")) {
692
+ return (number / 100) * viewportWidth;
693
+ }
694
+
695
+ if (trimmed.endsWith("px")) {
696
+ return number;
697
+ }
698
+
699
+ return number;
700
+ }
701
+
502
702
  registerCustomElement(MessageStateButton);