@nectary/components 4.1.4 → 4.3.0

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/button/index.js CHANGED
@@ -159,8 +159,11 @@ defineCustomElement('sinch-button', class extends NectaryElement {
159
159
  }
160
160
  }
161
161
  };
162
- #onButtonClick = () => {
163
- if (!this.disabled) {
162
+ #onButtonClick = e => {
163
+ if (this.disabled) {
164
+ e.stopPropagation();
165
+ e.preventDefault();
166
+ } else {
164
167
  this.dispatchEvent(new CustomEvent('-click'));
165
168
  }
166
169
  };
@@ -1,8 +1,8 @@
1
1
  import '../text';
2
2
  import '../file-picker';
3
3
  import { defineCustomElement, getAttribute, getBooleanAttribute, getIntegerAttribute, getReactEventHandler, isAttrEqual, isAttrTrue, NectaryElement, setClass, updateAttribute, updateBooleanAttribute } from '../utils';
4
- const templateHTML = '<style>:host{display:block}#wrapper{position:relative;display:flex;flex-direction:row;flex-wrap:wrap;justify-content:center;align-content:center;gap:8px;height:148px;min-width:148px;box-sizing:border-box;padding:16px;border-radius:var(--sinch-comp-file-drop-shape-radius);background-color:var(--sinch-comp-file-drop-color-default-background-initial)}#wrapper::after{content:"";position:absolute;top:0;left:0;right:0;bottom:0;border:1px dashed var(--sinch-comp-file-drop-color-default-border-initial);border-radius:var(--sinch-comp-file-drop-shape-radius);pointer-events:none}#placeholder{align-self:center;text-align:center;--sinch-global-color-text:var(--sinch-comp-file-drop-color-default-placeholder-initial);--sinch-comp-text-font:var(--sinch-comp-file-drop-font-placeholder)}:host([invalid]) #wrapper{background-color:var(--sinch-comp-file-drop-color-invalid-background-initial)}:host([invalid]) #wrapper::after{border-color:var(--sinch-comp-file-drop-color-invalid-border-initial);border-width:1px}#wrapper.drop::after{pointer-events:all}#wrapper.drop.valid{background-color:var(--sinch-comp-file-drop-color-default-background-active)}#wrapper.drop.valid::after{border-color:var(--sinch-comp-file-drop-color-default-border-active);border-width:2px}#wrapper.drop.valid>#placeholder{--sinch-global-color-text:var(--sinch-comp-file-drop-color-default-placeholder-active)}:host([disabled])>#wrapper>#placeholder{--sinch-global-color-text:var(--sinch-comp-file-drop-color-disabled-placeholder-initial)}:host([disabled])>#wrapper{background-color:var(--sinch-comp-file-drop-color-disabled-background-initial)}:host([disabled])>#wrapper::after{border-color:var(--sinch-comp-file-drop-color-disabled-border-initial);border-width:1px}</style><div id="wrapper"><sinch-text id="placeholder" type="m" aria-hidden="true"></sinch-text><sinch-file-picker id="file-picker"><slot></slot></sinch-file-picker></div>';
5
- import { areFilesAccepted, doFilesSatisfySize } from './utils';
4
+ const templateHTML = '<style>:host{display:block}#wrapper{position:relative;display:flex;flex-direction:row;flex-wrap:wrap;justify-content:center;align-content:center;gap:8px;height:148px;min-width:148px;box-sizing:border-box;padding:16px;border-radius:var(--sinch-comp-file-drop-shape-radius);background-color:var(--sinch-comp-file-drop-color-default-background-initial)}#wrapper::after{content:"";position:absolute;top:0;left:0;right:0;bottom:0;border:1px dashed var(--sinch-comp-file-drop-color-default-border-initial);border-radius:var(--sinch-comp-file-drop-shape-radius);pointer-events:none}#placeholder{align-self:center;text-align:center;--sinch-global-color-text:var(--sinch-comp-file-drop-color-default-placeholder-initial);--sinch-comp-text-font:var(--sinch-comp-file-drop-font-placeholder)}:host([invalid]) #wrapper{background-color:var(--sinch-comp-file-drop-color-invalid-background-initial)}:host([invalid]) #wrapper::after{border-color:var(--sinch-comp-file-drop-color-invalid-border-initial);border-width:1px}#wrapper.drop::after{pointer-events:all}#wrapper.drop.valid{background-color:var(--sinch-comp-file-drop-color-default-background-active)}#wrapper.drop.valid::after{border-color:var(--sinch-comp-file-drop-color-default-border-active);border-width:2px}#wrapper.drop.valid>#placeholder{--sinch-global-color-text:var(--sinch-comp-file-drop-color-default-placeholder-active)}#wrapper.drop.invalid{background-color:var(--sinch-comp-file-drop-color-invalid-background-active)}#wrapper.drop.invalid::after{border-color:var(--sinch-comp-file-drop-color-invalid-border-active);border-width:2px}#wrapper.drop.invalid>#placeholder{--sinch-global-color-text:var(--sinch-comp-file-drop-color-invalid-placeholder-active)}:host([disabled])>#wrapper>#placeholder{--sinch-global-color-text:var(--sinch-comp-file-drop-color-disabled-placeholder-initial)}:host([disabled])>#wrapper{background-color:var(--sinch-comp-file-drop-color-disabled-background-initial)}:host([disabled])>#wrapper::after{border-color:var(--sinch-comp-file-drop-color-disabled-border-initial);border-width:1px}</style><div id="wrapper"><sinch-text id="placeholder" type="m" aria-hidden="true"></sinch-text><sinch-file-picker id="file-picker"><slot></slot></sinch-file-picker></div>';
5
+ import { areFilesAccepted, areItemsAccepted, doFilesSatisfySize } from './utils';
6
6
  const template = document.createElement('template');
