kempo 3.2.0 → 3.2.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.
@@ -1 +1 @@
1
- import Component from"./Component.js";import{isInView}from"../utils/element.js";import raf from"../utils/raf.js";const inViewIntervalTimeout=Symbol(),inViewIntervalId=Symbol();export default class LazyComponent extends Component{constructor(e){super(e),this[inViewIntervalTimeout]=2e3,this[inViewIntervalId]=null,new IntersectionObserver((([e])=>{e.intersectionRatio>0&&!this.rendered?this.inViewCallback():this.outOfViewCallback()})).observe(this),this.registerAttribute("unrender",!1)}connectedCallback(){this.renderSkelton(),raf((()=>{isInView(this)?this.inViewCallback():this.outOfViewCallback()}),2)}async inViewCallback(){return clearInterval(this[inViewIntervalId]),await this.render()}async outOfViewCallback(){return clearInterval(this[inViewIntervalId]),this[inViewIntervalId]=setInterval((async()=>{await isInView(this)&&this.inViewCallback()}),this[inViewIntervalTimeout]),!!this.unrender&&(await this.renderSkelton(),!0)}async renderSkelton(){return this.shadowRoot.innerHTML=`<link rel="stylesheet" href="${Component.pathToKempo}/kempo-styles.css" />${this.skeletonTemplate}<style>${this.shadowStyles}</style>`,this.rendered=!1,!0}get skeletonTemplate(){return'<div style="height: var(--skeleton_height, 1rem)"></div>'}}
1
+ import Component from"./Component.js";import{isInView}from"../utils/element.js";import raf from"../utils/raf.js";const inViewIntervalId=Symbol();export default class LazyComponent extends Component{constructor(e){super(e),this[inViewIntervalId]=null,new IntersectionObserver((([e])=>{e.intersectionRatio>0&&!this.rendered?this.inViewCallback():this.outOfViewCallback()})).observe(this),this.registerAttributes({unrender:!1,inViewInterval:0})}connectedCallback(){this.renderSkelton(),raf((()=>{isInView(this)?this.inViewCallback():this.outOfViewCallback()}),2)}async inViewCallback(){return clearInterval(this[inViewIntervalId]),await this.render()}async outOfViewCallback(){return clearInterval(this[inViewIntervalId]),this[inViewIntervalId]=setInterval((async()=>{await isInView(this)&&this.inViewCallback()}),this.inViewInterval||this.constructor.inViewIntervalTimeout),!!this.unrender&&(await this.renderSkelton(),!0)}async renderSkelton(){return this.shadowRoot.innerHTML=`<link rel="stylesheet" href="${Component.pathToKempo}/kempo-styles.css" />${this.skeletonTemplate}<style>${this.shadowStyles}</style>`,this.rendered=!1,!0}get skeletonTemplate(){return'<div style="height: var(--skeleton_height, 1rem)"></div>'}static inViewIntervalTimeout=1e3}
@@ -41,6 +41,14 @@
41
41
  <p>Extend the <code>LazyComponent</code> class to create a lazy-loaded component. The component will render its content only when it becomes visible in the viewport.</p>
42
42
  <pre><code class="hljs javascript"><span class="hljs-keyword">import</span> LazyComponent <span class="hljs-keyword">from</span> <span class="hljs-string">'kempo'</span>;<br /><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyLazyComponent</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">LazyComponent</span> </span>{<br /> <span class="hljs-keyword">constructor</span>(){<br /> <span class="hljs-keyword">super</span>();<br /> <span class="hljs-comment">// Your Constructor</span><br /> }<br /> <span class="hljs-keyword">async</span> render(force){<br /> <span class="hljs-keyword">if</span>(<span class="hljs-keyword">await</span> <span class="hljs-keyword">super</span>.render(force)){<br /> <span class="hljs-comment">// Your render logic</span><br /> <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;<br /> }<br /> <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;<br /> }<br />}<br /><span class="hljs-built_in">window</span>.customElements.define(<span class="hljs-string">'my-lazy-component'</span>, MyLazyComponent);</code></pre>
43
43
 
