Draft
Conversation
Contributor
|
Preview is ready. |
makhnatkin
commented
Sep 16, 2024
| let currentElement = element; | ||
| while (currentElement) { | ||
| const value = getComputedStyle(currentElement).getPropertyValue(variableName); | ||
| if (value) { |
Collaborator
Author
There was a problem hiding this comment.
If the value is 0, then the check will fail. Fix it
d3m1d0v
reviewed
Sep 18, 2024
Comment on lines
+37
to
+48
| 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 | ||
| ); |
Member
There was a problem hiding this comment.
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'; |
Member
There was a problem hiding this comment.
plz, use throttle from src/lodash.ts
Comment on lines
+19
to
+22
| const overflow = window.getComputedStyle(currentElement).overflow; | ||
| if (overflow === 'auto' || overflow === 'scroll') { | ||
| return currentElement; | ||
| } |
| while (currentElement) { | ||
| const value = getComputedStyle(currentElement).getPropertyValue(variableName); | ||
| if (value) { | ||
| return parseFloat(value); |
Member
There was a problem hiding this comment.
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
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #368