zero-query 0.3.1 → 0.5.2

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.
@@ -0,0 +1,253 @@
1
+ /* contacts.css — scoped styles for contacts-page component
2
+ *
3
+ * Loaded via styleUrl — these styles are automatically scoped
4
+ * to the contacts-page component by zQuery.
5
+ */
6
+
7
+ /* -- Toolbar -- */
8
+ .contacts-toolbar-row {
9
+ display: flex;
10
+ gap: 0.75rem;
11
+ align-items: center;
12
+ justify-content: space-between;
13
+ }
14
+
15
+ /* -- Add Form -- */
16
+ .contacts-form {
17
+ display: grid;
18
+ grid-template-columns: 1fr 1fr;
19
+ gap: 0.75rem;
20
+ margin-top: 1rem;
21
+ padding-top: 1rem;
22
+ border-top: 1px solid var(--border);
23
+ }
24
+
25
+ .form-field {
26
+ display: flex;
27
+ flex-direction: column;
28
+ gap: 0.25rem;
29
+ }
30
+
31
+ .form-field-full {
32
+ grid-column: 1 / -1;
33
+ }
34
+
35
+ .form-field .input {
36
+ width: 100%;
37
+ }
38
+
39
+ .field-error {
40
+ color: var(--danger);
41
+ font-size: 0.78rem;
42
+ min-height: 0;
43
+ }
44
+
45
+ .contacts-form .btn {
46
+ grid-column: 1 / -1;
47
+ justify-self: start;
48
+ }
49
+
50
+ /* -- Count -- */
51
+ .contacts-count {
52
+ font-size: 0.85rem;
53
+ color: var(--text-muted);
54
+ }
55
+
56
+ /* -- List -- */
57
+ .contacts-list {
58
+ list-style: none;
59
+ display: flex;
60
+ flex-direction: column;
61
+ gap: 2px;
62
+ }
63
+
64
+ .contacts-item {
65
+ display: flex;
66
+ align-items: center;
67
+ gap: 0.75rem;
68
+ padding: 0.7rem 0.85rem;
69
+ border-radius: var(--radius);
70
+ cursor: pointer;
71
+ transition: all 0.15s ease;
72
+ border: 1px solid transparent;
73
+ }
74
+
75
+ .contacts-item:hover {
76
+ background: var(--bg-hover);
77
+ }
78
+
79
+ .contacts-item.selected {
80
+ background: var(--accent-soft);
81
+ border-color: var(--accent);
82
+ }
83
+
84
+ .contacts-item.is-favorite {
85
+ border-left: 3px solid var(--accent);
86
+ }
87
+
88
+ /* -- Status dot -- */
89
+ .status-dot {
90
+ width: 10px;
91
+ height: 10px;
92
+ border-radius: 50%;
93
+ flex-shrink: 0;
94
+ background: var(--text-muted);
95
+ }
96
+
97
+ .status-online { background: var(--success); }
98
+ .status-away { background: var(--info); }
99
+ .status-offline { background: var(--text-muted); }
100
+
101
+ /* -- Contact info -- */
102
+ .contacts-info {
103
+ flex: 1;
104
+ display: flex;
105
+ flex-direction: column;
106
+ min-width: 0;
107
+ }
108
+
109
+ .contacts-info strong {
110
+ font-size: 0.9rem;
111
+ white-space: nowrap;
112
+ overflow: hidden;
113
+ text-overflow: ellipsis;
114
+ }
115
+
116
+ .contacts-info small {
117
+ font-size: 0.8rem;
118
+ color: var(--text-muted);
119
+ white-space: nowrap;
120
+ overflow: hidden;
121
+ text-overflow: ellipsis;
122
+ }
123
+
124
+ /* -- Role badge -- */
125
+ .role-badge {
126
+ font-size: 0.75rem;
127
+ font-weight: 600;
128
+ padding: 0.2rem 0.55rem;
129
+ border-radius: 99px;
130
+ text-transform: uppercase;
131
+ letter-spacing: 0.03em;
132
+ white-space: nowrap;
133
+ }
134
+
135
+ .role-developer {
136
+ background: rgba(96, 165, 250, 0.15);
137
+ color: #60a5fa;
138
+ }
139
+
140
+ .role-designer {
141
+ background: rgba(168, 85, 247, 0.15);
142
+ color: #a855f7;
143
+ }
144
+
145
+ .role-manager {
146
+ background: rgba(52, 211, 153, 0.15);
147
+ color: #34d399;
148
+ }
149
+
150
+ .role-qa {
151
+ background: rgba(251, 191, 36, 0.15);
152
+ color: #fbbf24;
153
+ }
154
+
155
+ /* -- Favorite button -- */
156
+ .fav-btn {
157
+ background: none;
158
+ border: none;
159
+ font-size: 1.2rem;
160
+ cursor: pointer;
161
+ color: var(--text-muted);
162
+ padding: 0.2rem;
163
+ transition: all 0.15s ease;
164
+ line-height: 1;
165
+ }
166
+
167
+ .fav-btn:hover {
168
+ transform: scale(1.2);
169
+ }
170
+
171
+ .fav-btn.is-fav {
172
+ color: var(--accent);
173
+ }
174
+
175
+ /* -- Detail panel -- */
176
+ .contact-detail {
177
+ border-left: 3px solid var(--accent);
178
+ animation: slide-in 0.2s ease;
179
+ }
180
+
181
+ .detail-header {
182
+ display: flex;
183
+ justify-content: space-between;
184
+ align-items: flex-start;
185
+ gap: 1rem;
186
+ flex-wrap: wrap;
187
+ }
188
+
189
+ .detail-header h3 {
190
+ font-size: 1.1rem;
191
+ margin-bottom: 0.15rem;
192
+ }
193
+
194
+ .detail-header .muted {
195
+ font-size: 0.85rem;
196
+ color: var(--text-muted);
197
+ }
198
+
199
+ .detail-actions {
200
+ display: flex;
201
+ gap: 0.5rem;
202
+ align-items: center;
203
+ flex-wrap: wrap;
204
+ }
205
+
206
+ /* -- Confirm group -- */
207
+ .confirm-group {
208
+ display: inline-flex;
209
+ align-items: center;
210
+ gap: 0.35rem;
211
+ }
212
+
213
+ .confirm-text {
214
+ font-size: 0.82rem;
215
+ color: var(--danger);
216
+ font-weight: 500;
217
+ }
218
+
219
+ @keyframes slide-in {
220
+ from { opacity: 0; transform: translateY(-6px); }
221
+ to { opacity: 1; transform: translateY(0); }
222
+ }
223
+ }
224
+
225
+ /* -- Animation -- */
226
+ @keyframes slide-in {
227
+ from { opacity: 0; transform: translateY(-6px); }
228
+ to { opacity: 1; transform: translateY(0); }
229
+ }
230
+
231
+ /* -- Responsive -- */
232
+ @media (max-width: 768px) {
233
+ .contacts-toolbar-row {
234
+ flex-direction: column;
235
+ }
236
+
237
+ .contacts-form {
238
+ grid-template-columns: 1fr;
239
+ }
240
+
241
+ .contacts-item {
242
+ flex-wrap: wrap;
243
+ }
244
+
245
+ .role-badge {
246
+ order: 10;
247
+ margin-left: calc(10px + 0.75rem);
248
+ }
249
+
250
+ .detail-header {
251
+ flex-direction: column;
252
+ }
253
+ }
@@ -0,0 +1,139 @@
1
+ <!--
2
+ contacts.html — external template for contacts-page component
3
+
4
+ This template is fetched via templateUrl and uses {{expression}} syntax
5
+ for data binding. All zQuery directives work here: z-if, z-else,
6
+ z-for, z-show, z-bind, z-class, z-style, z-text, z-model, z-ref,
7
+ z-cloak, @click, @submit.prevent, etc.
8
+
9
+ Expressions have access to `state` and `props` automatically.
10
+ Inside z-for loops, use {{item.prop}} for per-item values.
11
+ -->
12
+ <div class="page-header" z-cloak>
13
+ <h1>Contacts</h1>
14
+ <p class="subtitle">
15
+ External template &amp; styles via <code>templateUrl</code> / <code>styleUrl</code>.
16
+ Directives: <code>z-if</code>, <code>z-for</code>, <code>z-show</code>,
17
+ <code>z-bind</code>, <code>z-class</code>, <code>z-model</code>, and more.
18
+ </p>
19
+ </div>
20
+
21
+ <!-- Toolbar: add button -->
22
+ <div class="card contacts-toolbar">
23
+ <div class="contacts-toolbar-row">
24
+ <span class="contacts-count" z-if="contacts.length > 0">
25
+ <strong z-text="contacts.length"></strong> contacts
26
+ · <strong z-text="favoriteCount"></strong> ★ favorited
27
+ <span z-show="totalAdded > 0"> · <span z-text="totalAdded"></span> added this session</span>
28
+ </span>
29
+ <span class="contacts-count" z-else>No contacts yet</span>
30
+ <button class="btn btn-primary" @click="toggleForm">
31
+ <span z-if="showForm">✕ Cancel</span>
32
+ <span z-else>+ Add Contact</span>
33
+ </button>
34
+ </div>
35
+
36
+ <!-- Add contact form — toggled via z-show -->
37
+ <form class="contacts-form" z-show="showForm" @submit.prevent="addContact">
38
+ <div class="form-field form-field-full">
39
+ <input
40
+ type="text"
41
+ z-model="newName"
42
+ z-trim
43
+ placeholder="Full name"
44
+ class="input"
45
+ @blur="validateName"
46
+ />
47
+ <small class="field-error" z-show="nameError" z-text="nameError"></small>
48
+ </div>
49
+ <div class="form-field">
50
+ <input
51
+ type="email"
52
+ z-model="newEmail"
53
+ z-trim
54
+ placeholder="Email address"
55
+ class="input"
56
+ @blur="validateEmail"
57
+ />
58
+ <small class="field-error" z-show="emailError" z-text="emailError"></small>
59
+ </div>
60
+ <select z-model="newRole" class="input">
61
+ <option value="Developer">Developer</option>
62
+ <option value="Designer">Designer</option>
63
+ <option value="Manager">Manager</option>
64
+ <option value="QA">QA</option>
65
+ </select>
66
+ <button type="submit" class="btn btn-primary">Save Contact</button>
67
+ </form>
68
+ </div>
69
+
70
+ <!-- Contact count -->
71
+ <div class="card" z-if="contacts.length > 0">
72
+ <!-- Contacts list — z-for renders each item -->
73
+ <ul class="contacts-list">
74
+ <li
75
+ z-for="contact in contacts"
76
+ class="contacts-item {{contact.id === selectedId ? 'selected' : ''}} {{contact.favorite ? 'is-favorite' : ''}}"
77
+ @click="selectContact({{contact.id}})"
78
+ >
79
+ <!-- Status indicator — class set per status -->
80
+ <span
81
+ class="status-dot status-{{contact.status}}"
82
+ title="{{contact.status}}"
83
+ ></span>
84
+
85
+ <!-- Contact info -->
86
+ <div class="contacts-info">
87
+ <strong>{{contact.name}}</strong>
88
+ <small>{{contact.email}}</small>
89
+ </div>
90
+
91
+ <!-- Role badge -->
92
+ <span class="role-badge role-{{contact.role.toLowerCase()}}">{{contact.role}}</span>
93
+
94
+ <!-- Favorite toggle — .stop modifier prevents row click -->
95
+ <button
96
+ class="fav-btn {{contact.favorite ? 'is-fav' : ''}}"
97
+ @click.stop="toggleFavorite({{contact.id}})"
98
+ >{{contact.favorite ? '★' : '☆'}}</button>
99
+ </li>
100
+ </ul>
101
+ </div>
102
+
103
+ <!-- Empty state -->
104
+ <div class="card" z-else>
105
+ <div class="empty-state">
106
+ <p>No contacts yet — add one above!</p>
107
+ </div>
108
+ </div>
109
+
110
+ <!-- Selected contact detail panel — z-if conditional rendering -->
111
+ <div class="card contact-detail" z-if="selectedId !== null">
112
+ <div class="detail-header">
113
+ <div>
114
+ <h3 z-text="contacts.find(c => c.id === selectedId)?.name || ''"></h3>
115
+ <p class="muted" z-text="contacts.find(c => c.id === selectedId)?.email || ''"></p>
116
+ </div>
117
+ <div class="detail-actions">
118
+ <button
119
+ class="btn btn-outline btn-sm"
120
+ @click="cycleStatus({{selectedId}})"
121
+ >
122
+ Status: <span z-text="contacts.find(c => c.id === selectedId)?.status || ''"></span>
123
+ </button>
124
+
125
+ <!-- Confirm delete pattern using z-if / z-else -->
126
+ <button
127
+ class="btn btn-danger btn-sm"
128
+ z-if="confirmDeleteId !== selectedId"
129
+ @click.stop="confirmDelete({{selectedId}})"
130
+ >Delete</button>
131
+
132
+ <span z-else class="confirm-group">
133
+ <span class="confirm-text">Sure?</span>
134
+ <button class="btn btn-danger btn-sm" @click.stop="deleteContact({{selectedId}})">Yes</button>
135
+ <button class="btn btn-ghost btn-sm" @click.stop="cancelDelete">No</button>
136
+ </span>
137
+ </div>
138
+ </div>
139
+ </div>
@@ -0,0 +1,137 @@
1
+ // scripts/components/contacts/contacts.js — contact book
2
+ //
3
+ // Demonstrates: external templateUrl + styleUrl, z-if/z-else, z-for,
4
+ // z-show, z-bind/:attr, z-class, z-style, z-text, z-html,
5
+ // z-model, z-ref, z-cloak, @click, @submit.prevent,
6
+ // @input.debounce, event modifiers, and template {{expressions}}
7
+ //
8
+ // This component uses external files for its template and styles,
9
+ // resolved automatically relative to this JS file's location.
10
+ // Contacts are persisted in the global $.store('main') so they
11
+ // survive navigation between routes.
12
+
13
+ $.component('contacts-page', {
14
+ templateUrl: 'contacts.html',
15
+ styleUrl: 'contacts.css',
16
+
17
+ state: () => ({
18
+ contacts: [],
19
+ showForm: false,
20
+ newName: '',
21
+ newEmail: '',
22
+ newRole: 'Developer',
23
+ nameError: '',
24
+ emailError: '',
25
+ selectedId: null,
26
+ confirmDeleteId: null,
27
+ totalAdded: 0,
28
+ favoriteCount: 0,
29
+ }),
30
+
31
+ mounted() {
32
+ const store = $.getStore('main');
33
+ this._syncFromStore(store);
34
+ this._unsub = store.subscribe(() => this._syncFromStore(store));
35
+ },
36
+
37
+ destroyed() {
38
+ if (this._unsub) this._unsub();
39
+ },
40
+
41
+ _syncFromStore(store) {
42
+ this.state.contacts = store.state.contacts;
43
+ this.state.totalAdded = store.state.contactsAdded;
44
+ this.state.favoriteCount = store.getters.favoriteCount;
45
+ },
46
+
47
+ // -- Actions --
48
+
49
+ toggleForm() {
50
+ this.state.showForm = !this.state.showForm;
51
+ if (!this.state.showForm) this._clearForm();
52
+ },
53
+
54
+ _validateName(name) {
55
+ if (!name) return 'Name is required.';
56
+ if (name.length < 2) return 'Name must be at least 2 characters.';
57
+ return '';
58
+ },
59
+
60
+ _validateEmail(email) {
61
+ if (!email) return 'Email is required.';
62
+ if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)) return 'Enter a valid email address.';
63
+ const store = $.getStore('main');
64
+ if (store.state.contacts.some(c => c.email.toLowerCase() === email.toLowerCase())) {
65
+ return 'A contact with this email already exists.';
66
+ }
67
+ return '';
68
+ },
69
+
70
+ validateName() {
71
+ this.state.nameError = this._validateName(this.state.newName.trim());
72
+ },
73
+
74
+ validateEmail() {
75
+ this.state.emailError = this._validateEmail(this.state.newEmail.trim());
76
+ },
77
+
78
+ addContact() {
79
+ const name = this.state.newName.trim();
80
+ const email = this.state.newEmail.trim();
81
+
82
+ const nameError = this._validateName(name);
83
+ const emailError = this._validateEmail(email);
84
+ this.state.nameError = nameError;
85
+ this.state.emailError = emailError;
86
+ if (nameError || emailError) return;
87
+
88
+ $.getStore('main').dispatch('addContact', {
89
+ name,
90
+ email,
91
+ role: this.state.newRole,
92
+ });
93
+
94
+ this._clearForm();
95
+ this.state.showForm = false;
96
+ $.bus.emit('toast', { message: `${name} added!`, type: 'success' });
97
+ },
98
+
99
+ toggleFavorite(id) {
100
+ $.getStore('main').dispatch('toggleFavorite', Number(id));
101
+ },
102
+
103
+ selectContact(id) {
104
+ this.state.selectedId = this.state.selectedId === Number(id) ? null : Number(id);
105
+ this.state.confirmDeleteId = null;
106
+ },
107
+
108
+ confirmDelete(id) {
109
+ this.state.confirmDeleteId = Number(id);
110
+ },
111
+
112
+ cancelDelete() {
113
+ this.state.confirmDeleteId = null;
114
+ },
115
+
116
+ deleteContact(id) {
117
+ const numId = Number(id);
118
+ const store = $.getStore('main');
119
+ const c = store.state.contacts.find(c => c.id === numId);
120
+ store.dispatch('deleteContact', numId);
121
+ this.state.selectedId = null;
122
+ this.state.confirmDeleteId = null;
123
+ $.bus.emit('toast', { message: `${c ? c.name : 'Contact'} removed`, type: 'error' });
124
+ },
125
+
126
+ cycleStatus(id) {
127
+ $.getStore('main').dispatch('cycleContactStatus', Number(id));
128
+ },
129
+
130
+ _clearForm() {
131
+ this.state.newName = '';
132
+ this.state.newEmail = '';
133
+ this.state.newRole = 'Developer';
134
+ this.state.nameError = '';
135
+ this.state.emailError = '';
136
+ },
137
+ });
@@ -0,0 +1,65 @@
1
+ // scripts/components/counter.js — interactive counter
2
+ //
3
+ // Demonstrates: component state, instance methods, @click event binding,
4
+ // z-model two-way binding with z-number modifier, z-class,
5
+ // z-if, z-for, $.bus toast notifications
6
+
7
+ $.component('counter-page', {
8
+ state: () => ({
9
+ count: 0,
10
+ step: 1,
11
+ history: [],
12
+ }),
13
+
14
+ increment() {
15
+ this.state.count += this.state.step;
16
+ this.state.history.push({ action: '+', value: this.state.step, result: this.state.count });
17
+ if (this.state.history.length > 8) this.state.history.shift();
18
+ },
19
+
20
+ decrement() {
21
+ this.state.count -= this.state.step;
22
+ this.state.history.push({ action: '−', value: this.state.step, result: this.state.count });
23
+ if (this.state.history.length > 8) this.state.history.shift();
24
+ },
25
+
26
+ reset() {
27
+ this.state.count = 0;
28
+ this.state.history = [];
29
+ $.bus.emit('toast', { message: 'Counter reset!', type: 'info' });
30
+ },
31
+
32
+ render() {
33
+ return `
34
+ <div class="page-header">
35
+ <h1>Counter</h1>
36
+ <p class="subtitle">Component state, <code>@click</code> handlers, <code>z-model</code>, <code>z-class</code>, and <code>z-for</code>.</p>
37
+ </div>
38
+
39
+ <div class="card counter-card">
40
+ <div class="counter-display">
41
+ <span class="counter-value" z-class="{'negative': count < 0}">${this.state.count}</span>
42
+ </div>
43
+
44
+ <div class="counter-controls">
45
+ <button class="btn btn-danger" @click="decrement">− Subtract</button>
46
+ <button class="btn btn-primary" @click="increment">+ Add</button>
47
+ </div>
48
+
49
+ <div class="counter-step">
50
+ <label>Step size:
51
+ <input type="number" z-model="step" z-number min="1" max="100" class="input input-sm" />
52
+ </label>
53
+ <button class="btn btn-ghost btn-sm" @click="reset">Reset</button>
54
+ </div>
55
+ </div>
56
+
57
+ <div class="card card-muted" z-if="history.length > 0">
58
+ <h3>History</h3>
59
+ <div class="history-list">
60
+ <span z-for="e in history" class="history-item">{{e.action}}{{e.value}} → <strong>{{e.result}}</strong></span>
61
+ </div>
62
+ </div>
63
+ `;
64
+ }
65
+ });