@ucd-lib/theme-elements 0.0.13 → 0.0.14

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ucd-lib/theme-elements",
3
- "version": "0.0.13",
3
+ "version": "0.0.14",
4
4
  "description": "Custom elements for the UCD brand theme",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -44,17 +44,19 @@ export default class UcdlibIcon extends LitElement {
44
44
  * @private
45
45
  */
46
46
  disconnectedCallback() {
47
- window.removeEventListener('ucdlib-iconset-added', this._onAddedIconset);
47
+ if ( this._setListener ){
48
+ window.removeEventListener('ucdlib-iconset-added', this._onAddedIconset);
49
+ }
48
50
  super.disconnectedCallback();
49
51
  }
50
52
 
51
53
  /**
52
- * @method updated
53
- * @description Lit lifecyle method called after element updates
54
+ * @method willUpdate
55
+ * @description Lit lifecyle method called right before element updates
54
56
  * @param {Map} props - Updated properties
55
57
  * @private
56
58
  */
57
- updated(props){
59
+ willUpdate(props){
58
60
  if ( props.has('icon') || props.has('src') ){
59
61
  if ( this.src ) {
60
62
  this._updateIcon();
@@ -85,17 +87,27 @@ export default class UcdlibIcon extends LitElement {
85
87
  _updateIcon(){
86
88
  // using the icon attribute
87
89
  if ( this._usesIconSet() ) {
90
+
91
+ // previously using the src attribute. remove it.
88
92
  if ( this._img && this._img.parentNode ) this.renderRoot.removeChild(this._img);
89
93
 
94
+ // empty icon name, remove it
90
95
  if ( this._iconName === '') {
91
96
  if ( this._iconset ) this._iconset.removeIcon(this);
97
+
98
+ // check if iconset exists, add event listener if it doesn't
92
99
  } else if ( this._iconsetName ){
93
100
  this._iconset = this._getIconset();
94
- if ( this._iconset ) {
101
+ if ( this._iconset && this._iconset.applyIcon ) {
95
102
  this._iconset.applyIcon(this, this._iconName);
96
- window.addEventListener('ucdlib-iconset-added', this._onAddedIconset);
103
+ if ( this._setListener ){
104
+ window.removeEventListener('ucdlib-iconset-added', this._onAddedIconset);
105
+ this._setListener = false;
106
+ }
97
107
  } else {
98
- window.removeEventListener('ucdlib-iconset-added', this._onAddedIconset);
108
+ if ( !this._setListener ){
109
+ this._setListener = window.addEventListener('ucdlib-iconset-added', this._onAddedIconset);
110
+ }
99
111
  }
100
112
  }
101
113
 
@@ -10,6 +10,7 @@ import { MutationObserverController } from '../../utils/controllers';
10
10
  * @property {String} name - Name of the icon set. Usage: <ucdlib-icon icon="{thisProperty}:{icon}"></ucdlib-icon>
11
11
  * @property {Number} size - The size of an individual icon. Note that icons must be square.
12
12
  * @property {String} label - Optional friendly label for iconset.
13
+ * @property {String} suppressWarnings - Suppress any "you're doing it wrong" console warnings
13
14
  * @example
14
15
  * <ucdlib-iconset name="arrows">
15
16
  <svg>
@@ -28,6 +29,7 @@ export default class UcdlibIconset extends Mixin(LitElement)
28
29
  name: {type: String},
29
30
  size: {type: Number},
30
31
  label: {type: String},
32
+ suppressWarnings: {type: Boolean, attribute: 'suppress-warnings'},
31
33
  _iconMap: {type: Object, state: true}
32
34
  };
33
35
  }
@@ -40,7 +42,7 @@ export default class UcdlibIconset extends Mixin(LitElement)
40
42
  this.label = "";
41
43
  this.size = 24;
42
44
  this._iconMap = {};
43
- this.style.display = "none";
45
+ this.suppressWarnings = false;
44
46
  }
45
47
 
46
48
  /**
@@ -51,12 +53,28 @@ export default class UcdlibIconset extends Mixin(LitElement)
51
53
  */
52
54
  updated( props ){
53
55
  if (props.has('name') && this.name ) {
54
- this.dispatchEvent(
55
- new CustomEvent('ucdlib-iconset-added', {bubbles: true, composed: true})
56
- );
56
+ this.dispatchLoadEvent();
57
57
  }
58
58
  }
59
59
 
60
+ /**
61
+ * @method firstUpdated
62
+ * @description Lit lifecycle method when element is first updated
63
+ */
64
+ firstUpdated(){
65
+ this.style.display = "none";
66
+ }
67
+
68
+ /**
69
+ * @method dispatchLoadEvent
70
+ * @description fires off a 'ucdlib-iconset-added' event so ucdlib-icon elements can re-render if applicable
71
+ */
72
+ dispatchLoadEvent(){
73
+ this.dispatchEvent(
74
+ new CustomEvent('ucdlib-iconset-added', {bubbles: true, composed: true})
75
+ );
76
+ }
77
+
60
78
  /**
61
79
  * @method getIconNames
62
80
  * @description Returns a list of icon names for this set
@@ -173,7 +191,7 @@ export default class UcdlibIconset extends Mixin(LitElement)
173
191
  iconMap[icon.id] = icon;
174
192
  });
175
193
 
176
- if ( !Object.keys(iconMap).length ) {
194
+ if ( !Object.keys(iconMap).length && !this.suppressWarnings ) {
177
195
  console.warn('No g elements with an id attribute found!.');
178
196
  }
179
197
  this._iconMap = iconMap;