MCPcopy Create free account
hub / github.com/adobe/react-spectrum / setStyle

Function setStyle

packages/react-aria/src/utils/domHelpers.ts:98–131  ·  view source on GitHub ↗
(
  target: HTMLElement | HTMLElement[] | null,
  property: string,
  value: string,
  priority?: string
)

Source from the content-addressed store, hash-verified

96 * Sets a CSS property on an element and returns a cleanup function.
97 */
98export function setStyle(
99 target: HTMLElement | HTMLElement[] | null,
100 property: string,
101 value: string,
102 priority?: string
103): () => void {
104 if (target == null) {
105 return () => {};
106 }
107
108 let restore = new Array<Function>();
109 let styleTargets = Array.isArray(target) ? target : [target];
110
111 for (let styleTarget of styleTargets) {
112 let initialValue = styleTarget.style.getPropertyValue(property);
113 let initialPriority = styleTarget.style.getPropertyPriority(property);
114
115 styleTarget.style.setProperty(property, value, priority);
116
117 restore.unshift(() => {
118 if (initialValue) {
119 styleTarget.style.setProperty(property, initialValue, initialPriority);
120 } else {
121 styleTarget.style.removeProperty(property);
122 }
123 });
124 }
125
126 return () => {
127 for (let cleanup of restore) {
128 cleanup();
129 }
130 };
131}

Callers 5

domHelpers.test.jsFile · 0.90
preventScrollStandardFunction · 0.90
TabLineFunction · 0.85
TabLineFunction · 0.85

Calls 1

cleanupFunction · 0.85

Tested by

no test coverage detected