44
+ <h3 id="basicUsage"><a href="#interval" class="no-link">In-View Interval Timeout</a></h3>
45
+ <p>In addition to using intersection observes to watch for components being scrolled into view, an interval is used to check if the component suddenly become visible via a display style change to itself or a parent. The timeout of the interval can be set for all instances of your LazyComponent by chaning the static member <code>inViewIntervalTimeout</code>.</p>
46
+ <pre><code class="hljs javascript"><span class="hljs-keyword">import</span> LazyComponent <span class="hljs-keyword">from</span> <span class="hljs-string">'kempo'</span>;<br /><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyLazyComponent</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">LazyComponent</span> </span>{<br /> <span class="hljs-keyword">constructor</span>(){<br /> <span class="hljs-keyword">super</span>();<br /> <span class="hljs-comment">// Your Constructor</span><br /> }<br /> <span class="hljs-keyword">async</span> render(force){<br /> <span class="hljs-keyword">if</span>(<span class="hljs-keyword">await</span> <span class="hljs-keyword">super</span>.render(force)){<br /> <span class="hljs-comment">// Your render logic</span><br /> <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;<br /> }<br /> <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;<br /> }<br /><br /> <span class="hljs-keyword">static</span> inViewIntervalTimeout = <span class="hljs-number">200</span>; <span class="hljs-comment">// Change the inView checking interval to 200ms instead of the default 1000</span><br />}<br /><span class="hljs-built_in">window</span>.customElements.define(<span class="hljs-string">'my-lazy-component'</span>, MyLazyComponent);</code></pre>
47
+
48
+ <h5>Individal Component interval timer overwrites</h5>
49
+ <p>You can overwrite this interval timeout for individual elements by setting the <code>in-view-interval</code> attribute.</p>
50
+ <pre><code class="hljs xml"><span class="hljs-tag">&lt;<span class="hljs-name">my-lazy-component</span> <span class="hljs-attr">in-view-interval</span>=<span class="hljs-string">"5000"</span>&gt;</span><br /> <span class="hljs-comment">&lt;!-- The interval to check if it is in-view (due to element/parent visibility change) is now set to check every 5 seconds --&gt;</span><br /><span class="hljs-tag">&lt;/<span class="hljs-name">my-lazy-component</span>&gt;</span></code></pre>
51
+
44
52
  <h2 id="jsRef">JavaScript Reference</h2>
45
53
 
46
54
  <h3 id="constructor"><a href="#constructor" class="no-link">Constructor</a></h3>
@@ -1 +1 @@
1
- import Component from"./Component.js";import{isInView}from"../utils/element.js";import raf from"../utils/raf.js";const inViewIntervalTimeout=Symbol(),inViewIntervalId=Symbol();export default class LazyComponent extends Component{constructor(e){super(e),this[inViewIntervalTimeout]=2e3,this[inViewIntervalId]=null,new IntersectionObserver((([e])=>{e.intersectionRatio>0&&!this.rendered?this.inViewCallback():this.outOfViewCallback()})).observe(this),this.registerAttribute("unrender",!1)}connectedCallback(){this.renderSkelton(),raf((()=>{isInView(this)?this.inViewCallback():this.outOfViewCallback()}),2)}async inViewCallback(){return clearInterval(this[inViewIntervalId]),await this.render()}async outOfViewCallback(){return clearInterval(this[inViewIntervalId]),this[inViewIntervalId]=setInterval((async()=>{await isInView(this)&&this.inViewCallback()}),this[inViewIntervalTimeout]),!!this.unrender&&(await this.renderSkelton(),!0)}async renderSkelton(){return this.shadowRoot.innerHTML=`<link rel="stylesheet" href="${Component.pathToKempo}/kempo-styles.css" />${this.skeletonTemplate}<style>${this.shadowStyles}</style>`,this.rendered=!1,!0}get skeletonTemplate(){return'<div style="height: var(--skeleton_height, 1rem)"></div>'}}
1
+ import Component from"./Component.js";import{isInView}from"../utils/element.js";import raf from"../utils/raf.js";const inViewIntervalId=Symbol();export default class LazyComponent extends Component{constructor(e){super(e),this[inViewIntervalId]=null,new IntersectionObserver((([e])=>{e.intersectionRatio>0&&!this.rendered?this.inViewCallback():this.outOfViewCallback()})).observe(this),this.registerAttributes({unrender:!1,inViewInterval:0})}connectedCallback(){this.renderSkelton(),raf((()=>{isInView(this)?this.inViewCallback():this.outOfViewCallback()}),2)}async inViewCallback(){return clearInterval(this[inViewIntervalId]),await this.render()}async outOfViewCallback(){return clearInterval(this[inViewIntervalId]),this[inViewIntervalId]=setInterval((async()=>{await isInView(this)&&this.inViewCallback()}),this.inViewInterval||this.constructor.inViewIntervalTimeout),!!this.unrender&&(await this.renderSkelton(),!0)}async renderSkelton(){return this.shadowRoot.innerHTML=`<link rel="stylesheet" href="${Component.pathToKempo}/kempo-styles.css" />${this.skeletonTemplate}<style>${this.shadowStyles}</style>`,this.rendered=!1,!0}get skeletonTemplate(){return'<div style="height: var(--skeleton_height, 1rem)"></div>'}static inViewIntervalTimeout=1e3}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kempo",
3
- "version": "3.2.0",
3
+ "version": "3.2.1",
4
4
  "type": "module",
5
5
  "description": "A web component library",
6
6
  "exports": {
@@ -11,7 +11,7 @@
11
11
  "./components/*": "./dist/components/*.js"
12
12
  },
13
13
  "scripts": {
14
- "test": "node ./scrips/test.js",
14
+ "test": "node ./scripts/test.js",
15
15
  "build": "node ./scripts/build.js",
16
16
  "docs": "node ./scripts/docs.js",
17
17
  "docs-src": "node ./scripts/docs.js -s"
@@ -0,0 +1,31 @@
1
+ import express from 'express';
2
+ import { dirname, join } from 'path';
3
+ import { fileURLToPath } from 'url';
4
+ import {
5
+ getArgs,
6
+ runChildNodeProcess
7
+ } from '../src/utils/cli.js';
8
+
9
+ const __filename = fileURLToPath(import.meta.url);
10
+ const __dirname = dirname(__filename);
11
+
12
+ const options = getArgs({
13
+ p: 'port',
14
+ b: 'build',
15
+ s: 'src'
16
+ });
17
+
18
+ const {
19
+ port = 8084,
20
+ build = true
21
+ } = options;
22
+
23
+ const app = express();
24
+
25
+ app.use('/kempo', express.static('src'));
26
+ app.use('/icons', express.static('icons'));
27
+ app.use(express.static('test'));
28
+
29
+ app.listen(port, () => {
30
+ console.log(`Docs Running on: http://localhost:${port}`);
31
+ });
@@ -107,3 +107,4 @@ export default class Component extends HTMLElement {
107
107
  static observedAttributes = ['rendered'];
108
108
  static pathToKempo = import.meta.url.substring(0, import.meta.url.indexOf('/components/Component.js') );
109
109
  }
110
+ window.customElements.define('k-component', Component);
@@ -2,14 +2,12 @@ import Component from './Component.js';
2
2
  import { isInView } from '../utils/element.js';
3
3
  import raf from '../utils/raf.js';
4
4
 
5
- const inViewIntervalTimeout = Symbol(),
6
- inViewIntervalId = Symbol();
5
+ const inViewIntervalId = Symbol();
7
6
  export default class LazyComponent extends Component {
8
7
  constructor(shadowDetails){
9
8
  super(shadowDetails);
10
9
 
11
10
  /* Private Members */
12
- this[inViewIntervalTimeout] = 2000;
13
11
  this[inViewIntervalId] = null;
14
12
 
15
13
  (new IntersectionObserver(([comp]) => {
@@ -20,7 +18,10 @@ export default class LazyComponent extends Component {
20
18
  }
21
19
  })).observe(this);
22
20
 
23
- this.registerAttribute('unrender', false);
21
+ this.registerAttributes({
22
+ unrender: false,
23
+ inViewInterval: 0
24
+ });
24
25
  }
25
26
  connectedCallback(){
26
27
  // Do not do super.connectedCallback() because it will render the component prematurely
@@ -43,7 +44,7 @@ export default class LazyComponent extends Component {
43
44
  if(await isInView(this)){
44
45
  this.inViewCallback();
45
46
  }
46
- }, this[inViewIntervalTimeout]);
47
+ }, this.inViewInterval || LazyComponent.inViewIntervalTimeout);
47
48
  if(this.unrender){
48
49
  await this.renderSkelton();
49
50
  return true;
@@ -58,4 +59,7 @@ export default class LazyComponent extends Component {
58
59
  get skeletonTemplate(){
59
60
  return /*html*/`<div style="height: var(--skeleton_height, 1rem)"></div>`;
60
61
  }
62
+
63
+ /* Static Members */
64
+ static inViewIntervalTimeout = 1000;
61
65
  }