@feitio/vanilla 0.0.0-stage → 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.
- package/LICENSE +21 -0
- package/README.md +28 -2
- package/dist/dialog.js +46 -0
- package/dist/feitio.js +416 -0
- package/dist/forms.js +111 -0
- package/dist/menu.js +65 -0
- package/dist/notice.js +143 -0
- package/dist/scroll.js +55 -0
- package/dist/status.js +16 -0
- package/dist/types/dialog/actions/wire-dialogs.d.ts +7 -0
- package/dist/types/dialog/calculations/confirm-word.d.ts +2 -0
- package/dist/types/dialog/index.d.ts +3 -0
- package/dist/types/forms/actions/wire-forms.d.ts +6 -0
- package/dist/types/forms/calculations/form-values.d.ts +6 -0
- package/dist/types/forms/index.d.ts +3 -0
- package/dist/types/index.d.ts +7 -0
- package/dist/types/menu/actions/wire-menus.d.ts +6 -0
- package/dist/types/menu/calculations/menu-focus.d.ts +21 -0
- package/dist/types/menu/index.d.ts +3 -0
- package/dist/types/notice/actions/close-notice.d.ts +3 -0
- package/dist/types/notice/actions/show-notice.d.ts +6 -0
- package/dist/types/notice/actions/start-notice-timer.d.ts +7 -0
- package/dist/types/notice/actions/wire-notices.d.ts +6 -0
- package/dist/types/notice/calculations/notice-timer.d.ts +16 -0
- package/dist/types/notice/data/notice.d.ts +8 -0
- package/dist/types/notice/index.d.ts +10 -0
- package/dist/types/scroll/actions/wire-scrolls.d.ts +7 -0
- package/dist/types/scroll/calculations/scroll-visibility.d.ts +4 -0
- package/dist/types/scroll/index.d.ts +3 -0
- package/dist/types/status/actions/set-status.d.ts +5 -0
- package/dist/types/status/index.d.ts +1 -0
- package/package.json +77 -4
- package/src/dialog/actions/wire-dialogs.ts +39 -0
- package/src/dialog/calculations/confirm-word.ts +4 -0
- package/src/dialog/index.ts +8 -0
- package/src/forms/actions/wire-forms.ts +92 -0
- package/src/forms/calculations/form-values.ts +19 -0
- package/src/forms/index.ts +8 -0
- package/src/index.ts +7 -0
- package/src/menu/actions/wire-menus.ts +44 -0
- package/src/menu/calculations/menu-focus.ts +33 -0
- package/src/menu/index.ts +8 -0
- package/src/notice/actions/close-notice.ts +17 -0
- package/src/notice/actions/show-notice.ts +17 -0
- package/src/notice/actions/start-notice-timer.ts +54 -0
- package/src/notice/actions/wire-notices.ts +40 -0
- package/src/notice/calculations/notice-timer.ts +29 -0
- package/src/notice/data/notice.ts +9 -0
- package/src/notice/index.ts +27 -0
- package/src/scroll/actions/wire-scrolls.ts +46 -0
- package/src/scroll/calculations/scroll-visibility.ts +9 -0
- package/src/scroll/index.ts +8 -0
- package/src/status/actions/set-status.ts +18 -0
- package/src/status/index.ts +1 -0
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { wireForms } from './actions/wire-forms.ts';
|
|
2
|
+
|
|
3
|
+
export { matchesFilter, plainText, togglePeriod } from './calculations/form-values.ts';
|
|
4
|
+
|
|
5
|
+
/** The form controls of a page: Switch, SegmentedControl, TimeInput and FilterField. */
|
|
6
|
+
export function forms(window: Window, root: Document | HTMLElement = window.document): void {
|
|
7
|
+
wireForms(window, root);
|
|
8
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/** Feitio's component behaviors in plain JavaScript. Each one also has its own entry point. */
|
|
2
|
+
export { dialogs } from './dialog/index.ts';
|
|
3
|
+
export { forms } from './forms/index.ts';
|
|
4
|
+
export { menus } from './menu/index.ts';
|
|
5
|
+
export { notices } from './notice/index.ts';
|
|
6
|
+
export { scrolls } from './scroll/index.ts';
|
|
7
|
+
export { status } from './status/index.ts';
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { menuPosition, nextMenuIndex } from '../calculations/menu-focus.ts';
|
|
2
|
+
|
|
3
|
+
/** Menus already wired: an Astro script and a React hook may both see one. */
|
|
4
|
+
const wired = new WeakSet<HTMLElement>();
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Wires every Feitio Menu under root, each only once: the button opens its list (the popover API does the opening,
|
|
8
|
+
* Esc and the click outside); this places it next to the button, puts the focus on the current item,
|
|
9
|
+
* and moves it with the arrows, Home and End.
|
|
10
|
+
*/
|
|
11
|
+
export function wireMenus(window: Window, root: ParentNode): void {
|
|
12
|
+
for (const list of root.querySelectorAll<HTMLElement>('.feitio-menu-list[popover]')) {
|
|
13
|
+
const button = root.querySelector<HTMLElement>(`[popovertarget="${list.id}"]`);
|
|
14
|
+
if (!button || wired.has(list)) continue;
|
|
15
|
+
wired.add(list);
|
|
16
|
+
const items = (): HTMLElement[] => [...list.querySelectorAll<HTMLElement>('a, button')];
|
|
17
|
+
list.addEventListener('toggle', (event) => {
|
|
18
|
+
const open = (event as ToggleEvent).newState === 'open';
|
|
19
|
+
button.setAttribute('aria-expanded', String(open));
|
|
20
|
+
if (!open) return;
|
|
21
|
+
const place = menuPosition(
|
|
22
|
+
button.getBoundingClientRect(),
|
|
23
|
+
list.getBoundingClientRect(),
|
|
24
|
+
{ width: window.innerWidth, height: window.innerHeight },
|
|
25
|
+
8,
|
|
26
|
+
);
|
|
27
|
+
list.style.left = `${place.left}px`;
|
|
28
|
+
list.style.top = `${place.top}px`;
|
|
29
|
+
const current = items().find((item) => item.getAttribute('aria-current')) ?? items()[0];
|
|
30
|
+
current?.focus();
|
|
31
|
+
});
|
|
32
|
+
list.addEventListener('keydown', (event) => {
|
|
33
|
+
const all = items();
|
|
34
|
+
const next = nextMenuIndex(
|
|
35
|
+
event.key,
|
|
36
|
+
all.indexOf(window.document.activeElement as HTMLElement),
|
|
37
|
+
all.length,
|
|
38
|
+
);
|
|
39
|
+
if (next === undefined) return;
|
|
40
|
+
event.preventDefault();
|
|
41
|
+
all[next]?.focus();
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/** Which item gets the focus after a key, in a menu of `count` items where `current` has it (-1: none). */
|
|
2
|
+
export function nextMenuIndex(key: string, current: number, count: number): number | undefined {
|
|
3
|
+
if (count === 0) return undefined;
|
|
4
|
+
switch (key) {
|
|
5
|
+
case 'ArrowDown':
|
|
6
|
+
return current < 0 ? 0 : (current + 1) % count;
|
|
7
|
+
case 'ArrowUp':
|
|
8
|
+
return current <= 0 ? count - 1 : current - 1;
|
|
9
|
+
case 'Home':
|
|
10
|
+
return 0;
|
|
11
|
+
case 'End':
|
|
12
|
+
return count - 1;
|
|
13
|
+
default:
|
|
14
|
+
return undefined;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Where the open menu goes, in the viewport: under its button, aligned to the button's end when it
|
|
20
|
+
* would pass the window's edge, and above it when there is no room below.
|
|
21
|
+
*/
|
|
22
|
+
export function menuPosition(
|
|
23
|
+
button: { left: number; right: number; top: number; bottom: number },
|
|
24
|
+
menu: { width: number; height: number },
|
|
25
|
+
viewport: { width: number; height: number },
|
|
26
|
+
gap: number,
|
|
27
|
+
): { left: number; top: number } {
|
|
28
|
+
const fitsRight = button.left + menu.width <= viewport.width - gap;
|
|
29
|
+
const left = Math.max(gap, fitsRight ? button.left : button.right - menu.width);
|
|
30
|
+
const fitsBelow = button.bottom + gap + menu.height <= viewport.height - gap;
|
|
31
|
+
const top = fitsBelow ? button.bottom + gap : Math.max(gap, button.top - gap - menu.height);
|
|
32
|
+
return { left, top };
|
|
33
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { wireMenus } from './actions/wire-menus.ts';
|
|
2
|
+
|
|
3
|
+
export { menuPosition, nextMenuIndex } from './calculations/menu-focus.ts';
|
|
4
|
+
|
|
5
|
+
/** The Menu for a page: every button with a list of links or actions under it. */
|
|
6
|
+
export function menus(window: Window, root: ParentNode = window.document): void {
|
|
7
|
+
wireMenus(window, root);
|
|
8
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { LEAVE_FALLBACK_MS, type NoticeHost } from '../data/notice.ts';
|
|
2
|
+
|
|
3
|
+
/** Closes a notice: it fades and its space slides shut, then it is hidden. Stops its clock. */
|
|
4
|
+
export function closeNotice(host: NoticeHost, notice: HTMLElement): void {
|
|
5
|
+
if (notice.hidden || notice.classList.contains('leaving')) return;
|
|
6
|
+
host.clocks.get(notice)?.();
|
|
7
|
+
host.clocks.delete(notice);
|
|
8
|
+
notice.classList.add('leaving');
|
|
9
|
+
function done(): void {
|
|
10
|
+
notice.hidden = true;
|
|
11
|
+
notice.classList.remove('leaving');
|
|
12
|
+
}
|
|
13
|
+
notice.addEventListener('animationend', done, { once: true });
|
|
14
|
+
host.window.setTimeout(() => {
|
|
15
|
+
if (!notice.hidden) done();
|
|
16
|
+
}, LEAVE_FALLBACK_MS);
|
|
17
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { NoticeHost } from '../data/notice.ts';
|
|
2
|
+
import { startNoticeTimer } from './start-notice-timer.ts';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Shows a notice after an action. Hidden, it arrives as placed paper; already on screen, it does not
|
|
6
|
+
* land again: only its time starts over (one paper at a time).
|
|
7
|
+
*/
|
|
8
|
+
export function showNotice(host: NoticeHost, notice: HTMLElement): void {
|
|
9
|
+
if (notice.hidden) {
|
|
10
|
+
notice.hidden = false;
|
|
11
|
+
notice.classList.remove('feitio-place');
|
|
12
|
+
void notice.offsetWidth;
|
|
13
|
+
notice.classList.add('feitio-place');
|
|
14
|
+
}
|
|
15
|
+
notice.style.setProperty('--notice-p', '0');
|
|
16
|
+
startNoticeTimer(host, notice);
|
|
17
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import {
|
|
2
|
+
closesByItself,
|
|
3
|
+
countFrame,
|
|
4
|
+
noticeProgress,
|
|
5
|
+
noticeStatus,
|
|
6
|
+
} from '../calculations/notice-timer.ts';
|
|
7
|
+
import type { NoticeHost } from '../data/notice.ts';
|
|
8
|
+
import { closeNotice } from './close-notice.ts';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Starts a timed notice's clock: it counts in JavaScript, frame by frame, not in a CSS animation, so
|
|
12
|
+
* it still runs with reduced motion. The time stops while the pointer or focus is on the notice, and
|
|
13
|
+
* --notice-p carries how far it has run, for the thread around the close button.
|
|
14
|
+
*/
|
|
15
|
+
export function startNoticeTimer(host: NoticeHost, notice: HTMLElement): void {
|
|
16
|
+
host.clocks.get(notice)?.();
|
|
17
|
+
const total = Number(notice.dataset.timeout);
|
|
18
|
+
if (!closesByItself(noticeStatus(notice.dataset.status), total)) return;
|
|
19
|
+
const { window } = host;
|
|
20
|
+
let used = 0;
|
|
21
|
+
let last = window.performance.now();
|
|
22
|
+
let paused = notice.matches(':hover, :focus-within');
|
|
23
|
+
let frame = 0;
|
|
24
|
+
function pause(): void {
|
|
25
|
+
paused = true;
|
|
26
|
+
}
|
|
27
|
+
function resume(): void {
|
|
28
|
+
window.setTimeout(() => {
|
|
29
|
+
if (notice.matches(':hover, :focus-within')) return;
|
|
30
|
+
paused = false;
|
|
31
|
+
last = window.performance.now();
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
const listeners: [string, () => void][] = [
|
|
35
|
+
['mouseenter', pause],
|
|
36
|
+
['focusin', pause],
|
|
37
|
+
['mouseleave', resume],
|
|
38
|
+
['focusout', resume],
|
|
39
|
+
];
|
|
40
|
+
for (const [event, listener] of listeners) notice.addEventListener(event, listener);
|
|
41
|
+
function tick(now: number): void {
|
|
42
|
+
used = countFrame(used, now - last, paused);
|
|
43
|
+
last = now;
|
|
44
|
+
const progress = noticeProgress(used, total);
|
|
45
|
+
notice.style.setProperty('--notice-p', progress.toFixed(4));
|
|
46
|
+
if (progress >= 1) closeNotice(host, notice);
|
|
47
|
+
else frame = window.requestAnimationFrame(tick);
|
|
48
|
+
}
|
|
49
|
+
host.clocks.set(notice, () => {
|
|
50
|
+
window.cancelAnimationFrame(frame);
|
|
51
|
+
for (const [event, listener] of listeners) notice.removeEventListener(event, listener);
|
|
52
|
+
});
|
|
53
|
+
frame = window.requestAnimationFrame(tick);
|
|
54
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { NoticeHost } from '../data/notice.ts';
|
|
2
|
+
import { closeNotice } from './close-notice.ts';
|
|
3
|
+
import { startNoticeTimer } from './start-notice-timer.ts';
|
|
4
|
+
|
|
5
|
+
/** Notices already wired, by any controller: an Astro script and a React hook may both see one. */
|
|
6
|
+
const wired = new WeakSet<HTMLElement>();
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Wires every notice under root (root itself too, when it is one), each only once: the close button closes it, and a timed notice already on the page
|
|
10
|
+
* (after saving and coming back, say) only starts counting once it shows in the window.
|
|
11
|
+
*/
|
|
12
|
+
export function wireNotices(host: NoticeHost, root: ParentNode): void {
|
|
13
|
+
const own = 'matches' in root && (root as Element).matches('.feitio-notice');
|
|
14
|
+
const notices = [
|
|
15
|
+
...(own ? [root as HTMLElement] : []),
|
|
16
|
+
...root.querySelectorAll<HTMLElement>('.feitio-notice'),
|
|
17
|
+
];
|
|
18
|
+
const Observer = (host.window as Window & { IntersectionObserver?: typeof IntersectionObserver })
|
|
19
|
+
.IntersectionObserver;
|
|
20
|
+
const seen = Observer
|
|
21
|
+
? new Observer(
|
|
22
|
+
(entries: IntersectionObserverEntry[]) => {
|
|
23
|
+
for (const entry of entries) {
|
|
24
|
+
if (!entry.isIntersecting) continue;
|
|
25
|
+
seen?.unobserve(entry.target);
|
|
26
|
+
startNoticeTimer(host, entry.target as HTMLElement);
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
{ threshold: 0.9 },
|
|
30
|
+
)
|
|
31
|
+
: undefined;
|
|
32
|
+
for (const notice of notices) {
|
|
33
|
+
if (wired.has(notice)) continue;
|
|
34
|
+
wired.add(notice);
|
|
35
|
+
notice.querySelector('.close')?.addEventListener('click', () => closeNotice(host, notice));
|
|
36
|
+
if (!notice.dataset.timeout || notice.hidden) continue;
|
|
37
|
+
if (seen) seen.observe(notice);
|
|
38
|
+
else startNoticeTimer(host, notice);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/** A notice's status, as its data-status says: none is information. */
|
|
2
|
+
export type NoticeStatus = 'info' | 'ok' | 'warn' | 'error';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Whether a notice may close by itself: only success and information, and only with a time.
|
|
6
|
+
* Warnings and errors stay until the person closes them.
|
|
7
|
+
*/
|
|
8
|
+
export function closesByItself(status: NoticeStatus, timeoutMs: number): boolean {
|
|
9
|
+
return (status === 'info' || status === 'ok') && Number.isFinite(timeoutMs) && timeoutMs > 0;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/** Reads data-status, where no attribute (or an unknown one) is information. */
|
|
13
|
+
export function noticeStatus(attribute: string | null | undefined): NoticeStatus {
|
|
14
|
+
return attribute === 'ok' || attribute === 'warn' || attribute === 'error' ? attribute : 'info';
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** How far the time has run, from 0 to 1, given the milliseconds already counted. */
|
|
18
|
+
export function noticeProgress(usedMs: number, timeoutMs: number): number {
|
|
19
|
+
if (timeoutMs <= 0) return 1;
|
|
20
|
+
return Math.min(Math.max(usedMs / timeoutMs, 0), 1);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* The time counted after one frame: it only grows while the notice is not paused (no pointer
|
|
25
|
+
* or focus on it), so hovering stops the clock instead of hiding the time it had left.
|
|
26
|
+
*/
|
|
27
|
+
export function countFrame(usedMs: number, elapsedMs: number, paused: boolean): number {
|
|
28
|
+
return paused ? usedMs : usedMs + Math.max(elapsedMs, 0);
|
|
29
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/** What the notice actions need from the page: passed in, so a test can hand in its own window. */
|
|
2
|
+
export type NoticeHost = {
|
|
3
|
+
readonly window: Window;
|
|
4
|
+
/** The running clock of each timed notice, to stop it when it closes or starts again. */
|
|
5
|
+
readonly clocks: WeakMap<HTMLElement, () => void>;
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
/** How long a leaving notice may take before it is hidden anyway (no animation with reduced motion). */
|
|
9
|
+
export const LEAVE_FALLBACK_MS = 600;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { closeNotice } from './actions/close-notice.ts';
|
|
2
|
+
import { showNotice } from './actions/show-notice.ts';
|
|
3
|
+
import { wireNotices } from './actions/wire-notices.ts';
|
|
4
|
+
import type { NoticeHost } from './data/notice.ts';
|
|
5
|
+
|
|
6
|
+
export { closesByItself, noticeProgress } from './calculations/notice-timer.ts';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* The Notice for a page: wires every notice in it and hands back show and close, and wire, for a
|
|
10
|
+
* notice added later (a React component mounting).
|
|
11
|
+
*/
|
|
12
|
+
export function notices(
|
|
13
|
+
window: Window,
|
|
14
|
+
root: ParentNode = window.document,
|
|
15
|
+
): {
|
|
16
|
+
show: (notice: HTMLElement) => void;
|
|
17
|
+
close: (notice: HTMLElement) => void;
|
|
18
|
+
wire: (root: ParentNode) => void;
|
|
19
|
+
} {
|
|
20
|
+
const host: NoticeHost = { window, clocks: new WeakMap() };
|
|
21
|
+
wireNotices(host, root);
|
|
22
|
+
return {
|
|
23
|
+
show: (notice) => showNotice(host, notice),
|
|
24
|
+
close: (notice) => closeNotice(host, notice),
|
|
25
|
+
wire: (more) => wireNotices(host, more),
|
|
26
|
+
};
|
|
27
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { backToTopVisible, fadeVisible } from '../calculations/scroll-visibility.ts';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Wires every ScrollFade and BackToTop under root to the box that scrolls around it (or the window):
|
|
5
|
+
* the fade shows while there is more below, BackToTop after half a screen, and its click scrolls
|
|
6
|
+
* back up (smoothly, unless motion is reduced) and moves the focus to its target. Each element is
|
|
7
|
+
* wired once, so calling it again after new HTML arrives is safe.
|
|
8
|
+
*/
|
|
9
|
+
export function wireScrolls(window: Window, root: ParentNode): void {
|
|
10
|
+
const document = window.document;
|
|
11
|
+
function scrollerOf(element: HTMLElement): HTMLElement | null {
|
|
12
|
+
for (let parent = element.parentElement; parent; parent = parent.parentElement) {
|
|
13
|
+
const overflow = window.getComputedStyle(parent).overflowY;
|
|
14
|
+
if (overflow === 'auto' || overflow === 'scroll') return parent;
|
|
15
|
+
}
|
|
16
|
+
return null;
|
|
17
|
+
}
|
|
18
|
+
for (const element of root.querySelectorAll<HTMLElement>(
|
|
19
|
+
'.feitio-scroll-fade, .feitio-back-to-top',
|
|
20
|
+
)) {
|
|
21
|
+
if (element.dataset.wired !== undefined) continue;
|
|
22
|
+
element.dataset.wired = '';
|
|
23
|
+
const box = scrollerOf(element);
|
|
24
|
+
const area = box ?? document.documentElement;
|
|
25
|
+
const fade = element.classList.contains('feitio-scroll-fade');
|
|
26
|
+
const update = (): void => {
|
|
27
|
+
const visible = fade
|
|
28
|
+
? fadeVisible(area.scrollTop, area.clientHeight, area.scrollHeight)
|
|
29
|
+
: backToTopVisible(area.scrollTop, area.clientHeight);
|
|
30
|
+
element.toggleAttribute('data-visible', visible);
|
|
31
|
+
};
|
|
32
|
+
(box ?? window).addEventListener('scroll', update, { passive: true });
|
|
33
|
+
window.addEventListener('resize', update);
|
|
34
|
+
update();
|
|
35
|
+
if (!fade) {
|
|
36
|
+
element.addEventListener('click', (event) => {
|
|
37
|
+
event.preventDefault();
|
|
38
|
+
const still = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
|
|
39
|
+
area.scrollTo({ top: 0, behavior: still ? 'auto' : 'smooth' });
|
|
40
|
+
const target = element.getAttribute('href');
|
|
41
|
+
if (target?.startsWith('#'))
|
|
42
|
+
document.querySelector<HTMLElement>(target)?.focus({ preventScroll: true });
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/** Whether the ScrollFade shows: there is more than a few pixels left below what is in view. */
|
|
2
|
+
export function fadeVisible(scrollTop: number, viewHeight: number, scrollHeight: number): boolean {
|
|
3
|
+
return scrollTop + viewHeight < scrollHeight - 8;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
/** Whether BackToTop shows: the person has scrolled past half a screen. */
|
|
7
|
+
export function backToTopVisible(scrollTop: number, viewHeight: number): boolean {
|
|
8
|
+
return scrollTop > viewHeight * 0.5;
|
|
9
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { wireScrolls } from './actions/wire-scrolls.ts';
|
|
2
|
+
|
|
3
|
+
export { backToTopVisible, fadeVisible } from './calculations/scroll-visibility.ts';
|
|
4
|
+
|
|
5
|
+
/** ScrollFade and BackToTop for a page or a part of it; safe to call again after new HTML. */
|
|
6
|
+
export function scrolls(window: Window, root: ParentNode = window.document): void {
|
|
7
|
+
wireScrolls(window, root);
|
|
8
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shows the feedback on a StatusOverlay's container: "loading", "ok" or "error", with the words for
|
|
3
|
+
* it; null clears it. The status region announces the words to screen readers.
|
|
4
|
+
*/
|
|
5
|
+
export function setStatus(
|
|
6
|
+
host: HTMLElement,
|
|
7
|
+
state: 'loading' | 'ok' | 'error' | null,
|
|
8
|
+
label = '',
|
|
9
|
+
): void {
|
|
10
|
+
const words = host.querySelector('.feitio-status .label');
|
|
11
|
+
if (state === null) {
|
|
12
|
+
delete host.dataset.state;
|
|
13
|
+
if (words) words.textContent = '';
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
host.dataset.state = state;
|
|
17
|
+
if (words) words.textContent = label;
|
|
18
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { setStatus as status } from './actions/set-status.ts';
|