@operato/popup 1.13.4 → 1.13.9

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.
@@ -23,6 +23,9 @@ function guaranteeFocus(element: HTMLElement) {
23
23
  closest?.focus()
24
24
  }
25
25
 
26
+ /**
27
+ * A custom element representing a list-like popup menu.
28
+ */
26
29
  @customElement('ox-popup-list')
27
30
  export class OxPopupList extends OxPopup {
28
31
  static styles = [
@@ -129,9 +132,30 @@ export class OxPopupList extends OxPopup {
129
132
  `
130
133
  ]
131
134
 
135
+ /**
136
+ * A boolean property that, when set to true, allows multiple options to be selected in the popup list.
137
+ * @type {boolean}
138
+ */
132
139
  @property({ type: Boolean }) multiple: boolean = false
140
+
141
+ /**
142
+ * An optional attribute that specifies the name of the attribute used to mark selected options in the list.
143
+ * @type {string|undefined}
144
+ */
133
145
  @property({ type: String, attribute: 'attr-selected' }) attrSelected?: string
146
+
147
+ /**
148
+ * A boolean property that, when set to true, enables the search functionality in the popup list.
149
+ * Users can search/filter options by typing in a search bar.
150
+ * @type {boolean|undefined}
151
+ */
134
152
  @property({ type: Boolean, attribute: 'with-search' }) withSearch?: boolean
153
+
154
+ /**
155
+ * The value(s) of the selected option(s) in the popup list.
156
+ * This property can be a string or an array of strings, depending on whether multiple selections are allowed.
157
+ * @type {string|string[]|undefined}
158
+ */
135
159
  @property({ type: String }) value?: string | string[]
136
160
 
137
161
  @state() activeIndex?: number
@@ -284,6 +308,11 @@ export class OxPopupList extends OxPopup {
284
308
  }
285
309
  }
286
310
 
311
+ /**
312
+ * Retrieves the labels of the selected options in the popup list.
313
+ * If multiple selections are allowed, an array of labels is returned. Otherwise, a single label is returned.
314
+ * @returns {string|string[]} The label(s) of the selected option(s).
315
+ */
287
316
  public getSelectedLabels(): string | string[] {
288
317
  const options = Array.from(this.querySelectorAll(':scope > [option]'))
289
318
 
@@ -294,6 +323,11 @@ export class OxPopupList extends OxPopup {
294
323
  return this.multiple ? selected : selected[0]
295
324
  }
296
325
 
326
+ /**
327
+ * Handles the selection of options in the popup list and dispatches a 'select' event with the selected value(s).
328
+ * If multiple selections are allowed, an array of selected values is dispatched; otherwise, a single value is dispatched.
329
+ * Also, it checks whether the selected option should remain alive and whether the popup should be closed.
330
+ */
297
331
  async select() {
298
332
  await this.updateComplete
299
333
 
@@ -317,6 +351,13 @@ export class OxPopupList extends OxPopup {
317
351
  }
318
352
  }
319
353
 
354
+ /**
355
+ * Sets the active option within the popup list based on the given index or Element.
356
+ * If 'withSelect' is true, it also manages the selection state of the option.
357
+ *
358
+ * @param {number | Element | null} active - The index or Element of the option to set as active.
359
+ * @param {boolean | undefined} withSelect - Indicates whether to manage the selection state of the option.
360
+ */
320
361
  setActive(active: number | Element | null, withSelect?: boolean) {
321
362
  var options = Array.from(this.querySelectorAll('[option]:not([hidden])'))
322
363
  if (this.withSearch) {
@@ -351,6 +392,12 @@ export class OxPopupList extends OxPopup {
351
392
  })
352
393
  }
353
394
 
395
+ /**
396
+ * Overrides the 'open' method of the base class 'OxPopup' to set the initial active index
397
+ * when the popup list is opened. It ensures that an option is initially selected for user interaction.
398
+ *
399
+ * @param {object} params - The parameters for opening the popup, including position and size.
400
+ */
354
401
  override open(params: {
355
402
  left?: number
356
403
  top?: number
@@ -369,6 +416,11 @@ export class OxPopupList extends OxPopup {
369
416
  }
370
417
  }
371
418
 
419
+ /**
420
+ * Overrides the 'close' method of the base class 'OxPopup' to dispatch a custom event
421
+ * indicating that the popup list is being closed. This event can be used for further interactions
422
+ * or logic in the application.
423
+ */
372
424
  override close() {
373
425
  if (this.hasAttribute('active')) {
374
426
  this.dispatchEvent(
@@ -15,6 +15,9 @@ function focusClosest(element: HTMLElement) {
15
15
  return closest
16
16
  }
17
17
 
18
+ /**
19
+ * Custom element representing a popup menu. It extends OxPopup.
20
+ */
18
21
  @customElement('ox-popup-menu')
19
22
  export class OxPopupMenu extends OxPopup {
20
23
  static styles = [
@@ -77,6 +80,9 @@ export class OxPopupMenu extends OxPopup {
77
80
  `
78
81
  ]
79
82
 
83
+ /**
84
+ * Property to track the index of the active menu item.
85
+ */
80
86
  @property({ type: Number }) activeIndex: number = 0
81
87
 
82
88
  render() {
@@ -151,6 +157,10 @@ export class OxPopupMenu extends OxPopup {
151
157
  }
152
158
  }
153
159
 
160
+ /**
161
+ * Selects a menu item by dispatching a 'select' event.
162
+ * Closes the menu if the item doesn't have 'alive-on-select' attribute.
163
+ */
154
164
  select(menu: Element) {
155
165
  menu.dispatchEvent(new CustomEvent('select'))
156
166
  if (!menu.hasAttribute('alive-on-select')) {
@@ -158,6 +168,9 @@ export class OxPopupMenu extends OxPopup {
158
168
  }
159
169
  }
160
170
 
171
+ /**
172
+ * Sets the active menu item based on the index or the menu element itself.
173
+ */
161
174
  setActive(active: number | Element | null) {
162
175
  const menus = Array.from(this.querySelectorAll(':scope > ox-popup-menuitem, :scope > [menu]'))
163
176
 
@@ -183,7 +196,8 @@ export class OxPopupMenu extends OxPopup {
183
196
  }
184
197
 
185
198
  /**
186
- * Open Popup
199
+ * Static method to open a popup menu with the provided template and position options.
200
+ * Creates and returns an instance of OxPopupMenu.
187
201
  *
188
202
  * @param {PopupOpenOptions}
189
203
  */
@@ -3,6 +3,10 @@ import { customElement, property, state } from 'lit/decorators.js'
3
3
 
4
4
  import { OxPopupMenu } from './ox-popup-menu'
5
5
 
6
+ /**
7
+ * Custom element representing a menu item within an OxPopup menu.
8
+ * It can contain a label and an optional submenu.
9
+ */
6
10
  @customElement('ox-popup-menuitem')
7
11
  export class OxPopupMenuItem extends LitElement {
8
12
  static styles = [
@@ -49,7 +53,15 @@ export class OxPopupMenuItem extends LitElement {
49
53
  `
50
54
  ]
51
55
 
56
+ /**
57
+ * Property indicating whether the menu item is active or not.
58
+ * When active, it may show a submenu.
59
+ */
52
60
  @property({ type: Boolean }) active: boolean = false
61
+
62
+ /**
63
+ * The label to display for the menu item.
64
+ */
53
65
  @property({ type: String }) label!: string
54
66
 
55
67
  @state() _submenu?: OxPopupMenu
@@ -133,6 +145,12 @@ export class OxPopupMenuItem extends LitElement {
133
145
  }
134
146
  }
135
147
 
148
+ /**
149
+ * Expands the submenu, if it exists.
150
+ * The submenu is displayed below and to the right of the menu item.
151
+ *
152
+ * @param {boolean} silent - If true, the submenu is opened silently without user interaction.
153
+ */
136
154
  expand(silent: boolean) {
137
155
  if (!this._submenu) {
138
156
  return
@@ -143,14 +161,25 @@ export class OxPopupMenuItem extends LitElement {
143
161
  this._submenu.open({ top, left, silent })
144
162
  }
145
163
 
164
+ /**
165
+ * Collapses the submenu, if it exists.
166
+ */
146
167
  collapse() {
147
168
  this._submenu?.close()
148
169
  }
149
170
 
171
+ /**
172
+ * Dispatches a custom 'ox-collapse' event indicating that the menu item should collapse itself.
173
+ * This event can be used to trigger further interactions or logic in the application.
174
+ */
150
175
  collapseSelf() {
151
176
  this.dispatchEvent(new CustomEvent('ox-collapse', { bubbles: true, composed: true, detail: this }))
152
177
  }
153
178
 
179
+ /**
180
+ * Dispatches a custom 'ox-close' event indicating that the menu item should close itself.
181
+ * This event can be used to trigger further interactions or logic in the application.
182
+ */
154
183
  close() {
155
184
  this.dispatchEvent(new CustomEvent('ox-close', { bubbles: true, composed: true, detail: this }))
156
185
  }
package/src/ox-popup.ts CHANGED
@@ -4,6 +4,15 @@ import { customElement, state } from 'lit/decorators.js'
4
4
 
5
5
  import { ScrollbarStyles } from '@operato/styles'
6
6
 
7
+ /**
8
+ * Custom element class representing the 'ox-popup' component.
9
+ *
10
+ * This component provides the functionality to display a popup with various configuration options.
11
+ * It can be used to show additional information, notifications, or user interactions within a web application.
12
+ *
13
+ * @fires ox-close - Dispatched when the popup is closed.
14
+ * @fires ox-collapse - Dispatched when the popup is collapsed.
15
+ */
7
16
  @customElement('ox-popup')
8
17
  export class OxPopup extends LitElement {
9
18
  static styles = [
@@ -34,6 +43,8 @@ export class OxPopup extends LitElement {
34
43
 
35
44
  @state() _parent?: Element
36
45
 
46
+ private lastActive: HTMLElement = document.activeElement as HTMLElement
47
+
37
48
  render() {
38
49
  return html` <slot> </slot> `
39
50
  }
@@ -71,8 +82,9 @@ export class OxPopup extends LitElement {
71
82
  e.stopPropagation()
72
83
  }.bind(this)
73
84
 
74
- protected _oncontext: (e: Event) => void = function (this: OxPopup, e: Event) {
85
+ protected _oncontextmenu: (e: Event) => void = function (this: OxPopup, e: Event) {
75
86
  e.stopPropagation()
87
+ // this.close()
76
88
  }.bind(this)
77
89
 
78
90
  protected _onclick: (e: MouseEvent) => void = function (this: OxPopup, e: MouseEvent) {
@@ -102,7 +114,7 @@ export class OxPopup extends LitElement {
102
114
  this.addEventListener('click', this._onclick)
103
115
  this.addEventListener('mouseup', this._onmouseup)
104
116
  this.addEventListener('mousedown', this._onmousedown)
105
- this.addEventListener('context', this._oncontext)
117
+ this.addEventListener('contextmenu', this._oncontextmenu)
106
118
  this.addEventListener('ox-close', this._onclose)
107
119
  this.addEventListener('ox-collapse', this._oncollapse)
108
120
 
@@ -119,12 +131,6 @@ export class OxPopup extends LitElement {
119
131
  * @property {Number} left The position-left where the pop-up will be displayed
120
132
  * @property {HTMLElement} parent Popup's parent element
121
133
  */
122
-
123
- /**
124
- * Open Popup
125
- *
126
- * @param {PopupOpenOptions}
127
- */
128
134
  static open({
129
135
  template,
130
136
  top,
@@ -157,6 +163,18 @@ export class OxPopup extends LitElement {
157
163
  return target
158
164
  }
159
165
 
166
+ /**
167
+ * Opens the 'ox-popup' with the specified position and dimensions.
168
+ *
169
+ * @param {PopupOpenOptions} options - An object specifying the position and dimensions of the popup.
170
+ * @param {number} options.left - The left position (in pixels) where the popup should be displayed. If not provided, it will be centered horizontally.
171
+ * @param {number} options.top - The top position (in pixels) where the popup should be displayed. If not provided, it will be centered vertically.
172
+ * @param {number} options.right - The right position (in pixels) where the popup should be displayed. If provided, it will override the 'left' value.
173
+ * @param {number} options.bottom - The bottom position (in pixels) where the popup should be displayed. If provided, it will override the 'top' value.
174
+ * @param {string} options.width - The maximum width of the popup (CSS string). For example, '300px'.
175
+ * @param {string} options.height - The maximum height of the popup (CSS string). For example, '200px'.
176
+ * @param {boolean} [options.silent=false] - A flag indicating whether the popup should auto-focus or not. If set to true, the popup won't attempt to focus on any element.
177
+ */
160
178
  open({
161
179
  left,
162
180
  top,
@@ -261,6 +279,9 @@ export class OxPopup extends LitElement {
261
279
  }
262
280
  }
263
281
 
282
+ /**
283
+ * Closes the 'ox-popup'.
284
+ */
264
285
  close() {
265
286
  this.removeAttribute('active')
266
287
 
@@ -276,10 +297,14 @@ export class OxPopup extends LitElement {
276
297
  this.removeEventListener('ox-collapse', this._oncollapse)
277
298
  this.removeEventListener('mouseup', this._onmouseup)
278
299
  this.removeEventListener('mousedown', this._onmousedown)
279
- this.removeEventListener('context', this._oncontext)
300
+ this.removeEventListener('contextmenu', this._oncontextmenu)
280
301
 
281
302
  this._parent.removeChild(this)
282
303
  delete this._parent
304
+
305
+ if (this.lastActive) {
306
+ this.lastActive.focus && this.lastActive.focus()
307
+ }
283
308
  }
284
309
  }
285
310
  }
package/src/ox-prompt.ts CHANGED
@@ -12,6 +12,9 @@ const TYPES_ICON = {
12
12
  question: 'question_mark'
13
13
  }
14
14
 
15
+ /**
16
+ * The `ox-prompt` custom element represents a modal popup that provides information or options for the user, such as confirmation or cancellation.
17
+ */
15
18
  @customElement('ox-prompt')
16
19
  export class OxPrompt extends LitElement {
17
20
  static styles = [
@@ -66,14 +69,49 @@ export class OxPrompt extends LitElement {
66
69
  `
67
70
  ]
68
71
 
72
+ /**
73
+ * Specifies the type of the popup. Possible values are 'success', 'error', 'warning', 'info', 'question'.
74
+ */
69
75
  @property({ type: String }) type?: 'success' | 'error' | 'warning' | 'info' | 'question'
76
+
77
+ /**
78
+ * Specifies the icon of the popup.
79
+ */
70
80
  @property({ type: String }) icon?: string
81
+
82
+ /**
83
+ * Specifies the title of the popup.
84
+ */
71
85
  @property({ type: String }) titler?: string = ''
86
+
87
+ /**
88
+ * Specifies the text content of the popup.
89
+ */
72
90
  @property({ type: String }) text?: string
91
+
92
+ /**
93
+ * Specifies the footer (additional information at the bottom) of the popup.
94
+ */
73
95
  @property({ type: String }) footer?: string
96
+
97
+ /**
98
+ * Determines whether the popup is displayed as a toast.
99
+ */
74
100
  @property({ type: Boolean }) toast?: boolean
101
+
102
+ /**
103
+ * Specifies settings for the confirmation button.
104
+ */
75
105
  @property({ type: Object }) confirmButton?: { text: string; color?: string }
106
+
107
+ /**
108
+ * Specifies settings for the cancel button.
109
+ */
76
110
  @property({ type: Object }) cancelButton?: { text: string; color?: string }
111
+
112
+ /**
113
+ * A callback function called when the popup is closed, providing the result of the user's interaction.
114
+ */
77
115
  @property({ type: Object }) callback?: (result: { value: boolean }) => void
78
116
 
79
117
  @state() _parent?: Element
@@ -102,11 +140,17 @@ export class OxPrompt extends LitElement {
102
140
  `
103
141
  }
104
142
 
143
+ /**
144
+ * Function called when the confirm button is clicked.
145
+ */
105
146
  onConfirm() {
106
147
  this.resolveFn && this.resolveFn(true)
107
148
  this.close()
108
149
  }
109
150
 
151
+ /**
152
+ * Function called when the cancel button is clicked.
153
+ */
110
154
  onCancel() {
111
155
  this.resolveFn && this.resolveFn(false)
112
156
  this.close()
@@ -145,7 +189,7 @@ export class OxPrompt extends LitElement {
145
189
  e.stopPropagation()
146
190
  }.bind(this)
147
191
 
148
- protected _oncontext: (e: Event) => void = function (this: OxPrompt, e: Event) {
192
+ protected _oncontextmenu: (e: Event) => void = function (this: OxPrompt, e: Event) {
149
193
  e.stopPropagation()
150
194
  }.bind(this)
151
195
 
@@ -176,7 +220,7 @@ export class OxPrompt extends LitElement {
176
220
  this.addEventListener('click', this._onclick)
177
221
  this.addEventListener('mouseup', this._onmouseup)
178
222
  this.addEventListener('mousedown', this._onmousedown)
179
- this.addEventListener('context', this._oncontext)
223
+ this.addEventListener('contextmenu', this._oncontextmenu)
180
224
  this.addEventListener('ox-close', this._onclose)
181
225
  this.addEventListener('ox-collapse', this._oncollapse)
182
226
 
@@ -184,6 +228,31 @@ export class OxPrompt extends LitElement {
184
228
  this.guaranteeFocus()
185
229
  }
186
230
 
231
+ /**
232
+ * Static method to open the `ox-prompt` popup.
233
+ * @param {object} options - An object containing popup options.
234
+ * @param {unknown} [options.template] - An optional content template to render inside the popup.
235
+ * @param {'success' | 'error' | 'warning' | 'info' | 'question'} [options.type] - The type of the popup, which can be one of: 'success', 'error', 'warning', 'info', 'question'.
236
+ * @param {string} [options.icon] - The icon to be displayed in the popup header.
237
+ * @param {string} [options.title] - The title to be displayed in the popup header.
238
+ * @param {string} [options.text] - The main text content of the popup.
239
+ * @param {string} [options.footer] - Additional information to be displayed at the bottom of the popup.
240
+ * @param {object} [options.confirmButton] - Configuration for the confirmation button in the popup.
241
+ * @param {string} options.confirmButton.text - The text to be displayed on the confirmation button.
242
+ * @param {string} [options.confirmButton.color] - The color of the confirmation button (CSS color).
243
+ * @param {object} [options.cancelButton] - Configuration for the cancel button in the popup.
244
+ * @param {string} options.cancelButton.text - The text to be displayed on the cancel button.
245
+ * @param {string} [options.cancelButton.color] - The color of the cancel button (CSS color).
246
+ * @param {number} [options.top] - The top position of the popup (in pixels).
247
+ * @param {number} [options.left] - The left position of the popup (in pixels).
248
+ * @param {number} [options.right] - The right position of the popup (in pixels).
249
+ * @param {number} [options.bottom] - The bottom position of the popup (in pixels).
250
+ * @param {string} [options.width] - The maximum width of the popup (CSS string).
251
+ * @param {string} [options.height] - The maximum height of the popup (CSS string).
252
+ * @param {Element | null} [options.parent] - The parent element to which the popup should be attached. If not provided, it will be attached to the document body.
253
+ * @param {(result: { value: boolean }) => void} [options.callback] - A callback function that will be invoked when the user interacts with the popup, providing the result of the interaction.
254
+ * @returns {Promise<boolean>} A Promise that resolves based on user interaction with the popup.
255
+ */
187
256
  public static async open({
188
257
  template,
189
258
  type,
@@ -244,6 +313,18 @@ export class OxPrompt extends LitElement {
244
313
  return result
245
314
  }
246
315
 
316
+ /**
317
+ * Opens the popup with specified position and dimensions.
318
+ * @param {object} options - An object specifying the position and dimensions of the popup.
319
+ * @param {number} [options.left] - The left position of the popup (in pixels). If not provided, the popup will be horizontally centered.
320
+ * @param {number} [options.top] - The top position of the popup (in pixels). If not provided, the popup will be vertically centered.
321
+ * @param {number} [options.right] - The right position of the popup (in pixels). Overrides 'left' if both 'left' and 'right' are provided.
322
+ * @param {number} [options.bottom] - The bottom position of the popup (in pixels). Overrides 'top' if both 'top' and 'bottom' are provided.
323
+ * @param {string} [options.width] - The maximum width of the popup (CSS string). If not provided, no width restriction is applied.
324
+ * @param {string} [options.height] - The maximum height of the popup (CSS string). If not provided, no height restriction is applied.
325
+ * @param {boolean} [options.silent=false] - Determines whether to focus the popup automatically (true) or not (false).
326
+ * @returns {Promise<boolean>} A Promise that resolves based on user interaction with the popup.
327
+ */
247
328
  open({
248
329
  left,
249
330
  top,
@@ -352,6 +433,9 @@ export class OxPrompt extends LitElement {
352
433
  }
353
434
  }
354
435
 
436
+ /**
437
+ * Closes the popup.
438
+ */
355
439
  close() {
356
440
  this.removeAttribute('active')
357
441
 
@@ -367,7 +451,7 @@ export class OxPrompt extends LitElement {
367
451
  this.removeEventListener('ox-collapse', this._oncollapse)
368
452
  this.removeEventListener('mouseup', this._onmouseup)
369
453
  this.removeEventListener('mousedown', this._onmousedown)
370
- this.removeEventListener('context', this._oncontext)
454
+ this.removeEventListener('contextmenu', this._oncontextmenu)
371
455
 
372
456
  this._parent.removeChild(this)
373
457
  delete this._parent