Last active
May 30, 2025 13:56
-
-
Save ETNOL/2f46244612c277b0bd61d5952533623e to your computer and use it in GitHub Desktop.
Hooks for style changes [WIP]
This file contains hidden or 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
| /** | |
| * Adds callbacks to an element's style changes. | |
| * Useful if you need to make changes right before and/or right | |
| * after an element recieves a style change. | |
| * @param el Element | |
| * @param hooks Pre and post style change callbacks | |
| */ | |
| export const addStyleHooks = ( | |
| el: HTMLElement, | |
| { pre, post }: { | |
| pre?: (style?: string, oldValue?: any, newValue?: any) => any, | |
| post?: (style?: string, oldValue?: any, newValue?: any) => any, | |
| }) => { | |
| if (Number.isInteger(parseInt(k, 10))) { | |
| return | |
| } | |
| Object.keys(el.style).forEach((k: string) => { | |
| Object.defineProperty(el.style, k, { | |
| set (newVal) { | |
| const oldVal = el.style.getPropertyValue(k) | |
| pre && pre(k, oldVal, newVal) | |
| newVal && el.style.setProperty(k, newVal) | |
| post && post(k, oldVal, newVal) | |
| } | |
| }) | |
| }) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment