|
| 1 | +# Lazy Loader |
| 2 | + |
| 3 | +A lightweight (1.6kb) Web Component based lazy loading library. |
| 4 | + |
| 5 | +## Install |
| 6 | + |
| 7 | +Install via NPM: |
| 8 | + |
| 9 | +```bash |
| 10 | +npm i -S @codewithkyle/lazy-loader |
| 11 | +``` |
| 12 | + |
| 13 | +Or via CDN: |
| 14 | + |
| 15 | +```javascript |
| 16 | +import { configure, update, mount, css } from "https://cdn.jsdelivr.net/npm/@codewithkyle/lazy-loader@1/lazy-loader.min.mjs"; |
| 17 | +``` |
| 18 | + |
| 19 | +```html |
| 20 | +<script src="https://cdn.jsdelivr.net/npm/@codewithkyle/lazy-loader@1/lazy-loader.min.js"> |
| 21 | +``` |
| 22 | +
|
| 23 | +## Usage |
| 24 | +
|
| 25 | +### ES Module |
| 26 | +
|
| 27 | +```typescript |
| 28 | +import { configure, update, mount, css } from "https://cdn.jsdelivr.net/npm/@codewithkyle/lazy-loader@1/lazy-loader.min.mjs"; |
| 29 | +
|
| 30 | +// Start the lazy loading process by configuring the default locations for your JS and CSS files |
| 31 | +configure({ |
| 32 | + jsDir: "/js", |
| 33 | + cssDir: "/css", |
| 34 | +}); |
| 35 | +
|
| 36 | +// Alternatively if the default settings (seen above) are okay you could simply call the update function instead |
| 37 | +// You can call the update function at any time |
| 38 | +update(); |
| 39 | +
|
| 40 | +// Manually mount new components |
| 41 | +import { MyCustomElement } from "./my-custom-element.js"; |
| 42 | +mount("my-custom-element", MyCustomElement); // returns a promise |
| 43 | +
|
| 44 | +// Alternatively if the components file name matches the tag name the library can dynamically import the script from the JS directory (see configure function) |
| 45 | +mount("my-custom-element"); |
| 46 | +
|
| 47 | +// Manually lazy load CSS files |
| 48 | +css("exmaple.css"); // returns a promise |
| 49 | +
|
| 50 | +// Alternatively you can load multiple files at once |
| 51 | +css(["example-one", "examle-two.css", "https://cdn.example.com/example-two.css", "../../relative-path-example.css"]); |
| 52 | +``` |
| 53 | +
|
| 54 | +### Common JS |
| 55 | +
|
| 56 | +```typescript |
| 57 | +LazyLoader.configure({ |
| 58 | + jsDir: "/", |
| 59 | + cssDir: "/", |
| 60 | +}); |
| 61 | +LazyLoader.update(); |
| 62 | +LazyLoader.mount("my-custom-element") |
| 63 | +``` |
| 64 | +
|
| 65 | +### Interfaces |
| 66 | +
|
| 67 | +```typescript |
| 68 | +type Loading = "eager" | "lazy"; |
| 69 | +
|
| 70 | +interface LazyLoaderSettings { |
| 71 | + cssDir?: string; |
| 72 | + jsDir?: string; |
| 73 | +}; |
| 74 | +
|
| 75 | +declare const configure: (settings?:Partial<LazyLoaderSettings>) => void; |
| 76 | +declare const update: () => void; |
| 77 | +declare const mount: (tagName:string, constructor?: CustomElementConstructor) => Promise<void>; |
| 78 | +declare const css: (files:string|string[]) => Promise<void>; |
| 79 | +``` |
| 80 | +
|
| 81 | +### HTML Attributes |
| 82 | +
|
| 83 | +```html |
| 84 | +<!-- Lazy load Web Components by tagging custom elements with the web-component attribute. --> |
| 85 | +<custom-element web-component></custom-element> |
| 86 | +
|
| 87 | +<!-- By default components are loaded and mounted when they enter the viewport. --> |
| 88 | +<!-- You can bypass the lazy loader using the loading attribute. --> |
| 89 | +<custom-element web-component loading="eager"></custom-element> |
| 90 | +
|
| 91 | +<!-- Lazy load CSS by attaching the css attribute to any element within the documents body. --> |
| 92 | +<!-- You can load multiple files using a whitespace separator. The .css file extenstion is optional. --> |
| 93 | +<div class="my-awesome-class" css="awesome-transitions awesome.css"></div> |
| 94 | +``` |
0 commit comments