Skip to content
This repository has been archived by the owner on Feb 1, 2024. It is now read-only.

Commit

Permalink
use ref.current instead of a constant
Browse files Browse the repository at this point in the history
  • Loading branch information
eniskraasniqi1 committed Jul 13, 2023
1 parent 74d88f8 commit 1627804
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
15 changes: 7 additions & 8 deletions src/components/tab/TabGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,9 @@ const TabGroup: GenericComponent<TabGroupProps> = ({ children, className, size,
const [canScrollLeft, setCanScrollLeft] = useState(false);
const [canScrollRight, setCanScrollRight] = useState(false);

const scrollContainerElement = scrollContainerRef.current;

const checkForScrollPosition = () => {
if (scrollContainerElement) {
const { scrollLeft, scrollWidth, clientWidth } = scrollContainerElement;
if (scrollContainerRef.current) {
const { scrollLeft, scrollWidth, clientWidth } = scrollContainerRef.current;

setCanScrollLeft(scrollLeft > 0);
setCanScrollRight(scrollLeft < scrollWidth - clientWidth);
Expand All @@ -39,7 +37,7 @@ const TabGroup: GenericComponent<TabGroupProps> = ({ children, className, size,
const scrollToActiveTab = () => {
if (activeTabRef.current) {
const { offsetLeft } = activeTabRef.current;
scrollContainerElement?.scrollTo({ left: offsetLeft - SCROLL_BUTTON_WIDTH });
scrollContainerRef.current?.scrollTo({ left: offsetLeft - SCROLL_BUTTON_WIDTH });
}
};
const handleResize = () => {
Expand All @@ -52,13 +50,14 @@ const TabGroup: GenericComponent<TabGroupProps> = ({ children, className, size,
};

const scrollContainerBy = (distance: number) => {
scrollContainerElement?.scrollBy({ left: distance, behavior: 'smooth' });
scrollContainerRef.current?.scrollBy({ left: distance, behavior: 'smooth' });
};

useEffect(() => {
smoothScroll.polyfill();
scrollContainerElement?.addEventListener('scroll', handleScroll);
return () => scrollContainerElement?.removeEventListener('scroll', handleScroll);
scrollContainerRef.current?.addEventListener('scroll', handleScroll);
// eslint-disable-next-line react-hooks/exhaustive-deps
return () => scrollContainerRef.current?.removeEventListener('scroll', handleScroll);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

Expand Down
10 changes: 5 additions & 5 deletions src/components/wysiwygEditor/WysiwygEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ const WysiwygEditor: GenericComponent<WysiwygEditorProps> = ({
const [isFocused, setIsFocused] = useState(false);
const [isPlaceholderShown, setIsPlaceholderShown] = useState(true);
const editorRef = useRef<EditorType>(null);
const editorElement = editorRef.current;

useEffect(() => {
if (autoFocus) {
Expand All @@ -116,19 +115,20 @@ const WysiwygEditor: GenericComponent<WysiwygEditorProps> = ({
}, [autoFocus]);

useEffect(() => {
if (!onKeyDown || !editorElement?.wrapper) {
if (!onKeyDown || !editorRef.current?.wrapper) {
return;
}

const handleKeyDown = (event: React.KeyboardEvent) => onKeyDown(event);

editorElement.wrapper.addEventListener('keydown', handleKeyDown);
editorRef.current.wrapper.addEventListener('keydown', handleKeyDown);

return () => {
if (!onKeyDown || !editorElement?.wrapper) {
if (!onKeyDown || !editorRef.current?.wrapper) {
return;
}
editorElement.wrapper.removeEventListener('keydown', handleKeyDown);
// eslint-disable-next-line react-hooks/exhaustive-deps
editorRef.current.wrapper.removeEventListener('keydown', handleKeyDown);
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
Expand Down

0 comments on commit 1627804

Please sign in to comment.