Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

web: migrate move note dialog to typescript

Abdullah Atta9638b55f02f9
diff --git a/apps/web/src/components/dialogs/move-note-dialog.js b/apps/web/src/components/dialogs/move-note-dialog.js
deleted file mode 100644
index ff89b6ed8..000000000
--- a/apps/web/src/components/dialogs/move-note-dialog.js
+++ /dev/null
@@ -1,338 +0,0 @@
-/*
-This file is part of the Notesnook project (https://notesnook.com/)
-
-Copyright (C) 2022 Streetwriters (Private) Limited
-
-This program is free software: you can redistribute it and/or modify
-it under the terms of the GNU General Public License as published by
-the Free Software Foundation, either version 3 of the License, or
-(at your option) any later version.
-
-This program is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-GNU General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with this program. If not, see <http://www.gnu.org/licenses/>.
-*/
-
-import { useCallback, useEffect, useRef, useState } from "react";
-import { Button, Flex, Text } from "@theme-ui/components";
-import * as Icon from "../icons";
-import { db } from "../../common/db";
-import Dialog from "./dialog";
-import { showToast } from "../../utils/toast";
-import Field from "../field";
-import { store as notestore } from "../../stores/note-store";
-import { useStore, store } from "../../stores/notebook-store";
-import { getTotalNotes } from "../../common";
-import Accordion from "../accordion";
-import { pluralize } from "../../utils/string";
-
-function MoveDialog({ onClose, noteIds }) {
- const [selected, setSelected] = useState([]);
-
- const refreshNotebooks = useStore((store) => store.refresh);
- const getAllNotebooks = useCallback(() => {
- refreshNotebooks();
- return store.get().notebooks.filter((a) => a.type !== "header");
- }, [refreshNotebooks]);
-
- return (
- <Dialog
- isOpen={true}
- title={"Add to notebook"}
- description={"You can add a single note to multiple notebooks & topics."}
- onClose={onClose}
- width={"30%"}
- positiveButton={{
- text: "Finish",
- disabled: !selected.length,
- onClick: async () => {
- for (let item of selected) {
- try {
- if (item.type === "remove") {
- await db.notebooks
- .notebook(item.id)
- .topics.topic(item.topic)
- .delete(...noteIds);
- } else if (item.type === "add") {
- await db.notes.addToNotebook(item, ...noteIds);
- }
- } catch (e) {
- showToast("error", e.message);
- console.error(e);
- }
- }
- notestore.refresh();
- showToast(
- "success",
- `Added ${noteIds.length} notes to ${selected.length} topics`
- );
- onClose();
- }
- }}
- negativeButton={{
- text: "Cancel",
- onClick: onClose
- }}
- >
- <Flex mt={1} sx={{ overflowY: "hidden", flexDirection: "column" }}>
- <FilteredList
- testId="mnd-new-notebook-title"
- placeholders={{
- empty: "Add a new notebook",
- filter: "Filter notebooks"
- }}
- getAll={getAllNotebooks}
- filter={(notebooks, query) => db.lookup.notebooks(notebooks, query)}
- onCreateNewItem={(title) =>
- db.notebooks.add({
- title
- })
- }
- refreshItems={refreshNotebooks}
- itemName="notebook"
- renderItem={(notebook, index) => {
- const selectedTopics = selected.filter((s) => s.id === notebook.id);
- const hasNotes = notebook.topics.some((topic) =>
- topicHasNotes(topic, noteIds)
- );
- return (
- <Accordion
- key={notebook.id}
- testId={`notebook-${index}`}
- title={
- <Flex sx={{ px: 1, flexDirection: "column" }}>
- <Text
- variant={"body"}
- sx={{ color: hasNotes ? "primary" : "text" }}
- >
- {notebook.title}
- </Text>
- <Text variant={"subBody"} sx={{ fontWeight: "normal" }}>
- {getTotalNotes(notebook)} notes &amp;{" "}
- {notebook.topics.length} topics{" "}
- {selectedTopics.length
- ? ` & ${selectedTopics.length} selected`
- : ""}
- </Text>
- </Flex>
- }
- sx={{ mb: 1 }}
- >
- <FilteredList
- placeholders={{
- empty: "Add a new topic",
- filter: "Filter topics"
- }}
- testId="mnd-new-topic-title"
- itemName="topic"
- filter={(topics, query) => db.lookup.topics(topics, query)}
- getAll={() => db.notebooks.notebook(notebook).topics.all}
- onCreateNewItem={async (title) => {
- await db.notebooks.notebook(notebook).topics.add(title);
- }}
- refreshItems={refreshNotebooks}
- renderItem={(topic, topicIndex) => {
- const isSelected = selected.find(
- (s) => s.topic === topic.id
- );
- const hasNotes = topicHasNotes(topic, noteIds);
-
- return (
- <Item
- data-test-id={`notebook-${index}-topic-${topicIndex}`}
- key={topic.id}
- onClick={() => {
- const opType = topicHasNotes(topic, noteIds)
- ? "remove"
- : "add";
- setSelected((s) => {
- const copy = s.slice();
- const index = copy.findIndex(
- (i) => i.topic === topic.id
- );
- if (index === -1)
- copy.push({
- id: notebook.id,
- topic: topic.id,
- type: opType
- });
- else copy.splice(index, 1);
- return copy;
- });
- }}
- indent={1}
- icon={Icon.Topic}
- title={
- hasNotes ? (
- <Text as="span" sx={{ color: "primary" }}>
- {topic.title}
- </Text>
- ) : (
- topic.title
- )
- }
- totalNotes={topic.notes?.length || 0}
- action={
- isSelected && hasNotes ? (
- <Icon.Close
- title="Notes will be removed from this topic."
- size={16}
- color="error"
- />
- ) : hasNotes ? (
- <Icon.DoubleCheckmark
- title="Some of the notes are already in this topic."
- size={16}
- color="primary"
- />
- ) : isSelected ? (
- <Icon.Checkmark
- title="Notes will be added to this topic."
- size={16}
- color="primary"
- />
- ) : null
- }
- />
- );
- }}
- />
- </Accordion>
- );
- }}
- />
- </Flex>
- </Dialog>
- );
-}
-
-function topicHasNotes(topic, noteIds) {
- return noteIds.some((id) => topic.notes && topic.notes.indexOf(id) > -1);
-}
-
-function FilteredList({
- placeholders,
- testId,
- getAll,
- filter,
- onCreateNewItem,
- renderItem,
- refreshItems,
- itemName
-}) {
- const [items, setItems] = useState([]);
- const [query, setQuery] = useState();
- const noItemsFound = items.length <= 0 && query?.length > 0;
- const inputRef = useRef();
-
- const refresh = useCallback(() => {
- refreshItems();
- setItems(getAll());
- }, [refreshItems, getAll]);
-
- useEffect(() => {
- refresh();
- }, [refresh]);
-
- const _filter = useCallback(
- (query) => {
- setItems(() => {
- const items = getAll();
- if (!query) {
- return items;
- }
- return filter(items, query);
- });
- setQuery(query);
- },
- [getAll, filter]
- );
-
- const _createNewItem = useCallback(
- async (title) => {
- await onCreateNewItem(title);
- refresh();
- setQuery();
- inputRef.current.value = "";
- },
- [inputRef, refresh, onCreateNewItem]
- );
-
- return (
- <>
- <Field
- inputRef={inputRef}
- data-test-id={testId}
- autoFocus
- placeholder={
- items.length <= 0 ? placeholders.empty : placeholders.filter
- }
- onChange={(e) => {
- _filter(e.target.value);
- }}
- onKeyUp={async (e) => {
- if (e.key === "Enter" && noItemsFound) {
- await _createNewItem(query);
- }
- }}
- action={
- items.length <= 0
- ? {
- icon: Icon.Plus,
- onClick: async () => await _createNewItem(query)
- }
- : { icon: Icon.Search, onClick: () => _filter(query) }
- }
- />
- <Flex mt={1} sx={{ overflowY: "hidden", flexDirection: "column" }}>
- {noItemsFound && (
- <Button
- variant={"secondary"}
- sx={{
- display: "flex",
- justifyContent: "space-between",
- alignItems: "center",
- py: 2
- }}
- onClick={async () => {
- await _createNewItem(query);
- }}
- >
- <Text variant={"body"}>{`Add "${query}" ${itemName}`}</Text>
- <Icon.Plus size={16} color="primary" />
- </Button>
- )}
- {items.map(renderItem)}
- </Flex>
- </>
- );
-}
-export default MoveDialog;
-
-function Item(props) {
- const { indent = 0, title, totalNotes, onClick, action } = props;
- return (
- <Button
- variant={"list"}
- data-test-id={props["data-test-id"]}
- p={1}
- pl={!indent ? 2 : indent * 15}
- onClick={onClick}
- sx={{
- display: "flex",
- alignItems: "center",
- justifyContent: "space-between"
- }}
- >
- <Flex sx={{ flexDirection: "column" }}>
- <Text variant="body">{title}</Text>
- <Text variant="subBody">{pluralize(totalNotes, "note", "notes")}</Text>
- </Flex>
- {action}
- </Button>
- );
-}
diff --git a/apps/web/src/components/dialogs/move-note-dialog.tsx b/apps/web/src/components/dialogs/move-note-dialog.tsx
new file mode 100644
index 000000000..70cfb0d85
--- /dev/null
+++ b/apps/web/src/components/dialogs/move-note-dialog.tsx
@@ -0,0 +1,415 @@
+/*
+This file is part of the Notesnook project (https://notesnook.com/)
+
+Copyright (C) 2022 Streetwriters (Private) Limited
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+import { ChangeEvent, useCallback, useEffect, useRef, useState } from "react";
+import { Button, Flex, Input, Text } from "@theme-ui/components";
+import * as Icon from "../icons";
+import { db } from "../../common/db";
+import Dialog from "./dialog";
+import Field from "../field";
+import { useStore, store } from "../../stores/notebook-store";
+import { store as notestore } from "../../stores/note-store";
+import { Perform } from "../../common/dialog-controller";
+import { ThemeUIStyleObject } from "@theme-ui/core";
+import { showToast } from "../../utils/toast";
+import { pluralize } from "../../utils/string";
+
+type MoveDialogProps = { onClose: Perform; noteIds: string[] };
+type NotebookReference = { id: string; topic: string; type: "add" | "remove" };
+type Item = {
+ id: string;
+ type: "topic" | "notebook" | "header";
+ title: string;
+};
+type Notebook = Item & { topics: Item[] };
+
+function MoveDialog({ onClose, noteIds }: MoveDialogProps) {
+ const [selected, setSelected] = useState<NotebookReference[]>([]);
+
+ const refreshNotebooks = useStore((store) => store.refresh);
+ const getAllNotebooks = useCallback(() => {
+ refreshNotebooks();
+ return (store.get().notebooks as Notebook[]).filter(
+ (a) => a.type !== "header"
+ );
+ }, [refreshNotebooks]);
+
+ return (
+ <Dialog
+ isOpen={true}
+ title={"Add to notebook"}
+ description={"You can add a single note to multiple notebooks & topics."}
+ onClose={onClose}
+ width={"30%"}
+ positiveButton={{
+ text: "Finish",
+ disabled: !selected.length,
+ onClick: async () => {
+ for (const item of selected) {
+ try {
+ if (item.type === "remove") {
+ await db.notes?.removeFromNotebook(item, ...noteIds);
+ } else if (item.type === "add") {
+ await db.notes?.addToNotebook(item, ...noteIds);
+ }
+ } catch (e) {
+ if (e instanceof Error) showToast("error", e.message);
+ console.error(e);
+ }
+ }
+ notestore.refresh();
+ showToast(
+ "success",
+ `Added ${pluralize(noteIds.length, "note", "notes")} to ${pluralize(
+ selected.filter((a) => a.type === "add").length,
+ "topic",
+ "topics"
+ )}`
+ );
+ onClose(true);
+ }
+ }}
+ negativeButton={{
+ text: "Cancel",
+ onClick: onClose
+ }}
+ >
+ <Flex
+ mt={1}
+ sx={{ overflowY: "hidden", flexDirection: "column" }}
+ data-test-id="notebook-list"
+ >
+ <FilteredList
+ placeholders={{
+ empty: "Add a new notebook",
+ filter: "Filter notebooks"
+ }}
+ items={getAllNotebooks}
+ filter={(notebooks, query) =>
+ db.lookup?.notebooks(notebooks, query) || []
+ }
+ onCreateNewItem={async (title) =>
+ await db.notebooks?.add({
+ title
+ })
+ }
+ renderItem={(notebook, _index, refresh) => {
+ return (
+ <TreeNode
+ item={notebook}
+ items={notebook.topics}
+ isSelected={(item) => {
+ if (item.type === "notebook") {
+ return notebook.topics.some((topic) =>
+ topicHasNotes(topic, noteIds)
+ );
+ } else if (item.type === "topic") {
+ const selectedItem = selected.find(
+ (t) => t.topic === item.id
+ );
+ if (selectedItem?.type === "remove") return false;
+ return (
+ selectedItem?.type === "add" ||
+ topicHasNotes(item, noteIds)
+ );
+ }
+ return false;
+ }}
+ onSelected={(topic) => {
+ if (!topic) return;
+
+ const opType = topicHasNotes(topic, noteIds)
+ ? "remove"
+ : "add";
+ setSelected((s) => {
+ const copy = s.slice();
+ const index = copy.findIndex((i) => i.topic === topic.id);
+ if (index === -1)
+ copy.push({
+ id: notebook.id,
+ topic: topic.id,
+ type: opType
+ });
+ else copy.splice(index, 1);
+ return copy;
+ });
+ }}
+ onCreateItem={async (title) => {
+ await db.notebooks?.notebook(notebook.id).topics.add(title);
+ refresh();
+ }}
+ />
+ );
+ }}
+ />
+ </Flex>
+ </Dialog>
+ );
+}
+
+function topicHasNotes(topic: Item, noteIds: string[]) {
+ const notes: string[] = db.notes?.topicReferences.get(topic.id) || [];
+ return noteIds.some((id) => notes.indexOf(id) > -1);
+}
+
+type FilteredListProps<T extends Item> = {
+ placeholders: { filter: string; empty: string };
+ items: () => T[];
+ filter: (items: T[], query: string) => T[];
+ onCreateNewItem: (title: string) => Promise<void>;
+ renderItem: (item: T, index: number, refresh: () => void) => JSX.Element;
+};
+
+function FilteredList<T extends Item>(props: FilteredListProps<T>) {
+ const {
+ items: _items,
+ filter,
+ onCreateNewItem,
+ placeholders,
+ renderItem
+ } = props;
+
+ const [items, setItems] = useState<T[]>([]);
+ const [query, setQuery] = useState<string>();
+ const noItemsFound = items.length <= 0 && query && query.length > 0;
+ const inputRef = useRef<HTMLInputElement>(null);
+
+ const refresh = useCallback(() => {
+ setItems(_items());
+ }, [_items]);
+
+ useEffect(() => {
+ refresh();
+ }, [refresh]);
+
+ const _filter = useCallback(
+ (query) => {
+ setItems(() => {
+ const items = _items();
+ if (!query) {
+ return items;
+ }
+ return filter(items, query);
+ });
+ setQuery(query);
+ },
+ [_items, filter]
+ );
+
+ const _createNewItem = useCallback(
+ async (title) => {
+ await onCreateNewItem(title);
+ refresh();
+ setQuery(undefined);
+ if (inputRef.current) inputRef.current.value = "";
+ },
+ [inputRef, refresh, onCreateNewItem]
+ );
+
+ return (
+ <>
+ <Field
+ inputRef={inputRef}
+ data-test-id={"filter-input"}
+ autoFocus
+ placeholder={
+ items.length <= 0 ? placeholders.empty : placeholders.filter
+ }
+ onChange={(e: ChangeEvent) =>
+ _filter((e.target as HTMLInputElement).value)
+ }
+ onKeyUp={async (e: KeyboardEvent) => {
+ if (e.key === "Enter" && noItemsFound) {
+ await _createNewItem(query);
+ }
+ }}
+ action={
+ items.length <= 0
+ ? {
+ icon: Icon.Plus,
+ onClick: async () => await _createNewItem(query)
+ }
+ : { icon: Icon.Search, onClick: () => _filter(query) }
+ }
+ />
+ <Flex mt={1} sx={{ overflowY: "hidden", flexDirection: "column" }}>
+ {noItemsFound && (
+ <Button
+ variant={"secondary"}
+ sx={{
+ display: "flex",
+ justifyContent: "space-between",
+ alignItems: "center",
+ py: 2
+ }}
+ onClick={async () => {
+ await _createNewItem(query);
+ }}
+ >
+ <Text variant={"body"}>{`Add "${query}"`}</Text>
+ <Icon.Plus size={16} color="primary" />
+ </Button>
+ )}
+ {items.map((item, index) => renderItem(item, index, refresh))}
+ </Flex>
+ </>
+ );
+}
+export default MoveDialog;
+
+type TreeNodeProps<T extends Item> = {
+ item: T;
+ items?: T[];
+ sx?: ThemeUIStyleObject;
+ isSelected: (item: T) => boolean;
+ onSelected: (item: T) => void;
+ onCreateItem?: (title: string) => void;
+};
+
+function TreeNode<T extends Item>(props: TreeNodeProps<T>) {
+ const { isSelected, item, items, onCreateItem, onSelected, sx } = props;
+
+ const [expanded, setExpanded] = useState(false);
+ const [isCreatingNew, setIsCreatingNew] = useState(false);
+
+ return (
+ <Flex sx={{ flexDirection: "column" }} data-test-id={item.type}>
+ <TreeItem
+ title={item.title}
+ testId={`tree-item`}
+ icon={
+ items ? (expanded ? Icon.ChevronDown : Icon.ChevronRight) : Icon.Topic
+ }
+ sx={{
+ bg: isSelected(item) ? "shade" : "transparent",
+ ...sx
+ }}
+ onExpand={() => {
+ if (items) {
+ setExpanded((s) => !s);
+ return;
+ }
+ onSelected(item);
+ }}
+ onCreateItem={
+ items && onCreateItem
+ ? () => {
+ setExpanded(true);
+ setIsCreatingNew(true);
+ }
+ : null
+ }
+ />
+ {expanded ? (
+ <>
+ {isCreatingNew ? (
+ <Flex sx={{ pl: 3, my: 1 }}>
+ <Input
+ data-test-id={`new-tree-item-input`}
+ autoFocus
+ sx={{ bg: "background", p: 1 }}
+ placeholder="Press enter to create"
+ onBlur={() => setIsCreatingNew(false)}
+ onKeyDown={(e) => {
+ e.stopPropagation();
+ e.preventDefault();
+
+ if (e.key === "Enter") {
+ setIsCreatingNew(false);
+ onCreateItem?.((e.target as HTMLInputElement).value);
+ } else if (e.key === "Escape") {
+ setIsCreatingNew(false);
+ }
+ }}
+ />
+ </Flex>
+ ) : null}
+ {items?.map((item) => (
+ <TreeNode
+ key={item.id}
+ item={item}
+ isSelected={isSelected}
+ onSelected={() => onSelected(item)}
+ sx={{ pl: 3 }}
+ />
+ ))}
+ </>
+ ) : null}
+ </Flex>
+ );
+}
+
+type TreeItemProps = {
+ title: string;
+ onExpand: () => void;
+ icon: (props: { size: number }) => JSX.Element;
+ sx?: ThemeUIStyleObject;
+ onCreateItem?: (() => void) | null;
+ testId?: string;
+};
+
+function TreeItem(props: TreeItemProps) {
+ const {
+ icon: TreeItemIcon,
+ onCreateItem,
+ onExpand,
+ title,
+ sx,
+ testId
+ } = props;
+
+ return (
+ <Flex
+ sx={{
+ alignItems: "center",
+ p: "3px",
+ cursor: "pointer",
+ ":hover": { bg: "hover" },
+ justifyContent: "space-between",
+ ...sx
+ }}
+ onClick={onExpand}
+ data-test-id={testId}
+ >
+ <Flex>
+ {TreeItemIcon && <TreeItemIcon size={18} />}
+ <Text
+ variant={"body"}
+ sx={{ fontSize: "subtitle" }}
+ data-test-id="title"
+ >
+ {title}
+ </Text>
+ </Flex>
+ {onCreateItem && (
+ <Button
+ variant="tool"
+ sx={{ p: "2px" }}
+ onClick={(e) => {
+ e.stopPropagation();
+ onCreateItem();
+ }}
+ data-test-id="tree-item-new"
+ >
+ <Icon.Plus size={18} />
+ </Button>
+ )}
+ </Flex>
+ );
+}
diff --git a/apps/web/src/components/list-container/index.tsx b/apps/web/src/components/list-container/index.tsx
index c6d31f137..47adf4c8f 100644
--- a/apps/web/src/components/list-container/index.tsx
+++ b/apps/web/src/components/list-container/index.tsx
@@ -17,10 +17,10 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-import { useEffect, useMemo, useRef, useState } from "react";
+import { forwardRef, useEffect, useMemo, useRef, useState } from "react";
import { Flex, Button } from "@theme-ui/components";
import * as Icon from "../icons";
-import { Virtuoso, VirtuosoHandle } from "react-virtuoso";
+import { ScrollerProps, Virtuoso, VirtuosoHandle } from "react-virtuoso";
import {
useStore as useSelectionStore,
store as selectionStore
@@ -32,11 +32,25 @@ import {
Item,
ListProfiles
} from "./list-profiles";
-import { CustomScrollbarsVirtualList } from "../scroll-container";
import ReminderBar from "../reminder-bar";
import Announcements from "../announcements";
import useAnnouncements from "../../hooks/use-announcements";
import { ListLoader } from "../loaders/list-loader";
+import ScrollContainer from "../scroll-container";
+
+const CustomScrollbarsVirtualList = forwardRef<HTMLDivElement, ScrollerProps>(
+ function CustomScrollbarsVirtualList(props, ref) {
+ return (
+ <ScrollContainer
+ {...props}
+ forwardedRef={(sRef) => {
+ if (typeof ref === "function") ref(sRef);
+ else if (ref) ref.current = sRef;
+ }}
+ />
+ );
+ }
+);
type ListContainerProps = {
type: keyof typeof ListProfiles;
diff --git a/apps/web/src/components/scroll-container/index.js b/apps/web/src/components/scroll-container/index.tsx
similarity index 65%
rename from apps/web/src/components/scroll-container/index.js
rename to apps/web/src/components/scroll-container/index.tsx
index f16d1073e..cc8d53043 100644
--- a/apps/web/src/components/scroll-container/index.js
+++ b/apps/web/src/components/scroll-container/index.tsx
@@ -17,18 +17,30 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-import React, { useRef } from "react";
+import React, { PropsWithChildren, useRef } from "react";
import { Box } from "@theme-ui/components";
import { Scrollbars } from "rc-scrollbars";
-const ScrollContainer = ({ children, style, forwardedRef, ...props }) => {
- const ref = useRef();
+type ScrollContainerProps = {
+ style?: React.CSSProperties;
+ forwardedRef?: (ref: HTMLDivElement | null) => void;
+};
+
+const ScrollContainer = ({
+ children,
+ style,
+ forwardedRef,
+ ...props
+}: PropsWithChildren<ScrollContainerProps>) => {
+ const ref = useRef<Scrollbars>();
+
return (
<Scrollbars
{...props}
autoHide
ref={(sRef) => {
- forwardedRef && sRef && forwardedRef(sRef.view);
+ if (!sRef) return;
+ forwardedRef && forwardedRef((sRef.view as HTMLDivElement) || null);
ref.current = sRef;
}}
style={{ ...style, overflowY: "hidden" }}
@@ -37,13 +49,25 @@ const ScrollContainer = ({ children, style, forwardedRef, ...props }) => {
{...props}
style={{ ...style, inset: "-1px" }}
onMouseEnter={() => {
- const height = ref.current.getThumbVerticalHeight();
- const width = ref.current.getThumbHorizontalWidth();
- if (height !== ref.current.thumbVertical.style.height)
- ref.current.thumbVertical.style.height = `${height}px`;
- else if (ref.current.thumbHorizontal.style.width !== width) {
- ref.current.thumbHorizontal.style.width = `${width}px`;
- }
+ if (
+ !ref.current ||
+ !ref.current.thumbVertical ||
+ !ref.current.thumbHorizontal
+ )
+ return;
+ const {
+ thumbHorizontal,
+ thumbVertical,
+ getThumbHorizontalWidth,
+ getThumbVerticalHeight
+ } = ref.current;
+
+ const height = `${getThumbVerticalHeight()}px`;
+ const width = `${getThumbHorizontalWidth()}px`;
+ if (height !== thumbVertical.style.height)
+ thumbVertical.style.height = height;
+ else if (thumbHorizontal.style.width !== width)
+ thumbHorizontal.style.width = width;
}}
/>
)}
@@ -63,16 +87,20 @@ const ScrollContainer = ({ children, style, forwardedRef, ...props }) => {
};
export default ScrollContainer;
+type FlexScrollContainerProps = {
+ className?: string;
+ style?: React.CSSProperties;
+ viewStyle?: React.CSSProperties;
+};
+
export function FlexScrollContainer({
children,
className,
style,
- viewStyle,
- ...props
-}) {
+ viewStyle
+}: PropsWithChildren<FlexScrollContainerProps>) {
return (
<Scrollbars
- {...props}
autoHide
style={{
overflowY: "hidden",
@@ -88,7 +116,7 @@ export function FlexScrollContainer({
{...props}
className={className}
style={{
- overflow: "auto",
+ overflow: "scroll",
position: "relative",
flex: "1 1 auto",
...viewStyle
@@ -114,9 +142,3 @@ export function FlexScrollContainer({
</Scrollbars>
);
}
-
-export const CustomScrollbarsVirtualList = React.forwardRef((props, ref) => {
- return (
- <ScrollContainer {...props} forwardedRef={(sRef) => (ref.current = sRef)} />
- );
-});