-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(react-utils): fixed useSticky #369
base: main
Are you sure you want to change the base?
Conversation
Preview is ready. |
let currentElement = element; | ||
while (currentElement) { | ||
const value = getComputedStyle(currentElement).getPropertyValue(variableName); | ||
if (value) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the value is 0
, then the check will fail. Fix it
let currentElement = element; | ||
while (currentElement) { | ||
const value = getComputedStyle(currentElement).getPropertyValue(variableName); | ||
if (value) { | ||
return parseFloat(value); | ||
} | ||
currentElement = currentElement.parentElement; | ||
} | ||
// Fallback to global :root if not found locally | ||
return ( | ||
parseFloat(getComputedStyle(document.documentElement).getPropertyValue(variableName)) || 0 | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's can be shorter, like this:
let currentElement = element || document.documentElement;
const value = getComputedStyle(currentElement).getPropertyValue(variableName);
return value === '' ? 0 : parseFloat(value);
Because getComputedStyle().getPropertyValue()
returns closest variable value in DOM tree
export function useSticky<T extends HTMLElement>(elemRef: RefObject<T>) { | ||
import {RefObject, useEffect, useState} from 'react'; | ||
|
||
import throttle from 'lodash/throttle'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
plz, use throttle
from src/lodash.ts
const overflow = window.getComputedStyle(currentElement).overflow; | ||
if (overflow === 'auto' || overflow === 'scroll') { | ||
return currentElement; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
or overflow-y: auto | scroll
?
while (currentElement) { | ||
const value = getComputedStyle(currentElement).getPropertyValue(variableName); | ||
if (value) { | ||
return parseFloat(value); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you need to calculate numeric value of css-var, you need to use something like this function, because css-vars can store a css expression like 5px + 7px
, so parseFloat or parseInt don't work correctly
Fixes #368