7
7
  template.innerHTML = templateHTML;
8
8
  defineCustomElement('sinch-file-drop', class extends NectaryElement {
@@ -129,18 +129,24 @@ defineCustomElement('sinch-file-drop', class extends NectaryElement {
129
129
  if (isElementEnabled) {
130
130
  setClass(this.#$dropArea, 'drop', true);
131
131
  setClass(this.#$dropArea, 'valid', isValid);
132
+ setClass(this.#$dropArea, 'invalid', !isValid);
132
133
  }
133
134
  } else {
134
135
  setClass(this.#$dropArea, 'drop', false);
135
136
  setClass(this.#$dropArea, 'valid', false);
137
+ setClass(this.#$dropArea, 'invalid', false);
136
138
  }
137
139
  }
138
140
  #onDragEnter = e => {
139
141
  e.stopPropagation();
140
142
  e.preventDefault();
141
- const hasTypes = e.dataTransfer?.types?.includes('Files') === true;
142
- const hasItems = e.dataTransfer?.items != null && e.dataTransfer.items.length > 0;
143
- this.#setDragEffect(true, hasTypes || hasItems);
143
+ const items = e.dataTransfer?.items;
144
+ let isValidItems = false;
145
+ if (items != null && items.length > 0) {
146
+ const itemsArray = Array.from(items);
147
+ isValidItems = areItemsAccepted(itemsArray, this.accept);
148
+ }
149
+ this.#setDragEffect(true, isValidItems);
144
150
  };
145
151
  #onDragLeave = e => {
146
152
  e.stopPropagation();
@@ -1,2 +1,3 @@
1
1
  export declare const areFilesAccepted: (files: File[], accept: string | null) => boolean;
2
+ export declare const areItemsAccepted: (item: DataTransferItem[], accept: string | null) => boolean;
2
3
  export declare const doFilesSatisfySize: (files: File[], size: number | null) => boolean;
@@ -21,6 +21,26 @@ export const areFilesAccepted = (files, accept) => {
21
21
  });
22
22
  });
23
23
  };
24
+ export const areItemsAccepted = (item, accept) => {
25
+ if (accept === null) {
26
+ return true;
27
+ }
28
+ const acceptValues = accept.split(/\s*,\s*/);
29
+ return item.every(file => {
30
+ return acceptValues.some(acceptValue => {
31
+ if (acceptValue === 'image/*') {
32
+ return file.type.startsWith('image/');
33
+ }
34
+ if (acceptValue === 'video/*') {
35
+ return file.type.startsWith('video/');
36
+ }
37
+ if (acceptValue === 'audio/*') {
38
+ return file.type.startsWith('audio/');
39
+ }
40
+ return acceptValue === file.type;
41
+ });
42
+ });
43
+ };
24
44
  export const doFilesSatisfySize = (files, size) => {
25
45
  if (size === null || size <= 0) {
26
46
  return true;
@@ -1,4 +1,4 @@
1
- import { defineCustomElement, getAttribute, getBooleanAttribute, getFirstSlotElement, getIntegerAttribute, getReactEventHandler, isAttrEqual, isAttrTrue, NectaryElement, updateAttribute, updateBooleanAttribute } from '../utils';
1
+ import { defineCustomElement, getAttribute, getBooleanAttribute, getIntegerAttribute, getReactEventHandler, isAttrEqual, isAttrTrue, NectaryElement, updateAttribute, updateBooleanAttribute } from '../utils';
2
2
  const templateHTML = '<style>:host{display:inline-block}::slotted(*){display:block}</style><slot></slot>';
3
3
  import { doFilesSatisfySize } from './utils';
4
4
  const template = document.createElement('template');
@@ -6,7 +6,6 @@ template.innerHTML = templateHTML;
6
6
  defineCustomElement('sinch-file-picker', class extends NectaryElement {
7
7
  #$input;
8
8
  #$targetSlot;
9
- #$target = null;
10
9
  #controller = null;
11
10
  constructor() {
12
11
  super();
@@ -30,7 +29,6 @@ defineCustomElement('sinch-file-picker', class extends NectaryElement {
30
29
  disconnectedCallback() {
31
30
  this.#controller.abort();
32
31
  this.#controller = null;
33
- this.#$target = null;
34
32
  }
35
33
  static get observedAttributes() {
36
34
  return ['accept', 'multiple'];
@@ -71,9 +69,8 @@ defineCustomElement('sinch-file-picker', class extends NectaryElement {
71
69
  updateAttribute(this, 'size', value);
72
70
  }
73
71
  #onTargetSlotChange = () => {
74
- this.#$target?.removeEventListener('-click', this.#onTargetClick);
75
- this.#$target = getFirstSlotElement(this.#$targetSlot, true);
76
- this.#$target?.addEventListener('-click', this.#onTargetClick);
72
+ this.#$targetSlot.removeEventListener('click', this.#onTargetClick);
73
+ this.#$targetSlot.addEventListener('click', this.#onTargetClick);
77
74
  };
78
75
  #onTargetClick = () => {
79
76
  this.#$input.click();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nectary/components",
3
- "version": "4.1.4",
3
+ "version": "4.3.0",
4
4
  "files": [
5
5
  "**/*/*.css",
6
6
  "**/*/*.json",
@@ -1,8 +1,9 @@
1
1
  import '../input';
2
2
  import '../icon';
3
3
  import '../text';
4
+ import { isSelectMenuOption } from '../select-menu-option/utils';
4
5
  import { attrValueToPixels, defineCustomElement, getAttribute, getBooleanAttribute, unpackCsv, getFirstCsvValue, getIntegerAttribute, getReactEventHandler, isAttrTrue, NectaryElement, updateAttribute, updateBooleanAttribute, updateCsv, updateExplicitBooleanAttribute, updateIntegerAttribute, debounceTimeout, setClass, subscribeContext, hasClass, isTargetEqual } from '../utils';
5
- const templateHTML = '<style>:host{display:block;outline:0}#listbox{overflow-y:auto}#search{display:none;margin:10px}#search.active{display:block}#search-clear:not(.active){display:none}#not-found{display:flex;align-items:center;justify-content:center;width:100%;height:30px;margin-bottom:10px;pointer-events:none;user-select:none;--sinch-comp-text-font:var(--sinch-comp-select-menu-font-not-found-text);--sinch-global-color-text:var(--sinch-comp-select-menu-color-default-not-found-text-initial)}#not-found:not(.active){display:none}::slotted(.hidden){display:none}</style><sinch-input id="search" size="s" placeholder="Search"><sinch-icon icons-version="2" name="magnifying-glass" id="icon-search" slot="icon"></sinch-icon><sinch-button id="search-clear" slot="right"><sinch-icon icons-version="2" name="fa-xmark" slot="icon"></sinch-icon></sinch-button></sinch-input><div id="not-found"><sinch-text type="m">No results</sinch-text></div><div id="listbox" role="presentation"><slot></slot></div>';
6
+ const templateHTML = '<style>:host{display:block;outline:0}#listbox{overflow-y:auto}#search{display:none;margin:10px}#search.active{display:block}#search-clear:not(.active){display:none}#not-found{display:flex;align-items:center;justify-content:center;width:100%;height:30px;margin-bottom:10px;pointer-events:none;user-select:none;--sinch-comp-text-font:var(--sinch-comp-select-menu-font-not-found-text);--sinch-global-color-text:var(--sinch-comp-select-menu-color-default-not-found-text-initial)}#not-found:not(.active){display:none}::slotted(.hidden){display:none}::slotted(sinch-title){padding:8px 16px;--sinch-global-color-text:var(--sinch-comp-select-menu-color-default-title-initial)}</style><sinch-input id="search" size="s" placeholder="Search"><sinch-icon icons-version="2" name="magnifying-glass" id="icon-search" slot="icon"></sinch-icon><sinch-button id="search-clear" slot="right"><sinch-icon icons-version="2" name="fa-xmark" slot="icon"></sinch-icon></sinch-button></sinch-input><div id="not-found"><sinch-text type="m">No results</sinch-text></div><div id="listbox" role="presentation"><slot></slot></div>';
6
7
  const ITEM_HEIGHT = 40;
7
8
  const NUM_ITEMS_SEARCH = 7;
8
9
  const template = document.createElement('template');
@@ -70,9 +71,12 @@ defineCustomElement('sinch-select-menu', class extends NectaryElement {
70
71
  }
71
72
  case 'rows':
72
73
  {
74
+ const numberOfItems = this.#$optionSlot.assignedElements().length;
75
+ const maxNumberOfRows = parseInt(newVal ?? '0', 10);
73
76
  this.#$listbox.style.maxHeight = attrValueToPixels(newVal, {
74
77
  min: 2,
75
- itemSizeMultiplier: ITEM_HEIGHT
78
+ itemSizeMultiplier: ITEM_HEIGHT,
79
+ addExtraSpace: numberOfItems > maxNumberOfRows
76
80
  });
77
81
  break;
78
82
  }
@@ -142,6 +146,9 @@ defineCustomElement('sinch-select-menu', class extends NectaryElement {
142
146
  };
143
147
  #onListboxClick = e => {
144
148
  const $elem = e.target;
149
+ if ($elem == null || !($elem instanceof Element) || !isSelectMenuOption($elem)) {
150
+ return;
151
+ }
145
152
  this.focus();
146
153
  if (!isTargetEqual(e, this.#$listbox) && !getBooleanAttribute($elem, 'disabled')) {
147
154
  this.#selectOption($elem);
@@ -231,7 +238,7 @@ defineCustomElement('sinch-select-menu', class extends NectaryElement {
231
238
  }
232
239
  #onOptionSlotChange = () => {
233
240
  const searchable = this.searchable;
234
- const options = this.#$optionSlot.assignedElements();
241
+ const options = this.#getOptionElements();
235
242
  const isEnoughOptions = options.length >= NUM_ITEMS_SEARCH;
236
243
  const isSearchActive = isEnoughOptions && searchable !== false || Boolean(searchable);
237
244
  if (!isSearchActive) {
@@ -314,7 +321,8 @@ defineCustomElement('sinch-select-menu', class extends NectaryElement {
314
321
  }
315
322
  }
316
323
  #getOptionElements() {
317
- return this.#$optionSlot.assignedElements();
324
+ const assignedElements = this.#$optionSlot.assignedElements();
325
+ return assignedElements.filter(isSelectMenuOption);
318
326
  }
319
327
  #getSelectedOptionIndex() {
320
328
  const elements = this.#getOptionElements();
@@ -0,0 +1,2 @@
1
+ import type { TSinchSelectMenuOptionElement } from './types';
2
+ export declare const isSelectMenuOption: (el: Element) => el is TSinchSelectMenuOptionElement;
@@ -0,0 +1 @@
1
+ export const isSelectMenuOption = el => el.localName === 'sinch-select-menu-option';
package/utils/dom.d.ts CHANGED
@@ -17,6 +17,7 @@ type IntegerOptions = {
17
17
  defaultValue?: number | null;
18
18
  itemSizeMultiplier?: number;
19
19
  itemSpaceBetween?: number;
20
+ addExtraSpace?: boolean;
20
21
  };
21
22
  export declare const attrValueToInteger: (value: string | null, options?: IntegerOptions) => number | null;
22
23
  export declare const attrValueToPixels: (value: string | null, options?: IntegerOptions) => string;
package/utils/dom.js CHANGED
@@ -72,7 +72,8 @@ export const attrValueToInteger = function (value) {
72
72
  const {
73
73
  defaultValue = null,
74
74
  itemSizeMultiplier = 1,
75
- itemSpaceBetween = 0
75
+ itemSpaceBetween = 0,
76
+ addExtraSpace = false
76
77
  } = options;
77
78
  let intValue = defaultValue;
78
79
  if (value !== null) {
@@ -82,7 +83,7 @@ export const attrValueToInteger = function (value) {
82
83
  }
83
84
  }
84
85
  if (intValue !== null) {
85
- return intValue * itemSizeMultiplier + Math.max(intValue - 1, 0) * itemSpaceBetween;
86
+ return intValue * itemSizeMultiplier + Math.max(intValue - 1, 0) * itemSpaceBetween + (addExtraSpace ? 0.5 * itemSizeMultiplier : 0);
86
87
  }
87
88
  return null;
88
89
  };