flip-toggle 0.0.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.
- package/README.md +68 -0
- package/dist/index.cjs +77 -0
- package/dist/index.d.cts +62 -0
- package/dist/index.d.ts +62 -0
- package/dist/index.js +51 -0
- package/dist/toggle.css +0 -0
- package/dist/toggle.js +0 -0
- package/package.json +26 -0
package/README.md
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
# Flip
|
|
2
|
+
|
|
3
|
+
A lightweight, accessible toggle component with smooth animations.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install flip
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```js
|
|
14
|
+
import { Toggle } from 'flip'
|
|
15
|
+
import 'flip/styles.css'
|
|
16
|
+
|
|
17
|
+
const toggle = new Toggle(document.getElementById('my-toggle'), {
|
|
18
|
+
checked: false,
|
|
19
|
+
disabled: false,
|
|
20
|
+
onChange: (checked) => {
|
|
21
|
+
console.log('Toggle state:', checked)
|
|
22
|
+
}
|
|
23
|
+
})
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## API
|
|
27
|
+
|
|
28
|
+
### Options
|
|
29
|
+
|
|
30
|
+
- `checked` (boolean) - Initial checked state. Default: `false`
|
|
31
|
+
- `disabled` (boolean) - Whether the toggle is disabled. Default: `false`
|
|
32
|
+
- `onChange` (function) - Callback fired when toggle state changes
|
|
33
|
+
|
|
34
|
+
### Methods
|
|
35
|
+
|
|
36
|
+
- `toggle.setChecked(checked)` - Programmatically set checked state
|
|
37
|
+
- `toggle.setDisabled(disabled)` - Programmatically set disabled state
|
|
38
|
+
|
|
39
|
+
## Customization
|
|
40
|
+
|
|
41
|
+
Override CSS variables to customize appearance:
|
|
42
|
+
|
|
43
|
+
```css
|
|
44
|
+
.flip-toggle {
|
|
45
|
+
--toggle-width: 61px;
|
|
46
|
+
--toggle-height: 31px;
|
|
47
|
+
--toggle-padding: 2px;
|
|
48
|
+
--thumb-size: 27px;
|
|
49
|
+
--toggle-bg: #e5e5ea;
|
|
50
|
+
--toggle-bg-checked: #34c759;
|
|
51
|
+
}
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## Features
|
|
55
|
+
|
|
56
|
+
- ✅ Lightweight (~2KB)
|
|
57
|
+
- ✅ Accessible (ARIA, keyboard navigation)
|
|
58
|
+
- ✅ Smooth animations
|
|
59
|
+
- ✅ Customizable
|
|
60
|
+
- ✅ No dependencies
|
|
61
|
+
|
|
62
|
+
## Author
|
|
63
|
+
|
|
64
|
+
[Ryan Cuff](https://ryancuff.xyz)
|
|
65
|
+
|
|
66
|
+
## License
|
|
67
|
+
|
|
68
|
+
MIT
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
|
|
19
|
+
// src/index.js
|
|
20
|
+
var index_exports = {};
|
|
21
|
+
__export(index_exports, {
|
|
22
|
+
Toggle: () => Toggle
|
|
23
|
+
});
|
|
24
|
+
module.exports = __toCommonJS(index_exports);
|
|
25
|
+
|
|
26
|
+
// src/toggle.js
|
|
27
|
+
var Toggle = class {
|
|
28
|
+
constructor(element, options = {}) {
|
|
29
|
+
this.element = element;
|
|
30
|
+
this.checked = options.checked ?? false;
|
|
31
|
+
this.disabled = options.disabled ?? false;
|
|
32
|
+
this.onChange = options.onChange;
|
|
33
|
+
this.render();
|
|
34
|
+
}
|
|
35
|
+
render() {
|
|
36
|
+
this.element.classList.add("flip-toggle");
|
|
37
|
+
this.element.setAttribute("role", "switch");
|
|
38
|
+
this.element.setAttribute("aria-checked", this.checked);
|
|
39
|
+
this.element.setAttribute("tabindex", "0");
|
|
40
|
+
if (this.disabled) {
|
|
41
|
+
this.element.setAttribute("aria-disabled", "true");
|
|
42
|
+
}
|
|
43
|
+
const thumb = document.createElement("span");
|
|
44
|
+
thumb.classList.add("flip-toggle-thumb");
|
|
45
|
+
this.element.appendChild(thumb);
|
|
46
|
+
this.element.addEventListener("click", () => this.toggle());
|
|
47
|
+
this.element.addEventListener("keydown", (e) => {
|
|
48
|
+
if (e.key === " " || e.key === "Enter") {
|
|
49
|
+
e.preventDefault();
|
|
50
|
+
this.toggle();
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
toggle() {
|
|
55
|
+
var _a;
|
|
56
|
+
if (this.disabled) return;
|
|
57
|
+
this.checked = !this.checked;
|
|
58
|
+
this.element.setAttribute("aria-checked", this.checked);
|
|
59
|
+
(_a = this.onChange) == null ? void 0 : _a.call(this, this.checked);
|
|
60
|
+
}
|
|
61
|
+
setChecked(checked) {
|
|
62
|
+
this.checked = checked;
|
|
63
|
+
this.element.setAttribute("aria-checked", this.checked);
|
|
64
|
+
}
|
|
65
|
+
setDisabled(disabled) {
|
|
66
|
+
this.disabled = disabled;
|
|
67
|
+
if (disabled) {
|
|
68
|
+
this.element.setAttribute("aria-disabled", "true");
|
|
69
|
+
} else {
|
|
70
|
+
this.element.removeAttribute("aria-disabled");
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
75
|
+
0 && (module.exports = {
|
|
76
|
+
Toggle
|
|
77
|
+
});
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
class Toggle {
|
|
2
|
+
constructor(element, options = {}) {
|
|
3
|
+
this.element = element;
|
|
4
|
+
this.checked = options.checked ?? false;
|
|
5
|
+
this.disabled = options.disabled ?? false;
|
|
6
|
+
this.onChange = options.onChange;
|
|
7
|
+
|
|
8
|
+
this.render();
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
render() {
|
|
12
|
+
// Add base class
|
|
13
|
+
this.element.classList.add('flip-toggle');
|
|
14
|
+
|
|
15
|
+
// Accessibility attributes
|
|
16
|
+
this.element.setAttribute('role', 'switch');
|
|
17
|
+
this.element.setAttribute('aria-checked', this.checked);
|
|
18
|
+
this.element.setAttribute('tabindex', '0');
|
|
19
|
+
|
|
20
|
+
if (this.disabled) {
|
|
21
|
+
this.element.setAttribute('aria-disabled', 'true');
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// Create thumb element
|
|
25
|
+
const thumb = document.createElement('span');
|
|
26
|
+
thumb.classList.add('flip-toggle-thumb');
|
|
27
|
+
this.element.appendChild(thumb);
|
|
28
|
+
|
|
29
|
+
// Event listeners
|
|
30
|
+
this.element.addEventListener('click', () => this.toggle());
|
|
31
|
+
this.element.addEventListener('keydown', (e) => {
|
|
32
|
+
if (e.key === ' ' || e.key === 'Enter') {
|
|
33
|
+
e.preventDefault();
|
|
34
|
+
this.toggle();
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
toggle() {
|
|
40
|
+
if (this.disabled) return
|
|
41
|
+
|
|
42
|
+
this.checked = !this.checked;
|
|
43
|
+
this.element.setAttribute('aria-checked', this.checked);
|
|
44
|
+
this.onChange?.(this.checked);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
setChecked(checked) {
|
|
48
|
+
this.checked = checked;
|
|
49
|
+
this.element.setAttribute('aria-checked', this.checked);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
setDisabled(disabled) {
|
|
53
|
+
this.disabled = disabled;
|
|
54
|
+
if (disabled) {
|
|
55
|
+
this.element.setAttribute('aria-disabled', 'true');
|
|
56
|
+
} else {
|
|
57
|
+
this.element.removeAttribute('aria-disabled');
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export { Toggle };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
class Toggle {
|
|
2
|
+
constructor(element, options = {}) {
|
|
3
|
+
this.element = element;
|
|
4
|
+
this.checked = options.checked ?? false;
|
|
5
|
+
this.disabled = options.disabled ?? false;
|
|
6
|
+
this.onChange = options.onChange;
|
|
7
|
+
|
|
8
|
+
this.render();
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
render() {
|
|
12
|
+
// Add base class
|
|
13
|
+
this.element.classList.add('flip-toggle');
|
|
14
|
+
|
|
15
|
+
// Accessibility attributes
|
|
16
|
+
this.element.setAttribute('role', 'switch');
|
|
17
|
+
this.element.setAttribute('aria-checked', this.checked);
|
|
18
|
+
this.element.setAttribute('tabindex', '0');
|
|
19
|
+
|
|
20
|
+
if (this.disabled) {
|
|
21
|
+
this.element.setAttribute('aria-disabled', 'true');
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// Create thumb element
|
|
25
|
+
const thumb = document.createElement('span');
|
|
26
|
+
thumb.classList.add('flip-toggle-thumb');
|
|
27
|
+
this.element.appendChild(thumb);
|
|
28
|
+
|
|
29
|
+
// Event listeners
|
|
30
|
+
this.element.addEventListener('click', () => this.toggle());
|
|
31
|
+
this.element.addEventListener('keydown', (e) => {
|
|
32
|
+
if (e.key === ' ' || e.key === 'Enter') {
|
|
33
|
+
e.preventDefault();
|
|
34
|
+
this.toggle();
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
toggle() {
|
|
40
|
+
if (this.disabled) return
|
|
41
|
+
|
|
42
|
+
this.checked = !this.checked;
|
|
43
|
+
this.element.setAttribute('aria-checked', this.checked);
|
|
44
|
+
this.onChange?.(this.checked);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
setChecked(checked) {
|
|
48
|
+
this.checked = checked;
|
|
49
|
+
this.element.setAttribute('aria-checked', this.checked);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
setDisabled(disabled) {
|
|
53
|
+
this.disabled = disabled;
|
|
54
|
+
if (disabled) {
|
|
55
|
+
this.element.setAttribute('aria-disabled', 'true');
|
|
56
|
+
} else {
|
|
57
|
+
this.element.removeAttribute('aria-disabled');
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export { Toggle };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
// src/toggle.js
|
|
2
|
+
var Toggle = class {
|
|
3
|
+
constructor(element, options = {}) {
|
|
4
|
+
this.element = element;
|
|
5
|
+
this.checked = options.checked ?? false;
|
|
6
|
+
this.disabled = options.disabled ?? false;
|
|
7
|
+
this.onChange = options.onChange;
|
|
8
|
+
this.render();
|
|
9
|
+
}
|
|
10
|
+
render() {
|
|
11
|
+
this.element.classList.add("flip-toggle");
|
|
12
|
+
this.element.setAttribute("role", "switch");
|
|
13
|
+
this.element.setAttribute("aria-checked", this.checked);
|
|
14
|
+
this.element.setAttribute("tabindex", "0");
|
|
15
|
+
if (this.disabled) {
|
|
16
|
+
this.element.setAttribute("aria-disabled", "true");
|
|
17
|
+
}
|
|
18
|
+
const thumb = document.createElement("span");
|
|
19
|
+
thumb.classList.add("flip-toggle-thumb");
|
|
20
|
+
this.element.appendChild(thumb);
|
|
21
|
+
this.element.addEventListener("click", () => this.toggle());
|
|
22
|
+
this.element.addEventListener("keydown", (e) => {
|
|
23
|
+
if (e.key === " " || e.key === "Enter") {
|
|
24
|
+
e.preventDefault();
|
|
25
|
+
this.toggle();
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
toggle() {
|
|
30
|
+
var _a;
|
|
31
|
+
if (this.disabled) return;
|
|
32
|
+
this.checked = !this.checked;
|
|
33
|
+
this.element.setAttribute("aria-checked", this.checked);
|
|
34
|
+
(_a = this.onChange) == null ? void 0 : _a.call(this, this.checked);
|
|
35
|
+
}
|
|
36
|
+
setChecked(checked) {
|
|
37
|
+
this.checked = checked;
|
|
38
|
+
this.element.setAttribute("aria-checked", this.checked);
|
|
39
|
+
}
|
|
40
|
+
setDisabled(disabled) {
|
|
41
|
+
this.disabled = disabled;
|
|
42
|
+
if (disabled) {
|
|
43
|
+
this.element.setAttribute("aria-disabled", "true");
|
|
44
|
+
} else {
|
|
45
|
+
this.element.removeAttribute("aria-disabled");
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
export {
|
|
50
|
+
Toggle
|
|
51
|
+
};
|
package/dist/toggle.css
ADDED
|
File without changes
|
package/dist/toggle.js
ADDED
|
File without changes
|
package/package.json
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "flip-toggle",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "./dist/index.cjs",
|
|
6
|
+
"module": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"import": "./dist/index.js",
|
|
11
|
+
"require": "./dist/index.cjs"
|
|
12
|
+
},
|
|
13
|
+
"./styles.css": "./dist/toggle.css"
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist"
|
|
17
|
+
],
|
|
18
|
+
"scripts": {
|
|
19
|
+
"build": "tsup src/index.js --format cjs,esm --minify",
|
|
20
|
+
"dev": "tsup src/index.js --format cjs,esm --dts --watch"
|
|
21
|
+
},
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"tsup": "^8.5.1",
|
|
24
|
+
"typescript": "^5.9.3"
|
|
25
|
+
}
|
|
26
|
+
}
|