-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add vendor prefix to CSS properties that are updated through Jav…
…aScript
- Loading branch information
Showing
2 changed files
with
29 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
interface SetStyleWithVendorPrefixOptions { | ||
element: HTMLElement; | ||
// The name of the CSS property, e.g. `transform`. | ||
style: string; | ||
value: string; | ||
} | ||
|
||
const supports = (typeof window !== 'undefined' && !!window.CSS && CSS.supports) || (() => false); | ||
|
||
/** | ||
* Note: we don't need to add vendor prefixes within `.scss` files since they're added automatically. | ||
* This function is necessary when the `element.style` is updated directly through the JavaScript. | ||
* This is not required to be used with CSS properties that don't require vendor prefixes (e.g. `opacity`). | ||
*/ | ||
export function setStyleWithVendorPrefix({ element, style, value }: SetStyleWithVendorPrefixOptions): void { | ||
element.style[style] = value; | ||
|
||
if (supports(`-webkit-${style}: ${value}`)) { | ||
// Note: some browsers still require setting `-webkit` vendor prefix. E.g. Mozilla 49 has implemented | ||
// the 3D support for `transform`, but it requires setting `-webkit-` prefix. | ||
element.style[`-webkit-${style}`] = value; | ||
} | ||
} |