Skip to content

Commit

Permalink
fix: tabs background may cover other elements
Browse files Browse the repository at this point in the history
  • Loading branch information
pengx17 committed Aug 30, 2021
1 parent 8f4de76 commit 16a0ac4
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 106 deletions.
1 change: 0 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"extends": [
"eslint:recommended",
"plugin:react/recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended"
],
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"@vitejs/plugin-react-refresh": "1.3.6",
"conventional-changelog-conventionalcommits": "4.6.0",
"eslint": "^7.32.0",
"eslint-plugin-react": "^7.24.0",
"eslint-plugin-react": "^7.25.1",
"typescript": "4.4.2",
"vite": "2.5.1",
"vite-plugin-windicss": "1.3.0",
Expand Down
42 changes: 27 additions & 15 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

168 changes: 86 additions & 82 deletions src/PageTabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
useAdaptMainUIStyle,
useEventCallback,
useOpeningPageTabs,
useScrollWidth,
} from "./utils";

const CloseSVG = () => (
Expand Down Expand Up @@ -59,90 +60,78 @@ const sortTabs = (tabs: ITabInfo[]) => {
return newTabs;
};

function Tabs({
activePage,
tabs,
onCloseTab,
onPinTab,
onSwapTab,
}: {
interface TabsProps {
tabs: ITabInfo[];
activePage: ITabInfo | null;
onCloseTab: (tab: ITabInfo, tabIdx: number) => void;
onPinTab: (tab: ITabInfo) => void;
onSwapTab: (tab: ITabInfo, anotherTab: ITabInfo) => void;
}) {
const ref = React.useRef<HTMLDivElement>(null);
React.useEffect(() => {
if (activePage) {
setTimeout(() => {
ref.current
?.querySelector(`[data-active]`)
?.scrollIntoView({ behavior: "smooth" });
}, 100);
}
}, [activePage]);
}

const [draggingTab, setDraggingTab] = React.useState<ITabInfo>();
const Tabs = React.forwardRef<HTMLElement, TabsProps>(
({ activePage, tabs, onCloseTab, onPinTab, onSwapTab }, ref) => {
const [draggingTab, setDraggingTab] = React.useState<ITabInfo>();

React.useEffect(() => {
const dragEndListener = () => {
setDraggingTab(undefined);
};
document.addEventListener("dragend", dragEndListener);
return () => {
document.removeEventListener("dragend", dragEndListener);
};
}, []);
React.useEffect(() => {
const dragEndListener = () => {
setDraggingTab(undefined);
};
document.addEventListener("dragend", dragEndListener);
return () => {
document.removeEventListener("dragend", dragEndListener);
};
}, []);

return (
<div
ref={ref}
className={`flex items-center h-full px-1`}
style={{ width: "fit-content" }}
>
{tabs.map((tab, idx) => {
const isActive = isTabEqual(tab, activePage);
const onClickTab = () =>
logseq.App.pushState("page", { name: tab.originalName });
const onClose: React.MouseEventHandler = (e) => {
e.stopPropagation();
onCloseTab(tab, idx);
};
const onDragOver: React.DragEventHandler = (e) => {
if (draggingTab) {
// Prevent drag fly back animation
e.preventDefault();
onSwapTab(tab, draggingTab);
}
};
return (
<div
onClick={onClickTab}
onDoubleClick={() => onPinTab(tab)}
key={tab.uuid}
data-active={isActive}
data-pinned={tab.pinned}
data-dragging={draggingTab === tab}
draggable={true}
onDragOver={onDragOver}
onDragStart={() => setDraggingTab(tab)}
className="logseq-tab"
>
<span className="logseq-tab-title">{tab.originalName}</span>
{tab.pinned ? (
<span>📌</span>
) : (
<button className="close-button" onClick={onClose}>
<CloseSVG />
</button>
)}
</div>
);
})}
</div>
);
}
return (
<div
// @ts-expect-error ???
ref={ref}
className={`flex items-center h-full px-1`}
style={{ width: "fit-content" }}
>
{tabs.map((tab, idx) => {
const isActive = isTabEqual(tab, activePage);
const onClickTab = () =>
logseq.App.pushState("page", { name: tab.originalName });
const onClose: React.MouseEventHandler = (e) => {
e.stopPropagation();
onCloseTab(tab, idx);
};
const onDragOver: React.DragEventHandler = (e) => {
if (draggingTab) {
// Prevent drag fly back animation
e.preventDefault();
onSwapTab(tab, draggingTab);
}
};
return (
<div
onClick={onClickTab}
onDoubleClick={() => onPinTab(tab)}
key={tab.uuid}
data-active={isActive}
data-pinned={tab.pinned}
data-dragging={draggingTab === tab}
draggable={true}
onDragOver={onDragOver}
onDragStart={() => setDraggingTab(tab)}
className="logseq-tab"
>
<span className="logseq-tab-title">{tab.originalName}</span>
{tab.pinned ? (
<span>📌</span>
) : (
<button className="close-button" onClick={onClose}>
<CloseSVG />
</button>
)}
</div>
);
})}
</div>
);
}
);

function isPageLink(element: HTMLElement) {
const el = element as HTMLAnchorElement;
Expand Down Expand Up @@ -172,9 +161,9 @@ function useCaptureAddPageAction(cb: (e: ITabInfo) => void) {
}
}
};
top.document.addEventListener("mousedown", listener, true);
top!.document.addEventListener("mousedown", listener, true);
return () => {
top.document.removeEventListener("mousedown", listener, true);
top!.document.removeEventListener("mousedown", listener, true);
};
}, [cb]);
}
Expand Down Expand Up @@ -217,10 +206,6 @@ export function PageTabs(): JSX.Element {
const [tabs, setTabs] = useOpeningPageTabs();
const activePage = useActivePage();

useAdaptMainUIStyle(
tabs.length > (activePage ? 1 : 0) || tabs.some((t) => t.pinned)
);

const onCloseTab = useEventCallback((tab: ITabInfo, idx?: number) => {
if (idx == null) {
idx = tabs.findIndex((t) => isTabEqual(t, tab));
Expand Down Expand Up @@ -323,8 +308,27 @@ export function PageTabs(): JSX.Element {
};
}, [onCloseTab]);

const ref = React.useRef<HTMLElement>(null);
const scrollWidth = useScrollWidth(ref);

useAdaptMainUIStyle(
tabs.length > (activePage ? 1 : 0) || tabs.some((t) => t.pinned),
scrollWidth
);

React.useEffect(() => {
if (activePage && ref) {
setTimeout(() => {
ref.current
?.querySelector(`[data-active]`)
?.scrollIntoView({ behavior: "smooth" });
}, 100);
}
}, [activePage, ref]);

return (
<Tabs
ref={ref}
activePage={activePage}
tabs={tabs}
onSwapTab={onSwapTab}
Expand Down
2 changes: 1 addition & 1 deletion src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function main() {

// @ts-expect-error
if (top[magicKey]) {
top.location.reload();
top!.location.reload();
}

logseq.ready(main).catch(console.error);
Loading

0 comments on commit 16a0ac4

Please sign in to comment.