( target: HTMLElement | HTMLElement[] | null, property: string, value: string, priority?: string )
| 96 | * Sets a CSS property on an element and returns a cleanup function. |
| 97 | */ |
| 98 | export 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 | } |
no test coverage detected