@itrocks/modal 0.0.11 → 0.1.0

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.
Files changed (3) hide show
  1. package/README.md +171 -0
  2. package/modal.js +1 -1
  3. package/package.json +8 -3
package/README.md CHANGED
@@ -7,3 +7,174 @@
7
7
  # modal
8
8
 
9
9
  Lightweight modal with default styling and auto-close on form submit or link click.
10
+
11
+ *This documentation was written by an artificial intelligence and may contain errors or approximations.
12
+ It has not yet been fully reviewed by a human. If anything seems unclear or incomplete,
13
+ please feel free to contact the author of this package.*
14
+
15
+ ## Installation
16
+
17
+ Install the package from npm:
18
+
19
+ ```bash
20
+ npm i @itrocks/modal
21
+ ```
22
+
23
+ Then include the stylesheet in your page. With a bundler:
24
+
25
+ ```ts
26
+ import '@itrocks/modal/modal.css'
27
+ ```
28
+
29
+ Or by linking to the built CSS file directly in HTML:
30
+
31
+ ```html
32
+ <link rel="stylesheet" href="/node_modules/@itrocks/modal/modal.css">
33
+ ```
34
+
35
+ ## Usage
36
+
37
+ `@itrocks/modal` provides default styles for a full-screen overlay container
38
+ with the id `modal`, plus a small helper function `modalForm` that
39
+ automatically closes the modal when:
40
+
41
+ - the contained form is successfully submitted, or
42
+ - a link with `href="about:blank"` is clicked.
43
+
44
+ The package is intentionally minimal: you are free to render any content
45
+ inside the modal and to decide when it should appear.
46
+
47
+ ### Minimal example
48
+
49
+ ```ts
50
+ import { modalForm } from '@itrocks/modal'
51
+
52
+ // Suppose you have just injected a `<form>` into `#modal` dynamically
53
+ const form = document.querySelector<HTMLFormElement>('#modal form')
54
+ if (form) {
55
+ modalForm(form)
56
+ }
57
+ ```
58
+
59
+ ```html
60
+ <div id="modal">
61
+ <form>
62
+ <p>Confirm action?</p>
63
+
64
+ <button type="submit">OK</button>
65
+ <a href="about:blank">Cancel</a>
66
+ </form>
67
+ </div>
68
+ ```
69
+
70
+ In this setup:
71
+
72
+ - When the user submits the form (by clicking **OK**), the surrounding
73
+ `#modal` element is removed from the DOM, effectively closing the modal.
74
+ - When the user clicks the **Cancel** link (whose `href` is `about:blank`),
75
+ the closest `#modal` ancestor is removed as well.
76
+
77
+ ### Complete example with lazy-loaded modal
78
+
79
+ The following example shows a small confirmation modal that is created on
80
+ the fly when the user clicks a button. The modal is then automatically
81
+ closed when the form is submitted or the cancel link is clicked.
82
+
83
+ ```ts
84
+ import { modalForm } from '@itrocks/modal'
85
+
86
+ function openConfirmModal(message: string, onConfirm: () => void) {
87
+ // Create the modal container if it does not exist yet
88
+ let modal = document.querySelector<HTMLDivElement>('#modal')
89
+ if (!modal) {
90
+ modal = document.createElement('div')
91
+ modal.id = 'modal'
92
+ document.body.appendChild(modal)
93
+ }
94
+
95
+ // Inject the confirmation form
96
+ modal.innerHTML = `
97
+ <form>
98
+ <p>${message}</p>
99
+ <button type="submit">Confirm</button>
100
+ <a href="about:blank">Cancel</a>
101
+ </form>
102
+ `
103
+
104
+ const form = modal.querySelector('form') as HTMLFormElement
105
+
106
+ // Wire the helper to auto-close the modal
107
+ modalForm(form)
108
+
109
+ // Perform custom logic on submit (e.g. call an API)
110
+ form.addEventListener('submit', (event) => {
111
+ event.preventDefault() // keep page from reloading
112
+ onConfirm()
113
+ // The modal element is removed by modalForm's submit listener
114
+ })
115
+ }
116
+
117
+ // Example: attach to a button
118
+ document
119
+ .querySelector<HTMLButtonElement>('#delete-button')
120
+ ?.addEventListener('click', () => {
121
+ openConfirmModal('Are you sure you want to delete this item?', () => {
122
+ // Custom confirm callback
123
+ console.log('Item deleted')
124
+ })
125
+ })
126
+ ```
127
+
128
+ With the stylesheet from this package, the `#modal` overlay covers the
129
+ whole viewport and slightly darkens the background while displaying your
130
+ content in a centered box.
131
+
132
+ ## API
133
+
134
+ ### `function modalForm(form: HTMLFormElement): void`
135
+
136
+ Attach auto-close behavior to a form displayed inside a modal overlay.
137
+
138
+ The function looks for the closest ancestor element with the id `modal`
139
+ and, if found, registers two behaviors:
140
+
141
+ 1. On form submission, the `#modal` element is removed from the DOM.
142
+ 2. For each anchor inside the form whose `href` is exactly
143
+ `about:blank`, a click handler is added that removes the closest
144
+ `#modal` ancestor.
145
+
146
+ If the form is not inside a `#modal` container, the function does
147
+ nothing.
148
+
149
+ #### Parameters
150
+
151
+ - `form` – the `HTMLFormElement` to enhance. It is expected to be a
152
+ direct or indirect child of the `#modal` element.
153
+
154
+ #### Return value
155
+
156
+ - `void` – the function does not return anything. It registers the
157
+ relevant event listeners on the provided form.
158
+
159
+ #### Example
160
+
161
+ ```ts
162
+ import { modalForm } from '@itrocks/modal'
163
+
164
+ const form = document.querySelector<HTMLFormElement>('#modal form')
165
+ if (form) {
166
+ modalForm(form)
167
+ }
168
+ ```
169
+
170
+ ## Typical use cases
171
+
172
+ - Simple confirmation dialogs ("Are you sure?" before deleting an item)
173
+ that should disappear as soon as the user confirms or cancels.
174
+ - Small input forms (e.g. rename, quick comment, password prompt)
175
+ displayed in a centered overlay without having to write modal logic
176
+ from scratch.
177
+ - Temporary status or error popups that can be dismissed with a
178
+ dedicated "Close" link using `href="about:blank"`.
179
+ - Projects that want a lightweight, dependency-free modal behavior and
180
+ default styling that can be easily overridden with custom CSS.
package/modal.js CHANGED
@@ -2,7 +2,7 @@ export function modalForm(form) {
2
2
  const modal = form.closest('#modal');
3
3
  if (!modal)
4
4
  return;
5
- form.addEventListener('submit', () => modal.remove());
5
+ form.addEventListener('submit', () => setTimeout(() => modal.remove()));
6
6
  form.querySelectorAll('a[href="about:blank"]').forEach(anchor => {
7
7
  anchor.addEventListener('click', () => { anchor.closest('#modal')?.remove(); });
8
8
  });
package/package.json CHANGED
@@ -22,12 +22,17 @@
22
22
  "auto-close",
23
23
  "automatically",
24
24
  "close",
25
+ "confirm",
26
+ "confirmation",
27
+ "confirmation-dialog",
25
28
  "css",
26
29
  "design",
27
30
  "dialog",
28
- "front-end",
31
+ "frontend",
29
32
  "it.rocks",
33
+ "lightweight",
30
34
  "modal",
35
+ "overlay",
31
36
  "popup",
32
37
  "responsive",
33
38
  "stylesheet",
@@ -48,6 +53,6 @@
48
53
  "build": "tsc && sass --no-source-map modal.scss:modal.css"
49
54
  },
50
55
  "type": "module",
51
- "types": "./modal.d.ts",
52
- "version": "0.0.11"
56
+ "types": "./modal.d.ts",
57
+ "version": "0.1.0"
53
58
  }