tyrell-components 1.0.0-RC15 → 1.0.0-RC17
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/README.md +44 -52
- package/css/tyrell-colors-static.css +14 -0
- package/css/tyrell-theme.css +13 -7
- package/css/tyrell.css +15 -22
- package/dist/tyrell-colors-static.css +1 -1
- package/dist/tyrell-theme.css +1 -1
- package/dist/tyrell.css +1 -1
- package/dist/tyrell.js +1 -1
- package/lib/base/ty-component.d.ts +37 -15
- package/lib/base/ty-component.js +90 -26
- package/lib/components/button.d.ts +14 -0
- package/lib/components/button.js +61 -7
- package/lib/components/calendar-month.d.ts +7 -1
- package/lib/components/calendar-month.js +38 -30
- package/lib/components/calendar-navigation.d.ts +11 -0
- package/lib/components/calendar-navigation.js +68 -44
- package/lib/components/calendar.d.ts +52 -3
- package/lib/components/calendar.js +274 -61
- package/lib/components/checkbox.d.ts +8 -1
- package/lib/components/checkbox.js +29 -8
- package/lib/components/copy.d.ts +2 -0
- package/lib/components/copy.js +24 -33
- package/lib/components/date-picker.d.ts +68 -34
- package/lib/components/date-picker.js +386 -389
- package/lib/components/file-upload.d.ts +7 -0
- package/lib/components/file-upload.js +36 -4
- package/lib/components/input.d.ts +73 -3
- package/lib/components/input.js +159 -24
- package/lib/components/modal.d.ts +4 -8
- package/lib/components/modal.js +103 -33
- package/lib/components/option.js +7 -5
- package/lib/components/popup.js +117 -14
- package/lib/components/radio.d.ts +24 -0
- package/lib/components/radio.js +55 -32
- package/lib/components/resize-observer.js +2 -0
- package/lib/components/scroll-container.d.ts +3 -2
- package/lib/components/scroll-container.js +7 -8
- package/lib/components/select.d.ts +68 -11
- package/lib/components/select.js +196 -44
- package/lib/components/selected-tags.d.ts +7 -1
- package/lib/components/selected-tags.js +10 -1
- package/lib/components/switch.d.ts +9 -1
- package/lib/components/switch.js +29 -8
- package/lib/components/tab.d.ts +49 -1
- package/lib/components/tab.js +151 -10
- package/lib/components/tabs.d.ts +14 -2
- package/lib/components/tabs.js +185 -136
- package/lib/components/tag.d.ts +4 -7
- package/lib/components/tag.js +6 -7
- package/lib/components/textarea.d.ts +51 -5
- package/lib/components/textarea.js +95 -30
- package/lib/components/tooltip.d.ts +4 -3
- package/lib/components/tooltip.js +34 -24
- package/lib/components/wizard.d.ts +5 -4
- package/lib/components/wizard.js +120 -156
- package/lib/index.d.ts +2 -1
- package/lib/index.js +1 -0
- package/lib/styles/button.js +21 -19
- package/lib/styles/calendar-month.d.ts +3 -0
- package/lib/styles/calendar-month.js +43 -27
- package/lib/styles/calendar-navigation.d.ts +1 -1
- package/lib/styles/calendar-navigation.js +55 -26
- package/lib/styles/calendar.d.ts +3 -2
- package/lib/styles/calendar.js +77 -0
- package/lib/styles/checkbox.js +31 -13
- package/lib/styles/copy.js +5 -1
- package/lib/styles/date-picker.js +68 -26
- package/lib/styles/file-upload.d.ts +1 -1
- package/lib/styles/file-upload.js +3 -3
- package/lib/styles/icon.d.ts +1 -1
- package/lib/styles/icon.js +2 -2
- package/lib/styles/input.js +6 -10
- package/lib/styles/option.d.ts +1 -1
- package/lib/styles/option.js +7 -3
- package/lib/styles/popup.d.ts +1 -1
- package/lib/styles/popup.js +10 -5
- package/lib/styles/radio.js +18 -6
- package/lib/styles/scroll-container.d.ts +1 -1
- package/lib/styles/scroll-container.js +1 -1
- package/lib/styles/select-base.js +24 -3
- package/lib/styles/select.d.ts +1 -1
- package/lib/styles/select.js +10 -0
- package/lib/styles/step.d.ts +1 -1
- package/lib/styles/step.js +1 -9
- package/lib/styles/switch.js +14 -6
- package/lib/styles/tab.d.ts +1 -1
- package/lib/styles/tab.js +67 -3
- package/lib/styles/tabs.d.ts +1 -1
- package/lib/styles/tabs.js +7 -3
- package/lib/styles/tag.js +3 -3
- package/lib/styles/textarea.d.ts +1 -1
- package/lib/styles/textarea.js +7 -0
- package/lib/styles/wizard.d.ts +9 -5
- package/lib/styles/wizard.js +211 -99
- package/lib/types/common.d.ts +2 -2
- package/lib/utils/calendar-utils.d.ts +49 -0
- package/lib/utils/calendar-utils.js +63 -6
- package/lib/utils/property-manager.d.ts +7 -1
- package/lib/utils/property-manager.js +16 -4
- package/lib/utils/scroll-lock.d.ts +10 -4
- package/lib/utils/scroll-lock.js +102 -2
- package/lib/utils/upgrade-properties.d.ts +21 -0
- package/lib/utils/upgrade-properties.js +47 -0
- package/lib/utils/validity.d.ts +122 -0
- package/lib/utils/validity.js +289 -0
- package/lib/version.d.ts +1 -1
- package/lib/version.js +1 -1
- package/package.json +27 -5
|
@@ -41,6 +41,14 @@ export interface DayContext {
|
|
|
41
41
|
/** Calendar's selected day (if any) */
|
|
42
42
|
selectedDay?: number;
|
|
43
43
|
}
|
|
44
|
+
/**
|
|
45
|
+
* Local midnight of a calendar day. Not the Date constructor: it reads years
|
|
46
|
+
* 0-99 as 1900-1999. Noon is never skipped by a clock change, so the day
|
|
47
|
+
* can't shift before setHours.
|
|
48
|
+
*/
|
|
49
|
+
export declare function localMidnight(year: number, month: number, day: number): Date;
|
|
50
|
+
/** UTC midnight of a calendar day, as a timestamp. Not Date.UTC(): it reads years 0-99 as 1900-1999. */
|
|
51
|
+
export declare function utcMidnight(year: number, month: number, day: number): number;
|
|
44
52
|
/**
|
|
45
53
|
* Generate a 42-day calendar grid for a given month
|
|
46
54
|
*
|
|
@@ -109,3 +117,44 @@ export declare function parseISODate(isoString: string): {
|
|
|
109
117
|
month: number;
|
|
110
118
|
day: number;
|
|
111
119
|
} | null;
|
|
120
|
+
/**
|
|
121
|
+
* Date/time components in local time, as ty-date-picker holds them.
|
|
122
|
+
*/
|
|
123
|
+
export interface DateTimeParts {
|
|
124
|
+
year?: number;
|
|
125
|
+
month?: number;
|
|
126
|
+
day?: number;
|
|
127
|
+
hour?: number;
|
|
128
|
+
minute?: number;
|
|
129
|
+
second?: number;
|
|
130
|
+
epoch?: number;
|
|
131
|
+
}
|
|
132
|
+
/** A Date whose UTC fields are these components: formats a wall-clock as written (timeZone 'UTC'). */
|
|
133
|
+
export declare function wallClockDate(c: DateTimeParts): Date;
|
|
134
|
+
/**
|
|
135
|
+
* Parse any date value ty-date-picker reads into local components:
|
|
136
|
+
* - Date only: '2024-09-21' (that calendar day)
|
|
137
|
+
* - Datetime-local: '2024-09-21T10:30[:45]' (local wall-clock)
|
|
138
|
+
* - UTC strings: '2024-09-21T08:30:00Z' or '2024-09-21T08:30:00.000Z'
|
|
139
|
+
* - With timezone: '2024-09-21T10:30:00+02:00'
|
|
140
|
+
* - Timestamps: milliseconds since epoch (a number, not a digit string)
|
|
141
|
+
*
|
|
142
|
+
* A zone-less value is taken as written, even a time the clocks skip when
|
|
143
|
+
* summer time starts (like <input type=datetime-local>); 2024-02-30 or 24:00
|
|
144
|
+
* is no value. An instant reads as its LOCAL day and time, and keeps its
|
|
145
|
+
* epoch.
|
|
146
|
+
*/
|
|
147
|
+
export declare function parseDateTime(value: string | number | null | undefined): DateTimeParts | null;
|
|
148
|
+
/**
|
|
149
|
+
* The calendar day of any value parseDateTime reads, or a Date: an instant
|
|
150
|
+
* is its local day. Years outside 1-9999 (no 4-digit YYYY-MM-DD) are no day.
|
|
151
|
+
*/
|
|
152
|
+
export declare function parseDateDay(value: string | number | Date | null | undefined): {
|
|
153
|
+
year: number;
|
|
154
|
+
month: number;
|
|
155
|
+
day: number;
|
|
156
|
+
} | null;
|
|
157
|
+
/** 'YYYY-MM-DD' of a calendar day (years below 1000 padded to 4 digits). */
|
|
158
|
+
export declare function formatISODay(year: number, month: number, day: number): string;
|
|
159
|
+
/** Any value parseDateDay reads as 'YYYY-MM-DD', or null. */
|
|
160
|
+
export declare function toISODay(value: string | number | Date | null | undefined): string | null;
|
|
@@ -13,8 +13,16 @@ return date1.getFullYear() === date2.getFullYear()
|
|
|
13
13
|
&& date1.getMonth() === date2.getMonth()
|
|
14
14
|
&& date1.getDate() === date2.getDate();
|
|
15
15
|
}
|
|
16
|
-
function
|
|
17
|
-
|
|
16
|
+
export function localMidnight(year, month, day) {
|
|
17
|
+
const date = new Date(2000, 0, 1, 12);
|
|
18
|
+
date.setFullYear(year, month - 1, day);
|
|
19
|
+
date.setHours(0, 0, 0, 0);
|
|
20
|
+
return date;
|
|
21
|
+
}
|
|
22
|
+
export function utcMidnight(year, month, day) {
|
|
23
|
+
const date = new Date(0);
|
|
24
|
+
date.setUTCFullYear(year, month - 1, day);
|
|
25
|
+
return date.getTime();
|
|
18
26
|
}
|
|
19
27
|
function getTodayMidnight() {
|
|
20
28
|
const now = new Date();
|
|
@@ -41,7 +49,7 @@ const isToday = isSameDay(date, todayMidnight);
|
|
|
41
49
|
const isSelected = selection?.year === year
|
|
42
50
|
&& selection?.month === month
|
|
43
51
|
&& selection?.day === dayInMonth;
|
|
44
|
-
const utcValue =
|
|
52
|
+
const utcValue = utcMidnight(year, month, dayInMonth);
|
|
45
53
|
const localValue = date.getTime();
|
|
46
54
|
return {
|
|
47
55
|
value: utcValue,
|
|
@@ -69,7 +77,7 @@ monday.setHours(0, 0, 0, 0);
|
|
|
69
77
|
return monday;
|
|
70
78
|
}
|
|
71
79
|
function getMonthGridStart(year, month) {
|
|
72
|
-
const firstDay =
|
|
80
|
+
const firstDay = localMidnight(year, month, 1);
|
|
73
81
|
return getMondayOfWeek(firstDay);
|
|
74
82
|
}
|
|
75
83
|
function addDays(date, days) {
|
|
@@ -120,9 +128,58 @@ if (month < 1 || month > 12)
|
|
|
120
128
|
return null;
|
|
121
129
|
if (day < 1 || day > 31)
|
|
122
130
|
return null;
|
|
123
|
-
const date = new Date(year, month
|
|
124
|
-
if (date.
|
|
131
|
+
const date = new Date(utcMidnight(year, month, day));
|
|
132
|
+
if (date.getUTCMonth() !== month - 1 || date.getUTCDate() !== day) {
|
|
125
133
|
return null;
|
|
126
134
|
}
|
|
127
135
|
return { year, month, day };
|
|
128
136
|
}
|
|
137
|
+
const LOCAL_VALUE = /^(\d{4})-(\d{2})-(\d{2})(?:[T ](\d{2}):(\d{2})(?::(\d{2})(?:\.\d+)?)?)?$/;
|
|
138
|
+
export function wallClockDate(c) {
|
|
139
|
+
const date = new Date(0);
|
|
140
|
+
date.setUTCFullYear(c.year, c.month - 1, c.day);
|
|
141
|
+
date.setUTCHours(c.hour || 0, c.minute || 0, c.second || 0);
|
|
142
|
+
return date;
|
|
143
|
+
}
|
|
144
|
+
export function parseDateTime(value) {
|
|
145
|
+
if (value === null || value === undefined || value === '')
|
|
146
|
+
return null;
|
|
147
|
+
if (typeof value === 'string') {
|
|
148
|
+
const local = LOCAL_VALUE.exec(value);
|
|
149
|
+
if (local) {
|
|
150
|
+
const [year, month, day, hour, minute, second] = local.slice(1, 7).map((g) => Number(g ?? 0));
|
|
151
|
+
const components = { year, month, day, hour, minute, second };
|
|
152
|
+
const check = wallClockDate(components);
|
|
153
|
+
const real = check.getUTCMonth() === month - 1 && check.getUTCDate() === day &&
|
|
154
|
+
hour < 24 && minute < 60 && second < 60;
|
|
155
|
+
return real ? components : null;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
const dateObj = typeof value === 'number' || typeof value === 'string' ? new Date(value) : null;
|
|
159
|
+
if (!dateObj || isNaN(dateObj.getTime())) {
|
|
160
|
+
return null;
|
|
161
|
+
}
|
|
162
|
+
return {
|
|
163
|
+
year: dateObj.getFullYear(),
|
|
164
|
+
month: dateObj.getMonth() + 1,
|
|
165
|
+
day: dateObj.getDate(),
|
|
166
|
+
hour: dateObj.getHours(),
|
|
167
|
+
minute: dateObj.getMinutes(),
|
|
168
|
+
second: dateObj.getSeconds(),
|
|
169
|
+
epoch: dateObj.getTime(),
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
export function parseDateDay(value) {
|
|
173
|
+
const parts = parseDateTime(value instanceof Date ? value.getTime() : value);
|
|
174
|
+
if (!parts || parts.year < 1 || parts.year > 9999)
|
|
175
|
+
return null;
|
|
176
|
+
return { year: parts.year, month: parts.month, day: parts.day };
|
|
177
|
+
}
|
|
178
|
+
export function formatISODay(year, month, day) {
|
|
179
|
+
const pad = (n, w) => n.toString().padStart(w, '0');
|
|
180
|
+
return `${pad(year, 4)}-${pad(month, 2)}-${pad(day, 2)}`;
|
|
181
|
+
}
|
|
182
|
+
export function toISODay(value) {
|
|
183
|
+
const d = parseDateDay(value);
|
|
184
|
+
return d && formatISODay(d.year, d.month, d.day);
|
|
185
|
+
}
|
|
@@ -29,6 +29,12 @@ export interface PropertyChange {
|
|
|
29
29
|
/**
|
|
30
30
|
* Manages component properties with unified lifecycle
|
|
31
31
|
*/
|
|
32
|
+
/**
|
|
33
|
+
* Boolean coercion for a property set from JS, HTML-style: '' (what React 19
|
|
34
|
+
* and Vue hand a property for a bare attribute) is true; false, null,
|
|
35
|
+
* undefined, 0, 'false' and '0' are false.
|
|
36
|
+
*/
|
|
37
|
+
export declare function coerceBoolean(value: unknown): boolean;
|
|
32
38
|
export declare class PropertyManager<T = any> {
|
|
33
39
|
private _props;
|
|
34
40
|
private _config;
|
|
@@ -83,7 +89,7 @@ export declare class PropertyManager<T = any> {
|
|
|
83
89
|
*/
|
|
84
90
|
shouldEmitChange(name: string): boolean;
|
|
85
91
|
/**
|
|
86
|
-
* Handle property aliases (
|
|
92
|
+
* Handle property aliases (extra attribute names mapped to a fixed value)
|
|
87
93
|
*/
|
|
88
94
|
handleAlias(aliasName: string, value: any): PropertyChange | null;
|
|
89
95
|
}
|
|
@@ -1,3 +1,13 @@
|
|
|
1
|
+
export function coerceBoolean(value) {
|
|
2
|
+
if (typeof value === 'string') {
|
|
3
|
+
const normalized = value.toLowerCase().trim();
|
|
4
|
+
return normalized !== 'false' && normalized !== '0';
|
|
5
|
+
}
|
|
6
|
+
return Boolean(value);
|
|
7
|
+
}
|
|
8
|
+
function defaultOf(config) {
|
|
9
|
+
return typeof config.default === 'function' ? config.default() : config.default;
|
|
10
|
+
}
|
|
1
11
|
export class PropertyManager {
|
|
2
12
|
constructor(config) {
|
|
3
13
|
this._props = new Map();
|
|
@@ -7,7 +17,7 @@ this._initializeDefaults();
|
|
|
7
17
|
_initializeDefaults() {
|
|
8
18
|
for (const [name, config] of Object.entries(this._config)) {
|
|
9
19
|
if (config.default !== undefined) {
|
|
10
|
-
this._props.set(name, config
|
|
20
|
+
this._props.set(name, defaultOf(config));
|
|
11
21
|
}
|
|
12
22
|
}
|
|
13
23
|
}
|
|
@@ -43,7 +53,7 @@ if (config.coerce) {
|
|
|
43
53
|
return config.coerce(value);
|
|
44
54
|
}
|
|
45
55
|
if (value === null || value === undefined) {
|
|
46
|
-
return config
|
|
56
|
+
return defaultOf(config) ?? null;
|
|
47
57
|
}
|
|
48
58
|
switch (config.type) {
|
|
49
59
|
case 'boolean':
|
|
@@ -58,7 +68,7 @@ return true;
|
|
|
58
68
|
return Boolean(value);
|
|
59
69
|
case 'number':
|
|
60
70
|
const num = Number(value);
|
|
61
|
-
return isNaN(num) ? (config
|
|
71
|
+
return isNaN(num) ? (defaultOf(config) ?? 0) : num;
|
|
62
72
|
case 'string':
|
|
63
73
|
return String(value);
|
|
64
74
|
case 'object':
|
|
@@ -69,7 +79,7 @@ return JSON.parse(value);
|
|
|
69
79
|
}
|
|
70
80
|
catch {
|
|
71
81
|
console.warn(`[PropertyManager] Failed to parse ${name}:`, value);
|
|
72
|
-
return config
|
|
82
|
+
return defaultOf(config) ?? (config.type === 'array' ? [] : {});
|
|
73
83
|
}
|
|
74
84
|
}
|
|
75
85
|
return value;
|
|
@@ -118,6 +128,8 @@ return this._config[name]?.emitChange ?? false;
|
|
|
118
128
|
handleAlias(aliasName, value) {
|
|
119
129
|
for (const [propName, config] of Object.entries(this._config)) {
|
|
120
130
|
if (config.aliases && aliasName in config.aliases) {
|
|
131
|
+
if (value === null)
|
|
132
|
+
return this.updateProperty(propName, defaultOf(config), 'attribute');
|
|
121
133
|
const aliasedValue = config.aliases[aliasName];
|
|
122
134
|
return this.updateProperty(propName, aliasedValue, 'attribute');
|
|
123
135
|
}
|
|
@@ -1,9 +1,15 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Page scroll lock, reference-counted so multiple components can lock at once.
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
4
|
+
* Two parts: overflow: hidden on <html> (the document scroller), and a guard
|
|
5
|
+
* for apps that scroll their own container (a full-height layout with an
|
|
6
|
+
* inner overflow: auto div), which <html> overflow doesn't reach. The guard
|
|
7
|
+
* cancels wheel, touchmove and scroll keys that would scroll anything outside
|
|
8
|
+
* the open dialog they come from (every lock holder is a top-layer <dialog>):
|
|
9
|
+
* scrollers inside the dialog keep working until their edge, nothing chains
|
|
10
|
+
* out of it, and events from the page behind scroll nothing. Keys a control
|
|
11
|
+
* uses itself (text fields, selects, Space on buttons, arrows in widgets) are
|
|
12
|
+
* left alone.
|
|
7
13
|
*/
|
|
8
14
|
declare global {
|
|
9
15
|
interface Window {
|
package/lib/utils/scroll-lock.js
CHANGED
|
@@ -3,6 +3,7 @@ locked: false,
|
|
|
3
3
|
scrollY: 0,
|
|
4
4
|
activeLocks: new Set(),
|
|
5
5
|
};
|
|
6
|
+
let savedStyle = { overflow: '', paddingRight: '' };
|
|
6
7
|
function getBodyEl() {
|
|
7
8
|
return document.body;
|
|
8
9
|
}
|
|
@@ -15,6 +16,102 @@ return window.scrollY || getDocEl().scrollTop || 0;
|
|
|
15
16
|
function getScrollbarWidth() {
|
|
16
17
|
return window.innerWidth - getDocEl().clientWidth;
|
|
17
18
|
}
|
|
19
|
+
const KEY_SCROLL = {
|
|
20
|
+
ArrowDown: ['y', 1], ArrowUp: ['y', -1], PageDown: ['y', 1], PageUp: ['y', -1],
|
|
21
|
+
End: ['y', 1], Home: ['y', -1], ' ': ['y', 1], ArrowRight: ['x', 1], ArrowLeft: ['x', -1],
|
|
22
|
+
};
|
|
23
|
+
const TEXT_INPUT = /^(?:text|search|email|url|tel|password|number|date|datetime-local|month|week|time|range|color)$/;
|
|
24
|
+
function keyBelongsTo(el, key) {
|
|
25
|
+
if (el instanceof HTMLTextAreaElement || el instanceof HTMLSelectElement)
|
|
26
|
+
return true;
|
|
27
|
+
if (el instanceof HTMLElement && el.isContentEditable)
|
|
28
|
+
return true;
|
|
29
|
+
if (el instanceof HTMLInputElement) {
|
|
30
|
+
if (TEXT_INPUT.test(el.type))
|
|
31
|
+
return true;
|
|
32
|
+
if (el.type === 'radio' && key.startsWith('Arrow'))
|
|
33
|
+
return true;
|
|
34
|
+
}
|
|
35
|
+
if (key === ' ' && el.matches('button, input, summary, a[href], [role]'))
|
|
36
|
+
return true;
|
|
37
|
+
if (key !== ' ' && el.matches('[role]:not([role=dialog]):not([role=document]):not([role=presentation]):not([role=none])'))
|
|
38
|
+
return true;
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
function canScroll(el, axis, dir) {
|
|
42
|
+
const style = getComputedStyle(el);
|
|
43
|
+
const overflow = axis === 'y' ? style.overflowY : style.overflowX;
|
|
44
|
+
if (!/^(?:auto|scroll|overlay)$/.test(overflow) && !(el instanceof HTMLTextAreaElement))
|
|
45
|
+
return false;
|
|
46
|
+
const pos = axis === 'y' ? el.scrollTop : Math.abs(el.scrollLeft);
|
|
47
|
+
const max = axis === 'y' ? el.scrollHeight - el.clientHeight : el.scrollWidth - el.clientWidth;
|
|
48
|
+
if (max < 1)
|
|
49
|
+
return false;
|
|
50
|
+
return dir > 0 ? pos < max - 1 : pos > 0;
|
|
51
|
+
}
|
|
52
|
+
const HAS_POPOVER = typeof HTMLElement !== 'undefined' && 'popover' in HTMLElement.prototype;
|
|
53
|
+
function isOpenPopover(el) {
|
|
54
|
+
return HAS_POPOVER && el.hasAttribute('popover') && el.matches(':popover-open');
|
|
55
|
+
}
|
|
56
|
+
function scrollsInside(event, axis, dir) {
|
|
57
|
+
if (!dir)
|
|
58
|
+
return true;
|
|
59
|
+
const path = event.composedPath().filter((n) => n instanceof Element);
|
|
60
|
+
const root = path.findIndex((n) => (n instanceof HTMLDialogElement && n.open) || isOpenPopover(n));
|
|
61
|
+
return root >= 0 && path.slice(0, root + 1).some((n) => canScroll(n, axis, dir));
|
|
62
|
+
}
|
|
63
|
+
function guardWheel(e) {
|
|
64
|
+
if (e.ctrlKey)
|
|
65
|
+
return;
|
|
66
|
+
const axis = Math.abs(e.deltaX) > Math.abs(e.deltaY) || e.shiftKey ? 'x' : 'y';
|
|
67
|
+
const delta = axis === 'y' && !e.shiftKey ? e.deltaY : e.deltaX || e.deltaY;
|
|
68
|
+
if (e.cancelable && !scrollsInside(e, axis, Math.sign(delta)))
|
|
69
|
+
e.preventDefault();
|
|
70
|
+
}
|
|
71
|
+
let touchX = 0;
|
|
72
|
+
let touchY = 0;
|
|
73
|
+
function guardTouchStart(e) {
|
|
74
|
+
if (e.touches.length !== 1)
|
|
75
|
+
return;
|
|
76
|
+
touchX = e.touches[0].clientX;
|
|
77
|
+
touchY = e.touches[0].clientY;
|
|
78
|
+
}
|
|
79
|
+
function guardTouchMove(e) {
|
|
80
|
+
if (e.touches.length !== 1 || !e.cancelable)
|
|
81
|
+
return;
|
|
82
|
+
const { clientX, clientY } = e.touches[0];
|
|
83
|
+
const dx = touchX - clientX;
|
|
84
|
+
const dy = touchY - clientY;
|
|
85
|
+
touchX = clientX;
|
|
86
|
+
touchY = clientY;
|
|
87
|
+
const axis = Math.abs(dx) > Math.abs(dy) ? 'x' : 'y';
|
|
88
|
+
if (!scrollsInside(e, axis, Math.sign(axis === 'y' ? dy : dx)))
|
|
89
|
+
e.preventDefault();
|
|
90
|
+
}
|
|
91
|
+
function guardKey(e) {
|
|
92
|
+
const scroll = KEY_SCROLL[e.key];
|
|
93
|
+
if (!scroll || e.defaultPrevented || e.altKey || e.ctrlKey || e.metaKey)
|
|
94
|
+
return;
|
|
95
|
+
const target = e.composedPath()[0];
|
|
96
|
+
if (target instanceof Element && keyBelongsTo(target, e.key))
|
|
97
|
+
return;
|
|
98
|
+
const [axis, dir] = scroll;
|
|
99
|
+
if (!scrollsInside(e, axis, e.key === ' ' && e.shiftKey ? -dir : dir))
|
|
100
|
+
e.preventDefault();
|
|
101
|
+
}
|
|
102
|
+
const WHEEL_OPTS = { capture: true, passive: false };
|
|
103
|
+
function addGuard() {
|
|
104
|
+
window.addEventListener('wheel', guardWheel, WHEEL_OPTS);
|
|
105
|
+
window.addEventListener('touchstart', guardTouchStart, { capture: true, passive: true });
|
|
106
|
+
window.addEventListener('touchmove', guardTouchMove, WHEEL_OPTS);
|
|
107
|
+
window.addEventListener('keydown', guardKey, true);
|
|
108
|
+
}
|
|
109
|
+
function removeGuard() {
|
|
110
|
+
window.removeEventListener('wheel', guardWheel, WHEEL_OPTS);
|
|
111
|
+
window.removeEventListener('touchstart', guardTouchStart, { capture: true });
|
|
112
|
+
window.removeEventListener('touchmove', guardTouchMove, WHEEL_OPTS);
|
|
113
|
+
window.removeEventListener('keydown', guardKey, true);
|
|
114
|
+
}
|
|
18
115
|
function lockBodyFixed() {
|
|
19
116
|
if (lockState.locked)
|
|
20
117
|
return;
|
|
@@ -25,11 +122,13 @@ console.log('🔒 Locking body scroll:', { scrollbarWidth });
|
|
|
25
122
|
}
|
|
26
123
|
lockState.scrollY = getScrollY();
|
|
27
124
|
lockState.locked = true;
|
|
125
|
+
savedStyle = { overflow: docEl.style.overflow, paddingRight: docEl.style.paddingRight };
|
|
28
126
|
docEl.style.overflow = 'hidden';
|
|
29
127
|
if (scrollbarWidth > 0) {
|
|
30
128
|
docEl.style.paddingRight = `${scrollbarWidth}px`;
|
|
31
129
|
}
|
|
32
130
|
docEl.classList.add('ty-scroll-locked');
|
|
131
|
+
addGuard();
|
|
33
132
|
}
|
|
34
133
|
function unlockBodyFixed() {
|
|
35
134
|
if (!lockState.locked)
|
|
@@ -38,9 +137,10 @@ const docEl = getDocEl();
|
|
|
38
137
|
if (window.tyScrollDebug) {
|
|
39
138
|
console.log('🔓 Unlocking body scroll');
|
|
40
139
|
}
|
|
140
|
+
removeGuard();
|
|
41
141
|
docEl.classList.remove('ty-scroll-locked');
|
|
42
|
-
docEl.style.overflow =
|
|
43
|
-
docEl.style.paddingRight =
|
|
142
|
+
docEl.style.overflow = savedStyle.overflow;
|
|
143
|
+
docEl.style.paddingRight = savedStyle.paddingRight;
|
|
44
144
|
lockState.locked = false;
|
|
45
145
|
}
|
|
46
146
|
export function lockScroll(componentId) {
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* For a component whose attributeChangedCallback assigns through its own
|
|
3
|
+
* accessors: call it in the constructor. While an element upgrades, the
|
|
4
|
+
* attribute callbacks run before connectedCallback and would overwrite the
|
|
5
|
+
* early values (an own property takes the assignment); taken off here, they
|
|
6
|
+
* reach the setters in upgradeProperties() instead, after the attributes.
|
|
7
|
+
*/
|
|
8
|
+
export declare function liftProperties(el: HTMLElement): void;
|
|
9
|
+
/**
|
|
10
|
+
* Properties set before the element upgraded (a framework rendering it
|
|
11
|
+
* before its definition loaded, a lazily defined element) are own data
|
|
12
|
+
* properties that shadow the class accessors, so later assignments would
|
|
13
|
+
* never reach a setter. Standard "upgrade property" pattern: read the own
|
|
14
|
+
* value, delete it, assign it again through the accessor. Covers every
|
|
15
|
+
* settable accessor on the prototype chain (value, checked, open, error,
|
|
16
|
+
* minLength, …). TyComponent does this itself; a component extending
|
|
17
|
+
* HTMLElement directly calls it at the start of connectedCallback.
|
|
18
|
+
* A value its setter rejects is reported, not thrown: connectedCallback
|
|
19
|
+
* goes on, as it would for an attribute with a bad value.
|
|
20
|
+
*/
|
|
21
|
+
export declare function upgradeProperties(el: HTMLElement): void;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
const accessorCache = new WeakMap();
|
|
2
|
+
function settableAccessors(ctor) {
|
|
3
|
+
let names = accessorCache.get(ctor);
|
|
4
|
+
if (!names) {
|
|
5
|
+
const set = new Set();
|
|
6
|
+
for (let proto = ctor.prototype; proto && proto !== HTMLElement.prototype; proto = Object.getPrototypeOf(proto)) {
|
|
7
|
+
for (const [name, d] of Object.entries(Object.getOwnPropertyDescriptors(proto))) {
|
|
8
|
+
if (d.set)
|
|
9
|
+
set.add(name);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
names = [...set];
|
|
13
|
+
accessorCache.set(ctor, names);
|
|
14
|
+
}
|
|
15
|
+
return names;
|
|
16
|
+
}
|
|
17
|
+
const lifted = new WeakMap();
|
|
18
|
+
function takeOwn(el) {
|
|
19
|
+
const taken = [];
|
|
20
|
+
for (const name of settableAccessors(el.constructor)) {
|
|
21
|
+
if (!Object.prototype.hasOwnProperty.call(el, name))
|
|
22
|
+
continue;
|
|
23
|
+
taken.push([name, el[name]]);
|
|
24
|
+
delete el[name];
|
|
25
|
+
}
|
|
26
|
+
return taken;
|
|
27
|
+
}
|
|
28
|
+
export function liftProperties(el) {
|
|
29
|
+
const taken = takeOwn(el);
|
|
30
|
+
if (taken.length)
|
|
31
|
+
lifted.set(el, taken);
|
|
32
|
+
}
|
|
33
|
+
export function upgradeProperties(el) {
|
|
34
|
+
const early = [...(lifted.get(el) ?? []), ...takeOwn(el)];
|
|
35
|
+
lifted.delete(el);
|
|
36
|
+
for (const [name, value] of early) {
|
|
37
|
+
if (value === undefined)
|
|
38
|
+
continue;
|
|
39
|
+
try {
|
|
40
|
+
;
|
|
41
|
+
el[name] = value;
|
|
42
|
+
}
|
|
43
|
+
catch (e) {
|
|
44
|
+
reportError(e);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Constraint validation for form-associated ty components.
|
|
3
|
+
*
|
|
4
|
+
* Components report their own state (e.g. valueMissing) through set(); users
|
|
5
|
+
* add a custom error through setCustomValidity(). Both are kept separately and
|
|
6
|
+
* merged into ElementInternals, so a component re-validating on the next
|
|
7
|
+
* keystroke doesn't wipe the user's custom error (same as native inputs).
|
|
8
|
+
*
|
|
9
|
+
* reportValidity() uses the component's own `error` display when it has one
|
|
10
|
+
* (ty-input, ty-textarea, ty-radio-group, ty-file-upload) instead of the
|
|
11
|
+
* browser's bubble, and clears that message again once the field is valid.
|
|
12
|
+
* Whenever the browser fires `invalid` on such a field — a submit attempt,
|
|
13
|
+
* form.reportValidity(), or checkValidity() on the field or its form — the
|
|
14
|
+
* message goes into that error line (no bubble), so a plain <form> needs no
|
|
15
|
+
* JS. For a check that shows nothing, read `field.validity.valid` or
|
|
16
|
+
* `form.matches(':invalid')`. A submit (a submitter click, Enter in a
|
|
17
|
+
* single-line field, form.requestSubmit()) and form.reportValidity() also
|
|
18
|
+
* move focus to the first failing field — the first in form order, like a
|
|
19
|
+
* native form, also when a later field shows a bubble. checkValidity() never
|
|
20
|
+
* does. For the script-run calls, a form holding a Ty field with an error
|
|
21
|
+
* line gets own requestSubmit / reportValidity wrappers (the prototype and
|
|
22
|
+
* other forms are left alone); they come off when the last such field leaves.
|
|
23
|
+
* An `error` the author set is never overwritten or removed;
|
|
24
|
+
* form.reset() clears every message the controller wrote.
|
|
25
|
+
*/
|
|
26
|
+
/**
|
|
27
|
+
* Run `fn` as a submit attempt: `invalid` events during it move focus to the
|
|
28
|
+
* first failing field. ty-button runs its form.requestSubmit() in this.
|
|
29
|
+
*/
|
|
30
|
+
export declare function asSubmitAttempt<T>(fn: () => T): T;
|
|
31
|
+
export declare class ValidityController {
|
|
32
|
+
private host;
|
|
33
|
+
private internals;
|
|
34
|
+
private own;
|
|
35
|
+
private custom;
|
|
36
|
+
/** The message this controller last wrote into `error`. */
|
|
37
|
+
private written;
|
|
38
|
+
/** The anchor last handed to setValidity(). */
|
|
39
|
+
private anchor?;
|
|
40
|
+
/**
|
|
41
|
+
* Anchors a validation bubble was shown on, the last two. WebKit keeps a
|
|
42
|
+
* pointer to that anchor without keeping it alive: once a re-render drops
|
|
43
|
+
* it (ty-calendar rebuilds its grid) and it is garbage-collected, the next
|
|
44
|
+
* submit crashes the page. Held until two later bubbles replaced it.
|
|
45
|
+
*/
|
|
46
|
+
private shown;
|
|
47
|
+
/** The form whose requestSubmit / reportValidity this field keeps wrapped. */
|
|
48
|
+
private form;
|
|
49
|
+
constructor(host: HTMLElement, internals: ElementInternals);
|
|
50
|
+
/**
|
|
51
|
+
* The host connected, disconnected or changed form: keep its form's
|
|
52
|
+
* requestSubmit / reportValidity wrapped while it (or another Ty field with
|
|
53
|
+
* an error line) is in it. Only such a field needs it: a native control or
|
|
54
|
+
* a bubble field is focused by the browser itself.
|
|
55
|
+
*/
|
|
56
|
+
track(): void;
|
|
57
|
+
/**
|
|
58
|
+
* Show the current message, if any, in the error display (on blur after an
|
|
59
|
+
* edit). During a pointer interaction it waits until after the click.
|
|
60
|
+
*/
|
|
61
|
+
reveal(): void;
|
|
62
|
+
/**
|
|
63
|
+
* `error` still holds what this controller wrote (a reportValidity(), a
|
|
64
|
+
* submit, a blur reveal), so it's ours to update and clear. Once the author
|
|
65
|
+
* sets their own message, it's theirs.
|
|
66
|
+
*/
|
|
67
|
+
private get errorFromReport();
|
|
68
|
+
private writeError;
|
|
69
|
+
/** Into `error`, unless the author's own message is there. */
|
|
70
|
+
private showError;
|
|
71
|
+
/**
|
|
72
|
+
* form.reset(): drop the message this controller showed and any blur
|
|
73
|
+
* reveal still waiting, so the field looks untouched again. An `error` the
|
|
74
|
+
* author set stays.
|
|
75
|
+
*/
|
|
76
|
+
reset(): void;
|
|
77
|
+
/** The component's own validity (replaces the previous one). */
|
|
78
|
+
set(flags: ValidityStateFlags, message?: string, anchor?: HTMLElement): void;
|
|
79
|
+
setCustom(message: string): void;
|
|
80
|
+
report(): boolean;
|
|
81
|
+
private sync;
|
|
82
|
+
}
|
|
83
|
+
type ProbeTag = 'input' | 'textarea' | 'select';
|
|
84
|
+
/**
|
|
85
|
+
* The browser's own (localized) message for a rule: a detached native control
|
|
86
|
+
* gets the same attributes and value and reports what it would say. `true`
|
|
87
|
+
* sets a boolean attribute; '', false and null leave the attribute off. One
|
|
88
|
+
* probe per tag, never in the document, stripped of every attribute before
|
|
89
|
+
* each use. Works for required and min/max; length rules only report after a
|
|
90
|
+
* user edit, so a probe can't word them (see lengthValidity()).
|
|
91
|
+
*/
|
|
92
|
+
export declare function nativeMessage(tag: ProbeTag, attrs: Record<string, string | boolean | null | undefined>, value?: string): string;
|
|
93
|
+
/**
|
|
94
|
+
* nativeMessage() plus the probe's own flags, for rules whose attribute the
|
|
95
|
+
* browser parses (min/max: ' 5 ' or '0x10' is no rule at all), so the flag
|
|
96
|
+
* and its message can't disagree. Read `validity` right away: the probe is
|
|
97
|
+
* reused by the next call.
|
|
98
|
+
*/
|
|
99
|
+
export declare function nativeValidity(tag: ProbeTag, attrs: Record<string, string | boolean | null | undefined>, value?: string): {
|
|
100
|
+
validity: ValidityState;
|
|
101
|
+
message: string;
|
|
102
|
+
};
|
|
103
|
+
/** Non-negative integer limit from an attribute string, or null when unset/invalid. */
|
|
104
|
+
export declare function toLimit(v: unknown): number | null;
|
|
105
|
+
/**
|
|
106
|
+
* minlength / maxlength for a text field (ty-input, ty-textarea): the failing
|
|
107
|
+
* flags with their messages. `control` is the inner native control carrying
|
|
108
|
+
* `minlength`; it only flags (and words, localized) a value the user typed,
|
|
109
|
+
* so its message is used then. Otherwise Ty's wording, and the author's
|
|
110
|
+
* too-short / too-long message always wins. Placeholders: {minlength}
|
|
111
|
+
* {maxlength} {length}.
|
|
112
|
+
*/
|
|
113
|
+
export declare function lengthValidity(value: string, rules: {
|
|
114
|
+
minlength: unknown;
|
|
115
|
+
maxlength: unknown;
|
|
116
|
+
tooShortMessage: string;
|
|
117
|
+
tooLongMessage: string;
|
|
118
|
+
}, control?: HTMLInputElement | HTMLTextAreaElement | null): {
|
|
119
|
+
tooShort?: string;
|
|
120
|
+
tooLong?: string;
|
|
121
|
+
};
|
|
122
|
+
export {};
|