Skip to content

Instantly share code, notes, and snippets.

@ETNOL
Last active May 30, 2025 13:56
Show Gist options
  • Select an option

  • Save ETNOL/2f46244612c277b0bd61d5952533623e to your computer and use it in GitHub Desktop.

Select an option

Save ETNOL/2f46244612c277b0bd61d5952533623e to your computer and use it in GitHub Desktop.
Hooks for style changes [WIP]
/**
* 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