From ca662b0f38901c98e2cda40b46053fce7f2cf4b2 Mon Sep 17 00:00:00 2001 From: hmacr Date: Fri, 3 Jan 2025 20:46:38 +0530 Subject: [PATCH] full page scroll --- app/components/FilterOptions.tsx | 4 +- app/components/Table.tsx | 213 ++++++++++-------- app/scenes/Settings/Members.tsx | 14 +- .../Settings/components/PeopleTable.tsx | 13 +- .../Settings/components/SharesTable.tsx | 11 +- .../Settings/components/UserRoleFilter.tsx | 1 + .../Settings/components/UserStatusFilter.tsx | 1 + 7 files changed, 152 insertions(+), 105 deletions(-) diff --git a/app/components/FilterOptions.tsx b/app/components/FilterOptions.tsx index 61fd0fd250..4725e33408 100644 --- a/app/components/FilterOptions.tsx +++ b/app/components/FilterOptions.tsx @@ -29,6 +29,7 @@ type Props = { showFilter?: boolean; fetchQuery?: (options: FetchPageParams) => Promise; fetchQueryOptions?: Record; + modalMenu?: boolean; }; const FilterOptions = ({ @@ -41,12 +42,13 @@ const FilterOptions = ({ showFilter, fetchQuery, fetchQueryOptions, + modalMenu, }: Props) => { const { t } = useTranslation(); const searchInputRef = React.useRef(null); const listRef = React.useRef(null); const menu = useMenuState({ - modal: true, + modal: modalMenu ?? true, }); const selectedItems = options.filter((option) => selectedKeys.includes(option.key) diff --git a/app/components/Table.tsx b/app/components/Table.tsx index 319d1da1b3..d1b9c25e74 100644 --- a/app/components/Table.tsx +++ b/app/components/Table.tsx @@ -10,7 +10,7 @@ import { AccessorFn, CellContext, } from "@tanstack/react-table"; -import { useVirtualizer } from "@tanstack/react-virtual"; +import { useWindowVirtualizer } from "@tanstack/react-virtual"; import { observer } from "mobx-react"; import { CollapsedIcon } from "outline-icons"; import * as React from "react"; @@ -24,7 +24,8 @@ import Flex from "~/components/Flex"; import NudeButton from "~/components/NudeButton"; import PlaceholderText from "~/components/PlaceholderText"; import usePrevious from "~/hooks/usePrevious"; -import useWindowSize from "~/hooks/useWindowSize"; + +const HEADER_HEIGHT = 40; type DataColumn = { type: "data"; @@ -55,6 +56,7 @@ export type Props = { fetchNext?: () => void; }; rowHeight: number; + stickyOffset?: number; }; function Table({ @@ -65,11 +67,12 @@ function Table({ loading, page, rowHeight, + stickyOffset = 0, }: Props) { const { t } = useTranslation(); - const { height } = useWindowSize(); - const [containerHeight, setContainerHeight] = React.useState(0); - const containerRef = React.useRef(null); + const virtualContainerRef = React.useRef(null); + const [virtualContainerTop, setVirtualContainerTop] = + React.useState(); const columnHelper = React.useMemo(() => createColumnHelper(), []); const observedColumns = React.useMemo( @@ -130,37 +133,46 @@ function Table({ const { rows } = table.getRowModel(); - const rowVirtualizer = useVirtualizer({ + const rowVirtualizer = useWindowVirtualizer({ count: rows.length, estimateSize: () => rowHeight, - getScrollElement: () => containerRef.current, + scrollMargin: virtualContainerTop, overscan: 5, }); - React.useLayoutEffect(() => { - if (containerRef.current) { - setContainerHeight(height - containerRef.current.offsetTop); - } - }, [height]); - React.useEffect(() => { - rowVirtualizer.scrollToOffset?.(0, { behavior: "smooth" }); - }, [sortChanged, rowVirtualizer]); + if (!sortChanged || !virtualContainerTop) { + return; + } + + const scrollThreshold = + virtualContainerTop - (stickyOffset + HEADER_HEIGHT); + const reset = window.scrollY > scrollThreshold; + + if (reset) { + rowVirtualizer.scrollToOffset(scrollThreshold, { + behavior: "smooth", + }); + } + }, [rowVirtualizer, sortChanged, virtualContainerTop, stickyOffset]); + + React.useLayoutEffect(() => { + if (virtualContainerRef.current) { + // determine the scrollable virtual container offsetTop on mount + setVirtualContainerTop( + virtualContainerRef.current.getBoundingClientRect().top + ); + } + }, []); return ( - - - + <> + + {table.getHeaderGroups().map((headerGroup) => ( - + {headerGroup.headers.map((header) => ( - + ({
)} - + ))} - + ))} - + - {rowVirtualizer.getVirtualItems().map((virtualRow) => { const row = rows[virtualRow.index] as TRow; return ( - {row.getAllCells().map((cell) => ( - + {flexRender(cell.column.columnDef.cell, cell.getContext())} - + ))} - + ); })} - + {showPlaceholder && ( )} @@ -225,7 +239,7 @@ function Table({ /> )} {isEmpty && {t("No results")}} - + ); } @@ -250,17 +264,17 @@ function Placeholder({ }) { return ( - + {new Array(rows).fill(1).map((_r, row) => ( - + {new Array(columns).fill(1).map((_c, col) => ( - + - + ))} - + ))} - + ); } @@ -277,18 +291,6 @@ const AscSortIcon = styled(DescSortIcon)` transform: rotate(180deg); `; -const Container = styled.div<{ $height: number; $empty: boolean }>` - overflow: auto; - height: ${({ $height, $empty }) => !$empty && `${$height}px`}; - width: 100%; - margin-top: 16px; -`; - -const InnerTable = styled.table` - width: 100%; - border-collapse: collapse; -`; - const SortWrapper = styled(Flex)<{ $sortable: boolean }>` display: inline-flex; height: 24px; @@ -305,7 +307,53 @@ const SortWrapper = styled(Flex)<{ $sortable: boolean }>` } `; -const Cell = styled.td` +const InnerTable = styled.div` + width: 100%; +`; + +const THead = styled.div<{ $topPos: number }>` + position: sticky; + top: ${({ $topPos }) => `${$topPos}px`}; + height: ${HEADER_HEIGHT}px; + z-index: 1; + font-size: 14px; + color: ${s("textSecondary")}; + font-weight: 500; + + border-bottom: 1px solid ${s("divider")}; + background: ${s("background")}; +`; + +const TBody = styled.div<{ $height: number }>` + position: relative; + height: ${({ $height }) => `${$height}px`}; +`; + +const TR = styled.div<{ $columns: string }>` + width: 100%; + display: grid; + grid-template-columns: ${({ $columns }) => `${$columns}`}; + align-items: center; + border-bottom: 1px solid ${s("divider")}; + + &:last-child { + border-bottom: 0; + } +`; + +const TH = styled.span` + padding: 6px 6px 2px; + + &:first-child { + padding-left: 0; + } + + &:last-child { + padding-right: 0; + } +`; + +const TD = styled.span` padding: 10px 6px; font-size: 14px; text-wrap: wrap; @@ -314,6 +362,11 @@ const Cell = styled.td` &:first-child { font-size: 15px; font-weight: 500; + padding-left: 0; + } + + &:last-child { + padding-right: 0; } &.actions, @@ -336,44 +389,4 @@ const Cell = styled.td` } `; -const Row = styled.tr<{ $columns: string }>` - width: 100%; - display: grid; - grid-template-columns: ${({ $columns }) => `${$columns}`}; - align-items: center; - border-bottom: 1px solid ${s("divider")}; - - ${Cell} { - &:first-child { - padding-left: 0; - } - &:last-child { - padding-right: 0; - } - } - &:last-child { - border-bottom: 0; - } -`; - -const Head = styled.th` - height: 100%; - text-align: left; - padding: 6px 6px 2px; - border-bottom: 1px solid ${s("divider")}; - background: ${s("background")}; - font-size: 14px; - color: ${s("textSecondary")}; - font-weight: 500; - z-index: 1; - - :first-child { - padding-left: 0; - } - - :last-child { - padding-right: 0; - } -`; - export default observer(Table); diff --git a/app/scenes/Settings/Members.tsx b/app/scenes/Settings/Members.tsx index 8212331801..65e7980ef5 100644 --- a/app/scenes/Settings/Members.tsx +++ b/app/scenes/Settings/Members.tsx @@ -6,11 +6,13 @@ import { Trans, useTranslation } from "react-i18next"; import { useHistory, useLocation } from "react-router-dom"; import { toast } from "sonner"; import styled from "styled-components"; +import { depths, s } from "@shared/styles"; import UsersStore from "~/stores/UsersStore"; import { Action } from "~/components/Actions"; import Button from "~/components/Button"; import Fade from "~/components/Fade"; import Flex from "~/components/Flex"; +import { HEADER_HEIGHT } from "~/components/Header"; import Heading from "~/components/Heading"; import InputSearch from "~/components/InputSearch"; import Scene from "~/components/Scene"; @@ -146,7 +148,7 @@ function Members() { {{ signinMethods: team.signinMethods }} but haven’t signed in yet. - + - + , "columns" | "rowHeight"> & { canManage: boolean; }; @@ -95,7 +99,14 @@ export function PeopleTable({ canManage, ...rest }: Props) { [t, currentUser, canManage] ); - return ; + return ( + + ); } const Badges = styled(Flex)` diff --git a/app/scenes/Settings/components/SharesTable.tsx b/app/scenes/Settings/components/SharesTable.tsx index 7139180166..d046290f85 100644 --- a/app/scenes/Settings/components/SharesTable.tsx +++ b/app/scenes/Settings/components/SharesTable.tsx @@ -5,6 +5,7 @@ import { unicodeCLDRtoBCP47 } from "@shared/utils/date"; import Share from "~/models/Share"; import { Avatar } from "~/components/Avatar"; import Flex from "~/components/Flex"; +import { HEADER_HEIGHT } from "~/components/Header"; import { type Props as TableProps, SortableTable, @@ -15,6 +16,8 @@ import useUserLocale from "~/hooks/useUserLocale"; import ShareMenu from "~/menus/ShareMenu"; import { formatNumber } from "~/utils/language"; +const ROW_HEIGHT = 50; + type Props = Omit, "columns" | "rowHeight"> & { canManage: boolean; }; @@ -118,6 +121,12 @@ export function SharesTable({ data, canManage, ...rest }: Props) { ); return ( - + ); } diff --git a/app/scenes/Settings/components/UserRoleFilter.tsx b/app/scenes/Settings/components/UserRoleFilter.tsx index a7be6e3430..9e55604c36 100644 --- a/app/scenes/Settings/components/UserRoleFilter.tsx +++ b/app/scenes/Settings/components/UserRoleFilter.tsx @@ -42,6 +42,7 @@ const UserRoleFilter = ({ activeKey, onSelect, ...rest }: Props) => { selectedKeys={[activeKey]} onSelect={onSelect} defaultLabel={t("All roles")} + modalMenu={false} {...rest} /> ); diff --git a/app/scenes/Settings/components/UserStatusFilter.tsx b/app/scenes/Settings/components/UserStatusFilter.tsx index eabdf3c778..b80327f242 100644 --- a/app/scenes/Settings/components/UserStatusFilter.tsx +++ b/app/scenes/Settings/components/UserStatusFilter.tsx @@ -47,6 +47,7 @@ const UserStatusFilter = ({ activeKey, onSelect, ...rest }: Props) => { selectedKeys={[activeKey]} onSelect={onSelect} defaultLabel={t("Active")} + modalMenu={false} {...rest} /> );