@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/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 property Style name
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
- property: keyof CSSStyleValues,
33
+ name: keyof CSSValues,
44
34
  computed?: boolean,
45
35
  ): string | undefined {
46
- if (element instanceof Element && typeof property === 'string') {
47
- return getStyleValue(element, property, computed === true);
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 properties Styles to get
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<Property extends keyof CSSStyleValues>(
49
+ export function getStyles<Name extends keyof CSSValues>(
60
50
  element: Element,
61
- properties: Property[],
51
+ names: Name[],
62
52
  computed?: boolean,
63
- ): Record<Property, string | undefined> {
64
- const styles = {} as Record<Property, string | undefined>;
53
+ ): Record<Name, string | undefined> {
54
+ const styles = {} as Record<Name, string | undefined>;
65
55
 
66
- if (!(element instanceof Element && Array.isArray(properties))) {
56
+ if (!(element instanceof Element && Array.isArray(names))) {
67
57
  return styles;
68
58
  }
69
59
 
70
- const {length} = properties;
60
+ const {length} = names;
71
61
 
72
62
  for (let index = 0; index < length; index += 1) {
73
- const property = properties[index];
63
+ const name = names[index];
74
64
 
75
- if (typeof property === 'string') {
76
- styles[property] = getStyleValue(element, property, computed === true) as never;
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 property Style name
113
+ * @param name Style name
124
114
  * @param value Style value
125
115
  */
126
- export function setStyle(element: Element, property: keyof CSSStyleValues, value?: unknown): void {
127
- setElementValues(element, property as string, value, null, updateStyleProperty, true);
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: Styles): void {
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: Styles): StyleToggler {
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: Styles;
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, string | undefined> = {};
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, property: string, style: unknown) {
196
- if (property.startsWith(VARIABLE_PREFIX)) {
197
- (this as HTMLElement).style.setProperty(property, getString(style));
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[property as never] = getString(style);
189
+ (this as HTMLElement).style[name as never] = getString(style);
200
190
  }
201
191
  },
202
- function (this: Element, property: string) {
203
- if (property.startsWith(VARIABLE_PREFIX)) {
204
- (this as HTMLElement).style.removeProperty(property);
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[property as never] = '';
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