Skip to content

Commit 0a62719

Browse files
committed
Added configure, mount, css, and update functionality
0 parents  commit 0a62719

File tree

11 files changed

+1059
-0
lines changed

11 files changed

+1059
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.DS_Store
2+
.editorconfig
3+
node_modules
4+
lazy-loader.min.js
5+
lazy-loader.min.mjs

CHANGELOG.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Changelog
2+
3+
All notable changes to this project will be documented in this file.
4+
5+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7+
8+
## [Unreleased]
9+
10+
## [1.0.0] - 2021-03-28
11+
12+
### Added
13+
14+
- `mount()` functionality
15+
- `configure()` functionality
16+
- `css()` functionality
17+
- `update()` functionality
18+
- automatic lazy loading web components
19+
- automatic lazy loading css
20+
21+
[unreleased]: https://github.com/codewithkyle/lazy-loader/compare/v1.0.0...HEAD
22+
[1.0.0]: https://github.com/codewithkyle/lazy-loader/releases/tag/v1.0.0

README.md

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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+
```

lazy-loader.d.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export type Loading = "eager" | "lazy";
2+
3+
export type LazyLoaderSettings = {
4+
cssDir?: string;
5+
jsDir?: string;
6+
};
7+
8+
declare const configure: (settings?:Partial<LazyLoaderSettings>) => void;
9+
declare const update: () => void;
10+
declare const mount: (tagName:string, constructor?: CustomElementConstructor) => Promise<void>;
11+
declare const css: (files:string|string[]) => Promise<void>;

0 commit comments

Comments
 (0)