mirror of
https://github.com/ente-io/ente.git
synced 2025-08-08 07:28:26 +00:00
Inline
This commit is contained in:
parent
f6f7fb3b8f
commit
bb4f415ae1
@ -2,9 +2,6 @@ import {
|
|||||||
IconButtonWithBG,
|
IconButtonWithBG,
|
||||||
SpaceBetweenFlex,
|
SpaceBetweenFlex,
|
||||||
} from "@ente/shared/components/Container";
|
} from "@ente/shared/components/Container";
|
||||||
import useComponentScroll, {
|
|
||||||
SCROLL_DIRECTION,
|
|
||||||
} from "@ente/shared/hooks/useComponentScroll";
|
|
||||||
import useWindowSize from "@ente/shared/hooks/useWindowSize";
|
import useWindowSize from "@ente/shared/hooks/useWindowSize";
|
||||||
import ExpandMore from "@mui/icons-material/ExpandMore";
|
import ExpandMore from "@mui/icons-material/ExpandMore";
|
||||||
import NavigateNextIcon from "@mui/icons-material/NavigateNext";
|
import NavigateNextIcon from "@mui/icons-material/NavigateNext";
|
||||||
@ -23,7 +20,7 @@ import {
|
|||||||
} from "components/Collections/styledComponents";
|
} from "components/Collections/styledComponents";
|
||||||
import { t } from "i18next";
|
import { t } from "i18next";
|
||||||
import memoize from "memoize-one";
|
import memoize from "memoize-one";
|
||||||
import React, { useEffect } from "react";
|
import React, { useEffect, useRef, useState } from "react";
|
||||||
import AutoSizer from "react-virtualized-auto-sizer";
|
import AutoSizer from "react-virtualized-auto-sizer";
|
||||||
import {
|
import {
|
||||||
FixedSizeList as List,
|
FixedSizeList as List,
|
||||||
@ -210,7 +207,74 @@ const CollectionListBar = (props: IProps) => {
|
|||||||
|
|
||||||
export default CollectionListBar;
|
export default CollectionListBar;
|
||||||
|
|
||||||
const Wrapper = styled("button")<{ direction: SCROLL_DIRECTION }>`
|
enum SCROLL_DIRECTION {
|
||||||
|
LEFT = -1,
|
||||||
|
RIGHT = +1,
|
||||||
|
}
|
||||||
|
|
||||||
|
function useComponentScroll({ dependencies }: { dependencies: any[] }) {
|
||||||
|
const componentRef = useRef<HTMLDivElement>(null);
|
||||||
|
|
||||||
|
const [scrollObj, setScrollObj] = useState<{
|
||||||
|
scrollLeft?: number;
|
||||||
|
scrollWidth?: number;
|
||||||
|
clientWidth?: number;
|
||||||
|
}>({});
|
||||||
|
|
||||||
|
const updateScrollObj = () => {
|
||||||
|
if (!componentRef.current) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const { scrollLeft, scrollWidth, clientWidth } = componentRef.current;
|
||||||
|
setScrollObj({ scrollLeft, scrollWidth, clientWidth });
|
||||||
|
};
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!componentRef.current) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// Add event listener
|
||||||
|
componentRef.current?.addEventListener("scroll", updateScrollObj);
|
||||||
|
|
||||||
|
// Call handler right away so state gets updated with initial window size
|
||||||
|
updateScrollObj();
|
||||||
|
// Remove event listener on cleanup
|
||||||
|
return () =>
|
||||||
|
componentRef.current?.removeEventListener(
|
||||||
|
"resize",
|
||||||
|
updateScrollObj,
|
||||||
|
);
|
||||||
|
}, [componentRef.current]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
updateScrollObj();
|
||||||
|
}, [...dependencies]);
|
||||||
|
|
||||||
|
const scrollComponent = (direction: SCROLL_DIRECTION) => () => {
|
||||||
|
componentRef.current.scrollBy(250 * direction, 0);
|
||||||
|
};
|
||||||
|
|
||||||
|
const hasScrollBar = scrollObj.scrollWidth > scrollObj.clientWidth;
|
||||||
|
const onFarLeft = scrollObj.scrollLeft === 0;
|
||||||
|
const onFarRight =
|
||||||
|
scrollObj.scrollLeft + scrollObj.clientWidth === scrollObj.scrollWidth;
|
||||||
|
|
||||||
|
return {
|
||||||
|
hasScrollBar,
|
||||||
|
onFarLeft,
|
||||||
|
onFarRight,
|
||||||
|
scrollComponent,
|
||||||
|
componentRef,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
const ScrollButton = ({ scrollDirection, ...rest }) => (
|
||||||
|
<ScrollButtonWrapper direction={scrollDirection} {...rest}>
|
||||||
|
<NavigateNextIcon />
|
||||||
|
</ScrollButtonWrapper>
|
||||||
|
);
|
||||||
|
|
||||||
|
const ScrollButtonWrapper = styled("button")<{ direction: SCROLL_DIRECTION }>`
|
||||||
position: absolute;
|
position: absolute;
|
||||||
z-index: 2;
|
z-index: 2;
|
||||||
top: 7px;
|
top: 7px;
|
||||||
@ -246,9 +310,3 @@ const Wrapper = styled("button")<{ direction: SCROLL_DIRECTION }>`
|
|||||||
width: 30px;
|
width: 30px;
|
||||||
}
|
}
|
||||||
`;
|
`;
|
||||||
|
|
||||||
const ScrollButton = ({ scrollDirection, ...rest }) => (
|
|
||||||
<Wrapper direction={scrollDirection} {...rest}>
|
|
||||||
<NavigateNextIcon />
|
|
||||||
</Wrapper>
|
|
||||||
);
|
|
||||||
|
@ -1,66 +0,0 @@
|
|||||||
import { useEffect, useRef, useState } from "react";
|
|
||||||
|
|
||||||
export enum SCROLL_DIRECTION {
|
|
||||||
LEFT = -1,
|
|
||||||
RIGHT = +1,
|
|
||||||
}
|
|
||||||
|
|
||||||
export default function useComponentScroll({
|
|
||||||
dependencies,
|
|
||||||
}: {
|
|
||||||
dependencies: any[];
|
|
||||||
}) {
|
|
||||||
const componentRef = useRef<HTMLDivElement>(null);
|
|
||||||
|
|
||||||
const [scrollObj, setScrollObj] = useState<{
|
|
||||||
scrollLeft?: number;
|
|
||||||
scrollWidth?: number;
|
|
||||||
clientWidth?: number;
|
|
||||||
}>({});
|
|
||||||
|
|
||||||
const updateScrollObj = () => {
|
|
||||||
if (!componentRef.current) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const { scrollLeft, scrollWidth, clientWidth } = componentRef.current;
|
|
||||||
setScrollObj({ scrollLeft, scrollWidth, clientWidth });
|
|
||||||
};
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (!componentRef.current) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
// Add event listener
|
|
||||||
componentRef.current?.addEventListener("scroll", updateScrollObj);
|
|
||||||
|
|
||||||
// Call handler right away so state gets updated with initial window size
|
|
||||||
updateScrollObj();
|
|
||||||
// Remove event listener on cleanup
|
|
||||||
return () =>
|
|
||||||
componentRef.current?.removeEventListener(
|
|
||||||
"resize",
|
|
||||||
updateScrollObj,
|
|
||||||
);
|
|
||||||
}, [componentRef.current]);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
updateScrollObj();
|
|
||||||
}, [...dependencies]);
|
|
||||||
|
|
||||||
const scrollComponent = (direction: SCROLL_DIRECTION) => () => {
|
|
||||||
componentRef.current.scrollBy(250 * direction, 0);
|
|
||||||
};
|
|
||||||
|
|
||||||
const hasScrollBar = scrollObj.scrollWidth > scrollObj.clientWidth;
|
|
||||||
const onFarLeft = scrollObj.scrollLeft === 0;
|
|
||||||
const onFarRight =
|
|
||||||
scrollObj.scrollLeft + scrollObj.clientWidth === scrollObj.scrollWidth;
|
|
||||||
|
|
||||||
return {
|
|
||||||
hasScrollBar,
|
|
||||||
onFarLeft,
|
|
||||||
onFarRight,
|
|
||||||
scrollComponent,
|
|
||||||
componentRef,
|
|
||||||
};
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user