@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/dist/index.mjs
CHANGED
|
@@ -528,7 +528,7 @@ function updateAttribute(element, name, value, dispatch) {
|
|
|
528
528
|
const isBoolean = booleanAttributesSet.has(lowerCaseName);
|
|
529
529
|
const next = isBoolean ? value === true || typeof value === "string" && (value === "" || value.toLowerCase() === lowerCaseName) : value == null ? "" : value;
|
|
530
530
|
if (isBoolean || dispatchedAttributes.has(name)) updateProperty(element, name, next, dispatch);
|
|
531
|
-
updateElementValue(element, name, isBoolean ? next ? "" : null : value, element.setAttribute, element.removeAttribute,
|
|
531
|
+
updateElementValue(element, name, isBoolean ? next ? "" : null : value, element.setAttribute, element.removeAttribute, false);
|
|
532
532
|
}
|
|
533
533
|
const EXPRESSION_CLOBBERED_NAME = /^(id|name)$/i;
|
|
534
534
|
const EXPRESSION_DATA_OR_SCRIPT = /^(?:data|\w+script):/i;
|
|
@@ -577,6 +577,10 @@ const formElement = document.createElement("form");
|
|
|
577
577
|
let textArea;
|
|
578
578
|
//#endregion
|
|
579
579
|
//#region src/internal/element-value.ts
|
|
580
|
+
function getValueForAttribute(value, json) {
|
|
581
|
+
if (typeof value === "string") return value;
|
|
582
|
+
return json ? JSON.stringify(value) : getString(value);
|
|
583
|
+
}
|
|
580
584
|
function ignoreSetAttribute(element, name) {
|
|
581
585
|
if (element instanceof HTMLTextAreaElement && name === "value") return true;
|
|
582
586
|
return false;
|
|
@@ -608,9 +612,9 @@ function setElementValues(element, first, second, third, callback, style) {
|
|
|
608
612
|
if (typeof entry === "object" && typeof entry?.name === "string") callback(element, normalizeKey(entry.name, style), entry.value, dispatch);
|
|
609
613
|
}
|
|
610
614
|
}
|
|
611
|
-
function updateElementValue(element, key, value, set, remove,
|
|
615
|
+
function updateElementValue(element, key, value, set, remove, json) {
|
|
612
616
|
if (value == null) remove.call(element, key);
|
|
613
|
-
else if (!ignoreSetAttribute(element, key)) set.call(element, key,
|
|
617
|
+
else if (!ignoreSetAttribute(element, key)) set.call(element, key, getValueForAttribute(value, json));
|
|
614
618
|
}
|
|
615
619
|
const CSS_VARIABLE_PREFIX$1 = "--";
|
|
616
620
|
//#endregion
|
|
@@ -627,7 +631,10 @@ function getAria(element, value) {
|
|
|
627
631
|
return arias;
|
|
628
632
|
}
|
|
629
633
|
function getAriaValue(element, attribute) {
|
|
630
|
-
|
|
634
|
+
const name = getName$1(attribute);
|
|
635
|
+
const value = element.getAttribute(name) ?? void 0;
|
|
636
|
+
if (ariaBooleanAttributesSet.has(name) && typeof value === "string" && EXPRESSION_BOOLEAN.test(value)) return value.toLowerCase() === "true";
|
|
637
|
+
return value;
|
|
631
638
|
}
|
|
632
639
|
function getName$1(value) {
|
|
633
640
|
return EXPRESSION_ARIA_PREFIX.test(value) ? value : `${ATTRIBUTE_ARIA_PREFIX}${value}`;
|
|
@@ -656,10 +663,39 @@ function setRole(element, role) {
|
|
|
656
663
|
else element.removeAttribute("role");
|
|
657
664
|
}
|
|
658
665
|
function updateAriaAttribute(element, key, value) {
|
|
659
|
-
|
|
666
|
+
const name = getName$1(key);
|
|
667
|
+
let actual = value;
|
|
668
|
+
if (ariaBooleanAttributesSet.has(name) && typeof value === "string" && EXPRESSION_BOOLEAN.test(value)) actual = value.toLowerCase() === "true";
|
|
669
|
+
updateElementValue(element, name, actual, element.setAttribute, element.removeAttribute, false);
|
|
660
670
|
}
|
|
661
671
|
const ATTRIBUTE_ARIA_PREFIX = "aria-";
|
|
662
672
|
const EXPRESSION_ARIA_PREFIX = /^aria-/i;
|
|
673
|
+
const EXPRESSION_BOOLEAN = /^(true|false)$/i;
|
|
674
|
+
/**
|
|
675
|
+
* List of _ARIA_ attributes that can be treated as boolean values
|
|
676
|
+
*/
|
|
677
|
+
const ariaBooleanAttributes = Object.freeze([
|
|
678
|
+
"aria-atomic",
|
|
679
|
+
"aria-busy",
|
|
680
|
+
"aria-checked",
|
|
681
|
+
"aria-current",
|
|
682
|
+
"aria-disabled",
|
|
683
|
+
"aria-expanded",
|
|
684
|
+
"aria-haspopup",
|
|
685
|
+
"aria-hidden",
|
|
686
|
+
"aria-invalid",
|
|
687
|
+
"aria-modal",
|
|
688
|
+
"aria-multiline",
|
|
689
|
+
"aria-multiselectable",
|
|
690
|
+
"aria-pressed",
|
|
691
|
+
"aria-readonly",
|
|
692
|
+
"aria-required",
|
|
693
|
+
"aria-selected"
|
|
694
|
+
]);
|
|
695
|
+
/**
|
|
696
|
+
* Set of _ARIA_ attributes that can be treated as boolean values
|
|
697
|
+
*/
|
|
698
|
+
const ariaBooleanAttributesSet = new Set(ariaBooleanAttributes);
|
|
663
699
|
//#endregion
|
|
664
700
|
//#region src/internal/get-value.ts
|
|
665
701
|
function getBoolean(value, defaultValue) {
|
|
@@ -722,6 +758,36 @@ function isInvalidBooleanAttribute(first, second) {
|
|
|
722
758
|
return _isInvalidBooleanAttribute(first, second, true);
|
|
723
759
|
}
|
|
724
760
|
//#endregion
|
|
761
|
+
//#region src/data.ts
|
|
762
|
+
function getData(element, keys, parseValues) {
|
|
763
|
+
if (!(element instanceof Element)) return;
|
|
764
|
+
const noParse = parseValues === false;
|
|
765
|
+
if (typeof keys === "string") return getDataValue(element, keys, noParse);
|
|
766
|
+
const { length } = keys;
|
|
767
|
+
const data = {};
|
|
768
|
+
for (let index = 0; index < length; index += 1) {
|
|
769
|
+
const key = keys[index];
|
|
770
|
+
data[key] = getDataValue(element, key, noParse);
|
|
771
|
+
}
|
|
772
|
+
return data;
|
|
773
|
+
}
|
|
774
|
+
function getDataValue(element, key, noParse) {
|
|
775
|
+
const value = element.dataset[camelCase(key)];
|
|
776
|
+
if (value == null) return;
|
|
777
|
+
if (noParse) return value;
|
|
778
|
+
return parse(value) ?? value;
|
|
779
|
+
}
|
|
780
|
+
function getName(original) {
|
|
781
|
+
return `${ATTRIBUTE_DATA_PREFIX}${kebabCase(original.replace(EXPRESSION_DATA_PREFIX, ""))}`;
|
|
782
|
+
}
|
|
783
|
+
function setData(element, first, second) {
|
|
784
|
+
setElementValues(element, first, second, null, updateDataAttribute);
|
|
785
|
+
}
|
|
786
|
+
function updateDataAttribute(element, key, value) {
|
|
787
|
+
updateElementValue(element, getName(key), value, element.setAttribute, element.removeAttribute, true);
|
|
788
|
+
}
|
|
789
|
+
const ATTRIBUTE_DATA_PREFIX = "data-";
|
|
790
|
+
//#endregion
|
|
725
791
|
//#region src/property/get.property.ts
|
|
726
792
|
/**
|
|
727
793
|
* Get the values of one or more properties on an element
|
|
@@ -786,28 +852,28 @@ function setPropertyValue(element, property, value, dispatch) {
|
|
|
786
852
|
* Get a style from an element
|
|
787
853
|
*
|
|
788
854
|
* @param element Element to get the style from
|
|
789
|
-
* @param
|
|
855
|
+
* @param name Style name
|
|
790
856
|
* @param computed Get the computed style? _(defaults to `false`)_
|
|
791
857
|
* @returns Style value
|
|
792
858
|
*/
|
|
793
|
-
function getStyle(element,
|
|
794
|
-
if (element instanceof Element && typeof
|
|
859
|
+
function getStyle(element, name, computed) {
|
|
860
|
+
if (element instanceof Element && typeof name === "string") return getStyleValue(element, name, computed === true);
|
|
795
861
|
}
|
|
796
862
|
/**
|
|
797
863
|
* Get styles from an element
|
|
798
864
|
*
|
|
799
865
|
* @param element Element to get the styles from
|
|
800
|
-
* @param
|
|
866
|
+
* @param names Styles to get
|
|
801
867
|
* @param computed Get the computed styles? _(defaults to `false`)_
|
|
802
868
|
* @returns Style values
|
|
803
869
|
*/
|
|
804
|
-
function getStyles(element,
|
|
870
|
+
function getStyles(element, names, computed) {
|
|
805
871
|
const styles = {};
|
|
806
|
-
if (!(element instanceof Element && Array.isArray(
|
|
807
|
-
const { length } =
|
|
872
|
+
if (!(element instanceof Element && Array.isArray(names))) return styles;
|
|
873
|
+
const { length } = names;
|
|
808
874
|
for (let index = 0; index < length; index += 1) {
|
|
809
|
-
const
|
|
810
|
-
if (typeof
|
|
875
|
+
const name = names[index];
|
|
876
|
+
if (typeof name === "string") styles[name] = getStyleValue(element, name, computed === true);
|
|
811
877
|
}
|
|
812
878
|
return styles;
|
|
813
879
|
}
|
|
@@ -823,11 +889,11 @@ function getTextDirection(node) {
|
|
|
823
889
|
* Set a style on an element
|
|
824
890
|
*
|
|
825
891
|
* @param element Element to set the style on
|
|
826
|
-
* @param
|
|
892
|
+
* @param name Style name
|
|
827
893
|
* @param value Style value
|
|
828
894
|
*/
|
|
829
|
-
function setStyle(element,
|
|
830
|
-
setElementValues(element,
|
|
895
|
+
function setStyle(element, name, value) {
|
|
896
|
+
setElementValues(element, name, value, null, updateStyleProperty, true);
|
|
831
897
|
}
|
|
832
898
|
/**
|
|
833
899
|
* Set styles on an element
|
|
@@ -873,14 +939,14 @@ function toggleStyles(element, styles) {
|
|
|
873
939
|
};
|
|
874
940
|
}
|
|
875
941
|
function updateStyleProperty(element, key, value) {
|
|
876
|
-
updateElementValue(element, key, value, function(
|
|
877
|
-
if (
|
|
878
|
-
else this.style[
|
|
879
|
-
}, function(
|
|
880
|
-
if (
|
|
881
|
-
else this.style[
|
|
942
|
+
updateElementValue(element, key, value, function(name, style) {
|
|
943
|
+
if (name.startsWith(VARIABLE_PREFIX)) this.style.setProperty(name, getString(style));
|
|
944
|
+
else this.style[name] = getString(style);
|
|
945
|
+
}, function(name) {
|
|
946
|
+
if (name.startsWith(VARIABLE_PREFIX)) this.style.removeProperty(name);
|
|
947
|
+
else this.style[name] = "";
|
|
882
948
|
if (this.getAttribute(ATTRIBUTE_STYLE) === "") this.removeAttribute(ATTRIBUTE_STYLE);
|
|
883
|
-
}, false
|
|
949
|
+
}, false);
|
|
884
950
|
}
|
|
885
951
|
const ATTRIBUTE_STYLE = "style";
|
|
886
952
|
const DIRECTION_LTR = "ltr";
|
|
@@ -889,45 +955,28 @@ const PROPERTY_DIRECTION = "direction";
|
|
|
889
955
|
const VARIABLE_PREFIX = "--";
|
|
890
956
|
//#endregion
|
|
891
957
|
//#region src/create.ts
|
|
892
|
-
function createElement(tag,
|
|
958
|
+
function createElement(tag, values) {
|
|
893
959
|
if (typeof tag !== "string") throw new TypeError(MESSAGE);
|
|
894
960
|
const element = document.createElement(tag);
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
961
|
+
const { aria, attribute, data, property, style } = getElementValues(values);
|
|
962
|
+
setAria(element, aria ?? {});
|
|
963
|
+
setAttributes(element, attribute ?? {});
|
|
964
|
+
setData(element, data ?? {});
|
|
965
|
+
setProperties(element, property ?? {});
|
|
966
|
+
setStyles(element, style ?? {});
|
|
898
967
|
return element;
|
|
899
968
|
}
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
return noParse ? value : parse(value);
|
|
910
|
-
}
|
|
911
|
-
const { length } = keys;
|
|
912
|
-
const data = {};
|
|
913
|
-
for (let index = 0; index < length; index += 1) {
|
|
914
|
-
const key = keys[index];
|
|
915
|
-
const value = element.dataset[camelCase(key)];
|
|
916
|
-
if (value == null) data[key] = void 0;
|
|
917
|
-
else data[key] = noParse ? value : parse(value);
|
|
918
|
-
}
|
|
919
|
-
return data;
|
|
920
|
-
}
|
|
921
|
-
function getName(original) {
|
|
922
|
-
return `${ATTRIBUTE_DATA_PREFIX}${kebabCase(original.replace(EXPRESSION_DATA_PREFIX, ""))}`;
|
|
923
|
-
}
|
|
924
|
-
function setData(element, first, second) {
|
|
925
|
-
setElementValues(element, first, second, null, updateDataAttribute);
|
|
926
|
-
}
|
|
927
|
-
function updateDataAttribute(element, key, value) {
|
|
928
|
-
updateElementValue(element, getName(key), value, element.setAttribute, element.removeAttribute, false, true);
|
|
969
|
+
function getElementValues(input) {
|
|
970
|
+
if (!isPlainObject(input)) return {};
|
|
971
|
+
return {
|
|
972
|
+
aria: isPlainObject(input.aria) ? input.aria : void 0,
|
|
973
|
+
attribute: isPlainObject(input.attribute) ? input.attribute : void 0,
|
|
974
|
+
data: isPlainObject(input.data) ? input.data : void 0,
|
|
975
|
+
property: isPlainObject(input.property) ? input.property : void 0,
|
|
976
|
+
style: isPlainObject(input.style) ? input.style : void 0
|
|
977
|
+
};
|
|
929
978
|
}
|
|
930
|
-
const
|
|
979
|
+
const MESSAGE = "Tag name must be a string";
|
|
931
980
|
//#endregion
|
|
932
981
|
//#region node_modules/@oscarpalmer/atoms/dist/internal/function/misc.mjs
|
|
933
982
|
/**
|
|
@@ -1673,4 +1722,4 @@ const CHILD_NODE_TYPES = /* @__PURE__ */ new Set([
|
|
|
1673
1722
|
Node.DOCUMENT_TYPE_NODE
|
|
1674
1723
|
]);
|
|
1675
1724
|
//#endregion
|
|
1676
|
-
export { findElement as $, findElement, findElements as $$, findElements, booleanAttributes, createElement, dispatch, findAncestor, findRelatives, getAria, getAttribute, getAttributes, getData, getDistance, getElementFromPosition, getElementUnderPointer, getFocusable, getPosition, getProperties, getProperty, getRole, getStyle, getStyles, getTabbable, getTextDirection, html, isBadAttribute, isBooleanAttribute, isChildNode, isEventPosition, isEventTarget, isFocusable, isHTMLOrSVGElement, isInDocument, isInputElement, isInvalidBooleanAttribute, isTabbable, off, on, sanitize, setAria, setAttribute, setAttributes, setData, setProperties, setProperty, setRole, setStyle, setStyles, supportsTouch, toggleStyles };
|
|
1725
|
+
export { findElement as $, findElement, findElements as $$, findElements, ariaBooleanAttributes, ariaBooleanAttributesSet, booleanAttributes, createElement, dispatch, findAncestor, findRelatives, getAria, getAttribute, getAttributes, getData, getDistance, getElementFromPosition, getElementUnderPointer, getFocusable, getPosition, getProperties, getProperty, getRole, getStyle, getStyles, getTabbable, getTextDirection, html, isBadAttribute, isBooleanAttribute, isChildNode, isEventPosition, isEventTarget, isFocusable, isHTMLOrSVGElement, isInDocument, isInputElement, isInvalidBooleanAttribute, isTabbable, off, on, sanitize, setAria, setAttribute, setAttributes, setData, setProperties, setProperty, setRole, setStyle, setStyles, supportsTouch, toggleStyles };
|
|
@@ -56,7 +56,7 @@ function updateAttribute(element, name, value, dispatch) {
|
|
|
56
56
|
const isBoolean = booleanAttributesSet.has(lowerCaseName);
|
|
57
57
|
const next = isBoolean ? value === true || typeof value === "string" && (value === "" || value.toLowerCase() === lowerCaseName) : value == null ? "" : value;
|
|
58
58
|
if (isBoolean || dispatchedAttributes.has(name)) updateProperty(element, name, next, dispatch);
|
|
59
|
-
updateElementValue(element, name, isBoolean ? next ? "" : null : value, element.setAttribute, element.removeAttribute,
|
|
59
|
+
updateElementValue(element, name, isBoolean ? next ? "" : null : value, element.setAttribute, element.removeAttribute, false);
|
|
60
60
|
}
|
|
61
61
|
const EXPRESSION_CLOBBERED_NAME = /^(id|name)$/i;
|
|
62
62
|
const EXPRESSION_DATA_OR_SCRIPT = /^(?:data|\w+script):/i;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
//#region src/internal/element-value.d.ts
|
|
2
2
|
declare function setElementValue(element: Element, first: unknown, second: unknown, third: unknown, callback: (element: Element, key: string, value: unknown, dispatch: boolean) => void, style?: boolean): void;
|
|
3
3
|
declare function setElementValues(element: Element, first: unknown, second: unknown, third: unknown, callback: (element: Element, key: string, value: unknown, dispatch: boolean) => void, style?: boolean): void;
|
|
4
|
-
declare function updateElementValue(element: Element, key: string, value: unknown, set: (key: string, value: string) => void, remove: (key: string) => void,
|
|
4
|
+
declare function updateElementValue(element: Element, key: string, value: unknown, set: (key: string, value: string) => void, remove: (key: string) => void, json: boolean): void;
|
|
5
5
|
//#endregion
|
|
6
6
|
export { setElementValue, setElementValues, updateElementValue };
|
|
@@ -2,6 +2,10 @@ import { isAttribute } from "./attribute.mjs";
|
|
|
2
2
|
import { getString } from "@oscarpalmer/atoms/string";
|
|
3
3
|
import { kebabCase } from "@oscarpalmer/atoms/string/case";
|
|
4
4
|
//#region src/internal/element-value.ts
|
|
5
|
+
function getValueForAttribute(value, json) {
|
|
6
|
+
if (typeof value === "string") return value;
|
|
7
|
+
return json ? JSON.stringify(value) : getString(value);
|
|
8
|
+
}
|
|
5
9
|
function ignoreSetAttribute(element, name) {
|
|
6
10
|
if (element instanceof HTMLTextAreaElement && name === "value") return true;
|
|
7
11
|
return false;
|
|
@@ -33,9 +37,9 @@ function setElementValues(element, first, second, third, callback, style) {
|
|
|
33
37
|
if (typeof entry === "object" && typeof entry?.name === "string") callback(element, normalizeKey(entry.name, style), entry.value, dispatch);
|
|
34
38
|
}
|
|
35
39
|
}
|
|
36
|
-
function updateElementValue(element, key, value, set, remove,
|
|
40
|
+
function updateElementValue(element, key, value, set, remove, json) {
|
|
37
41
|
if (value == null) remove.call(element, key);
|
|
38
|
-
else if (!ignoreSetAttribute(element, key)) set.call(element, key,
|
|
42
|
+
else if (!ignoreSetAttribute(element, key)) set.call(element, key, getValueForAttribute(value, json));
|
|
39
43
|
}
|
|
40
44
|
const CSS_VARIABLE_PREFIX = "--";
|
|
41
45
|
//#endregion
|
package/dist/models.d.mts
CHANGED
|
@@ -1,4 +1,12 @@
|
|
|
1
1
|
//#region src/models.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Any _ARIA_ attribute for an element _(both prefixed and unprefixed)_
|
|
4
|
+
*/
|
|
5
|
+
type AnyAriaAttribute = AriaAttribute | AriaAttributeUnprefixed;
|
|
6
|
+
/**
|
|
7
|
+
* Any _ARIA_ attribute for an element that can be set to a boolean value _(both prefixed and unprefixed)_
|
|
8
|
+
*/
|
|
9
|
+
type AnyAriaBooleanAttribute = AriaBooleanAttribute | AriaBooleanAttributeUnprefixed;
|
|
2
10
|
/**
|
|
3
11
|
* _ARIA_ attribute for an element
|
|
4
12
|
*
|
|
@@ -10,8 +18,16 @@ type AriaAttribute = keyof AriaAttributes;
|
|
|
10
18
|
*
|
|
11
19
|
* _(https://www.w3.org/TR/wai-aria-1.3/#aria-attributes)_
|
|
12
20
|
*/
|
|
13
|
-
type AriaAttributeUnprefixed = keyof { [Key in AriaAttribute as Key extends `aria-${infer Name}` ? Name : never]:
|
|
14
|
-
type AriaAttributes = { [Key in keyof ARIAMixin as NormalizedName<Key>]:
|
|
21
|
+
type AriaAttributeUnprefixed = keyof { [Key in AriaAttribute as Key extends `aria-${infer Name}` ? Name : never]: unknown; };
|
|
22
|
+
type AriaAttributes = { [Key in keyof ARIAMixin as NormalizedName<Key>]: unknown; };
|
|
23
|
+
/**
|
|
24
|
+
* _ARIA_ attribute for an element that can be set to a boolean value
|
|
25
|
+
*/
|
|
26
|
+
type AriaBooleanAttribute = 'aria-atomic' | 'aria-busy' | 'aria-checked' | 'aria-current' | 'aria-disabled' | 'aria-expanded' | 'aria-haspopup' | 'aria-hidden' | 'aria-invalid' | 'aria-modal' | 'aria-multiline' | 'aria-multiselectable' | 'aria-pressed' | 'aria-readonly' | 'aria-required' | 'aria-selected';
|
|
27
|
+
/**
|
|
28
|
+
* _ARIA_ attribute for an element that can be set to a boolean value, without the `aria-` prefix
|
|
29
|
+
*/
|
|
30
|
+
type AriaBooleanAttributeUnprefixed = keyof { [Key in AriaBooleanAttribute as Key extends `aria-${infer Name}` ? Name : never]: string | null; };
|
|
15
31
|
type NormalizedName<Key extends string> = Key extends `aria${infer Name}` ? Name extends `${infer Part}Element` ? `aria-${Lowercase<Part>}` : Name extends `${infer Part}Elements` ? `aria-${Lowercase<Part>}` : `aria-${Lowercase<Name>}` : never;
|
|
16
32
|
/**
|
|
17
33
|
* _ARIA_ role for an element
|
|
@@ -26,6 +42,18 @@ type Attribute = {
|
|
|
26
42
|
name: string;
|
|
27
43
|
value: unknown;
|
|
28
44
|
};
|
|
45
|
+
/**
|
|
46
|
+
* CSS styles for an element
|
|
47
|
+
*/
|
|
48
|
+
type CSSStyles = Record<keyof CSSStyleDeclaration, unknown>;
|
|
49
|
+
/**
|
|
50
|
+
* CSSS values for an element _(both styles and variables)_
|
|
51
|
+
*/
|
|
52
|
+
type CSSValues = CSSVariables & CSSStyles;
|
|
53
|
+
/**
|
|
54
|
+
* CSS variables for an element
|
|
55
|
+
*/
|
|
56
|
+
type CSSVariables<Value extends Record<string, unknown> = Record<string, unknown>> = { [Property in keyof Value as `--${string & Property}`]?: unknown; };
|
|
29
57
|
/**
|
|
30
58
|
* Event listener for custom events
|
|
31
59
|
*/
|
|
@@ -43,4 +71,4 @@ type Selector = string | Node | Node[] | NodeList;
|
|
|
43
71
|
*/
|
|
44
72
|
type TextDirection = 'ltr' | 'rtl';
|
|
45
73
|
//#endregion
|
|
46
|
-
export { AriaAttribute, AriaAttributeUnprefixed, AriaRole, Attribute, CustomEventListener, RemovableEventListener, Selector, TextDirection };
|
|
74
|
+
export { AnyAriaAttribute, AnyAriaBooleanAttribute, AriaAttribute, AriaAttributeUnprefixed, AriaBooleanAttribute, AriaBooleanAttributeUnprefixed, AriaRole, Attribute, CSSStyles, CSSValues, CSSVariables, CustomEventListener, RemovableEventListener, Selector, TextDirection };
|
package/dist/style.d.mts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import { TextDirection } from "./models.mjs";
|
|
1
|
+
import { CSSValues, TextDirection } from "./models.mjs";
|
|
2
2
|
//#region src/style.d.ts
|
|
3
|
-
type CSSStyleValues = Variables & CSSStyleDeclaration;
|
|
4
3
|
type StyleToggler = {
|
|
5
4
|
/**
|
|
6
5
|
* Set the provided styles on the element
|
|
@@ -11,26 +10,24 @@ type StyleToggler = {
|
|
|
11
10
|
*/
|
|
12
11
|
remove(): void;
|
|
13
12
|
};
|
|
14
|
-
type Styles = Partial<Record<keyof CSSStyleValues, unknown>>;
|
|
15
|
-
type Variables<Value extends Record<string, string | undefined> = Record<string, string | undefined>> = { [property in keyof Value as `--${string & property}`]?: string | undefined; };
|
|
16
13
|
/**
|
|
17
14
|
* Get a style from an element
|
|
18
15
|
*
|
|
19
16
|
* @param element Element to get the style from
|
|
20
|
-
* @param
|
|
17
|
+
* @param name Style name
|
|
21
18
|
* @param computed Get the computed style? _(defaults to `false`)_
|
|
22
19
|
* @returns Style value
|
|
23
20
|
*/
|
|
24
|
-
declare function getStyle(element: Element,
|
|
21
|
+
declare function getStyle(element: Element, name: keyof CSSValues, computed?: boolean): string | undefined;
|
|
25
22
|
/**
|
|
26
23
|
* Get styles from an element
|
|
27
24
|
*
|
|
28
25
|
* @param element Element to get the styles from
|
|
29
|
-
* @param
|
|
26
|
+
* @param names Styles to get
|
|
30
27
|
* @param computed Get the computed styles? _(defaults to `false`)_
|
|
31
28
|
* @returns Style values
|
|
32
29
|
*/
|
|
33
|
-
declare function getStyles<
|
|
30
|
+
declare function getStyles<Name extends keyof CSSValues>(element: Element, names: Name[], computed?: boolean): Record<Name, string | undefined>;
|
|
34
31
|
/**
|
|
35
32
|
* Get the text direction of a node or element _(or document, if element is invalid)_
|
|
36
33
|
*
|
|
@@ -48,17 +45,17 @@ declare function getTextDirection(): TextDirection;
|
|
|
48
45
|
* Set a style on an element
|
|
49
46
|
*
|
|
50
47
|
* @param element Element to set the style on
|
|
51
|
-
* @param
|
|
48
|
+
* @param name Style name
|
|
52
49
|
* @param value Style value
|
|
53
50
|
*/
|
|
54
|
-
declare function setStyle(element: Element,
|
|
51
|
+
declare function setStyle(element: Element, name: keyof CSSValues, value?: unknown): void;
|
|
55
52
|
/**
|
|
56
53
|
* Set styles on an element
|
|
57
54
|
*
|
|
58
55
|
* @param element Element to set the styles on
|
|
59
56
|
* @param styles Styles to set
|
|
60
57
|
*/
|
|
61
|
-
declare function setStyles(element: Element, styles:
|
|
58
|
+
declare function setStyles(element: Element, styles: Partial<CSSValues>): void;
|
|
62
59
|
/**
|
|
63
60
|
* Toggle styles for an element
|
|
64
61
|
*
|
|
@@ -66,6 +63,6 @@ declare function setStyles(element: Element, styles: Styles): void;
|
|
|
66
63
|
* @param styles Styles to be set or removed
|
|
67
64
|
* @returns Style toggler
|
|
68
65
|
*/
|
|
69
|
-
declare function toggleStyles(element: Element, styles:
|
|
66
|
+
declare function toggleStyles(element: Element, styles: Partial<CSSValues>): StyleToggler;
|
|
70
67
|
//#endregion
|
|
71
68
|
export { StyleToggler, getStyle, getStyles, getTextDirection, setStyle, setStyles, toggleStyles };
|
package/dist/style.mjs
CHANGED
|
@@ -6,28 +6,28 @@ import { getString } from "@oscarpalmer/atoms/string";
|
|
|
6
6
|
* Get a style from an element
|
|
7
7
|
*
|
|
8
8
|
* @param element Element to get the style from
|
|
9
|
-
* @param
|
|
9
|
+
* @param name Style name
|
|
10
10
|
* @param computed Get the computed style? _(defaults to `false`)_
|
|
11
11
|
* @returns Style value
|
|
12
12
|
*/
|
|
13
|
-
function getStyle(element,
|
|
14
|
-
if (element instanceof Element && typeof
|
|
13
|
+
function getStyle(element, name, computed) {
|
|
14
|
+
if (element instanceof Element && typeof name === "string") return getStyleValue(element, name, computed === true);
|
|
15
15
|
}
|
|
16
16
|
/**
|
|
17
17
|
* Get styles from an element
|
|
18
18
|
*
|
|
19
19
|
* @param element Element to get the styles from
|
|
20
|
-
* @param
|
|
20
|
+
* @param names Styles to get
|
|
21
21
|
* @param computed Get the computed styles? _(defaults to `false`)_
|
|
22
22
|
* @returns Style values
|
|
23
23
|
*/
|
|
24
|
-
function getStyles(element,
|
|
24
|
+
function getStyles(element, names, computed) {
|
|
25
25
|
const styles = {};
|
|
26
|
-
if (!(element instanceof Element && Array.isArray(
|
|
27
|
-
const { length } =
|
|
26
|
+
if (!(element instanceof Element && Array.isArray(names))) return styles;
|
|
27
|
+
const { length } = names;
|
|
28
28
|
for (let index = 0; index < length; index += 1) {
|
|
29
|
-
const
|
|
30
|
-
if (typeof
|
|
29
|
+
const name = names[index];
|
|
30
|
+
if (typeof name === "string") styles[name] = getStyleValue(element, name, computed === true);
|
|
31
31
|
}
|
|
32
32
|
return styles;
|
|
33
33
|
}
|
|
@@ -43,11 +43,11 @@ function getTextDirection(node) {
|
|
|
43
43
|
* Set a style on an element
|
|
44
44
|
*
|
|
45
45
|
* @param element Element to set the style on
|
|
46
|
-
* @param
|
|
46
|
+
* @param name Style name
|
|
47
47
|
* @param value Style value
|
|
48
48
|
*/
|
|
49
|
-
function setStyle(element,
|
|
50
|
-
setElementValues(element,
|
|
49
|
+
function setStyle(element, name, value) {
|
|
50
|
+
setElementValues(element, name, value, null, updateStyleProperty, true);
|
|
51
51
|
}
|
|
52
52
|
/**
|
|
53
53
|
* Set styles on an element
|
|
@@ -93,14 +93,14 @@ function toggleStyles(element, styles) {
|
|
|
93
93
|
};
|
|
94
94
|
}
|
|
95
95
|
function updateStyleProperty(element, key, value) {
|
|
96
|
-
updateElementValue(element, key, value, function(
|
|
97
|
-
if (
|
|
98
|
-
else this.style[
|
|
99
|
-
}, function(
|
|
100
|
-
if (
|
|
101
|
-
else this.style[
|
|
96
|
+
updateElementValue(element, key, value, function(name, style) {
|
|
97
|
+
if (name.startsWith(VARIABLE_PREFIX)) this.style.setProperty(name, getString(style));
|
|
98
|
+
else this.style[name] = getString(style);
|
|
99
|
+
}, function(name) {
|
|
100
|
+
if (name.startsWith(VARIABLE_PREFIX)) this.style.removeProperty(name);
|
|
101
|
+
else this.style[name] = "";
|
|
102
102
|
if (this.getAttribute(ATTRIBUTE_STYLE) === "") this.removeAttribute(ATTRIBUTE_STYLE);
|
|
103
|
-
}, false
|
|
103
|
+
}, false);
|
|
104
104
|
}
|
|
105
105
|
const ATTRIBUTE_STYLE = "style";
|
|
106
106
|
const DIRECTION_LTR = "ltr";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@oscarpalmer/toretto",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.49.1",
|
|
4
4
|
"description": "A collection of badass DOM utilities.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"dom",
|
|
@@ -93,10 +93,10 @@
|
|
|
93
93
|
"@oscarpalmer/atoms": "^0.188.1"
|
|
94
94
|
},
|
|
95
95
|
"devDependencies": {
|
|
96
|
-
"@oxlint/plugins": "^1.
|
|
96
|
+
"@oxlint/plugins": "^1.76",
|
|
97
97
|
"@types/node": "^26.1",
|
|
98
98
|
"@vitest/coverage-istanbul": "^4.1",
|
|
99
|
-
"jsdom": "^
|
|
99
|
+
"jsdom": "^30",
|
|
100
100
|
"tsdown": "^0.22",
|
|
101
101
|
"typescript": "^6",
|
|
102
102
|
"vite": "npm:@voidzero-dev/vite-plus-core@latest",
|