Skip to content
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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft

fix(react-utils): fixed useSticky #369

wants to merge 2 commits into from

Conversation

makhnatkin
Copy link
Collaborator

Fixes #368

@makhnatkin makhnatkin self-assigned this Sep 16, 2024
@gravity-ui-bot
Copy link
Contributor

Preview is ready.

let currentElement = element;
while (currentElement) {
const value = getComputedStyle(currentElement).getPropertyValue(variableName);
if (value) {
Copy link
Collaborator Author

@makhnatkin makhnatkin Sep 16, 2024

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

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
);
Copy link
Collaborator

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';
Copy link
Collaborator

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

Comment on lines +19 to +22
const overflow = window.getComputedStyle(currentElement).overflow;
if (overflow === 'auto' || overflow === 'scroll') {
return currentElement;
}
Copy link
Collaborator

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);
Copy link
Collaborator

@d3m1d0v d3m1d0v Sep 18, 2024

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

@makhnatkin makhnatkin marked this pull request as draft October 17, 2024 12:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Sticky behavior breaks in editor with parent overflow: auto
3 participants