@oscarpalmer/toretto 0.48.0 → 0.49.1
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/dist/aria.d.mts +36 -11
- package/dist/aria.mjs +35 -3
- package/dist/create.d.mts +14 -10
- package/dist/create.mjs +20 -4
- package/dist/data.d.mts +22 -6
- package/dist/data.mjs +9 -9
- package/dist/index.d.mts +108 -39
- package/dist/index.mjs +108 -59
- package/dist/internal/attribute.mjs +1 -1
- package/dist/internal/element-value.d.mts +1 -1
- package/dist/internal/element-value.mjs +6 -2
- package/dist/models.d.mts +31 -3
- package/dist/style.d.mts +9 -12
- package/dist/style.mjs +19 -19
- package/package.json +3 -3
- package/src/aria.ts +100 -26
- package/src/create.ts +45 -30
- package/src/data.ts +41 -21
- package/src/internal/attribute.ts +0 -1
- package/src/internal/element-value.ts +9 -2
- package/src/models.ts +57 -2
- package/src/style.ts +30 -41
package/src/style.ts
CHANGED
|
@@ -1,12 +1,10 @@
|
|
|
1
1
|
import {getString} from '@oscarpalmer/atoms/string';
|
|
2
2
|
import {setElementValues, updateElementValue} from './internal/element-value';
|
|
3
3
|
import {getStyleValue} from './internal/get-value';
|
|
4
|
-
import type {TextDirection} from './models';
|
|
4
|
+
import type {CSSValues, TextDirection} from './models';
|
|
5
5
|
|
|
6
6
|
// #region Types
|
|
7
7
|
|
|
8
|
-
type CSSStyleValues = Variables & CSSStyleDeclaration;
|
|
9
|
-
|
|
10
8
|
export type StyleToggler = {
|
|
11
9
|
/**
|
|
12
10
|
* Set the provided styles on the element
|
|
@@ -18,14 +16,6 @@ export type StyleToggler = {
|
|
|
18
16
|
remove(): void;
|
|
19
17
|
};
|
|
20
18
|
|
|
21
|
-
type Styles = Partial<Record<keyof CSSStyleValues, unknown>>;
|
|
22
|
-
|
|
23
|
-
type Variables<
|
|
24
|
-
Value extends Record<string, string | undefined> = Record<string, string | undefined>,
|
|
25
|
-
> = {
|
|
26
|
-
[property in keyof Value as `--${string & property}`]?: string | undefined;
|
|
27
|
-
};
|
|
28
|
-
|
|
29
19
|
// #endregion
|
|
30
20
|
|
|
31
21
|
// #region Functions
|
|
@@ -34,17 +24,17 @@ type Variables<
|
|
|
34
24
|
* Get a style from an element
|
|
35
25
|
*
|
|
36
26
|
* @param element Element to get the style from
|
|
37
|
-
* @param
|
|
27
|
+
* @param name Style name
|
|
38
28
|
* @param computed Get the computed style? _(defaults to `false`)_
|
|
39
29
|
* @returns Style value
|
|
40
30
|
*/
|
|
41
31
|
export function getStyle(
|
|
42
32
|
element: Element,
|
|
43
|
-
|
|
33
|
+
name: keyof CSSValues,
|
|
44
34
|
computed?: boolean,
|
|
45
35
|
): string | undefined {
|
|
46
|
-
if (element instanceof Element && typeof
|
|
47
|
-
return getStyleValue(element,
|
|
36
|
+
if (element instanceof Element && typeof name === 'string') {
|
|
37
|
+
return getStyleValue(element, name, computed === true);
|
|
48
38
|
}
|
|
49
39
|
}
|
|
50
40
|
|
|
@@ -52,28 +42,28 @@ export function getStyle(
|
|
|
52
42
|
* Get styles from an element
|
|
53
43
|
*
|
|
54
44
|
* @param element Element to get the styles from
|
|
55
|
-
* @param
|
|
45
|
+
* @param names Styles to get
|
|
56
46
|
* @param computed Get the computed styles? _(defaults to `false`)_
|
|
57
47
|
* @returns Style values
|
|
58
48
|
*/
|
|
59
|
-
export function getStyles<
|
|
49
|
+
export function getStyles<Name extends keyof CSSValues>(
|
|
60
50
|
element: Element,
|
|
61
|
-
|
|
51
|
+
names: Name[],
|
|
62
52
|
computed?: boolean,
|
|
63
|
-
): Record<
|
|
64
|
-
const styles = {} as Record<
|
|
53
|
+
): Record<Name, string | undefined> {
|
|
54
|
+
const styles = {} as Record<Name, string | undefined>;
|
|
65
55
|
|
|
66
|
-
if (!(element instanceof Element && Array.isArray(
|
|
56
|
+
if (!(element instanceof Element && Array.isArray(names))) {
|
|
67
57
|
return styles;
|
|
68
58
|
}
|
|
69
59
|
|
|
70
|
-
const {length} =
|
|
60
|
+
const {length} = names;
|
|
71
61
|
|
|
72
62
|
for (let index = 0; index < length; index += 1) {
|
|
73
|
-
const
|
|
63
|
+
const name = names[index];
|
|
74
64
|
|
|
75
|
-
if (typeof
|
|
76
|
-
styles[
|
|
65
|
+
if (typeof name === 'string') {
|
|
66
|
+
styles[name] = getStyleValue(element, name, computed === true) as never;
|
|
77
67
|
}
|
|
78
68
|
}
|
|
79
69
|
|
|
@@ -120,11 +110,11 @@ export function getTextDirection(node?: Element | Node): TextDirection {
|
|
|
120
110
|
* Set a style on an element
|
|
121
111
|
*
|
|
122
112
|
* @param element Element to set the style on
|
|
123
|
-
* @param
|
|
113
|
+
* @param name Style name
|
|
124
114
|
* @param value Style value
|
|
125
115
|
*/
|
|
126
|
-
export function setStyle(element: Element,
|
|
127
|
-
setElementValues(element,
|
|
116
|
+
export function setStyle(element: Element, name: keyof CSSValues, value?: unknown): void {
|
|
117
|
+
setElementValues(element, name as string, value, null, updateStyleProperty, true);
|
|
128
118
|
}
|
|
129
119
|
|
|
130
120
|
/**
|
|
@@ -133,7 +123,7 @@ export function setStyle(element: Element, property: keyof CSSStyleValues, value
|
|
|
133
123
|
* @param element Element to set the styles on
|
|
134
124
|
* @param styles Styles to set
|
|
135
125
|
*/
|
|
136
|
-
export function setStyles(element: Element, styles:
|
|
126
|
+
export function setStyles(element: Element, styles: Partial<CSSValues>): void {
|
|
137
127
|
setElementValues(element, styles as never, null, null, updateStyleProperty, true);
|
|
138
128
|
}
|
|
139
129
|
|
|
@@ -144,11 +134,11 @@ export function setStyles(element: Element, styles: Styles): void {
|
|
|
144
134
|
* @param styles Styles to be set or removed
|
|
145
135
|
* @returns Style toggler
|
|
146
136
|
*/
|
|
147
|
-
export function toggleStyles(element: Element, styles:
|
|
137
|
+
export function toggleStyles(element: Element, styles: Partial<CSSValues>): StyleToggler {
|
|
148
138
|
function toggle(set: boolean): void {
|
|
149
139
|
hasSet = set;
|
|
150
140
|
|
|
151
|
-
let next:
|
|
141
|
+
let next: Partial<CSSValues>;
|
|
152
142
|
|
|
153
143
|
if (set) {
|
|
154
144
|
values = getStyles(element, keys);
|
|
@@ -171,7 +161,7 @@ export function toggleStyles(element: Element, styles: Styles): StyleToggler {
|
|
|
171
161
|
const {length} = keys;
|
|
172
162
|
|
|
173
163
|
let hasSet = false;
|
|
174
|
-
let values: Record<string,
|
|
164
|
+
let values: Record<string, unknown> = {};
|
|
175
165
|
|
|
176
166
|
return {
|
|
177
167
|
set(): void {
|
|
@@ -192,18 +182,18 @@ function updateStyleProperty(element: Element, key: string, value: unknown): voi
|
|
|
192
182
|
element,
|
|
193
183
|
key,
|
|
194
184
|
value,
|
|
195
|
-
function (this: Element,
|
|
196
|
-
if (
|
|
197
|
-
(this as HTMLElement).style.setProperty(
|
|
185
|
+
function (this: Element, name: string, style: unknown) {
|
|
186
|
+
if (name.startsWith(VARIABLE_PREFIX)) {
|
|
187
|
+
(this as HTMLElement).style.setProperty(name, getString(style));
|
|
198
188
|
} else {
|
|
199
|
-
(this as HTMLElement).style[
|
|
189
|
+
(this as HTMLElement).style[name as never] = getString(style);
|
|
200
190
|
}
|
|
201
191
|
},
|
|
202
|
-
function (this: Element,
|
|
203
|
-
if (
|
|
204
|
-
(this as HTMLElement).style.removeProperty(
|
|
192
|
+
function (this: Element, name: string) {
|
|
193
|
+
if (name.startsWith(VARIABLE_PREFIX)) {
|
|
194
|
+
(this as HTMLElement).style.removeProperty(name);
|
|
205
195
|
} else {
|
|
206
|
-
(this as HTMLElement).style[
|
|
196
|
+
(this as HTMLElement).style[name as never] = '';
|
|
207
197
|
}
|
|
208
198
|
|
|
209
199
|
if ((this as HTMLElement).getAttribute(ATTRIBUTE_STYLE) === '') {
|
|
@@ -211,7 +201,6 @@ function updateStyleProperty(element: Element, key: string, value: unknown): voi
|
|
|
211
201
|
}
|
|
212
202
|
},
|
|
213
203
|
false,
|
|
214
|
-
false,
|
|
215
204
|
);
|
|
216
205
|
}
|
|
217
206
|
|