Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
nk-o committed Nov 27, 2023
1 parent 37a8cd8 commit 75e8c71
Show file tree
Hide file tree
Showing 43 changed files with 6,543 additions and 1 deletion.
1 change: 1 addition & 0 deletions .browserslistrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
extends browserslist-config-nk
11 changes: 11 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# editorconfig.org

root = true

[*]
charset = utf-8
end_of_line = lf
indent_size = 2
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
demo
dist
6 changes: 6 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

{
"extends": [
"eslint-config-nk"
]
}
12 changes: 12 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Enforce Unix newlines
*.css text eol=lf
*.html text eol=lf
*.js text eol=lf
*.json text eol=lf
*.md text eol=lf
*.php text eol=lf
*.rb text eol=lf
*.scss text eol=lf
*.svg text eol=lf
*.txt text eol=lf
*.yml text eol=lf
24 changes: 24 additions & 0 deletions .github/workflows/npm-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# This workflow will publish a package to GitHub Packages when a release is created
# For more information see: https://docs.github.com/en/actions/publishing-packages/publishing-nodejs-packages

name: Node.js Package

on:
workflow_dispatch:
push:
tags:
- 'v*'

jobs:
publish-npm:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 16
registry-url: https://registry.npmjs.org/
- run: npm ci
- run: npm publish
env:
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
4 changes: 4 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env sh
. "$(dirname "$0")/_/husky.sh"

npx lint-staged
4 changes: 4 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules
dist
vendor
vendors
1 change: 1 addition & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('prettier-config-nk');
8 changes: 8 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"recommendations": [
"esbenp.prettier-vscode",
"streetsidesoftware.code-spell-checker",
"editorconfig.editorconfig",
"dbaeumer.vscode-eslint"
]
}
10 changes: 10 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"editor.defaultFormatter": null,
"[javascript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[json]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"editor.formatOnSave": true
}
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Changelog

## [0.1.0] - Nov 26, 2023

- initial release
158 changes: 157 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,157 @@
# ivents
# ivents <!-- omit in toc -->

![ivents.min.js](https://img.badgesize.io/nk-crew/ivents/master/dist/ivents.min.js?compression=gzip)

Helper functions for browser event listener

## Table of Contents <!-- omit in toc -->

- [Import ivents](#import-ivents)
- [ESM](#esm)
- [ESM CDN](#esm-cdn)
- [UMD](#umd)
- [UMD CDN](#umd-cdn)
- [CJS (Bundlers like Webpack)](#cjs-bundlers-like-webpack)
- [Methods](#methods)
- [on|one](#onone)
- [off](#off)
- [trigger](#trigger)
- [For Developers](#for-developers)

## Import ivents

Use one of the following examples to import script.

### ESM

We provide a version of ivents built as ESM (ivents.esm.js and ivents.esm.min.js) which allows you to use ivents as a module in your browser, if your [targeted browsers support it](https://caniuse.com/es6-module).

```html
<script type="module">
import { on, off } from "ivents.esm.min.js";
</script>
```

### ESM CDN

```html
<script type="module">
import { on, off } from "https://cdn.jsdelivr.net/npm/[email protected]/+esm";
</script>
```

### UMD

ivents may be also used in a traditional way by including script in HTML and using library by accessing `window.ivents`.

```html
<script src="ivents.min.js"></script>
```


### UMD CDN

```html
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/ivents.min.js"></script>
```

### CJS (Bundlers like Webpack)

Install ivents as a Node.js module using npm

```
npm install ivents
```

Import ivents by adding this line to your app's entry point (usually `index.js` or `app.js`):

```javascript
import { on, off } from 'ivents';
```

## Methods

### on|one

DOM event listener:

```javascript
import { on } from 'ivents';

on(document, 'click', (e) => {
console.log('clicked', e);
});
```

Event listener with delegated target:

```javascript
import { on } from 'ivents';

on(document, 'click', '.custom-element-selector', (e) => {
console.log('clicked', e);
});
```

Custom event listener with namespace:

```javascript
import { on } from 'ivents';

on(document, 'the-custom-event.with-namespace', (e) => {
console.log('clicked', e);
});
```

### off

Remove DOM event listener:

```javascript
import { on } from 'ivents';

on(document, 'click', (e) => {
console.log('clicked', e);
});

off(document, 'click');
```

Remove DOM event listener by namespace:

```javascript
import { on } from 'ivents';

on(document, 'click.my-namespace', (e) => {
console.log('clicked', e);
});

off(document, '.my-namespace');
```

### trigger

Trigger event:

```javascript
import { trigger } from 'ivents';

trigger(document, 'click', (e) => {
console.log('clicked', e);
});
```

## For Developers

### Installation <!-- omit in toc -->

* Run `npm install` in the command line. Or if you need to update some dependencies, run `npm update`

### Building <!-- omit in toc -->

* `npm run build` to run build

### Linting <!-- omit in toc -->

* `npm run js-lint` to show eslint errors
* `npm run js-lint-fix` to automatically fix some of the eslint errors
36 changes: 36 additions & 0 deletions demo/esm.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Test</title>
</head>
<body>
<nav>
<a href="esm.html">ESM</a>
<a href="umd.html">UMD</a>
</nav>
<h1>Test ivents calls as ESM.</h1>
<p>Check browser console for output.</p>
<button class="ivents-test-click">Click.</button>

<script type="module">
import { on, trigger } from "../dist/ivents.esm.min.js";

on(document, 'custom-event-name', (e) => {
e.preventDefault();
console.log('custom event triggered', e);
});
trigger(document, 'custom-event-name', { data: 'custom event data' });

on(window, 'load', (e) => {
console.log('window loaded', e);
});
on(document, 'click', '.ivents-test-click', (e) => {
e.preventDefault();
console.log('button clicked', e);
});
</script>
</body>
</html>
15 changes: 15 additions & 0 deletions demo/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Test</title>
</head>
<body>
<nav>
<a href="esm.html">ESM</a>
<a href="umd.html">UMD</a>
</nav>
</body>
</html>
35 changes: 35 additions & 0 deletions demo/umd.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Test</title>
</head>
<body>
<nav>
<a href="esm.html">ESM</a>
<a href="umd.html">UMD</a>
</nav>
<h1>Test ivents calls as UMD.</h1>
<p>Check browser console for output.</p>
<button class="ivents-test-click">Click.</button>

<script src="../dist/ivents.min.js"></script>
<script>
ivents.on(document, 'custom-event-name', (e) => {
e.preventDefault();
console.log('custom event triggered', e);
});
ivents.trigger(document, 'custom-event-name', { data: 'custom event data' });

ivents.on(window, 'load', (e) => {
console.log('window loaded', e);
});
ivents.on(document, 'click', '.ivents-test-click', (e) => {
e.preventDefault();
console.log('button clicked', e);
});
</script>
</body>
</html>
Loading

0 comments on commit 75e8c71

Please sign in to comment.