Skip to content

Commit

Permalink
feat: export ueseScrollState hook from utils package
Browse files Browse the repository at this point in the history
affects: @medly-components/core, @medly-components/utils
  • Loading branch information
gmukul01 committed Jan 26, 2025
1 parent cea7a4f commit b4dadf6
Show file tree
Hide file tree
Showing 12 changed files with 31 additions and 25 deletions.
5 changes: 2 additions & 3 deletions packages/core/src/components/Drawer/Content/Content.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { WithStyle } from '@medly-components/utils';
import { useScrollState, WithStyle } from '@medly-components/utils';
import type { FC } from 'react';
import { memo, useContext, useRef } from 'react';
import { useScrollState } from '../../Modal/useScrollState';
import { DrawerContext } from '../Drawer.context';
import { ContentStyled } from './Content.styled';
import type { FC } from 'react';

const Component: FC = memo(props => {
const contentRef = useRef<HTMLDivElement>(null),
Expand Down
5 changes: 2 additions & 3 deletions packages/core/src/components/Drawer/Drawer.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { useKeyPress, WithStyle } from '@medly-components/utils';
import { scrollStateReducer, useKeyPress, WithStyle } from '@medly-components/utils';
import type { FC, MouseEvent } from 'react';
import { forwardRef, memo, useCallback, useEffect, useReducer, useState } from 'react';
import { reducer } from '../Modal/scrollStateReducer';
import Content from './Content';
import { DrawerContext } from './Drawer.context';
import { DrawerBackground, DrawerStyled } from './Drawer.styled';
Expand All @@ -13,7 +12,7 @@ const Component: FC<DrawerProps> = memo(
forwardRef(({ id, onClose, open, width, children, withOverlay, position, shouldCloseOnOutsideClick, ...props }, ref) => {
const isEscPressed = useKeyPress('Escape'),
[shouldRender, setRenderState] = useState(open),
[scrollState, dispatch] = useReducer(reducer, { scrolledToTop: true, scrolledToBottom: false, scrollPosition: 0 });
[scrollState, dispatch] = useReducer(scrollStateReducer, { scrolledToTop: true, scrolledToBottom: false, scrollPosition: 0 });

const handleBackgroundClick = useCallback(
(event: MouseEvent<HTMLDivElement>) =>
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/components/Drawer/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { ScrollActionTypes } from '@medly-components/utils';
import { HTMLProps, WithStyle } from '@medly-components/utils';
import type { Dispatch, FC } from 'react';
import { ScrollActionTypes } from '../Modal/scrollStateReducer/types';
import { DrawerFooterProps } from './Footer/types';

export interface DrawerProps extends HTMLProps<HTMLDivElement> {
Expand Down
3 changes: 1 addition & 2 deletions packages/core/src/components/Modal/Content/Content.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { WithStyle } from '@medly-components/utils';
import { useScrollState, WithStyle } from '@medly-components/utils';
import type { FC } from 'react';
import { memo, useContext, useRef } from 'react';
import { ModalContext } from '../Modal.context';
import { useScrollState } from '../useScrollState';
import * as Styled from './Content.styled';

const Component: FC = memo(props => {
Expand Down
6 changes: 2 additions & 4 deletions packages/core/src/components/Modal/Modal.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useCombinedRefs, useKeyPress, useWindowSize, WithStyle } from '@medly-components/utils';
import { scrollStateReducer, useCombinedRefs, useKeyPress, useScrollState, useWindowSize, WithStyle } from '@medly-components/utils';
import { FC, forwardRef, memo, MouseEvent, useCallback, useEffect, useLayoutEffect, useReducer, useRef, useState } from 'react';
import Actions from './Actions';
import CloseIcon from './CloseIcon';
Expand All @@ -8,9 +8,7 @@ import { ModalContext } from './Modal.context';
import { InnerContainerStyled, ModalBackgroundStyled } from './Modal.styled';
import ModalManager from './ModalManager';
import Popup from './Popup';
import { reducer } from './scrollStateReducer/scrollStateReducer';
import { ModalProps, ModalStaticProps } from './types';
import { useScrollState } from './useScrollState';

const manager = new ModalManager();

Expand All @@ -33,7 +31,7 @@ const Component: FC<ModalProps> = memo(
innerContainerRef = useRef<HTMLDivElement>(null),
[headerHeight, setHeaderHeight] = useState(0),
[activeElement, setActiveElement] = useState<HTMLElement>(),
[scrollState, dispatch] = useReducer(reducer, { scrolledToTop: true, scrolledToBottom: false, scrollPosition: 0 }),
[scrollState, dispatch] = useReducer(scrollStateReducer, { scrolledToTop: true, scrolledToBottom: false, scrollPosition: 0 }),
[shouldRender, setShouldRender] = useState(open),
{ width: windowWidth } = useWindowSize(),
isSmallScreen = windowWidth < 768;
Expand Down

This file was deleted.

10 changes: 2 additions & 8 deletions packages/core/src/components/Modal/types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { ScrollActionTypes } from '@medly-components/utils';
import { HTMLProps, WithStyle } from '@medly-components/utils';
import type { Dispatch, FC, RefObject } from 'react';
import type { Dispatch, FC } from 'react';
import { ModalActionUserProps } from './Actions/types';
import { ModalPopupProps } from './Popup/types';
import { ScrollActionTypes } from './scrollStateReducer/types';

export interface ModalProps extends HTMLProps<HTMLDivElement> {
/** Shows modal only when this prop is true */
Expand Down Expand Up @@ -42,12 +42,6 @@ export interface InnerContainerProps {
overflowVisible?: boolean;
}

export interface UseScrollStateProps {
scrollState: ScrollState;
ref: RefObject<HTMLDivElement>;
dispatch: Dispatch<ScrollActionTypes>;
}

export interface ModalContextType {
headerHeight: number;
setHeaderHeight: (height: number) => void;
Expand Down
3 changes: 2 additions & 1 deletion packages/utils/src/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ export * from './useKeyPress';
export * from './useMediaQuery';
export * from './useOuterClickNotifier';
export * from './useRunAfterUpdate';
export * from './useSWRAxios';
export * from './useScrollState';
export * from './useStorage';
export * from './useSWRAxios';
export * from './useUpdateEffect';
export * from './useWindowSize';
3 changes: 3 additions & 0 deletions packages/utils/src/hooks/useScrollState/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './scrollStateReducer';
export * from './types';
export * from './useScrollState';
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ScrollActionTypes } from './types';

export const reducer = (
export const scrollStateReducer = (
state: { scrolledToTop: boolean; scrolledToBottom: boolean; scrollPosition: number },
action: ScrollActionTypes
) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
import type { Dispatch, RefObject } from 'react';

export interface ScrollState {
scrolledToTop: boolean;
scrolledToBottom: boolean;
scrollPosition: number;
}

interface ScrolledToTopAction {
type: 'scrolledToTop';
value: boolean;
Expand All @@ -14,3 +22,9 @@ interface ScrollPositionAction {
}

export type ScrollActionTypes = ScrolledToTopAction | ScrolledToBottomAction | ScrollPositionAction;

export interface UseScrollStateProps {
scrollState: ScrollState;
ref: RefObject<HTMLDivElement>;
dispatch: Dispatch<ScrollActionTypes>;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { UIEvent } from 'react';
import { useEffect } from 'react';
import { UseScrollStateProps } from './types';
import type { UseScrollStateProps } from './types';

export const useScrollState = ({ ref, scrollState, dispatch }: UseScrollStateProps): ((e: React.UIEvent<HTMLDivElement>) => void) => {
useEffect(() => {
Expand Down

0 comments on commit b4dadf6

Please sign in to comment.