@y14e/parent-checkbox 1.0.7

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Yusuke Kamiyamane
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,44 @@
1
+ # Parent Checkbox
2
+
3
+ WAI-ARIA compliant [checkbox](https://www.w3.org/WAI/ARIA/apg/patterns/checkbox/) (mixed) pattern implementation in TypeScript.
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ npm i @y14e/parent-checkbox
9
+ ```
10
+
11
+ ```ts
12
+ // npm
13
+ import ParentCheckbox from '@y14e/parent-checkbox';
14
+
15
+ // CDNs
16
+ import ParentCheckbox from 'https://esm.sh/@y14e/parent-checkbox'
17
+ // or
18
+ import ParentCheckbox from 'https://cdn.jsdelivr.net/npm/@y14e/parent-checkbox/+esm';
19
+ // or
20
+ import ParentCheckbox from 'https://unpkg.com/@y14e/parent-checkbox/dist/index.js';
21
+ ```
22
+
23
+ ## Usage
24
+
25
+ ```ts
26
+ new ParentCheckbox(root);
27
+ // => ParentCheckbox
28
+ //
29
+ // root: HTMLInputElement
30
+ ```
31
+
32
+ ## 📦 APIs
33
+
34
+ ### `destroy`
35
+
36
+ Destroys the instance and cleans up all event listeners.
37
+
38
+ ```ts
39
+ checkbox.destroy();
40
+ // => void
41
+ ```
42
+ ## Demo
43
+
44
+ https://y14e.github.io/parent-checkbox/
package/dist/index.cjs ADDED
@@ -0,0 +1,73 @@
1
+ 'use strict';
2
+
3
+ // src/index.ts
4
+ var ParentCheckbox = class {
5
+ #rootElement;
6
+ #childElements;
7
+ #controller = null;
8
+ #isDestroyed = false;
9
+ constructor(root) {
10
+ if (!(root instanceof HTMLInputElement)) {
11
+ throw new TypeError("Invalid root element");
12
+ }
13
+ if (root.hasAttribute("data-parent-checkbox-initialized")) {
14
+ console.warn("Already initialized");
15
+ return;
16
+ }
17
+ this.#rootElement = root;
18
+ const ids = root.getAttribute("aria-controls")?.trim() ?? "";
19
+ !ids && console.warn("Invalid aria-controls attribute");
20
+ this.#childElements = ids.split(/\s+/).map((id) => document.getElementById(id)).filter((element) => element instanceof HTMLInputElement);
21
+ !this.#childElements.length && console.warn("Missing child elements");
22
+ this.#initialize();
23
+ }
24
+ destroy() {
25
+ if (this.#isDestroyed) {
26
+ return;
27
+ }
28
+ this.#isDestroyed = true;
29
+ this.#controller?.abort();
30
+ this.#controller = null;
31
+ this.#childElements.length = 0;
32
+ this.#rootElement.removeAttribute("data-parent-checkbox-initialized");
33
+ }
34
+ #initialize() {
35
+ this.#controller = new AbortController();
36
+ const { signal } = this.#controller;
37
+ this.#rootElement.addEventListener("change", this.#onRootChange, {
38
+ signal
39
+ });
40
+ this.#childElements.forEach((child) => {
41
+ child.addEventListener("change", this.#onChildChange, { signal });
42
+ });
43
+ this.#update();
44
+ this.#rootElement.setAttribute("data-parent-checkbox-initialized", "");
45
+ }
46
+ #update() {
47
+ const isAllChecked = this.#childElements.every((child) => child.checked);
48
+ this.#rootElement.checked = isAllChecked;
49
+ this.#rootElement.indeterminate = !isAllChecked && this.#childElements.some((child) => child.checked);
50
+ }
51
+ #onRootChange = () => {
52
+ this.#rootElement.indeterminate = false;
53
+ const isChecked = this.#rootElement.checked;
54
+ this.#childElements.forEach((child) => {
55
+ child.checked = isChecked;
56
+ });
57
+ };
58
+ #onChildChange = () => {
59
+ this.#update();
60
+ };
61
+ };
62
+ /**
63
+ * Parent Checkbox
64
+ * WAI-ARIA compliant checkbox (mixed) pattern implementation in TypeScript.
65
+ *
66
+ * @version 1.0.7
67
+ * @author Yusuke Kamiyamane
68
+ * @license MIT
69
+ * @copyright Copyright (c) Yusuke Kamiyamane
70
+ * @see {@link https://github.com/y14e/parent-checkbox}
71
+ */
72
+
73
+ module.exports = ParentCheckbox;
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Parent Checkbox
3
+ * WAI-ARIA compliant checkbox (mixed) pattern implementation in TypeScript.
4
+ *
5
+ * @version 1.0.7
6
+ * @author Yusuke Kamiyamane
7
+ * @license MIT
8
+ * @copyright Copyright (c) Yusuke Kamiyamane
9
+ * @see {@link https://github.com/y14e/parent-checkbox}
10
+ */
11
+ declare class ParentCheckbox {
12
+ #private;
13
+ constructor(root: HTMLInputElement);
14
+ destroy(): void;
15
+ }
16
+
17
+ export { ParentCheckbox as default };
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Parent Checkbox
3
+ * WAI-ARIA compliant checkbox (mixed) pattern implementation in TypeScript.
4
+ *
5
+ * @version 1.0.7
6
+ * @author Yusuke Kamiyamane
7
+ * @license MIT
8
+ * @copyright Copyright (c) Yusuke Kamiyamane
9
+ * @see {@link https://github.com/y14e/parent-checkbox}
10
+ */
11
+ declare class ParentCheckbox {
12
+ #private;
13
+ constructor(root: HTMLInputElement);
14
+ destroy(): void;
15
+ }
16
+
17
+ export { ParentCheckbox as default };
package/dist/index.js ADDED
@@ -0,0 +1,71 @@
1
+ // src/index.ts
2
+ var ParentCheckbox = class {
3
+ #rootElement;
4
+ #childElements;
5
+ #controller = null;
6
+ #isDestroyed = false;
7
+ constructor(root) {
8
+ if (!(root instanceof HTMLInputElement)) {
9
+ throw new TypeError("Invalid root element");
10
+ }
11
+ if (root.hasAttribute("data-parent-checkbox-initialized")) {
12
+ console.warn("Already initialized");
13
+ return;
14
+ }
15
+ this.#rootElement = root;
16
+ const ids = root.getAttribute("aria-controls")?.trim() ?? "";
17
+ !ids && console.warn("Invalid aria-controls attribute");
18
+ this.#childElements = ids.split(/\s+/).map((id) => document.getElementById(id)).filter((element) => element instanceof HTMLInputElement);
19
+ !this.#childElements.length && console.warn("Missing child elements");
20
+ this.#initialize();
21
+ }
22
+ destroy() {
23
+ if (this.#isDestroyed) {
24
+ return;
25
+ }
26
+ this.#isDestroyed = true;
27
+ this.#controller?.abort();
28
+ this.#controller = null;
29
+ this.#childElements.length = 0;
30
+ this.#rootElement.removeAttribute("data-parent-checkbox-initialized");
31
+ }
32
+ #initialize() {
33
+ this.#controller = new AbortController();
34
+ const { signal } = this.#controller;
35
+ this.#rootElement.addEventListener("change", this.#onRootChange, {
36
+ signal
37
+ });
38
+ this.#childElements.forEach((child) => {
39
+ child.addEventListener("change", this.#onChildChange, { signal });
40
+ });
41
+ this.#update();
42
+ this.#rootElement.setAttribute("data-parent-checkbox-initialized", "");
43
+ }
44
+ #update() {
45
+ const isAllChecked = this.#childElements.every((child) => child.checked);
46
+ this.#rootElement.checked = isAllChecked;
47
+ this.#rootElement.indeterminate = !isAllChecked && this.#childElements.some((child) => child.checked);
48
+ }
49
+ #onRootChange = () => {
50
+ this.#rootElement.indeterminate = false;
51
+ const isChecked = this.#rootElement.checked;
52
+ this.#childElements.forEach((child) => {
53
+ child.checked = isChecked;
54
+ });
55
+ };
56
+ #onChildChange = () => {
57
+ this.#update();
58
+ };
59
+ };
60
+ /**
61
+ * Parent Checkbox
62
+ * WAI-ARIA compliant checkbox (mixed) pattern implementation in TypeScript.
63
+ *
64
+ * @version 1.0.7
65
+ * @author Yusuke Kamiyamane
66
+ * @license MIT
67
+ * @copyright Copyright (c) Yusuke Kamiyamane
68
+ * @see {@link https://github.com/y14e/parent-checkbox}
69
+ */
70
+
71
+ export { ParentCheckbox as default };
package/package.json ADDED
@@ -0,0 +1,53 @@
1
+ {
2
+ "name": "@y14e/parent-checkbox",
3
+ "version": "1.0.7",
4
+ "description": "WAI-ARIA compliant checkbox (mixed) pattern implementation in TypeScript",
5
+ "type": "module",
6
+ "main": "./dist/index.cjs",
7
+ "module": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "import": "./dist/index.js",
13
+ "require": "./dist/index.cjs"
14
+ }
15
+ },
16
+ "files": [
17
+ "dist",
18
+ "LICENSE",
19
+ "README.md"
20
+ ],
21
+ "scripts": {
22
+ "build": "tsup",
23
+ "prepublishOnly": "npm run build",
24
+ "lint": "tsc --noEmit"
25
+ },
26
+ "keywords": [
27
+ "a11y",
28
+ "accessibility",
29
+ "checkbox",
30
+ "checkboxes",
31
+ "component",
32
+ "typescript",
33
+ "utility"
34
+ ],
35
+ "author": "Yusuke Kamiyamane",
36
+ "license": "MIT",
37
+ "repository": {
38
+ "type": "git",
39
+ "url": "git+https://github.com/y14e/parent-checkbox.git"
40
+ },
41
+ "bugs": {
42
+ "url": "https://github.com/y14e/parent-checkbox/issues"
43
+ },
44
+ "homepage": "https://github.com/y14e/parent-checkbox#readme",
45
+ "devDependencies": {
46
+ "bun-types": "latest",
47
+ "tsup": "^8.0.0",
48
+ "typescript": "^5.6.0"
49
+ },
50
+ "engines": {
51
+ "node": ">=18"
52
+ }
53
+ }