Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
268 lines (224 loc) · 9.84 KB
/
Copy pathscript.js
File metadata and controls
268 lines (224 loc) · 9.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/**
* Portfolio — interações da página.
* Sem dependências: cada bloco sai cedo se o elemento não existir.
*/
(() => {
'use strict';
const $ = (selector, root = document) => root.querySelector(selector);
const $$ = (selector, root = document) => Array.from(root.querySelectorAll(selector));
const reduceMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
const supportsObserver = 'IntersectionObserver' in window;
/* ----------------------------------------------------------------------
* Navegação mobile
* -------------------------------------------------------------------- */
const navbar = $('.navbar');
const navToggle = $('.nav-toggle');
const nav = $('#primary-nav');
if (navToggle && nav) {
const setNavState = (isOpen) => {
nav.classList.toggle('is-open', isOpen);
navToggle.setAttribute('aria-expanded', String(isOpen));
navToggle.setAttribute(
'aria-label',
isOpen ? 'Fechar menu de navegação' : 'Abrir menu de navegação',
);
};
navToggle.addEventListener('click', () => {
setNavState(!nav.classList.contains('is-open'));
});
// Fecha ao escolher um destino
nav.addEventListener('click', (event) => {
if (event.target.closest('a')) {
setNavState(false);
}
});
// Fecha ao clicar fora do menu
document.addEventListener('click', (event) => {
if (!nav.classList.contains('is-open')) return;
if (nav.contains(event.target) || navToggle.contains(event.target)) return;
setNavState(false);
});
// Fecha com Escape e devolve o foco ao botão
document.addEventListener('keydown', (event) => {
if (event.key !== 'Escape' || !nav.classList.contains('is-open')) return;
setNavState(false);
navToggle.focus();
});
// Ao entrar no layout desktop o painel mobile deixa de existir
const desktopQuery = window.matchMedia('(min-width: 768px)');
desktopQuery.addEventListener('change', (event) => {
if (event.matches) setNavState(false);
});
}
/* ----------------------------------------------------------------------
* Navbar compacta ao rolar (um único listener, limitado por rAF)
* -------------------------------------------------------------------- */
if (navbar) {
let ticking = false;
const updateNavbar = () => {
navbar.classList.toggle('is-scrolled', window.scrollY > 50);
ticking = false;
};
window.addEventListener(
'scroll',
() => {
if (ticking) return;
ticking = true;
window.requestAnimationFrame(updateNavbar);
},
{ passive: true },
);
updateNavbar();
}
/* ----------------------------------------------------------------------
* Link ativo conforme a seção visível
* -------------------------------------------------------------------- */
const sections = $$('main section[id]');
const navLinks = $$('.nav-links a');
if (supportsObserver && sections.length && navLinks.length) {
const setActiveLink = (id) => {
navLinks.forEach((link) => {
const isActive = link.getAttribute('href') === `#${id}`;
if (isActive) {
link.setAttribute('aria-current', 'page');
} else {
link.removeAttribute('aria-current');
}
});
};
const spy = new IntersectionObserver(
(entries) => {
entries.forEach((entry) => {
if (entry.isIntersecting) setActiveLink(entry.target.id);
});
},
{ rootMargin: '-45% 0px -50% 0px', threshold: 0 },
);
sections.forEach((section) => spy.observe(section));
}
/* ----------------------------------------------------------------------
* Revelar elementos ao entrar na viewport
* -------------------------------------------------------------------- */
const revealElements = $$('.reveal');
if (revealElements.length) {
if (reduceMotion || !supportsObserver) {
revealElements.forEach((element) => element.classList.add('is-visible'));
} else {
const revealObserver = new IntersectionObserver(
(entries, observer) => {
entries.forEach((entry) => {
if (!entry.isIntersecting) return;
entry.target.classList.add('is-visible');
observer.unobserve(entry.target);
});
},
{ rootMargin: '0px 0px -40px 0px', threshold: 0.15 },
);
revealElements.forEach((element) => revealObserver.observe(element));
}
}
/* ----------------------------------------------------------------------
* Barras de skill (a largura vem de --level, definido no HTML)
* -------------------------------------------------------------------- */
const skillCards = $$('.skill-card');
if (skillCards.length) {
if (reduceMotion || !supportsObserver) {
skillCards.forEach((card) => card.classList.add('is-filled'));
} else {
const skillObserver = new IntersectionObserver(
(entries, observer) => {
entries.forEach((entry) => {
if (!entry.isIntersecting) return;
entry.target.classList.add('is-filled');
observer.unobserve(entry.target);
});
},
{ threshold: 0.4 },
);
skillCards.forEach((card) => skillObserver.observe(card));
}
}
/* ----------------------------------------------------------------------
* Efeito de digitação no subtítulo do hero
* O texto completo permanece no DOM via .sr-only, então leitores de tela
* não são afetados pela animação.
* -------------------------------------------------------------------- */
const roleText = $('.hero-role-text');
if (roleText && !reduceMotion) {
const fullText = roleText.textContent.trim();
let index = 0;
roleText.textContent = '';
roleText.classList.add('is-typing');
const type = () => {
index += 1;
roleText.textContent = fullText.slice(0, index);
if (index < fullText.length) {
window.setTimeout(type, 55);
} else {
window.setTimeout(() => roleText.classList.remove('is-typing'), 1200);
}
};
window.setTimeout(type, 900);
}
/* ----------------------------------------------------------------------
* Formulário de contato (abre o cliente de e-mail, sem backend)
* -------------------------------------------------------------------- */
const form = $('#contact-form');
if (form) {
const status = $('.form-status', form);
const submitButton = $('button[type="submit"]', form);
const emailPattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
const showStatus = (message, type) => {
if (!status) return;
status.textContent = message;
status.classList.remove('form-status--error', 'form-status--success');
status.classList.add(`form-status--${type}`);
status.hidden = false;
};
form.addEventListener('submit', (event) => {
event.preventDefault();
const data = Object.fromEntries(new FormData(form));
const fields = ['name', 'email', 'subject', 'message'];
let firstInvalid = null;
fields.forEach((field) => {
const input = form.elements[field];
if (!input) return;
const value = String(data[field] ?? '').trim();
const isInvalid = value === '' || (field === 'email' && !emailPattern.test(value));
if (isInvalid) {
input.setAttribute('aria-invalid', 'true');
firstInvalid = firstInvalid ?? input;
} else {
input.removeAttribute('aria-invalid');
}
});
if (firstInvalid) {
showStatus('Preencha todos os campos com um e-mail válido.', 'error');
firstInvalid.focus();
return;
}
const subject = encodeURIComponent(String(data.subject).trim());
const body = encodeURIComponent(
`${String(data.message).trim()}\n\n— ${String(data.name).trim()} (${String(data.email).trim()})`,
);
window.location.href = `mailto:dionarley@gmail.com?subject=${subject}&body=${body}`;
// Mantém os valores preenchidos: se nenhum cliente de e-mail abrir,
// o visitante ainda pode copiar o texto e tentar de novo.
showStatus('Abrindo seu aplicativo de e-mail para concluir o envio…', 'success');
if (submitButton) submitButton.blur();
});
// Limpa o estado de erro assim que o usuário corrige o campo
form.addEventListener('input', (event) => {
if (event.target.matches('input, textarea')) {
event.target.removeAttribute('aria-invalid');
}
});
}
/* ----------------------------------------------------------------------
* Ano corrente no rodapé
* -------------------------------------------------------------------- */
const yearElement = $('#year');
if (yearElement) {
yearElement.textContent = String(new Date().getFullYear());
}
})();